public_edb_classify.go 31 KB

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