123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513 |
- package services
- import (
- "errors"
- aiPredictModel "eta/eta_api/models/ai_predict_model"
- "eta/eta_api/models/data_manage"
- "eta/eta_api/models/system"
- "eta/eta_api/utils"
- "fmt"
- "time"
- )
- // AiPredictModelMoveClassify 移动分类
- func AiPredictModelMoveClassify(req aiPredictModel.AiPredictModelClassifyMoveReq, sysUser *system.Admin) (err error, errMsg string) {
- classifyId := req.ClassifyId
- parentClassifyId := req.ParentClassifyId
- prevClassifyId := req.PrevClassifyId
- nextClassifyId := req.NextClassifyId
- itemId := req.ItemId
- prevItemId := req.PrevItemId
- nextItemId := req.NextItemId
- //首先确定移动的对象是分类还是指标
- //判断上一个节点是分类还是指标
- //判断下一个节点是分类还是指标
- //同时更新分类目录下的分类sort和指标sort
- //更新当前移动的分类或者指标sort
- classifyOb := new(aiPredictModel.AiPredictModelClassify)
- indexOb := new(aiPredictModel.AiPredictModelIndex)
- var parentClassify *aiPredictModel.AiPredictModelClassify
- if parentClassifyId > 0 {
- parentClassify, err = classifyOb.GetItemById(parentClassifyId)
- if err != nil {
- errMsg = "移动失败"
- err = errors.New("获取上级分类信息失败,Err:" + err.Error())
- return
- }
- }
- //如果有传入 上一个兄弟节点分类id
- var (
- edbClassifyInfo *aiPredictModel.AiPredictModelClassify
- prevClassify *aiPredictModel.AiPredictModelClassify
- nextClassify *aiPredictModel.AiPredictModelClassify
- edbInfo *aiPredictModel.AiPredictModelIndex
- prevEdbInfo *aiPredictModel.AiPredictModelIndex
- nextEdbInfo *aiPredictModel.AiPredictModelIndex
- prevSort int
- nextSort int
- )
- // 移动对象为分类, 判断权限
- if itemId == 0 {
- edbClassifyInfo, err = classifyOb.GetItemById(classifyId)
- if err != nil {
- if err.Error() == utils.ErrNoRow() {
- errMsg = "当前分类不存在"
- err = errors.New("获取分类信息失败,Err:" + err.Error())
- return
- }
- errMsg = "移动失败"
- err = errors.New("获取分类信息失败,Err:" + err.Error())
- return
- }
- if parentClassifyId > 0 && parentClassify.Level == 6 {
- errMsg = "最高只支持添加6级分类"
- err = errors.New(errMsg)
- return
- }
- // 如果是移动目录, 那么校验一下父级目录下是否有重名目录
- exists, e := data_manage.GetEdbClassifyByParentIdAndName(parentClassifyId, edbClassifyInfo.ClassifyName, classifyId)
- if e != nil && e.Error() != utils.ErrNoRow() {
- errMsg = "移动失败"
- err = fmt.Errorf("获取父级分类下的同名分类失败, Err: %s", e.Error())
- return
- }
- if exists != nil {
- errMsg = "移动失败,分类名称已存在"
- return
- }
- } else {
- edbInfo, err = indexOb.GetItemById(req.ItemId)
- if err != nil {
- if err.Error() == utils.ErrNoRow() {
- errMsg = "当前指标不存在"
- err = errors.New("获取分类信息失败,Err:" + err.Error())
- return
- }
- errMsg = "移动失败"
- err = errors.New("获取分类信息失败,Err:" + err.Error())
- return
- }
- if parentClassifyId == 0 {
- errMsg = "移动失败,指标必须挂在分类下"
- err = errors.New(errMsg)
- return
- }
- //// 移动权限校验
- //button := GetEdbOpButton(sysUser, edbInfo.SysUserId, edbInfo.EdbType, edbInfo.EdbInfoType, haveOperaAuth)
- //if !button.MoveButton {
- // errMsg = "无操作权限"
- // err = errors.New(errMsg)
- // return
- //}
- }
- if prevClassifyId > 0 {
- prevClassify, err = classifyOb.GetItemById(prevClassifyId)
- if err != nil {
- errMsg = "移动失败"
- err = errors.New("获取上一个兄弟节点分类信息失败,Err:" + err.Error())
- return
- }
- prevSort = prevClassify.Sort
- } else if prevItemId > 0 {
- prevEdbInfo, err = indexOb.GetItemById(prevItemId)
- if err != nil {
- errMsg = "移动失败"
- err = errors.New("获取上一个兄弟节点分类信息失败,Err:" + err.Error())
- return
- }
- prevSort = prevEdbInfo.Sort
- }
- if nextClassifyId > 0 {
- //下一个兄弟节点
- nextClassify, err = classifyOb.GetItemById(nextClassifyId)
- if err != nil {
- errMsg = "移动失败"
- err = errors.New("获取下一个兄弟节点分类信息失败,Err:" + err.Error())
- return
- }
- nextSort = nextClassify.Sort
- } else if nextItemId > 0 {
- //下一个兄弟节点
- nextEdbInfo, err = indexOb.GetItemById(nextItemId)
- if err != nil {
- errMsg = "移动失败"
- err = errors.New("获取下一个兄弟节点分类信息失败,Err:" + err.Error())
- return
- }
- nextSort = nextEdbInfo.Sort
- }
- err, errMsg = moveAiPredictModelClassify(parentClassify, edbClassifyInfo, prevClassify, nextClassify, edbInfo, prevEdbInfo, nextEdbInfo, parentClassifyId, prevSort, nextSort)
- return
- }
- // moveAiPredictModelClassify 移动指标分类
- func moveAiPredictModelClassify(parentClassify, edbClassifyInfo, prevClassify, nextClassify *aiPredictModel.AiPredictModelClassify, edbInfo, prevEdbInfo, nextEdbInfo *aiPredictModel.AiPredictModelIndex, parentClassifyId int, prevSort, nextSort int) (err error, errMsg string) {
- updateCol := make([]string, 0)
- // 移动对象为分类, 判断分类是否存在
- if edbClassifyInfo != nil {
- oldParentId := edbClassifyInfo.ParentId
- oldLevel := edbClassifyInfo.Level
- var classifyIds []int
- if oldParentId != parentClassifyId {
- //更新子分类对应的level
- childList, e, m := GetAiPredictModelChildClassifyByClassifyId(edbClassifyInfo.AiPredictModelClassifyId)
- if e != nil {
- errMsg = "移动失败"
- err = errors.New("查询子分类失败,Err:" + e.Error() + m)
- return
- }
- if len(childList) > 0 {
- for _, v := range childList {
- if v.AiPredictModelClassifyId == edbClassifyInfo.AiPredictModelClassifyId {
- continue
- }
- classifyIds = append(classifyIds, v.AiPredictModelClassifyId)
- }
- }
- }
- //判断上级id是否一致,如果不一致的话,那么需要移动该分类层级
- if edbClassifyInfo.ParentId != parentClassifyId && parentClassifyId != 0 {
- if edbClassifyInfo.Level != parentClassify.Level+1 { //禁止层级调整
- errMsg = "移动失败"
- err = errors.New("不支持目录层级变更")
- return
- }
- edbClassifyInfo.ParentId = parentClassify.AiPredictModelClassifyId
- edbClassifyInfo.RootId = parentClassify.RootId
- edbClassifyInfo.Level = parentClassify.Level + 1
- edbClassifyInfo.ModifyTime = time.Now()
- // 更改层级路径
- edbClassifyInfo.LevelPath = fmt.Sprintf("%s,%d", parentClassify.LevelPath, edbClassifyInfo.AiPredictModelClassifyId)
- updateCol = append(updateCol, "ParentId", "RootId", "Level", "ModifyTime", "LevelPath")
- } else if edbClassifyInfo.ParentId != parentClassifyId && parentClassifyId == 0 {
- errMsg = "移动失败"
- err = errors.New("不支持目录层级变更")
- return
- }
- if prevSort > 0 {
- //如果是移动在两个兄弟节点之间
- if nextSort > 0 {
- //下一个兄弟节点
- //如果上一个兄弟与下一个兄弟的排序权重是一致的,那么需要将下一个兄弟(以及下个兄弟的同样排序权重)的排序权重+2,自己变成上一个兄弟的排序权重+1
- if prevSort == nextSort || prevSort == edbClassifyInfo.Sort {
- //变更兄弟节点的排序
- updateSortStr := `sort + 2`
- //变更分类
- if prevClassify != nil {
- _ = aiPredictModel.UpdateAiPredictModelClassifySortByParentId(parentClassifyId, prevClassify.AiPredictModelClassifyId, prevClassify.Sort, updateSortStr)
- } else {
- _ = aiPredictModel.UpdateAiPredictModelClassifySortByParentId(parentClassifyId, 0, prevSort, updateSortStr)
- }
- //变更指标
- if prevEdbInfo != nil {
- //变更兄弟节点的排序
- _ = aiPredictModel.UpdateAiPredictModelIndexSortByClassifyId(parentClassifyId, prevSort, prevEdbInfo.AiPredictModelIndexId, updateSortStr)
- } else {
- _ = aiPredictModel.UpdateAiPredictModelIndexSortByClassifyId(parentClassifyId, prevSort, 0, updateSortStr)
- }
- } else {
- //如果下一个兄弟的排序权重正好是上个兄弟节点的下一层,那么需要再加一层了
- if nextSort-prevSort == 1 {
- //变更兄弟节点的排序
- updateSortStr := `sort + 1`
- //变更分类
- if prevClassify != nil {
- _ = aiPredictModel.UpdateAiPredictModelClassifySortByParentId(parentClassifyId, prevClassify.AiPredictModelClassifyId, prevSort, updateSortStr)
- } else {
- _ = aiPredictModel.UpdateAiPredictModelClassifySortByParentId(parentClassifyId, 0, prevSort, updateSortStr)
- }
- //变更指标
- if prevEdbInfo != nil {
- //变更兄弟节点的排序
- _ = aiPredictModel.UpdateAiPredictModelIndexSortByClassifyId(parentClassifyId, prevSort, prevEdbInfo.AiPredictModelIndexId, updateSortStr)
- } else {
- _ = aiPredictModel.UpdateAiPredictModelIndexSortByClassifyId(parentClassifyId, prevSort, 0, updateSortStr)
- }
- }
- }
- }
- edbClassifyInfo.Sort = prevSort + 1
- edbClassifyInfo.ModifyTime = time.Now()
- updateCol = append(updateCol, "Sort", "ModifyTime")
- } else if prevClassify == nil && nextClassify == nil && prevEdbInfo == nil && nextEdbInfo == nil && parentClassifyId > 0 {
- //处理只拖动到目录里,默认放到目录底部的情况
- var maxSort int
- maxSort, err = GetAiPredictModelClassifyMaxSort(parentClassifyId)
- if err != nil {
- errMsg = "移动失败"
- err = errors.New("查询组内排序信息失败,Err:" + err.Error())
- return
- }
- edbClassifyInfo.Sort = maxSort + 1 //那就是排在组内最后一位
- edbClassifyInfo.ModifyTime = time.Now()
- updateCol = append(updateCol, "Sort", "ModifyTime")
- } else {
- // 拖动到父级分类的第一位
- firstClassify, tmpErr := aiPredictModel.GetFirstAiPredictModelClassifyByParentId(parentClassifyId)
- if tmpErr != nil && tmpErr.Error() != utils.ErrNoRow() {
- errMsg = "移动失败"
- err = errors.New("获取获取当前父级分类下的排序第一条的分类信息失败,Err:" + tmpErr.Error())
- return
- }
- //如果该分类下存在其他分类,且第一个其他分类的排序等于0,那么需要调整排序
- if firstClassify != nil && firstClassify.Sort == 0 {
- updateSortStr := ` sort + 1 `
- _ = aiPredictModel.UpdateAiPredictModelClassifySortByParentId(parentClassifyId, firstClassify.AiPredictModelClassifyId-1, 0, updateSortStr)
- //该分类下的所有指标也需要+1
- _ = aiPredictModel.UpdateAiPredictModelIndexSortByClassifyId(parentClassifyId, 0, 0, updateSortStr)
- } else {
- //如果该分类下存在指标,且第一个指标的排序等于0,那么需要调整排序
- firstEdb, tErr := aiPredictModel.GetFirstAiPredictModelIndexByClassifyId(parentClassifyId)
- if tErr != nil && tErr.Error() != utils.ErrNoRow() {
- errMsg = "移动失败"
- err = errors.New("获取获取当前父级分类下的排序第一条的分类信息失败,Err:" + tErr.Error())
- return
- }
- //如果该分类下存在其他分类,且第一个其他分类的排序等于0,那么需要调整排序
- if firstEdb != nil && firstEdb.Sort == 0 {
- updateSortStr := ` sort + 1 `
- _ = aiPredictModel.UpdateAiPredictModelIndexSortByClassifyId(parentClassifyId, 0, firstEdb.AiPredictModelIndexId-1, updateSortStr)
- _ = aiPredictModel.UpdateAiPredictModelClassifySortByParentId(parentClassifyId, 0, 0, updateSortStr)
- }
- }
- edbClassifyInfo.Sort = 0 //那就是排在第一位
- edbClassifyInfo.ModifyTime = time.Now()
- updateCol = append(updateCol, "Sort", "ModifyTime")
- }
- //更新
- if len(updateCol) > 0 {
- err = edbClassifyInfo.Update(updateCol)
- if err != nil {
- errMsg = "移动失败"
- err = errors.New("修改失败,Err:" + err.Error())
- return
- }
- //更新对应分类的root_id和层级
- if oldParentId != parentClassifyId {
- if len(classifyIds) > 0 {
- levelStep := edbClassifyInfo.Level - oldLevel
- err = aiPredictModel.UpdateAiPredictModelClassifyChildByParentClassifyId(classifyIds, edbClassifyInfo.RootId, levelStep)
- if err != nil {
- errMsg = "移动失败"
- err = errors.New("更新子分类失败,Err:" + err.Error())
- return
- }
- }
- }
- }
- } else {
- if edbInfo == nil {
- errMsg = "当前指标不存在"
- err = errors.New(errMsg)
- return
- }
- //如果改变了分类,那么移动该指标数据
- if edbInfo.ClassifyId != parentClassifyId {
- edbInfo.ClassifyId = parentClassifyId
- edbInfo.ModifyTime = time.Now()
- updateCol = append(updateCol, "ClassifyId", "ModifyTime")
- }
- if prevSort > 0 {
- //如果是移动在两个兄弟节点之间
- if nextSort > 0 {
- //下一个兄弟节点
- //如果上一个兄弟与下一个兄弟的排序权重是一致的,那么需要将下一个兄弟(以及下个兄弟的同样排序权重)的排序权重+2,自己变成上一个兄弟的排序权重+1
- if prevSort == nextSort || prevSort == edbInfo.Sort {
- //变更兄弟节点的排序
- updateSortStr := `sort + 2`
- //变更分类
- if prevClassify != nil {
- _ = aiPredictModel.UpdateAiPredictModelClassifySortByParentId(parentClassifyId, prevClassify.AiPredictModelClassifyId, prevClassify.Sort, updateSortStr)
- } else {
- _ = aiPredictModel.UpdateAiPredictModelClassifySortByParentId(parentClassifyId, 0, prevSort, updateSortStr)
- }
- //变更指标
- if prevEdbInfo != nil {
- //变更兄弟节点的排序
- _ = aiPredictModel.UpdateAiPredictModelIndexSortByClassifyId(parentClassifyId, prevSort, prevEdbInfo.AiPredictModelIndexId, updateSortStr)
- } else {
- _ = aiPredictModel.UpdateAiPredictModelIndexSortByClassifyId(parentClassifyId, prevSort, 0, updateSortStr)
- }
- } else {
- //如果下一个兄弟的排序权重正好是上个兄弟节点的下一层,那么需要再加一层了
- if nextSort-prevSort == 1 {
- //变更兄弟节点的排序
- updateSortStr := `sort + 1`
- //变更分类
- if prevClassify != nil {
- _ = aiPredictModel.UpdateAiPredictModelClassifySortByParentId(parentClassifyId, prevClassify.AiPredictModelClassifyId, prevSort, updateSortStr)
- } else {
- _ = aiPredictModel.UpdateAiPredictModelClassifySortByParentId(parentClassifyId, 0, prevSort, updateSortStr)
- }
- //变更指标
- if prevEdbInfo != nil {
- //变更兄弟节点的排序
- _ = aiPredictModel.UpdateAiPredictModelIndexSortByClassifyId(parentClassifyId, prevSort, prevEdbInfo.AiPredictModelIndexId, updateSortStr)
- } else {
- _ = aiPredictModel.UpdateAiPredictModelIndexSortByClassifyId(parentClassifyId, prevSort, 0, updateSortStr)
- }
- }
- }
- }
- edbInfo.Sort = prevSort + 1
- edbInfo.ModifyTime = time.Now()
- updateCol = append(updateCol, "Sort", "ModifyTime")
- } else if prevClassify == nil && nextClassify == nil && prevEdbInfo == nil && nextEdbInfo == nil && parentClassifyId > 0 {
- //处理只拖动到目录里,默认放到目录底部的情况
- var maxSort int
- classifyOb := new(aiPredictModel.AiPredictModelClassify)
- maxSort, err = classifyOb.GetSortMax(parentClassifyId)
- if err != nil {
- errMsg = "移动失败"
- err = errors.New("查询组内排序信息失败,Err:" + err.Error())
- return
- }
- edbInfo.Sort = maxSort + 1 //那就是排在组内最后一位
- edbInfo.ModifyTime = time.Now()
- updateCol = append(updateCol, "Sort", "ModifyTime")
- } else {
- // 拖动到父级分类的第一位
- firstClassify, tmpErr := aiPredictModel.GetFirstAiPredictModelClassifyByParentId(parentClassifyId)
- if tmpErr != nil && tmpErr.Error() != utils.ErrNoRow() {
- errMsg = "移动失败"
- err = errors.New("获取获取当前父级分类下的排序第一条的分类信息失败,Err:" + tmpErr.Error())
- return
- }
- //如果该分类下存在其他分类,且第一个其他分类的排序等于0,那么需要调整排序
- if firstClassify != nil && firstClassify.Sort == 0 {
- updateSortStr := ` sort + 1 `
- _ = aiPredictModel.UpdateAiPredictModelClassifySortByParentId(parentClassifyId, firstClassify.AiPredictModelClassifyId-1, 0, updateSortStr)
- //该分类下的所有指标也需要+1
- _ = aiPredictModel.UpdateAiPredictModelIndexSortByClassifyId(parentClassifyId, 0, 0, updateSortStr)
- } else {
- //如果该分类下存在指标,且第一个指标的排序等于0,那么需要调整排序
- firstEdb, tErr := aiPredictModel.GetFirstAiPredictModelIndexByClassifyId(parentClassifyId)
- if tErr != nil && tErr.Error() != utils.ErrNoRow() {
- errMsg = "移动失败"
- err = errors.New("获取获取当前父级分类下的排序第一条的分类信息失败,Err:" + tErr.Error())
- return
- }
- //如果该分类下存在其他分类,且第一个其他分类的排序等于0,那么需要调整排序
- if firstEdb != nil && firstEdb.Sort == 0 {
- updateSortStr := ` sort + 1 `
- _ = aiPredictModel.UpdateAiPredictModelIndexSortByClassifyId(parentClassifyId, 0, firstEdb.AiPredictModelIndexId-1, updateSortStr)
- _ = aiPredictModel.UpdateAiPredictModelClassifySortByParentId(parentClassifyId, 0, 0, updateSortStr)
- }
- }
- edbInfo.Sort = 0 //那就是排在第一位
- edbInfo.ModifyTime = time.Now()
- updateCol = append(updateCol, "Sort", "ModifyTime")
- }
- //更新
- if len(updateCol) > 0 {
- err = edbInfo.Update(updateCol)
- if err != nil {
- errMsg = "移动失败"
- err = errors.New("修改失败,Err:" + err.Error())
- return
- }
- }
- }
- return
- }
- func GetAiPredictModelChildClassifyByClassifyId(targetClassifyId int) (targetList []*aiPredictModel.AiPredictModelClassify, err error, errMsg string) {
- classifyOb := new(aiPredictModel.AiPredictModelClassify)
- //判断是否是挂在顶级目录下
- targetClassify, err := classifyOb.GetItemById(targetClassifyId)
- if err != nil {
- if err.Error() == utils.ErrNoRow() {
- errMsg = "当前分类不存在"
- err = errors.New(errMsg)
- return
- }
- errMsg = "获取失败"
- err = errors.New("获取分类信息失败,Err:" + err.Error())
- return
- }
- orderStr := ` order by level asc, sort asc, ai_predict_model_classify_id asc`
- tmpList, err := aiPredictModel.GetAiPredictModelClassifyByRootIdLevel(targetClassify.RootId, orderStr)
- if err != nil && err.Error() != utils.ErrNoRow() {
- errMsg = "获取失败"
- err = errors.New("获取数据失败,Err:" + err.Error())
- return
- }
- idMap := make(map[int]struct{})
- if len(tmpList) > 0 {
- for _, v := range tmpList {
- if v.AiPredictModelClassifyId == targetClassify.AiPredictModelClassifyId {
- idMap[v.AiPredictModelClassifyId] = struct{}{}
- }
- }
- for _, v := range tmpList {
- if _, ok := idMap[v.ParentId]; ok {
- idMap[v.AiPredictModelClassifyId] = struct{}{}
- }
- }
- for _, v := range tmpList {
- if _, ok := idMap[v.AiPredictModelClassifyId]; ok {
- targetItem := new(aiPredictModel.AiPredictModelClassify)
- targetItem.AiPredictModelClassifyId = v.AiPredictModelClassifyId
- targetItem.ParentId = v.ParentId
- targetItem.RootId = v.RootId
- //targetItem.UniqueCode = v.UniqueCode
- targetItem.Level = v.Level
- targetItem.ClassifyName = v.ClassifyName
- //targetItem.IsJoinPermission = v.IsJoinPermission
- targetList = append(targetList, targetItem)
- }
- }
- }
- return
- }
- func GetAiPredictModelClassifyMaxSort(parentId int) (maxSort int, err error) {
- //获取该层级下最大的排序数
- classifyOb := new(aiPredictModel.AiPredictModelClassify)
- classifyMaxSort, err := classifyOb.GetSortMax(parentId)
- if err != nil {
- return
- }
- maxSort = classifyMaxSort
- edbMaxSort, err := data_manage.GetThsHfIndexMaxSortByClassifyId(parentId)
- if err != nil {
- return
- }
- if maxSort < edbMaxSort {
- maxSort = edbMaxSort
- }
- return
- }
|