public_edb_classify.go 31 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902
  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. // AddEdbPublicClassify
  12. // @Description: 添加公共指标分类
  13. // @author: Roc
  14. // @datetime 2024-12-04 18:00:22
  15. // @param classifyName string
  16. // @param parentId int
  17. // @param level int
  18. // @param classifyType uint8
  19. // @param sysUserId int
  20. // @param sysUserName string
  21. // @return classifyInfo *data_manage.EdbPublicClassify
  22. // @return err error
  23. // @return errMsg string
  24. func AddEdbPublicClassify(classifyName string, parentId int, classifyType uint8, sysUserId int, sysUserName string) (classifyInfo *data_manage.EdbPublicClassify, err error, errMsg string) {
  25. edbPublicClassifyObj := data_manage.EdbPublicClassify{}
  26. // 校验分类名称相同的数量
  27. {
  28. var count int
  29. count, err = edbPublicClassifyObj.GetEdbClassifyCount(classifyName, parentId, classifyType)
  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. level := 0
  42. //获取该层级下最大的排序数
  43. maxSort, err := GetEdbPublicClassifyMaxSort(parentId, classifyType)
  44. if err != nil {
  45. errMsg = "获取失败"
  46. err = errors.New("查询排序信息失败,Err:" + err.Error())
  47. return
  48. }
  49. //查询顶级rootId
  50. rootId := 0
  51. var classifyNamePath, classifyIdPath string
  52. if parentId > 0 {
  53. parentClassify, tErr := edbPublicClassifyObj.GetEdbClassifyById(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.EdbPublicClassifyNamePath, "|", classifyName)
  67. classifyIdPath = fmt.Sprint(parentClassify.EdbPublicClassifyIdPath, ",")
  68. } else {
  69. classifyNamePath = classifyName
  70. }
  71. // 层级校验
  72. if level > utils.EdbClassifyMaxLevel {
  73. errMsg = fmt.Sprintf("最高只支持添加%d级分类", level)
  74. return
  75. }
  76. timestamp := strconv.FormatInt(time.Now().UnixNano(), 10)
  77. classifyInfo = &data_manage.EdbPublicClassify{
  78. //EdbPublicClassifyId: 0,
  79. ClassifyType: classifyType,
  80. EdbPublicClassifyName: 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. EdbPublicClassifyNamePath: classifyNamePath,
  94. EdbPublicClassifyIdPath: "",
  95. }
  96. err = classifyInfo.Add()
  97. if err != nil {
  98. errMsg = "保存分类失败"
  99. return
  100. }
  101. // 更改分类id完整路径path
  102. updateCols := []string{"EdbPublicClassifyIdPath"}
  103. classifyInfo.EdbPublicClassifyIdPath = fmt.Sprint(classifyIdPath, classifyInfo.EdbPublicClassifyId)
  104. if parentId == 0 { //一级目录的rootId等于自己本身
  105. classifyInfo.RootId = classifyInfo.EdbPublicClassifyId
  106. updateCols = append(updateCols, "RootId")
  107. }
  108. err = classifyInfo.Update(updateCols)
  109. if err != nil {
  110. errMsg = "更新分类失败"
  111. return
  112. }
  113. return
  114. }
  115. // EditEdbPublicClassify
  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 EditEdbPublicClassify(classifyId int, classifyName string, sysUser *system.Admin) (err error, errMsg string) {
  125. edbPublicClassifyObj := data_manage.EdbPublicClassify{}
  126. item, err := edbPublicClassifyObj.GetEdbClassifyById(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.EdbPublicClassifyName == classifyName {
  140. return
  141. }
  142. // 判断名称是否已存在
  143. count, tmpErr := edbPublicClassifyObj.GetEdbClassifyCount(classifyName, item.ParentId, item.ClassifyType)
  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.EdbPublicClassifyNamePath
  156. if item.ParentId > 0 {
  157. parentItem, tmpErr := edbPublicClassifyObj.GetEdbClassifyById(item.ParentId)
  158. if tmpErr != nil {
  159. err = tmpErr
  160. return
  161. }
  162. newClassifyNamePath = fmt.Sprint(parentItem.EdbPublicClassifyNamePath, "|", classifyName)
  163. } else {
  164. newClassifyNamePath = classifyName
  165. }
  166. // 更新自己的信息
  167. item.EdbPublicClassifyName = classifyName
  168. item.EdbPublicClassifyNamePath = newClassifyNamePath
  169. item.LastModifyUserId = sysUser.AdminId
  170. item.LastModifyUserRealName = sysUser.RealName
  171. updateCols = append(updateCols, "EdbPublicClassifyName", "EdbPublicClassifyNamePath", "LastModifyUserId", "LastModifyUserRealName")
  172. // 修改数据
  173. if len(updateCols) > 0 {
  174. err = item.UpdateEdbClassifyNameAndNamePath(updateCols, oldClassifyNamePath, newClassifyNamePath)
  175. if err != nil {
  176. errMsg = "保存失败"
  177. }
  178. }
  179. return
  180. }
  181. // GetEdbPublicClassifyMaxSort
  182. // @Description: 获取最大排序值
  183. // @author: Roc
  184. // @datetime 2024-12-04 16:54:57
  185. // @param parentId int
  186. // @param classifyType uint8
  187. // @return maxSort int
  188. // @return err error
  189. func GetEdbPublicClassifyMaxSort(parentId int, classifyType uint8) (maxSort int, err error) {
  190. edbPublicClassifyObj := data_manage.EdbPublicClassify{}
  191. //获取该层级下最大的排序数
  192. classifyMaxSort, err := edbPublicClassifyObj.GetEdbClassifyMaxSort(parentId, classifyType)
  193. if err != nil {
  194. return
  195. }
  196. maxSort = classifyMaxSort
  197. edbMaxSort, err := data_manage.GetEdbInfoMaxSortByClassifyId(parentId)
  198. if err != nil {
  199. return
  200. }
  201. if maxSort < edbMaxSort {
  202. maxSort = edbMaxSort
  203. }
  204. return
  205. }
  206. // DeleteEdbPublicClassifyCheck 删除检测
  207. func DeleteEdbPublicClassifyCheck(classifyId int, sysUser *system.Admin) (deleteStatus int, tipsMsg string, allClassifyIdList []int, tableList []*data_manage.ExcelBaseInfo, err error, errMsg string) {
  208. // TODO 操作权限校验
  209. {
  210. }
  211. edbPublicClassifyObj := data_manage.EdbPublicClassify{}
  212. // 查找当前分类
  213. item, tmpErr := edbPublicClassifyObj.GetEdbClassifyById(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.EdbPublicClassifyId)
  228. // 判断分类下,是否含有指标
  229. {
  230. //判断分类下,是否含有指标
  231. count, tmpErr := edbPublicClassifyObj.GetEdbInfoCountByClassifyIdList(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. // DeleteEdbPublicClassify 删除分类/指标
  252. func DeleteEdbPublicClassify(classifyId int, sysUser *system.Admin, requestBody, requestUrl string) (nextItem *data_manage.EdbInfo, tableList []*data_manage.ExcelBaseInfo, err error, errMsg string) {
  253. //删除分类校验
  254. deleteStatus, tipsMsg, allClassifyIdList, _, err, errMsg := DeleteEdbPublicClassifyCheck(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. edbPublicClassifyObj := data_manage.EdbPublicClassify{}
  266. err = edbPublicClassifyObj.BatchesDel(allClassifyIdList)
  267. if err != nil {
  268. errMsg = "删除失败"
  269. return
  270. }
  271. return
  272. }
  273. // MoveEdbClassify 移动指标分类
  274. func MoveEdbPublicClassify(req data_manage.MoveEdbClassifyReq, sysUser *system.Admin, classifyType uint8) (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. edbInfoId := req.EdbInfoId
  281. prevEdbInfoId := req.PrevEdbInfoId
  282. nextEdbInfoId := req.NextEdbInfoId
  283. obj := data_manage.EdbPublicClassify{}
  284. //首先确定移动的对象是分类还是指标
  285. //判断上一个节点是分类还是指标
  286. //判断下一个节点是分类还是指标
  287. //同时更新分类目录下的分类sort和指标sort
  288. //更新当前移动的分类或者指标sort
  289. var parentEdbClassifyInfo *data_manage.EdbPublicClassify
  290. if parentClassifyId > 0 {
  291. parentEdbClassifyInfo, err = obj.GetEdbClassifyById(parentClassifyId)
  292. if err != nil {
  293. errMsg = "移动失败"
  294. err = errors.New("获取上级分类信息失败,Err:" + err.Error())
  295. return
  296. }
  297. }
  298. //如果有传入 上一个兄弟节点分类id
  299. var (
  300. edbClassifyInfo *data_manage.EdbPublicClassify
  301. prevClassify *data_manage.EdbPublicClassify
  302. nextClassify *data_manage.EdbPublicClassify
  303. edbInfo *data_manage.EdbInfo
  304. prevEdbInfo *data_manage.EdbInfo
  305. nextEdbInfo *data_manage.EdbInfo
  306. prevSort int
  307. nextSort int
  308. )
  309. // 移动对象为分类, 判断权限
  310. if edbInfoId == 0 {
  311. edbClassifyInfo, err = obj.GetEdbClassifyById(classifyId)
  312. if err != nil {
  313. if utils.IsErrNoRow(err) {
  314. errMsg = "当前分类不存在"
  315. err = errors.New("获取分类信息失败,Err:" + err.Error())
  316. return
  317. }
  318. errMsg = "移动失败"
  319. err = errors.New("获取分类信息失败,Err:" + err.Error())
  320. return
  321. }
  322. if parentClassifyId > 0 && parentEdbClassifyInfo.Level == 6 {
  323. errMsg = "最高只支持添加6级分类"
  324. err = errors.New(errMsg)
  325. return
  326. }
  327. // 如果是移动目录, 那么校验一下父级目录下是否有重名目录
  328. exists, e := obj.GetEdbPublicClassifyByParentIdAndName(parentClassifyId, edbClassifyInfo.EdbPublicClassifyName, classifyId, classifyType)
  329. if e != nil && !utils.IsErrNoRow(e) {
  330. errMsg = "移动失败"
  331. err = fmt.Errorf("获取父级分类下的同名分类失败, Err: %s", e.Error())
  332. return
  333. }
  334. if exists != nil && exists.EdbPublicClassifyId > 0 {
  335. errMsg = "移动失败,分类名称已存在"
  336. return
  337. }
  338. } else {
  339. edbInfo, err = data_manage.GetEdbInfoById(req.EdbInfoId)
  340. if err != nil {
  341. if utils.IsErrNoRow(err) {
  342. errMsg = "当前指标不存在"
  343. err = errors.New("获取分类信息失败,Err:" + err.Error())
  344. return
  345. }
  346. errMsg = "移动失败"
  347. err = errors.New("获取分类信息失败,Err:" + err.Error())
  348. return
  349. }
  350. if parentClassifyId == 0 {
  351. errMsg = "移动失败,指标必须挂在分类下"
  352. err = errors.New(errMsg)
  353. return
  354. }
  355. // TODO 移动权限校验
  356. }
  357. if prevClassifyId > 0 {
  358. prevClassify, err = obj.GetEdbClassifyById(prevClassifyId)
  359. if err != nil {
  360. errMsg = "移动失败"
  361. err = errors.New("获取上一个兄弟节点分类信息失败,Err:" + err.Error())
  362. return
  363. }
  364. prevSort = prevClassify.Sort
  365. } else if prevEdbInfoId > 0 {
  366. prevEdbInfo, err = data_manage.GetEdbInfoById(prevEdbInfoId)
  367. if err != nil {
  368. errMsg = "移动失败"
  369. err = errors.New("获取上一个兄弟节点分类信息失败,Err:" + err.Error())
  370. return
  371. }
  372. prevSort = prevEdbInfo.PublicSort
  373. }
  374. if nextClassifyId > 0 {
  375. //下一个兄弟节点
  376. nextClassify, err = obj.GetEdbClassifyById(nextClassifyId)
  377. if err != nil {
  378. errMsg = "移动失败"
  379. err = errors.New("获取下一个兄弟节点分类信息失败,Err:" + err.Error())
  380. return
  381. }
  382. nextSort = nextClassify.Sort
  383. } else if nextEdbInfoId > 0 {
  384. //下一个兄弟节点
  385. nextEdbInfo, err = data_manage.GetEdbInfoById(nextEdbInfoId)
  386. if err != nil {
  387. errMsg = "移动失败"
  388. err = errors.New("获取下一个兄弟节点分类信息失败,Err:" + err.Error())
  389. return
  390. }
  391. nextSort = nextEdbInfo.PublicSort
  392. }
  393. err, errMsg = movePublicEdbOrClassify(parentEdbClassifyInfo, edbClassifyInfo, prevClassify, nextClassify, edbInfo, prevEdbInfo, nextEdbInfo, parentClassifyId, prevSort, nextSort, classifyType)
  394. return
  395. }
  396. // movePublicEdbOrClassify
  397. // @Description: 移动公共指标/分类
  398. // @param parentEdbClassifyInfo
  399. // @param edbClassifyInfo
  400. // @param prevClassify
  401. // @param nextClassify
  402. // @param edbInfo
  403. // @param prevEdbInfo
  404. // @param nextEdbInfo
  405. // @param parentClassifyId
  406. // @param prevSort
  407. // @param nextSort
  408. // @param classifyType
  409. // @return err
  410. // @return errMsg
  411. func movePublicEdbOrClassify(parentEdbClassifyInfo, edbClassifyInfo, prevClassify, nextClassify *data_manage.EdbPublicClassify, edbInfo, prevEdbInfo, nextEdbInfo *data_manage.EdbInfo, parentClassifyId int, prevSort, nextSort int, classifyType uint8) (err error, errMsg string) {
  412. if edbClassifyInfo != nil {
  413. // 移动分类
  414. return movePublicEdbClassify(parentEdbClassifyInfo, edbClassifyInfo, prevClassify, nextClassify, prevEdbInfo, nextEdbInfo, parentClassifyId, prevSort, nextSort, classifyType)
  415. } else {
  416. // 移动指标
  417. return moveEdbPublic(prevClassify, nextClassify, edbInfo, prevEdbInfo, nextEdbInfo, parentClassifyId, prevSort, nextSort, classifyType)
  418. }
  419. return
  420. }
  421. // moveEdbPublic
  422. // @Description: 移动指标
  423. // @author: Roc
  424. // @datetime 2024-11-26 16:07:37
  425. // @param prevClassify *data_manage.EdbClassify
  426. // @param nextClassify *data_manage.EdbClassify
  427. // @param edbInfo *data_manage.EdbInfo
  428. // @param prevEdbInfo *data_manage.EdbInfo
  429. // @param nextEdbInfo *data_manage.EdbInfo
  430. // @param parentClassifyId int
  431. // @param prevSort int
  432. // @param nextSort int
  433. // @param classifyType uint8
  434. // @return err error
  435. // @return errMsg string
  436. func moveEdbPublic(prevClassify, nextClassify *data_manage.EdbPublicClassify, edbInfo, prevEdbInfo, nextEdbInfo *data_manage.EdbInfo, parentClassifyId int, prevSort, nextSort int, classifyType uint8) (err error, errMsg string) {
  437. updateCol := make([]string, 0)
  438. obj := data_manage.EdbPublicClassify{}
  439. fmt.Println("prevSort:", prevSort)
  440. fmt.Println("prevEdbInfo:", prevEdbInfo)
  441. fmt.Println("parentClassifyId:", parentClassifyId)
  442. if edbInfo == nil {
  443. errMsg = "当前指标不存在"
  444. err = errors.New(errMsg)
  445. return
  446. }
  447. //如果改变了分类,那么移动该指标数据
  448. if edbInfo.ClassifyId != parentClassifyId {
  449. edbInfo.EdbPublicClassifyId = parentClassifyId
  450. edbInfo.ModifyTime = time.Now()
  451. updateCol = append(updateCol, "EdbPublicClassifyId", "ModifyTime")
  452. }
  453. if prevEdbInfo != nil && prevEdbInfo.EdbInfoId > 0 {
  454. //如果是移动在两个兄弟节点之间
  455. if nextSort > 0 {
  456. //下一个兄弟节点
  457. //如果上一个兄弟与下一个兄弟的排序权重是一致的,那么需要将下一个兄弟(以及下个兄弟的同样排序权重)的排序权重+2,自己变成上一个兄弟的排序权重+1
  458. if prevSort == nextSort || prevSort == edbInfo.PublicSort {
  459. //变更兄弟节点的排序
  460. updateSortStr := ` sort + 2`
  461. updateEdbSortStr := ` public_sort + 2`
  462. //变更分类
  463. if prevClassify != nil {
  464. _ = obj.UpdateEdbClassifySortByParentId(parentClassifyId, prevClassify.EdbPublicClassifyId, prevClassify.Sort, updateSortStr, classifyType)
  465. } else {
  466. _ = obj.UpdateEdbClassifySortByParentId(parentClassifyId, 0, prevSort, updateSortStr, classifyType)
  467. }
  468. //变更指标
  469. if prevEdbInfo != nil {
  470. //变更兄弟节点的排序
  471. _ = data_manage.UpdateEdbInfoPublicSortByClassifyId(parentClassifyId, prevSort, prevEdbInfo.EdbInfoId, updateEdbSortStr)
  472. } else {
  473. _ = data_manage.UpdateEdbInfoPublicSortByClassifyId(parentClassifyId, prevSort, 0, updateEdbSortStr)
  474. }
  475. } else {
  476. //如果下一个兄弟的排序权重正好是上个兄弟节点的下一层,那么需要再加一层了
  477. if nextSort-prevSort == 1 {
  478. //变更兄弟节点的排序
  479. updateSortStr := ` sort + 1`
  480. updateEdbSortStr := ` public_sort + 1`
  481. //变更分类
  482. if prevClassify != nil {
  483. _ = obj.UpdateEdbClassifySortByParentId(parentClassifyId, prevClassify.EdbPublicClassifyId, prevSort, updateSortStr, classifyType)
  484. } else {
  485. _ = obj.UpdateEdbClassifySortByParentId(parentClassifyId, 0, prevSort, updateSortStr, classifyType)
  486. }
  487. //变更指标
  488. if prevEdbInfo != nil {
  489. //变更兄弟节点的排序
  490. _ = data_manage.UpdateEdbInfoPublicSortByClassifyId(parentClassifyId, prevSort, prevEdbInfo.EdbInfoId, updateSortStr)
  491. } else {
  492. _ = data_manage.UpdateEdbInfoPublicSortByClassifyId(parentClassifyId, prevSort, 0, updateEdbSortStr)
  493. }
  494. }
  495. }
  496. }
  497. edbInfo.PublicSort = prevEdbInfo.PublicSort + 1
  498. edbInfo.ModifyTime = time.Now()
  499. updateCol = append(updateCol, "PublicSort", "ModifyTime")
  500. } else if prevClassify == nil && nextClassify == nil && prevEdbInfo == nil && nextEdbInfo == nil && parentClassifyId > 0 {
  501. //处理只拖动到目录里,默认放到目录底部的情况
  502. var maxSort int
  503. maxSort, err = GetPublicEdbClassifyMaxSort(parentClassifyId, classifyType)
  504. if err != nil {
  505. errMsg = "移动失败"
  506. err = errors.New("查询组内排序信息失败,Err:" + err.Error())
  507. return
  508. }
  509. fmt.Println("maxSort:", maxSort)
  510. edbInfo.PublicSort = maxSort + 1 //那就是排在组内最后一位
  511. edbInfo.ModifyTime = time.Now()
  512. updateCol = append(updateCol, "PublicSort", "ModifyTime")
  513. } else {
  514. fmt.Println("进来:")
  515. // 拖动到父级分类的第一位
  516. firstClassify, tmpErr := obj.GetFirstEdbClassifyByParentId(parentClassifyId)
  517. if tmpErr != nil && !utils.IsErrNoRow(tmpErr) {
  518. errMsg = "移动失败"
  519. err = errors.New("获取获取当前父级分类下的排序第一条的分类信息失败,Err:" + tmpErr.Error())
  520. return
  521. }
  522. //如果该分类下存在其他分类,且第一个其他分类的排序等于0,那么需要调整排序
  523. if firstClassify != nil && firstClassify.EdbPublicClassifyId > 0 && firstClassify.Sort == 0 {
  524. updateSortStr := ` sort + 1 `
  525. _ = obj.UpdateEdbClassifySortByParentId(parentClassifyId, firstClassify.EdbPublicClassifyId-1, 0, updateSortStr, classifyType)
  526. //该分类下的所有指标也需要+1
  527. updateEdbSortStr := ` public_sort + 1 `
  528. _ = data_manage.UpdateEdbInfoPublicSortByClassifyId(parentClassifyId, 0, 0, updateEdbSortStr)
  529. } else {
  530. //如果该分类下存在指标,且第一个指标的排序等于0,那么需要调整排序
  531. firstEdb, tErr := data_manage.GetFirstEdbInfoByPublicClassifyId(parentClassifyId)
  532. if tErr != nil && !utils.IsErrNoRow(tErr) {
  533. errMsg = "移动失败"
  534. err = errors.New("获取获取当前父级分类下的排序第一条的分类信息失败,Err:" + tErr.Error())
  535. return
  536. }
  537. //如果该分类下存在其他分类,且第一个其他分类的排序等于0,那么需要调整排序
  538. if firstEdb != nil && firstEdb.ClassifyId > 0 && firstEdb.Sort == 0 {
  539. updateSortStr := ` sort + 1 `
  540. _ = obj.UpdateEdbClassifySortByParentId(parentClassifyId, 0, 0, updateSortStr, classifyType)
  541. // 指标
  542. updateEdbSortStr := ` public_sort + 1 `
  543. _ = data_manage.UpdateEdbInfoPublicSortByClassifyId(parentClassifyId, 0, firstEdb.EdbInfoId-1, updateEdbSortStr)
  544. }
  545. }
  546. edbInfo.PublicSort = 0 //那就是排在第一位
  547. edbInfo.ModifyTime = time.Now()
  548. updateCol = append(updateCol, "PublicSort", "ModifyTime")
  549. }
  550. //更新
  551. if len(updateCol) > 0 {
  552. err = edbInfo.Update(updateCol)
  553. if err != nil {
  554. errMsg = "移动失败"
  555. err = errors.New("修改失败,Err:" + err.Error())
  556. return
  557. }
  558. }
  559. return
  560. }
  561. // movePublicEdbClassify
  562. // @Description: 移动指标分类
  563. // @author: Roc
  564. // @datetime 2024-11-26 16:07:44
  565. // @param parentEdbClassifyInfo *data_manage.EdbClassify
  566. // @param edbClassifyInfo *data_manage.EdbClassify
  567. // @param prevClassify *data_manage.EdbClassify
  568. // @param nextClassify *data_manage.EdbClassify
  569. // @param edbInfo *data_manage.EdbInfo
  570. // @param prevEdbInfo *data_manage.EdbInfo
  571. // @param nextEdbInfo *data_manage.EdbInfo
  572. // @param parentClassifyId int
  573. // @param prevSort int
  574. // @param nextSort int
  575. // @param classifyType uint8
  576. // @return err error
  577. // @return errMsg string
  578. func movePublicEdbClassify(parentEdbClassifyInfo, edbClassifyInfo, prevClassify, nextClassify *data_manage.EdbPublicClassify, prevEdbInfo, nextEdbInfo *data_manage.EdbInfo, parentClassifyId int, prevSort, nextSort int, classifyType uint8) (err error, errMsg string) {
  579. updateCol := make([]string, 0)
  580. obj := data_manage.EdbPublicClassify{}
  581. // 移动对象为分类, 判断分类是否存在
  582. oldParentId := edbClassifyInfo.ParentId
  583. oldLevel := edbClassifyInfo.Level
  584. var classifyIds []int
  585. if oldParentId != parentClassifyId {
  586. //更新子分类对应的level
  587. childList, e, m := GetChildClassifyByPublicClassifyId(edbClassifyInfo.EdbPublicClassifyId)
  588. if e != nil {
  589. errMsg = "移动失败"
  590. err = errors.New("查询子分类失败,Err:" + e.Error() + m)
  591. return
  592. }
  593. if len(childList) > 0 {
  594. for _, v := range childList {
  595. if v.ClassifyId == edbClassifyInfo.EdbPublicClassifyId {
  596. continue
  597. }
  598. classifyIds = append(classifyIds, v.ClassifyId)
  599. }
  600. }
  601. }
  602. //判断上级id是否一致,如果不一致的话,那么需要移动该分类层级
  603. if edbClassifyInfo.ParentId != parentClassifyId && parentClassifyId != 0 {
  604. if edbClassifyInfo.Level != parentEdbClassifyInfo.Level+1 { //禁止层级调整
  605. errMsg = "移动失败"
  606. err = errors.New("不支持目录层级变更")
  607. return
  608. }
  609. edbClassifyInfo.ParentId = parentEdbClassifyInfo.EdbPublicClassifyId
  610. edbClassifyInfo.RootId = parentEdbClassifyInfo.RootId
  611. edbClassifyInfo.Level = parentEdbClassifyInfo.Level + 1
  612. edbClassifyInfo.ModifyTime = time.Now()
  613. updateCol = append(updateCol, "ParentId", "RootId", "Level", "ModifyTime")
  614. } else if edbClassifyInfo.ParentId != parentClassifyId && parentClassifyId == 0 {
  615. errMsg = "移动失败"
  616. err = errors.New("不支持目录层级变更")
  617. return
  618. }
  619. if prevSort > 0 {
  620. //如果是移动在两个兄弟节点之间
  621. if nextSort > 0 {
  622. //下一个兄弟节点
  623. //如果上一个兄弟与下一个兄弟的排序权重是一致的,那么需要将下一个兄弟(以及下个兄弟的同样排序权重)的排序权重+2,自己变成上一个兄弟的排序权重+1
  624. if prevSort == nextSort || prevSort == edbClassifyInfo.Sort {
  625. //变更兄弟节点的排序
  626. updateSortStr := `sort + 2`
  627. updateEdbSortStr := ` public_sort + 2`
  628. //变更分类
  629. if prevClassify != nil {
  630. _ = obj.UpdateEdbClassifySortByParentId(parentClassifyId, prevClassify.EdbPublicClassifyId, prevClassify.Sort, updateSortStr, classifyType)
  631. } else {
  632. _ = obj.UpdateEdbClassifySortByParentId(parentClassifyId, 0, prevSort, updateSortStr, classifyType)
  633. }
  634. //变更指标
  635. if prevEdbInfo != nil {
  636. //变更兄弟节点的排序
  637. _ = data_manage.UpdateEdbInfoPublicSortByClassifyId(parentClassifyId, prevSort, prevEdbInfo.EdbInfoId, updateEdbSortStr)
  638. } else {
  639. _ = data_manage.UpdateEdbInfoPublicSortByClassifyId(parentClassifyId, prevSort, 0, updateEdbSortStr)
  640. }
  641. } else {
  642. //如果下一个兄弟的排序权重正好是上个兄弟节点的下一层,那么需要再加一层了
  643. if nextSort-prevSort == 1 {
  644. //变更兄弟节点的排序
  645. updateSortStr := `sort + 1`
  646. updateEdbSortStr := `public_sort + 1`
  647. //变更分类
  648. if prevClassify != nil {
  649. _ = obj.UpdateEdbClassifySortByParentId(parentClassifyId, prevClassify.EdbPublicClassifyId, prevSort, updateSortStr, classifyType)
  650. } else {
  651. _ = obj.UpdateEdbClassifySortByParentId(parentClassifyId, 0, prevSort, updateSortStr, classifyType)
  652. }
  653. //变更指标
  654. if prevEdbInfo != nil {
  655. //变更兄弟节点的排序
  656. _ = data_manage.UpdateEdbInfoPublicSortByClassifyId(parentClassifyId, prevSort, prevEdbInfo.EdbInfoId, updateEdbSortStr)
  657. } else {
  658. _ = data_manage.UpdateEdbInfoPublicSortByClassifyId(parentClassifyId, prevSort, 0, updateEdbSortStr)
  659. }
  660. }
  661. }
  662. }
  663. edbClassifyInfo.Sort = prevSort + 1
  664. edbClassifyInfo.ModifyTime = time.Now()
  665. updateCol = append(updateCol, "Sort", "ModifyTime")
  666. } else if prevClassify == nil && nextClassify == nil && prevEdbInfo == nil && nextEdbInfo == nil && parentClassifyId > 0 {
  667. //处理只拖动到目录里,默认放到目录底部的情况
  668. var maxSort int
  669. maxSort, err = GetEdbPublicClassifyMaxSort(parentClassifyId, classifyType)
  670. if err != nil {
  671. errMsg = "移动失败"
  672. err = errors.New("查询组内排序信息失败,Err:" + err.Error())
  673. return
  674. }
  675. edbClassifyInfo.Sort = maxSort + 1 //那就是排在组内最后一位
  676. edbClassifyInfo.ModifyTime = time.Now()
  677. updateCol = append(updateCol, "Sort", "ModifyTime")
  678. } else {
  679. // 拖动到父级分类的第一位
  680. firstClassify, tmpErr := obj.GetFirstEdbClassifyByParentId(parentClassifyId)
  681. if tmpErr != nil && !utils.IsErrNoRow(tmpErr) {
  682. errMsg = "移动失败"
  683. err = errors.New("获取获取当前父级分类下的排序第一条的分类信息失败,Err:" + tmpErr.Error())
  684. return
  685. }
  686. //如果该分类下存在其他分类,且第一个其他分类的排序等于0,那么需要调整排序
  687. if firstClassify != nil && firstClassify.EdbPublicClassifyId > 0 && firstClassify.Sort == 0 {
  688. updateSortStr := ` sort + 1 `
  689. updateEdbSortStr := ` public_sort + 1 `
  690. _ = obj.UpdateEdbClassifySortByParentId(parentClassifyId, firstClassify.EdbPublicClassifyId-1, 0, updateSortStr, classifyType)
  691. //该分类下的所有指标也需要+1
  692. _ = data_manage.UpdateEdbInfoPublicSortByClassifyId(parentClassifyId, 0, 0, updateEdbSortStr)
  693. } else {
  694. //如果该分类下存在指标,且第一个指标的排序等于0,那么需要调整排序
  695. firstEdb, tErr := data_manage.GetFirstEdbInfoByPublicClassifyId(parentClassifyId)
  696. if tErr != nil && !utils.IsErrNoRow(tErr) {
  697. errMsg = "移动失败"
  698. err = errors.New("获取获取当前父级分类下的排序第一条的分类信息失败,Err:" + tErr.Error())
  699. return
  700. }
  701. //如果该分类下存在其他分类,且第一个其他分类的排序等于0,那么需要调整排序
  702. if firstEdb != nil && firstEdb.ClassifyId > 0 && firstEdb.Sort == 0 {
  703. updateSortStr := ` sort + 1 `
  704. _ = obj.UpdateEdbClassifySortByParentId(parentClassifyId, 0, 0, updateSortStr, classifyType)
  705. updateEdbSortStr := ` public_sort + 1 `
  706. _ = data_manage.UpdateEdbInfoPublicSortByClassifyId(parentClassifyId, 0, firstEdb.EdbInfoId-1, updateEdbSortStr)
  707. }
  708. }
  709. edbClassifyInfo.Sort = 0 //那就是排在第一位
  710. edbClassifyInfo.ModifyTime = time.Now()
  711. updateCol = append(updateCol, "Sort", "ModifyTime")
  712. }
  713. oldClassifyIdPath := edbClassifyInfo.EdbPublicClassifyIdPath
  714. oldClassifyNamePath := edbClassifyInfo.EdbPublicClassifyNamePath
  715. newClassifyNamePath := edbClassifyInfo.EdbPublicClassifyName
  716. newClassifyIdPath := fmt.Sprint(edbClassifyInfo.EdbPublicClassifyId)
  717. if parentEdbClassifyInfo != nil {
  718. newClassifyNamePath = fmt.Sprint(parentEdbClassifyInfo.EdbPublicClassifyNamePath, `|`, edbClassifyInfo.EdbPublicClassifyName)
  719. newClassifyIdPath = fmt.Sprint(parentEdbClassifyInfo.EdbPublicClassifyIdPath, `,`, edbClassifyInfo.EdbPublicClassifyId)
  720. }
  721. //更新
  722. if len(updateCol) > 0 {
  723. edbClassifyInfo.EdbPublicClassifyNamePath = newClassifyNamePath
  724. edbClassifyInfo.EdbPublicClassifyIdPath = newClassifyIdPath
  725. updateCol = append(updateCol, "EdbPublicClassifyNamePath", "EdbPublicClassifyIdPath")
  726. err = edbClassifyInfo.Update(updateCol)
  727. if err != nil {
  728. errMsg = "移动失败"
  729. err = errors.New("修改失败,Err:" + err.Error())
  730. return
  731. }
  732. //更新对应分类的root_id和层级
  733. if oldParentId != parentClassifyId {
  734. if len(classifyIds) > 0 {
  735. levelStep := edbClassifyInfo.Level - oldLevel
  736. err = obj.UpdateEdbClassifyChildByParentClassifyId(classifyIds, edbClassifyInfo.RootId, levelStep)
  737. if err != nil {
  738. errMsg = "移动失败"
  739. err = errors.New("更新子分类失败,Err:" + err.Error())
  740. return
  741. }
  742. }
  743. // 更改了上级分类,那么需要同步去更改自己的分类全路径
  744. tmpErr := obj.UpdateEdbClassifyNameAndNamePathByOldClassifyIdPath(oldClassifyIdPath, newClassifyIdPath, oldClassifyNamePath, newClassifyNamePath)
  745. if tmpErr != nil {
  746. utils.FileLog.Error(fmt.Sprintf("更新分类全路径失败,分类id:%d;失败原因Err:%s", edbClassifyInfo.EdbPublicClassifyId, tmpErr.Error()))
  747. }
  748. }
  749. }
  750. return
  751. }
  752. func GetPublicEdbClassifyMaxSort(parentId int, classifyType uint8) (maxSort int, err error) {
  753. //获取该层级下最大的排序数
  754. obj := data_manage.EdbPublicClassify{}
  755. classifyMaxSort, err := obj.GetEdbClassifyMaxSort(parentId, classifyType)
  756. if err != nil {
  757. return
  758. }
  759. maxSort = classifyMaxSort
  760. edbMaxSort, err := data_manage.GetEdbInfoMaxPublicSortByClassifyId(parentId)
  761. if err != nil {
  762. return
  763. }
  764. if maxSort < edbMaxSort {
  765. maxSort = edbMaxSort
  766. }
  767. return
  768. }
  769. func GetChildClassifyByPublicClassifyId(targetClassifyId int) (targetList []*data_manage.EdbClassifyIdItems, err error, errMsg string) {
  770. obj := data_manage.EdbPublicClassify{}
  771. //判断是否是挂在顶级目录下
  772. targetClassify, err := obj.GetEdbClassifyById(targetClassifyId)
  773. if err != nil {
  774. if utils.IsErrNoRow(err) {
  775. errMsg = "当前分类不存在"
  776. err = errors.New(errMsg)
  777. return
  778. }
  779. errMsg = "获取失败"
  780. err = errors.New("获取分类信息失败,Err:" + err.Error())
  781. return
  782. }
  783. orderStr := ` order by level asc, sort asc, edb_public_classify_id asc`
  784. tmpList, err := obj.GetEdbClassifyByRootIdLevel(targetClassify.RootId, targetClassify.ClassifyType, orderStr)
  785. if err != nil && !utils.IsErrNoRow(err) {
  786. errMsg = "获取失败"
  787. err = errors.New("获取数据失败,Err:" + err.Error())
  788. return
  789. }
  790. idMap := make(map[int]struct{})
  791. if len(tmpList) > 0 {
  792. for _, v := range tmpList {
  793. if v.EdbPublicClassifyId == targetClassify.EdbPublicClassifyId {
  794. idMap[v.EdbPublicClassifyId] = struct{}{}
  795. }
  796. }
  797. for _, v := range tmpList {
  798. if _, ok := idMap[v.ParentId]; ok {
  799. idMap[v.EdbPublicClassifyId] = struct{}{}
  800. }
  801. }
  802. for _, v := range tmpList {
  803. if _, ok := idMap[v.EdbPublicClassifyId]; ok {
  804. targetItem := new(data_manage.EdbClassifyIdItems)
  805. targetItem.ClassifyId = v.EdbPublicClassifyId
  806. targetItem.ParentId = v.ParentId
  807. targetItem.RootId = v.RootId
  808. targetItem.UniqueCode = v.UniqueCode
  809. targetItem.Level = v.Level
  810. targetItem.ClassifyName = v.EdbPublicClassifyName
  811. targetList = append(targetList, targetItem)
  812. }
  813. }
  814. }
  815. return
  816. }