public_chart_classify.go 32 KB

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