package services import ( "errors" "eta/eta_forum_admin/models" "eta/eta_forum_admin/models/system" "eta/eta_forum_admin/utils" "fmt" "strconv" "strings" "time" ) func ChartClassifyItemsMakeTree(sysUser *system.Admin, allNode []*models.ChartClassifyItems, node *models.ChartClassifyItems) { // node.HaveOperaAuth = data_manage_permission.CheckEdbClassifyPermissionByPermissionIdList(node.IsJoinPermission, node.ChartClassifyId, permissionClassifyIdList) button := GetChartClassifyOpButton(sysUser, node.SysUserId, node.HaveOperaAuth) node.Button = button childs, _ := chartClassifyHaveChild(allNode, node) //判断节点是否有子节点并返回 if len(childs) > 0 { for _, child := range childs { //child.HaveOperaAuth = data_manage_permission.CheckEdbClassifyPermissionByPermissionIdList(child.IsJoinPermission, child.ChartClassifyId, permissionClassifyIdList) childButton := GetChartClassifyOpButton(sysUser, child.SysUserId, child.HaveOperaAuth) if child.Level == 3 { childButton.AddButton = false //第三级的话,默认图表都是没有添加按钮的 } child.Button = childButton //node.Children = append(node.Children, child) } node.Children = append(node.Children, childs[0:]...) //添加子节点 for _, v := range childs { //v.HaveOperaAuth = data_manage_permission.CheckEdbClassifyPermissionByPermissionIdList(v.IsJoinPermission, v.ChartClassifyId, permissionClassifyIdList) //查询子节点的子节点,并添加到子节点 _, has := chartClassifyHaveChild(allNode, v) if has { ChartClassifyItemsMakeTree(sysUser, allNode, v) //递归添加节点 } else { childrenArr := make([]*models.ChartClassifyItems, 0) v.Children = childrenArr } } } else { childrenArr := make([]*models.ChartClassifyItems, 0) node.Children = childrenArr } } func chartClassifyHaveChild(allNode []*models.ChartClassifyItems, node *models.ChartClassifyItems) (childs []*models.ChartClassifyItems, yes bool) { for _, v := range allNode { if v.ParentId == node.ChartClassifyId { childs = append(childs, v) } } if len(childs) > 0 { yes = true } return } func FixChartClassify() { //新增三级分类 list, err := models.GetChartClassifyByLevel(2, 1) if err != nil { return } for _, v := range list { classify := new(models.ChartClassify) classify.ParentId = v.ChartClassifyId classify.ChartClassifyName = v.ChartClassifyName classify.HasData = 0 classify.CreateTime = time.Now() classify.ModifyTime = time.Now() if utils.RunMode == "debug" { classify.SysUserId = 11 } else { classify.SysUserId = 163 } classify.SysUserRealName = "admin" classify.Level = 3 timestamp := strconv.FormatInt(time.Now().UnixNano(), 10) classify.UniqueCode = utils.MD5(utils.DATA_PREFIX + "_" + timestamp) classify.Sort = v.Sort classify.Source = utils.CHART_SOURCE_DEFAULT id, err := models.AddChartClassify(classify) if err != nil { return } err = models.FIXChartClassifyId(int(id), v.ChartClassifyId) if err != nil { return } } } func FixChartClassifySysUserId() { //获取所有父级分类信息 list, err := models.GetChartClassifyByLevel(2, 1) if err != nil { return } for _, v := range list { err := models.EditChartClassifySysUser(v.ChartClassifyId, v.SysUserId, v.SysUserRealName) if err != nil { return } fmt.Println("ChartClassifyId:", v.ChartClassifyId) } fmt.Println("FixChartClassifySysUserId end") } // GetChartClassifyOpButton 获取ETA图库分类的操作权限 func GetChartClassifyOpButton(sysUser *system.Admin, belongUserId int, haveOperaAuth bool) (button models.ChartClassifyItemsButton) { // 没有数据权限就直接返回 if !haveOperaAuth { return } //ficc管理员和超管和ficc研究员有权限创建和管理分类,可以编辑分类名称(分类名称不允许重复),可以拖动分类,改变分类顺序,可以拖动分类下模型,改变顺序,可以删除分类,若分类下有预测指标,则不允许删除; //if utils.InArrayByStr([]string{utils.ROLE_TYPE_CODE_ADMIN, utils.ROLE_TYPE_CODE_FICC_ADMIN, utils.ROLE_TYPE_CODE_RESEARCHR, utils.ROLE_TYPE_CODE_FICC_RESEARCHR}, sysUser.RoleTypeCode) { button.AddButton = true button.OpButton = true button.DeleteButton = true button.MoveButton = true //} return } // GetCanOpChartClassify 获取是否允许添加图表分类 func GetCanOpChartClassify(roleTypeCode string) (canOpClassify bool) { // 如果不是 超管 或者 ficc管理员 或者 ficc研究员,那么就没有权限 if utils.InArrayByStr([]string{utils.ROLE_TYPE_CODE_ADMIN, utils.ROLE_TYPE_CODE_FICC_ADMIN, utils.ROLE_TYPE_CODE_RESEARCHR, utils.ROLE_TYPE_CODE_FICC_RESEARCHR}, roleTypeCode) { canOpClassify = true } return } // GetChartOpButton 获取ETA图库的操作权限 func GetChartOpButton(sysUser *system.Admin, belongUserId int, haveOperaAuth bool) (button models.ChartClassifyItemsButton) { // 数据权限判断 if !haveOperaAuth { return } //预测指标的添加人对该预测指标有全部操作权限,ficc管理员、超管对所有预测指标有全部操作权限; if sysUser.RoleTypeCode == utils.ROLE_TYPE_CODE_ADMIN || sysUser.RoleTypeCode == utils.ROLE_TYPE_CODE_FICC_ADMIN || sysUser.AdminId == belongUserId || sysUser.PredictEdbPermission == 1 { button.AddButton = true button.OpButton = true button.DeleteButton = true button.MoveButton = true } return } // AddChartClassify // @Description: 添加图表分类 // @author: Roc // @datetime 2024-04-17 14:48:23 // @param chartClassifyName string // @param parentId int // @param level int // @param source int // @param lang string // @param sysUser *system.Admin // @return classifyInfo *models.ChartClassify // @return err error // @return errMsg string // @return isSendEmail bool func AddChartClassify(chartClassifyName string, parentId, source int, lang string, sysUser *system.Admin) (classifyInfo *models.ChartClassify, err error, errMsg string, isSendEmail bool) { isSendEmail = true errMsg = "保存分类失败" // 校验分类名称相同的数量 { var count int switch lang { case utils.EnLangVersion: count, err = models.GetChartClassifyEnCount(chartClassifyName, parentId, source) default: count, err = models.GetChartClassifyCount(chartClassifyName, parentId, source) } if err != nil { errMsg = "判断名称是否已存在失败" return } if count > 0 { errMsg = "分类名称已存在,请重新输入" err = errors.New(errMsg) isSendEmail = false return } } // 查询level值 level := 0 levelPath := "" if parentId > 0 { parentClassify, tErr := models.GetChartClassifyById(parentId) if tErr != nil { if tErr.Error() == utils.ErrNoRow() { errMsg = "父级分类不存在" err = errors.New(errMsg) return } errMsg = "获取失败" err = errors.New("获取分类信息失败,Err:" + tErr.Error()) return } level = parentClassify.Level levelPath = parentClassify.LevelPath } //获取该层级下最大的排序数 maxSort, err := models.GetChartClassifyMaxSort(parentId, source) classifyInfo = new(models.ChartClassify) classifyInfo.ParentId = parentId classifyInfo.ChartClassifyName = chartClassifyName classifyInfo.ChartClassifyNameEn = chartClassifyName classifyInfo.HasData = 0 classifyInfo.CreateTime = time.Now() classifyInfo.ModifyTime = time.Now() classifyInfo.SysUserId = sysUser.AdminId classifyInfo.SysUserRealName = sysUser.RealName classifyInfo.Level = level + 1 timestamp := strconv.FormatInt(time.Now().UnixNano(), 10) classifyInfo.UniqueCode = utils.MD5(utils.DATA_PREFIX + "_" + timestamp) classifyInfo.Sort = maxSort + 1 classifyInfo.Source = source classifyId, e := models.AddChartClassify(classifyInfo) if e != nil { errMsg = "保存分类失败" err = errors.New("保存分类失败,Err:" + e.Error()) return } if parentId > 0 { levelPath = fmt.Sprintf("%s%d,", levelPath, classifyId) } else { levelPath = fmt.Sprintf("%d,", classifyId) } classifyInfo.ChartClassifyId = int(classifyId) classifyInfo.LevelPath = levelPath e = classifyInfo.Update([]string{"LevelPath"}) if e != nil { errMsg = "保存分类失败" err = errors.New("保存分类失败,Err:" + e.Error()) return } return } // EditChartClassify // @Description: 修改图表分类 // @author: Roc // @datetime 2024-04-17 14:59:37 // @param chartClassifyId int // @param source int // @param chartClassifyName string // @param lang string // @param sysUser *system.Admin // @return classifyInfo *models.ChartClassify // @return err error // @return errMsg string // @return isSendEmail bool func EditChartClassify(chartClassifyId, source int, chartClassifyName, lang string, sysUser *system.Admin) (classifyInfo *models.ChartClassify, err error, errMsg string, isSendEmail bool) { isSendEmail = true errMsg = "保存失败" // 获取分类信息 classifyInfo, err = models.GetChartClassifyById(chartClassifyId) if err != nil { return } // 分类来源校验 if classifyInfo.Source != source { errMsg = "图表分类异常" err = errors.New(fmt.Sprintf("图表分类来源异常,修改来源:%d,分类来源:%d", source, classifyInfo.Source)) isSendEmail = false return } // 需要变更的字段 updateCols := make([]string, 0) // 语言版本校验 switch lang { case utils.EnLangVersion: if classifyInfo.ChartClassifyNameEn != chartClassifyName { count, tmpErr := models.GetChartClassifyEnCount(chartClassifyName, classifyInfo.ParentId, source) if tmpErr != nil { errMsg = "判断名称是否已存在失败" err = errors.New("判断名称是否已存在失败,Err:" + tmpErr.Error()) return } if count > 0 { errMsg = "分类名称已存在,请重新输入" err = errors.New(errMsg) isSendEmail = false return } classifyInfo.ChartClassifyNameEn = chartClassifyName classifyInfo.ModifyTime = time.Now() updateCols = append(updateCols, "ChartClassifyNameEn", "ModifyTime") } default: if classifyInfo.ChartClassifyName != chartClassifyName { count, tmpErr := models.GetChartClassifyCount(chartClassifyName, classifyInfo.ParentId, source) if tmpErr != nil { errMsg = "判断名称是否已存在失败" err = errors.New("判断名称是否已存在失败,Err:" + tmpErr.Error()) return } if count > 0 { errMsg = "分类名称已存在,请重新输入" err = errors.New(errMsg) isSendEmail = false return } classifyInfo.ChartClassifyName = chartClassifyName classifyInfo.ModifyTime = time.Now() updateCols = append(updateCols, "ChartClassifyName", "ModifyTime") } } if len(updateCols) > 0 { err = classifyInfo.Update(updateCols) } return } // MoveChartClassify 移动图表分类 func MoveChartClassify(req models.MoveChartClassifyReq, sysUser *system.Admin, classifyType int) (err error, errMsg string) { // req.ClassifyId, req.ParentClassifyId, req.PrevClassifyId, req.NextClassifyId classifyId := req.ClassifyId parentClassifyId := req.ParentClassifyId prevClassifyId := req.PrevClassifyId nextClassifyId := req.NextClassifyId chartInfoId := req.ChartInfoId prevChartInfoId := req.PrevChartInfoId nextChartInfoId := req.NextChartInfoId //首先确定移动的对象是分类还是指标 //判断上一个节点是分类还是指标 //判断下一个节点是分类还是指标 //同时更新分类目录下的分类sort和指标sort //更新当前移动的分类或者指标sort var parentEdbClassifyInfo *models.ChartClassify if parentClassifyId > 0 { parentEdbClassifyInfo, err = models.GetChartClassifyById(parentClassifyId) if err != nil { errMsg = "移动失败" err = errors.New("获取上级分类信息失败,Err:" + err.Error()) return } } //如果有传入 上一个兄弟节点分类id var ( edbClassifyInfo *models.ChartClassify prevClassify *models.ChartClassify nextClassify *models.ChartClassify chartInfo *models.ChartInfo prevChartInfo *models.ChartInfo nextChartInfo *models.ChartInfo prevSort int nextSort int ) // 移动对象为分类, 判断权限 if chartInfoId == 0 { edbClassifyInfo, err = models.GetChartClassifyById(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 := models.GetChartClassifyByParentIdAndName(parentClassifyId, edbClassifyInfo.ChartClassifyName, classifyId) if e != nil && e.Error() != utils.ErrNoRow() { errMsg = "移动失败" err = fmt.Errorf("获取父级分类下的同名分类失败, Err: %s", e.Error()) return } if exists != nil { errMsg = "移动失败,分类名称已存在" return } } else { chartInfo, err = models.GetChartInfoById(req.ChartInfoId) 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 } } if prevClassifyId > 0 { prevClassify, err = models.GetChartClassifyById(prevClassifyId) if err != nil { errMsg = "移动失败" err = errors.New("获取上一个兄弟节点分类信息失败,Err:" + err.Error()) return } prevSort = prevClassify.Sort } else if prevChartInfoId > 0 { prevChartInfo, err = models.GetChartInfoById(prevChartInfoId) if err != nil { errMsg = "移动失败" err = errors.New("获取上一个兄弟节点分类信息失败,Err:" + err.Error()) return } prevSort = prevChartInfo.Sort } if nextClassifyId > 0 { //下一个兄弟节点 nextClassify, err = models.GetChartClassifyById(nextClassifyId) if err != nil { errMsg = "移动失败" err = errors.New("获取下一个兄弟节点分类信息失败,Err:" + err.Error()) return } nextSort = nextClassify.Sort } else if nextChartInfoId > 0 { //下一个兄弟节点 nextChartInfo, err = models.GetChartInfoById(nextChartInfoId) if err != nil { errMsg = "移动失败" err = errors.New("获取下一个兄弟节点分类信息失败,Err:" + err.Error()) return } nextSort = nextChartInfo.Sort } err, errMsg = moveChartClassify(parentEdbClassifyInfo, edbClassifyInfo, prevClassify, nextClassify, chartInfo, prevChartInfo, nextChartInfo, parentClassifyId, prevSort, nextSort, classifyType) return } // moveChartClassify 移动图表分类 func moveChartClassify(parentClassifyInfo, edbClassifyInfo, prevClassify, nextClassify *models.ChartClassify, chartInfo, prevChartInfo, nextChartInfo *models.ChartInfo, parentClassifyId int, prevSort, nextSort int, classifyType 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 := GetChildClassifyByClassifyId(edbClassifyInfo.ClassifyId) if e != nil { errMsg = "移动失败" err = errors.New("查询子分类失败,Err:" + e.Error() + m) return } if len(childList) > 0 { for _, v := range childList { if v.ClassifyId == edbClassifyInfo.ClassifyId { continue } classifyIds = append(classifyIds, v.ClassifyId) } }*/ } //判断上级id是否一致,如果不一致的话,那么需要移动该分类层级 if edbClassifyInfo.ParentId != parentClassifyId && parentClassifyId != 0 { if edbClassifyInfo.Level != parentClassifyInfo.Level+1 { //禁止层级调整 errMsg = "移动失败" err = errors.New("不支持目录层级变更") return } edbClassifyInfo.ParentId = parentClassifyInfo.ChartClassifyId edbClassifyInfo.Level = parentClassifyInfo.Level + 1 edbClassifyInfo.ModifyTime = time.Now() updateCol = append(updateCol, "ParentId", "Level", "ModifyTime") } 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 { _ = models.UpdateChartClassifySortByParentId(parentClassifyId, prevClassify.ChartClassifyId, prevClassify.Sort, updateSortStr, classifyType) } else { _ = models.UpdateChartClassifySortByParentId(parentClassifyId, 0, prevSort, updateSortStr, classifyType) } //变更指标 if prevChartInfo != nil { //变更兄弟节点的排序 _ = models.UpdateChartInfoSortByClassifyId(parentClassifyId, prevSort, prevChartInfo.ChartInfoId, []int{classifyType}, updateSortStr) } else { _ = models.UpdateChartInfoSortByClassifyId(parentClassifyId, prevSort, 0, []int{classifyType}, updateSortStr) } } else { //如果下一个兄弟的排序权重正好是上个兄弟节点的下一层,那么需要再加一层了 if nextSort-prevSort == 1 { //变更兄弟节点的排序 updateSortStr := `sort + 1` //变更分类 if prevClassify != nil { _ = models.UpdateChartClassifySortByParentId(parentClassifyId, prevClassify.ChartClassifyId, prevSort, updateSortStr, classifyType) } else { _ = models.UpdateChartClassifySortByParentId(parentClassifyId, 0, prevSort, updateSortStr, classifyType) } //变更指标 if prevChartInfo != nil { //变更兄弟节点的排序 _ = models.UpdateChartInfoSortByClassifyId(parentClassifyId, prevSort, prevChartInfo.ChartInfoId, []int{classifyType}, updateSortStr) } else { _ = models.UpdateChartInfoSortByClassifyId(parentClassifyId, prevSort, 0, []int{classifyType}, updateSortStr) } } } } edbClassifyInfo.Sort = prevSort + 1 edbClassifyInfo.ModifyTime = time.Now() updateCol = append(updateCol, "Sort", "ModifyTime") } else if prevClassify == nil && nextClassify == nil && prevChartInfo == nil && nextChartInfo == nil && parentClassifyId > 0 { //处理只拖动到目录里,默认放到目录底部的情况 var maxSort int maxSort, err = models.GetChartClassifyMaxSort(parentClassifyId, classifyType) 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 := models.GetFirstChartClassifyByParentId(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 ` _ = models.UpdateChartClassifySortByParentId(parentClassifyId, firstClassify.ChartClassifyId-1, 0, updateSortStr, classifyType) //该分类下的所有指标也需要+1 _ = models.UpdateChartInfoSortByClassifyId(parentClassifyId, 0, 0, []int{classifyType}, updateSortStr) } else { //如果该分类下存在指标,且第一个指标的排序等于0,那么需要调整排序 firstEdb, tErr := models.GetFirstChartInfoByClassifyId(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 ` _ = models.UpdateChartInfoSortByClassifyId(parentClassifyId, 0, firstEdb.ChartInfoId-1, []int{classifyType}, updateSortStr) _ = models.UpdateChartClassifySortByParentId(parentClassifyId, 0, 0, updateSortStr, classifyType) } } 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 } // todo 更新对应分类的root_id和层级 /*if oldParentId != parentClassifyId { if len(classifyIds) > 0 { levelStep := edbClassifyInfo.Level - oldLevel err = models.UpdateEdbClassifyChildByParentClassifyId(classifyIds, edbClassifyInfo.RootId, levelStep) if err != nil { errMsg = "移动失败" err = errors.New("更新子分类失败,Err:" + err.Error()) return } } }*/ if oldParentId != parentClassifyId { oldLevelPath := edbClassifyInfo.LevelPath levelPath := fmt.Sprintf("%s%d,", parentClassifyInfo.LevelPath, edbClassifyInfo.ChartClassifyId) edbClassifyInfo.LevelPath = levelPath err = edbClassifyInfo.Update([]string{"LevelPath"}) if err != nil { err = fmt.Errorf("修改失败,Err:" + err.Error()) return } //更新子分类的levelpath tmpList, e := models.GetChartClassifyByLevelPath(oldLevelPath) if e != nil { err = fmt.Errorf("保存分类失败,Err:" + e.Error()) return } // 把原先的父级levePath,替换成最新的父级序列 for _, tmp := range tmpList { //获取字符串前缀的位置 after, _ := strings.CutPrefix(tmp.LevelPath, oldLevelPath) fmt.Println("after", after) // 拼接字符串 if after != "" { tmp.LevelPath = levelPath + after tmp.ModifyTime = time.Now() e = tmp.Update([]string{"LevelPath", "ModifyTime"}) if e != nil { err = fmt.Errorf("修改子分类,Err:" + e.Error()) return } } } } } } else { if chartInfo == nil { errMsg = "当前图表不存在" err = errors.New(errMsg) return } //如果改变了分类,那么移动该指标数据 if chartInfo.ChartClassifyId != parentClassifyId { chartInfo.ChartClassifyId = parentClassifyId chartInfo.ModifyTime = time.Now() updateCol = append(updateCol, "ChartClassifyId", "ModifyTime") } if prevSort > 0 { //如果是移动在两个兄弟节点之间 if nextSort > 0 { //下一个兄弟节点 //如果上一个兄弟与下一个兄弟的排序权重是一致的,那么需要将下一个兄弟(以及下个兄弟的同样排序权重)的排序权重+2,自己变成上一个兄弟的排序权重+1 if prevSort == nextSort || prevSort == chartInfo.Sort { //变更兄弟节点的排序 updateSortStr := `sort + 2` //变更分类 if prevClassify != nil { _ = models.UpdateChartClassifySortByParentId(parentClassifyId, prevClassify.ChartClassifyId, prevClassify.Sort, updateSortStr, classifyType) } else { _ = models.UpdateChartClassifySortByParentId(parentClassifyId, 0, prevSort, updateSortStr, classifyType) } //变更指标 if prevChartInfo != nil { //变更兄弟节点的排序 _ = models.UpdateChartInfoSortByClassifyId(parentClassifyId, prevSort, prevChartInfo.ChartInfoId, []int{classifyType}, updateSortStr) } else { _ = models.UpdateChartInfoSortByClassifyId(parentClassifyId, prevSort, 0, []int{classifyType}, updateSortStr) } } else { //如果下一个兄弟的排序权重正好是上个兄弟节点的下一层,那么需要再加一层了 if nextSort-prevSort == 1 { //变更兄弟节点的排序 updateSortStr := `sort + 1` //变更分类 if prevClassify != nil { _ = models.UpdateChartClassifySortByParentId(parentClassifyId, prevClassify.ChartClassifyId, prevSort, updateSortStr, classifyType) } else { _ = models.UpdateChartClassifySortByParentId(parentClassifyId, 0, prevSort, updateSortStr, classifyType) } //变更指标 if prevChartInfo != nil { //变更兄弟节点的排序 _ = models.UpdateChartInfoSortByClassifyId(parentClassifyId, prevSort, prevChartInfo.ChartInfoId, []int{classifyType}, updateSortStr) } else { _ = models.UpdateChartInfoSortByClassifyId(parentClassifyId, prevSort, 0, []int{classifyType}, updateSortStr) } } } } chartInfo.Sort = prevSort + 1 chartInfo.ModifyTime = time.Now() updateCol = append(updateCol, "Sort", "ModifyTime") } else if prevClassify == nil && nextClassify == nil && prevChartInfo == nil && nextChartInfo == nil && parentClassifyId > 0 { //处理只拖动到目录里,默认放到目录底部的情况 var maxSort int maxSort, err = models.GetChartClassifyMaxSort(parentClassifyId, classifyType) if err != nil { errMsg = "移动失败" err = errors.New("查询组内排序信息失败,Err:" + err.Error()) return } chartInfo.Sort = maxSort + 1 //那就是排在组内最后一位 chartInfo.ModifyTime = time.Now() updateCol = append(updateCol, "Sort", "ModifyTime") } else { // 拖动到父级分类的第一位 firstClassify, tmpErr := models.GetFirstChartClassifyByParentId(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 ` _ = models.UpdateChartClassifySortByParentId(parentClassifyId, firstClassify.ChartClassifyId-1, 0, updateSortStr, classifyType) //该分类下的所有指标也需要+1 _ = models.UpdateChartInfoSortByClassifyId(parentClassifyId, 0, 0, []int{classifyType}, updateSortStr) } else { //如果该分类下存在指标,且第一个指标的排序等于0,那么需要调整排序 firstEdb, tErr := models.GetFirstChartInfoByClassifyId(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 ` _ = models.UpdateChartInfoSortByClassifyId(parentClassifyId, 0, firstEdb.ChartInfoId-1, []int{classifyType}, updateSortStr) _ = models.UpdateChartClassifySortByParentId(parentClassifyId, 0, 0, updateSortStr, classifyType) } } chartInfo.Sort = 0 //那就是排在第一位 chartInfo.ModifyTime = time.Now() updateCol = append(updateCol, "Sort", "ModifyTime") } //更新 if len(updateCol) > 0 { err = chartInfo.Update(updateCol) if err != nil { errMsg = "移动失败" err = errors.New("修改失败,Err:" + err.Error()) return } } } return } // GetChartClassifyListByAdmins 获取某个创建者的图表分类 func GetChartClassifyListByAdmins(adminIdsSlice []int) (hasClassifyMap map[int]bool, err error, errMsg string) { classifyAll, err := models.GetChartClassify() if err != nil && err.Error() != utils.ErrNoRow() { errMsg = "获取失败" return } if len(classifyAll) == 0 { return } allChartInfo, err := models.GetChartClassifyByAdminIdsAndLevel([]int{utils.CHART_SOURCE_DEFAULT}, adminIdsSlice) if err != nil && err.Error() != utils.ErrNoRow() { errMsg = "获取失败" return } if len(allChartInfo) == 0 { return } hasClassifyMap = make(map[int]bool) for _, v := range allChartInfo { hasClassifyMap[v.ChartClassifyId] = true } // 遍历分类,如果存在则返回 for _, v := range classifyAll { if _, ok := hasClassifyMap[v.ChartClassifyId]; ok { hasClassifyMap[v.ParentId] = true } } // 遍历分类,如果存在则返回 for _, v := range classifyAll { if _, ok := hasClassifyMap[v.ChartClassifyId]; ok { hasClassifyMap[v.ParentId] = true } } // 遍历分类,如果存在则返回 for _, v := range classifyAll { if _, ok := hasClassifyMap[v.ChartClassifyId]; ok { hasClassifyMap[v.ParentId] = true } } return } // GetClassifyTreeRecursive 递归获取分类树形结构 func GetClassifyTreeRecursive(list []*models.ChartClassifyItems, parentId int) []*models.ChartClassifyItems { res := make([]*models.ChartClassifyItems, 0) for _, v := range list { if v.ParentId == parentId { v.Children = GetClassifyTreeRecursive(list, v.ChartClassifyId) res = append(res, v) } } return res } func GetChildClassifyByClassifyId(targetClassifyId int) (targetList []*models.ChartClassifyItems, err error, errMsg string) { //判断是否是挂在顶级目录下 targetClassify, err := models.GetChartClassifyById(targetClassifyId) if err != nil { if err.Error() == utils.ErrNoRow() { errMsg = "当前分类不存在" err = errors.New(errMsg) return } errMsg = "获取失败" err = errors.New("获取分类信息失败,Err:" + err.Error()) return } tmpList, err := models.GetChartClassifyOrderByLevel() 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.ChartClassifyId == targetClassify.ChartClassifyId { idMap[v.ChartClassifyId] = struct{}{} } } for _, v := range tmpList { if _, ok := idMap[v.ParentId]; ok { idMap[v.ChartClassifyId] = struct{}{} } } for _, v := range tmpList { if _, ok := idMap[v.ChartClassifyId]; ok { targetItem := new(models.ChartClassifyItems) targetItem.ChartClassifyId = v.ChartClassifyId targetItem.ParentId = v.ParentId targetItem.Level = v.Level targetItem.ChartClassifyName = v.ChartClassifyName targetItem.IsJoinPermission = v.IsJoinPermission targetList = append(targetList, targetItem) } } } return }