chart.go 32 KB

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