edb.go 33 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944
  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. // EdbInfoPermission
  10. // @Description: 指标权限表
  11. type EdbInfoPermission struct {
  12. EdbInfoPermissionId int64 `json:"edb_info_permission_id" orm:"column(edb_info_permission_id);pk" gorm:"primaryKey" `
  13. EdbInfoId int32 `json:"edb_info_id"` // 指标id
  14. EdbInfoType int32 `json:"edb_info_type"` // 指标类型,0:普通指标,1:预测指标
  15. SysUserId int32 `json:"sys_user_id"` // 系统用户id
  16. ModifyTime time.Time `json:"modify_time"` // 变更时间
  17. CreateTime time.Time `json:"create_time"` // 关系建立时间
  18. }
  19. // EdbClassifyPermission
  20. // @Description: 指标分类权限表
  21. type EdbClassifyPermission struct {
  22. EdbClassifyPermissionId int64 `json:"edb_classify_permission_id" orm:"column(edb_classify_permission_id);pk" gorm:"primaryKey" `
  23. EdbClassifyId int32 `json:"edb_classify_id"` // 分类id
  24. EdbClassifyType int32 `json:"edb_classify_type"` // 分类类型,0:普通指标分类,1:预测指标分类
  25. SysUserId int32 `json:"sys_user_id"` // 系统用户id
  26. ModifyTime time.Time `json:"modify_time"` // 变更时间
  27. CreateTime time.Time `json:"create_time"` // 关系建立时间
  28. }
  29. // SetIsPermissionEdbChartByEdbClassifyIdList
  30. // @Description: 设置指标分类是否涉密
  31. // @author: Roc
  32. // @datetime 2024-03-27 14:15:42
  33. // @param classifyIdList []int
  34. func SetIsPermissionEdbChartByEdbClassifyIdList(classifyIdList []int, classifyType int) (err error) {
  35. num := len(classifyIdList)
  36. to := global.DmSQL["data"].Begin()
  37. defer func() {
  38. if err != nil {
  39. _ = to.Rollback()
  40. } else {
  41. _ = to.Commit()
  42. }
  43. }()
  44. sql := `UPDATE edb_classify SET is_join_permission=?,modify_time=now() WHERE is_join_permission = 1 AND classify_type = ?`
  45. err = to.Exec(sql, 0, classifyType).Error
  46. if err != nil {
  47. return
  48. }
  49. if num > 0 {
  50. sql = `UPDATE edb_classify SET is_join_permission=?,modify_time=now() WHERE classify_type = ? AND classify_id in (` + utils.GetOrmInReplace(num) + `) `
  51. err = to.Exec(sql, 1, classifyType, classifyIdList).Error
  52. if err != nil {
  53. return
  54. }
  55. }
  56. return
  57. }
  58. // SetPermissionByEdbIdList
  59. // @Description: 根据指标ID列表设置指标的用户权限
  60. // @author: Roc
  61. // @datetime 2024-03-27 14:03:42
  62. // @param edbIdList []string
  63. // @param userIdList []int
  64. // @param chartInfoType int
  65. // @return err error
  66. func SetPermissionByEdbIdList(edbIdList []string, userIdList []int, edbInfoType int) (err error) {
  67. edbNum := len(edbIdList)
  68. if edbNum <= 0 {
  69. return
  70. }
  71. to := global.DmSQL["data"].Begin()
  72. defer func() {
  73. if err != nil {
  74. _ = to.Rollback()
  75. } else {
  76. _ = to.Commit()
  77. }
  78. }()
  79. // 获取已经配置的指标权限用户
  80. edbInfoPermissionList := make([]*EdbInfoPermission, 0)
  81. sql := `SELECT * FROM edb_info_permission WHERE edb_info_type = ? AND edb_info_id in (` + utils.GetOrmInReplace(edbNum) + `) `
  82. err = to.Raw(sql, edbInfoType, edbIdList).Find(&edbInfoPermissionList).Error
  83. if err != nil {
  84. return
  85. }
  86. edbInfoPermissionMap := make(map[string]*EdbInfoPermission)
  87. for _, v := range edbInfoPermissionList {
  88. edbInfoPermissionMap[fmt.Sprint(v.EdbInfoId, "_", v.SysUserId)] = v
  89. }
  90. // 标记指标是否纳入权限管控
  91. {
  92. // 默认 标记指标为纳入权限管控
  93. isJoinPermission := 1
  94. // 用户不选的情况下,说明是要给这些指标移除权限管控
  95. if len(userIdList) <= 0 {
  96. // 标记指标为不纳入权限管控
  97. isJoinPermission = 0
  98. }
  99. sql = `UPDATE edb_info SET is_join_permission=?,modify_time=now() WHERE edb_info_type = ? AND edb_info_id in (?) `
  100. err = to.Exec(sql, isJoinPermission, edbInfoType, edbIdList).Error
  101. if err != nil {
  102. return
  103. }
  104. }
  105. // 待添加的配置项
  106. addList := make([]*EdbInfoPermission, 0)
  107. // 遍历待配置的指标和用户,筛选出需要添加的配置项
  108. for _, edbInfoIdStr := range edbIdList {
  109. edbInfoId, tmpErr := strconv.ParseInt(edbInfoIdStr, 10, 64)
  110. if tmpErr != nil {
  111. err = tmpErr
  112. return
  113. }
  114. for _, userId := range userIdList {
  115. key := fmt.Sprint(edbInfoId, "_", userId)
  116. if _, ok := edbInfoPermissionMap[key]; ok {
  117. // 如果存在那么就移除,说明不需要处理了
  118. delete(edbInfoPermissionMap, key)
  119. } else {
  120. // 如果不存在,那么就添加
  121. addList = append(addList, &EdbInfoPermission{
  122. //PermissionId: 0,
  123. EdbInfoId: int32(edbInfoId),
  124. SysUserId: int32(userId),
  125. EdbInfoType: int32(edbInfoType),
  126. ModifyTime: time.Now(),
  127. CreateTime: time.Now(),
  128. })
  129. }
  130. }
  131. }
  132. // 添加待配置项
  133. if len(addList) > 0 {
  134. err = to.CreateInBatches(addList, utils.MultiAddNum).Error
  135. if err != nil {
  136. return
  137. }
  138. }
  139. // 移除废弃的配置项
  140. {
  141. // 待移除的配置项
  142. deletePermissionIdList := make([]int64, 0)
  143. for _, v := range edbInfoPermissionMap {
  144. deletePermissionIdList = append(deletePermissionIdList, v.EdbInfoPermissionId)
  145. }
  146. deletePermissionIdNum := len(deletePermissionIdList)
  147. if deletePermissionIdNum > 0 {
  148. sql = `DELETE FROM edb_info_permission WHERE edb_info_permission_id in (?)`
  149. err = to.Exec(sql, deletePermissionIdList).Error
  150. if err != nil {
  151. return
  152. }
  153. }
  154. }
  155. return
  156. }
  157. // SetPermissionByEdbClassifyIdList
  158. // @Description: 根据指标分类ID列表设置分类的用户权限
  159. // @author: Roc
  160. // @datetime 2024-03-28 14:53:04
  161. // @param classifyIdList []int
  162. // @param userIdList []int
  163. // @return err error
  164. func SetPermissionByEdbClassifyIdList(classifyIdList []int, userIdList []int, classifyType int) (err error) {
  165. userNum := len(userIdList)
  166. if userNum <= 0 {
  167. return
  168. }
  169. to := global.DmSQL["data"].Begin()
  170. defer func() {
  171. if err != nil {
  172. _ = to.Rollback()
  173. } else {
  174. _ = to.Commit()
  175. }
  176. }()
  177. // 获取当前选择用户已经配置的指标分类权限
  178. classifyPermissionList := make([]*EdbClassifyPermission, 0)
  179. sql := `SELECT * FROM edb_classify_permission WHERE edb_classify_type = ? AND sys_user_id in (` + utils.GetOrmInReplace(userNum) + `) `
  180. err = to.Raw(sql, classifyType, userIdList).Find(&classifyPermissionList).Error
  181. if err != nil {
  182. return
  183. }
  184. classifyPermissionMap := make(map[string]*EdbClassifyPermission)
  185. for _, v := range classifyPermissionList {
  186. classifyPermissionMap[fmt.Sprint(v.EdbClassifyId, "_", v.SysUserId)] = v
  187. }
  188. // 待添加的配置项
  189. addList := make([]*EdbClassifyPermission, 0)
  190. // 遍历待配置的指标和用户,筛选出需要添加的配置项
  191. for _, userId := range userIdList {
  192. for _, classifyId := range classifyIdList {
  193. key := fmt.Sprint(classifyId, "_", userId)
  194. if _, ok := classifyPermissionMap[key]; ok {
  195. // 如果存在那么就移除,说明不需要处理了
  196. delete(classifyPermissionMap, key)
  197. } else {
  198. // 如果不存在,那么就提那家
  199. addList = append(addList, &EdbClassifyPermission{
  200. //PermissionId: 0,
  201. EdbClassifyId: int32(classifyId),
  202. EdbClassifyType: int32(classifyType),
  203. SysUserId: int32(userId),
  204. ModifyTime: time.Now(),
  205. CreateTime: time.Now(),
  206. })
  207. }
  208. }
  209. }
  210. // 添加待配置项
  211. if len(addList) > 0 {
  212. err = to.CreateInBatches(addList, utils.MultiAddNum).Error
  213. if err != nil {
  214. return
  215. }
  216. }
  217. // 移除废弃的配置项
  218. {
  219. // 获取移除的配置项
  220. deletePermissionIdList := make([]int64, 0)
  221. for _, v := range classifyPermissionMap {
  222. deletePermissionIdList = append(deletePermissionIdList, v.EdbClassifyPermissionId)
  223. }
  224. deletePermissionIdNum := len(deletePermissionIdList)
  225. if deletePermissionIdNum > 0 {
  226. sql = "DELETE FROM edb_classify_permission WHERE edb_classify_permission_id in (" + utils.GetOrmInReplace(deletePermissionIdNum) + ")"
  227. err = to.Exec(sql, deletePermissionIdList).Error
  228. if err != nil {
  229. return
  230. }
  231. }
  232. }
  233. return
  234. }
  235. // GetPermissionEdbClassifyIdListByUserId
  236. // @Description: 根据用户ID获取已经配置的分类id列表
  237. // @author: Roc
  238. // @datetime 2024-03-29 16:24:46
  239. // @param userId int
  240. // @param classifyType int
  241. // @return edbClassifyIdList []int
  242. // @return err error
  243. func GetPermissionEdbClassifyIdListByUserId(userId int, classifyType int) (edbClassifyIdList []int, err error) {
  244. sql := `SELECT edb_classify_id FROM edb_classify_permission WHERE edb_classify_type = ? AND sys_user_id = ? `
  245. err = global.DmSQL["data"].Raw(sql, classifyType, userId).Find(&edbClassifyIdList).Error
  246. return
  247. }
  248. // GetPermissionUserIdListByEdbId
  249. // @Description: 根据指标ID获取已经配置的用户id列表
  250. // @author: Roc
  251. // @datetime 2024-03-29 16:24:46
  252. // @param dataId int
  253. // @param edbInfoType int
  254. // @return userIdList []int
  255. // @return err error
  256. func GetPermissionUserIdListByEdbId(dataId int, edbInfoType int) (userIdList []int, err error) {
  257. sql := `SELECT sys_user_id FROM edb_info_permission WHERE edb_info_type = ? AND edb_info_id= ? `
  258. err = global.DmSQL["data"].Raw(sql, edbInfoType, dataId).Find(&userIdList).Error
  259. return
  260. }
  261. // GetPermissionUserIdListByEdbClassifyId
  262. // @Description: 根据指标分类ID获取已经配置的用户id列表
  263. // @author: Roc
  264. // @datetime 2024-03-29 16:24:46
  265. // @param classifyId int
  266. // @param edbClassifyType int
  267. // @return userIdList []int
  268. // @return err error
  269. func GetPermissionUserIdListByEdbClassifyId(classifyId int, edbClassifyType int) (userIdList []int, err error) {
  270. sql := `SELECT sys_user_id FROM edb_classify_permission WHERE edb_classify_type = ? AND edb_classify_id= ? `
  271. err = global.DmSQL["data"].Raw(sql, edbClassifyType, classifyId).Find(&userIdList).Error
  272. return
  273. }
  274. // GetPermissionEdbIdList
  275. // @Description: 获取用户权限的指标列表
  276. // @author: Roc
  277. // @datetime 2024-03-28 16:50:47
  278. // @param userId int
  279. // @param edbInfoId int
  280. // @return idList []int
  281. // @return err error
  282. func GetPermissionEdbIdList(userId, edbInfoId int) (idList []int, err error) {
  283. pars := []interface{}{userId}
  284. sql := `SELECT edb_info_id FROM edb_info_permission WHERE sys_user_id = ? `
  285. if edbInfoId > 0 {
  286. sql += ` AND edb_info_id = ? `
  287. pars = append(pars, edbInfoId)
  288. }
  289. err = global.DmSQL["data"].Raw(sql, pars...).Find(&idList).Error
  290. return
  291. }
  292. // GetPermissionEdbClassifyIdList
  293. // @Description: 获取用户权限的指标分类列表
  294. // @author: Roc
  295. // @datetime 2024-03-28 16:50:47
  296. // @param userId int
  297. // @param classifyId int
  298. // @return idList []int
  299. // @return err error
  300. func GetPermissionEdbClassifyIdList(userId, classifyId int) (idList []int, err error) {
  301. pars := []interface{}{userId}
  302. sql := `SELECT edb_classify_id FROM edb_classify_permission WHERE sys_user_id = ? `
  303. if classifyId > 0 {
  304. sql += ` AND edb_classify_id = ? `
  305. pars = append(pars, classifyId)
  306. }
  307. err = global.DmSQL["data"].Raw(sql, pars...).Find(&idList).Error
  308. return
  309. }
  310. // InheritParentClassifyByEdbClassifyId
  311. // @Description: 继承父级分类的指标权限信息
  312. // @author: Roc
  313. // @datetime 2024-04-07 21:02:51
  314. // @param source int
  315. // @param classifyType int
  316. // @param classifyId int
  317. // @param parentClassifyId int
  318. // @param classifyName string
  319. // @param uniqueCode string
  320. // @return err error
  321. func InheritParentClassifyByEdbClassifyId(source, classifyType, classifyId, parentClassifyId int, classifyName, uniqueCode string) (err error) {
  322. to := global.DmSQL["data"].Begin()
  323. defer func() {
  324. if err != nil {
  325. _ = to.Rollback()
  326. } else {
  327. _ = to.Commit()
  328. }
  329. }()
  330. sql := `UPDATE edb_classify SET is_join_permission=?,modify_time=now() WHERE classify_type = ? AND classify_id = ? `
  331. err = to.Exec(sql, 1, classifyType, classifyId).Error
  332. if err != nil {
  333. return
  334. }
  335. // 添加未授权记录
  336. {
  337. // 获取父级未授权的用户记录
  338. var parentRecordItems []*EdbInfoClassifyPermissionNoAuthRecord
  339. 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 ?,? `
  340. err = to.Raw(sql, parentClassifyId, source, classifyType).Find(&parentRecordItems).Error
  341. addNoAuthRecordItems := make([]*EdbInfoClassifyPermissionNoAuthRecord, 0)
  342. for _, v := range parentRecordItems {
  343. addNoAuthRecordItems = append(addNoAuthRecordItems, &EdbInfoClassifyPermissionNoAuthRecord{
  344. EdbInfoClassifyPermissionNoAuthRecordId: 0,
  345. EdbClassifyType: v.EdbClassifyType,
  346. OpUniqueCode: uniqueCode,
  347. ClassifyId: fmt.Sprint(classifyId),
  348. ClassifyName: classifyName,
  349. SysUserId: v.SysUserId,
  350. CreateTime: time.Now(),
  351. })
  352. }
  353. // 添加待配置项
  354. if len(addNoAuthRecordItems) > 0 {
  355. err = to.CreateInBatches(addNoAuthRecordItems, utils.MultiAddNum).Error
  356. if err != nil {
  357. return
  358. }
  359. }
  360. }
  361. // 添加授权记录
  362. {
  363. // 获取父级分类已经授权的用户
  364. parentClassifyPermissionList := make([]*EdbClassifyPermission, 0)
  365. sql = `SELECT * FROM edb_classify_permission WHERE edb_classify_type = ? AND edb_classify_id = ? `
  366. err = to.Raw(sql, classifyType, parentClassifyId).Find(&parentClassifyPermissionList).Error
  367. if err != nil {
  368. return
  369. }
  370. addList := make([]*EdbClassifyPermission, 0)
  371. for _, v := range parentClassifyPermissionList {
  372. // 如果不存在,那么就提那家
  373. addList = append(addList, &EdbClassifyPermission{
  374. //PermissionId: 0,
  375. EdbClassifyId: int32(classifyId),
  376. EdbClassifyType: int32(classifyType),
  377. SysUserId: v.SysUserId,
  378. ModifyTime: time.Now(),
  379. CreateTime: time.Now(),
  380. })
  381. }
  382. // 添加待配置项
  383. if len(addList) > 0 {
  384. err = to.CreateInBatches(addList, utils.MultiAddNum).Error
  385. if err != nil {
  386. return
  387. }
  388. }
  389. }
  390. return
  391. }
  392. // EdbInfoPermissionNoAuthRecord
  393. // @Description: 指标数据权限未授权记录表
  394. type EdbInfoPermissionNoAuthRecord struct {
  395. EdbInfoPermissionNoAuthRecordId int64 `json:"edb_info_permission_no_auth_record_id" orm:"column(edb_info_permission_no_auth_record_id);pk" gorm:"primaryKey" ` // 资产数据操作记录id
  396. OpUniqueCode string `json:"op_unique_code"` // 操作的唯一编码,主要是记录统一操作的日志
  397. EdbInfoType int32 `json:"edb_info_type"` // 指标类型,0:普通指标,1:预测指标
  398. EdbInfoId int32 `json:"edb_info_id"` // 指标id
  399. EdbCode string `json:"edb_code"` // 指标编码
  400. EdbName string `json:"edb_name"` // 指标名称
  401. SysUserId int32 `json:"sys_user_id"` // 系统用户id
  402. CreateTime time.Time `json:"create_time"` // 创建时间
  403. }
  404. // AddEdbInfoPermissionNoAuthRecordBySourceAndDataIdList
  405. // @Description: 添加未授权用户记录
  406. // @author: Roc
  407. // @datetime 2024-04-07 15:25:49
  408. // @param source int
  409. // @param edbInfoType int
  410. // @param dataList []DataItem
  411. // @param noAuthUserIdList []int 未授权用户
  412. // @param authUserIdList []int 已授权用户
  413. // @param uniqueCode
  414. // @param content string
  415. // @param opUserId int
  416. // @return err error
  417. func AddEdbInfoPermissionNoAuthRecordBySourceAndDataIdList(source, edbInfoType int, dataList []DataItem, noAuthUserIdList, authUserIdList []int, uniqueCode, title, content string, opUserId int) (err error) {
  418. num := len(dataList)
  419. if num <= 0 {
  420. return
  421. }
  422. dataIdList := make([]int, 0)
  423. for _, v := range dataList {
  424. dataIdList = append(dataIdList, v.DataId)
  425. }
  426. userNum := len(noAuthUserIdList)
  427. if userNum <= 0 {
  428. return
  429. }
  430. to := global.DmSQL["data"].Begin()
  431. defer func() {
  432. if err != nil {
  433. _ = to.Rollback()
  434. } else {
  435. _ = to.Commit()
  436. }
  437. }()
  438. // 根据指标获取已经存在的未授权记录
  439. var existList []*EdbInfoPermissionNoAuthRecord
  440. sql := `SELECT * FROM edb_info_permission_no_auth_record WHERE edb_info_type = ? AND edb_info_id in (` + utils.GetOrmInReplace(num) + `)`
  441. err = to.Raw(sql, edbInfoType, dataIdList).Find(&existList).Error
  442. if err != nil {
  443. return
  444. }
  445. // 已经标记了的数据
  446. existMap := make(map[int32]map[string]*EdbInfoPermissionNoAuthRecord)
  447. for _, v := range existList {
  448. tmpUserExistMap, ok := existMap[v.SysUserId]
  449. if !ok {
  450. tmpUserExistMap = make(map[string]*EdbInfoPermissionNoAuthRecord)
  451. }
  452. key := fmt.Sprint(v.EdbInfoType, "_", v.EdbInfoId)
  453. tmpUserExistMap[key] = v
  454. existMap[v.SysUserId] = tmpUserExistMap
  455. }
  456. addMessageList := make([]*DataPermissionMessage, 0)
  457. addRecordList := make([]*EdbInfoPermissionNoAuthRecord, 0)
  458. for _, userId := range noAuthUserIdList {
  459. isAdd := false
  460. tmpUserExistMap, userExistOk := existMap[int32(userId)]
  461. for _, dataItem := range dataList {
  462. // 判断是否已经存在,已经存在就过滤
  463. if userExistOk {
  464. key := fmt.Sprint(edbInfoType, "_", dataItem.DataId)
  465. _, ok := tmpUserExistMap[key]
  466. if ok {
  467. continue
  468. }
  469. }
  470. isAdd = true
  471. addRecordList = append(addRecordList, &EdbInfoPermissionNoAuthRecord{
  472. EdbInfoPermissionNoAuthRecordId: 0,
  473. OpUniqueCode: uniqueCode,
  474. EdbInfoType: int32(edbInfoType),
  475. EdbInfoId: int32(dataItem.DataId),
  476. EdbCode: dataItem.DataCode,
  477. EdbName: dataItem.DataName,
  478. SysUserId: int32(userId),
  479. CreateTime: time.Now(),
  480. })
  481. }
  482. // 有记录的话,需要添加消息
  483. if isAdd {
  484. addMessageList = append(addMessageList, &DataPermissionMessage{
  485. DataPermissionMessageId: 0,
  486. SendUserId: int32(opUserId),
  487. ReceiveUserId: int32(userId),
  488. Content: title,
  489. Remark: content,
  490. OpType: 3,
  491. Source: int32(source),
  492. SubSource: int32(edbInfoType),
  493. OpUniqueCode: uniqueCode,
  494. IsRead: 0,
  495. CreateTime: time.Now(),
  496. ModifyTime: time.Now(),
  497. })
  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. // 添加记录
  508. if len(addRecordList) > 0 {
  509. err = to.CreateInBatches(addRecordList, utils.MultiAddNum).Error
  510. if err != nil {
  511. return
  512. }
  513. }
  514. // 已经授权了的用户,需要删除未授权记录
  515. authUserIdNum := len(authUserIdList)
  516. if authUserIdNum > 0 {
  517. 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) + `)`
  518. err = to.Exec(sql, edbInfoType, authUserIdList, dataIdList).Error
  519. }
  520. return
  521. }
  522. // DeleteEdbInfoPermissionNoAuthRecordBySourceAndDataIdList
  523. // @Description: 根据来源和数据id列表删除记录
  524. // @author: Roc
  525. // @datetime 2024-04-07 14:47:37
  526. // @param source int
  527. // @param subSource int
  528. // @param dataIdList []string
  529. // @return err error
  530. func DeleteEdbInfoPermissionNoAuthRecordBySourceAndDataIdList(edbInfoType int, dataIdList []string) (err error) {
  531. num := len(dataIdList)
  532. if num <= 0 {
  533. return
  534. }
  535. sql := `DELETE FROM edb_info_permission_no_auth_record WHERE edb_info_type = ? AND edb_info_id in (` + utils.GetOrmInReplace(num) + `)`
  536. err = global.DmSQL["data"].Exec(sql, edbInfoType, dataIdList).Error
  537. return
  538. }
  539. // GetEdbInfoDataPermissionNoAuthRecordListByUserId
  540. // @Description: 获取明细数据
  541. // @author: Roc
  542. // @datetime 2024-04-10 14:23:15
  543. // @param userId int32
  544. // @param edbInfoType int32
  545. // @param startSize int
  546. // @param pageSize int
  547. // @return total int
  548. // @return items []*DataPermissionNoAuthRecord
  549. // @return err error
  550. func GetEdbInfoDataPermissionNoAuthRecordListByUserId(userId int32, edbInfoType, startSize, pageSize int) (total int, items []*DataPermissionNoAuthRecord, err error) {
  551. // 获取总数
  552. sql := `SELECT count(1) AS total FROM edb_info_permission_no_auth_record WHERE sys_user_id = ? AND edb_info_type = ? `
  553. err = global.DmSQL["data"].Raw(sql, userId, edbInfoType).First(&total).Error
  554. if err != nil {
  555. return
  556. }
  557. 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 ?,? `
  558. err = global.DmSQL["data"].Raw(sql, userId, edbInfoType, startSize, pageSize).Find(&items).Error
  559. return
  560. }
  561. type EdbInfoClassifyPermissionNoAuthRecord struct {
  562. 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
  563. EdbClassifyType int32 `json:"edb_classify_type"` // 子来源 :ETA表格中的各种表格类型,以及图表的来源(这个是后续的扩展方向)
  564. OpUniqueCode string `json:"op_unique_code"` // 操作的唯一编码,主要是记录统一操作的日志
  565. ClassifyId string `json:"classify_id"` // 资产分类id(指标、图表、表格)
  566. ClassifyName string `json:"classify_name"` // 资产分类名称(指标、图表、表格)
  567. SysUserId int32 `json:"sys_user_id"` // 系统用户id
  568. CreateTime time.Time `json:"create_time"` // 创建时间
  569. }
  570. // AddEdbInfoClassifyNoAuthRecordBySourceAndClassifyIdList
  571. // @Description: 根据分类添加用户分类未授权记录
  572. // @author: Roc
  573. // @datetime 2024-04-07 16:44:21
  574. // @param source int
  575. // @param edbClassifyType int
  576. // @param classifyInfoList []ClassifyDataItem
  577. // @param noAuthUserIdList []int
  578. // @param uniqueCode string
  579. // @param content string
  580. // @param opUserId int
  581. // @return err error
  582. func AddEdbInfoClassifyNoAuthRecordBySourceAndClassifyIdList(source, edbClassifyType int, classifyInfoList []ClassifyDataItem, noAuthUserIdList []int, uniqueCode, title, content string, opUserId int) (err error) {
  583. num := len(classifyInfoList)
  584. if num <= 0 {
  585. return
  586. }
  587. // 分类id
  588. classifyIdList := make([]int, 0)
  589. for _, v := range classifyInfoList {
  590. classifyIdList = append(classifyIdList, v.ClassifyId)
  591. }
  592. userNum := len(noAuthUserIdList)
  593. if userNum <= 0 {
  594. return
  595. }
  596. to := global.DmSQL["data"].Begin()
  597. defer func() {
  598. if err != nil {
  599. _ = to.Rollback()
  600. } else {
  601. _ = to.Commit()
  602. }
  603. }()
  604. // 根据分类获取已经存在的未授权记录
  605. var existList []*EdbInfoClassifyPermissionNoAuthRecord
  606. sql := `SELECT * FROM edb_info_classify_permission_no_auth_record WHERE edb_classify_type = ? AND classify_id in (` + utils.GetOrmInReplace(num) + `)`
  607. err = to.Raw(sql, edbClassifyType, classifyIdList).Find(&existList).Error
  608. if err != nil {
  609. return
  610. }
  611. // 已经标记了的数据
  612. existMap := make(map[int32]map[string]*EdbInfoClassifyPermissionNoAuthRecord)
  613. for _, v := range existList {
  614. tmpUserExistMap, ok := existMap[v.SysUserId]
  615. if !ok {
  616. tmpUserExistMap = make(map[string]*EdbInfoClassifyPermissionNoAuthRecord)
  617. }
  618. key := fmt.Sprint(v.EdbClassifyType, "_", v.ClassifyId)
  619. tmpUserExistMap[key] = v
  620. existMap[v.SysUserId] = tmpUserExistMap
  621. }
  622. addMessageList := make([]*DataPermissionMessage, 0)
  623. addRecordList := make([]*EdbInfoClassifyPermissionNoAuthRecord, 0)
  624. for _, userId := range noAuthUserIdList {
  625. isAdd := false
  626. tmpUserExistMap, userExistOk := existMap[int32(userId)]
  627. for _, dataItem := range classifyInfoList {
  628. // 判断是否已经存在,已经存在就过滤
  629. if userExistOk {
  630. key := fmt.Sprint(edbClassifyType, "_", dataItem.ClassifyId)
  631. _, ok := tmpUserExistMap[key]
  632. if ok {
  633. continue
  634. }
  635. }
  636. isAdd = true
  637. addRecordList = append(addRecordList, &EdbInfoClassifyPermissionNoAuthRecord{
  638. EdbInfoClassifyPermissionNoAuthRecordId: 0,
  639. EdbClassifyType: int32(edbClassifyType),
  640. OpUniqueCode: uniqueCode,
  641. ClassifyId: fmt.Sprint(dataItem.ClassifyId),
  642. ClassifyName: dataItem.ClassifyName,
  643. SysUserId: int32(userId),
  644. CreateTime: time.Now(),
  645. })
  646. }
  647. // 有记录的话,需要添加消息
  648. if isAdd {
  649. addMessageList = append(addMessageList, &DataPermissionMessage{
  650. DataPermissionMessageId: 0,
  651. SendUserId: int32(opUserId),
  652. ReceiveUserId: int32(userId),
  653. Content: title,
  654. Remark: content,
  655. OpType: 4,
  656. Source: int32(source),
  657. SubSource: int32(edbClassifyType),
  658. OpUniqueCode: uniqueCode,
  659. IsRead: 0,
  660. CreateTime: time.Now(),
  661. ModifyTime: time.Now(),
  662. })
  663. }
  664. }
  665. // 添加消息
  666. if len(addMessageList) > 0 {
  667. err = to.CreateInBatches(addMessageList, utils.MultiAddNum).Error
  668. if err != nil {
  669. return
  670. }
  671. }
  672. // 添加记录
  673. if len(addRecordList) > 0 {
  674. err = to.CreateInBatches(addRecordList, utils.MultiAddNum).Error
  675. if err != nil {
  676. return
  677. }
  678. }
  679. // 移除已经公开了的分类权限,需要删除未授权记录
  680. authUserIdNum := len(classifyIdList)
  681. if authUserIdNum > 0 {
  682. sql = `DELETE FROM edb_info_classify_permission_no_auth_record WHERE edb_classify_type = ? AND classify_id not in (` + utils.GetOrmInReplace(num) + `)`
  683. err = to.Exec(sql, edbClassifyType, classifyIdList).Error
  684. }
  685. return
  686. }
  687. // AddEdbInfoClassifyNoAuthRecordBySourceAndUserIdList
  688. // @Description: 根据用户添加用户分类未授权记录
  689. // @author: Roc
  690. // @datetime 2024-04-07 20:12:44
  691. // @param source int
  692. // @param edbClassifyType int
  693. // @param noAuthClassifyMap map[int]ClassifyDataItem
  694. // @param configUserIdList []int
  695. // @param uniqueCode string
  696. // @param content string
  697. // @param opUserId int
  698. // @return err error
  699. func AddEdbInfoClassifyNoAuthRecordBySourceAndUserIdList(source, edbClassifyType int, noAuthClassifyMap map[int]ClassifyDataItem, configUserIdList []int, uniqueCode, title, content string, opUserId int) (err error) {
  700. // 当前配置用户
  701. configUserNum := len(configUserIdList)
  702. if configUserNum <= 0 {
  703. return
  704. }
  705. to := global.DmSQL["data"].Begin()
  706. defer func() {
  707. if err != nil {
  708. _ = to.Rollback()
  709. } else {
  710. _ = to.Commit()
  711. }
  712. }()
  713. // 根据当前配置用户获取已经存在的未授权记录
  714. var existList []*EdbInfoClassifyPermissionNoAuthRecord
  715. sql := `SELECT * FROM edb_info_classify_permission_no_auth_record WHERE edb_classify_type = ? AND sys_user_id in (` + utils.GetOrmInReplace(configUserNum) + `)`
  716. err = to.Raw(sql, edbClassifyType, configUserIdList).Find(&existList).Error
  717. if err != nil {
  718. return
  719. }
  720. // 已经标记了的数据
  721. existMap := make(map[int32]map[string]*EdbInfoClassifyPermissionNoAuthRecord)
  722. delRecordIdMap := make(map[int64]int64)
  723. for _, v := range existList {
  724. tmpUserExistMap, ok := existMap[v.SysUserId]
  725. if !ok {
  726. tmpUserExistMap = make(map[string]*EdbInfoClassifyPermissionNoAuthRecord)
  727. }
  728. tmpUserExistMap[v.ClassifyId] = v
  729. existMap[v.SysUserId] = tmpUserExistMap
  730. // 已经配置了的记录id
  731. delRecordIdMap[v.EdbInfoClassifyPermissionNoAuthRecordId] = v.EdbInfoClassifyPermissionNoAuthRecordId
  732. }
  733. addMessageList := make([]*DataPermissionMessage, 0)
  734. addRecordList := make([]*EdbInfoClassifyPermissionNoAuthRecord, 0)
  735. for _, userId := range configUserIdList {
  736. isAdd := false
  737. tmpUserExistMap, userExistOk := existMap[int32(userId)]
  738. for _, dataItem := range noAuthClassifyMap {
  739. // 判断是否已经存在,已经存在就过滤
  740. if userExistOk {
  741. key := fmt.Sprint(dataItem.ClassifyId)
  742. tmpUserRecord, ok := tmpUserExistMap[key]
  743. if ok {
  744. delete(delRecordIdMap, tmpUserRecord.EdbInfoClassifyPermissionNoAuthRecordId)
  745. continue
  746. }
  747. }
  748. isAdd = true
  749. addRecordList = append(addRecordList, &EdbInfoClassifyPermissionNoAuthRecord{
  750. EdbInfoClassifyPermissionNoAuthRecordId: 0,
  751. EdbClassifyType: int32(edbClassifyType),
  752. OpUniqueCode: uniqueCode,
  753. ClassifyId: fmt.Sprint(dataItem.ClassifyId),
  754. ClassifyName: dataItem.ClassifyName,
  755. SysUserId: int32(userId),
  756. CreateTime: time.Now(),
  757. })
  758. }
  759. // 有记录的话,需要添加消息
  760. if isAdd {
  761. addMessageList = append(addMessageList, &DataPermissionMessage{
  762. DataPermissionMessageId: 0,
  763. SendUserId: int32(opUserId),
  764. ReceiveUserId: int32(userId),
  765. Content: title,
  766. Remark: content,
  767. OpType: 4,
  768. Source: int32(source),
  769. SubSource: int32(edbClassifyType),
  770. OpUniqueCode: uniqueCode,
  771. IsRead: 0,
  772. CreateTime: time.Now(),
  773. ModifyTime: time.Now(),
  774. })
  775. }
  776. }
  777. // 添加消息
  778. if len(addMessageList) > 0 {
  779. err = to.CreateInBatches(addMessageList, utils.MultiAddNum).Error
  780. if err != nil {
  781. return
  782. }
  783. }
  784. // 添加记录
  785. if len(addRecordList) > 0 {
  786. err = to.CreateInBatches(addRecordList, utils.MultiAddNum).Error
  787. if err != nil {
  788. return
  789. }
  790. }
  791. // 需要删除未授权记录
  792. delRecordIdNum := len(delRecordIdMap)
  793. if delRecordIdNum > 0 {
  794. delRecordIdList := make([]int64, 0)
  795. for _, v := range delRecordIdMap {
  796. delRecordIdList = append(delRecordIdList, v)
  797. }
  798. sql = `DELETE FROM edb_info_classify_permission_no_auth_record WHERE edb_info_classify_permission_no_auth_record_id in (` + utils.GetOrmInReplace(delRecordIdNum) + `) `
  799. err = to.Exec(sql, delRecordIdList).Error
  800. }
  801. return
  802. }
  803. // DeleteEdbInfoClassifyNoAuthRecordBySourceAndClassifyIdList
  804. // @Description: 根据来源和删除分类授权记录
  805. // @author: Roc
  806. // @datetime 2024-04-07 14:47:37
  807. // @param source int
  808. // @param edbClassifyType int
  809. // @return err error
  810. func DeleteEdbInfoClassifyNoAuthRecordBySourceAndClassifyIdList(edbClassifyType int) (err error) {
  811. sql := `DELETE FROM edb_info_classify_permission_no_auth_record WHERE edb_classify_type = ?`
  812. err = global.DmSQL["data"].Exec(sql, edbClassifyType).Error
  813. return
  814. }
  815. // GetEdbInfoDataPermissionClassifyNoAuthRecordListByUserId
  816. // @Description: 根据用户获取未授权的资产分类记录
  817. // @author: Roc
  818. // @datetime 2024-04-07 20:14:49
  819. // @param userId int
  820. // @param source int
  821. // @param subSource int
  822. // @param startSize int
  823. // @param pageSize int
  824. // @return total int
  825. // @return items []*DataPermissionClassifyNoAuthRecord
  826. // @return err error
  827. func GetEdbInfoDataPermissionClassifyNoAuthRecordListByUserId(userId int32, edbClassifyType, startSize, pageSize int) (total int, items []*DataPermissionClassifyNoAuthRecord, err error) {
  828. // 获取总数
  829. sql := `SELECT count(1) AS total FROM edb_info_classify_permission_no_auth_record WHERE sys_user_id = ? AND edb_classify_type = ? `
  830. err = global.DmSQL["data"].Raw(sql, userId, edbClassifyType).Scan(&total).Error
  831. if err != nil {
  832. return
  833. }
  834. 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 ?,? `
  835. err = global.DmSQL["data"].Raw(sql, userId, edbClassifyType, startSize, pageSize).Find(&items).Error
  836. return
  837. }