123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169 |
- package services
- import (
- "eta/eta_api/models"
- "eta/eta_api/utils"
- "fmt"
- "time"
- )
- func MoveEnglishReportClassify(req models.EnglishClassifyMoveReq) (err error, errMsg string) {
- classifyId := req.ClassifyId
- prevClassifyId := req.PrevClassifyId
- nextClassifyId := req.NextClassifyId
-
- var (
- classifyInfo *models.EnglishClassify
- prevClassify *models.EnglishClassify
- nextClassify *models.EnglishClassify
- prevSort int
- nextSort int
- )
-
- classifyInfo, err = models.GetEnglishReportClassifyById(classifyId)
- if err != nil {
- if err.Error() == utils.ErrNoRow() {
- errMsg = "分类不存在, 请刷新页面"
- err = fmt.Errorf("获取分类信息失败,Err:" + err.Error())
- return
- }
- errMsg = "移动失败"
- err = fmt.Errorf("获取分类信息失败,Err:" + err.Error())
- return
- } else if classifyInfo.Id == 0 {
- errMsg = "分类不存在, 请刷新页面"
- err = fmt.Errorf("获取分类信息失败,Err:" + err.Error())
- return
- }
- parentClassifyId := classifyInfo.ParentId
- if prevClassifyId > 0 {
- prevClassify, err = models.GetEnglishReportClassifyById(prevClassifyId)
- if err != nil {
- if err.Error() == utils.ErrNoRow() {
- errMsg = "上一个分类不存在, 请刷新页面"
- err = fmt.Errorf("获取分类信息失败,Err:" + err.Error())
- return
- }
- errMsg = "移动失败"
- err = fmt.Errorf("获取上一个兄弟节点分类信息失败,Err:" + err.Error())
- return
- }
- prevSort = prevClassify.Sort
- }
- if nextClassifyId > 0 {
-
- nextClassify, err = models.GetEnglishReportClassifyById(nextClassifyId)
- if err != nil {
- if err.Error() == utils.ErrNoRow() {
- errMsg = "下一个分类不存在, 请刷新页面"
- err = fmt.Errorf("获取分类信息失败,Err:" + err.Error())
- return
- }
- errMsg = "移动失败"
- err = fmt.Errorf("获取下一个兄弟节点分类信息失败,Err:" + err.Error())
- return
- }
- nextSort = nextClassify.Sort
- }
- err, errMsg = moveEnglishReportClassify(classifyInfo, prevClassify, nextClassify, parentClassifyId, prevSort, nextSort)
- return
- }
- func moveEnglishReportClassify(classifyInfo, prevClassify, nextClassify *models.EnglishClassify, parentId, prevSort, nextSort int) (err error, errMsg string) {
- ob := new(models.EnglishClassify)
- updateCol := make([]string, 0)
-
- if classifyInfo.ParentId != parentId {
- errMsg = "移动失败"
- err = fmt.Errorf("不支持目录层级变更")
- return
- }
- if prevSort > 0 {
-
- if nextSort > 0 {
-
-
- if prevSort == nextSort || prevSort == classifyInfo.Sort {
-
- updateSortStr := `sort + 2`
-
- if prevClassify != nil {
- _ = models.UpdateEnglishClassifySortByParentId(parentId, prevClassify.Id, prevClassify.Sort, updateSortStr)
- } else {
- _ = models.UpdateEnglishClassifySortByParentId(parentId, 0, prevSort, updateSortStr)
- }
- } else {
-
- if nextSort-prevSort == 1 {
-
- updateSortStr := `sort + 1`
-
- if prevClassify != nil {
- _ = models.UpdateEnglishClassifySortByParentId(parentId, prevClassify.Id, prevSort, updateSortStr)
- } else {
- _ = models.UpdateEnglishClassifySortByParentId(parentId, 0, prevSort, updateSortStr)
- }
- }
- }
- }
- classifyInfo.Sort = prevSort + 1
- classifyInfo.ModifyTime = time.Now()
- updateCol = append(updateCol, "Sort", "ModifyTime")
- } else if prevClassify == nil && nextClassify == nil && parentId > 0 {
-
- var maxSort int
- maxSort, err = ob.GetMaxSortByParentId(parentId)
- if err != nil {
- errMsg = "移动失败"
- err = fmt.Errorf("查询组内排序信息失败,Err:" + err.Error())
- return
- }
- classifyInfo.Sort = maxSort + 1
- classifyInfo.ModifyTime = time.Now()
- updateCol = append(updateCol, "Sort", "ModifyTime")
- } else {
-
- firstPermission, tmpErr := ob.GetFirstClassifyByParentId(parentId)
- if tmpErr != nil && tmpErr.Error() != utils.ErrNoRow() {
- errMsg = "移动失败"
- err = fmt.Errorf("获取获取当前父级分类下的排序第一条的分类信息失败,Err:" + tmpErr.Error())
- return
- }
-
- if firstPermission != nil && firstPermission.Id != 0 && firstPermission.Sort == 0 {
- updateSortStr := ` sort + 1 `
- _ = models.UpdateEnglishClassifySortByParentId(parentId, firstPermission.Id-1, 0, updateSortStr)
- }
- classifyInfo.Sort = 0
- classifyInfo.ModifyTime = time.Now()
- updateCol = append(updateCol, "Sort", "ModifyTime")
- }
-
- if len(updateCol) > 0 {
- err = classifyInfo.UpdateEnglishClassify(updateCol)
- if err != nil {
- errMsg = "移动失败"
- err = fmt.Errorf("修改失败,Err:" + err.Error())
- return
- }
- }
- return
- }
|