excel_classify.go 18 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490
  1. package excel
  2. import (
  3. "errors"
  4. "eta/eta_api/models/data_manage/excel"
  5. "eta/eta_api/models/data_manage/excel/request"
  6. "eta/eta_api/utils"
  7. "time"
  8. )
  9. func GetExcelClassifyMaxSort(parentId int, source int) (maxSort int, err error) {
  10. //获取该层级下最大的排序数
  11. maxSort, err = excel.GetExcelClassifyMaxSort(parentId, source)
  12. if err != nil {
  13. return
  14. }
  15. edbMaxSort, err := excel.GetExcelMaxSortByClassifyId(parentId, source)
  16. if err != nil {
  17. return
  18. }
  19. if maxSort < edbMaxSort {
  20. maxSort = edbMaxSort
  21. }
  22. return
  23. }
  24. // MoveExcelClassify 移动指标分类
  25. func MoveExcelClassify(req request.MoveExcelClassifyReq) (err error, errMsg string) {
  26. classifyId := req.ClassifyId
  27. parentClassifyId := req.ParentClassifyId
  28. prevClassifyId := req.PrevClassifyId
  29. nextClassifyId := req.NextClassifyId
  30. excelInfoId := req.ExcelInfoId
  31. prevExcelInfoId := req.PrevExcelInfoId
  32. nextExcelInfoId := req.NextExcelInfoId
  33. source := req.Source
  34. //首先确定移动的对象是分类还是指标
  35. //判断上一个节点是分类还是指标
  36. //判断下一个节点是分类还是指标
  37. //同时更新分类目录下的分类sort和指标sort
  38. //更新当前移动的分类或者指标sort
  39. var parentExcelClassifyInfo *excel.ExcelClassify
  40. if parentClassifyId > 0 {
  41. parentExcelClassifyInfo, err = excel.GetExcelClassifyById(parentClassifyId)
  42. if err != nil {
  43. errMsg = "移动失败"
  44. err = errors.New("获取上级分类信息失败,Err:" + err.Error())
  45. return
  46. }
  47. }
  48. //如果有传入 上一个兄弟节点分类id
  49. var (
  50. excelClassifyInfo *excel.ExcelClassify
  51. prevClassify *excel.ExcelClassify
  52. nextClassify *excel.ExcelClassify
  53. excelInfo *excel.ExcelInfo
  54. prevExcelInfo *excel.ExcelInfo
  55. nextExcelInfo *excel.ExcelInfo
  56. prevSort int
  57. nextSort int
  58. )
  59. // 移动对象为分类, 判断权限
  60. if excelInfoId == 0 {
  61. excelClassifyInfo, err = excel.GetExcelClassifyById(classifyId)
  62. if err != nil {
  63. if err.Error() == utils.ErrNoRow() {
  64. errMsg = "当前分类不存在"
  65. err = errors.New("获取分类信息失败,Err:" + err.Error())
  66. return
  67. }
  68. errMsg = "移动失败"
  69. err = errors.New("获取分类信息失败,Err:" + err.Error())
  70. return
  71. }
  72. } else {
  73. excelInfo, err = excel.GetExcelInfoById(excelInfoId)
  74. if err != nil {
  75. if err.Error() == utils.ErrNoRow() {
  76. errMsg = "当前表格不存在"
  77. err = errors.New("获取分类信息失败,Err:" + err.Error())
  78. return
  79. }
  80. errMsg = "移动失败"
  81. err = errors.New("获取分类信息失败,Err:" + err.Error())
  82. return
  83. }
  84. if parentClassifyId == 0 {
  85. errMsg = "移动失败,表格必须挂在分类下"
  86. err = errors.New(errMsg)
  87. return
  88. }
  89. }
  90. if prevClassifyId > 0 {
  91. prevClassify, err = excel.GetExcelClassifyById(prevClassifyId)
  92. if err != nil {
  93. errMsg = "移动失败"
  94. err = errors.New("获取上一个兄弟节点分类信息失败,Err:" + err.Error())
  95. return
  96. }
  97. prevSort = prevClassify.Sort
  98. } else if prevExcelInfoId > 0 {
  99. prevExcelInfo, err = excel.GetExcelInfoById(prevExcelInfoId)
  100. if err != nil {
  101. errMsg = "移动失败"
  102. err = errors.New("获取上一个兄弟节点分类信息失败,Err:" + err.Error())
  103. return
  104. }
  105. prevSort = prevExcelInfo.Sort
  106. }
  107. if nextClassifyId > 0 {
  108. //下一个兄弟节点
  109. nextClassify, err = excel.GetExcelClassifyById(nextClassifyId)
  110. if err != nil {
  111. errMsg = "移动失败"
  112. err = errors.New("获取下一个兄弟节点分类信息失败,Err:" + err.Error())
  113. return
  114. }
  115. nextSort = nextClassify.Sort
  116. } else if nextExcelInfoId > 0 {
  117. //下一个兄弟节点
  118. nextExcelInfo, err = excel.GetExcelInfoById(nextExcelInfoId)
  119. if err != nil {
  120. errMsg = "移动失败"
  121. err = errors.New("获取下一个兄弟节点分类信息失败,Err:" + err.Error())
  122. return
  123. }
  124. nextSort = nextExcelInfo.Sort
  125. }
  126. err, errMsg = moveExcelClassify(parentExcelClassifyInfo, excelClassifyInfo, prevClassify, nextClassify, excelInfo, prevExcelInfo, nextExcelInfo, parentClassifyId, prevSort, nextSort, source)
  127. return
  128. }
  129. // moveExcelClassify 移动表格分类
  130. func moveExcelClassify(parentExcelClassifyInfo, excelClassifyInfo, prevClassify, nextClassify *excel.ExcelClassify, excelInfo, prevExcelInfo, nextExcelInfo *excel.ExcelInfo, parentClassifyId int, prevSort, nextSort int, source int) (err error, errMsg string) {
  131. updateCol := make([]string, 0)
  132. // 移动对象为分类, 判断分类是否存在
  133. if excelClassifyInfo != nil {
  134. oldParentId := excelClassifyInfo.ParentId
  135. /*oldLevel := excelClassifyInfo.Level
  136. var classifyIds []int*/
  137. if oldParentId != parentClassifyId {
  138. //todo 更新子分类对应的level
  139. /*childList, e, m := GetChildClassifyByClassifyId(excelClassifyInfo.ClassifyId)
  140. if e != nil {
  141. errMsg = "移动失败"
  142. err = errors.New("查询子分类失败,Err:" + e.Error() + m)
  143. return
  144. }
  145. if len(childList) > 0 {
  146. for _, v := range childList {
  147. if v.ClassifyId == excelClassifyInfo.ClassifyId {
  148. continue
  149. }
  150. classifyIds = append(classifyIds, v.ClassifyId)
  151. }
  152. }*/
  153. }
  154. //判断上级id是否一致,如果不一致的话,那么需要移动该分类层级
  155. if excelClassifyInfo.ParentId != parentClassifyId && parentClassifyId != 0 {
  156. if excelClassifyInfo.Level != parentExcelClassifyInfo.Level+1 { //禁止层级调整
  157. errMsg = "移动失败"
  158. err = errors.New("不支持目录层级变更")
  159. return
  160. }
  161. excelClassifyInfo.ParentId = parentExcelClassifyInfo.ExcelClassifyId
  162. excelClassifyInfo.Level = parentExcelClassifyInfo.Level + 1
  163. excelClassifyInfo.ModifyTime = time.Now()
  164. updateCol = append(updateCol, "ParentId", "Level", "ModifyTime")
  165. } else if excelClassifyInfo.ParentId != parentClassifyId && parentClassifyId == 0 {
  166. errMsg = "移动失败"
  167. err = errors.New("不支持目录层级变更")
  168. return
  169. }
  170. if prevSort > 0 {
  171. //如果是移动在两个兄弟节点之间
  172. if nextSort > 0 {
  173. //下一个兄弟节点
  174. //如果上一个兄弟与下一个兄弟的排序权重是一致的,那么需要将下一个兄弟(以及下个兄弟的同样排序权重)的排序权重+2,自己变成上一个兄弟的排序权重+1
  175. if prevSort == nextSort || prevSort == excelClassifyInfo.Sort {
  176. //变更兄弟节点的排序
  177. updateSortStr := `sort + 2`
  178. //变更分类
  179. if prevClassify != nil {
  180. _ = excel.UpdateExcelClassifySortByParentId(parentClassifyId, prevClassify.ExcelClassifyId, prevClassify.Sort, updateSortStr, source)
  181. } else {
  182. _ = excel.UpdateExcelClassifySortByParentId(parentClassifyId, 0, prevSort, updateSortStr, source)
  183. }
  184. //变更表格
  185. if prevExcelInfo != nil {
  186. //变更兄弟节点的排序
  187. _ = excel.UpdateExcelInfoSortByClassifyId(parentClassifyId, prevSort, prevExcelInfo.ExcelInfoId, updateSortStr, source)
  188. } else {
  189. _ = excel.UpdateExcelInfoSortByClassifyId(parentClassifyId, prevSort, 0, updateSortStr, source)
  190. }
  191. } else {
  192. //如果下一个兄弟的排序权重正好是上个兄弟节点的下一层,那么需要再加一层了
  193. if nextSort-prevSort == 1 {
  194. //变更兄弟节点的排序
  195. updateSortStr := `sort + 1`
  196. //变更分类
  197. if prevClassify != nil {
  198. _ = excel.UpdateExcelClassifySortByParentId(parentClassifyId, prevClassify.ExcelClassifyId, prevSort, updateSortStr, source)
  199. } else {
  200. _ = excel.UpdateExcelClassifySortByParentId(parentClassifyId, 0, prevSort, updateSortStr, source)
  201. }
  202. //变更表格
  203. if prevExcelInfo != nil {
  204. //变更兄弟节点的排序
  205. _ = excel.UpdateExcelInfoSortByClassifyId(parentClassifyId, prevSort, prevExcelInfo.ExcelInfoId, updateSortStr, source)
  206. } else {
  207. _ = excel.UpdateExcelInfoSortByClassifyId(parentClassifyId, prevSort, 0, updateSortStr, source)
  208. }
  209. }
  210. }
  211. }
  212. excelClassifyInfo.Sort = prevSort + 1
  213. excelClassifyInfo.ModifyTime = time.Now()
  214. updateCol = append(updateCol, "Sort", "ModifyTime")
  215. } else if prevClassify == nil && nextClassify == nil && prevExcelInfo == nil && nextExcelInfo == nil && parentClassifyId > 0 {
  216. //处理只拖动到目录里,默认放到目录底部的情况
  217. var maxSort int
  218. maxSort, err = GetExcelClassifyMaxSort(parentClassifyId, source)
  219. if err != nil {
  220. errMsg = "移动失败"
  221. err = errors.New("查询组内排序信息失败,Err:" + err.Error())
  222. return
  223. }
  224. excelClassifyInfo.Sort = maxSort + 1 //那就是排在组内最后一位
  225. excelClassifyInfo.ModifyTime = time.Now()
  226. updateCol = append(updateCol, "Sort", "ModifyTime")
  227. } else {
  228. // 拖动到父级分类的第一位
  229. firstClassify, tmpErr := excel.GetFirstExcelClassifyByParentId(parentClassifyId)
  230. if tmpErr != nil && tmpErr.Error() != utils.ErrNoRow() {
  231. errMsg = "移动失败"
  232. err = errors.New("获取获取当前父级分类下的排序第一条的分类信息失败,Err:" + tmpErr.Error())
  233. return
  234. }
  235. //如果该分类下存在其他分类,且第一个其他分类的排序等于0,那么需要调整排序
  236. if firstClassify != nil && firstClassify.Sort == 0 {
  237. updateSortStr := ` sort + 1 `
  238. _ = excel.UpdateExcelClassifySortByParentId(parentClassifyId, firstClassify.ExcelClassifyId-1, 0, updateSortStr, source)
  239. //该分类下的所有表格也需要+1
  240. _ = excel.UpdateExcelInfoSortByClassifyId(parentClassifyId, 0, 0, updateSortStr, source)
  241. } else {
  242. //如果该分类下存在表格,且第一个表格的排序等于0,那么需要调整排序
  243. firstExcel, tErr := excel.GetFirstExcelInfoByClassifyId(parentClassifyId)
  244. if tErr != nil && tErr.Error() != utils.ErrNoRow() {
  245. errMsg = "移动失败"
  246. err = errors.New("获取获取当前父级分类下的排序第一条的分类信息失败,Err:" + tErr.Error())
  247. return
  248. }
  249. //如果该分类下存在其他分类,且第一个其他分类的排序等于0,那么需要调整排序
  250. if firstExcel != nil && firstExcel.Sort == 0 {
  251. updateSortStr := ` sort + 1 `
  252. _ = excel.UpdateExcelInfoSortByClassifyId(parentClassifyId, 0, firstExcel.ExcelInfoId-1, updateSortStr, source)
  253. _ = excel.UpdateExcelClassifySortByParentId(parentClassifyId, 0, 0, updateSortStr, source)
  254. }
  255. }
  256. excelClassifyInfo.Sort = 0 //那就是排在第一位
  257. excelClassifyInfo.ModifyTime = time.Now()
  258. updateCol = append(updateCol, "Sort", "ModifyTime")
  259. }
  260. //更新
  261. if len(updateCol) > 0 {
  262. err = excelClassifyInfo.Update(updateCol)
  263. if err != nil {
  264. errMsg = "移动失败"
  265. err = errors.New("修改失败,Err:" + err.Error())
  266. return
  267. }
  268. //更新对应分类的root_id和层级
  269. if oldParentId != parentClassifyId {
  270. /*if len(classifyIds) > 0 {
  271. levelStep := excelClassifyInfo.Level - oldLevel
  272. err = data_manage.UpdateEdbClassifyChildByParentClassifyId(classifyIds, excelClassifyInfo.RootId, levelStep)
  273. if err != nil {
  274. errMsg = "移动失败"
  275. err = errors.New("更新子分类失败,Err:" + err.Error())
  276. return
  277. }
  278. }*/
  279. }
  280. }
  281. } else {
  282. if excelInfo == nil {
  283. errMsg = "当前表格不存在"
  284. err = errors.New(errMsg)
  285. return
  286. }
  287. //如果改变了分类,那么移动该表格数据
  288. if excelInfo.ExcelClassifyId != parentClassifyId {
  289. excelInfo.ExcelClassifyId = parentClassifyId
  290. excelInfo.ModifyTime = time.Now()
  291. updateCol = append(updateCol, "ExcelClassifyId", "ModifyTime")
  292. }
  293. if prevSort > 0 {
  294. //如果是移动在两个兄弟节点之间
  295. if nextSort > 0 {
  296. //下一个兄弟节点
  297. //如果上一个兄弟与下一个兄弟的排序权重是一致的,那么需要将下一个兄弟(以及下个兄弟的同样排序权重)的排序权重+2,自己变成上一个兄弟的排序权重+1
  298. if prevSort == nextSort || prevSort == excelInfo.Sort {
  299. //变更兄弟节点的排序
  300. updateSortStr := `sort + 2`
  301. //变更分类
  302. if prevClassify != nil {
  303. _ = excel.UpdateExcelClassifySortByParentId(parentClassifyId, prevClassify.ExcelClassifyId, prevClassify.Sort, updateSortStr, source)
  304. } else {
  305. _ = excel.UpdateExcelClassifySortByParentId(parentClassifyId, 0, prevSort, updateSortStr, source)
  306. }
  307. //变更表格
  308. if prevExcelInfo != nil {
  309. //变更兄弟节点的排序
  310. _ = excel.UpdateExcelInfoSortByClassifyId(parentClassifyId, prevSort, prevExcelInfo.ExcelInfoId, updateSortStr, source)
  311. } else {
  312. _ = excel.UpdateExcelInfoSortByClassifyId(parentClassifyId, prevSort, 0, updateSortStr, source)
  313. }
  314. } else {
  315. //如果下一个兄弟的排序权重正好是上个兄弟节点的下一层,那么需要再加一层了
  316. if nextSort-prevSort == 1 {
  317. //变更兄弟节点的排序
  318. updateSortStr := `sort + 1`
  319. //变更分类
  320. if prevClassify != nil {
  321. _ = excel.UpdateExcelClassifySortByParentId(parentClassifyId, prevClassify.ExcelClassifyId, prevSort, updateSortStr, source)
  322. } else {
  323. _ = excel.UpdateExcelClassifySortByParentId(parentClassifyId, 0, prevSort, updateSortStr, source)
  324. }
  325. //变更表格
  326. if prevExcelInfo != nil {
  327. //变更兄弟节点的排序
  328. _ = excel.UpdateExcelInfoSortByClassifyId(parentClassifyId, prevSort, prevExcelInfo.ExcelInfoId, updateSortStr, source)
  329. } else {
  330. _ = excel.UpdateExcelInfoSortByClassifyId(parentClassifyId, prevSort, 0, updateSortStr, source)
  331. }
  332. }
  333. }
  334. }
  335. excelInfo.Sort = prevSort + 1
  336. excelInfo.ModifyTime = time.Now()
  337. updateCol = append(updateCol, "Sort", "ModifyTime")
  338. } else if prevClassify == nil && nextClassify == nil && prevExcelInfo == nil && nextExcelInfo == nil && parentClassifyId > 0 {
  339. //处理只拖动到目录里,默认放到目录底部的情况
  340. var maxSort int
  341. maxSort, err = GetExcelClassifyMaxSort(parentClassifyId, source)
  342. if err != nil {
  343. errMsg = "移动失败"
  344. err = errors.New("查询组内排序信息失败,Err:" + err.Error())
  345. return
  346. }
  347. excelInfo.Sort = maxSort + 1 //那就是排在组内最后一位
  348. excelInfo.ModifyTime = time.Now()
  349. updateCol = append(updateCol, "Sort", "ModifyTime")
  350. } else {
  351. // 拖动到父级分类的第一位
  352. firstClassify, tmpErr := excel.GetFirstExcelClassifyByParentId(parentClassifyId)
  353. if tmpErr != nil && tmpErr.Error() != utils.ErrNoRow() {
  354. errMsg = "移动失败"
  355. err = errors.New("获取获取当前父级分类下的排序第一条的分类信息失败,Err:" + tmpErr.Error())
  356. return
  357. }
  358. //如果该分类下存在其他分类,且第一个其他分类的排序等于0,那么需要调整排序
  359. if firstClassify != nil && firstClassify.Sort == 0 {
  360. updateSortStr := ` sort + 1 `
  361. _ = excel.UpdateExcelClassifySortByParentId(parentClassifyId, firstClassify.ExcelClassifyId-1, 0, updateSortStr, source)
  362. //该分类下的所有表格也需要+1
  363. _ = excel.UpdateExcelInfoSortByClassifyId(parentClassifyId, 0, 0, updateSortStr, source)
  364. } else {
  365. //如果该分类下存在表格,且第一个表格的排序等于0,那么需要调整排序
  366. firstExcel, tErr := excel.GetFirstExcelInfoByClassifyId(parentClassifyId)
  367. if tErr != nil && tErr.Error() != utils.ErrNoRow() {
  368. errMsg = "移动失败"
  369. err = errors.New("获取获取当前父级分类下的排序第一条的分类信息失败,Err:" + tErr.Error())
  370. return
  371. }
  372. //如果该分类下存在其他分类,且第一个其他分类的排序等于0,那么需要调整排序
  373. if firstExcel != nil && firstExcel.Sort == 0 {
  374. updateSortStr := ` sort + 1 `
  375. _ = excel.UpdateExcelInfoSortByClassifyId(parentClassifyId, 0, firstExcel.ExcelInfoId-1, updateSortStr, source)
  376. _ = excel.UpdateExcelClassifySortByParentId(parentClassifyId, 0, 0, updateSortStr, source)
  377. }
  378. }
  379. excelInfo.Sort = 0 //那就是排在第一位
  380. excelInfo.ModifyTime = time.Now()
  381. updateCol = append(updateCol, "Sort", "ModifyTime")
  382. }
  383. //更新
  384. if len(updateCol) > 0 {
  385. err = excelInfo.Update(updateCol)
  386. if err != nil {
  387. errMsg = "移动失败"
  388. err = errors.New("修改失败,Err:" + err.Error())
  389. return
  390. }
  391. }
  392. }
  393. return
  394. }
  395. func GetChildClassifyByClassifyId(targetClassifyId int, source int) (targetList []*excel.ExcelClassifyItems, err error, errMsg string) {
  396. //判断是否是挂在顶级目录下
  397. targetClassify, err := excel.GetExcelClassifyById(targetClassifyId)
  398. if err != nil {
  399. if err.Error() == utils.ErrNoRow() {
  400. errMsg = "当前分类不存在"
  401. err = errors.New(errMsg)
  402. return
  403. }
  404. errMsg = "获取失败"
  405. err = errors.New("获取分类信息失败,Err:" + err.Error())
  406. return
  407. }
  408. tmpList, err := excel.GetExcelClassifyBySourceOrderByLevel(source)
  409. if err != nil && err.Error() != utils.ErrNoRow() {
  410. errMsg = "获取失败"
  411. err = errors.New("获取数据失败,Err:" + err.Error())
  412. return
  413. }
  414. idMap := make(map[int]struct{})
  415. if len(tmpList) > 0 {
  416. for _, v := range tmpList {
  417. if v.ExcelClassifyId == targetClassify.ExcelClassifyId {
  418. idMap[v.ExcelClassifyId] = struct{}{}
  419. }
  420. }
  421. for _, v := range tmpList {
  422. if _, ok := idMap[v.ParentId]; ok {
  423. idMap[v.ExcelClassifyId] = struct{}{}
  424. }
  425. }
  426. for _, v := range tmpList {
  427. if _, ok := idMap[v.ExcelClassifyId]; ok {
  428. targetItem := new(excel.ExcelClassifyItems)
  429. targetItem.ExcelClassifyId = v.ExcelClassifyId
  430. targetItem.ParentId = v.ParentId
  431. targetItem.UniqueCode = v.UniqueCode
  432. targetItem.Level = v.Level
  433. targetList = append(targetList, targetItem)
  434. }
  435. }
  436. }
  437. return
  438. }
  439. // GetClassifyListRemoveNoExcel 去除没有表格的分类
  440. func GetClassifyListRemoveNoExcel(classifyList []*excel.ExcelClassifyItems) []*excel.ExcelClassifyItems {
  441. res := make([]*excel.ExcelClassifyItems, 0)
  442. for _, classify := range classifyList {
  443. if classify.ExcelInfoId > 0 {
  444. res = append(res, classify)
  445. continue
  446. }
  447. classify.Children = append([]*excel.ExcelClassifyItems{}, GetClassifyListRemoveNoExcel(classify.Children)...)
  448. if len(classify.Children) > 0 {
  449. res = append(res, classify)
  450. }
  451. }
  452. return res
  453. }