chart_classify.go 34 KB

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