edb.go 34 KB

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