edb.go 26 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736
  1. package data_manage_permission
  2. import (
  3. "eta_gn/eta_api/global"
  4. "eta_gn/eta_api/utils"
  5. "fmt"
  6. "strconv"
  7. "time"
  8. )
  9. type EdbInfoPermission struct {
  10. EdbInfoPermissionId int64 `json:"edb_info_permission_id" orm:"column(edb_info_permission_id);pk" gorm:"primaryKey" `
  11. EdbInfoId int32 `json:"edb_info_id"` // 指标id
  12. EdbInfoType int32 `json:"edb_info_type"` // 指标类型,0:普通指标,1:预测指标
  13. SysUserId int32 `json:"sys_user_id"` // 系统用户id
  14. ModifyTime time.Time `json:"modify_time"` // 变更时间
  15. CreateTime time.Time `json:"create_time"` // 关系建立时间
  16. }
  17. type EdbClassifyPermission struct {
  18. EdbClassifyPermissionId int64 `json:"edb_classify_permission_id" orm:"column(edb_classify_permission_id);pk" gorm:"primaryKey" `
  19. EdbClassifyId int32 `json:"edb_classify_id"` // 分类id
  20. EdbClassifyType int32 `json:"edb_classify_type"` // 分类类型,0:普通指标分类,1:预测指标分类
  21. SysUserId int32 `json:"sys_user_id"` // 系统用户id
  22. ModifyTime time.Time `json:"modify_time"` // 变更时间
  23. CreateTime time.Time `json:"create_time"` // 关系建立时间
  24. }
  25. func SetIsPermissionEdbChartByEdbClassifyIdList(classifyIdList []int, classifyType int) (err error) {
  26. num := len(classifyIdList)
  27. to := global.DmSQL["data"].Begin()
  28. defer func() {
  29. if err != nil {
  30. _ = to.Rollback()
  31. } else {
  32. _ = to.Commit()
  33. }
  34. }()
  35. sql := `UPDATE edb_classify SET is_join_permission=?,modify_time=now() WHERE is_join_permission = 1 AND classify_type = ?`
  36. err = to.Exec(sql, 0, classifyType).Error
  37. if err != nil {
  38. return
  39. }
  40. if num > 0 {
  41. sql = `UPDATE edb_classify SET is_join_permission=?,modify_time=now() WHERE classify_type = ? AND classify_id in (` + utils.GetOrmInReplace(num) + `) `
  42. err = to.Exec(sql, 1, classifyType, classifyIdList).Error
  43. if err != nil {
  44. return
  45. }
  46. }
  47. return
  48. }
  49. func SetPermissionByEdbIdList(edbIdList []string, userIdList []int, edbInfoType int) (err error) {
  50. edbNum := len(edbIdList)
  51. if edbNum <= 0 {
  52. return
  53. }
  54. to := global.DmSQL["data"].Begin()
  55. defer func() {
  56. if err != nil {
  57. _ = to.Rollback()
  58. } else {
  59. _ = to.Commit()
  60. }
  61. }()
  62. edbInfoPermissionList := make([]*EdbInfoPermission, 0)
  63. sql := `SELECT * FROM edb_info_permission WHERE edb_info_type = ? AND edb_info_id in (` + utils.GetOrmInReplace(edbNum) + `) `
  64. err = to.Raw(sql, edbInfoType, edbIdList).Find(&edbInfoPermissionList).Error
  65. if err != nil {
  66. return
  67. }
  68. edbInfoPermissionMap := make(map[string]*EdbInfoPermission)
  69. for _, v := range edbInfoPermissionList {
  70. edbInfoPermissionMap[fmt.Sprint(v.EdbInfoId, "_", v.SysUserId)] = v
  71. }
  72. {
  73. isJoinPermission := 1
  74. if len(userIdList) <= 0 {
  75. isJoinPermission = 0
  76. }
  77. sql = `UPDATE edb_info SET is_join_permission=?,modify_time=now() WHERE edb_info_type = ? AND edb_info_id in (?) `
  78. err = to.Exec(sql, isJoinPermission, edbInfoType, edbIdList).Error
  79. if err != nil {
  80. return
  81. }
  82. }
  83. addList := make([]*EdbInfoPermission, 0)
  84. for _, edbInfoIdStr := range edbIdList {
  85. edbInfoId, tmpErr := strconv.ParseInt(edbInfoIdStr, 10, 64)
  86. if tmpErr != nil {
  87. err = tmpErr
  88. return
  89. }
  90. for _, userId := range userIdList {
  91. key := fmt.Sprint(edbInfoId, "_", userId)
  92. if _, ok := edbInfoPermissionMap[key]; ok {
  93. delete(edbInfoPermissionMap, key)
  94. } else {
  95. addList = append(addList, &EdbInfoPermission{
  96. EdbInfoId: int32(edbInfoId),
  97. SysUserId: int32(userId),
  98. EdbInfoType: int32(edbInfoType),
  99. ModifyTime: time.Now(),
  100. CreateTime: time.Now(),
  101. })
  102. }
  103. }
  104. }
  105. if len(addList) > 0 {
  106. err = to.CreateInBatches(addList, utils.MultiAddNum).Error
  107. if err != nil {
  108. return
  109. }
  110. }
  111. {
  112. deletePermissionIdList := make([]int64, 0)
  113. for _, v := range edbInfoPermissionMap {
  114. deletePermissionIdList = append(deletePermissionIdList, v.EdbInfoPermissionId)
  115. }
  116. deletePermissionIdNum := len(deletePermissionIdList)
  117. if deletePermissionIdNum > 0 {
  118. sql = `DELETE FROM edb_info_permission WHERE edb_info_permission_id in (?)`
  119. err = to.Exec(sql, deletePermissionIdList).Error
  120. if err != nil {
  121. return
  122. }
  123. }
  124. }
  125. return
  126. }
  127. func SetPermissionByEdbClassifyIdList(classifyIdList []int, userIdList []int, classifyType int) (err error) {
  128. userNum := len(userIdList)
  129. if userNum <= 0 {
  130. return
  131. }
  132. to := global.DmSQL["data"].Begin()
  133. defer func() {
  134. if err != nil {
  135. _ = to.Rollback()
  136. } else {
  137. _ = to.Commit()
  138. }
  139. }()
  140. classifyPermissionList := make([]*EdbClassifyPermission, 0)
  141. sql := `SELECT * FROM edb_classify_permission WHERE edb_classify_type = ? AND sys_user_id in (` + utils.GetOrmInReplace(userNum) + `) `
  142. err = to.Raw(sql, classifyType, userIdList).Find(&classifyPermissionList).Error
  143. if err != nil {
  144. return
  145. }
  146. classifyPermissionMap := make(map[string]*EdbClassifyPermission)
  147. for _, v := range classifyPermissionList {
  148. classifyPermissionMap[fmt.Sprint(v.EdbClassifyId, "_", v.SysUserId)] = v
  149. }
  150. addList := make([]*EdbClassifyPermission, 0)
  151. for _, userId := range userIdList {
  152. for _, classifyId := range classifyIdList {
  153. key := fmt.Sprint(classifyId, "_", userId)
  154. if _, ok := classifyPermissionMap[key]; ok {
  155. delete(classifyPermissionMap, key)
  156. } else {
  157. addList = append(addList, &EdbClassifyPermission{
  158. EdbClassifyId: int32(classifyId),
  159. EdbClassifyType: int32(classifyType),
  160. SysUserId: int32(userId),
  161. ModifyTime: time.Now(),
  162. CreateTime: time.Now(),
  163. })
  164. }
  165. }
  166. }
  167. if len(addList) > 0 {
  168. err = to.CreateInBatches(addList, utils.MultiAddNum).Error
  169. if err != nil {
  170. return
  171. }
  172. }
  173. {
  174. deletePermissionIdList := make([]int64, 0)
  175. for _, v := range classifyPermissionMap {
  176. deletePermissionIdList = append(deletePermissionIdList, v.EdbClassifyPermissionId)
  177. }
  178. deletePermissionIdNum := len(deletePermissionIdList)
  179. if deletePermissionIdNum > 0 {
  180. sql = "DELETE FROM edb_classify_permission WHERE edb_classify_permission_id in (" + utils.GetOrmInReplace(deletePermissionIdNum) + ")"
  181. err = to.Exec(sql, deletePermissionIdList).Error
  182. if err != nil {
  183. return
  184. }
  185. }
  186. }
  187. return
  188. }
  189. func GetPermissionEdbClassifyIdListByUserId(userId int, classifyType int) (edbClassifyIdList []int, err error) {
  190. sql := `SELECT edb_classify_id FROM edb_classify_permission WHERE edb_classify_type = ? AND sys_user_id = ? `
  191. err = global.DmSQL["data"].Raw(sql, classifyType, userId).Find(&edbClassifyIdList).Error
  192. return
  193. }
  194. func GetPermissionUserIdListByEdbId(dataId int, edbInfoType int) (userIdList []int, err error) {
  195. sql := `SELECT sys_user_id FROM edb_info_permission WHERE edb_info_type = ? AND edb_info_id= ? `
  196. err = global.DmSQL["data"].Raw(sql, edbInfoType, dataId).Find(&userIdList).Error
  197. return
  198. }
  199. func GetPermissionUserIdListByEdbClassifyId(classifyId int, edbClassifyType int) (userIdList []int, err error) {
  200. sql := `SELECT sys_user_id FROM edb_classify_permission WHERE edb_classify_type = ? AND edb_classify_id= ? `
  201. err = global.DmSQL["data"].Raw(sql, edbClassifyType, classifyId).Find(&userIdList).Error
  202. return
  203. }
  204. func GetPermissionEdbIdList(userId, edbInfoId int) (idList []int, err error) {
  205. pars := []interface{}{userId}
  206. sql := `SELECT edb_info_id FROM edb_info_permission WHERE sys_user_id = ? `
  207. if edbInfoId > 0 {
  208. sql += ` AND edb_info_id = ? `
  209. pars = append(pars, edbInfoId)
  210. }
  211. err = global.DmSQL["data"].Raw(sql, pars...).Find(&idList).Error
  212. return
  213. }
  214. func GetPermissionEdbClassifyIdList(userId, classifyId int) (idList []int, err error) {
  215. pars := []interface{}{userId}
  216. sql := `SELECT edb_classify_id FROM edb_classify_permission WHERE sys_user_id = ? `
  217. if classifyId > 0 {
  218. sql += ` AND edb_classify_id = ? `
  219. pars = append(pars, classifyId)
  220. }
  221. err = global.DmSQL["data"].Raw(sql, pars...).Find(&idList).Error
  222. return
  223. }
  224. func InheritParentClassifyByEdbClassifyId(source, classifyType, classifyId, parentClassifyId int, classifyName, uniqueCode string) (err error) {
  225. to := global.DmSQL["data"].Begin()
  226. defer func() {
  227. if err != nil {
  228. _ = to.Rollback()
  229. } else {
  230. _ = to.Commit()
  231. }
  232. }()
  233. sql := `UPDATE edb_classify SET is_join_permission=?,modify_time=now() WHERE classify_type = ? AND classify_id = ? `
  234. err = to.Exec(sql, 1, classifyType, classifyId).Error
  235. if err != nil {
  236. return
  237. }
  238. {
  239. var parentRecordItems []*EdbInfoClassifyPermissionNoAuthRecord
  240. sql = `SELECT * FROM edb_info_classify_permission_no_auth_record WHERE classify_id = ? AND edb_classify_type = ? ORDER BY edb_info_classify_permission_no_auth_record_id desc LIMIT ?,? `
  241. err = to.Raw(sql, parentClassifyId, source, classifyType).Find(&parentRecordItems).Error
  242. addNoAuthRecordItems := make([]*EdbInfoClassifyPermissionNoAuthRecord, 0)
  243. for _, v := range parentRecordItems {
  244. addNoAuthRecordItems = append(addNoAuthRecordItems, &EdbInfoClassifyPermissionNoAuthRecord{
  245. EdbInfoClassifyPermissionNoAuthRecordId: 0,
  246. EdbClassifyType: v.EdbClassifyType,
  247. OpUniqueCode: uniqueCode,
  248. ClassifyId: fmt.Sprint(classifyId),
  249. ClassifyName: classifyName,
  250. SysUserId: v.SysUserId,
  251. CreateTime: time.Now(),
  252. })
  253. }
  254. if len(addNoAuthRecordItems) > 0 {
  255. err = to.CreateInBatches(addNoAuthRecordItems, utils.MultiAddNum).Error
  256. if err != nil {
  257. return
  258. }
  259. }
  260. }
  261. {
  262. parentClassifyPermissionList := make([]*EdbClassifyPermission, 0)
  263. sql = `SELECT * FROM edb_classify_permission WHERE edb_classify_type = ? AND edb_classify_id = ? `
  264. err = to.Raw(sql, classifyType, parentClassifyId).Find(&parentClassifyPermissionList).Error
  265. if err != nil {
  266. return
  267. }
  268. addList := make([]*EdbClassifyPermission, 0)
  269. for _, v := range parentClassifyPermissionList {
  270. addList = append(addList, &EdbClassifyPermission{
  271. EdbClassifyId: int32(classifyId),
  272. EdbClassifyType: int32(classifyType),
  273. SysUserId: v.SysUserId,
  274. ModifyTime: time.Now(),
  275. CreateTime: time.Now(),
  276. })
  277. }
  278. if len(addList) > 0 {
  279. err = to.CreateInBatches(addList, utils.MultiAddNum).Error
  280. if err != nil {
  281. return
  282. }
  283. }
  284. }
  285. return
  286. }
  287. type EdbInfoPermissionNoAuthRecord struct {
  288. EdbInfoPermissionNoAuthRecordId int64 `json:"edb_info_permission_no_auth_record_id" orm:"column(edb_info_permission_no_auth_record_id);pk" gorm:"primaryKey" ` // 资产数据操作记录id
  289. OpUniqueCode string `json:"op_unique_code"` // 操作的唯一编码,主要是记录统一操作的日志
  290. EdbInfoType int32 `json:"edb_info_type"` // 指标类型,0:普通指标,1:预测指标
  291. EdbInfoId int32 `json:"edb_info_id"` // 指标id
  292. EdbCode string `json:"edb_code"` // 指标编码
  293. EdbName string `json:"edb_name"` // 指标名称
  294. SysUserId int32 `json:"sys_user_id"` // 系统用户id
  295. CreateTime time.Time `json:"create_time"` // 创建时间
  296. }
  297. func AddEdbInfoPermissionNoAuthRecordBySourceAndDataIdList(source, edbInfoType int, dataList []DataItem, noAuthUserIdList, authUserIdList []int, uniqueCode, title, content string, opUserId int) (err error) {
  298. num := len(dataList)
  299. if num <= 0 {
  300. return
  301. }
  302. dataIdList := make([]int, 0)
  303. for _, v := range dataList {
  304. dataIdList = append(dataIdList, v.DataId)
  305. }
  306. userNum := len(noAuthUserIdList)
  307. if userNum <= 0 {
  308. return
  309. }
  310. to := global.DmSQL["data"].Begin()
  311. defer func() {
  312. if err != nil {
  313. _ = to.Rollback()
  314. } else {
  315. _ = to.Commit()
  316. }
  317. }()
  318. var existList []*EdbInfoPermissionNoAuthRecord
  319. sql := `SELECT * FROM edb_info_permission_no_auth_record WHERE edb_info_type = ? AND edb_info_id in (` + utils.GetOrmInReplace(num) + `)`
  320. err = to.Raw(sql, edbInfoType, dataIdList).Find(&existList).Error
  321. if err != nil {
  322. return
  323. }
  324. existMap := make(map[int32]map[string]*EdbInfoPermissionNoAuthRecord)
  325. for _, v := range existList {
  326. tmpUserExistMap, ok := existMap[v.SysUserId]
  327. if !ok {
  328. tmpUserExistMap = make(map[string]*EdbInfoPermissionNoAuthRecord)
  329. }
  330. key := fmt.Sprint(v.EdbInfoType, "_", v.EdbInfoId)
  331. tmpUserExistMap[key] = v
  332. existMap[v.SysUserId] = tmpUserExistMap
  333. }
  334. addMessageList := make([]*DataPermissionMessage, 0)
  335. addRecordList := make([]*EdbInfoPermissionNoAuthRecord, 0)
  336. for _, userId := range noAuthUserIdList {
  337. isAdd := false
  338. tmpUserExistMap, userExistOk := existMap[int32(userId)]
  339. for _, dataItem := range dataList {
  340. if userExistOk {
  341. key := fmt.Sprint(edbInfoType, "_", dataItem.DataId)
  342. _, ok := tmpUserExistMap[key]
  343. if ok {
  344. continue
  345. }
  346. }
  347. isAdd = true
  348. addRecordList = append(addRecordList, &EdbInfoPermissionNoAuthRecord{
  349. EdbInfoPermissionNoAuthRecordId: 0,
  350. OpUniqueCode: uniqueCode,
  351. EdbInfoType: int32(edbInfoType),
  352. EdbInfoId: int32(dataItem.DataId),
  353. EdbCode: dataItem.DataCode,
  354. EdbName: dataItem.DataName,
  355. SysUserId: int32(userId),
  356. CreateTime: time.Now(),
  357. })
  358. }
  359. if isAdd {
  360. addMessageList = append(addMessageList, &DataPermissionMessage{
  361. DataPermissionMessageId: 0,
  362. SendUserId: int32(opUserId),
  363. ReceiveUserId: int32(userId),
  364. Content: title,
  365. Remark: content,
  366. OpType: 3,
  367. Source: int32(source),
  368. SubSource: int32(edbInfoType),
  369. OpUniqueCode: uniqueCode,
  370. IsRead: 0,
  371. CreateTime: time.Now(),
  372. ModifyTime: time.Now(),
  373. })
  374. }
  375. }
  376. if len(addMessageList) > 0 {
  377. err = to.CreateInBatches(addMessageList, utils.MultiAddNum).Error
  378. if err != nil {
  379. return
  380. }
  381. }
  382. if len(addRecordList) > 0 {
  383. err = to.CreateInBatches(addRecordList, utils.MultiAddNum).Error
  384. if err != nil {
  385. return
  386. }
  387. }
  388. authUserIdNum := len(authUserIdList)
  389. if authUserIdNum > 0 {
  390. sql = `DELETE FROM edb_info_permission_no_auth_record WHERE edb_info_type = ? AND sys_user_id in (` + utils.GetOrmInReplace(authUserIdNum) + `) AND edb_info_id in (` + utils.GetOrmInReplace(num) + `)`
  391. err = to.Exec(sql, edbInfoType, authUserIdList, dataIdList).Error
  392. }
  393. return
  394. }
  395. func DeleteEdbInfoPermissionNoAuthRecordBySourceAndDataIdList(edbInfoType int, dataIdList []string) (err error) {
  396. num := len(dataIdList)
  397. if num <= 0 {
  398. return
  399. }
  400. sql := `DELETE FROM edb_info_permission_no_auth_record WHERE edb_info_type = ? AND edb_info_id in (` + utils.GetOrmInReplace(num) + `)`
  401. err = global.DmSQL["data"].Exec(sql, edbInfoType, dataIdList).Error
  402. return
  403. }
  404. func GetEdbInfoDataPermissionNoAuthRecordListByUserId(userId int32, edbInfoType, startSize, pageSize int) (total int, items []*DataPermissionNoAuthRecord, err error) {
  405. sql := `SELECT count(1) AS total FROM edb_info_permission_no_auth_record WHERE sys_user_id = ? AND edb_info_type = ? `
  406. err = global.DmSQL["data"].Raw(sql, userId, edbInfoType).First(&total).Error
  407. if err != nil {
  408. return
  409. }
  410. sql = `SELECT edb_info_permission_no_auth_record_id as data_permission_no_auth_record_id,op_unique_code,edb_info_type as sub_source,edb_info_id as data_id,edb_code as data_code,edb_name as data_name,sys_user_id,create_time FROM edb_info_permission_no_auth_record WHERE sys_user_id = ? AND edb_info_type = ? ORDER BY edb_info_permission_no_auth_record_id desc LIMIT ?,? `
  411. err = global.DmSQL["data"].Raw(sql, userId, edbInfoType, startSize, pageSize).Find(&items).Error
  412. return
  413. }
  414. type EdbInfoClassifyPermissionNoAuthRecord struct {
  415. EdbInfoClassifyPermissionNoAuthRecordId int64 `json:"edb_info_classify_permission_no_auth_record_id" orm:"column(edb_info_classify_permission_no_auth_record_id);pk" gorm:"primaryKey" ` // 资产分类数据操作记录id
  416. EdbClassifyType int32 `json:"edb_classify_type"` // 子来源 :ETA表格中的各种表格类型,以及图表的来源(这个是后续的扩展方向)
  417. OpUniqueCode string `json:"op_unique_code"` // 操作的唯一编码,主要是记录统一操作的日志
  418. ClassifyId string `json:"classify_id"` // 资产分类id(指标、图表、表格)
  419. ClassifyName string `json:"classify_name"` // 资产分类名称(指标、图表、表格)
  420. SysUserId int32 `json:"sys_user_id"` // 系统用户id
  421. CreateTime time.Time `json:"create_time"` // 创建时间
  422. }
  423. func AddEdbInfoClassifyNoAuthRecordBySourceAndClassifyIdList(source, edbClassifyType int, classifyInfoList []ClassifyDataItem, noAuthUserIdList []int, uniqueCode, title, content string, opUserId int) (err error) {
  424. num := len(classifyInfoList)
  425. if num <= 0 {
  426. return
  427. }
  428. classifyIdList := make([]int, 0)
  429. for _, v := range classifyInfoList {
  430. classifyIdList = append(classifyIdList, v.ClassifyId)
  431. }
  432. userNum := len(noAuthUserIdList)
  433. if userNum <= 0 {
  434. return
  435. }
  436. to := global.DmSQL["data"].Begin()
  437. defer func() {
  438. if err != nil {
  439. _ = to.Rollback()
  440. } else {
  441. _ = to.Commit()
  442. }
  443. }()
  444. var existList []*EdbInfoClassifyPermissionNoAuthRecord
  445. sql := `SELECT * FROM edb_info_classify_permission_no_auth_record WHERE edb_classify_type = ? AND classify_id in (` + utils.GetOrmInReplace(num) + `)`
  446. err = to.Raw(sql, edbClassifyType, classifyIdList).Find(&existList).Error
  447. if err != nil {
  448. return
  449. }
  450. existMap := make(map[int32]map[string]*EdbInfoClassifyPermissionNoAuthRecord)
  451. for _, v := range existList {
  452. tmpUserExistMap, ok := existMap[v.SysUserId]
  453. if !ok {
  454. tmpUserExistMap = make(map[string]*EdbInfoClassifyPermissionNoAuthRecord)
  455. }
  456. key := fmt.Sprint(v.EdbClassifyType, "_", v.ClassifyId)
  457. tmpUserExistMap[key] = v
  458. existMap[v.SysUserId] = tmpUserExistMap
  459. }
  460. addMessageList := make([]*DataPermissionMessage, 0)
  461. addRecordList := make([]*EdbInfoClassifyPermissionNoAuthRecord, 0)
  462. for _, userId := range noAuthUserIdList {
  463. isAdd := false
  464. tmpUserExistMap, userExistOk := existMap[int32(userId)]
  465. for _, dataItem := range classifyInfoList {
  466. if userExistOk {
  467. key := fmt.Sprint(edbClassifyType, "_", dataItem.ClassifyId)
  468. _, ok := tmpUserExistMap[key]
  469. if ok {
  470. continue
  471. }
  472. }
  473. isAdd = true
  474. addRecordList = append(addRecordList, &EdbInfoClassifyPermissionNoAuthRecord{
  475. EdbInfoClassifyPermissionNoAuthRecordId: 0,
  476. EdbClassifyType: int32(edbClassifyType),
  477. OpUniqueCode: uniqueCode,
  478. ClassifyId: fmt.Sprint(dataItem.ClassifyId),
  479. ClassifyName: dataItem.ClassifyName,
  480. SysUserId: int32(userId),
  481. CreateTime: time.Now(),
  482. })
  483. }
  484. if isAdd {
  485. addMessageList = append(addMessageList, &DataPermissionMessage{
  486. DataPermissionMessageId: 0,
  487. SendUserId: int32(opUserId),
  488. ReceiveUserId: int32(userId),
  489. Content: title,
  490. Remark: content,
  491. OpType: 4,
  492. Source: int32(source),
  493. SubSource: int32(edbClassifyType),
  494. OpUniqueCode: uniqueCode,
  495. IsRead: 0,
  496. CreateTime: time.Now(),
  497. ModifyTime: time.Now(),
  498. })
  499. }
  500. }
  501. if len(addMessageList) > 0 {
  502. err = to.CreateInBatches(addMessageList, utils.MultiAddNum).Error
  503. if err != nil {
  504. return
  505. }
  506. }
  507. if len(addRecordList) > 0 {
  508. err = to.CreateInBatches(addRecordList, utils.MultiAddNum).Error
  509. if err != nil {
  510. return
  511. }
  512. }
  513. authUserIdNum := len(classifyIdList)
  514. if authUserIdNum > 0 {
  515. sql = `DELETE FROM edb_info_classify_permission_no_auth_record WHERE edb_classify_type = ? AND classify_id not in (` + utils.GetOrmInReplace(num) + `)`
  516. err = to.Exec(sql, edbClassifyType, classifyIdList).Error
  517. }
  518. return
  519. }
  520. func AddEdbInfoClassifyNoAuthRecordBySourceAndUserIdList(source, edbClassifyType int, noAuthClassifyMap map[int]ClassifyDataItem, configUserIdList []int, uniqueCode, title, content string, opUserId int) (err error) {
  521. configUserNum := len(configUserIdList)
  522. if configUserNum <= 0 {
  523. return
  524. }
  525. to := global.DmSQL["data"].Begin()
  526. defer func() {
  527. if err != nil {
  528. _ = to.Rollback()
  529. } else {
  530. _ = to.Commit()
  531. }
  532. }()
  533. var existList []*EdbInfoClassifyPermissionNoAuthRecord
  534. sql := `SELECT * FROM edb_info_classify_permission_no_auth_record WHERE edb_classify_type = ? AND sys_user_id in (` + utils.GetOrmInReplace(configUserNum) + `)`
  535. err = to.Raw(sql, edbClassifyType, configUserIdList).Find(&existList).Error
  536. if err != nil {
  537. return
  538. }
  539. existMap := make(map[int32]map[string]*EdbInfoClassifyPermissionNoAuthRecord)
  540. delRecordIdMap := make(map[int64]int64)
  541. for _, v := range existList {
  542. tmpUserExistMap, ok := existMap[v.SysUserId]
  543. if !ok {
  544. tmpUserExistMap = make(map[string]*EdbInfoClassifyPermissionNoAuthRecord)
  545. }
  546. tmpUserExistMap[v.ClassifyId] = v
  547. existMap[v.SysUserId] = tmpUserExistMap
  548. delRecordIdMap[v.EdbInfoClassifyPermissionNoAuthRecordId] = v.EdbInfoClassifyPermissionNoAuthRecordId
  549. }
  550. addMessageList := make([]*DataPermissionMessage, 0)
  551. addRecordList := make([]*EdbInfoClassifyPermissionNoAuthRecord, 0)
  552. for _, userId := range configUserIdList {
  553. isAdd := false
  554. tmpUserExistMap, userExistOk := existMap[int32(userId)]
  555. for _, dataItem := range noAuthClassifyMap {
  556. if userExistOk {
  557. key := fmt.Sprint(dataItem.ClassifyId)
  558. tmpUserRecord, ok := tmpUserExistMap[key]
  559. if ok {
  560. delete(delRecordIdMap, tmpUserRecord.EdbInfoClassifyPermissionNoAuthRecordId)
  561. continue
  562. }
  563. }
  564. isAdd = true
  565. addRecordList = append(addRecordList, &EdbInfoClassifyPermissionNoAuthRecord{
  566. EdbInfoClassifyPermissionNoAuthRecordId: 0,
  567. EdbClassifyType: int32(edbClassifyType),
  568. OpUniqueCode: uniqueCode,
  569. ClassifyId: fmt.Sprint(dataItem.ClassifyId),
  570. ClassifyName: dataItem.ClassifyName,
  571. SysUserId: int32(userId),
  572. CreateTime: time.Now(),
  573. })
  574. }
  575. if isAdd {
  576. addMessageList = append(addMessageList, &DataPermissionMessage{
  577. DataPermissionMessageId: 0,
  578. SendUserId: int32(opUserId),
  579. ReceiveUserId: int32(userId),
  580. Content: title,
  581. Remark: content,
  582. OpType: 4,
  583. Source: int32(source),
  584. SubSource: int32(edbClassifyType),
  585. OpUniqueCode: uniqueCode,
  586. IsRead: 0,
  587. CreateTime: time.Now(),
  588. ModifyTime: time.Now(),
  589. })
  590. }
  591. }
  592. if len(addMessageList) > 0 {
  593. err = to.CreateInBatches(addMessageList, utils.MultiAddNum).Error
  594. if err != nil {
  595. return
  596. }
  597. }
  598. if len(addRecordList) > 0 {
  599. err = to.CreateInBatches(addRecordList, utils.MultiAddNum).Error
  600. if err != nil {
  601. return
  602. }
  603. }
  604. delRecordIdNum := len(delRecordIdMap)
  605. if delRecordIdNum > 0 {
  606. delRecordIdList := make([]int64, 0)
  607. for _, v := range delRecordIdMap {
  608. delRecordIdList = append(delRecordIdList, v)
  609. }
  610. sql = `DELETE FROM edb_info_classify_permission_no_auth_record WHERE edb_info_classify_permission_no_auth_record_id in (` + utils.GetOrmInReplace(delRecordIdNum) + `) `
  611. err = to.Exec(sql, delRecordIdList).Error
  612. }
  613. return
  614. }
  615. func DeleteEdbInfoClassifyNoAuthRecordBySourceAndClassifyIdList(edbClassifyType int) (err error) {
  616. sql := `DELETE FROM edb_info_classify_permission_no_auth_record WHERE edb_classify_type = ?`
  617. err = global.DmSQL["data"].Exec(sql, edbClassifyType).Error
  618. return
  619. }
  620. func GetEdbInfoDataPermissionClassifyNoAuthRecordListByUserId(userId int32, edbClassifyType, startSize, pageSize int) (total int, items []*DataPermissionClassifyNoAuthRecord, err error) {
  621. sql := `SELECT count(1) AS total FROM edb_info_classify_permission_no_auth_record WHERE sys_user_id = ? AND edb_classify_type = ? `
  622. err = global.DmSQL["data"].Raw(sql, userId, edbClassifyType).Scan(&total).Error
  623. if err != nil {
  624. return
  625. }
  626. sql = `SELECT edb_info_classify_permission_no_auth_record_id as data_permission_classify_no_auth_record_id,edb_classify_type as sub_source,op_unique_code,classify_id,classify_name,sys_user_id,create_time FROM edb_info_classify_permission_no_auth_record WHERE sys_user_id = ? AND edb_classify_type = ? ORDER BY edb_info_classify_permission_no_auth_record_id desc LIMIT ?,? `
  627. err = global.DmSQL["data"].Raw(sql, userId, edbClassifyType, startSize, pageSize).Find(&items).Error
  628. return
  629. }