edb.go 33 KB

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