123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255 |
- package services
- import (
- "eta/eta_api/models"
- "eta/eta_api/models/report_approve"
- "eta/eta_api/utils"
- "fmt"
- "time"
- )
- // MoveEnglishReportClassify 移动分类
- func MoveEnglishReportClassify(req models.EnglishClassifyMoveReq) (err error, errMsg string) {
- classifyId := req.ClassifyId
- prevClassifyId := req.PrevClassifyId
- nextClassifyId := req.NextClassifyId
- //如果有传入 上一个兄弟节点分类id
- 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
- }
- // moveEnglishReportClassify 移动分类
- func moveEnglishReportClassify(classifyInfo, prevClassify, nextClassify *models.EnglishClassify, parentId, prevSort, nextSort int) (err error, errMsg string) {
- ob := new(models.EnglishClassify)
- updateCol := make([]string, 0)
- //判断上级id是否一致,如果不一致的话,那么需要移动该分类层级
- if classifyInfo.ParentId != parentId {
- errMsg = "移动失败"
- err = fmt.Errorf("不支持目录层级变更")
- return
- }
- if prevSort > 0 {
- //如果是移动在两个兄弟节点之间
- if nextSort > 0 {
- //下一个兄弟节点
- //如果上一个兄弟与下一个兄弟的排序权重是一致的,那么需要将下一个兄弟(以及下个兄弟的同样排序权重)的排序权重+2,自己变成上一个兄弟的排序权重+1
- 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
- }
- //如果该分类下存在其他分类,且第一个其他分类的排序等于0,那么需要调整排序
- 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
- }
- func MarkEnableDeleteEnlishClassify(rootClassifyList, childClassifyList []*models.EnglishClassifyList) (err error) {
- if len(rootClassifyList) == 0 && len(childClassifyList) == 0 {
- return
- }
- // 合并分类列表
- allClassifyList := append(rootClassifyList, childClassifyList...)
- classifyIds := make([]int, len(allClassifyList))
- rootClassifyMap := make(map[int]*models.EnglishClassifyList)
- for i, v := range allClassifyList {
- classifyIds[i] = v.Id
- if v.RootId == v.Id {
- rootClassifyMap[v.Id] = v
- }
- }
- // 检查是否有报告
- reportList, err := models.GetExistEnglishReportClassifyIdByClassifyIds(classifyIds)
- if err != nil {
- return
- }
- // 查询该分类是否关联了审批流
- flowOb := new(report_approve.ReportApproveFlow)
- flowCond := fmt.Sprintf(` AND %s = ? `, report_approve.ReportApproveFlowCols.ReportType)
- flowPars := make([]interface{}, 0)
- flowPars = append(flowPars, report_approve.FlowReportTypeEnglish)
- flowList, err := flowOb.GetItemsByCondition(flowCond, flowPars, []string{"classify_first_id", "classify_second_id", "classify_third_id"}, "")
- if err != nil {
- return
- }
- // 查询路演视频分类
- videoClassify, err := models.GetAllEnglishVideoClassify()
- if err != nil {
- return
- }
- disableDeleteClassifyMap := make(map[int]struct{})
- // 记录关联报告的分类
- for _, v := range reportList {
- if v.ClassifyIdFirst > 0 {
- disableDeleteClassifyMap[v.ClassifyIdFirst] = struct{}{}
- }
- if v.ClassifyIdSecond > 0 {
- disableDeleteClassifyMap[v.ClassifyIdSecond] = struct{}{}
- }
- }
- // 记录关联审批流的分类
- for _, v := range flowList {
- if v.ClassifyFirstId > 0 {
- disableDeleteClassifyMap[v.ClassifyFirstId] = struct{}{}
- }
- if v.ClassifySecondId > 0 {
- disableDeleteClassifyMap[v.ClassifySecondId] = struct{}{}
- }
- if v.ClassifyThirdId > 0 {
- disableDeleteClassifyMap[v.ClassifyThirdId] = struct{}{}
- }
- }
- for _, v := range videoClassify {
- if v.ClassifyIdFirst > 0 {
- disableDeleteClassifyMap[v.Id] = struct{}{}
- }
- if v.ClassifyIdSecond > 0 {
- disableDeleteClassifyMap[v.Id] = struct{}{}
- }
- }
- markedClassifyMap := make(map[int]struct{})
- for _, v := range allClassifyList {
- if _, ok := markedClassifyMap[v.Id]; ok {
- continue
- }
- if _, ok := disableDeleteClassifyMap[v.Id]; !ok {
- v.IsEnableDelete = 1
- } else {
- rootClassifyMap[v.RootId].IsEnableDelete = 0
- markedClassifyMap[v.RootId] = struct{}{}
- }
- markedClassifyMap[v.Id] = struct{}{}
- }
- return
- }
|