chart_classify.go 32 KB

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