chart_classify.go 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424
  1. package data
  2. import (
  3. "errors"
  4. "eta/eta_mobile/models/data_manage"
  5. "eta/eta_mobile/models/system"
  6. "eta/eta_mobile/services/data/data_manage_permission"
  7. "eta/eta_mobile/utils"
  8. "fmt"
  9. "strconv"
  10. "time"
  11. )
  12. func chartClassifyHaveChild(allNode []*data_manage.ChartClassifyItems, node *data_manage.ChartClassifyItems) (childs []*data_manage.ChartClassifyItems, yes bool) {
  13. for _, v := range allNode {
  14. if v.ParentId == node.ChartClassifyId {
  15. childs = append(childs, v)
  16. }
  17. }
  18. if len(childs) > 0 {
  19. yes = true
  20. }
  21. return
  22. }
  23. func ChartClassifyItemsMakeTree(sysUser *system.Admin, allNode []*data_manage.ChartClassifyItems, node *data_manage.ChartClassifyItems, permissionClassifyIdList []int) {
  24. node.HaveOperaAuth = data_manage_permission.CheckEdbClassifyPermissionByPermissionIdList(node.IsJoinPermission, node.ChartClassifyId, permissionClassifyIdList)
  25. button := GetChartClassifyOpButton(sysUser, node.SysUserId, node.HaveOperaAuth)
  26. node.Button = button
  27. childs, _ := chartClassifyHaveChild(allNode, node) //判断节点是否有子节点并返回
  28. if len(childs) > 0 {
  29. for _, child := range childs {
  30. child.HaveOperaAuth = data_manage_permission.CheckEdbClassifyPermissionByPermissionIdList(child.IsJoinPermission, child.ChartClassifyId, permissionClassifyIdList)
  31. childButton := GetChartClassifyOpButton(sysUser, child.SysUserId, child.HaveOperaAuth)
  32. if child.Level == 3 {
  33. childButton.AddButton = false //第三级的话,默认图表都是没有添加按钮的
  34. }
  35. child.Button = childButton
  36. //node.Children = append(node.Children, child)
  37. }
  38. node.Children = append(node.Children, childs[0:]...) //添加子节点
  39. for _, v := range childs {
  40. v.HaveOperaAuth = data_manage_permission.CheckEdbClassifyPermissionByPermissionIdList(v.IsJoinPermission, v.ChartClassifyId, permissionClassifyIdList)
  41. //查询子节点的子节点,并添加到子节点
  42. _, has := chartClassifyHaveChild(allNode, v)
  43. if has {
  44. ChartClassifyItemsMakeTree(sysUser, allNode, v, permissionClassifyIdList) //递归添加节点
  45. } else {
  46. childrenArr := make([]*data_manage.ChartClassifyItems, 0)
  47. v.Children = childrenArr
  48. }
  49. }
  50. } else {
  51. childrenArr := make([]*data_manage.ChartClassifyItems, 0)
  52. node.Children = childrenArr
  53. }
  54. }
  55. func FixChartClassify() {
  56. //新增三级分类
  57. list, err := data_manage.GetChartClassifyByLevel(2, 1)
  58. if err != nil {
  59. return
  60. }
  61. for _, v := range list {
  62. classify := new(data_manage.ChartClassify)
  63. classify.ParentId = v.ChartClassifyId
  64. classify.ChartClassifyName = v.ChartClassifyName
  65. classify.HasData = 0
  66. classify.CreateTime = time.Now()
  67. classify.ModifyTime = time.Now()
  68. if utils.RunMode == "debug" {
  69. classify.SysUserId = 11
  70. } else {
  71. classify.SysUserId = 163
  72. }
  73. classify.SysUserRealName = "admin"
  74. classify.Level = 3
  75. timestamp := strconv.FormatInt(time.Now().UnixNano(), 10)
  76. classify.UniqueCode = utils.MD5(utils.DATA_PREFIX + "_" + timestamp)
  77. classify.Sort = v.Sort
  78. classify.Source = utils.CHART_SOURCE_DEFAULT
  79. id, err := data_manage.AddChartClassify(classify)
  80. if err != nil {
  81. return
  82. }
  83. err = data_manage.FIXChartClassifyId(int(id), v.ChartClassifyId)
  84. if err != nil {
  85. return
  86. }
  87. }
  88. }
  89. func FixChartClassifySysUserId() {
  90. //获取所有父级分类信息
  91. list, err := data_manage.GetChartClassifyByLevel(2, 1)
  92. if err != nil {
  93. return
  94. }
  95. for _, v := range list {
  96. err := data_manage.EditChartClassifySysUser(v.ChartClassifyId, v.SysUserId, v.SysUserRealName)
  97. if err != nil {
  98. return
  99. }
  100. fmt.Println("ChartClassifyId:", v.ChartClassifyId)
  101. }
  102. fmt.Println("FixChartClassifySysUserId end")
  103. }
  104. // GetChartClassifyOpButton 获取ETA图库分类的操作权限
  105. func GetChartClassifyOpButton(sysUser *system.Admin, belongUserId int, haveOperaAuth bool) (button data_manage.ChartClassifyItemsButton) {
  106. // 没有数据权限就直接返回
  107. if !haveOperaAuth {
  108. return
  109. }
  110. //ficc管理员和超管和ficc研究员有权限创建和管理分类,可以编辑分类名称(分类名称不允许重复),可以拖动分类,改变分类顺序,可以拖动分类下模型,改变顺序,可以删除分类,若分类下有预测指标,则不允许删除;
  111. //if utils.InArrayByStr([]string{utils.ROLE_TYPE_CODE_ADMIN, utils.ROLE_TYPE_CODE_FICC_ADMIN, utils.ROLE_TYPE_CODE_RESEARCHR, utils.ROLE_TYPE_CODE_FICC_RESEARCHR}, sysUser.RoleTypeCode) {
  112. button.AddButton = true
  113. button.OpButton = true
  114. button.DeleteButton = true
  115. button.MoveButton = true
  116. //}
  117. return
  118. }
  119. // GetCanOpChartClassify 获取是否允许添加图表分类
  120. func GetCanOpChartClassify(roleTypeCode string) (canOpClassify bool) {
  121. // 如果不是 超管 或者 ficc管理员 或者 ficc研究员,那么就没有权限
  122. if utils.InArrayByStr([]string{utils.ROLE_TYPE_CODE_ADMIN, utils.ROLE_TYPE_CODE_FICC_ADMIN, utils.ROLE_TYPE_CODE_RESEARCHR, utils.ROLE_TYPE_CODE_FICC_RESEARCHR}, roleTypeCode) {
  123. canOpClassify = true
  124. }
  125. return
  126. }
  127. // GetChartOpButton 获取ETA图库的操作权限
  128. func GetChartOpButton(sysUser *system.Admin, belongUserId int, haveOperaAuth bool) (button data_manage.ChartClassifyItemsButton) {
  129. // 数据权限判断
  130. if !haveOperaAuth {
  131. return
  132. }
  133. //预测指标的添加人对该预测指标有全部操作权限,ficc管理员、超管对所有预测指标有全部操作权限;
  134. if sysUser.RoleTypeCode == utils.ROLE_TYPE_CODE_ADMIN || sysUser.RoleTypeCode == utils.ROLE_TYPE_CODE_FICC_ADMIN || sysUser.AdminId == belongUserId || sysUser.PredictEdbPermission == 1 {
  135. button.AddButton = true
  136. button.OpButton = true
  137. button.DeleteButton = true
  138. button.MoveButton = true
  139. }
  140. return
  141. }
  142. // HandleNoPermissionChart 图表列表返回,将没有权限的图表移除
  143. func HandleNoPermissionChart(allNodes []*data_manage.ChartClassifyItems, noPermissionChartIdMap map[int]bool, sysUserId int) (newAllNodes []*data_manage.ChartClassifyItems) {
  144. // 涉及分类id
  145. classifyIdList := make([]int, 0)
  146. // 移除没有权限的图表
  147. newAllNodes = make([]*data_manage.ChartClassifyItems, 0)
  148. for _, node := range allNodes {
  149. // 二级分类
  150. tmpNodeInfo := *node
  151. tmpNodeList := make([]*data_manage.ChartClassifyItems, 0)
  152. if node.Children != nil {
  153. for _, chartInfo := range node.Children {
  154. // 如果指标不可见,那么就不返回该指标
  155. if _, ok := noPermissionChartIdMap[chartInfo.ChartInfoId]; ok {
  156. continue
  157. }
  158. tmpNodeList = append(tmpNodeList, chartInfo)
  159. classifyIdList = append(classifyIdList, chartInfo.ChartClassifyId)
  160. }
  161. }
  162. tmpNodeInfo.Children = tmpNodeList
  163. newAllNodes = append(newAllNodes, &tmpNodeInfo)
  164. classifyIdList = append(classifyIdList, node.ChartClassifyId)
  165. }
  166. // 数据权限
  167. if len(classifyIdList) > 0 {
  168. // 图表分类
  169. chartClassifyList, err := data_manage.GetChartClassifyByIdList(classifyIdList)
  170. if err != nil {
  171. return
  172. }
  173. chartClassifyMap := make(map[int]*data_manage.ChartClassify)
  174. for _, v := range chartClassifyList {
  175. chartClassifyMap[v.ChartClassifyId] = v
  176. }
  177. // 已授权分类id
  178. permissionChartIdList, permissionClassifyIdList, err := data_manage_permission.GetUserChartAndClassifyPermissionList(sysUserId, 0, 0)
  179. if err != nil {
  180. return
  181. }
  182. for _, node := range newAllNodes {
  183. if node.Children != nil {
  184. for _, chartInfo := range node.Children {
  185. if currClassify, ok := chartClassifyMap[chartInfo.ChartClassifyId]; ok {
  186. chartInfo.HaveOperaAuth = data_manage_permission.CheckChartPermissionByPermissionIdList(chartInfo.IsJoinPermission, currClassify.IsJoinPermission, chartInfo.ChartInfoId, chartInfo.ChartClassifyId, permissionChartIdList, permissionClassifyIdList)
  187. }
  188. }
  189. }
  190. if currClassify, ok := chartClassifyMap[node.ChartClassifyId]; ok {
  191. node.HaveOperaAuth = data_manage_permission.CheckChartPermissionByPermissionIdList(node.IsJoinPermission, currClassify.IsJoinPermission, node.ChartInfoId, node.ChartClassifyId, permissionChartIdList, permissionClassifyIdList)
  192. }
  193. }
  194. }
  195. return
  196. }
  197. // AddChartClassify
  198. // @Description: 添加图表分类
  199. // @author: Roc
  200. // @datetime 2024-04-17 14:48:23
  201. // @param chartClassifyName string
  202. // @param parentId int
  203. // @param level int
  204. // @param source int
  205. // @param lang string
  206. // @param sysUser *system.Admin
  207. // @return classifyInfo *data_manage.ChartClassify
  208. // @return err error
  209. // @return errMsg string
  210. // @return isSendEmail bool
  211. func AddChartClassify(chartClassifyName string, parentId, level, source int, lang string, sysUser *system.Admin) (classifyInfo *data_manage.ChartClassify, err error, errMsg string, isSendEmail bool) {
  212. isSendEmail = true
  213. errMsg = "保存分类失败"
  214. // 校验分类名称相同的数量
  215. {
  216. var count int
  217. switch lang {
  218. case utils.EnLangVersion:
  219. count, err = data_manage.GetChartClassifyEnCount(chartClassifyName, parentId, source)
  220. default:
  221. count, err = data_manage.GetChartClassifyCount(chartClassifyName, parentId, source)
  222. }
  223. if err != nil {
  224. errMsg = "判断名称是否已存在失败"
  225. return
  226. }
  227. if count > 0 {
  228. errMsg = "分类名称已存在,请重新输入"
  229. err = errors.New(errMsg)
  230. isSendEmail = false
  231. return
  232. }
  233. }
  234. //获取该层级下最大的排序数
  235. maxSort, err := data_manage.GetChartClassifyMaxSort(parentId, source)
  236. classifyInfo = new(data_manage.ChartClassify)
  237. classifyInfo.ParentId = parentId
  238. classifyInfo.ChartClassifyName = chartClassifyName
  239. classifyInfo.ChartClassifyNameEn = chartClassifyName
  240. classifyInfo.HasData = 0
  241. classifyInfo.CreateTime = time.Now()
  242. classifyInfo.ModifyTime = time.Now()
  243. classifyInfo.SysUserId = sysUser.AdminId
  244. classifyInfo.SysUserRealName = sysUser.RealName
  245. classifyInfo.Level = level + 1
  246. timestamp := strconv.FormatInt(time.Now().UnixNano(), 10)
  247. classifyInfo.UniqueCode = utils.MD5(utils.DATA_PREFIX + "_" + timestamp)
  248. classifyInfo.Sort = maxSort + 1
  249. classifyInfo.Source = source
  250. _, err = data_manage.AddChartClassify(classifyInfo)
  251. if err != nil {
  252. return
  253. }
  254. // 目前只有ETA图库需要继承分类权限
  255. if classifyInfo.Source == utils.CHART_SOURCE_DEFAULT {
  256. go data_manage_permission.InheritParentClassify(5, classifyInfo.Source, classifyInfo.ChartClassifyId, classifyInfo.ParentId, classifyInfo.ChartClassifyName)
  257. }
  258. return
  259. }
  260. // EditChartClassify
  261. // @Description: 修改图表分类
  262. // @author: Roc
  263. // @datetime 2024-04-17 14:59:37
  264. // @param chartClassifyId int
  265. // @param source int
  266. // @param chartClassifyName string
  267. // @param lang string
  268. // @param sysUser *system.Admin
  269. // @return classifyInfo *data_manage.ChartClassify
  270. // @return err error
  271. // @return errMsg string
  272. // @return isSendEmail bool
  273. func EditChartClassify(chartClassifyId, source int, chartClassifyName, lang string, sysUser *system.Admin) (classifyInfo *data_manage.ChartClassify, err error, errMsg string, isSendEmail bool) {
  274. isSendEmail = true
  275. errMsg = "保存失败"
  276. // 获取分类信息
  277. classifyInfo, err = data_manage.GetChartClassifyById(chartClassifyId)
  278. if err != nil {
  279. return
  280. }
  281. // 分类来源校验
  282. if classifyInfo.Source != source {
  283. errMsg = "图表分类异常"
  284. err = errors.New(fmt.Sprintf("图表分类来源异常,修改来源:%d,分类来源:%d", source, classifyInfo.Source))
  285. isSendEmail = false
  286. return
  287. }
  288. // 权限校验
  289. if classifyInfo.Source == utils.CHART_SOURCE_DEFAULT {
  290. // 已授权分类id
  291. permissionClassifyIdList, tmpErr := data_manage_permission.GetUserChartClassifyPermissionList(sysUser.AdminId, classifyInfo.ChartClassifyId)
  292. if tmpErr != nil {
  293. err = errors.New("获取已授权分类id数据失败,Err:" + err.Error())
  294. return
  295. }
  296. haveOperaAuth := data_manage_permission.CheckEdbClassifyPermissionByPermissionIdList(classifyInfo.IsJoinPermission, classifyInfo.ChartClassifyId, permissionClassifyIdList)
  297. // 权限校验
  298. {
  299. button := GetChartClassifyOpButton(sysUser, classifyInfo.SysUserId, haveOperaAuth)
  300. if !button.OpButton {
  301. errMsg = "无操作权限"
  302. err = errors.New(errMsg)
  303. isSendEmail = false
  304. return
  305. }
  306. }
  307. }
  308. // 需要变更的字段
  309. updateCols := make([]string, 0)
  310. // 语言版本校验
  311. switch lang {
  312. case utils.EnLangVersion:
  313. if classifyInfo.ChartClassifyNameEn != chartClassifyName {
  314. count, tmpErr := data_manage.GetChartClassifyEnCount(chartClassifyName, classifyInfo.ParentId, source)
  315. if tmpErr != nil {
  316. errMsg = "判断名称是否已存在失败"
  317. err = errors.New("判断名称是否已存在失败,Err:" + tmpErr.Error())
  318. return
  319. }
  320. if count > 0 {
  321. errMsg = "分类名称已存在,请重新输入"
  322. err = errors.New(errMsg)
  323. isSendEmail = false
  324. return
  325. }
  326. classifyInfo.ChartClassifyNameEn = chartClassifyName
  327. classifyInfo.ModifyTime = time.Now()
  328. updateCols = append(updateCols, "ChartClassifyNameEn", "ModifyTime")
  329. }
  330. default:
  331. if classifyInfo.ChartClassifyName != chartClassifyName {
  332. count, tmpErr := data_manage.GetChartClassifyCount(chartClassifyName, classifyInfo.ParentId, source)
  333. if tmpErr != nil {
  334. errMsg = "判断名称是否已存在失败"
  335. err = errors.New("判断名称是否已存在失败,Err:" + tmpErr.Error())
  336. return
  337. }
  338. if count > 0 {
  339. errMsg = "分类名称已存在,请重新输入"
  340. err = errors.New(errMsg)
  341. isSendEmail = false
  342. return
  343. }
  344. classifyInfo.ChartClassifyName = chartClassifyName
  345. classifyInfo.ModifyTime = time.Now()
  346. updateCols = append(updateCols, "ChartClassifyName", "ModifyTime")
  347. }
  348. }
  349. if len(updateCols) > 0 {
  350. err = classifyInfo.Update(updateCols)
  351. }
  352. return
  353. }
  354. // GetChartClassifyTreeRecursive 递归获取分类树形结构
  355. func GetChartClassifyTreeRecursive(list []*data_manage.ChartClassifyItems, parentId int) []*data_manage.ChartClassifyItems {
  356. res := make([]*data_manage.ChartClassifyItems, 0)
  357. for _, v := range list {
  358. if v.ParentId == parentId {
  359. v.Children = GetChartClassifyTreeRecursive(list, v.ChartClassifyId)
  360. res = append(res, v)
  361. }
  362. }
  363. return res
  364. }
  365. // GetChartClassifyParentRecursive 根据子目录递归父级目录
  366. func GetChartClassifyParentRecursive(list []*data_manage.ChartClassifyItems, classifyId int) []*data_manage.ChartClassifyItems {
  367. res := make([]*data_manage.ChartClassifyItems, 0)
  368. for _, v := range list {
  369. if v.ChartClassifyId == classifyId {
  370. if v.ParentId > 0 {
  371. res = GetChartClassifyParentRecursive(list, v.ParentId)
  372. }
  373. res = append(res, v)
  374. }
  375. }
  376. return res
  377. }