chart_classify.go 29 KB

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