chart.go 32 KB

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