public_chart_classify.go 31 KB

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