chart_classify.go 27 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791
  1. package data
  2. import (
  3. "errors"
  4. "eta_gn/eta_api/models/data_manage"
  5. "eta_gn/eta_api/models/system"
  6. "eta_gn/eta_api/services/data/data_manage_permission"
  7. "eta_gn/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) {
  24. node.HaveOperaAuth = data_manage_permission.CheckEdbClassifyPermissionByPermissionIdList(node.IsJoinPermission, node.ChartClassifyId, permissionClassifyIdList)
  25. button := GetChartClassifyOpButton(sysUser, node.SysUserId, node.HaveOperaAuth)
  26. node.Button = button
  27. childs, _ := chartClassifyHaveChild(allNode, node) //判断节点是否有子节点并返回
  28. if len(childs) > 0 {
  29. for _, child := range childs {
  30. child.HaveOperaAuth = data_manage_permission.CheckEdbClassifyPermissionByPermissionIdList(child.IsJoinPermission, child.ChartClassifyId, permissionClassifyIdList)
  31. childButton := GetChartClassifyOpButton(sysUser, child.SysUserId, child.HaveOperaAuth)
  32. if child.Level == 3 {
  33. childButton.AddButton = false //第三级的话,默认图表都是没有添加按钮的
  34. }
  35. child.Button = childButton
  36. }
  37. node.Children = append(node.Children, childs[0:]...) //添加子节点
  38. for _, v := range childs {
  39. v.HaveOperaAuth = data_manage_permission.CheckEdbClassifyPermissionByPermissionIdList(v.IsJoinPermission, v.ChartClassifyId, permissionClassifyIdList)
  40. _, has := chartClassifyHaveChild(allNode, v)
  41. if has {
  42. ChartClassifyItemsMakeTree(sysUser, allNode, v, permissionClassifyIdList) //递归添加节点
  43. } else {
  44. childrenArr := make([]*data_manage.ChartClassifyItems, 0)
  45. v.Children = childrenArr
  46. }
  47. }
  48. } else {
  49. childrenArr := make([]*data_manage.ChartClassifyItems, 0)
  50. node.Children = childrenArr
  51. }
  52. }
  53. func GetChartClassifyOpButton(sysUser *system.Admin, belongUserId int, haveOperaAuth bool) (button data_manage.ChartClassifyItemsButton) {
  54. if !haveOperaAuth {
  55. return
  56. }
  57. button.AddButton = true
  58. button.OpButton = true
  59. button.DeleteButton = true
  60. button.MoveButton = true
  61. return
  62. }
  63. func GetChartOpButton(sysUser *system.Admin, belongUserId int, haveOperaAuth bool) (button data_manage.ChartClassifyItemsButton) {
  64. if !haveOperaAuth {
  65. return
  66. }
  67. if sysUser.RoleTypeCode == utils.ROLE_TYPE_CODE_ADMIN || sysUser.RoleTypeCode == utils.ROLE_TYPE_CODE_FICC_ADMIN || sysUser.AdminId == belongUserId || sysUser.PredictEdbPermission == 1 {
  68. button.AddButton = true
  69. button.OpButton = true
  70. button.DeleteButton = true
  71. button.MoveButton = true
  72. }
  73. return
  74. }
  75. func HandleNoPermissionChart(allNodes []*data_manage.ChartClassifyItems, noPermissionChartIdMap map[int]bool, sysUserId int) (newAllNodes []*data_manage.ChartClassifyItems) {
  76. classifyIdList := make([]int, 0)
  77. newAllNodes = make([]*data_manage.ChartClassifyItems, 0)
  78. for _, node := range allNodes {
  79. tmpNodeInfo := *node
  80. tmpNodeList := make([]*data_manage.ChartClassifyItems, 0)
  81. if node.Children != nil {
  82. for _, chartInfo := range node.Children {
  83. if _, ok := noPermissionChartIdMap[chartInfo.ChartInfoId]; ok {
  84. continue
  85. }
  86. tmpNodeList = append(tmpNodeList, chartInfo)
  87. classifyIdList = append(classifyIdList, chartInfo.ChartClassifyId)
  88. }
  89. }
  90. tmpNodeInfo.Children = tmpNodeList
  91. newAllNodes = append(newAllNodes, &tmpNodeInfo)
  92. classifyIdList = append(classifyIdList, node.ChartClassifyId)
  93. }
  94. if len(classifyIdList) > 0 {
  95. chartClassifyList, err := data_manage.GetChartClassifyByIdList(classifyIdList)
  96. if err != nil {
  97. return
  98. }
  99. chartClassifyMap := make(map[int]*data_manage.ChartClassify)
  100. for _, v := range chartClassifyList {
  101. chartClassifyMap[v.ChartClassifyId] = v
  102. }
  103. permissionChartIdList, permissionClassifyIdList, err := data_manage_permission.GetUserChartAndClassifyPermissionList(sysUserId, 0, 0)
  104. if err != nil {
  105. return
  106. }
  107. for _, node := range newAllNodes {
  108. if node.Children != nil {
  109. for _, chartInfo := range node.Children {
  110. if currClassify, ok := chartClassifyMap[chartInfo.ChartClassifyId]; ok {
  111. chartInfo.HaveOperaAuth = data_manage_permission.CheckChartPermissionByPermissionIdList(chartInfo.IsJoinPermission, currClassify.IsJoinPermission, chartInfo.ChartInfoId, chartInfo.ChartClassifyId, permissionChartIdList, permissionClassifyIdList)
  112. }
  113. }
  114. }
  115. if currClassify, ok := chartClassifyMap[node.ChartClassifyId]; ok {
  116. node.HaveOperaAuth = data_manage_permission.CheckChartPermissionByPermissionIdList(node.IsJoinPermission, currClassify.IsJoinPermission, node.ChartInfoId, node.ChartClassifyId, permissionChartIdList, permissionClassifyIdList)
  117. }
  118. }
  119. }
  120. return
  121. }
  122. func AddChartClassify(chartClassifyName string, parentId, level, source int, lang string, sysUser *system.Admin) (classifyInfo *data_manage.ChartClassify, err error, errMsg string, isSendEmail bool) {
  123. isSendEmail = true
  124. errMsg = "保存分类失败"
  125. {
  126. var count int
  127. switch lang {
  128. case utils.EnLangVersion:
  129. count, err = data_manage.GetChartClassifyEnCount(chartClassifyName, parentId, source)
  130. default:
  131. count, err = data_manage.GetChartClassifyCount(chartClassifyName, parentId, source)
  132. }
  133. if err != nil {
  134. errMsg = "判断名称是否已存在失败"
  135. return
  136. }
  137. if count > 0 {
  138. errMsg = "分类名称已存在,请重新输入"
  139. err = errors.New(errMsg)
  140. isSendEmail = false
  141. return
  142. }
  143. }
  144. if level > 6 {
  145. errMsg = `最高只支持添加6级分类`
  146. return
  147. }
  148. maxSort, err := data_manage.GetChartClassifyMaxSort(parentId, source)
  149. if err != nil {
  150. errMsg = "获取失败"
  151. err = errors.New("查询排序信息失败,Err:" + err.Error())
  152. return
  153. }
  154. rootId := 0
  155. if parentId > 0 {
  156. parentClassify, tErr := data_manage.GetChartClassifyById(parentId)
  157. if tErr != nil {
  158. if utils.IsErrNoRow(tErr) {
  159. errMsg = "父级分类不存在"
  160. err = errors.New(errMsg)
  161. return
  162. }
  163. errMsg = "获取失败"
  164. err = errors.New("获取分类信息失败,Err:" + tErr.Error())
  165. return
  166. }
  167. rootId = parentClassify.RootId
  168. }
  169. classifyInfo = new(data_manage.ChartClassify)
  170. classifyInfo.ParentId = parentId
  171. classifyInfo.ChartClassifyName = chartClassifyName
  172. classifyInfo.ChartClassifyNameEn = chartClassifyName
  173. classifyInfo.HasData = 0
  174. classifyInfo.CreateTime = time.Now()
  175. classifyInfo.ModifyTime = time.Now()
  176. classifyInfo.SysUserId = sysUser.AdminId
  177. classifyInfo.SysUserRealName = sysUser.RealName
  178. classifyInfo.Level = level + 1
  179. timestamp := strconv.FormatInt(time.Now().UnixNano(), 10)
  180. classifyInfo.UniqueCode = utils.MD5(utils.DATA_PREFIX + "_" + timestamp)
  181. classifyInfo.Sort = maxSort + 1
  182. classifyInfo.Source = source
  183. classifyInfo.RootId = rootId
  184. newId, err := data_manage.AddChartClassify(classifyInfo)
  185. if err != nil {
  186. return
  187. }
  188. if parentId == 0 { //一级目录的rootId等于自己本身
  189. classifyInfo.RootId = int(newId)
  190. err = classifyInfo.Update([]string{"RootId"})
  191. if err != nil {
  192. errMsg = "更新分类失败"
  193. return
  194. }
  195. }
  196. if classifyInfo.Source == utils.CHART_SOURCE_DEFAULT {
  197. go data_manage_permission.InheritParentClassify(5, classifyInfo.Source, classifyInfo.ChartClassifyId, classifyInfo.ParentId, classifyInfo.ChartClassifyName)
  198. }
  199. return
  200. }
  201. func EditChartClassify(chartClassifyId, source int, chartClassifyName, lang string, sysUser *system.Admin) (classifyInfo *data_manage.ChartClassify, err error, errMsg string, isSendEmail bool) {
  202. isSendEmail = true
  203. errMsg = "保存失败"
  204. classifyInfo, err = data_manage.GetChartClassifyById(chartClassifyId)
  205. if err != nil {
  206. return
  207. }
  208. if classifyInfo.Source != source {
  209. errMsg = "图表分类异常"
  210. err = errors.New(fmt.Sprintf("图表分类来源异常,修改来源:%d,分类来源:%d", source, classifyInfo.Source))
  211. isSendEmail = false
  212. return
  213. }
  214. updateCols := make([]string, 0)
  215. switch lang {
  216. case utils.EnLangVersion:
  217. if classifyInfo.ChartClassifyNameEn != chartClassifyName {
  218. count, tmpErr := data_manage.GetChartClassifyEnCount(chartClassifyName, classifyInfo.ParentId, source)
  219. if tmpErr != nil {
  220. errMsg = "判断名称是否已存在失败"
  221. err = errors.New("判断名称是否已存在失败,Err:" + tmpErr.Error())
  222. return
  223. }
  224. if count > 0 {
  225. errMsg = "分类名称已存在,请重新输入"
  226. err = errors.New(errMsg)
  227. isSendEmail = false
  228. return
  229. }
  230. classifyInfo.ChartClassifyNameEn = chartClassifyName
  231. classifyInfo.ModifyTime = time.Now()
  232. updateCols = append(updateCols, "ChartClassifyNameEn", "ModifyTime")
  233. }
  234. default:
  235. if classifyInfo.ChartClassifyName != chartClassifyName {
  236. count, tmpErr := data_manage.GetChartClassifyCount(chartClassifyName, classifyInfo.ParentId, source)
  237. if tmpErr != nil {
  238. errMsg = "判断名称是否已存在失败"
  239. err = errors.New("判断名称是否已存在失败,Err:" + tmpErr.Error())
  240. return
  241. }
  242. if count > 0 {
  243. errMsg = "分类名称已存在,请重新输入"
  244. err = errors.New(errMsg)
  245. isSendEmail = false
  246. return
  247. }
  248. classifyInfo.ChartClassifyName = chartClassifyName
  249. classifyInfo.ModifyTime = time.Now()
  250. updateCols = append(updateCols, "ChartClassifyName", "ModifyTime")
  251. }
  252. }
  253. if len(updateCols) > 0 {
  254. err = classifyInfo.Update(updateCols)
  255. }
  256. return
  257. }
  258. func MoveChartClassify(req data_manage.MoveChartClassifyReq, sysUser *system.Admin, source int) (err error, errMsg string) {
  259. classifyId := req.ClassifyId
  260. parentClassifyId := req.ParentClassifyId
  261. prevClassifyId := req.PrevClassifyId
  262. nextClassifyId := req.NextClassifyId
  263. chartInfoId := req.ChartInfoId
  264. prevChartInfoId := req.PrevChartInfoId
  265. nextChartInfoId := req.NextChartInfoId
  266. var parentChartClassifyInfo *data_manage.ChartClassify
  267. if parentClassifyId > 0 {
  268. parentChartClassifyInfo, err = data_manage.GetChartClassifyById(parentClassifyId)
  269. if err != nil {
  270. errMsg = "移动失败"
  271. err = errors.New("获取上级分类信息失败,Err:" + err.Error())
  272. return
  273. }
  274. }
  275. var (
  276. chartClassifyInfo *data_manage.ChartClassify
  277. prevClassify *data_manage.ChartClassify
  278. nextClassify *data_manage.ChartClassify
  279. chartInfo *data_manage.ChartInfo
  280. prevChartInfo *data_manage.ChartInfo
  281. nextChartInfo *data_manage.ChartInfo
  282. prevSort int
  283. nextSort int
  284. )
  285. if chartInfoId == 0 {
  286. chartClassifyInfo, err = data_manage.GetChartClassifyById(classifyId)
  287. if err != nil {
  288. if utils.IsErrNoRow(err) {
  289. errMsg = "当前分类不存在"
  290. err = errors.New("获取分类信息失败,Err:" + err.Error())
  291. return
  292. }
  293. errMsg = "移动失败"
  294. err = errors.New("获取分类信息失败,Err:" + err.Error())
  295. return
  296. }
  297. if parentClassifyId > 0 && parentChartClassifyInfo.Level == 6 {
  298. errMsg = "最高只支持添加6级分类"
  299. err = errors.New(errMsg)
  300. return
  301. }
  302. exists, e := data_manage.GetChartClassifyByParentIdAndName(source, parentClassifyId, chartClassifyInfo.ChartClassifyName, classifyId)
  303. if e != nil && !utils.IsErrNoRow(e) {
  304. errMsg = "移动失败"
  305. err = fmt.Errorf("获取父级分类下的同名分类失败, Err: %s", e.Error())
  306. return
  307. }
  308. if exists != nil && exists.ChartClassifyId > 0 {
  309. errMsg = "移动失败,分类名称已存在"
  310. return
  311. }
  312. {
  313. permissionClassifyIdList, tmpErr := data_manage_permission.GetUserChartClassifyPermissionList(sysUser.AdminId, classifyId)
  314. if tmpErr != nil {
  315. errMsg = "移动失败"
  316. err = errors.New("获取已授权分类id数据失败,Err:" + tmpErr.Error())
  317. return
  318. }
  319. haveOperaAuth := data_manage_permission.CheckChartClassifyPermissionByPermissionIdList(chartClassifyInfo.IsJoinPermission, chartClassifyInfo.ChartClassifyId, permissionClassifyIdList)
  320. button := GetChartClassifyOpButton(sysUser, chartClassifyInfo.SysUserId, haveOperaAuth)
  321. if !button.MoveButton {
  322. errMsg = "无操作权限"
  323. err = errors.New(errMsg)
  324. return
  325. }
  326. }
  327. } else {
  328. chartInfo, err = data_manage.GetChartInfoById(req.ChartInfoId)
  329. if err != nil {
  330. if utils.IsErrNoRow(err) {
  331. errMsg = "当前图表不存在"
  332. err = errors.New("获取分类信息失败,Err:" + err.Error())
  333. return
  334. }
  335. errMsg = "移动失败"
  336. err = errors.New("获取分类信息失败,Err:" + err.Error())
  337. return
  338. }
  339. if parentClassifyId == 0 {
  340. errMsg = "移动失败,图表必须挂在分类下"
  341. err = errors.New(errMsg)
  342. return
  343. }
  344. var haveOperaAuth bool
  345. {
  346. haveOperaAuth, err = data_manage_permission.CheckChartPermissionByChartInfoId(chartInfo.ChartInfoId, chartInfo.ChartClassifyId, chartInfo.IsJoinPermission, sysUser.AdminId)
  347. if err != nil {
  348. errMsg = "移动失败"
  349. err = errors.New("获取图表权限信息失败,Err:" + err.Error())
  350. return
  351. }
  352. }
  353. button := GetChartOpButton(sysUser, chartInfo.SysUserId, haveOperaAuth)
  354. if !button.MoveButton {
  355. errMsg = "无操作权限"
  356. err = errors.New(errMsg)
  357. return
  358. }
  359. }
  360. if prevClassifyId > 0 {
  361. prevClassify, err = data_manage.GetChartClassifyById(prevClassifyId)
  362. if err != nil {
  363. errMsg = "移动失败"
  364. err = errors.New("获取上一个兄弟节点分类信息失败,Err:" + err.Error())
  365. return
  366. }
  367. prevSort = prevClassify.Sort
  368. } else if prevChartInfoId > 0 {
  369. prevChartInfo, err = data_manage.GetChartInfoById(prevChartInfoId)
  370. if err != nil {
  371. errMsg = "移动失败"
  372. err = errors.New("获取上一个兄弟节点分类信息失败,Err:" + err.Error())
  373. return
  374. }
  375. prevSort = prevChartInfo.Sort
  376. }
  377. if nextClassifyId > 0 {
  378. nextClassify, err = data_manage.GetChartClassifyById(nextClassifyId)
  379. if err != nil {
  380. errMsg = "移动失败"
  381. err = errors.New("获取下一个兄弟节点分类信息失败,Err:" + err.Error())
  382. return
  383. }
  384. nextSort = nextClassify.Sort
  385. } else if nextChartInfoId > 0 {
  386. nextChartInfo, err = data_manage.GetChartInfoById(nextChartInfoId)
  387. if err != nil {
  388. errMsg = "移动失败"
  389. err = errors.New("获取下一个兄弟节点分类信息失败,Err:" + err.Error())
  390. return
  391. }
  392. nextSort = nextChartInfo.Sort
  393. }
  394. err, errMsg = moveChartClassify(parentChartClassifyInfo, chartClassifyInfo, prevClassify, nextClassify, chartInfo, prevChartInfo, nextChartInfo, parentClassifyId, prevSort, nextSort, source)
  395. return
  396. }
  397. func moveChartClassify(parentChartClassifyInfo, chartClassifyInfo, prevClassify, nextClassify *data_manage.ChartClassify, chartInfo, prevChartInfo, nextChartInfo *data_manage.ChartInfo, parentClassifyId, prevSort, nextSort, source int) (err error, errMsg string) {
  398. updateCol := make([]string, 0)
  399. if chartClassifyInfo != nil {
  400. oldParentId := chartClassifyInfo.ParentId
  401. oldLevel := chartClassifyInfo.Level
  402. var classifyIds []int
  403. if oldParentId != parentClassifyId {
  404. childList, e, m := GetChildChartClassifyByClassifyId(chartClassifyInfo.ChartClassifyId)
  405. if e != nil {
  406. errMsg = "移动失败"
  407. err = errors.New("查询子分类失败,Err:" + e.Error() + m)
  408. return
  409. }
  410. if len(childList) > 0 {
  411. for _, v := range childList {
  412. if v.ChartClassifyId == chartClassifyInfo.ChartClassifyId {
  413. continue
  414. }
  415. classifyIds = append(classifyIds, v.ChartClassifyId)
  416. }
  417. }
  418. }
  419. if chartClassifyInfo.ParentId != parentClassifyId && parentClassifyId != 0 {
  420. if chartClassifyInfo.Level != parentChartClassifyInfo.Level+1 { //禁止层级调整
  421. errMsg = "移动失败"
  422. err = errors.New("不支持目录层级变更")
  423. return
  424. }
  425. chartClassifyInfo.ParentId = parentChartClassifyInfo.ChartClassifyId
  426. chartClassifyInfo.RootId = parentChartClassifyInfo.RootId
  427. chartClassifyInfo.Level = parentChartClassifyInfo.Level + 1
  428. chartClassifyInfo.ModifyTime = time.Now()
  429. updateCol = append(updateCol, "ParentId", "RootId", "Level", "ModifyTime")
  430. } else if chartClassifyInfo.ParentId != parentClassifyId && parentClassifyId == 0 {
  431. errMsg = "移动失败"
  432. err = errors.New("不支持目录层级变更")
  433. return
  434. }
  435. if prevSort > 0 {
  436. if nextSort > 0 {
  437. if prevSort == nextSort || prevSort == chartClassifyInfo.Sort {
  438. updateSortStr := `sort + 2`
  439. if prevClassify != nil {
  440. _ = data_manage.UpdateChartClassifySortByParentIdAndSource(parentClassifyId, prevClassify.ChartClassifyId, prevClassify.Sort, updateSortStr, source)
  441. } else {
  442. _ = data_manage.UpdateChartClassifySortByParentIdAndSource(parentClassifyId, 0, prevSort, updateSortStr, source)
  443. }
  444. if prevChartInfo != nil {
  445. _ = data_manage.UpdateChartInfoSortByClassifyIdV2(parentClassifyId, prevSort, prevChartInfo.ChartInfoId, updateSortStr)
  446. } else {
  447. _ = data_manage.UpdateChartInfoSortByClassifyIdV2(parentClassifyId, prevSort, 0, updateSortStr)
  448. }
  449. } else {
  450. if nextSort-prevSort == 1 {
  451. updateSortStr := `sort + 1`
  452. if prevClassify != nil {
  453. _ = data_manage.UpdateChartClassifySortByParentIdAndSource(parentClassifyId, prevClassify.ChartClassifyId, prevSort, updateSortStr, source)
  454. } else {
  455. _ = data_manage.UpdateChartClassifySortByParentIdAndSource(parentClassifyId, 0, prevSort, updateSortStr, source)
  456. }
  457. if prevChartInfo != nil {
  458. _ = data_manage.UpdateChartInfoSortByClassifyIdV2(parentClassifyId, prevSort, prevChartInfo.ChartInfoId, updateSortStr)
  459. } else {
  460. _ = data_manage.UpdateChartInfoSortByClassifyIdV2(parentClassifyId, prevSort, 0, updateSortStr)
  461. }
  462. }
  463. }
  464. }
  465. chartClassifyInfo.Sort = prevSort + 1
  466. chartClassifyInfo.ModifyTime = time.Now()
  467. updateCol = append(updateCol, "Sort", "ModifyTime")
  468. } else if prevClassify == nil && nextClassify == nil && prevChartInfo == nil && nextChartInfo == nil && parentClassifyId > 0 {
  469. var maxSort int
  470. maxSort, err = GetChartClassifyMaxSort(parentClassifyId, source)
  471. if err != nil {
  472. errMsg = "移动失败"
  473. err = errors.New("查询组内排序信息失败,Err:" + err.Error())
  474. return
  475. }
  476. chartClassifyInfo.Sort = maxSort + 1 //那就是排在组内最后一位
  477. chartClassifyInfo.ModifyTime = time.Now()
  478. updateCol = append(updateCol, "Sort", "ModifyTime")
  479. } else {
  480. firstClassify, tmpErr := data_manage.GetFirstChartClassifyByParentId(parentClassifyId)
  481. if tmpErr != nil && !utils.IsErrNoRow(tmpErr) {
  482. errMsg = "移动失败"
  483. err = errors.New("获取获取当前父级分类下的排序第一条的分类信息失败,Err:" + tmpErr.Error())
  484. return
  485. }
  486. if firstClassify != nil && firstClassify.ChartClassifyId > 0 && firstClassify.Sort == 0 {
  487. updateSortStr := ` sort + 1 `
  488. _ = data_manage.UpdateChartClassifySortByParentIdAndSource(parentClassifyId, firstClassify.ChartClassifyId-1, 0, updateSortStr, source)
  489. _ = data_manage.UpdateChartInfoSortByClassifyIdV2(parentClassifyId, 0, 0, updateSortStr)
  490. } else {
  491. firstEdb, tErr := data_manage.GetFirstChartInfoByClassifyId(parentClassifyId)
  492. if tErr != nil && !utils.IsErrNoRow(tErr) {
  493. errMsg = "移动失败"
  494. err = errors.New("获取获取当前父级分类下的排序第一条的分类信息失败,Err:" + tErr.Error())
  495. return
  496. }
  497. if firstEdb != nil && firstEdb.ChartClassifyId > 0 && firstEdb.Sort == 0 {
  498. updateSortStr := ` sort + 1 `
  499. _ = data_manage.UpdateChartInfoSortByClassifyIdV2(parentClassifyId, 0, firstEdb.ChartInfoId-1, updateSortStr)
  500. _ = data_manage.UpdateChartClassifySortByParentIdAndSource(parentClassifyId, 0, 0, updateSortStr, source)
  501. }
  502. }
  503. chartClassifyInfo.Sort = 0 //那就是排在第一位
  504. chartClassifyInfo.ModifyTime = time.Now()
  505. updateCol = append(updateCol, "Sort", "ModifyTime")
  506. }
  507. if len(updateCol) > 0 {
  508. err = chartClassifyInfo.Update(updateCol)
  509. if err != nil {
  510. errMsg = "移动失败"
  511. err = errors.New("修改失败,Err:" + err.Error())
  512. return
  513. }
  514. if oldParentId != parentClassifyId {
  515. if len(classifyIds) > 0 {
  516. levelStep := chartClassifyInfo.Level - oldLevel
  517. err = data_manage.UpdateEdbClassifyChildByParentClassifyId(classifyIds, chartClassifyInfo.RootId, levelStep)
  518. if err != nil {
  519. errMsg = "移动失败"
  520. err = errors.New("更新子分类失败,Err:" + err.Error())
  521. return
  522. }
  523. }
  524. }
  525. }
  526. } else {
  527. if chartInfo == nil {
  528. errMsg = "当前图表不存在"
  529. err = errors.New(errMsg)
  530. return
  531. }
  532. if chartInfo.ChartClassifyId != parentClassifyId {
  533. chartInfo.ChartClassifyId = parentClassifyId
  534. chartInfo.ModifyTime = time.Now()
  535. updateCol = append(updateCol, "ChartClassifyId", "ModifyTime")
  536. }
  537. if prevSort > 0 {
  538. if nextSort > 0 {
  539. if prevSort == nextSort || prevSort == chartInfo.Sort {
  540. updateSortStr := `sort + 2`
  541. if prevClassify != nil {
  542. _ = data_manage.UpdateChartClassifySortByParentIdAndSource(parentClassifyId, prevClassify.ChartClassifyId, prevClassify.Sort, updateSortStr, source)
  543. } else {
  544. _ = data_manage.UpdateChartClassifySortByParentIdAndSource(parentClassifyId, 0, prevSort, updateSortStr, source)
  545. }
  546. if prevChartInfo != nil {
  547. _ = data_manage.UpdateChartInfoSortByClassifyIdV2(parentClassifyId, prevSort, prevChartInfo.ChartInfoId, updateSortStr)
  548. } else {
  549. _ = data_manage.UpdateChartInfoSortByClassifyIdV2(parentClassifyId, prevSort, 0, updateSortStr)
  550. }
  551. } else {
  552. if nextSort-prevSort == 1 {
  553. updateSortStr := `sort + 1`
  554. if prevClassify != nil {
  555. _ = data_manage.UpdateChartClassifySortByParentIdAndSource(parentClassifyId, prevClassify.ChartClassifyId, prevSort, updateSortStr, source)
  556. } else {
  557. _ = data_manage.UpdateChartClassifySortByParentIdAndSource(parentClassifyId, 0, prevSort, updateSortStr, source)
  558. }
  559. if prevChartInfo != nil {
  560. _ = data_manage.UpdateChartInfoSortByClassifyIdV2(parentClassifyId, prevSort, prevChartInfo.ChartInfoId, updateSortStr)
  561. } else {
  562. _ = data_manage.UpdateChartInfoSortByClassifyIdV2(parentClassifyId, prevSort, 0, updateSortStr)
  563. }
  564. }
  565. }
  566. }
  567. chartInfo.Sort = prevSort + 1
  568. chartInfo.ModifyTime = time.Now()
  569. updateCol = append(updateCol, "Sort", "ModifyTime")
  570. } else if prevClassify == nil && nextClassify == nil && prevChartInfo == nil && nextChartInfo == nil && parentClassifyId > 0 {
  571. var maxSort int
  572. maxSort, err = GetChartClassifyMaxSort(parentClassifyId, source)
  573. if err != nil {
  574. errMsg = "移动失败"
  575. err = errors.New("查询组内排序信息失败,Err:" + err.Error())
  576. return
  577. }
  578. chartInfo.Sort = maxSort + 1 //那就是排在组内最后一位
  579. chartInfo.ModifyTime = time.Now()
  580. updateCol = append(updateCol, "Sort", "ModifyTime")
  581. } else {
  582. firstClassify, tmpErr := data_manage.GetFirstChartClassifyByParentId(parentClassifyId)
  583. if tmpErr != nil && !utils.IsErrNoRow(tmpErr) {
  584. errMsg = "移动失败"
  585. err = errors.New("获取获取当前父级分类下的排序第一条的分类信息失败,Err:" + tmpErr.Error())
  586. return
  587. }
  588. if firstClassify != nil && firstClassify.ChartClassifyId > 0 && firstClassify.Sort == 0 {
  589. updateSortStr := ` sort + 1 `
  590. _ = data_manage.UpdateChartClassifySortByParentIdAndSource(parentClassifyId, firstClassify.ChartClassifyId-1, 0, updateSortStr, source)
  591. _ = data_manage.UpdateChartInfoSortByClassifyIdV2(parentClassifyId, 0, 0, updateSortStr)
  592. } else {
  593. firstEdb, tErr := data_manage.GetFirstChartInfoByClassifyId(parentClassifyId)
  594. if tErr != nil && !utils.IsErrNoRow(tErr) {
  595. errMsg = "移动失败"
  596. err = errors.New("获取获取当前父级分类下的排序第一条的分类信息失败,Err:" + tErr.Error())
  597. return
  598. }
  599. if firstEdb != nil && firstEdb.ChartClassifyId > 0 && firstEdb.Sort == 0 {
  600. updateSortStr := ` sort + 1 `
  601. _ = data_manage.UpdateChartInfoSortByClassifyIdV2(parentClassifyId, 0, firstEdb.ChartInfoId-1, updateSortStr)
  602. _ = data_manage.UpdateChartClassifySortByParentIdAndSource(parentClassifyId, 0, 0, updateSortStr, source)
  603. }
  604. }
  605. chartInfo.Sort = 0 //那就是排在第一位
  606. chartInfo.ModifyTime = time.Now()
  607. updateCol = append(updateCol, "Sort", "ModifyTime")
  608. }
  609. if len(updateCol) > 0 {
  610. err = chartInfo.Update(updateCol)
  611. if err != nil {
  612. errMsg = "移动失败"
  613. err = errors.New("修改失败,Err:" + err.Error())
  614. return
  615. }
  616. }
  617. }
  618. return
  619. }
  620. func GetChildChartClassifyByClassifyId(targetClassifyId int) (targetList []*data_manage.ChartClassifyIdItems, err error, errMsg string) {
  621. targetClassify, err := data_manage.GetChartClassifyById(targetClassifyId)
  622. if err != nil {
  623. if utils.IsErrNoRow(err) {
  624. errMsg = "当前分类不存在"
  625. err = errors.New(errMsg)
  626. return
  627. }
  628. errMsg = "获取失败"
  629. err = errors.New("获取分类信息失败,Err:" + err.Error())
  630. return
  631. }
  632. orderStr := ` order by level asc, sort asc, chart_classify_id asc`
  633. tmpList, err := data_manage.GetChartClassifyByRootIdLevel(targetClassify.RootId, targetClassify.Source, orderStr)
  634. if err != nil && !utils.IsErrNoRow(err) {
  635. errMsg = "获取失败"
  636. err = errors.New("获取数据失败,Err:" + err.Error())
  637. return
  638. }
  639. idMap := make(map[int]struct{})
  640. if len(tmpList) > 0 {
  641. for _, v := range tmpList {
  642. if v.ChartClassifyId == targetClassify.ChartClassifyId {
  643. idMap[v.ChartClassifyId] = struct{}{}
  644. }
  645. }
  646. for _, v := range tmpList {
  647. if _, ok := idMap[v.ParentId]; ok {
  648. idMap[v.ChartClassifyId] = struct{}{}
  649. }
  650. }
  651. for _, v := range tmpList {
  652. if _, ok := idMap[v.ChartClassifyId]; ok {
  653. targetItem := new(data_manage.ChartClassifyIdItems)
  654. targetItem.ChartClassifyId = v.ChartClassifyId
  655. targetItem.ParentId = v.ParentId
  656. targetItem.RootId = v.RootId
  657. targetItem.UniqueCode = v.UniqueCode
  658. targetItem.Level = v.Level
  659. targetItem.ChartClassifyName = v.ChartClassifyName
  660. targetItem.IsJoinPermission = v.IsJoinPermission
  661. targetList = append(targetList, targetItem)
  662. }
  663. }
  664. }
  665. return
  666. }
  667. func GetChartClassifyMaxSort(parentId, source int) (maxSort int, err error) {
  668. classifyMaxSort, err := data_manage.GetChartClassifyMaxSort(parentId, source)
  669. if err != nil {
  670. return
  671. }
  672. maxSort = classifyMaxSort
  673. edbMaxSort, err := data_manage.GetChartInfoMaxSortByClassifyId(parentId)
  674. if err != nil {
  675. return
  676. }
  677. if maxSort < edbMaxSort {
  678. maxSort = edbMaxSort
  679. }
  680. return
  681. }
  682. func GetChartClassifyTreeRecursive(list []*data_manage.ChartClassifyItems, parentId int) []*data_manage.ChartClassifyItems {
  683. res := make([]*data_manage.ChartClassifyItems, 0)
  684. for _, v := range list {
  685. if v.ParentId == parentId {
  686. v.Children = GetChartClassifyTreeRecursive(list, v.ChartClassifyId)
  687. res = append(res, v)
  688. }
  689. }
  690. return res
  691. }
  692. func GetChartClassifyParentRecursive(list []*data_manage.ChartClassifyItems, classifyId int) []*data_manage.ChartClassifyItems {
  693. res := make([]*data_manage.ChartClassifyItems, 0)
  694. for _, v := range list {
  695. if v.ChartClassifyId == classifyId {
  696. if v.ParentId > 0 {
  697. res = GetChartClassifyParentRecursive(list, v.ParentId)
  698. }
  699. res = append(res, v)
  700. }
  701. }
  702. return res
  703. }
  704. func GetChartClassifyChildIds(classifyId int, classifymap map[int]*data_manage.ChartClassifyItems, childIds *[]int) {
  705. for _, item := range classifymap {
  706. if item.ParentId == classifyId {
  707. *childIds = append(*childIds, item.ChartClassifyId)
  708. GetChartClassifyChildIds(item.ChartClassifyId, classifymap, childIds)
  709. }
  710. }
  711. }