chart.go 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472
  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. // 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. if num <= 0 {
  39. return
  40. }
  41. o, err := orm.NewOrmUsingDB("data").Begin()
  42. if err != nil {
  43. return
  44. }
  45. defer func() {
  46. if err != nil {
  47. _ = o.Rollback()
  48. } else {
  49. _ = o.Commit()
  50. }
  51. }()
  52. //// 获取已经配置涉密的分类权限
  53. //chartClassifyList := make([]*ChartClassify, 0)
  54. //sql := `SELECT * FROM chart_classify WHERE is_join_permission = ? `
  55. //_, err = o.Raw(sql, 1).QueryRows(&chartClassifyList)
  56. //if err != nil {
  57. // return
  58. //}
  59. //chartClassifyMap := make(map[int]*ChartClassify)
  60. //for _, v := range chartClassifyList {
  61. // chartClassifyMap[v.ClassifyId] = v
  62. //}
  63. // 先将所有已经设置了涉密的分类设置为不涉密
  64. sql := `UPDATE chart_classify SET is_join_permission=?,modify_time=now() WHERE is_join_permission = 1 AND source = ?`
  65. _, err = o.Raw(sql, 0, chartClassifySource).Exec()
  66. if err != nil {
  67. return
  68. }
  69. if len(classifyIdList) > 0 {
  70. // 将对应的分类设置为涉密
  71. sql = `UPDATE chart_classify SET is_join_permission=?,modify_time=now() WHERE source = ? AND chart_classify_id in (` + utils.GetOrmInReplace(num) + `) `
  72. _, err = o.Raw(sql, 1, chartClassifySource, classifyIdList).Exec()
  73. if err != nil {
  74. return
  75. }
  76. }
  77. return
  78. }
  79. // SetPermissionByChartIdList
  80. // @Description: 根据图表ID列表设置图表的用户权限
  81. // @author: Roc
  82. // @datetime 2024-04-01 10:26:17
  83. // @param chartIdList []string
  84. // @param userIdList []int
  85. // @param source int
  86. // @return err error
  87. func SetPermissionByChartIdList(chartIdList []string, userIdList []int, source int) (err error) {
  88. chartNum := len(chartIdList)
  89. if chartNum <= 0 {
  90. return
  91. }
  92. o, err := orm.NewOrmUsingDB("data").Begin()
  93. if err != nil {
  94. return
  95. }
  96. defer func() {
  97. if err != nil {
  98. _ = o.Rollback()
  99. } else {
  100. _ = o.Commit()
  101. }
  102. }()
  103. // 获取已经配置的图表权限用户
  104. chartInfoPermissionList := make([]*ChartInfoPermission, 0)
  105. sql := `SELECT * FROM chart_info_permission WHERE source = ? AND chart_info_id in (` + utils.GetOrmInReplace(chartNum) + `) `
  106. _, err = o.Raw(sql, source, chartIdList).QueryRows(&chartInfoPermissionList)
  107. if err != nil {
  108. return
  109. }
  110. chartInfoPermissionMap := make(map[string]*ChartInfoPermission)
  111. for _, v := range chartInfoPermissionList {
  112. chartInfoPermissionMap[fmt.Sprint(v.ChartInfoId, "_", v.SysUserId)] = v
  113. }
  114. // 标记图表是否纳入权限管控
  115. {
  116. // 默认 标记图表为纳入权限管控
  117. isJoinPermission := 1
  118. // 用户不选的情况下,说明是要给这些图表移除权限管控
  119. if len(userIdList) <= 0 {
  120. // 标记图表为不纳入权限管控
  121. isJoinPermission = 0
  122. }
  123. sql = `UPDATE chart_info SET is_join_permission=?,modify_time=now() WHERE source = ? AND chart_info_id in (` + utils.GetOrmInReplace(chartNum) + `) `
  124. _, err = o.Raw(sql, isJoinPermission, source, chartIdList).Exec()
  125. if err != nil {
  126. return
  127. }
  128. }
  129. // 待添加的配置项
  130. addList := make([]*ChartInfoPermission, 0)
  131. // 遍历待配置的图表和用户,筛选出需要添加的配置项
  132. for _, chartInfoIdStr := range chartIdList {
  133. chartInfoId, tmpErr := strconv.ParseInt(chartInfoIdStr, 10, 64)
  134. if tmpErr != nil {
  135. err = tmpErr
  136. return
  137. }
  138. for _, userId := range userIdList {
  139. key := fmt.Sprint(chartInfoId, "_", userId)
  140. if _, ok := chartInfoPermissionMap[key]; ok {
  141. // 如果存在那么就移除,说明不需要处理了
  142. delete(chartInfoPermissionMap, key)
  143. } else {
  144. // 如果不存在,那么就添加
  145. addList = append(addList, &ChartInfoPermission{
  146. //PermissionId: 0,
  147. ChartInfoId: int32(chartInfoId),
  148. SysUserId: int32(userId),
  149. Source: int32(source),
  150. ModifyTime: time.Now(),
  151. CreateTime: time.Now(),
  152. })
  153. }
  154. }
  155. }
  156. // 添加待配置项
  157. if len(addList) > 0 {
  158. _, err = o.InsertMulti(500, addList)
  159. if err != nil {
  160. return
  161. }
  162. }
  163. // 移除废弃的配置项
  164. {
  165. // 待移除的配置项
  166. deletePermissionIdList := make([]int64, 0)
  167. for _, v := range chartInfoPermissionMap {
  168. deletePermissionIdList = append(deletePermissionIdList, v.ChartInfoPermissionId)
  169. }
  170. deletePermissionIdNum := len(deletePermissionIdList)
  171. if deletePermissionIdNum > 0 {
  172. sql = "DELETE FROM chart_info_permission WHERE chart_info_permission_id in (" + utils.GetOrmInReplace(deletePermissionIdNum) + ")"
  173. _, err = o.Raw(sql, deletePermissionIdList).Exec()
  174. if err != nil {
  175. return
  176. }
  177. }
  178. }
  179. return
  180. }
  181. // SetPermissionByChartClassifyIdList
  182. // @Description: 根据图表分类ID列表设置分类的用户权限
  183. // @author: Roc
  184. // @datetime 2024-03-28 14:53:04
  185. // @param classifyIdList []int
  186. // @param userIdList []int
  187. // @return err error
  188. func SetPermissionByChartClassifyIdList(classifyIdList []int, userIdList []int, classifyType int) (err error) {
  189. userNum := len(userIdList)
  190. if userNum <= 0 {
  191. return
  192. }
  193. o, err := orm.NewOrmUsingDB("data").Begin()
  194. if err != nil {
  195. return
  196. }
  197. defer func() {
  198. if err != nil {
  199. _ = o.Rollback()
  200. } else {
  201. _ = o.Commit()
  202. }
  203. }()
  204. // 获取当前选择用户已经配置的图表分类权限
  205. classifyPermissionList := make([]*ChartClassifyPermission, 0)
  206. sql := `SELECT * FROM chart_classify_permission WHERE source = ? AND sys_user_id in (` + utils.GetOrmInReplace(userNum) + `) `
  207. _, err = o.Raw(sql, classifyType, userIdList).QueryRows(&classifyPermissionList)
  208. if err != nil {
  209. return
  210. }
  211. classifyPermissionMap := make(map[string]*ChartClassifyPermission)
  212. for _, v := range classifyPermissionList {
  213. classifyPermissionMap[fmt.Sprint(v.ChartClassifyId, "_", v.SysUserId)] = v
  214. }
  215. // 待添加的配置项
  216. addList := make([]*ChartClassifyPermission, 0)
  217. // 遍历待配置的图表和用户,筛选出需要添加的配置项
  218. for _, userId := range userIdList {
  219. for _, classifyId := range classifyIdList {
  220. key := fmt.Sprint(classifyId, "_", userId)
  221. if _, ok := classifyPermissionMap[key]; ok {
  222. // 如果存在那么就移除,说明不需要处理了
  223. delete(classifyPermissionMap, key)
  224. } else {
  225. // 如果不存在,那么就提那家
  226. addList = append(addList, &ChartClassifyPermission{
  227. //PermissionId: 0,
  228. ChartClassifyId: int32(classifyId),
  229. Source: int32(classifyType),
  230. SysUserId: int32(userId),
  231. ModifyTime: time.Now(),
  232. CreateTime: time.Now(),
  233. })
  234. }
  235. }
  236. }
  237. // 添加待配置项
  238. if len(addList) > 0 {
  239. _, err = o.InsertMulti(500, addList)
  240. if err != nil {
  241. return
  242. }
  243. }
  244. // 移除废弃的配置项
  245. {
  246. // 获取移除的配置项
  247. deletePermissionIdList := make([]int64, 0)
  248. for _, v := range classifyPermissionMap {
  249. deletePermissionIdList = append(deletePermissionIdList, v.ChartClassifyPermissionId)
  250. }
  251. deletePermissionIdNum := len(deletePermissionIdList)
  252. if deletePermissionIdNum > 0 {
  253. sql = "DELETE FROM chart_classify_permission WHERE chart_classify_permission_id in (" + utils.GetOrmInReplace(deletePermissionIdNum) + ")"
  254. _, err = o.Raw(sql, deletePermissionIdList).Exec()
  255. if err != nil {
  256. return
  257. }
  258. }
  259. }
  260. return
  261. }
  262. // GetPermissionChartClassifyIdListByUserId
  263. // @Description: 根据用户ID获取已经配置的分类id列表
  264. // @author: Roc
  265. // @datetime 2024-03-29 16:24:46
  266. // @param userId int
  267. // @param classifyType int
  268. // @return chartClassifyIdList []int
  269. // @return err error
  270. func GetPermissionChartClassifyIdListByUserId(userId int, classifyType int) (chartClassifyIdList []int, err error) {
  271. o := orm.NewOrmUsingDB("data")
  272. sql := `SELECT chart_classify_id FROM chart_classify_permission WHERE source = ? AND sys_user_id = ? `
  273. _, err = o.Raw(sql, classifyType, userId).QueryRows(&chartClassifyIdList)
  274. return
  275. }
  276. // GetPermissionUserIdListByChartId
  277. // @Description: 根据图表ID获取已经配置的用户id列表
  278. // @author: Roc
  279. // @datetime 2024-03-29 16:24:46
  280. // @param dataId int
  281. // @param source int
  282. // @return chartIdList []int
  283. // @return err error
  284. func GetPermissionUserIdListByChartId(dataId int, source int) (userIdList []int, err error) {
  285. o := orm.NewOrmUsingDB("data")
  286. sql := `SELECT sys_user_id FROM chart_info_permission WHERE source = ? AND chart_info_id= ? `
  287. _, err = o.Raw(sql, source, dataId).QueryRows(&userIdList)
  288. return
  289. }
  290. // GetPermissionUserIdListByChartClassifyId
  291. // @Description: 根据图表分类ID获取已经配置的用户id列表
  292. // @author: Roc
  293. // @datetime 2024-03-29 16:24:46
  294. // @param classifyId int
  295. // @param source int
  296. // @return userIdList []int
  297. // @return err error
  298. func GetPermissionUserIdListByChartClassifyId(classifyId int, source int) (userIdList []int, err error) {
  299. o := orm.NewOrmUsingDB("data")
  300. sql := `SELECT sys_user_id FROM chart_classify_permission WHERE source = ? AND chart_classify_id= ? `
  301. _, err = o.Raw(sql, source, classifyId).QueryRows(&userIdList)
  302. return
  303. }
  304. // GetPermissionChartIdList
  305. // @Description: 获取用户权限的图表列表
  306. // @author: Roc
  307. // @datetime 2024-03-28 16:50:47
  308. // @param userId int
  309. // @param chartInfoId int
  310. // @return idList []int
  311. // @return err error
  312. func GetPermissionChartIdList(userId, chartInfoId int) (idList []int, err error) {
  313. pars := []interface{}{userId}
  314. o := orm.NewOrmUsingDB("data")
  315. sql := `SELECT chart_info_id FROM chart_info_permission WHERE sys_user_id = ? `
  316. if chartInfoId > 0 {
  317. sql += ` AND chart_info_id = ? `
  318. pars = append(pars, chartInfoId)
  319. }
  320. _, err = o.Raw(sql, pars).QueryRows(&idList)
  321. return
  322. }
  323. // GetPermissionChartClassifyIdList
  324. // @Description: 获取用户权限的图表分类列表
  325. // @author: Roc
  326. // @datetime 2024-03-28 16:50:47
  327. // @param userId int
  328. // @param classifyId int
  329. // @return idList []int
  330. // @return err error
  331. func GetPermissionChartClassifyIdList(userId, classifyId int) (idList []int, err error) {
  332. pars := []interface{}{userId}
  333. o := orm.NewOrmUsingDB("data")
  334. sql := `SELECT chart_classify_id FROM chart_classify_permission WHERE sys_user_id = ? `
  335. if classifyId > 0 {
  336. sql += ` AND chart_classify_id = ? `
  337. pars = append(pars, classifyId)
  338. }
  339. _, err = o.Raw(sql, pars).QueryRows(&idList)
  340. return
  341. }
  342. // InheritParentClassifyByChartClassifyId
  343. // @Description: 继承父级分类的图表权限信息
  344. // @author: Roc
  345. // @datetime 2024-04-07 21:02:51
  346. // @param dataSource int
  347. // @param chartSource int
  348. // @param classifyId int
  349. // @param parentClassifyId int
  350. // @param classifyName string
  351. // @param uniqueCode string
  352. // @return err error
  353. func InheritParentClassifyByChartClassifyId(dataSource, chartSource, classifyId, parentClassifyId int, classifyName, uniqueCode string) (err error) {
  354. o, err := orm.NewOrmUsingDB("data").Begin()
  355. if err != nil {
  356. return
  357. }
  358. defer func() {
  359. if err != nil {
  360. _ = o.Rollback()
  361. } else {
  362. _ = o.Commit()
  363. }
  364. }()
  365. // 将对应的分类设置为涉密
  366. sql := `UPDATE chart_classify SET is_join_permission=?,modify_time=now() WHERE source = ? AND chart_classify_id = ? `
  367. _, err = o.Raw(sql, 1, chartSource, classifyId).Exec()
  368. if err != nil {
  369. return
  370. }
  371. // 添加未授权记录
  372. {
  373. // 获取父级未授权的用户记录
  374. var parentRecordItems []*DataPermissionClassifyNoAuthRecord
  375. 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 ?,? `
  376. _, err = o.Raw(sql, parentClassifyId, dataSource, chartSource).QueryRows(&parentRecordItems)
  377. addNoAuthRecordItems := make([]*DataPermissionClassifyNoAuthRecord, 0)
  378. for _, v := range parentRecordItems {
  379. addNoAuthRecordItems = append(addNoAuthRecordItems, &DataPermissionClassifyNoAuthRecord{
  380. DataPermissionClassifyNoAuthRecordId: 0,
  381. Source: v.Source,
  382. SubSource: v.SubSource,
  383. OpUniqueCode: uniqueCode,
  384. ClassifyId: fmt.Sprint(classifyId),
  385. ClassifyName: classifyName,
  386. SysUserId: v.SysUserId,
  387. CreateTime: time.Now(),
  388. })
  389. }
  390. // 添加待配置项
  391. if len(addNoAuthRecordItems) > 0 {
  392. _, err = o.InsertMulti(500, addNoAuthRecordItems)
  393. if err != nil {
  394. return
  395. }
  396. }
  397. }
  398. // 添加授权记录
  399. {
  400. // 获取父级分类已经授权的用户
  401. parentClassifyPermissionList := make([]*ChartClassifyPermission, 0)
  402. sql = `SELECT * FROM chart_classify_permission WHERE source = ? AND chart_classify_id = ? `
  403. _, err = o.Raw(sql, chartSource, parentClassifyId).QueryRows(&parentClassifyPermissionList)
  404. if err != nil {
  405. return
  406. }
  407. addList := make([]*ChartClassifyPermission, 0)
  408. for _, v := range parentClassifyPermissionList {
  409. // 如果不存在,那么就提那家
  410. addList = append(addList, &ChartClassifyPermission{
  411. //PermissionId: 0,
  412. ChartClassifyId: int32(classifyId),
  413. Source: int32(chartSource),
  414. SysUserId: v.SysUserId,
  415. ModifyTime: time.Now(),
  416. CreateTime: time.Now(),
  417. })
  418. }
  419. // 添加待配置项
  420. if len(addList) > 0 {
  421. _, err = o.InsertMulti(500, addList)
  422. if err != nil {
  423. return
  424. }
  425. }
  426. }
  427. return
  428. }