edb.go 33 KB

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