chart_classify.go 34 KB

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