excel_classify.go 17 KB

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