123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899 |
- package data
- import (
- "errors"
- "eta_gn/eta_api/models/data_manage"
- "eta_gn/eta_api/models/system"
- "eta_gn/eta_api/utils"
- "fmt"
- "strconv"
- "time"
- )
- // AddEdbPublicClassify
- // @Description: 添加公共指标分类
- // @author: Roc
- // @datetime 2024-12-04 18:00:22
- // @param classifyName string
- // @param parentId int
- // @param level int
- // @param classifyType uint8
- // @param sysUserId int
- // @param sysUserName string
- // @return classifyInfo *data_manage.EdbPublicClassify
- // @return err error
- // @return errMsg string
- func AddEdbPublicClassify(classifyName string, parentId int, classifyType uint8, sysUserId int, sysUserName string) (classifyInfo *data_manage.EdbPublicClassify, err error, errMsg string) {
- edbPublicClassifyObj := data_manage.EdbPublicClassify{}
- // 校验分类名称相同的数量
- {
- var count int
- count, err = edbPublicClassifyObj.GetEdbClassifyCount(classifyName, parentId, classifyType)
- if err != nil {
- errMsg = `判断名称是否已存在失败`
- return
- }
- if count > 0 {
- errMsg = `分类名称已存在,请重新输入`
- err = errors.New(errMsg)
- return
- }
- }
- // 父级的层级
- level := 0
- //获取该层级下最大的排序数
- maxSort, err := GetEdbPublicClassifyMaxSort(parentId, classifyType)
- if err != nil {
- errMsg = "获取失败"
- err = errors.New("查询排序信息失败,Err:" + err.Error())
- return
- }
- //查询顶级rootId
- rootId := 0
- var classifyNamePath, classifyIdPath string
- if parentId > 0 {
- parentClassify, tErr := edbPublicClassifyObj.GetEdbClassifyById(parentId)
- if tErr != nil {
- if utils.IsErrNoRow(tErr) {
- errMsg = "父级分类不存在"
- err = errors.New(errMsg)
- return
- }
- errMsg = "获取失败"
- err = errors.New("获取分类信息失败,Err:" + tErr.Error())
- return
- }
- level = parentClassify.Level
- rootId = parentClassify.RootId
- classifyNamePath = fmt.Sprint(parentClassify.EdbPublicClassifyNamePath, "|", classifyName)
- classifyIdPath = fmt.Sprint(parentClassify.EdbPublicClassifyIdPath, ",")
- } else {
- classifyNamePath = classifyName
- }
- // 层级校验
- if level > utils.EdbClassifyMaxLevel {
- errMsg = fmt.Sprintf("最高只支持添加%d级分类", level)
- return
- }
- timestamp := strconv.FormatInt(time.Now().UnixNano(), 10)
- classifyInfo = &data_manage.EdbPublicClassify{
- //EdbPublicClassifyId: 0,
- ClassifyType: classifyType,
- EdbPublicClassifyName: classifyName,
- ParentId: parentId,
- HasData: 0,
- RootId: rootId,
- CreateTime: time.Now(),
- ModifyTime: time.Now(),
- SysUserId: sysUserId,
- SysUserRealName: sysUserName,
- Level: level + 1,
- UniqueCode: utils.MD5(utils.PUBLIC_CLASSIFY_PREFIX + "_" + timestamp),
- Sort: maxSort + 1,
- LastModifyUserId: sysUserId,
- LastModifyUserRealName: sysUserName,
- EdbPublicClassifyNamePath: classifyNamePath,
- EdbPublicClassifyIdPath: "",
- }
- err = classifyInfo.Add()
- if err != nil {
- errMsg = "保存分类失败"
- return
- }
- // 更改分类id完整路径path
- updateCols := []string{"EdbPublicClassifyIdPath"}
- classifyInfo.EdbPublicClassifyIdPath = fmt.Sprint(classifyIdPath, classifyInfo.EdbPublicClassifyId)
- if parentId == 0 { //一级目录的rootId等于自己本身
- classifyInfo.RootId = classifyInfo.EdbPublicClassifyId
- updateCols = append(updateCols, "RootId")
- }
- err = classifyInfo.Update(updateCols)
- if err != nil {
- errMsg = "更新分类失败"
- return
- }
- return
- }
- // EditEdbPublicClassify
- // @Description: 编辑指标分类
- // @author: Roc
- // @datetime 2024-12-05 09:29:38
- // @param classifyId int
- // @param classifyName string
- // @param sysUser *system.Admin
- // @return err error
- // @return errMsg string
- func EditEdbPublicClassify(classifyId int, classifyName string, sysUser *system.Admin) (err error, errMsg string) {
- edbPublicClassifyObj := data_manage.EdbPublicClassify{}
- item, err := edbPublicClassifyObj.GetEdbClassifyById(classifyId)
- if err != nil {
- errMsg = `修改失败`
- return
- }
- // TODO 操作权限校验
- {
- }
- // 需要变更的字段
- updateCols := make([]string, 0)
- // 旧完整路径 , 新的完整路径
- var oldClassifyNamePath, newClassifyNamePath string
- // 名字相同,那么就直接返回
- if item.EdbPublicClassifyName == classifyName {
- return
- }
- // 判断名称是否已存在
- count, tmpErr := edbPublicClassifyObj.GetEdbClassifyCount(classifyName, item.ParentId, item.ClassifyType)
- if tmpErr != nil {
- err = tmpErr
- errMsg = "判断名称是否已存在失败"
- return
- }
- if count > 0 {
- errMsg = "分类名称已存在,请重新输入"
- err = errors.New(errMsg)
- return
- }
- // 旧完整路径
- oldClassifyNamePath = item.EdbPublicClassifyNamePath
- if item.ParentId > 0 {
- parentItem, tmpErr := edbPublicClassifyObj.GetEdbClassifyById(item.ParentId)
- if tmpErr != nil {
- err = tmpErr
- return
- }
- newClassifyNamePath = fmt.Sprint(parentItem.EdbPublicClassifyNamePath, "|", classifyName)
- } else {
- newClassifyNamePath = classifyName
- }
- // 更新自己的信息
- item.EdbPublicClassifyName = classifyName
- item.EdbPublicClassifyNamePath = newClassifyNamePath
- item.LastModifyUserId = sysUser.AdminId
- item.LastModifyUserRealName = sysUser.RealName
- updateCols = append(updateCols, "EdbPublicClassifyName", "EdbPublicClassifyNamePath", "LastModifyUserId", "LastModifyUserRealName")
- // 修改数据
- if len(updateCols) > 0 {
- err = item.UpdateEdbClassifyNameAndNamePath(updateCols, oldClassifyNamePath, newClassifyNamePath)
- if err != nil {
- errMsg = "保存失败"
- }
- }
- return
- }
- // GetEdbPublicClassifyMaxSort
- // @Description: 获取最大排序值
- // @author: Roc
- // @datetime 2024-12-04 16:54:57
- // @param parentId int
- // @param classifyType uint8
- // @return maxSort int
- // @return err error
- func GetEdbPublicClassifyMaxSort(parentId int, classifyType uint8) (maxSort int, err error) {
- edbPublicClassifyObj := data_manage.EdbPublicClassify{}
- //获取该层级下最大的排序数
- classifyMaxSort, err := edbPublicClassifyObj.GetEdbClassifyMaxSort(parentId, classifyType)
- if err != nil {
- return
- }
- maxSort = classifyMaxSort
- edbMaxSort, err := data_manage.GetEdbInfoMaxSortByClassifyId(parentId)
- if err != nil {
- return
- }
- if maxSort < edbMaxSort {
- maxSort = edbMaxSort
- }
- return
- }
- // DeleteEdbPublicClassifyCheck 删除检测
- func DeleteEdbPublicClassifyCheck(classifyId int, sysUser *system.Admin) (deleteStatus int, tipsMsg string, allClassifyIdList []int, tableList []*data_manage.ExcelBaseInfo, err error, errMsg string) {
- // TODO 操作权限校验
- {
- }
- edbPublicClassifyObj := data_manage.EdbPublicClassify{}
- // 查找当前分类
- item, tmpErr := edbPublicClassifyObj.GetEdbClassifyById(classifyId)
- if tmpErr != nil {
- errMsg = `查找分类失败`
- err = tmpErr
- return
- }
- // 查找分类下所有子分类
- childClassifyIdList, tmpErr := item.GetAllChildClassifyIdList()
- if tmpErr != nil {
- errMsg = `查找下级分类失败`
- err = tmpErr
- return
- }
- allClassifyIdList = childClassifyIdList
- allClassifyIdList = append(allClassifyIdList, item.EdbPublicClassifyId)
- // 判断分类下,是否含有指标
- {
- //判断分类下,是否含有指标
- count, tmpErr := edbPublicClassifyObj.GetEdbInfoCountByClassifyIdList(allClassifyIdList)
- if tmpErr != nil {
- errMsg = "删除失败"
- err = errors.New("分类下是否含有指标失败,Err:" + tmpErr.Error())
- return
- }
- if count > 0 {
- deleteStatus = 1
- tipsMsg = "目录关联指标不可删除"
- return
- }
- }
- // 子目录数量校验
- if len(childClassifyIdList) > 0 {
- deleteStatus = 2
- tipsMsg = "确认删除当前目录及包含的子目录吗"
- return
- }
- return
- }
- // DeleteEdbPublicClassify 删除分类/指标
- func DeleteEdbPublicClassify(classifyId int, sysUser *system.Admin, requestBody, requestUrl string) (nextItem *data_manage.EdbInfo, tableList []*data_manage.ExcelBaseInfo, err error, errMsg string) {
- //删除分类校验
- deleteStatus, tipsMsg, allClassifyIdList, _, err, errMsg := DeleteEdbPublicClassifyCheck(classifyId, sysUser)
- // 0:可以;2:删除子目录;1:不可删除(有关联指标)
- if deleteStatus == 1 {
- if tipsMsg != `` {
- errMsg = tipsMsg
- }
- if err == nil {
- err = errors.New(errMsg)
- }
- return
- }
- edbPublicClassifyObj := data_manage.EdbPublicClassify{}
- err = edbPublicClassifyObj.BatchesDel(allClassifyIdList)
- if err != nil {
- errMsg = "删除失败"
- return
- }
- return
- }
- // MoveEdbClassify 移动指标分类
- func MoveEdbPublicClassify(req data_manage.MoveEdbClassifyReq, sysUser *system.Admin, classifyType uint8) (err error, errMsg string) {
- // req.ClassifyId, req.ParentClassifyId, req.PrevClassifyId, req.NextClassifyId
- classifyId := req.ClassifyId
- parentClassifyId := req.ParentClassifyId
- prevClassifyId := req.PrevClassifyId
- nextClassifyId := req.NextClassifyId
- edbInfoId := req.EdbInfoId
- prevEdbInfoId := req.PrevEdbInfoId
- nextEdbInfoId := req.NextEdbInfoId
- obj := data_manage.EdbPublicClassify{}
- //首先确定移动的对象是分类还是指标
- //判断上一个节点是分类还是指标
- //判断下一个节点是分类还是指标
- //同时更新分类目录下的分类sort和指标sort
- //更新当前移动的分类或者指标sort
- var parentEdbClassifyInfo *data_manage.EdbPublicClassify
- if parentClassifyId > 0 {
- parentEdbClassifyInfo, err = obj.GetEdbClassifyById(parentClassifyId)
- if err != nil {
- errMsg = "移动失败"
- err = errors.New("获取上级分类信息失败,Err:" + err.Error())
- return
- }
- }
- //如果有传入 上一个兄弟节点分类id
- var (
- edbClassifyInfo *data_manage.EdbPublicClassify
- prevClassify *data_manage.EdbPublicClassify
- nextClassify *data_manage.EdbPublicClassify
- edbInfo *data_manage.EdbInfo
- prevEdbInfo *data_manage.EdbInfo
- nextEdbInfo *data_manage.EdbInfo
- prevSort int
- nextSort int
- )
- // 移动对象为分类, 判断权限
- if edbInfoId == 0 {
- edbClassifyInfo, err = obj.GetEdbClassifyById(classifyId)
- if err != nil {
- if utils.IsErrNoRow(err) {
- 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 := obj.GetEdbPublicClassifyByParentIdAndName(parentClassifyId, edbClassifyInfo.EdbPublicClassifyName, classifyId, classifyType)
- if e != nil && !utils.IsErrNoRow(e) {
- errMsg = "移动失败"
- err = fmt.Errorf("获取父级分类下的同名分类失败, Err: %s", e.Error())
- return
- }
- if exists != nil && exists.EdbPublicClassifyId > 0 {
- errMsg = "移动失败,分类名称已存在"
- return
- }
- } else {
- edbInfo, err = data_manage.GetEdbInfoById(req.EdbInfoId)
- if err != nil {
- if utils.IsErrNoRow(err) {
- 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
- }
- // TODO 移动权限校验
- }
- if prevClassifyId > 0 {
- prevClassify, err = obj.GetEdbClassifyById(prevClassifyId)
- if err != nil {
- errMsg = "移动失败"
- err = errors.New("获取上一个兄弟节点分类信息失败,Err:" + err.Error())
- return
- }
- prevSort = prevClassify.Sort
- } else if prevEdbInfoId > 0 {
- prevEdbInfo, err = data_manage.GetEdbInfoById(prevEdbInfoId)
- if err != nil {
- errMsg = "移动失败"
- err = errors.New("获取上一个兄弟节点分类信息失败,Err:" + err.Error())
- return
- }
- prevSort = prevEdbInfo.PublicSort
- }
- if nextClassifyId > 0 {
- //下一个兄弟节点
- nextClassify, err = obj.GetEdbClassifyById(nextClassifyId)
- if err != nil {
- errMsg = "移动失败"
- err = errors.New("获取下一个兄弟节点分类信息失败,Err:" + err.Error())
- return
- }
- nextSort = nextClassify.Sort
- } else if nextEdbInfoId > 0 {
- //下一个兄弟节点
- nextEdbInfo, err = data_manage.GetEdbInfoById(nextEdbInfoId)
- if err != nil {
- errMsg = "移动失败"
- err = errors.New("获取下一个兄弟节点分类信息失败,Err:" + err.Error())
- return
- }
- nextSort = nextEdbInfo.PublicSort
- }
- err, errMsg = movePublicEdbOrClassify(parentEdbClassifyInfo, edbClassifyInfo, prevClassify, nextClassify, edbInfo, prevEdbInfo, nextEdbInfo, parentClassifyId, prevSort, nextSort, classifyType)
- return
- }
- // movePublicEdbOrClassify
- // @Description: 移动公共指标/分类
- // @param parentEdbClassifyInfo
- // @param edbClassifyInfo
- // @param prevClassify
- // @param nextClassify
- // @param edbInfo
- // @param prevEdbInfo
- // @param nextEdbInfo
- // @param parentClassifyId
- // @param prevSort
- // @param nextSort
- // @param classifyType
- // @return err
- // @return errMsg
- func movePublicEdbOrClassify(parentEdbClassifyInfo, edbClassifyInfo, prevClassify, nextClassify *data_manage.EdbPublicClassify, edbInfo, prevEdbInfo, nextEdbInfo *data_manage.EdbInfo, parentClassifyId int, prevSort, nextSort int, classifyType uint8) (err error, errMsg string) {
- if edbClassifyInfo != nil {
- // 移动分类
- return movePublicEdbClassify(parentEdbClassifyInfo, edbClassifyInfo, prevClassify, nextClassify, prevEdbInfo, nextEdbInfo, parentClassifyId, prevSort, nextSort, classifyType)
- } else {
- // 移动指标
- return moveEdbPublic(prevClassify, nextClassify, edbInfo, prevEdbInfo, nextEdbInfo, parentClassifyId, prevSort, nextSort, classifyType)
- }
- return
- }
- // moveEdbPublic
- // @Description: 移动指标
- // @author: Roc
- // @datetime 2024-11-26 16:07:37
- // @param prevClassify *data_manage.EdbClassify
- // @param nextClassify *data_manage.EdbClassify
- // @param edbInfo *data_manage.EdbInfo
- // @param prevEdbInfo *data_manage.EdbInfo
- // @param nextEdbInfo *data_manage.EdbInfo
- // @param parentClassifyId int
- // @param prevSort int
- // @param nextSort int
- // @param classifyType uint8
- // @return err error
- // @return errMsg string
- func moveEdbPublic(prevClassify, nextClassify *data_manage.EdbPublicClassify, edbInfo, prevEdbInfo, nextEdbInfo *data_manage.EdbInfo, parentClassifyId int, prevSort, nextSort int, classifyType uint8) (err error, errMsg string) {
- updateCol := make([]string, 0)
- obj := data_manage.EdbPublicClassify{}
- fmt.Println("prevSort:", prevSort)
- fmt.Println("prevEdbInfo:", prevEdbInfo)
- fmt.Println("parentClassifyId:", parentClassifyId)
- if edbInfo == nil {
- errMsg = "当前指标不存在"
- err = errors.New(errMsg)
- return
- }
- //如果改变了分类,那么移动该指标数据
- if edbInfo.ClassifyId != parentClassifyId {
- edbInfo.EdbPublicClassifyId = parentClassifyId
- edbInfo.ModifyTime = time.Now()
- updateCol = append(updateCol, "EdbPublicClassifyId", "ModifyTime")
- }
- if prevEdbInfo != nil && prevEdbInfo.EdbInfoId > 0 {
- //如果是移动在两个兄弟节点之间
- if nextSort > 0 {
- //下一个兄弟节点
- //如果上一个兄弟与下一个兄弟的排序权重是一致的,那么需要将下一个兄弟(以及下个兄弟的同样排序权重)的排序权重+2,自己变成上一个兄弟的排序权重+1
- if prevSort == nextSort || prevSort == edbInfo.PublicSort {
- //变更兄弟节点的排序
- updateSortStr := ` sort + 2`
- updateEdbSortStr := ` public_sort + 2`
- //变更分类
- if prevClassify != nil {
- _ = obj.UpdateEdbClassifySortByParentId(parentClassifyId, prevClassify.EdbPublicClassifyId, prevClassify.Sort, updateSortStr, classifyType)
- } else {
- _ = obj.UpdateEdbClassifySortByParentId(parentClassifyId, 0, prevSort, updateSortStr, classifyType)
- }
- //变更指标
- if prevEdbInfo != nil {
- //变更兄弟节点的排序
- _ = data_manage.UpdateEdbInfoPublicSortByClassifyId(parentClassifyId, prevSort, prevEdbInfo.EdbInfoId, updateEdbSortStr)
- } else {
- _ = data_manage.UpdateEdbInfoPublicSortByClassifyId(parentClassifyId, prevSort, 0, updateEdbSortStr)
- }
- } else {
- //如果下一个兄弟的排序权重正好是上个兄弟节点的下一层,那么需要再加一层了
- if nextSort-prevSort == 1 {
- //变更兄弟节点的排序
- updateSortStr := ` sort + 1`
- updateEdbSortStr := ` public_sort + 1`
- //变更分类
- if prevClassify != nil {
- _ = obj.UpdateEdbClassifySortByParentId(parentClassifyId, prevClassify.EdbPublicClassifyId, prevSort, updateSortStr, classifyType)
- } else {
- _ = obj.UpdateEdbClassifySortByParentId(parentClassifyId, 0, prevSort, updateSortStr, classifyType)
- }
- //变更指标
- if prevEdbInfo != nil {
- //变更兄弟节点的排序
- _ = data_manage.UpdateEdbInfoPublicSortByClassifyId(parentClassifyId, prevSort, prevEdbInfo.EdbInfoId, updateSortStr)
- } else {
- _ = data_manage.UpdateEdbInfoPublicSortByClassifyId(parentClassifyId, prevSort, 0, updateEdbSortStr)
- }
- }
- }
- }
- edbInfo.PublicSort = prevEdbInfo.PublicSort + 1
- edbInfo.ModifyTime = time.Now()
- updateCol = append(updateCol, "PublicSort", "ModifyTime")
- } else if prevClassify == nil && nextClassify == nil && prevEdbInfo == nil && nextEdbInfo == nil && parentClassifyId > 0 {
- //处理只拖动到目录里,默认放到目录底部的情况
- var maxSort int
- maxSort, err = GetPublicEdbClassifyMaxSort(parentClassifyId, classifyType)
- if err != nil {
- errMsg = "移动失败"
- err = errors.New("查询组内排序信息失败,Err:" + err.Error())
- return
- }
- fmt.Println("maxSort:", maxSort)
- edbInfo.PublicSort = maxSort + 1 //那就是排在组内最后一位
- edbInfo.ModifyTime = time.Now()
- updateCol = append(updateCol, "PublicSort", "ModifyTime")
- } else {
- fmt.Println("进来:")
- // 拖动到父级分类的第一位
- firstClassify, tmpErr := obj.GetFirstEdbClassifyByParentId(parentClassifyId)
- if tmpErr != nil && !utils.IsErrNoRow(tmpErr) {
- errMsg = "移动失败"
- err = errors.New("获取获取当前父级分类下的排序第一条的分类信息失败,Err:" + tmpErr.Error())
- return
- }
- //如果该分类下存在其他分类,且第一个其他分类的排序等于0,那么需要调整排序
- if firstClassify != nil && firstClassify.EdbPublicClassifyId > 0 && firstClassify.Sort == 0 {
- updateSortStr := ` sort + 1 `
- _ = obj.UpdateEdbClassifySortByParentId(parentClassifyId, firstClassify.EdbPublicClassifyId-1, 0, updateSortStr, classifyType)
- //该分类下的所有指标也需要+1
- updateEdbSortStr := ` public_sort + 1 `
- _ = data_manage.UpdateEdbInfoPublicSortByClassifyId(parentClassifyId, 0, 0, updateEdbSortStr)
- } else {
- //如果该分类下存在指标,且第一个指标的排序等于0,那么需要调整排序
- firstEdb, tErr := data_manage.GetFirstEdbInfoByPublicClassifyId(parentClassifyId)
- if tErr != nil && !utils.IsErrNoRow(tErr) {
- errMsg = "移动失败"
- err = errors.New("获取获取当前父级分类下的排序第一条的分类信息失败,Err:" + tErr.Error())
- return
- }
- //如果该分类下存在其他分类,且第一个其他分类的排序等于0,那么需要调整排序
- if firstEdb != nil && firstEdb.ClassifyId > 0 && firstEdb.Sort == 0 {
- updateSortStr := ` sort + 1 `
- _ = obj.UpdateEdbClassifySortByParentId(parentClassifyId, 0, 0, updateSortStr, classifyType)
- // 指标
- updateEdbSortStr := ` public_sort + 1 `
- _ = data_manage.UpdateEdbInfoPublicSortByClassifyId(parentClassifyId, 0, firstEdb.EdbInfoId-1, updateEdbSortStr)
- }
- }
- edbInfo.PublicSort = 0 //那就是排在第一位
- edbInfo.ModifyTime = time.Now()
- updateCol = append(updateCol, "PublicSort", "ModifyTime")
- }
- //更新
- if len(updateCol) > 0 {
- err = edbInfo.Update(updateCol)
- if err != nil {
- errMsg = "移动失败"
- err = errors.New("修改失败,Err:" + err.Error())
- return
- }
- }
- return
- }
- // movePublicEdbClassify
- // @Description: 移动指标分类
- // @author: Roc
- // @datetime 2024-11-26 16:07:44
- // @param parentEdbClassifyInfo *data_manage.EdbClassify
- // @param edbClassifyInfo *data_manage.EdbClassify
- // @param prevClassify *data_manage.EdbClassify
- // @param nextClassify *data_manage.EdbClassify
- // @param edbInfo *data_manage.EdbInfo
- // @param prevEdbInfo *data_manage.EdbInfo
- // @param nextEdbInfo *data_manage.EdbInfo
- // @param parentClassifyId int
- // @param prevSort int
- // @param nextSort int
- // @param classifyType uint8
- // @return err error
- // @return errMsg string
- func movePublicEdbClassify(parentEdbClassifyInfo, edbClassifyInfo, prevClassify, nextClassify *data_manage.EdbPublicClassify, prevEdbInfo, nextEdbInfo *data_manage.EdbInfo, parentClassifyId int, prevSort, nextSort int, classifyType uint8) (err error, errMsg string) {
- updateCol := make([]string, 0)
- obj := data_manage.EdbPublicClassify{}
- // 移动对象为分类, 判断分类是否存在
- oldParentId := edbClassifyInfo.ParentId
- oldLevel := edbClassifyInfo.Level
- var classifyIds []int
- if oldParentId != parentClassifyId {
- //更新子分类对应的level
- childList, e, m := GetChildClassifyByPublicClassifyId(edbClassifyInfo.EdbPublicClassifyId)
- if e != nil {
- errMsg = "移动失败"
- err = errors.New("查询子分类失败,Err:" + e.Error() + m)
- return
- }
- if len(childList) > 0 {
- for _, v := range childList {
- if v.ClassifyId == edbClassifyInfo.EdbPublicClassifyId {
- continue
- }
- classifyIds = append(classifyIds, v.ClassifyId)
- }
- }
- }
- //判断上级id是否一致,如果不一致的话,那么需要移动该分类层级
- if edbClassifyInfo.ParentId != parentClassifyId && parentClassifyId != 0 {
- if edbClassifyInfo.Level != parentEdbClassifyInfo.Level+1 { //禁止层级调整
- errMsg = "移动失败"
- err = errors.New("不支持目录层级变更")
- return
- }
- edbClassifyInfo.ParentId = parentEdbClassifyInfo.EdbPublicClassifyId
- edbClassifyInfo.RootId = parentEdbClassifyInfo.RootId
- edbClassifyInfo.Level = parentEdbClassifyInfo.Level + 1
- edbClassifyInfo.ModifyTime = time.Now()
- updateCol = append(updateCol, "ParentId", "RootId", "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`
- updateEdbSortStr := ` public_sort + 2`
- //变更分类
- if prevClassify != nil {
- _ = obj.UpdateEdbClassifySortByParentId(parentClassifyId, prevClassify.EdbPublicClassifyId, prevClassify.Sort, updateSortStr, classifyType)
- } else {
- _ = obj.UpdateEdbClassifySortByParentId(parentClassifyId, 0, prevSort, updateSortStr, classifyType)
- }
- //变更指标
- if prevEdbInfo != nil {
- //变更兄弟节点的排序
- _ = data_manage.UpdateEdbInfoPublicSortByClassifyId(parentClassifyId, prevSort, prevEdbInfo.EdbInfoId, updateEdbSortStr)
- } else {
- _ = data_manage.UpdateEdbInfoPublicSortByClassifyId(parentClassifyId, prevSort, 0, updateEdbSortStr)
- }
- } else {
- //如果下一个兄弟的排序权重正好是上个兄弟节点的下一层,那么需要再加一层了
- if nextSort-prevSort == 1 {
- //变更兄弟节点的排序
- updateSortStr := `sort + 1`
- updateEdbSortStr := `public_sort + 1`
- //变更分类
- if prevClassify != nil {
- _ = obj.UpdateEdbClassifySortByParentId(parentClassifyId, prevClassify.EdbPublicClassifyId, prevSort, updateSortStr, classifyType)
- } else {
- _ = obj.UpdateEdbClassifySortByParentId(parentClassifyId, 0, prevSort, updateSortStr, classifyType)
- }
- //变更指标
- if prevEdbInfo != nil {
- //变更兄弟节点的排序
- _ = data_manage.UpdateEdbInfoPublicSortByClassifyId(parentClassifyId, prevSort, prevEdbInfo.EdbInfoId, updateEdbSortStr)
- } else {
- _ = data_manage.UpdateEdbInfoPublicSortByClassifyId(parentClassifyId, prevSort, 0, updateEdbSortStr)
- }
- }
- }
- }
- 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 = GetEdbPublicClassifyMaxSort(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 := obj.GetFirstEdbClassifyByParentId(parentClassifyId)
- if tmpErr != nil && !utils.IsErrNoRow(tmpErr) {
- errMsg = "移动失败"
- err = errors.New("获取获取当前父级分类下的排序第一条的分类信息失败,Err:" + tmpErr.Error())
- return
- }
- //如果该分类下存在其他分类,且第一个其他分类的排序等于0,那么需要调整排序
- if firstClassify != nil && firstClassify.EdbPublicClassifyId > 0 && firstClassify.Sort == 0 {
- updateSortStr := ` sort + 1 `
- updateEdbSortStr := ` public_sort + 1 `
- _ = obj.UpdateEdbClassifySortByParentId(parentClassifyId, firstClassify.EdbPublicClassifyId-1, 0, updateSortStr, classifyType)
- //该分类下的所有指标也需要+1
- _ = data_manage.UpdateEdbInfoPublicSortByClassifyId(parentClassifyId, 0, 0, updateEdbSortStr)
- } else {
- //如果该分类下存在指标,且第一个指标的排序等于0,那么需要调整排序
- firstEdb, tErr := data_manage.GetFirstEdbInfoByPublicClassifyId(parentClassifyId)
- if tErr != nil && !utils.IsErrNoRow(tErr) {
- errMsg = "移动失败"
- err = errors.New("获取获取当前父级分类下的排序第一条的分类信息失败,Err:" + tErr.Error())
- return
- }
- //如果该分类下存在其他分类,且第一个其他分类的排序等于0,那么需要调整排序
- if firstEdb != nil && firstEdb.ClassifyId > 0 && firstEdb.Sort == 0 {
- updateSortStr := ` sort + 1 `
- _ = obj.UpdateEdbClassifySortByParentId(parentClassifyId, 0, 0, updateSortStr, classifyType)
- updateEdbSortStr := ` public_sort + 1 `
- _ = data_manage.UpdateEdbInfoPublicSortByClassifyId(parentClassifyId, 0, firstEdb.EdbInfoId-1, updateEdbSortStr)
- }
- }
- edbClassifyInfo.Sort = 0 //那就是排在第一位
- edbClassifyInfo.ModifyTime = time.Now()
- updateCol = append(updateCol, "Sort", "ModifyTime")
- }
- oldClassifyIdPath := edbClassifyInfo.EdbPublicClassifyIdPath
- oldClassifyNamePath := edbClassifyInfo.EdbPublicClassifyNamePath
- newClassifyNamePath := fmt.Sprint(parentEdbClassifyInfo.EdbPublicClassifyNamePath, `|`, edbClassifyInfo.EdbPublicClassifyName)
- newClassifyIdPath := fmt.Sprint(parentEdbClassifyInfo.EdbPublicClassifyIdPath, `,`, edbClassifyInfo.EdbPublicClassifyId)
- //更新
- if len(updateCol) > 0 {
- edbClassifyInfo.EdbPublicClassifyNamePath = newClassifyNamePath
- edbClassifyInfo.EdbPublicClassifyIdPath = newClassifyIdPath
- updateCol = append(updateCol, "EdbPublicClassifyNamePath", "EdbPublicClassifyIdPath")
- 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 = obj.UpdateEdbClassifyChildByParentClassifyId(classifyIds, edbClassifyInfo.RootId, levelStep)
- if err != nil {
- errMsg = "移动失败"
- err = errors.New("更新子分类失败,Err:" + err.Error())
- return
- }
- }
- // 更改了上级分类,那么需要同步去更改自己的分类全路径
- tmpErr := obj.UpdateEdbClassifyNameAndNamePathByOldClassifyIdPath(oldClassifyIdPath, newClassifyIdPath, oldClassifyNamePath, newClassifyNamePath)
- if tmpErr != nil {
- utils.FileLog.Error(fmt.Sprintf("更新分类全路径失败,分类id:%d;失败原因Err:%s", edbClassifyInfo.EdbPublicClassifyId, tmpErr.Error()))
- }
- }
- }
- return
- }
- func GetPublicEdbClassifyMaxSort(parentId int, classifyType uint8) (maxSort int, err error) {
- //获取该层级下最大的排序数
- obj := data_manage.EdbPublicClassify{}
- classifyMaxSort, err := obj.GetEdbClassifyMaxSort(parentId, classifyType)
- if err != nil {
- return
- }
- maxSort = classifyMaxSort
- edbMaxSort, err := data_manage.GetEdbInfoMaxPublicSortByClassifyId(parentId)
- if err != nil {
- return
- }
- if maxSort < edbMaxSort {
- maxSort = edbMaxSort
- }
- return
- }
- func GetChildClassifyByPublicClassifyId(targetClassifyId int) (targetList []*data_manage.EdbClassifyIdItems, err error, errMsg string) {
- obj := data_manage.EdbPublicClassify{}
- //判断是否是挂在顶级目录下
- targetClassify, err := obj.GetEdbClassifyById(targetClassifyId)
- if err != nil {
- if utils.IsErrNoRow(err) {
- errMsg = "当前分类不存在"
- err = errors.New(errMsg)
- return
- }
- errMsg = "获取失败"
- err = errors.New("获取分类信息失败,Err:" + err.Error())
- return
- }
- orderStr := ` order by level asc, sort asc, edb_public_classify_id asc`
- tmpList, err := obj.GetEdbClassifyByRootIdLevel(targetClassify.RootId, targetClassify.ClassifyType, orderStr)
- if err != nil && !utils.IsErrNoRow(err) {
- errMsg = "获取失败"
- err = errors.New("获取数据失败,Err:" + err.Error())
- return
- }
- idMap := make(map[int]struct{})
- if len(tmpList) > 0 {
- for _, v := range tmpList {
- if v.EdbPublicClassifyId == targetClassify.EdbPublicClassifyId {
- idMap[v.EdbPublicClassifyId] = struct{}{}
- }
- }
- for _, v := range tmpList {
- if _, ok := idMap[v.ParentId]; ok {
- idMap[v.EdbPublicClassifyId] = struct{}{}
- }
- }
- for _, v := range tmpList {
- if _, ok := idMap[v.EdbPublicClassifyId]; ok {
- targetItem := new(data_manage.EdbClassifyIdItems)
- targetItem.ClassifyId = v.EdbPublicClassifyId
- targetItem.ParentId = v.ParentId
- targetItem.RootId = v.RootId
- targetItem.UniqueCode = v.UniqueCode
- targetItem.Level = v.Level
- targetItem.ClassifyName = v.EdbPublicClassifyName
- targetList = append(targetList, targetItem)
- }
- }
- }
- return
- }
|