chart_classify.go 41 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197
  1. package data
  2. import (
  3. "errors"
  4. "eta/eta_api/models/data_manage"
  5. "eta/eta_api/models/system"
  6. "eta/eta_api/services/data/data_manage_permission"
  7. "eta/eta_api/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, level 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. node.Disable = true
  28. if node.Level == level {
  29. node.Disable = false
  30. }
  31. childs, _ := chartClassifyHaveChild(allNode, node) //判断节点是否有子节点并返回
  32. if len(childs) > 0 {
  33. for _, child := range childs {
  34. child.HaveOperaAuth = data_manage_permission.CheckEdbClassifyPermissionByPermissionIdList(child.IsJoinPermission, child.ChartClassifyId, permissionClassifyIdList)
  35. childButton := GetChartClassifyOpButton(sysUser, child.SysUserId, child.HaveOperaAuth)
  36. if child.Level == 6 {
  37. childButton.AddButton = false //第六级的话,默认图表都是没有添加按钮的
  38. }
  39. child.Disable = true
  40. if child.Level == level {
  41. child.Disable = false
  42. }
  43. child.Button = childButton
  44. //node.Children = append(node.Children, child)
  45. }
  46. node.Children = append(node.Children, childs[0:]...) //添加子节点
  47. for _, v := range childs {
  48. v.HaveOperaAuth = data_manage_permission.CheckEdbClassifyPermissionByPermissionIdList(v.IsJoinPermission, v.ChartClassifyId, permissionClassifyIdList)
  49. //查询子节点的子节点,并添加到子节点
  50. _, has := chartClassifyHaveChild(allNode, v)
  51. if has {
  52. ChartClassifyItemsMakeTree(sysUser, allNode, v, permissionClassifyIdList, level) //递归添加节点
  53. } else {
  54. if level > 0 {
  55. v.Children = nil
  56. } else {
  57. childrenArr := make([]*data_manage.ChartClassifyItems, 0)
  58. v.Children = childrenArr
  59. }
  60. }
  61. }
  62. } else {
  63. if level > 0 {
  64. node.Children = nil
  65. } else {
  66. childrenArr := make([]*data_manage.ChartClassifyItems, 0)
  67. node.Children = childrenArr
  68. }
  69. }
  70. }
  71. func FixChartClassify() {
  72. //新增三级分类
  73. list, err := data_manage.GetChartClassifyByLevel(2, 1)
  74. if err != nil {
  75. return
  76. }
  77. for _, v := range list {
  78. classify := new(data_manage.ChartClassify)
  79. classify.ParentId = v.ChartClassifyId
  80. classify.ChartClassifyName = v.ChartClassifyName
  81. classify.HasData = 0
  82. classify.CreateTime = time.Now()
  83. classify.ModifyTime = time.Now()
  84. if utils.RunMode == "debug" {
  85. classify.SysUserId = 11
  86. } else {
  87. classify.SysUserId = 163
  88. }
  89. classify.SysUserRealName = "admin"
  90. classify.Level = 3
  91. timestamp := strconv.FormatInt(time.Now().UnixNano(), 10)
  92. classify.UniqueCode = utils.MD5(utils.DATA_PREFIX + "_" + timestamp)
  93. classify.Sort = v.Sort
  94. classify.Source = utils.CHART_SOURCE_DEFAULT
  95. id, err := data_manage.AddChartClassify(classify)
  96. if err != nil {
  97. return
  98. }
  99. err = data_manage.FIXChartClassifyId(int(id), v.ChartClassifyId)
  100. if err != nil {
  101. return
  102. }
  103. }
  104. }
  105. func FixChartClassifySysUserId() {
  106. //获取所有父级分类信息
  107. list, err := data_manage.GetChartClassifyByLevel(2, 1)
  108. if err != nil {
  109. return
  110. }
  111. for _, v := range list {
  112. err := data_manage.EditChartClassifySysUser(v.ChartClassifyId, v.SysUserId, v.SysUserRealName)
  113. if err != nil {
  114. return
  115. }
  116. fmt.Println("ChartClassifyId:", v.ChartClassifyId)
  117. }
  118. fmt.Println("FixChartClassifySysUserId end")
  119. }
  120. // GetChartClassifyOpButton 获取ETA图库分类的操作权限
  121. func GetChartClassifyOpButton(sysUser *system.Admin, belongUserId int, haveOperaAuth bool) (button data_manage.ChartClassifyItemsButton) {
  122. // 没有数据权限就直接返回
  123. if !haveOperaAuth {
  124. return
  125. }
  126. //ficc管理员和超管和ficc研究员有权限创建和管理分类,可以编辑分类名称(分类名称不允许重复),可以拖动分类,改变分类顺序,可以拖动分类下模型,改变顺序,可以删除分类,若分类下有预测指标,则不允许删除;
  127. //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) {
  128. button.AddButton = true
  129. button.OpButton = true
  130. button.DeleteButton = true
  131. button.MoveButton = true
  132. //}
  133. return
  134. }
  135. // GetCanOpChartClassify 获取是否允许添加图表分类
  136. func GetCanOpChartClassify(roleTypeCode string) (canOpClassify bool) {
  137. // 如果不是 超管 或者 ficc管理员 或者 ficc研究员,那么就没有权限
  138. 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) {
  139. canOpClassify = true
  140. }
  141. return
  142. }
  143. // GetChartOpButton 获取ETA图库的操作权限
  144. func GetChartOpButton(sysUser *system.Admin, belongUserId int, haveOperaAuth bool) (button data_manage.ChartClassifyItemsButton) {
  145. // 数据权限判断
  146. if !haveOperaAuth {
  147. return
  148. }
  149. //预测指标的添加人对该预测指标有全部操作权限,ficc管理员、超管对所有预测指标有全部操作权限;
  150. if sysUser.RoleTypeCode == utils.ROLE_TYPE_CODE_ADMIN || sysUser.RoleTypeCode == utils.ROLE_TYPE_CODE_FICC_ADMIN || sysUser.AdminId == belongUserId || sysUser.PredictEdbPermission == 1 {
  151. button.AddButton = true
  152. button.OpButton = true
  153. button.DeleteButton = true
  154. button.MoveButton = true
  155. }
  156. return
  157. }
  158. // HandleNoPermissionChart 图表列表返回,将没有权限的图表移除
  159. func HandleNoPermissionChart(allNodes []*data_manage.ChartClassifyItems, noPermissionChartIdMap map[int]bool, sysUserId int) (newAllNodes []*data_manage.ChartClassifyItems) {
  160. // 涉及分类id
  161. classifyIdList := make([]int, 0)
  162. // 移除没有权限的图表
  163. newAllNodes = make([]*data_manage.ChartClassifyItems, 0)
  164. for _, node := range allNodes {
  165. // 二级分类
  166. tmpNodeInfo := *node
  167. tmpNodeList := make([]*data_manage.ChartClassifyItems, 0)
  168. if node.Children != nil {
  169. for _, chartInfo := range node.Children {
  170. // 如果指标不可见,那么就不返回该指标
  171. if _, ok := noPermissionChartIdMap[chartInfo.ChartInfoId]; ok {
  172. continue
  173. }
  174. tmpNodeList = append(tmpNodeList, chartInfo)
  175. classifyIdList = append(classifyIdList, chartInfo.ChartClassifyId)
  176. }
  177. }
  178. tmpNodeInfo.Children = tmpNodeList
  179. newAllNodes = append(newAllNodes, &tmpNodeInfo)
  180. classifyIdList = append(classifyIdList, node.ChartClassifyId)
  181. }
  182. // 数据权限
  183. if len(classifyIdList) > 0 {
  184. // 图表分类
  185. chartClassifyList, err := data_manage.GetChartClassifyByIdList(classifyIdList)
  186. if err != nil {
  187. return
  188. }
  189. chartClassifyMap := make(map[int]*data_manage.ChartClassify)
  190. for _, v := range chartClassifyList {
  191. chartClassifyMap[v.ChartClassifyId] = v
  192. }
  193. // 已授权分类id
  194. permissionChartIdList, permissionClassifyIdList, err := data_manage_permission.GetUserChartAndClassifyPermissionList(sysUserId, 0, 0)
  195. if err != nil {
  196. return
  197. }
  198. for _, node := range newAllNodes {
  199. if node.Children != nil {
  200. for _, chartInfo := range node.Children {
  201. if currClassify, ok := chartClassifyMap[chartInfo.ChartClassifyId]; ok {
  202. chartInfo.HaveOperaAuth = data_manage_permission.CheckChartPermissionByPermissionIdList(chartInfo.IsJoinPermission, currClassify.IsJoinPermission, chartInfo.ChartInfoId, chartInfo.ChartClassifyId, permissionChartIdList, permissionClassifyIdList)
  203. }
  204. }
  205. }
  206. if currClassify, ok := chartClassifyMap[node.ChartClassifyId]; ok {
  207. node.HaveOperaAuth = data_manage_permission.CheckChartPermissionByPermissionIdList(node.IsJoinPermission, currClassify.IsJoinPermission, node.ChartInfoId, node.ChartClassifyId, permissionChartIdList, permissionClassifyIdList)
  208. }
  209. }
  210. }
  211. return
  212. }
  213. // AddChartClassify
  214. // @Description: 添加图表分类
  215. // @author: Roc
  216. // @datetime 2024-04-17 14:48:23
  217. // @param chartClassifyName string
  218. // @param parentId int
  219. // @param level int
  220. // @param source int
  221. // @param lang string
  222. // @param sysUser *system.Admin
  223. // @return classifyInfo *data_manage.ChartClassify
  224. // @return err error
  225. // @return errMsg string
  226. // @return isSendEmail bool
  227. func AddChartClassify(chartClassifyName string, parentId, level, source int, lang string, sysUser *system.Admin) (classifyInfo *data_manage.ChartClassify, err error, errMsg string, isSendEmail bool) {
  228. isSendEmail = true
  229. errMsg = "保存分类失败"
  230. // 校验分类名称相同的数量
  231. {
  232. var count int
  233. switch lang {
  234. case utils.EnLangVersion:
  235. count, err = data_manage.GetChartClassifyEnCount(chartClassifyName, parentId, source)
  236. default:
  237. count, err = data_manage.GetChartClassifyCount(chartClassifyName, parentId, source)
  238. }
  239. if err != nil {
  240. errMsg = "判断名称是否已存在失败"
  241. return
  242. }
  243. if count > 0 {
  244. errMsg = "分类名称已存在,请重新输入"
  245. err = errors.New(errMsg)
  246. isSendEmail = false
  247. return
  248. }
  249. }
  250. if level > 5 {
  251. errMsg = `最高只支持添加6级分类`
  252. return
  253. }
  254. //获取该层级下最大的排序数
  255. maxSort, err := data_manage.GetChartClassifyMaxSort(parentId, source)
  256. if err != nil {
  257. errMsg = "获取失败"
  258. err = errors.New("查询排序信息失败,Err:" + err.Error())
  259. return
  260. }
  261. //查询顶级rootId
  262. rootId := 0
  263. if parentId > 0 {
  264. parentClassify, tErr := data_manage.GetChartClassifyById(parentId)
  265. if tErr != nil {
  266. if tErr.Error() == utils.ErrNoRow() {
  267. errMsg = "父级分类不存在"
  268. err = errors.New(errMsg)
  269. return
  270. }
  271. errMsg = "获取失败"
  272. err = errors.New("获取分类信息失败,Err:" + tErr.Error())
  273. return
  274. }
  275. rootId = parentClassify.RootId
  276. }
  277. classifyInfo = new(data_manage.ChartClassify)
  278. classifyInfo.ParentId = parentId
  279. classifyInfo.ChartClassifyName = chartClassifyName
  280. classifyInfo.ChartClassifyNameEn = chartClassifyName
  281. classifyInfo.HasData = 0
  282. classifyInfo.CreateTime = time.Now()
  283. classifyInfo.ModifyTime = time.Now()
  284. classifyInfo.SysUserId = sysUser.AdminId
  285. classifyInfo.SysUserRealName = sysUser.RealName
  286. classifyInfo.Level = level + 1
  287. timestamp := strconv.FormatInt(time.Now().UnixNano(), 10)
  288. classifyInfo.UniqueCode = utils.MD5(utils.DATA_PREFIX + "_" + timestamp)
  289. classifyInfo.Sort = maxSort + 1
  290. classifyInfo.Source = source
  291. classifyInfo.RootId = rootId
  292. newId, err := data_manage.AddChartClassify(classifyInfo)
  293. if err != nil {
  294. return
  295. }
  296. if parentId == 0 { //一级目录的rootId等于自己本身
  297. classifyInfo.RootId = int(newId)
  298. err = classifyInfo.Update([]string{"RootId"})
  299. if err != nil {
  300. errMsg = "更新分类失败"
  301. return
  302. }
  303. }
  304. // 目前只有ETA图库需要继承分类权限
  305. if classifyInfo.Source == utils.CHART_SOURCE_DEFAULT {
  306. go data_manage_permission.InheritParentClassify(5, classifyInfo.Source, classifyInfo.ChartClassifyId, classifyInfo.ParentId, classifyInfo.ChartClassifyName)
  307. }
  308. return
  309. }
  310. // EditChartClassify
  311. // @Description: 修改图表分类
  312. // @author: Roc
  313. // @datetime 2024-04-17 14:59:37
  314. // @param chartClassifyId int
  315. // @param source int
  316. // @param chartClassifyName string
  317. // @param lang string
  318. // @param sysUser *system.Admin
  319. // @return classifyInfo *data_manage.ChartClassify
  320. // @return err error
  321. // @return errMsg string
  322. // @return isSendEmail bool
  323. func EditChartClassify(chartClassifyId, source int, chartClassifyName, lang string, sysUser *system.Admin) (classifyInfo *data_manage.ChartClassify, err error, errMsg string, isSendEmail bool) {
  324. isSendEmail = true
  325. errMsg = "保存失败"
  326. // 获取分类信息
  327. classifyInfo, err = data_manage.GetChartClassifyById(chartClassifyId)
  328. if err != nil {
  329. return
  330. }
  331. // 分类来源校验
  332. if classifyInfo.Source != source {
  333. errMsg = "图表分类异常"
  334. err = errors.New(fmt.Sprintf("图表分类来源异常,修改来源:%d,分类来源:%d", source, classifyInfo.Source))
  335. isSendEmail = false
  336. return
  337. }
  338. // 需要变更的字段
  339. updateCols := make([]string, 0)
  340. // 语言版本校验
  341. switch lang {
  342. case utils.EnLangVersion:
  343. if classifyInfo.ChartClassifyNameEn != chartClassifyName {
  344. count, tmpErr := data_manage.GetChartClassifyEnCount(chartClassifyName, classifyInfo.ParentId, source)
  345. if tmpErr != nil {
  346. errMsg = "判断名称是否已存在失败"
  347. err = errors.New("判断名称是否已存在失败,Err:" + tmpErr.Error())
  348. return
  349. }
  350. if count > 0 {
  351. errMsg = "分类名称已存在,请重新输入"
  352. err = errors.New(errMsg)
  353. isSendEmail = false
  354. return
  355. }
  356. classifyInfo.ChartClassifyNameEn = chartClassifyName
  357. classifyInfo.ModifyTime = time.Now()
  358. updateCols = append(updateCols, "ChartClassifyNameEn", "ModifyTime")
  359. }
  360. default:
  361. if classifyInfo.ChartClassifyName != chartClassifyName {
  362. count, tmpErr := data_manage.GetChartClassifyCount(chartClassifyName, classifyInfo.ParentId, source)
  363. if tmpErr != nil {
  364. errMsg = "判断名称是否已存在失败"
  365. err = errors.New("判断名称是否已存在失败,Err:" + tmpErr.Error())
  366. return
  367. }
  368. if count > 0 {
  369. errMsg = "分类名称已存在,请重新输入"
  370. err = errors.New(errMsg)
  371. isSendEmail = false
  372. return
  373. }
  374. classifyInfo.ChartClassifyName = chartClassifyName
  375. classifyInfo.ModifyTime = time.Now()
  376. updateCols = append(updateCols, "ChartClassifyName", "ModifyTime")
  377. }
  378. }
  379. if len(updateCols) > 0 {
  380. err = classifyInfo.Update(updateCols)
  381. }
  382. return
  383. }
  384. // MoveChartClassify 移动图表分类
  385. func MoveChartClassify(req data_manage.MoveChartClassifyReq, sysUser *system.Admin, source int) (err error, errMsg string) {
  386. classifyId := req.ClassifyId
  387. parentClassifyId := req.ParentClassifyId
  388. prevClassifyId := req.PrevClassifyId
  389. nextClassifyId := req.NextClassifyId
  390. chartInfoId := req.ChartInfoId
  391. prevChartInfoId := req.PrevChartInfoId
  392. nextChartInfoId := req.NextChartInfoId
  393. //首先确定移动的对象是分类还是指标
  394. //判断上一个节点是分类还是指标
  395. //判断下一个节点是分类还是指标
  396. //同时更新分类目录下的分类sort和指标sort
  397. //更新当前移动的分类或者指标sort
  398. var parentChartClassifyInfo *data_manage.ChartClassify
  399. if parentClassifyId > 0 {
  400. parentChartClassifyInfo, err = data_manage.GetChartClassifyById(parentClassifyId)
  401. if err != nil {
  402. errMsg = "移动失败"
  403. err = errors.New("获取上级分类信息失败,Err:" + err.Error())
  404. return
  405. }
  406. }
  407. //如果有传入 上一个兄弟节点分类id
  408. var (
  409. chartClassifyInfo *data_manage.ChartClassify
  410. prevClassify *data_manage.ChartClassify
  411. nextClassify *data_manage.ChartClassify
  412. chartInfo *data_manage.ChartInfo
  413. prevChartInfo *data_manage.ChartInfo
  414. nextChartInfo *data_manage.ChartInfo
  415. prevSort int
  416. nextSort int
  417. )
  418. // 移动对象为分类, 判断权限
  419. if chartInfoId == 0 {
  420. chartClassifyInfo, err = data_manage.GetChartClassifyById(classifyId)
  421. if err != nil {
  422. if err.Error() == utils.ErrNoRow() {
  423. errMsg = "当前分类不存在"
  424. err = errors.New("获取分类信息失败,Err:" + err.Error())
  425. return
  426. }
  427. errMsg = "移动失败"
  428. err = errors.New("获取分类信息失败,Err:" + err.Error())
  429. return
  430. }
  431. if parentClassifyId > 0 && parentChartClassifyInfo.Level == 6 {
  432. errMsg = "最高只支持添加6级分类"
  433. err = errors.New(errMsg)
  434. return
  435. }
  436. // 如果是移动目录, 那么校验一下父级目录下是否有重名目录
  437. exists, e := data_manage.GetChartClassifyByParentIdAndName(parentClassifyId, chartClassifyInfo.ChartClassifyName, classifyId)
  438. if e != nil && e.Error() != utils.ErrNoRow() {
  439. errMsg = "移动失败"
  440. err = fmt.Errorf("获取父级分类下的同名分类失败, Err: %s", e.Error())
  441. return
  442. }
  443. if exists != nil {
  444. errMsg = "移动失败,分类名称已存在"
  445. return
  446. }
  447. // 权限校验
  448. {
  449. // 已授权分类id
  450. permissionClassifyIdList, tmpErr := data_manage_permission.GetUserChartClassifyPermissionList(sysUser.AdminId, classifyId)
  451. if tmpErr != nil {
  452. errMsg = "移动失败"
  453. err = errors.New("获取已授权分类id数据失败,Err:" + tmpErr.Error())
  454. return
  455. }
  456. // 数据权限
  457. haveOperaAuth := data_manage_permission.CheckChartClassifyPermissionByPermissionIdList(chartClassifyInfo.IsJoinPermission, chartClassifyInfo.ChartClassifyId, permissionClassifyIdList)
  458. button := GetChartClassifyOpButton(sysUser, chartClassifyInfo.SysUserId, haveOperaAuth)
  459. if !button.MoveButton {
  460. errMsg = "无操作权限"
  461. err = errors.New(errMsg)
  462. return
  463. }
  464. }
  465. } else {
  466. chartInfo, err = data_manage.GetChartInfoById(req.ChartInfoId)
  467. if err != nil {
  468. if err.Error() == utils.ErrNoRow() {
  469. errMsg = "当前图表不存在"
  470. err = errors.New("获取分类信息失败,Err:" + err.Error())
  471. return
  472. }
  473. errMsg = "移动失败"
  474. err = errors.New("获取分类信息失败,Err:" + err.Error())
  475. return
  476. }
  477. if parentClassifyId == 0 {
  478. errMsg = "移动失败,图表必须挂在分类下"
  479. err = errors.New(errMsg)
  480. return
  481. }
  482. var haveOperaAuth bool
  483. // 权限校验
  484. {
  485. haveOperaAuth, err = data_manage_permission.CheckChartPermissionByChartInfoId(chartInfo.ChartInfoId, chartInfo.ChartClassifyId, chartInfo.IsJoinPermission, sysUser.AdminId)
  486. if err != nil {
  487. errMsg = "移动失败"
  488. err = errors.New("获取图表权限信息失败,Err:" + err.Error())
  489. return
  490. }
  491. }
  492. // 移动权限校验
  493. button := GetChartOpButton(sysUser, chartInfo.SysUserId, haveOperaAuth)
  494. if !button.MoveButton {
  495. errMsg = "无操作权限"
  496. err = errors.New(errMsg)
  497. return
  498. }
  499. }
  500. if prevClassifyId > 0 {
  501. prevClassify, err = data_manage.GetChartClassifyById(prevClassifyId)
  502. if err != nil {
  503. errMsg = "移动失败"
  504. err = errors.New("获取上一个兄弟节点分类信息失败,Err:" + err.Error())
  505. return
  506. }
  507. prevSort = prevClassify.Sort
  508. } else if prevChartInfoId > 0 {
  509. prevChartInfo, err = data_manage.GetChartInfoById(prevChartInfoId)
  510. if err != nil {
  511. errMsg = "移动失败"
  512. err = errors.New("获取上一个兄弟节点分类信息失败,Err:" + err.Error())
  513. return
  514. }
  515. prevSort = prevChartInfo.Sort
  516. }
  517. if nextClassifyId > 0 {
  518. //下一个兄弟节点
  519. nextClassify, err = data_manage.GetChartClassifyById(nextClassifyId)
  520. if err != nil {
  521. errMsg = "移动失败"
  522. err = errors.New("获取下一个兄弟节点分类信息失败,Err:" + err.Error())
  523. return
  524. }
  525. nextSort = nextClassify.Sort
  526. } else if nextChartInfoId > 0 {
  527. //下一个兄弟节点
  528. nextChartInfo, err = data_manage.GetChartInfoById(nextChartInfoId)
  529. if err != nil {
  530. errMsg = "移动失败"
  531. err = errors.New("获取下一个兄弟节点分类信息失败,Err:" + err.Error())
  532. return
  533. }
  534. nextSort = nextChartInfo.Sort
  535. }
  536. err, errMsg = moveChartClassify(parentChartClassifyInfo, chartClassifyInfo, prevClassify, nextClassify, chartInfo, prevChartInfo, nextChartInfo, parentClassifyId, prevSort, nextSort, source)
  537. return
  538. }
  539. // moveChartClassify 移动图表分类
  540. func moveChartClassify(parentChartClassifyInfo, chartClassifyInfo, prevClassify, nextClassify *data_manage.ChartClassify, chartInfo, prevChartInfo, nextChartInfo *data_manage.ChartInfo, parentClassifyId, prevSort, nextSort, source int) (err error, errMsg string) {
  541. updateCol := make([]string, 0)
  542. // 移动对象为分类, 判断分类是否存在
  543. if chartClassifyInfo != nil {
  544. oldParentId := chartClassifyInfo.ParentId
  545. oldLevel := chartClassifyInfo.Level
  546. var classifyIds []int
  547. if oldParentId != parentClassifyId {
  548. //更新子分类对应的level
  549. childList, e, m := GetChildChartClassifyByClassifyId(chartClassifyInfo.ChartClassifyId)
  550. if e != nil {
  551. errMsg = "移动失败"
  552. err = errors.New("查询子分类失败,Err:" + e.Error() + m)
  553. return
  554. }
  555. if len(childList) > 0 {
  556. for _, v := range childList {
  557. if v.ChartClassifyId == chartClassifyInfo.ChartClassifyId {
  558. continue
  559. }
  560. classifyIds = append(classifyIds, v.ChartClassifyId)
  561. }
  562. }
  563. }
  564. //判断上级id是否一致,如果不一致的话,那么需要移动该分类层级
  565. if chartClassifyInfo.ParentId != parentClassifyId && parentClassifyId != 0 {
  566. if chartClassifyInfo.Level != parentChartClassifyInfo.Level+1 { //禁止层级调整
  567. errMsg = "移动失败"
  568. err = errors.New("不支持目录层级变更")
  569. return
  570. }
  571. chartClassifyInfo.ParentId = parentChartClassifyInfo.ChartClassifyId
  572. chartClassifyInfo.RootId = parentChartClassifyInfo.RootId
  573. chartClassifyInfo.Level = parentChartClassifyInfo.Level + 1
  574. chartClassifyInfo.ModifyTime = time.Now()
  575. updateCol = append(updateCol, "ParentId", "RootId", "Level", "ModifyTime")
  576. } else if chartClassifyInfo.ParentId != parentClassifyId && parentClassifyId == 0 {
  577. errMsg = "移动失败"
  578. err = errors.New("不支持目录层级变更")
  579. return
  580. }
  581. if prevSort > 0 {
  582. //如果是移动在两个兄弟节点之间
  583. if nextSort > 0 {
  584. //下一个兄弟节点
  585. //如果上一个兄弟与下一个兄弟的排序权重是一致的,那么需要将下一个兄弟(以及下个兄弟的同样排序权重)的排序权重+2,自己变成上一个兄弟的排序权重+1
  586. if prevSort == nextSort || prevSort == chartClassifyInfo.Sort {
  587. //变更兄弟节点的排序
  588. updateSortStr := `sort + 2`
  589. //变更分类
  590. if prevClassify != nil {
  591. _ = data_manage.UpdateChartClassifySortByParentIdAndSource(parentClassifyId, prevClassify.ChartClassifyId, prevClassify.Sort, updateSortStr, source)
  592. } else {
  593. _ = data_manage.UpdateChartClassifySortByParentIdAndSource(parentClassifyId, 0, prevSort, updateSortStr, source)
  594. }
  595. //变更图表
  596. if prevChartInfo != nil {
  597. //变更兄弟节点的排序
  598. _ = data_manage.UpdateChartInfoSortByClassifyIdV2(parentClassifyId, prevSort, prevChartInfo.ChartInfoId, updateSortStr)
  599. } else {
  600. _ = data_manage.UpdateChartInfoSortByClassifyIdV2(parentClassifyId, prevSort, 0, updateSortStr)
  601. }
  602. } else {
  603. //如果下一个兄弟的排序权重正好是上个兄弟节点的下一层,那么需要再加一层了
  604. if nextSort-prevSort == 1 {
  605. //变更兄弟节点的排序
  606. updateSortStr := `sort + 1`
  607. //变更分类
  608. if prevClassify != nil {
  609. _ = data_manage.UpdateChartClassifySortByParentIdAndSource(parentClassifyId, prevClassify.ChartClassifyId, prevSort, updateSortStr, source)
  610. } else {
  611. _ = data_manage.UpdateChartClassifySortByParentIdAndSource(parentClassifyId, 0, prevSort, updateSortStr, source)
  612. }
  613. //变更图表
  614. if prevChartInfo != nil {
  615. //变更兄弟节点的排序
  616. _ = data_manage.UpdateChartInfoSortByClassifyIdV2(parentClassifyId, prevSort, prevChartInfo.ChartInfoId, updateSortStr)
  617. } else {
  618. _ = data_manage.UpdateChartInfoSortByClassifyIdV2(parentClassifyId, prevSort, 0, updateSortStr)
  619. }
  620. }
  621. }
  622. }
  623. chartClassifyInfo.Sort = prevSort + 1
  624. chartClassifyInfo.ModifyTime = time.Now()
  625. updateCol = append(updateCol, "Sort", "ModifyTime")
  626. } else if prevClassify == nil && nextClassify == nil && prevChartInfo == nil && nextChartInfo == nil && parentClassifyId > 0 {
  627. //处理只拖动到目录里,默认放到目录底部的情况
  628. var maxSort int
  629. maxSort, err = GetChartClassifyMaxSort(parentClassifyId, source)
  630. if err != nil {
  631. errMsg = "移动失败"
  632. err = errors.New("查询组内排序信息失败,Err:" + err.Error())
  633. return
  634. }
  635. chartClassifyInfo.Sort = maxSort + 1 //那就是排在组内最后一位
  636. chartClassifyInfo.ModifyTime = time.Now()
  637. updateCol = append(updateCol, "Sort", "ModifyTime")
  638. } else {
  639. // 拖动到父级分类的第一位
  640. firstClassify, tmpErr := data_manage.GetFirstChartClassifyByParentId(parentClassifyId)
  641. if tmpErr != nil && tmpErr.Error() != utils.ErrNoRow() {
  642. errMsg = "移动失败"
  643. err = errors.New("获取获取当前父级分类下的排序第一条的分类信息失败,Err:" + tmpErr.Error())
  644. return
  645. }
  646. //如果该分类下存在其他分类,且第一个其他分类的排序等于0,那么需要调整排序
  647. if firstClassify != nil && firstClassify.Sort == 0 {
  648. updateSortStr := ` sort + 1 `
  649. _ = data_manage.UpdateChartClassifySortByParentIdAndSource(parentClassifyId, firstClassify.ChartClassifyId-1, 0, updateSortStr, source)
  650. //该分类下的所有图表也需要+1
  651. _ = data_manage.UpdateChartInfoSortByClassifyIdV2(parentClassifyId, 0, 0, updateSortStr)
  652. } else {
  653. //如果该分类下存在图表,且第一个图表的排序等于0,那么需要调整排序
  654. firstEdb, tErr := data_manage.GetFirstChartInfoByClassifyId(parentClassifyId)
  655. if tErr != nil && tErr.Error() != utils.ErrNoRow() {
  656. errMsg = "移动失败"
  657. err = errors.New("获取获取当前父级分类下的排序第一条的分类信息失败,Err:" + tErr.Error())
  658. return
  659. }
  660. //如果该分类下存在其他分类,且第一个其他分类的排序等于0,那么需要调整排序
  661. if firstEdb != nil && firstEdb.Sort == 0 {
  662. updateSortStr := ` sort + 1 `
  663. _ = data_manage.UpdateChartInfoSortByClassifyIdV2(parentClassifyId, 0, firstEdb.ChartInfoId-1, updateSortStr)
  664. _ = data_manage.UpdateChartClassifySortByParentIdAndSource(parentClassifyId, 0, 0, updateSortStr, source)
  665. }
  666. }
  667. chartClassifyInfo.Sort = 0 //那就是排在第一位
  668. chartClassifyInfo.ModifyTime = time.Now()
  669. updateCol = append(updateCol, "Sort", "ModifyTime")
  670. }
  671. //更新
  672. if len(updateCol) > 0 {
  673. err = chartClassifyInfo.Update(updateCol)
  674. if err != nil {
  675. errMsg = "移动失败"
  676. err = errors.New("修改失败,Err:" + err.Error())
  677. return
  678. }
  679. //更新对应分类的root_id和层级
  680. if oldParentId != parentClassifyId {
  681. if len(classifyIds) > 0 {
  682. levelStep := chartClassifyInfo.Level - oldLevel
  683. err = data_manage.UpdateEdbClassifyChildByParentClassifyId(classifyIds, chartClassifyInfo.RootId, levelStep)
  684. if err != nil {
  685. errMsg = "移动失败"
  686. err = errors.New("更新子分类失败,Err:" + err.Error())
  687. return
  688. }
  689. }
  690. }
  691. }
  692. } else {
  693. if chartInfo == nil {
  694. errMsg = "当前图表不存在"
  695. err = errors.New(errMsg)
  696. return
  697. }
  698. //如果改变了分类,那么移动该图表数据
  699. if chartInfo.ChartClassifyId != parentClassifyId {
  700. chartInfo.ChartClassifyId = parentClassifyId
  701. chartInfo.ModifyTime = time.Now()
  702. updateCol = append(updateCol, "ChartClassifyId", "ModifyTime")
  703. }
  704. if prevSort > 0 {
  705. //如果是移动在两个兄弟节点之间
  706. if nextSort > 0 {
  707. //下一个兄弟节点
  708. //如果上一个兄弟与下一个兄弟的排序权重是一致的,那么需要将下一个兄弟(以及下个兄弟的同样排序权重)的排序权重+2,自己变成上一个兄弟的排序权重+1
  709. if prevSort == nextSort || prevSort == chartInfo.Sort {
  710. //变更兄弟节点的排序
  711. updateSortStr := `sort + 2`
  712. //变更分类
  713. if prevClassify != nil {
  714. _ = data_manage.UpdateChartClassifySortByParentIdAndSource(parentClassifyId, prevClassify.ChartClassifyId, prevClassify.Sort, updateSortStr, source)
  715. } else {
  716. _ = data_manage.UpdateChartClassifySortByParentIdAndSource(parentClassifyId, 0, prevSort, updateSortStr, source)
  717. }
  718. //变更图表
  719. if prevChartInfo != nil {
  720. //变更兄弟节点的排序
  721. _ = data_manage.UpdateChartInfoSortByClassifyIdV2(parentClassifyId, prevSort, prevChartInfo.ChartInfoId, updateSortStr)
  722. } else {
  723. _ = data_manage.UpdateChartInfoSortByClassifyIdV2(parentClassifyId, prevSort, 0, updateSortStr)
  724. }
  725. } else {
  726. //如果下一个兄弟的排序权重正好是上个兄弟节点的下一层,那么需要再加一层了
  727. if nextSort-prevSort == 1 {
  728. //变更兄弟节点的排序
  729. updateSortStr := `sort + 1`
  730. //变更分类
  731. if prevClassify != nil {
  732. _ = data_manage.UpdateChartClassifySortByParentIdAndSource(parentClassifyId, prevClassify.ChartClassifyId, prevSort, updateSortStr, source)
  733. } else {
  734. _ = data_manage.UpdateChartClassifySortByParentIdAndSource(parentClassifyId, 0, prevSort, updateSortStr, source)
  735. }
  736. //变更图表
  737. if prevChartInfo != nil {
  738. //变更兄弟节点的排序
  739. _ = data_manage.UpdateChartInfoSortByClassifyIdV2(parentClassifyId, prevSort, prevChartInfo.ChartInfoId, updateSortStr)
  740. } else {
  741. _ = data_manage.UpdateChartInfoSortByClassifyIdV2(parentClassifyId, prevSort, 0, updateSortStr)
  742. }
  743. }
  744. }
  745. }
  746. chartInfo.Sort = prevSort + 1
  747. chartInfo.ModifyTime = time.Now()
  748. updateCol = append(updateCol, "Sort", "ModifyTime")
  749. } else if prevClassify == nil && nextClassify == nil && prevChartInfo == nil && nextChartInfo == nil && parentClassifyId > 0 {
  750. //处理只拖动到目录里,默认放到目录底部的情况
  751. var maxSort int
  752. maxSort, err = GetChartClassifyMaxSort(parentClassifyId, source)
  753. if err != nil {
  754. errMsg = "移动失败"
  755. err = errors.New("查询组内排序信息失败,Err:" + err.Error())
  756. return
  757. }
  758. chartInfo.Sort = maxSort + 1 //那就是排在组内最后一位
  759. chartInfo.ModifyTime = time.Now()
  760. updateCol = append(updateCol, "Sort", "ModifyTime")
  761. } else {
  762. // 拖动到父级分类的第一位
  763. firstClassify, tmpErr := data_manage.GetFirstChartClassifyByParentId(parentClassifyId)
  764. if tmpErr != nil && tmpErr.Error() != utils.ErrNoRow() {
  765. errMsg = "移动失败"
  766. err = errors.New("获取获取当前父级分类下的排序第一条的分类信息失败,Err:" + tmpErr.Error())
  767. return
  768. }
  769. //如果该分类下存在其他分类,且第一个其他分类的排序等于0,那么需要调整排序
  770. if firstClassify != nil && firstClassify.Sort == 0 {
  771. updateSortStr := ` sort + 1 `
  772. _ = data_manage.UpdateChartClassifySortByParentIdAndSource(parentClassifyId, firstClassify.ChartClassifyId-1, 0, updateSortStr, source)
  773. //该分类下的所有图表也需要+1
  774. _ = data_manage.UpdateChartInfoSortByClassifyIdV2(parentClassifyId, 0, 0, updateSortStr)
  775. } else {
  776. //如果该分类下存在图表,且第一个图表的排序等于0,那么需要调整排序
  777. firstEdb, tErr := data_manage.GetFirstChartInfoByClassifyId(parentClassifyId)
  778. if tErr != nil && tErr.Error() != utils.ErrNoRow() {
  779. errMsg = "移动失败"
  780. err = errors.New("获取获取当前父级分类下的排序第一条的分类信息失败,Err:" + tErr.Error())
  781. return
  782. }
  783. //如果该分类下存在其他分类,且第一个其他分类的排序等于0,那么需要调整排序
  784. if firstEdb != nil && firstEdb.Sort == 0 {
  785. updateSortStr := ` sort + 1 `
  786. _ = data_manage.UpdateChartInfoSortByClassifyIdV2(parentClassifyId, 0, firstEdb.ChartInfoId-1, updateSortStr)
  787. _ = data_manage.UpdateChartClassifySortByParentIdAndSource(parentClassifyId, 0, 0, updateSortStr, source)
  788. }
  789. }
  790. chartInfo.Sort = 0 //那就是排在第一位
  791. chartInfo.ModifyTime = time.Now()
  792. updateCol = append(updateCol, "Sort", "ModifyTime")
  793. }
  794. //更新
  795. if len(updateCol) > 0 {
  796. err = chartInfo.Update(updateCol)
  797. if err != nil {
  798. errMsg = "移动失败"
  799. err = errors.New("修改失败,Err:" + err.Error())
  800. return
  801. }
  802. }
  803. }
  804. return
  805. }
  806. func GetChildChartClassifyByClassifyId(targetClassifyId int) (targetList []*data_manage.ChartClassifyIdItems, err error, errMsg string) {
  807. //判断是否是挂在顶级目录下
  808. targetClassify, err := data_manage.GetChartClassifyById(targetClassifyId)
  809. if err != nil {
  810. if err.Error() == utils.ErrNoRow() {
  811. errMsg = "当前分类不存在"
  812. err = errors.New(errMsg)
  813. return
  814. }
  815. errMsg = "获取失败"
  816. err = errors.New("获取分类信息失败,Err:" + err.Error())
  817. return
  818. }
  819. orderStr := ` order by level asc, sort asc, chart_classify_id asc`
  820. tmpList, err := data_manage.GetChartClassifyByRootIdLevel(targetClassify.RootId, targetClassify.Source, orderStr)
  821. if err != nil && err.Error() != utils.ErrNoRow() {
  822. errMsg = "获取失败"
  823. err = errors.New("获取数据失败,Err:" + err.Error())
  824. return
  825. }
  826. idMap := make(map[int]struct{})
  827. if len(tmpList) > 0 {
  828. for _, v := range tmpList {
  829. if v.ChartClassifyId == targetClassify.ChartClassifyId {
  830. idMap[v.ChartClassifyId] = struct{}{}
  831. }
  832. }
  833. for _, v := range tmpList {
  834. if _, ok := idMap[v.ParentId]; ok {
  835. idMap[v.ChartClassifyId] = struct{}{}
  836. }
  837. }
  838. for _, v := range tmpList {
  839. if _, ok := idMap[v.ChartClassifyId]; ok {
  840. targetItem := new(data_manage.ChartClassifyIdItems)
  841. targetItem.ChartClassifyId = v.ChartClassifyId
  842. targetItem.ParentId = v.ParentId
  843. targetItem.RootId = v.RootId
  844. targetItem.UniqueCode = v.UniqueCode
  845. targetItem.Level = v.Level
  846. targetItem.ChartClassifyName = v.ChartClassifyName
  847. targetItem.IsJoinPermission = v.IsJoinPermission
  848. targetList = append(targetList, targetItem)
  849. }
  850. }
  851. }
  852. return
  853. }
  854. func GetChartClassifyMaxSort(parentId, source int) (maxSort int, err error) {
  855. //获取该层级下最大的排序数
  856. classifyMaxSort, err := data_manage.GetChartClassifyMaxSort(parentId, source)
  857. if err != nil {
  858. return
  859. }
  860. maxSort = classifyMaxSort
  861. edbMaxSort, err := data_manage.GetChartInfoMaxSortByClassifyId(parentId)
  862. if err != nil {
  863. return
  864. }
  865. if maxSort < edbMaxSort {
  866. maxSort = edbMaxSort
  867. }
  868. return
  869. }
  870. // GetChartClassifyTreeRecursive 递归获取分类树形结构
  871. func GetChartClassifyTreeRecursive(list []*data_manage.ChartClassifyItems, parentId int) []*data_manage.ChartClassifyItems {
  872. res := make([]*data_manage.ChartClassifyItems, 0)
  873. for _, v := range list {
  874. if v.ParentId == parentId {
  875. v.Children = GetChartClassifyTreeRecursive(list, v.ChartClassifyId)
  876. res = append(res, v)
  877. }
  878. }
  879. return res
  880. }
  881. // GetChartClassifyParentRecursive 根据子目录递归父级目录
  882. func GetChartClassifyParentRecursive(list []*data_manage.ChartClassifyItems, classifyId int) []*data_manage.ChartClassifyItems {
  883. res := make([]*data_manage.ChartClassifyItems, 0)
  884. for _, v := range list {
  885. if v.ChartClassifyId == classifyId {
  886. if v.ParentId > 0 {
  887. res = GetChartClassifyParentRecursive(list, v.ParentId)
  888. }
  889. res = append(res, v)
  890. }
  891. }
  892. return res
  893. }
  894. // 修改图表分类,可以修改父级
  895. func EditChartClassifyV2(chartClassifyId, praentId, source int, chartClassifyName, lang string) (classifyInfo *data_manage.ChartClassify, err error, errMsg string, isSendEmail bool) {
  896. isSendEmail = true
  897. errMsg = "保存失败"
  898. // 获取分类信息
  899. classifyInfo, err = data_manage.GetChartClassifyById(chartClassifyId)
  900. if err != nil {
  901. return
  902. }
  903. if praentId != classifyInfo.ParentId {
  904. parentClassifyInfo, e := data_manage.GetChartClassifyById(praentId)
  905. if e != nil {
  906. err = e
  907. return
  908. }
  909. if classifyInfo.Level != parentClassifyInfo.Level-1 {
  910. err = errors.New("父级分类层级异常")
  911. }
  912. }
  913. // 分类来源校验
  914. if classifyInfo.Source != source {
  915. errMsg = "图表分类异常"
  916. err = errors.New(fmt.Sprintf("图表分类来源异常,修改来源:%d,分类来源:%d", source, classifyInfo.Source))
  917. isSendEmail = false
  918. return
  919. }
  920. // 需要变更的字段
  921. updateCols := make([]string, 0)
  922. if praentId != classifyInfo.ParentId {
  923. classifyInfo.ParentId = praentId
  924. classifyInfo.ModifyTime = time.Now()
  925. updateCols = append(updateCols, "ParentId")
  926. }
  927. // 语言版本校验
  928. switch lang {
  929. case utils.EnLangVersion:
  930. if classifyInfo.ChartClassifyNameEn != chartClassifyName {
  931. count, tmpErr := data_manage.GetChartClassifyEnCount(chartClassifyName, classifyInfo.ParentId, source)
  932. if tmpErr != nil {
  933. errMsg = "判断名称是否已存在失败"
  934. err = errors.New("判断名称是否已存在失败,Err:" + tmpErr.Error())
  935. return
  936. }
  937. if count > 0 {
  938. errMsg = "分类名称已存在,请重新输入"
  939. err = errors.New(errMsg)
  940. isSendEmail = false
  941. return
  942. }
  943. classifyInfo.ChartClassifyNameEn = chartClassifyName
  944. classifyInfo.ModifyTime = time.Now()
  945. updateCols = append(updateCols, "ChartClassifyNameEn", "ModifyTime")
  946. }
  947. default:
  948. if classifyInfo.ChartClassifyName != chartClassifyName {
  949. count, tmpErr := data_manage.GetChartClassifyCount(chartClassifyName, classifyInfo.ParentId, source)
  950. if tmpErr != nil {
  951. errMsg = "判断名称是否已存在失败"
  952. err = errors.New("判断名称是否已存在失败,Err:" + tmpErr.Error())
  953. return
  954. }
  955. if count > 0 {
  956. errMsg = "分类名称已存在,请重新输入"
  957. err = errors.New(errMsg)
  958. isSendEmail = false
  959. return
  960. }
  961. classifyInfo.ChartClassifyName = chartClassifyName
  962. classifyInfo.ModifyTime = time.Now()
  963. updateCols = append(updateCols, "ChartClassifyName", "ModifyTime")
  964. }
  965. }
  966. if len(updateCols) > 0 {
  967. err = classifyInfo.Update(updateCols)
  968. }
  969. return
  970. }
  971. // RemoveNotChartClassifyItemsMakeTree
  972. // @Description: 过滤名下没有自己图表的分类,并生成树形结构
  973. // @author: Roc
  974. // @datetime 2024-09-11 16:59:39
  975. // @param node *data_manage.ChartClassifyItems
  976. // @param selfChartClassifyIdList []int
  977. // @return isSelf bool
  978. func RemoveNotChartClassifyItemsMakeTree(node *data_manage.ChartClassifyItems, selfChartClassifyIdList []int) (isSelf bool) {
  979. if utils.InArrayByInt(selfChartClassifyIdList, node.ChartClassifyId) {
  980. return true
  981. }
  982. childrenList := make([]*data_manage.ChartClassifyItems, 0)
  983. for _, child := range node.Children {
  984. if tmpIsSelf := RemoveNotChartClassifyItemsMakeTree(child, selfChartClassifyIdList); tmpIsSelf {
  985. childrenList = append(childrenList, child)
  986. isSelf = tmpIsSelf
  987. }
  988. }
  989. node.Children = childrenList
  990. return
  991. }
  992. // GetChartClassifyChildrenRecursive 根据父目录递归子级目录
  993. func GetChartClassifyChildrenRecursive(list []*data_manage.ChartClassifyItems, parentId int) []*data_manage.ChartClassifyItems {
  994. var res []*data_manage.ChartClassifyItems
  995. for _, v := range list {
  996. if v.ParentId == parentId {
  997. // 递归调用以获取更深层次的子级
  998. children := GetChartClassifyChildrenRecursive(list, v.ChartClassifyId)
  999. // 将当前节点和所有子节点添加到结果中
  1000. res = append(res, v)
  1001. res = append(res, children...)
  1002. } else if v.ChartClassifyId == parentId {
  1003. // 将当前节点添加到结果中
  1004. res = append(res, v)
  1005. }
  1006. }
  1007. return res
  1008. }
  1009. // GetChartClassifyChildrenRecursiveByParentIds 根据父目录递归子级目录
  1010. func GetChartClassifyChildrenRecursiveByParentIds(list []*data_manage.ChartClassifyItems, parentIds []string) []*data_manage.ChartClassifyItems {
  1011. var res []*data_manage.ChartClassifyItems
  1012. for _, v := range list {
  1013. for _, id := range parentIds {
  1014. parentId, _ := strconv.Atoi(id)
  1015. if v.ParentId == parentId || v.ChartClassifyId == parentId {
  1016. // 递归调用以获取更深层次的子级
  1017. children := GetChartClassifyChildrenRecursive(list, v.ChartClassifyId)
  1018. // 将当前节点和所有子节点添加到结果中
  1019. res = append(res, v)
  1020. res = append(res, children...)
  1021. }
  1022. }
  1023. }
  1024. return res
  1025. }
  1026. // GetChartClassifyRelation 获取图库分类的父级关系 map[classifyId]map[parentId]struct{} classifyId为图库classify_id, map[parentId]struct{}为当前分类id中所有的父级id
  1027. func GetChartClassifyRelation() (classifyRelationMap map[int]map[int]struct{}, err error) {
  1028. classifyAll, err := data_manage.GetChartClassifyBySource(utils.CHART_SOURCE_DEFAULT)
  1029. if err != nil {
  1030. return
  1031. }
  1032. classifyRelationMap = make(map[int]map[int]struct{})
  1033. for _, classify := range classifyAll {
  1034. if classify.ParentId == 0 {
  1035. classifyRelationMap[classify.ChartClassifyId] = make(map[int]struct{})
  1036. continue
  1037. }
  1038. if parent, ok := classifyRelationMap[classify.ParentId]; !ok {
  1039. continue
  1040. } else {
  1041. if _, ok := classifyRelationMap[classify.ChartClassifyId]; !ok {
  1042. classifyRelationMap[classify.ChartClassifyId] = make(map[int]struct{})
  1043. classifyRelationMap[classify.ChartClassifyId][classify.ParentId] = struct{}{}
  1044. for k, v := range parent {
  1045. classifyRelationMap[classify.ChartClassifyId][k] = v
  1046. }
  1047. } else {
  1048. classifyRelationMap[classify.ChartClassifyId][classify.ParentId] = struct{}{}
  1049. for k, v := range parent {
  1050. classifyRelationMap[classify.ChartClassifyId][k] = v
  1051. }
  1052. }
  1053. }
  1054. }
  1055. return
  1056. }
  1057. func GetChartClassifyByIsMe(adminId, parentId, source int, classifyList []*data_manage.ChartClassifyItems) (list []*data_manage.ChartClassifyItems, err error) {
  1058. chartInfoList, err := data_manage.GetChartInfoByAdminId([]int{source}, adminId)
  1059. if err != nil {
  1060. return
  1061. }
  1062. classifyRelation, err := GetChartClassifyRelation()
  1063. if err != nil {
  1064. return
  1065. }
  1066. chartInfoRelationClassifyMap := make(map[int]struct{})
  1067. for _, classify := range chartInfoList {
  1068. if classify.ChartClassifyId == 0 {
  1069. continue
  1070. } else {
  1071. if relationClassify, ok := classifyRelation[classify.ChartClassifyId]; ok {
  1072. chartInfoRelationClassifyMap[classify.ChartClassifyId] = struct{}{}
  1073. for k, v := range relationClassify {
  1074. chartInfoRelationClassifyMap[k] = v
  1075. }
  1076. }
  1077. }
  1078. }
  1079. for _, classify := range classifyList {
  1080. if classify.ChartInfoId > 0 {
  1081. list = append(list, classify)
  1082. } else {
  1083. if _, ok := chartInfoRelationClassifyMap[classify.ChartClassifyId]; ok {
  1084. list = append(list, classify)
  1085. }
  1086. }
  1087. }
  1088. return
  1089. }