chart_classify.go 31 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899
  1. package services
  2. import (
  3. "errors"
  4. "eta/eta_forum_admin/models"
  5. "eta/eta_forum_admin/models/system"
  6. "eta/eta_forum_admin/utils"
  7. "fmt"
  8. "strconv"
  9. "strings"
  10. "time"
  11. )
  12. func ChartClassifyItemsMakeTree(sysUser *system.Admin, allNode []*models.ChartClassifyItems, node *models.ChartClassifyItems) {
  13. // node.HaveOperaAuth = data_manage_permission.CheckEdbClassifyPermissionByPermissionIdList(node.IsJoinPermission, node.ChartClassifyId, permissionClassifyIdList)
  14. button := GetChartClassifyOpButton(sysUser, node.SysUserId, node.HaveOperaAuth)
  15. node.Button = button
  16. childs, _ := chartClassifyHaveChild(allNode, node) //判断节点是否有子节点并返回
  17. if len(childs) > 0 {
  18. for _, child := range childs {
  19. //child.HaveOperaAuth = data_manage_permission.CheckEdbClassifyPermissionByPermissionIdList(child.IsJoinPermission, child.ChartClassifyId, permissionClassifyIdList)
  20. childButton := GetChartClassifyOpButton(sysUser, child.SysUserId, child.HaveOperaAuth)
  21. if child.Level == 3 {
  22. childButton.AddButton = false //第三级的话,默认图表都是没有添加按钮的
  23. }
  24. child.Button = childButton
  25. //node.Children = append(node.Children, child)
  26. }
  27. node.Children = append(node.Children, childs[0:]...) //添加子节点
  28. for _, v := range childs {
  29. //v.HaveOperaAuth = data_manage_permission.CheckEdbClassifyPermissionByPermissionIdList(v.IsJoinPermission, v.ChartClassifyId, permissionClassifyIdList)
  30. //查询子节点的子节点,并添加到子节点
  31. _, has := chartClassifyHaveChild(allNode, v)
  32. if has {
  33. ChartClassifyItemsMakeTree(sysUser, allNode, v) //递归添加节点
  34. } else {
  35. childrenArr := make([]*models.ChartClassifyItems, 0)
  36. v.Children = childrenArr
  37. }
  38. }
  39. } else {
  40. childrenArr := make([]*models.ChartClassifyItems, 0)
  41. node.Children = childrenArr
  42. }
  43. }
  44. func chartClassifyHaveChild(allNode []*models.ChartClassifyItems, node *models.ChartClassifyItems) (childs []*models.ChartClassifyItems, yes bool) {
  45. for _, v := range allNode {
  46. if v.ParentId == node.ChartClassifyId {
  47. childs = append(childs, v)
  48. }
  49. }
  50. if len(childs) > 0 {
  51. yes = true
  52. }
  53. return
  54. }
  55. func FixChartClassify() {
  56. //新增三级分类
  57. list, err := models.GetChartClassifyByLevel(2, 1)
  58. if err != nil {
  59. return
  60. }
  61. for _, v := range list {
  62. classify := new(models.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 := models.AddChartClassify(classify)
  80. if err != nil {
  81. return
  82. }
  83. err = models.FIXChartClassifyId(int(id), v.ChartClassifyId)
  84. if err != nil {
  85. return
  86. }
  87. }
  88. }
  89. func FixChartClassifySysUserId() {
  90. //获取所有父级分类信息
  91. list, err := models.GetChartClassifyByLevel(2, 1)
  92. if err != nil {
  93. return
  94. }
  95. for _, v := range list {
  96. err := models.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 models.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 models.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. // AddChartClassify
  143. // @Description: 添加图表分类
  144. // @author: Roc
  145. // @datetime 2024-04-17 14:48:23
  146. // @param chartClassifyName string
  147. // @param parentId int
  148. // @param level int
  149. // @param source int
  150. // @param lang string
  151. // @param sysUser *system.Admin
  152. // @return classifyInfo *models.ChartClassify
  153. // @return err error
  154. // @return errMsg string
  155. // @return isSendEmail bool
  156. func AddChartClassify(chartClassifyName string, parentId, source int, lang string, sysUser *system.Admin) (classifyInfo *models.ChartClassify, err error, errMsg string, isSendEmail bool) {
  157. isSendEmail = true
  158. errMsg = "保存分类失败"
  159. // 校验分类名称相同的数量
  160. {
  161. var count int
  162. switch lang {
  163. case utils.EnLangVersion:
  164. count, err = models.GetChartClassifyEnCount(chartClassifyName, parentId, source)
  165. default:
  166. count, err = models.GetChartClassifyCount(chartClassifyName, parentId, source)
  167. }
  168. if err != nil {
  169. errMsg = "判断名称是否已存在失败"
  170. return
  171. }
  172. if count > 0 {
  173. errMsg = "分类名称已存在,请重新输入"
  174. err = errors.New(errMsg)
  175. isSendEmail = false
  176. return
  177. }
  178. }
  179. // 查询level值
  180. level := 0
  181. levelPath := ""
  182. if parentId > 0 {
  183. parentClassify, tErr := models.GetChartClassifyById(parentId)
  184. if tErr != nil {
  185. if tErr.Error() == utils.ErrNoRow() {
  186. errMsg = "父级分类不存在"
  187. err = errors.New(errMsg)
  188. return
  189. }
  190. errMsg = "获取失败"
  191. err = errors.New("获取分类信息失败,Err:" + tErr.Error())
  192. return
  193. }
  194. level = parentClassify.Level
  195. levelPath = parentClassify.LevelPath
  196. }
  197. //获取该层级下最大的排序数
  198. maxSort, err := models.GetChartClassifyMaxSort(parentId, source)
  199. classifyInfo = new(models.ChartClassify)
  200. classifyInfo.ParentId = parentId
  201. classifyInfo.ChartClassifyName = chartClassifyName
  202. classifyInfo.ChartClassifyNameEn = chartClassifyName
  203. classifyInfo.HasData = 0
  204. classifyInfo.CreateTime = time.Now()
  205. classifyInfo.ModifyTime = time.Now()
  206. classifyInfo.SysUserId = sysUser.AdminId
  207. classifyInfo.SysUserRealName = sysUser.RealName
  208. classifyInfo.Level = level + 1
  209. timestamp := strconv.FormatInt(time.Now().UnixNano(), 10)
  210. classifyInfo.UniqueCode = utils.MD5(utils.DATA_PREFIX + "_" + timestamp)
  211. classifyInfo.Sort = maxSort + 1
  212. classifyInfo.Source = source
  213. classifyId, e := models.AddChartClassify(classifyInfo)
  214. if e != nil {
  215. errMsg = "保存分类失败"
  216. err = errors.New("保存分类失败,Err:" + e.Error())
  217. return
  218. }
  219. if parentId > 0 {
  220. levelPath = fmt.Sprintf("%s%d,", levelPath, classifyId)
  221. } else {
  222. levelPath = fmt.Sprintf("%d,", classifyId)
  223. }
  224. classifyInfo.ChartClassifyId = int(classifyId)
  225. classifyInfo.LevelPath = levelPath
  226. e = classifyInfo.Update([]string{"LevelPath"})
  227. if e != nil {
  228. errMsg = "保存分类失败"
  229. err = errors.New("保存分类失败,Err:" + e.Error())
  230. return
  231. }
  232. return
  233. }
  234. // EditChartClassify
  235. // @Description: 修改图表分类
  236. // @author: Roc
  237. // @datetime 2024-04-17 14:59:37
  238. // @param chartClassifyId int
  239. // @param source int
  240. // @param chartClassifyName string
  241. // @param lang string
  242. // @param sysUser *system.Admin
  243. // @return classifyInfo *models.ChartClassify
  244. // @return err error
  245. // @return errMsg string
  246. // @return isSendEmail bool
  247. func EditChartClassify(chartClassifyId, source int, chartClassifyName, lang string, sysUser *system.Admin) (classifyInfo *models.ChartClassify, err error, errMsg string, isSendEmail bool) {
  248. isSendEmail = true
  249. errMsg = "保存失败"
  250. // 获取分类信息
  251. classifyInfo, err = models.GetChartClassifyById(chartClassifyId)
  252. if err != nil {
  253. return
  254. }
  255. // 分类来源校验
  256. if classifyInfo.Source != source {
  257. errMsg = "图表分类异常"
  258. err = errors.New(fmt.Sprintf("图表分类来源异常,修改来源:%d,分类来源:%d", source, classifyInfo.Source))
  259. isSendEmail = false
  260. return
  261. }
  262. // 需要变更的字段
  263. updateCols := make([]string, 0)
  264. // 语言版本校验
  265. switch lang {
  266. case utils.EnLangVersion:
  267. if classifyInfo.ChartClassifyNameEn != chartClassifyName {
  268. count, tmpErr := models.GetChartClassifyEnCount(chartClassifyName, classifyInfo.ParentId, source)
  269. if tmpErr != nil {
  270. errMsg = "判断名称是否已存在失败"
  271. err = errors.New("判断名称是否已存在失败,Err:" + tmpErr.Error())
  272. return
  273. }
  274. if count > 0 {
  275. errMsg = "分类名称已存在,请重新输入"
  276. err = errors.New(errMsg)
  277. isSendEmail = false
  278. return
  279. }
  280. classifyInfo.ChartClassifyNameEn = chartClassifyName
  281. classifyInfo.ModifyTime = time.Now()
  282. updateCols = append(updateCols, "ChartClassifyNameEn", "ModifyTime")
  283. }
  284. default:
  285. if classifyInfo.ChartClassifyName != chartClassifyName {
  286. count, tmpErr := models.GetChartClassifyCount(chartClassifyName, classifyInfo.ParentId, source)
  287. if tmpErr != nil {
  288. errMsg = "判断名称是否已存在失败"
  289. err = errors.New("判断名称是否已存在失败,Err:" + tmpErr.Error())
  290. return
  291. }
  292. if count > 0 {
  293. errMsg = "分类名称已存在,请重新输入"
  294. err = errors.New(errMsg)
  295. isSendEmail = false
  296. return
  297. }
  298. classifyInfo.ChartClassifyName = chartClassifyName
  299. classifyInfo.ModifyTime = time.Now()
  300. updateCols = append(updateCols, "ChartClassifyName", "ModifyTime")
  301. }
  302. }
  303. if len(updateCols) > 0 {
  304. err = classifyInfo.Update(updateCols)
  305. }
  306. return
  307. }
  308. // MoveChartClassify 移动图表分类
  309. func MoveChartClassify(req models.MoveChartClassifyReq, sysUser *system.Admin, classifyType int) (err error, errMsg string) {
  310. // req.ClassifyId, req.ParentClassifyId, req.PrevClassifyId, req.NextClassifyId
  311. classifyId := req.ClassifyId
  312. parentClassifyId := req.ParentClassifyId
  313. prevClassifyId := req.PrevClassifyId
  314. nextClassifyId := req.NextClassifyId
  315. chartInfoId := req.ChartInfoId
  316. prevChartInfoId := req.PrevChartInfoId
  317. nextChartInfoId := req.NextChartInfoId
  318. //首先确定移动的对象是分类还是指标
  319. //判断上一个节点是分类还是指标
  320. //判断下一个节点是分类还是指标
  321. //同时更新分类目录下的分类sort和指标sort
  322. //更新当前移动的分类或者指标sort
  323. var parentEdbClassifyInfo *models.ChartClassify
  324. if parentClassifyId > 0 {
  325. parentEdbClassifyInfo, err = models.GetChartClassifyById(parentClassifyId)
  326. if err != nil {
  327. errMsg = "移动失败"
  328. err = errors.New("获取上级分类信息失败,Err:" + err.Error())
  329. return
  330. }
  331. }
  332. //如果有传入 上一个兄弟节点分类id
  333. var (
  334. edbClassifyInfo *models.ChartClassify
  335. prevClassify *models.ChartClassify
  336. nextClassify *models.ChartClassify
  337. chartInfo *models.ChartInfo
  338. prevChartInfo *models.ChartInfo
  339. nextChartInfo *models.ChartInfo
  340. prevSort int
  341. nextSort int
  342. )
  343. // 移动对象为分类, 判断权限
  344. if chartInfoId == 0 {
  345. edbClassifyInfo, err = models.GetChartClassifyById(classifyId)
  346. if err != nil {
  347. if err.Error() == utils.ErrNoRow() {
  348. errMsg = "当前分类不存在"
  349. err = errors.New("获取分类信息失败,Err:" + err.Error())
  350. return
  351. }
  352. errMsg = "移动失败"
  353. err = errors.New("获取分类信息失败,Err:" + err.Error())
  354. return
  355. }
  356. if parentClassifyId > 0 && parentEdbClassifyInfo.Level == 6 {
  357. errMsg = "最高只支持添加6级分类"
  358. err = errors.New(errMsg)
  359. return
  360. }
  361. // 如果是移动目录, 那么校验一下父级目录下是否有重名目录
  362. exists, e := models.GetChartClassifyByParentIdAndName(parentClassifyId, edbClassifyInfo.ChartClassifyName, classifyId)
  363. if e != nil && e.Error() != utils.ErrNoRow() {
  364. errMsg = "移动失败"
  365. err = fmt.Errorf("获取父级分类下的同名分类失败, Err: %s", e.Error())
  366. return
  367. }
  368. if exists != nil {
  369. errMsg = "移动失败,分类名称已存在"
  370. return
  371. }
  372. } else {
  373. chartInfo, err = models.GetChartInfoById(req.ChartInfoId)
  374. if err != nil {
  375. if err.Error() == utils.ErrNoRow() {
  376. errMsg = "当前指标不存在"
  377. err = errors.New("获取分类信息失败,Err:" + err.Error())
  378. return
  379. }
  380. errMsg = "移动失败"
  381. err = errors.New("获取分类信息失败,Err:" + err.Error())
  382. return
  383. }
  384. if parentClassifyId == 0 {
  385. errMsg = "移动失败,指标必须挂在分类下"
  386. err = errors.New(errMsg)
  387. return
  388. }
  389. }
  390. if prevClassifyId > 0 {
  391. prevClassify, err = models.GetChartClassifyById(prevClassifyId)
  392. if err != nil {
  393. errMsg = "移动失败"
  394. err = errors.New("获取上一个兄弟节点分类信息失败,Err:" + err.Error())
  395. return
  396. }
  397. prevSort = prevClassify.Sort
  398. } else if prevChartInfoId > 0 {
  399. prevChartInfo, err = models.GetChartInfoById(prevChartInfoId)
  400. if err != nil {
  401. errMsg = "移动失败"
  402. err = errors.New("获取上一个兄弟节点分类信息失败,Err:" + err.Error())
  403. return
  404. }
  405. prevSort = prevChartInfo.Sort
  406. }
  407. if nextClassifyId > 0 {
  408. //下一个兄弟节点
  409. nextClassify, err = models.GetChartClassifyById(nextClassifyId)
  410. if err != nil {
  411. errMsg = "移动失败"
  412. err = errors.New("获取下一个兄弟节点分类信息失败,Err:" + err.Error())
  413. return
  414. }
  415. nextSort = nextClassify.Sort
  416. } else if nextChartInfoId > 0 {
  417. //下一个兄弟节点
  418. nextChartInfo, err = models.GetChartInfoById(nextChartInfoId)
  419. if err != nil {
  420. errMsg = "移动失败"
  421. err = errors.New("获取下一个兄弟节点分类信息失败,Err:" + err.Error())
  422. return
  423. }
  424. nextSort = nextChartInfo.Sort
  425. }
  426. err, errMsg = moveChartClassify(parentEdbClassifyInfo, edbClassifyInfo, prevClassify, nextClassify, chartInfo, prevChartInfo, nextChartInfo, parentClassifyId, prevSort, nextSort, classifyType)
  427. return
  428. }
  429. // moveChartClassify 移动图表分类
  430. func moveChartClassify(parentClassifyInfo, edbClassifyInfo, prevClassify, nextClassify *models.ChartClassify, chartInfo, prevChartInfo, nextChartInfo *models.ChartInfo, parentClassifyId int, prevSort, nextSort int, classifyType int) (err error, errMsg string) {
  431. updateCol := make([]string, 0)
  432. // 移动对象为分类, 判断分类是否存在
  433. if edbClassifyInfo != nil {
  434. oldParentId := edbClassifyInfo.ParentId
  435. //oldLevel := edbClassifyInfo.Level
  436. //var classifyIds []int
  437. if oldParentId != parentClassifyId {
  438. //更新子分类对应的level
  439. /*childList, e, m := GetChildClassifyByClassifyId(edbClassifyInfo.ClassifyId)
  440. if e != nil {
  441. errMsg = "移动失败"
  442. err = errors.New("查询子分类失败,Err:" + e.Error() + m)
  443. return
  444. }
  445. if len(childList) > 0 {
  446. for _, v := range childList {
  447. if v.ClassifyId == edbClassifyInfo.ClassifyId {
  448. continue
  449. }
  450. classifyIds = append(classifyIds, v.ClassifyId)
  451. }
  452. }*/
  453. }
  454. //判断上级id是否一致,如果不一致的话,那么需要移动该分类层级
  455. if edbClassifyInfo.ParentId != parentClassifyId && parentClassifyId != 0 {
  456. if edbClassifyInfo.Level != parentClassifyInfo.Level+1 { //禁止层级调整
  457. errMsg = "移动失败"
  458. err = errors.New("不支持目录层级变更")
  459. return
  460. }
  461. edbClassifyInfo.ParentId = parentClassifyInfo.ChartClassifyId
  462. edbClassifyInfo.Level = parentClassifyInfo.Level + 1
  463. edbClassifyInfo.ModifyTime = time.Now()
  464. updateCol = append(updateCol, "ParentId", "Level", "ModifyTime")
  465. } else if edbClassifyInfo.ParentId != parentClassifyId && parentClassifyId == 0 {
  466. errMsg = "移动失败"
  467. err = errors.New("不支持目录层级变更")
  468. return
  469. }
  470. if prevSort > 0 {
  471. //如果是移动在两个兄弟节点之间
  472. if nextSort > 0 {
  473. //下一个兄弟节点
  474. //如果上一个兄弟与下一个兄弟的排序权重是一致的,那么需要将下一个兄弟(以及下个兄弟的同样排序权重)的排序权重+2,自己变成上一个兄弟的排序权重+1
  475. if prevSort == nextSort || prevSort == edbClassifyInfo.Sort {
  476. //变更兄弟节点的排序
  477. updateSortStr := `sort + 2`
  478. //变更分类
  479. if prevClassify != nil {
  480. _ = models.UpdateChartClassifySortByParentId(parentClassifyId, prevClassify.ChartClassifyId, prevClassify.Sort, updateSortStr, classifyType)
  481. } else {
  482. _ = models.UpdateChartClassifySortByParentId(parentClassifyId, 0, prevSort, updateSortStr, classifyType)
  483. }
  484. //变更指标
  485. if prevChartInfo != nil {
  486. //变更兄弟节点的排序
  487. _ = models.UpdateChartInfoSortByClassifyId(parentClassifyId, prevSort, prevChartInfo.ChartInfoId, []int{classifyType}, updateSortStr)
  488. } else {
  489. _ = models.UpdateChartInfoSortByClassifyId(parentClassifyId, prevSort, 0, []int{classifyType}, updateSortStr)
  490. }
  491. } else {
  492. //如果下一个兄弟的排序权重正好是上个兄弟节点的下一层,那么需要再加一层了
  493. if nextSort-prevSort == 1 {
  494. //变更兄弟节点的排序
  495. updateSortStr := `sort + 1`
  496. //变更分类
  497. if prevClassify != nil {
  498. _ = models.UpdateChartClassifySortByParentId(parentClassifyId, prevClassify.ChartClassifyId, prevSort, updateSortStr, classifyType)
  499. } else {
  500. _ = models.UpdateChartClassifySortByParentId(parentClassifyId, 0, prevSort, updateSortStr, classifyType)
  501. }
  502. //变更指标
  503. if prevChartInfo != nil {
  504. //变更兄弟节点的排序
  505. _ = models.UpdateChartInfoSortByClassifyId(parentClassifyId, prevSort, prevChartInfo.ChartInfoId, []int{classifyType}, updateSortStr)
  506. } else {
  507. _ = models.UpdateChartInfoSortByClassifyId(parentClassifyId, prevSort, 0, []int{classifyType}, updateSortStr)
  508. }
  509. }
  510. }
  511. }
  512. edbClassifyInfo.Sort = prevSort + 1
  513. edbClassifyInfo.ModifyTime = time.Now()
  514. updateCol = append(updateCol, "Sort", "ModifyTime")
  515. } else if prevClassify == nil && nextClassify == nil && prevChartInfo == nil && nextChartInfo == nil && parentClassifyId > 0 {
  516. //处理只拖动到目录里,默认放到目录底部的情况
  517. var maxSort int
  518. maxSort, err = models.GetChartClassifyMaxSort(parentClassifyId, classifyType)
  519. if err != nil {
  520. errMsg = "移动失败"
  521. err = errors.New("查询组内排序信息失败,Err:" + err.Error())
  522. return
  523. }
  524. edbClassifyInfo.Sort = maxSort + 1 //那就是排在组内最后一位
  525. edbClassifyInfo.ModifyTime = time.Now()
  526. updateCol = append(updateCol, "Sort", "ModifyTime")
  527. } else {
  528. // 拖动到父级分类的第一位
  529. firstClassify, tmpErr := models.GetFirstChartClassifyByParentId(parentClassifyId)
  530. if tmpErr != nil && tmpErr.Error() != utils.ErrNoRow() {
  531. errMsg = "移动失败"
  532. err = errors.New("获取获取当前父级分类下的排序第一条的分类信息失败,Err:" + tmpErr.Error())
  533. return
  534. }
  535. //如果该分类下存在其他分类,且第一个其他分类的排序等于0,那么需要调整排序
  536. if firstClassify != nil && firstClassify.Sort == 0 {
  537. updateSortStr := ` sort + 1 `
  538. _ = models.UpdateChartClassifySortByParentId(parentClassifyId, firstClassify.ChartClassifyId-1, 0, updateSortStr, classifyType)
  539. //该分类下的所有指标也需要+1
  540. _ = models.UpdateChartInfoSortByClassifyId(parentClassifyId, 0, 0, []int{classifyType}, updateSortStr)
  541. } else {
  542. //如果该分类下存在指标,且第一个指标的排序等于0,那么需要调整排序
  543. firstEdb, tErr := models.GetFirstChartInfoByClassifyId(parentClassifyId)
  544. if tErr != nil && tErr.Error() != utils.ErrNoRow() {
  545. errMsg = "移动失败"
  546. err = errors.New("获取获取当前父级分类下的排序第一条的分类信息失败,Err:" + tErr.Error())
  547. return
  548. }
  549. //如果该分类下存在其他分类,且第一个其他分类的排序等于0,那么需要调整排序
  550. if firstEdb != nil && firstEdb.Sort == 0 {
  551. updateSortStr := ` sort + 1 `
  552. _ = models.UpdateChartInfoSortByClassifyId(parentClassifyId, 0, firstEdb.ChartInfoId-1, []int{classifyType}, updateSortStr)
  553. _ = models.UpdateChartClassifySortByParentId(parentClassifyId, 0, 0, updateSortStr, classifyType)
  554. }
  555. }
  556. edbClassifyInfo.Sort = 0 //那就是排在第一位
  557. edbClassifyInfo.ModifyTime = time.Now()
  558. updateCol = append(updateCol, "Sort", "ModifyTime")
  559. }
  560. //更新
  561. if len(updateCol) > 0 {
  562. err = edbClassifyInfo.Update(updateCol)
  563. if err != nil {
  564. errMsg = "移动失败"
  565. err = errors.New("修改失败,Err:" + err.Error())
  566. return
  567. }
  568. // todo 更新对应分类的root_id和层级
  569. /*if oldParentId != parentClassifyId {
  570. if len(classifyIds) > 0 {
  571. levelStep := edbClassifyInfo.Level - oldLevel
  572. err = models.UpdateEdbClassifyChildByParentClassifyId(classifyIds, edbClassifyInfo.RootId, levelStep)
  573. if err != nil {
  574. errMsg = "移动失败"
  575. err = errors.New("更新子分类失败,Err:" + err.Error())
  576. return
  577. }
  578. }
  579. }*/
  580. if oldParentId != parentClassifyId {
  581. oldLevelPath := edbClassifyInfo.LevelPath
  582. levelPath := fmt.Sprintf("%s%d,", parentClassifyInfo.LevelPath, edbClassifyInfo.ChartClassifyId)
  583. edbClassifyInfo.LevelPath = levelPath
  584. err = edbClassifyInfo.Update([]string{"LevelPath"})
  585. if err != nil {
  586. err = fmt.Errorf("修改失败,Err:" + err.Error())
  587. return
  588. }
  589. //更新子分类的levelpath
  590. tmpList, e := models.GetChartClassifyByLevelPath(oldLevelPath)
  591. if e != nil {
  592. err = fmt.Errorf("保存分类失败,Err:" + e.Error())
  593. return
  594. }
  595. // 把原先的父级levePath,替换成最新的父级序列
  596. for _, tmp := range tmpList {
  597. //获取字符串前缀的位置
  598. after, _ := strings.CutPrefix(tmp.LevelPath, oldLevelPath)
  599. fmt.Println("after", after)
  600. // 拼接字符串
  601. if after != "" {
  602. tmp.LevelPath = levelPath + after
  603. tmp.ModifyTime = time.Now()
  604. e = tmp.Update([]string{"LevelPath", "ModifyTime"})
  605. if e != nil {
  606. err = fmt.Errorf("修改子分类,Err:" + e.Error())
  607. return
  608. }
  609. }
  610. }
  611. }
  612. }
  613. } else {
  614. if chartInfo == nil {
  615. errMsg = "当前图表不存在"
  616. err = errors.New(errMsg)
  617. return
  618. }
  619. //如果改变了分类,那么移动该指标数据
  620. if chartInfo.ChartClassifyId != parentClassifyId {
  621. chartInfo.ChartClassifyId = parentClassifyId
  622. chartInfo.ModifyTime = time.Now()
  623. updateCol = append(updateCol, "ChartClassifyId", "ModifyTime")
  624. }
  625. if prevSort > 0 {
  626. //如果是移动在两个兄弟节点之间
  627. if nextSort > 0 {
  628. //下一个兄弟节点
  629. //如果上一个兄弟与下一个兄弟的排序权重是一致的,那么需要将下一个兄弟(以及下个兄弟的同样排序权重)的排序权重+2,自己变成上一个兄弟的排序权重+1
  630. if prevSort == nextSort || prevSort == chartInfo.Sort {
  631. //变更兄弟节点的排序
  632. updateSortStr := `sort + 2`
  633. //变更分类
  634. if prevClassify != nil {
  635. _ = models.UpdateChartClassifySortByParentId(parentClassifyId, prevClassify.ChartClassifyId, prevClassify.Sort, updateSortStr, classifyType)
  636. } else {
  637. _ = models.UpdateChartClassifySortByParentId(parentClassifyId, 0, prevSort, updateSortStr, classifyType)
  638. }
  639. //变更指标
  640. if prevChartInfo != nil {
  641. //变更兄弟节点的排序
  642. _ = models.UpdateChartInfoSortByClassifyId(parentClassifyId, prevSort, prevChartInfo.ChartInfoId, []int{classifyType}, updateSortStr)
  643. } else {
  644. _ = models.UpdateChartInfoSortByClassifyId(parentClassifyId, prevSort, 0, []int{classifyType}, updateSortStr)
  645. }
  646. } else {
  647. //如果下一个兄弟的排序权重正好是上个兄弟节点的下一层,那么需要再加一层了
  648. if nextSort-prevSort == 1 {
  649. //变更兄弟节点的排序
  650. updateSortStr := `sort + 1`
  651. //变更分类
  652. if prevClassify != nil {
  653. _ = models.UpdateChartClassifySortByParentId(parentClassifyId, prevClassify.ChartClassifyId, prevSort, updateSortStr, classifyType)
  654. } else {
  655. _ = models.UpdateChartClassifySortByParentId(parentClassifyId, 0, prevSort, updateSortStr, classifyType)
  656. }
  657. //变更指标
  658. if prevChartInfo != nil {
  659. //变更兄弟节点的排序
  660. _ = models.UpdateChartInfoSortByClassifyId(parentClassifyId, prevSort, prevChartInfo.ChartInfoId, []int{classifyType}, updateSortStr)
  661. } else {
  662. _ = models.UpdateChartInfoSortByClassifyId(parentClassifyId, prevSort, 0, []int{classifyType}, updateSortStr)
  663. }
  664. }
  665. }
  666. }
  667. chartInfo.Sort = prevSort + 1
  668. chartInfo.ModifyTime = time.Now()
  669. updateCol = append(updateCol, "Sort", "ModifyTime")
  670. } else if prevClassify == nil && nextClassify == nil && prevChartInfo == nil && nextChartInfo == nil && parentClassifyId > 0 {
  671. //处理只拖动到目录里,默认放到目录底部的情况
  672. var maxSort int
  673. maxSort, err = models.GetChartClassifyMaxSort(parentClassifyId, classifyType)
  674. if err != nil {
  675. errMsg = "移动失败"
  676. err = errors.New("查询组内排序信息失败,Err:" + err.Error())
  677. return
  678. }
  679. chartInfo.Sort = maxSort + 1 //那就是排在组内最后一位
  680. chartInfo.ModifyTime = time.Now()
  681. updateCol = append(updateCol, "Sort", "ModifyTime")
  682. } else {
  683. // 拖动到父级分类的第一位
  684. firstClassify, tmpErr := models.GetFirstChartClassifyByParentId(parentClassifyId)
  685. if tmpErr != nil && tmpErr.Error() != utils.ErrNoRow() {
  686. errMsg = "移动失败"
  687. err = errors.New("获取获取当前父级分类下的排序第一条的分类信息失败,Err:" + tmpErr.Error())
  688. return
  689. }
  690. //如果该分类下存在其他分类,且第一个其他分类的排序等于0,那么需要调整排序
  691. if firstClassify != nil && firstClassify.Sort == 0 {
  692. updateSortStr := ` sort + 1 `
  693. _ = models.UpdateChartClassifySortByParentId(parentClassifyId, firstClassify.ChartClassifyId-1, 0, updateSortStr, classifyType)
  694. //该分类下的所有指标也需要+1
  695. _ = models.UpdateChartInfoSortByClassifyId(parentClassifyId, 0, 0, []int{classifyType}, updateSortStr)
  696. } else {
  697. //如果该分类下存在指标,且第一个指标的排序等于0,那么需要调整排序
  698. firstEdb, tErr := models.GetFirstChartInfoByClassifyId(parentClassifyId)
  699. if tErr != nil && tErr.Error() != utils.ErrNoRow() {
  700. errMsg = "移动失败"
  701. err = errors.New("获取获取当前父级分类下的排序第一条的分类信息失败,Err:" + tErr.Error())
  702. return
  703. }
  704. //如果该分类下存在其他分类,且第一个其他分类的排序等于0,那么需要调整排序
  705. if firstEdb != nil && firstEdb.Sort == 0 {
  706. updateSortStr := ` sort + 1 `
  707. _ = models.UpdateChartInfoSortByClassifyId(parentClassifyId, 0, firstEdb.ChartInfoId-1, []int{classifyType}, updateSortStr)
  708. _ = models.UpdateChartClassifySortByParentId(parentClassifyId, 0, 0, updateSortStr, classifyType)
  709. }
  710. }
  711. chartInfo.Sort = 0 //那就是排在第一位
  712. chartInfo.ModifyTime = time.Now()
  713. updateCol = append(updateCol, "Sort", "ModifyTime")
  714. }
  715. //更新
  716. if len(updateCol) > 0 {
  717. err = chartInfo.Update(updateCol)
  718. if err != nil {
  719. errMsg = "移动失败"
  720. err = errors.New("修改失败,Err:" + err.Error())
  721. return
  722. }
  723. }
  724. }
  725. return
  726. }
  727. // GetChartClassifyListByAdmins 获取某个创建者的图表分类
  728. func GetChartClassifyListByAdmins(adminIdsSlice []int) (hasClassifyMap map[int]bool, err error, errMsg string) {
  729. classifyAll, err := models.GetChartClassify()
  730. if err != nil && err.Error() != utils.ErrNoRow() {
  731. errMsg = "获取失败"
  732. return
  733. }
  734. if len(classifyAll) == 0 {
  735. return
  736. }
  737. allChartInfo, err := models.GetChartClassifyByAdminIdsAndLevel([]int{utils.CHART_SOURCE_DEFAULT}, adminIdsSlice)
  738. if err != nil && err.Error() != utils.ErrNoRow() {
  739. errMsg = "获取失败"
  740. return
  741. }
  742. if len(allChartInfo) == 0 {
  743. return
  744. }
  745. hasClassifyMap = make(map[int]bool)
  746. for _, v := range allChartInfo {
  747. hasClassifyMap[v.ChartClassifyId] = true
  748. }
  749. // 遍历分类,如果存在则返回
  750. for _, v := range classifyAll {
  751. if _, ok := hasClassifyMap[v.ChartClassifyId]; ok {
  752. hasClassifyMap[v.ParentId] = true
  753. }
  754. }
  755. // 遍历分类,如果存在则返回
  756. for _, v := range classifyAll {
  757. if _, ok := hasClassifyMap[v.ChartClassifyId]; ok {
  758. hasClassifyMap[v.ParentId] = true
  759. }
  760. }
  761. // 遍历分类,如果存在则返回
  762. for _, v := range classifyAll {
  763. if _, ok := hasClassifyMap[v.ChartClassifyId]; ok {
  764. hasClassifyMap[v.ParentId] = true
  765. }
  766. }
  767. return
  768. }
  769. // GetClassifyTreeRecursive 递归获取分类树形结构
  770. func GetClassifyTreeRecursive(list []*models.ChartClassifyItems, parentId int) []*models.ChartClassifyItems {
  771. res := make([]*models.ChartClassifyItems, 0)
  772. for _, v := range list {
  773. if v.ParentId == parentId {
  774. v.Children = GetClassifyTreeRecursive(list, v.ChartClassifyId)
  775. res = append(res, v)
  776. }
  777. }
  778. return res
  779. }
  780. func GetChildClassifyByClassifyId(targetClassifyId int) (targetList []*models.ChartClassifyItems, err error, errMsg string) {
  781. //判断是否是挂在顶级目录下
  782. targetClassify, err := models.GetChartClassifyById(targetClassifyId)
  783. if err != nil {
  784. if err.Error() == utils.ErrNoRow() {
  785. errMsg = "当前分类不存在"
  786. err = errors.New(errMsg)
  787. return
  788. }
  789. errMsg = "获取失败"
  790. err = errors.New("获取分类信息失败,Err:" + err.Error())
  791. return
  792. }
  793. tmpList, err := models.GetChartClassifyOrderByLevel()
  794. if err != nil && err.Error() != utils.ErrNoRow() {
  795. errMsg = "获取失败"
  796. err = errors.New("获取数据失败,Err:" + err.Error())
  797. return
  798. }
  799. idMap := make(map[int]struct{})
  800. if len(tmpList) > 0 {
  801. for _, v := range tmpList {
  802. if v.ChartClassifyId == targetClassify.ChartClassifyId {
  803. idMap[v.ChartClassifyId] = struct{}{}
  804. }
  805. }
  806. for _, v := range tmpList {
  807. if _, ok := idMap[v.ParentId]; ok {
  808. idMap[v.ChartClassifyId] = struct{}{}
  809. }
  810. }
  811. for _, v := range tmpList {
  812. if _, ok := idMap[v.ChartClassifyId]; ok {
  813. targetItem := new(models.ChartClassifyItems)
  814. targetItem.ChartClassifyId = v.ChartClassifyId
  815. targetItem.ParentId = v.ParentId
  816. targetItem.Level = v.Level
  817. targetItem.ChartClassifyName = v.ChartClassifyName
  818. targetItem.IsJoinPermission = v.IsJoinPermission
  819. targetList = append(targetList, targetItem)
  820. }
  821. }
  822. }
  823. return
  824. }