package data import ( "errors" "eta_gn/eta_api/models/data_manage" "eta_gn/eta_api/models/system" "eta_gn/eta_api/utils" "fmt" "time" ) // GetThsHfClassifyTreeRecursive 递归获取分类树形结构 func GetThsHfClassifyTreeRecursive(list []*data_manage.BaseFromThsHfClassifyItem, parentId int) []*data_manage.BaseFromThsHfClassifyItem { res := make([]*data_manage.BaseFromThsHfClassifyItem, 0) for _, v := range list { if v.ParentId == parentId { t := GetThsHfClassifyTreeRecursive(list, v.ClassifyId) v.Children = nil // 这一步是方便前端组件判断null... if len(t) > 0 { v.Children = t } res = append(res, v) } } return res } // ThsHfMoveClassify 移动指标分类 func ThsHfMoveClassify(req data_manage.BaseFromThsHfClassifyMoveReq, sysUser *system.Admin) (err error, errMsg string) { // req.ClassifyId, req.ParentClassifyId, req.PrevClassifyId, req.NextClassifyId classifyId := req.ClassifyId parentClassifyId := req.ParentClassifyId prevClassifyId := req.PrevClassifyId nextClassifyId := req.NextClassifyId itemId := req.ItemId prevItemId := req.PrevItemId nextItemId := req.NextItemId //首先确定移动的对象是分类还是指标 //判断上一个节点是分类还是指标 //判断下一个节点是分类还是指标 //同时更新分类目录下的分类sort和指标sort //更新当前移动的分类或者指标sort var parentEdbClassifyInfo *data_manage.BaseFromThsHfClassify if parentClassifyId > 0 { parentEdbClassifyInfo, err = data_manage.GetThsHfClassifyById(parentClassifyId) if err != nil { errMsg = "移动失败" err = errors.New("获取上级分类信息失败,Err:" + err.Error()) return } } //如果有传入 上一个兄弟节点分类id var ( edbClassifyInfo *data_manage.BaseFromThsHfClassify prevClassify *data_manage.BaseFromThsHfClassify nextClassify *data_manage.BaseFromThsHfClassify edbInfo *data_manage.BaseFromThsHfIndex prevEdbInfo *data_manage.BaseFromThsHfIndex nextEdbInfo *data_manage.BaseFromThsHfIndex prevSort int nextSort int ) // 移动对象为分类, 判断权限 if itemId == 0 { edbClassifyInfo, err = data_manage.GetThsHfClassifyById(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 && parentEdbClassifyInfo.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 = data_manage.GetThsHfIndexById(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 = data_manage.GetThsHfClassifyById(prevClassifyId) if err != nil { errMsg = "移动失败" err = errors.New("获取上一个兄弟节点分类信息失败,Err:" + err.Error()) return } prevSort = prevClassify.Sort } else if prevItemId > 0 { prevEdbInfo, err = data_manage.GetThsHfIndexById(prevItemId) if err != nil { errMsg = "移动失败" err = errors.New("获取上一个兄弟节点分类信息失败,Err:" + err.Error()) return } prevSort = prevEdbInfo.Sort } if nextClassifyId > 0 { //下一个兄弟节点 nextClassify, err = data_manage.GetThsHfClassifyById(nextClassifyId) if err != nil { errMsg = "移动失败" err = errors.New("获取下一个兄弟节点分类信息失败,Err:" + err.Error()) return } nextSort = nextClassify.Sort } else if nextItemId > 0 { //下一个兄弟节点 nextEdbInfo, err = data_manage.GetThsHfIndexById(nextItemId) if err != nil { errMsg = "移动失败" err = errors.New("获取下一个兄弟节点分类信息失败,Err:" + err.Error()) return } nextSort = nextEdbInfo.Sort } err, errMsg = thsHfMoveEdbClassify(parentEdbClassifyInfo, edbClassifyInfo, prevClassify, nextClassify, edbInfo, prevEdbInfo, nextEdbInfo, parentClassifyId, prevSort, nextSort) return } // thsHfMoveEdbClassify 移动指标分类 func thsHfMoveEdbClassify(parentEdbClassifyInfo, edbClassifyInfo, prevClassify, nextClassify *data_manage.BaseFromThsHfClassify, edbInfo, prevEdbInfo, nextEdbInfo *data_manage.BaseFromThsHfIndex, 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 := GetThsHfChildClassifyByClassifyId(edbClassifyInfo.BaseFromThsHfClassifyId) if e != nil { errMsg = "移动失败" err = errors.New("查询子分类失败,Err:" + e.Error() + m) return } if len(childList) > 0 { for _, v := range childList { if v.BaseFromThsHfClassifyId == edbClassifyInfo.BaseFromThsHfClassifyId { continue } classifyIds = append(classifyIds, v.BaseFromThsHfClassifyId) } } } //判断上级id是否一致,如果不一致的话,那么需要移动该分类层级 if edbClassifyInfo.ParentId != parentClassifyId && parentClassifyId != 0 { if edbClassifyInfo.Level != parentEdbClassifyInfo.Level+1 { //禁止层级调整 errMsg = "移动失败" err = errors.New("不支持目录层级变更") return } edbClassifyInfo.ParentId = parentEdbClassifyInfo.BaseFromThsHfClassifyId edbClassifyInfo.RootId = parentEdbClassifyInfo.RootId edbClassifyInfo.Level = parentEdbClassifyInfo.Level + 1 edbClassifyInfo.ModifyTime = time.Now() // 更改层级路径 edbClassifyInfo.LevelPath = fmt.Sprintf("%s,%d", parentEdbClassifyInfo.LevelPath, edbClassifyInfo.BaseFromThsHfClassifyId) 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 { _ = data_manage.UpdateThsHfClassifySortByParentId(parentClassifyId, prevClassify.BaseFromThsHfClassifyId, prevClassify.Sort, updateSortStr) } else { _ = data_manage.UpdateThsHfClassifySortByParentId(parentClassifyId, 0, prevSort, updateSortStr) } //变更指标 if prevEdbInfo != nil { //变更兄弟节点的排序 _ = data_manage.UpdateThsHfIndexSortByClassifyId(parentClassifyId, prevSort, prevEdbInfo.BaseFromThsHfIndexId, updateSortStr) } else { _ = data_manage.UpdateThsHfIndexSortByClassifyId(parentClassifyId, prevSort, 0, updateSortStr) } } else { //如果下一个兄弟的排序权重正好是上个兄弟节点的下一层,那么需要再加一层了 if nextSort-prevSort == 1 { //变更兄弟节点的排序 updateSortStr := `sort + 1` //变更分类 if prevClassify != nil { _ = data_manage.UpdateThsHfClassifySortByParentId(parentClassifyId, prevClassify.BaseFromThsHfClassifyId, prevSort, updateSortStr) } else { _ = data_manage.UpdateThsHfClassifySortByParentId(parentClassifyId, 0, prevSort, updateSortStr) } //变更指标 if prevEdbInfo != nil { //变更兄弟节点的排序 _ = data_manage.UpdateThsHfIndexSortByClassifyId(parentClassifyId, prevSort, prevEdbInfo.BaseFromThsHfIndexId, updateSortStr) } else { _ = data_manage.UpdateThsHfIndexSortByClassifyId(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 = GetThsHfClassifyMaxSort(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 := data_manage.GetFirstThsHfClassifyByParentId(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 ` _ = data_manage.UpdateThsHfClassifySortByParentId(parentClassifyId, firstClassify.BaseFromThsHfClassifyId-1, 0, updateSortStr) //该分类下的所有指标也需要+1 _ = data_manage.UpdateThsHfIndexSortByClassifyId(parentClassifyId, 0, 0, updateSortStr) } else { //如果该分类下存在指标,且第一个指标的排序等于0,那么需要调整排序 firstEdb, tErr := data_manage.GetFirstThsHfIndexByClassifyId(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 ` _ = data_manage.UpdateThsHfIndexSortByClassifyId(parentClassifyId, 0, firstEdb.BaseFromThsHfIndexId-1, updateSortStr) _ = data_manage.UpdateThsHfClassifySortByParentId(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 = data_manage.UpdateThsHfClassifyChildByParentClassifyId(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.BaseFromThsHfClassifyId != parentClassifyId { edbInfo.BaseFromThsHfClassifyId = parentClassifyId edbInfo.ModifyTime = time.Now() updateCol = append(updateCol, "BaseFromThsHfClassifyId", "ModifyTime") } if prevSort > 0 { //如果是移动在两个兄弟节点之间 if nextSort > 0 { //下一个兄弟节点 //如果上一个兄弟与下一个兄弟的排序权重是一致的,那么需要将下一个兄弟(以及下个兄弟的同样排序权重)的排序权重+2,自己变成上一个兄弟的排序权重+1 if prevSort == nextSort || prevSort == edbInfo.Sort { //变更兄弟节点的排序 updateSortStr := `sort + 2` //变更分类 if prevClassify != nil { _ = data_manage.UpdateThsHfClassifySortByParentId(parentClassifyId, prevClassify.BaseFromThsHfClassifyId, prevClassify.Sort, updateSortStr) } else { _ = data_manage.UpdateThsHfClassifySortByParentId(parentClassifyId, 0, prevSort, updateSortStr) } //变更指标 if prevEdbInfo != nil { //变更兄弟节点的排序 _ = data_manage.UpdateThsHfIndexSortByClassifyId(parentClassifyId, prevSort, prevEdbInfo.BaseFromThsHfIndexId, updateSortStr) } else { _ = data_manage.UpdateThsHfIndexSortByClassifyId(parentClassifyId, prevSort, 0, updateSortStr) } } else { //如果下一个兄弟的排序权重正好是上个兄弟节点的下一层,那么需要再加一层了 if nextSort-prevSort == 1 { //变更兄弟节点的排序 updateSortStr := `sort + 1` //变更分类 if prevClassify != nil { _ = data_manage.UpdateThsHfClassifySortByParentId(parentClassifyId, prevClassify.BaseFromThsHfClassifyId, prevSort, updateSortStr) } else { _ = data_manage.UpdateThsHfClassifySortByParentId(parentClassifyId, 0, prevSort, updateSortStr) } //变更指标 if prevEdbInfo != nil { //变更兄弟节点的排序 _ = data_manage.UpdateThsHfIndexSortByClassifyId(parentClassifyId, prevSort, prevEdbInfo.BaseFromThsHfIndexId, updateSortStr) } else { _ = data_manage.UpdateThsHfIndexSortByClassifyId(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(data_manage.BaseFromThsHfClassify) 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 := data_manage.GetFirstThsHfClassifyByParentId(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 ` _ = data_manage.UpdateThsHfClassifySortByParentId(parentClassifyId, firstClassify.BaseFromThsHfClassifyId-1, 0, updateSortStr) //该分类下的所有指标也需要+1 _ = data_manage.UpdateThsHfIndexSortByClassifyId(parentClassifyId, 0, 0, updateSortStr) } else { //如果该分类下存在指标,且第一个指标的排序等于0,那么需要调整排序 firstEdb, tErr := data_manage.GetFirstThsHfIndexByClassifyId(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 ` _ = data_manage.UpdateThsHfIndexSortByClassifyId(parentClassifyId, 0, firstEdb.BaseFromThsHfIndexId-1, updateSortStr) _ = data_manage.UpdateThsHfClassifySortByParentId(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 GetThsHfChildClassifyByClassifyId(targetClassifyId int) (targetList []*data_manage.BaseFromThsHfClassify, err error, errMsg string) { //判断是否是挂在顶级目录下 targetClassify, err := data_manage.GetThsHfClassifyById(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, base_from_ths_hf_classify_id asc` tmpList, err := data_manage.GetThsHfClassifyByRootIdLevel(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.BaseFromThsHfClassifyId == targetClassify.BaseFromThsHfClassifyId { idMap[v.BaseFromThsHfClassifyId] = struct{}{} } } for _, v := range tmpList { if _, ok := idMap[v.ParentId]; ok { idMap[v.BaseFromThsHfClassifyId] = struct{}{} } } for _, v := range tmpList { if _, ok := idMap[v.BaseFromThsHfClassifyId]; ok { targetItem := new(data_manage.BaseFromThsHfClassify) targetItem.BaseFromThsHfClassifyId = v.BaseFromThsHfClassifyId 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 GetThsHfClassifyMaxSort(parentId int) (maxSort int, err error) { //获取该层级下最大的排序数 classifyOb := new(data_manage.BaseFromThsHfClassify) 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 }