common_classify.go 20 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485
  1. package data
  2. import (
  3. "eta/eta_api/models/common"
  4. "eta/eta_api/utils"
  5. "fmt"
  6. "strconv"
  7. "strings"
  8. "time"
  9. )
  10. func CommonClassifyMove(req common.CommonClassifyMoveReq, strategy CommonClassifyStrategy) (tips string, err error) {
  11. ctx := NewCommonClassifyCtx(strategy)
  12. var (
  13. classify *common.CommonClassify
  14. parentClassify *common.CommonClassify
  15. prevClassify *common.CommonClassify
  16. nextClassify *common.CommonClassify
  17. object *common.CommonClassifyObj
  18. prevObject *common.CommonClassifyObj
  19. nextObject *common.CommonClassifyObj
  20. sortPrev int
  21. sortNext int
  22. )
  23. // 获取父级分类
  24. if req.ParentClassifyId > 0 {
  25. c, e := ctx.GetCommonClassifyById(req.ParentClassifyId)
  26. if e != nil {
  27. err = fmt.Errorf("获取上级分类失败, %v", e)
  28. return
  29. }
  30. parentClassify = c
  31. }
  32. // 兄弟节点
  33. if req.PrevClassifyId > 0 {
  34. c, e := ctx.GetCommonClassifyById(req.PrevClassifyId)
  35. if e != nil {
  36. if e.Error() == utils.ErrNoRow() {
  37. tips = "上一个分类不存在, 请刷新页面"
  38. return
  39. }
  40. err = fmt.Errorf("获取上一个分类失败, %v", e)
  41. return
  42. }
  43. prevClassify = c
  44. sortPrev = prevClassify.Sort
  45. } else if req.PrevObjectId > 0 {
  46. obj, e := ctx.GetObjectById(req.PrevObjectId)
  47. if e != nil {
  48. if e.Error() == utils.ErrNoRow() {
  49. tips = "上一个移动对象不存在, 请刷新页面"
  50. return
  51. }
  52. err = fmt.Errorf("获取上一个移动对象失败, %v", e)
  53. return
  54. }
  55. prevObject = obj
  56. sortPrev = prevObject.Sort
  57. }
  58. if req.NextClassifyId > 0 {
  59. c, e := ctx.GetCommonClassifyById(req.NextClassifyId)
  60. if e != nil {
  61. if e.Error() == utils.ErrNoRow() {
  62. tips = "下一个分类不存在, 请刷新页面"
  63. return
  64. }
  65. err = fmt.Errorf("获取下一个分类失败, %v", e)
  66. return
  67. }
  68. nextClassify = c
  69. sortNext = nextClassify.Sort
  70. } else if req.NextObjectId > 0 {
  71. obj, e := ctx.GetObjectById(req.NextObjectId)
  72. if e != nil {
  73. if e.Error() == utils.ErrNoRow() {
  74. tips = "下一个移动对象不存在, 请刷新页面"
  75. return
  76. }
  77. err = fmt.Errorf("获取下一个移动对象失败, %v", e)
  78. return
  79. }
  80. nextObject = obj
  81. sortNext = nextObject.Sort
  82. }
  83. // 移动分类
  84. if req.ObjectId == 0 {
  85. c, e := ctx.GetCommonClassifyById(req.ClassifyId)
  86. if e != nil {
  87. if e.Error() == utils.ErrNoRow() {
  88. tips = "当前分类不存在, 请刷新页面"
  89. return
  90. }
  91. err = fmt.Errorf("获取当前分类失败, %v", e)
  92. return
  93. }
  94. classify = c
  95. return moveCommonClassify(ctx, req, classify, parentClassify, prevClassify, nextClassify, prevObject, nextObject, sortPrev, sortNext)
  96. }
  97. // 移动对象
  98. if req.ObjectId > 0 {
  99. obj, e := ctx.GetObjectById(req.ObjectId)
  100. if e != nil {
  101. if e.Error() == utils.ErrNoRow() {
  102. tips = "移动对象不存在, 请刷新页面"
  103. return
  104. }
  105. err = fmt.Errorf("获取移动对象失败, %v", e)
  106. return
  107. }
  108. if req.ParentClassifyId <= 0 {
  109. tips = "移动对象必须挂在分类下"
  110. return
  111. }
  112. object = obj
  113. // TODO:对象的不同实现, 如指标校验权限
  114. return moveCommonClassifyObj(ctx, req, prevClassify, nextClassify, object, prevObject, nextObject, sortPrev, sortNext)
  115. }
  116. return
  117. }
  118. func moveCommonClassify(ctx *CommonClassifyCtx, req common.CommonClassifyMoveReq, classify, parentClassify, prevClassify, nextClassify *common.CommonClassify, prevObject, nextObject *common.CommonClassifyObj, sortPrev, sortNext int) (tips string, err error) {
  119. // 校验层级以及父级分类下同名分类
  120. if req.ParentClassifyId > 0 && parentClassify.Level == 6 {
  121. tips = "最高只支持添加6级分类"
  122. return
  123. }
  124. exists, e := ctx.GetClassifyByParentIdAndName(req.ParentClassifyId, classify.ClassifyName, classify.ClassifyId)
  125. if e != nil && e.Error() != utils.ErrNoRow() {
  126. err = fmt.Errorf("获取父级分类下的同名分类失败, %v", e)
  127. return
  128. }
  129. if exists != nil {
  130. tips = "当前父级分类下存在相同名称"
  131. return
  132. }
  133. // TODO:分类的非通用实现, 如指标分类需校验权限, 可以采用适配器模式兼容进来
  134. var classifyChildIds []int
  135. var classifyUpdateCols []string
  136. originParentId := classify.ParentId
  137. originLevel := classify.Level
  138. // 需更新子层级分类ID
  139. if originParentId != req.ParentClassifyId {
  140. // TODO:此处如果要兼容以前的分类表, 那么要提出来处理
  141. levelPathArr := strings.Split(classify.LevelPath, ",")
  142. for _, p := range levelPathArr {
  143. d, _ := strconv.Atoi(p)
  144. if d > 0 {
  145. classifyChildIds = append(classifyChildIds, d)
  146. }
  147. }
  148. }
  149. colsMapping := ctx.GetCommonClassifyCols() // 分类表实际的字段映射
  150. // 判断上级ID是否一致, 不一致的话需要移动该分类层级
  151. if classify.ParentId != req.ParentClassifyId && req.ParentClassifyId != 0 {
  152. if classify.Level != parentClassify.Level+1 { //禁止层级调整
  153. tips = "不支持目录层级变更"
  154. return
  155. }
  156. classify.ParentId = parentClassify.ClassifyId
  157. classify.RootId = parentClassify.RootId
  158. classify.Level = parentClassify.Level + 1
  159. classify.ModifyTime = time.Now()
  160. classifyUpdateCols = append(classifyUpdateCols, colsMapping.ParentId, colsMapping.RootId, colsMapping.Level, colsMapping.ModifyTime)
  161. }
  162. if classify.ParentId != req.ParentClassifyId && req.ParentClassifyId == 0 {
  163. tips = "不支持目录层级变更"
  164. return
  165. }
  166. if sortPrev > 0 {
  167. // 移动至两个兄弟之间
  168. if sortNext > 0 {
  169. // 如果上一个兄弟与下一个兄弟的排序权重是一致的, 那么需要将下一个兄弟(以及下个兄弟的同样排序权重)的排序权重+2, 自己变成上一个兄弟的排序权重+1
  170. if sortPrev == sortNext || sortPrev == classify.Sort {
  171. sortUpdate := `sort + 2`
  172. if prevClassify != nil {
  173. if e = ctx.SetClassifySortByParentId(req.ParentClassifyId, prevClassify.ClassifyId, prevClassify.Sort, sortUpdate); e != nil {
  174. err = fmt.Errorf("SetClassifySortByParentId, parentId: %d, classifyId: %d, sort: %d, sortUpdate: %s, err: %v", req.ParentClassifyId, prevClassify.ClassifyId, prevClassify.Sort, sortUpdate, e)
  175. return
  176. }
  177. } else {
  178. if e = ctx.SetClassifySortByParentId(req.ParentClassifyId, 0, sortPrev, sortUpdate); e != nil {
  179. err = fmt.Errorf("SetClassifySortByParentId, parentId: %d, classifyId: %d, sort: %d, sortUpdate: %s, err: %v", req.ParentClassifyId, 0, sortPrev, sortUpdate, e)
  180. return
  181. }
  182. }
  183. if prevObject != nil {
  184. if e = ctx.SetObjectSortByClassifyId(req.ParentClassifyId, sortPrev, prevObject.ObjectId, sortUpdate); e != nil {
  185. err = fmt.Errorf("SetObjectSortByClassifyId, classifyId: %d, sort: %d, objectId: %d, sortUpdate: %s, err: %v", req.ParentClassifyId, sortPrev, prevObject.ObjectId, sortUpdate, e)
  186. return
  187. }
  188. } else {
  189. if e = ctx.SetObjectSortByClassifyId(req.ParentClassifyId, sortPrev, 0, sortUpdate); e != nil {
  190. err = fmt.Errorf("SetObjectSortByClassifyId, classifyId: %d, sort: %d, objectId: %d, sortUpdate: %s, err: %v", req.ParentClassifyId, sortPrev, 0, sortUpdate, e)
  191. return
  192. }
  193. }
  194. } else {
  195. // 如果下一个兄弟的排序权重正好是上个兄弟节点的下一层, 那么需要再加一层
  196. if sortNext-sortPrev == 1 {
  197. sortUpdate := `sort + 1`
  198. if prevClassify != nil {
  199. if e = ctx.SetClassifySortByParentId(req.ParentClassifyId, prevClassify.ClassifyId, sortPrev, sortUpdate); e != nil {
  200. err = fmt.Errorf("SetClassifySortByParentId, parentId: %d, classifyId: %d, sort: %d, sortUpdate: %s, err: %v", req.ParentClassifyId, prevClassify.ClassifyId, sortPrev, sortUpdate, e)
  201. return
  202. }
  203. } else {
  204. if e = ctx.SetClassifySortByParentId(req.ParentClassifyId, 0, sortPrev, sortUpdate); e != nil {
  205. err = fmt.Errorf("SetClassifySortByParentId, parentId: %d, classifyId: %d, sort: %d, sortUpdate: %s, err: %v", req.ParentClassifyId, 0, sortPrev, sortUpdate, e)
  206. return
  207. }
  208. }
  209. if prevObject != nil {
  210. if e = ctx.SetObjectSortByClassifyId(req.ParentClassifyId, sortPrev, prevObject.ObjectId, sortUpdate); e != nil {
  211. err = fmt.Errorf("SetObjectSortByClassifyId, classifyId: %d, sort: %d, objectId: %d, sortUpdate: %s, err: %v", req.ParentClassifyId, sortPrev, prevObject.ObjectId, sortUpdate, e)
  212. return
  213. }
  214. } else {
  215. if e = ctx.SetObjectSortByClassifyId(req.ParentClassifyId, sortPrev, 0, sortUpdate); e != nil {
  216. err = fmt.Errorf("SetObjectSortByClassifyId, classifyId: %d, sort: %d, objectId: %d, sortUpdate: %s, err: %v", req.ParentClassifyId, sortPrev, 0, sortUpdate, e)
  217. return
  218. }
  219. }
  220. }
  221. }
  222. }
  223. classify.Sort = sortPrev + 1
  224. classify.ModifyTime = time.Now()
  225. classifyUpdateCols = append(classifyUpdateCols, colsMapping.Sort, colsMapping.ModifyTime)
  226. } else if prevClassify == nil && nextClassify == nil && prevObject == nil && nextObject == nil && req.ParentClassifyId > 0 {
  227. // 处理只拖动到目录里, 默认放到目录底部的情况
  228. m, e := GetCommonClassifySortMaxByParentId(req.ParentClassifyId, ctx)
  229. if e != nil {
  230. err = fmt.Errorf("GetCommonClassifySortMaxByParentId, %v", e)
  231. return
  232. }
  233. classify.Sort = m + 1
  234. classify.ModifyTime = time.Now()
  235. classifyUpdateCols = append(classifyUpdateCols, colsMapping.Sort, colsMapping.ModifyTime)
  236. } else {
  237. // 拖动到父级分类的第一位
  238. firstClassify, e := ctx.GetFirstClassifyByParentId(req.ParentClassifyId)
  239. if e != nil && e.Error() != utils.ErrNoRow() {
  240. //tips = "移动失败"
  241. err = fmt.Errorf("GetFirstClassifyByParentId, %v", e)
  242. return
  243. }
  244. // 如果该分类下存在其他分类, 且第一个其他分类的排序等于0, 那么需要调整排序
  245. if firstClassify != nil && firstClassify.Sort == 0 {
  246. sortUpdate := ` sort + 1 `
  247. if e = ctx.SetClassifySortByParentId(req.ParentClassifyId, firstClassify.ClassifyId-1, 0, sortUpdate); e != nil {
  248. err = fmt.Errorf("SetClassifySortByParentId, parentId: %d, classifyId: %d, sort: %d, sortUpdate: %s, err: %v", req.ParentClassifyId, firstClassify.ClassifyId-1, 0, sortUpdate, e)
  249. return
  250. }
  251. // 该分类下的所有指标也需要+1
  252. if e = ctx.SetObjectSortByClassifyId(req.ParentClassifyId, 0, 0, sortUpdate); e != nil {
  253. err = fmt.Errorf("SetObjectSortByClassifyId, classifyId: %d, sort: %d, objectId: %d, sortUpdate: %s, err: %v", req.ParentClassifyId, 0, 0, sortUpdate, e)
  254. return
  255. }
  256. } else {
  257. // 如果该分类下存在指标, 且第一个指标的排序等于0, 那么需要调整排序
  258. firstObject, e := ctx.GetFirstObjectByClassifyId(req.ParentClassifyId)
  259. if e != nil && e.Error() != utils.ErrNoRow() {
  260. err = fmt.Errorf("GetFirstObjectByClassifyId, %v", e)
  261. return
  262. }
  263. //如果该分类下存在其他分类,且第一个其他分类的排序等于0,那么需要调整排序
  264. if firstObject != nil && firstObject.Sort == 0 {
  265. sortUpdate := ` sort + 1 `
  266. if e = ctx.SetObjectSortByClassifyId(req.ParentClassifyId, 0, firstObject.ObjectId-1, sortUpdate); e != nil {
  267. err = fmt.Errorf("SetObjectSortByClassifyId, classifyId: %d, sort: %d, objectId: %d, sortUpdate: %s, err: %v", req.ParentClassifyId, 0, firstObject.ObjectId-1, sortUpdate, e)
  268. return
  269. }
  270. if e = ctx.SetClassifySortByParentId(req.ParentClassifyId, 0, 0, sortUpdate); e != nil {
  271. err = fmt.Errorf("SetClassifySortByParentId, parentId: %d, classifyId: %d, sort: %d, sortUpdate: %s, err: %v", req.ParentClassifyId, 0, 0, sortUpdate, e)
  272. return
  273. }
  274. }
  275. }
  276. classify.Sort = 0 // 那就是排在第一位
  277. classify.ModifyTime = time.Now()
  278. classifyUpdateCols = append(classifyUpdateCols, colsMapping.Sort, colsMapping.ModifyTime)
  279. }
  280. // 更新分类
  281. if len(classifyUpdateCols) > 0 {
  282. if e = ctx.UpdateCommonClassify(classify, classifyUpdateCols); e != nil {
  283. err = fmt.Errorf("UpdateCommonClassify, %v", e)
  284. return
  285. }
  286. // 更新对应分类的root_id和层级
  287. if originParentId != req.ParentClassifyId {
  288. if len(classifyChildIds) > 0 {
  289. stepLevel := classify.Level - originLevel
  290. if e = ctx.UpdateClassifyChildByParentId(classifyChildIds, classify.RootId, stepLevel); e != nil {
  291. err = fmt.Errorf("UpdateClassifyChildByParentId, parentId: %d, classifyId: %d, stepLevel: %d, err: %v", req.ParentClassifyId, classify.ClassifyId, stepLevel, e)
  292. return
  293. }
  294. }
  295. }
  296. }
  297. return
  298. }
  299. func moveCommonClassifyObj(ctx *CommonClassifyCtx, req common.CommonClassifyMoveReq, prevClassify, nextClassify *common.CommonClassify, object, prevObject, nextObject *common.CommonClassifyObj, sortPrev, sortNext int) (tips string, err error) {
  300. var objUpdateCols []string
  301. colsMapping := ctx.GetCommonClassifyObjCols() // 分类对象表实际的字段映射
  302. // 分类变更的情况
  303. if object.ClassifyId != req.ParentClassifyId {
  304. object.ClassifyId = req.ParentClassifyId
  305. object.ModifyTime = time.Now()
  306. objUpdateCols = append(objUpdateCols, colsMapping.ClassifyId, colsMapping.ModifyTime)
  307. }
  308. if sortPrev > 0 {
  309. // 移动至两个兄弟之间
  310. if sortNext > 0 {
  311. // 如果上一个兄弟与下一个兄弟的排序权重是一致的, 那么需要将下一个兄弟(以及下个兄弟的同样排序权重)的排序权重+2, 自己变成上一个兄弟的排序权重+1
  312. if sortPrev == sortNext || sortPrev == object.Sort {
  313. sortUpdate := `sort + 2`
  314. if prevClassify != nil {
  315. if e := ctx.SetClassifySortByParentId(req.ParentClassifyId, prevClassify.ClassifyId, prevClassify.Sort, sortUpdate); e != nil {
  316. err = fmt.Errorf("SetClassifySortByParentId, parentId: %d, classifyId: %d, sort: %d, sortUpdate: %s, err: %v", req.ParentClassifyId, prevClassify.ClassifyId, prevClassify.Sort, sortUpdate, e)
  317. return
  318. }
  319. } else {
  320. if e := ctx.SetClassifySortByParentId(req.ParentClassifyId, 0, sortPrev, sortUpdate); e != nil {
  321. err = fmt.Errorf("SetClassifySortByParentId, parentId: %d, classifyId: %d, sort: %d, sortUpdate: %s, err: %v", req.ParentClassifyId, 0, sortPrev, sortUpdate, e)
  322. return
  323. }
  324. }
  325. if prevObject != nil {
  326. if e := ctx.SetObjectSortByClassifyId(req.ParentClassifyId, sortPrev, prevObject.ObjectId, sortUpdate); e != nil {
  327. err = fmt.Errorf("SetObjectSortByClassifyId, classifyId: %d, sort: %d, objectId: %d, sortUpdate: %s, err: %v", req.ParentClassifyId, sortPrev, prevObject.ObjectId, sortUpdate, e)
  328. return
  329. }
  330. } else {
  331. if e := ctx.SetObjectSortByClassifyId(req.ParentClassifyId, sortPrev, 0, sortUpdate); e != nil {
  332. err = fmt.Errorf("SetObjectSortByClassifyId, classifyId: %d, sort: %d, objectId: %d, sortUpdate: %s, err: %v", req.ParentClassifyId, sortPrev, 0, sortUpdate, e)
  333. return
  334. }
  335. }
  336. } else {
  337. // 如果下一个兄弟的排序权重正好是上个兄弟节点的下一层, 那么需要再加一层
  338. if sortNext-sortPrev == 1 {
  339. sortUpdate := `sort + 1`
  340. if prevClassify != nil {
  341. if e := ctx.SetClassifySortByParentId(req.ParentClassifyId, prevClassify.ClassifyId, sortPrev, sortUpdate); e != nil {
  342. err = fmt.Errorf("SetClassifySortByParentId, parentId: %d, classifyId: %d, sort: %d, sortUpdate: %s, err: %v", req.ParentClassifyId, prevClassify.ClassifyId, sortPrev, sortUpdate, e)
  343. return
  344. }
  345. } else {
  346. if e := ctx.SetClassifySortByParentId(req.ParentClassifyId, 0, sortPrev, sortUpdate); e != nil {
  347. err = fmt.Errorf("SetClassifySortByParentId, parentId: %d, classifyId: %d, sort: %d, sortUpdate: %s, err: %v", req.ParentClassifyId, 0, sortPrev, sortUpdate, e)
  348. return
  349. }
  350. }
  351. if prevObject != nil {
  352. if e := ctx.SetObjectSortByClassifyId(req.ParentClassifyId, sortPrev, prevObject.ObjectId, sortUpdate); e != nil {
  353. err = fmt.Errorf("SetObjectSortByClassifyId, classifyId: %d, sort: %d, objectId: %d, sortUpdate: %s, err: %v", req.ParentClassifyId, sortPrev, prevObject.ObjectId, sortUpdate, e)
  354. return
  355. }
  356. } else {
  357. if e := ctx.SetObjectSortByClassifyId(req.ParentClassifyId, sortPrev, 0, sortUpdate); e != nil {
  358. err = fmt.Errorf("SetObjectSortByClassifyId, classifyId: %d, sort: %d, objectId: %d, sortUpdate: %s, err: %v", req.ParentClassifyId, sortPrev, 0, sortUpdate, e)
  359. return
  360. }
  361. }
  362. }
  363. }
  364. }
  365. object.Sort = sortPrev + 1
  366. object.ModifyTime = time.Now()
  367. objUpdateCols = append(objUpdateCols, colsMapping.Sort, colsMapping.ModifyTime)
  368. } else if prevClassify == nil && nextClassify == nil && prevObject == nil && nextObject == nil && req.ParentClassifyId > 0 {
  369. // 处理只拖动到目录里, 默认放到目录底部的情况
  370. m, e := GetCommonClassifySortMaxByParentId(req.ParentClassifyId, ctx)
  371. if e != nil {
  372. err = fmt.Errorf("GetCommonClassifySortMaxByParentId, %v", e)
  373. return
  374. }
  375. object.Sort = m + 1 //那就是排在组内最后一位
  376. object.ModifyTime = time.Now()
  377. objUpdateCols = append(objUpdateCols, colsMapping.Sort, colsMapping.ModifyTime)
  378. } else {
  379. // 拖动到父级分类的第一位
  380. firstClassify, e := ctx.GetFirstClassifyByParentId(req.ParentClassifyId)
  381. if e != nil && e.Error() != utils.ErrNoRow() {
  382. err = fmt.Errorf("GetFirstClassifyByParentId, %v", e)
  383. return
  384. }
  385. // 如果该分类下存在其他分类, 且第一个其他分类的排序等于0, 那么需要调整排序
  386. if firstClassify != nil && firstClassify.Sort == 0 {
  387. sortUpdate := ` sort + 1 `
  388. if e = ctx.SetClassifySortByParentId(req.ParentClassifyId, firstClassify.ClassifyId-1, 0, sortUpdate); e != nil {
  389. err = fmt.Errorf("SetClassifySortByParentId, parentId: %d, classifyId: %d, sort: %d, sortUpdate: %s, err: %v", req.ParentClassifyId, firstClassify.ClassifyId-1, 0, sortUpdate, e)
  390. return
  391. }
  392. // 该分类下的所有指标也需要+1
  393. if e = ctx.SetObjectSortByClassifyId(req.ParentClassifyId, 0, 0, sortUpdate); e != nil {
  394. err = fmt.Errorf("SetObjectSortByClassifyId, classifyId: %d, sort: %d, objectId: %d, sortUpdate: %s, err: %v", req.ParentClassifyId, 0, 0, sortUpdate, e)
  395. return
  396. }
  397. } else {
  398. // 如果该分类下存在对象, 且第一个对象的排序等于0, 那么需要调整排序
  399. firstObject, e := ctx.GetFirstObjectByClassifyId(req.ParentClassifyId)
  400. if e != nil && e.Error() != utils.ErrNoRow() {
  401. err = fmt.Errorf("GetFirstObjectByClassifyId, %v", e)
  402. return
  403. }
  404. // 如果该分类下存在其他分类, 且第一个其他分类的排序等于0, 那么需要调整排序
  405. if firstObject != nil && firstObject.Sort == 0 {
  406. sortUpdate := ` sort + 1 `
  407. if e = ctx.SetObjectSortByClassifyId(req.ParentClassifyId, 0, firstObject.ObjectId-1, sortUpdate); e != nil {
  408. err = fmt.Errorf("SetObjectSortByClassifyId, classifyId: %d, sort: %d, objectId: %d, sortUpdate: %s, err: %v", req.ParentClassifyId, 0, firstObject.ObjectId-1, sortUpdate, e)
  409. return
  410. }
  411. if e = ctx.SetClassifySortByParentId(req.ParentClassifyId, 0, 0, sortUpdate); e != nil {
  412. err = fmt.Errorf("SetClassifySortByParentId, parentId: %d, classifyId: %d, sort: %d, sortUpdate: %s, err: %v", req.ParentClassifyId, 0, 0, sortUpdate, e)
  413. return
  414. }
  415. }
  416. }
  417. object.Sort = 0 // 那就是排在第一位
  418. object.ModifyTime = time.Now()
  419. objUpdateCols = append(objUpdateCols, colsMapping.Sort, colsMapping.ModifyTime)
  420. }
  421. // 更新分类对象
  422. if len(objUpdateCols) > 0 {
  423. if e := ctx.UpdateCommonClassifyObj(object, objUpdateCols); e != nil {
  424. err = fmt.Errorf("UpdateCommonClassifyObj, %v", e)
  425. return
  426. }
  427. }
  428. return
  429. }
  430. // GetCommonClassifySortMaxByParentId 获取分类下最大排序
  431. func GetCommonClassifySortMaxByParentId(parentId int, ctx *CommonClassifyCtx) (sortMax int, err error) {
  432. // 比对分类和对象的最大排序
  433. classifyMax, e := ctx.GetClassifySortMaxByParentId(parentId)
  434. if e != nil {
  435. err = fmt.Errorf("GetClassifySortMaxByParentId, %v", e)
  436. return
  437. }
  438. sortMax = classifyMax
  439. objectMax, e := ctx.GetObjectSortMaxByClassifyId(parentId)
  440. if e != nil {
  441. err = fmt.Errorf("GetObjectSortMaxByClassifyId, %v", e)
  442. return
  443. }
  444. if sortMax < objectMax {
  445. sortMax = objectMax
  446. }
  447. return
  448. }