12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517 |
- package data
- import (
- "errors"
- "eta_gn/eta_api/models/data_manage"
- "eta_gn/eta_api/models/system"
- "eta_gn/eta_api/services/data/data_manage_permission"
- "eta_gn/eta_api/utils"
- "fmt"
- "strconv"
- "time"
- )
- func chartClassifyHaveChild(allNode []*data_manage.ChartClassifyItems, node *data_manage.ChartClassifyItems) (childs []*data_manage.ChartClassifyItems, yes bool) {
- for _, v := range allNode {
- if v.ParentId == node.ChartClassifyId {
- childs = append(childs, v)
- }
- }
- if len(childs) > 0 {
- yes = true
- }
- return
- }
- func ChartClassifyItemsMakeTree(sysUser *system.Admin, allNode []*data_manage.ChartClassifyItems, node *data_manage.ChartClassifyItems, permissionClassifyIdList []int) {
- 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, permissionClassifyIdList) //递归添加节点
- } else {
- childrenArr := make([]*data_manage.ChartClassifyItems, 0)
- v.Children = childrenArr
- }
- }
- } else {
- childrenArr := make([]*data_manage.ChartClassifyItems, 0)
- node.Children = childrenArr
- }
- }
- func ChartClassifyItemsMakeTreeV2(allNode []*data_manage.ChartClassifyItems, node *data_manage.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 {
- ChartClassifyItemsMakeTreeV2(allNode, v) //递归添加节点
- } else {
- childrenArr := make([]*data_manage.ChartClassifyItems, 0)
- v.Children = childrenArr
- }
- }
- } else {
- childrenArr := make([]*data_manage.ChartClassifyItems, 0)
- node.Children = childrenArr
- }
- }
- // GetChartClassifyOpButton 获取ETA图库分类的操作权限
- func GetChartClassifyOpButton(sysUser *system.Admin, belongUserId int, haveOperaAuth bool) (button data_manage.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
- }
- // GetChartOpButton 获取ETA图库的操作权限
- func GetChartOpButton(sysUser *system.Admin, belongUserId, chartInfoId int, haveOperaAuth bool, editShareChartMap map[int]bool) (button data_manage.ChartClassifyItemsButton) {
- // 数据权限判断
- if !haveOperaAuth {
- return
- }
- if sysUser.AdminId == belongUserId {
- //button.AddButton = true
- button.OpButton = true
- button.DeleteButton = true
- button.MoveButton = true
- } else {
- // 拥有别人分享的操作权限
- if _, ok := editShareChartMap[chartInfoId]; ok {
- button.OpButton = true
- }
- }
- return
- }
- // HandleNoPermissionChart 图表列表返回,将没有权限的图表移除
- func HandleNoPermissionChart(allNodes []*data_manage.ChartClassifyItems, noPermissionChartIdMap map[int]bool, sysUserId int) (newAllNodes []*data_manage.ChartClassifyItems) {
- // 涉及分类id
- classifyIdList := make([]int, 0)
- // 移除没有权限的图表
- newAllNodes = make([]*data_manage.ChartClassifyItems, 0)
- for _, node := range allNodes {
- // 二级分类
- tmpNodeInfo := *node
- tmpNodeList := make([]*data_manage.ChartClassifyItems, 0)
- if node.Children != nil {
- for _, chartInfo := range node.Children {
- // 如果指标不可见,那么就不返回该指标
- if _, ok := noPermissionChartIdMap[chartInfo.ChartInfoId]; ok {
- continue
- }
- tmpNodeList = append(tmpNodeList, chartInfo)
- classifyIdList = append(classifyIdList, chartInfo.ChartClassifyId)
- }
- }
- tmpNodeInfo.Children = tmpNodeList
- newAllNodes = append(newAllNodes, &tmpNodeInfo)
- classifyIdList = append(classifyIdList, node.ChartClassifyId)
- }
- // 数据权限
- if len(classifyIdList) > 0 {
- // 图表分类
- chartClassifyList, err := data_manage.GetChartClassifyByIdList(classifyIdList)
- if err != nil {
- return
- }
- chartClassifyMap := make(map[int]*data_manage.ChartClassify)
- for _, v := range chartClassifyList {
- chartClassifyMap[v.ChartClassifyId] = v
- }
- // 已授权分类id
- permissionChartIdList, permissionClassifyIdList, err := data_manage_permission.GetUserChartAndClassifyPermissionList(sysUserId, 0, 0)
- if err != nil {
- return
- }
- for _, node := range newAllNodes {
- if node.Children != nil {
- for _, chartInfo := range node.Children {
- if currClassify, ok := chartClassifyMap[chartInfo.ChartClassifyId]; ok {
- chartInfo.HaveOperaAuth = data_manage_permission.CheckChartPermissionByPermissionIdList(chartInfo.IsJoinPermission, currClassify.IsJoinPermission, chartInfo.ChartInfoId, chartInfo.ChartClassifyId, permissionChartIdList, permissionClassifyIdList)
- }
- }
- }
- if currClassify, ok := chartClassifyMap[node.ChartClassifyId]; ok {
- node.HaveOperaAuth = data_manage_permission.CheckChartPermissionByPermissionIdList(node.IsJoinPermission, currClassify.IsJoinPermission, node.ChartInfoId, node.ChartClassifyId, permissionChartIdList, permissionClassifyIdList)
- }
- }
- }
- 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 *data_manage.ChartClassify
- // @return err error
- // @return errMsg string
- // @return isSendEmail bool
- func AddChartClassify(chartClassifyName string, parentId, level, source int, lang string, sysUser *system.Admin) (classifyInfo *data_manage.ChartClassify, err error, errMsg string, isSendEmail bool) {
- isSendEmail = true
- errMsg = "保存分类失败"
- // 校验分类名称相同的数量(eta2.3,图表名称允许重复)
- //{
- // var count int
- // switch lang {
- // case utils.EnLangVersion:
- // count, err = data_manage.GetChartClassifyEnCount(chartClassifyName, parentId, source)
- // default:
- // count, err = data_manage.GetChartClassifyCount(chartClassifyName, parentId, source)
- // }
- // if err != nil {
- // errMsg = "判断名称是否已存在失败"
- // return
- // }
- // if count > 0 {
- // errMsg = "分类名称已存在,请重新输入"
- // err = errors.New(errMsg)
- // isSendEmail = false
- // return
- // }
- //}
- if level > 6 {
- errMsg = `最高只支持添加6级分类`
- return
- }
- //获取该层级下最大的排序数
- maxSort, err := data_manage.GetChartClassifyMaxSort(parentId, source)
- if err != nil {
- errMsg = "获取失败"
- err = errors.New("查询排序信息失败,Err:" + err.Error())
- return
- }
- //查询顶级rootId
- rootId := 0
- var classifyNamePath, classifyIdPath string
- if parentId > 0 {
- parentClassify, tErr := data_manage.GetChartClassifyById(parentId)
- if tErr != nil {
- if utils.IsErrNoRow(tErr) {
- errMsg = "父级分类不存在"
- err = errors.New(errMsg)
- return
- }
- errMsg = "获取失败"
- err = errors.New("获取分类信息失败,Err:" + tErr.Error())
- return
- }
- rootId = parentClassify.RootId
- classifyNamePath = fmt.Sprint(parentClassify.ChartClassifyNamePath, "|", chartClassifyName)
- classifyIdPath = fmt.Sprint(parentClassify.ChartClassifyIdPath, ",")
- } else {
- classifyNamePath = chartClassifyName
- }
- classifyInfo = new(data_manage.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.CHART_PREFIX + "_" + timestamp)
- classifyInfo.Sort = maxSort + 1
- classifyInfo.Source = source
- classifyInfo.RootId = rootId
- classifyInfo.ChartClassifyNamePath = classifyNamePath
- err = data_manage.AddChartClassify(classifyInfo)
- if err != nil {
- return
- }
- // 更改分类id完整路径path
- updateCols := []string{"ClassifyIdPath"}
- classifyInfo.ChartClassifyIdPath = fmt.Sprint(classifyIdPath, classifyInfo.ChartClassifyId)
- if parentId == 0 { //一级目录的rootId等于自己本身
- classifyInfo.RootId = classifyInfo.ChartClassifyId
- updateCols = append(updateCols, "RootId")
- }
- err = classifyInfo.Update(updateCols)
- if err != nil {
- errMsg = "更新分类失败"
- return
- }
- //// 目前只有ETA图库需要继承分类权限
- //if classifyInfo.Source == utils.CHART_SOURCE_DEFAULT {
- // go data_manage_permission.InheritParentClassify(5, classifyInfo.Source, classifyInfo.ChartClassifyId, classifyInfo.ParentId, classifyInfo.ChartClassifyName)
- //}
- 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 *data_manage.ChartClassify
- // @return err error
- // @return errMsg string
- // @return isSendEmail bool
- func EditChartClassify(chartClassifyId, source int, chartClassifyName, lang string, sysUser *system.Admin) (classifyInfo *data_manage.ChartClassify, err error, errMsg string, isSendEmail bool) {
- isSendEmail = true
- errMsg = "保存失败"
- // 获取分类信息
- classifyInfo, err = data_manage.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)
- // 旧完整路径 , 新的完整路径
- var oldClassifyNamePath, newClassifyNamePath string
- // 语言版本校验
- switch lang {
- case utils.EnLangVersion:
- if classifyInfo.ChartClassifyNameEn != chartClassifyName {
- count, tmpErr := data_manage.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 := data_manage.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
- //}
- // 旧完整路径
- oldClassifyNamePath = classifyInfo.ChartClassifyNamePath
- if classifyInfo.ParentId > 0 {
- parentItem, tmpErr := data_manage.GetChartClassifyById(classifyInfo.ParentId)
- if tmpErr != nil {
- err = tmpErr
- return
- }
- newClassifyNamePath = fmt.Sprint(parentItem.ChartClassifyNamePath, "|", chartClassifyName)
- } else {
- newClassifyNamePath = chartClassifyName
- }
- classifyInfo.ChartClassifyName = chartClassifyName
- classifyInfo.ModifyTime = time.Now()
- classifyInfo.ChartClassifyNamePath = newClassifyNamePath
- updateCols = append(updateCols, "ChartClassifyName", "ModifyTime", "ChartClassifyNamePath")
- }
- }
- if len(updateCols) > 0 {
- err = classifyInfo.UpdateClassifyNameAndNamePath(updateCols, oldClassifyNamePath, newClassifyNamePath)
- }
- return
- }
- // MoveChartClassify 移动图表分类
- func MoveChartClassify(req data_manage.MoveChartClassifyReq, sysUser *system.Admin, source int) (err error, errMsg string) {
- 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 parentChartClassifyInfo *data_manage.ChartClassify
- if parentClassifyId > 0 {
- parentChartClassifyInfo, err = data_manage.GetChartClassifyById(parentClassifyId)
- if err != nil {
- errMsg = "移动失败"
- err = errors.New("获取上级分类信息失败,Err:" + err.Error())
- return
- }
- }
- //如果有传入 上一个兄弟节点分类id
- var (
- chartClassifyInfo *data_manage.ChartClassify
- prevClassify *data_manage.ChartClassify
- nextClassify *data_manage.ChartClassify
- chartInfo *data_manage.ChartInfo
- prevChartInfo *data_manage.ChartInfo
- nextChartInfo *data_manage.ChartInfo
- prevSort int
- nextSort int
- )
- // 移动对象为分类, 判断权限
- if chartInfoId == 0 {
- chartClassifyInfo, err = data_manage.GetChartClassifyById(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 && parentChartClassifyInfo.Level == 6 {
- errMsg = "最高只支持添加6级分类"
- err = errors.New(errMsg)
- return
- }
- // 如果是移动目录, 那么校验一下父级目录下是否有重名目录
- exists, e := data_manage.GetChartClassifyByParentIdAndName(source, parentClassifyId, chartClassifyInfo.ChartClassifyName, classifyId)
- if e != nil && !utils.IsErrNoRow(e) {
- errMsg = "移动失败"
- err = fmt.Errorf("获取父级分类下的同名分类失败, Err: %s", e.Error())
- return
- }
- if exists != nil && exists.ChartClassifyId > 0 {
- errMsg = "移动失败,分类名称已存在"
- return
- }
- // 权限校验
- {
- // 已授权分类id
- permissionClassifyIdList, tmpErr := data_manage_permission.GetUserChartClassifyPermissionList(sysUser.AdminId, classifyId)
- if tmpErr != nil {
- errMsg = "移动失败"
- err = errors.New("获取已授权分类id数据失败,Err:" + tmpErr.Error())
- return
- }
- // 数据权限
- haveOperaAuth := data_manage_permission.CheckChartClassifyPermissionByPermissionIdList(chartClassifyInfo.IsJoinPermission, chartClassifyInfo.ChartClassifyId, permissionClassifyIdList)
- button := GetChartClassifyOpButton(sysUser, chartClassifyInfo.SysUserId, haveOperaAuth)
- if !button.MoveButton {
- errMsg = "无操作权限"
- err = errors.New(errMsg)
- return
- }
- }
- } else {
- chartInfo, err = data_manage.GetChartInfoById(req.ChartInfoId)
- 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
- }
- var haveOperaAuth bool
- // 权限校验
- {
- haveOperaAuth, err = data_manage_permission.CheckChartPermissionByChartInfoId(chartInfo.ChartInfoId, chartInfo.ChartClassifyId, chartInfo.IsJoinPermission, sysUser.AdminId)
- if err != nil {
- errMsg = "移动失败"
- err = errors.New("获取图表权限信息失败,Err:" + err.Error())
- return
- }
- }
- editShareChartInfoIdMap, tmpErr := GetAllEditSharedChartInfoIdMapByReceivedUserIdAndChartInfoIdList(sysUser.AdminId, []int{chartInfo.ChartInfoId})
- if tmpErr != nil {
- errMsg = "移动失败"
- err = errors.New("获取所有有编辑权限的分享图表失败,Err:" + tmpErr.Error())
- return
- }
- // 移动权限校验
- button := GetChartOpButton(sysUser, chartInfo.SysUserId, chartInfo.ChartInfoId, haveOperaAuth, editShareChartInfoIdMap)
- if !button.MoveButton {
- errMsg = "无操作权限"
- err = errors.New(errMsg)
- return
- }
- }
- if prevClassifyId > 0 {
- prevClassify, err = data_manage.GetChartClassifyById(prevClassifyId)
- if err != nil {
- errMsg = "移动失败"
- err = errors.New("获取上一个兄弟节点分类信息失败,Err:" + err.Error())
- return
- }
- prevSort = prevClassify.Sort
- } else if prevChartInfoId > 0 {
- prevChartInfo, err = data_manage.GetChartInfoById(prevChartInfoId)
- if err != nil {
- errMsg = "移动失败"
- err = errors.New("获取上一个兄弟节点分类信息失败,Err:" + err.Error())
- return
- }
- prevSort = prevChartInfo.Sort
- }
- if nextClassifyId > 0 {
- //下一个兄弟节点
- nextClassify, err = data_manage.GetChartClassifyById(nextClassifyId)
- if err != nil {
- errMsg = "移动失败"
- err = errors.New("获取下一个兄弟节点分类信息失败,Err:" + err.Error())
- return
- }
- nextSort = nextClassify.Sort
- } else if nextChartInfoId > 0 {
- //下一个兄弟节点
- nextChartInfo, err = data_manage.GetChartInfoById(nextChartInfoId)
- if err != nil {
- errMsg = "移动失败"
- err = errors.New("获取下一个兄弟节点分类信息失败,Err:" + err.Error())
- return
- }
- nextSort = nextChartInfo.Sort
- }
- err, errMsg = moveChartOrClassify(parentChartClassifyInfo, chartClassifyInfo, prevClassify, nextClassify, chartInfo, prevChartInfo, nextChartInfo, parentClassifyId, prevSort, nextSort, source)
- return
- }
- func moveChartOrClassify(parentChartClassifyInfo, chartClassifyInfo, prevClassify, nextClassify *data_manage.ChartClassify, chartInfo, prevChartInfo, nextChartInfo *data_manage.ChartInfo, parentClassifyId, prevSort, nextSort, source int) (err error, errMsg string) {
- updateCol := make([]string, 0)
- // 移动对象为分类, 判断分类是否存在
- if chartClassifyInfo != nil {
- oldParentId := chartClassifyInfo.ParentId
- oldLevel := chartClassifyInfo.Level
- var classifyIds []int
- if oldParentId != parentClassifyId {
- //更新子分类对应的level
- childList, e, m := GetChildChartClassifyByClassifyId(chartClassifyInfo.ChartClassifyId)
- if e != nil {
- errMsg = "移动失败"
- err = errors.New("查询子分类失败,Err:" + e.Error() + m)
- return
- }
- if len(childList) > 0 {
- for _, v := range childList {
- if v.ChartClassifyId == chartClassifyInfo.ChartClassifyId {
- continue
- }
- classifyIds = append(classifyIds, v.ChartClassifyId)
- }
- }
- }
- //判断上级id是否一致,如果不一致的话,那么需要移动该分类层级
- if chartClassifyInfo.ParentId != parentClassifyId && parentClassifyId != 0 {
- if chartClassifyInfo.Level != parentChartClassifyInfo.Level+1 { //禁止层级调整
- errMsg = "移动失败"
- err = errors.New("不支持目录层级变更")
- return
- }
- chartClassifyInfo.ParentId = parentChartClassifyInfo.ChartClassifyId
- chartClassifyInfo.RootId = parentChartClassifyInfo.RootId
- chartClassifyInfo.Level = parentChartClassifyInfo.Level + 1
- chartClassifyInfo.ModifyTime = time.Now()
- updateCol = append(updateCol, "ParentId", "RootId", "Level", "ModifyTime")
- } else if chartClassifyInfo.ParentId != parentClassifyId && parentClassifyId == 0 {
- errMsg = "移动失败"
- err = errors.New("不支持目录层级变更")
- return
- }
- if prevSort > 0 {
- //如果是移动在两个兄弟节点之间
- if nextSort > 0 {
- //下一个兄弟节点
- //如果上一个兄弟与下一个兄弟的排序权重是一致的,那么需要将下一个兄弟(以及下个兄弟的同样排序权重)的排序权重+2,自己变成上一个兄弟的排序权重+1
- if prevSort == nextSort || prevSort == chartClassifyInfo.Sort {
- //变更兄弟节点的排序
- updateSortStr := `sort + 2`
- //变更分类
- if prevClassify != nil {
- _ = data_manage.UpdateChartClassifySortByParentIdAndSource(parentClassifyId, prevClassify.ChartClassifyId, prevClassify.Sort, updateSortStr, source)
- } else {
- _ = data_manage.UpdateChartClassifySortByParentIdAndSource(parentClassifyId, 0, prevSort, updateSortStr, source)
- }
- //变更图表
- if prevChartInfo != nil {
- //变更兄弟节点的排序
- _ = data_manage.UpdateChartInfoSortByClassifyIdV2(parentClassifyId, prevSort, prevChartInfo.ChartInfoId, updateSortStr)
- } else {
- _ = data_manage.UpdateChartInfoSortByClassifyIdV2(parentClassifyId, prevSort, 0, updateSortStr)
- }
- } else {
- //如果下一个兄弟的排序权重正好是上个兄弟节点的下一层,那么需要再加一层了
- if nextSort-prevSort == 1 {
- //变更兄弟节点的排序
- updateSortStr := `sort + 1`
- //变更分类
- if prevClassify != nil {
- _ = data_manage.UpdateChartClassifySortByParentIdAndSource(parentClassifyId, prevClassify.ChartClassifyId, prevSort, updateSortStr, source)
- } else {
- _ = data_manage.UpdateChartClassifySortByParentIdAndSource(parentClassifyId, 0, prevSort, updateSortStr, source)
- }
- //变更图表
- if prevChartInfo != nil {
- //变更兄弟节点的排序
- _ = data_manage.UpdateChartInfoSortByClassifyIdV2(parentClassifyId, prevSort, prevChartInfo.ChartInfoId, updateSortStr)
- } else {
- _ = data_manage.UpdateChartInfoSortByClassifyIdV2(parentClassifyId, prevSort, 0, updateSortStr)
- }
- }
- }
- }
- chartClassifyInfo.Sort = prevSort + 1
- chartClassifyInfo.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 = GetChartClassifyMaxSort(parentClassifyId, source)
- if err != nil {
- errMsg = "移动失败"
- err = errors.New("查询组内排序信息失败,Err:" + err.Error())
- return
- }
- chartClassifyInfo.Sort = maxSort + 1 //那就是排在组内最后一位
- chartClassifyInfo.ModifyTime = time.Now()
- updateCol = append(updateCol, "Sort", "ModifyTime")
- } else {
- // 拖动到父级分类的第一位
- firstClassify, tmpErr := data_manage.GetFirstChartClassifyByParentId(parentClassifyId)
- if tmpErr != nil && !utils.IsErrNoRow(tmpErr) {
- errMsg = "移动失败"
- err = errors.New("获取获取当前父级分类下的排序第一条的分类信息失败,Err:" + tmpErr.Error())
- return
- }
- //如果该分类下存在其他分类,且第一个其他分类的排序等于0,那么需要调整排序
- if firstClassify != nil && firstClassify.ChartClassifyId > 0 && firstClassify.Sort == 0 {
- updateSortStr := ` sort + 1 `
- _ = data_manage.UpdateChartClassifySortByParentIdAndSource(parentClassifyId, firstClassify.ChartClassifyId-1, 0, updateSortStr, source)
- //该分类下的所有图表也需要+1
- _ = data_manage.UpdateChartInfoSortByClassifyIdV2(parentClassifyId, 0, 0, updateSortStr)
- } else {
- //如果该分类下存在图表,且第一个图表的排序等于0,那么需要调整排序
- firstEdb, tErr := data_manage.GetFirstChartInfoByClassifyId(parentClassifyId)
- if tErr != nil && !utils.IsErrNoRow(tErr) {
- errMsg = "移动失败"
- err = errors.New("获取获取当前父级分类下的排序第一条的分类信息失败,Err:" + tErr.Error())
- return
- }
- //如果该分类下存在其他分类,且第一个其他分类的排序等于0,那么需要调整排序
- if firstEdb != nil && firstEdb.ChartClassifyId > 0 && firstEdb.Sort == 0 {
- updateSortStr := ` sort + 1 `
- _ = data_manage.UpdateChartInfoSortByClassifyIdV2(parentClassifyId, 0, firstEdb.ChartInfoId-1, updateSortStr)
- _ = data_manage.UpdateChartClassifySortByParentIdAndSource(parentClassifyId, 0, 0, updateSortStr, source)
- }
- }
- chartClassifyInfo.Sort = 0 //那就是排在第一位
- chartClassifyInfo.ModifyTime = time.Now()
- updateCol = append(updateCol, "Sort", "ModifyTime")
- }
- //更新
- if len(updateCol) > 0 {
- err = chartClassifyInfo.Update(updateCol)
- if err != nil {
- errMsg = "移动失败"
- err = errors.New("修改失败,Err:" + err.Error())
- return
- }
- //更新对应分类的root_id和层级
- if oldParentId != parentClassifyId {
- if len(classifyIds) > 0 {
- levelStep := chartClassifyInfo.Level - oldLevel
- err = data_manage.UpdateEdbClassifyChildByParentClassifyId(classifyIds, chartClassifyInfo.RootId, levelStep)
- if err != nil {
- errMsg = "移动失败"
- err = errors.New("更新子分类失败,Err:" + err.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 {
- _ = data_manage.UpdateChartClassifySortByParentIdAndSource(parentClassifyId, prevClassify.ChartClassifyId, prevClassify.Sort, updateSortStr, source)
- } else {
- _ = data_manage.UpdateChartClassifySortByParentIdAndSource(parentClassifyId, 0, prevSort, updateSortStr, source)
- }
- //变更图表
- if prevChartInfo != nil {
- //变更兄弟节点的排序
- _ = data_manage.UpdateChartInfoSortByClassifyIdV2(parentClassifyId, prevSort, prevChartInfo.ChartInfoId, updateSortStr)
- } else {
- _ = data_manage.UpdateChartInfoSortByClassifyIdV2(parentClassifyId, prevSort, 0, updateSortStr)
- }
- } else {
- //如果下一个兄弟的排序权重正好是上个兄弟节点的下一层,那么需要再加一层了
- if nextSort-prevSort == 1 {
- //变更兄弟节点的排序
- updateSortStr := `sort + 1`
- //变更分类
- if prevClassify != nil {
- _ = data_manage.UpdateChartClassifySortByParentIdAndSource(parentClassifyId, prevClassify.ChartClassifyId, prevSort, updateSortStr, source)
- } else {
- _ = data_manage.UpdateChartClassifySortByParentIdAndSource(parentClassifyId, 0, prevSort, updateSortStr, source)
- }
- //变更图表
- if prevChartInfo != nil {
- //变更兄弟节点的排序
- _ = data_manage.UpdateChartInfoSortByClassifyIdV2(parentClassifyId, prevSort, prevChartInfo.ChartInfoId, updateSortStr)
- } else {
- _ = data_manage.UpdateChartInfoSortByClassifyIdV2(parentClassifyId, prevSort, 0, 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 = GetChartClassifyMaxSort(parentClassifyId, source)
- 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 := data_manage.GetFirstChartClassifyByParentId(parentClassifyId)
- if tmpErr != nil && !utils.IsErrNoRow(tmpErr) {
- errMsg = "移动失败"
- err = errors.New("获取获取当前父级分类下的排序第一条的分类信息失败,Err:" + tmpErr.Error())
- return
- }
- //如果该分类下存在其他分类,且第一个其他分类的排序等于0,那么需要调整排序
- if firstClassify != nil && firstClassify.ChartClassifyId > 0 && firstClassify.Sort == 0 {
- updateSortStr := ` sort + 1 `
- _ = data_manage.UpdateChartClassifySortByParentIdAndSource(parentClassifyId, firstClassify.ChartClassifyId-1, 0, updateSortStr, source)
- //该分类下的所有图表也需要+1
- _ = data_manage.UpdateChartInfoSortByClassifyIdV2(parentClassifyId, 0, 0, updateSortStr)
- } else {
- //如果该分类下存在图表,且第一个图表的排序等于0,那么需要调整排序
- firstEdb, tErr := data_manage.GetFirstChartInfoByClassifyId(parentClassifyId)
- if tErr != nil && !utils.IsErrNoRow(tErr) {
- errMsg = "移动失败"
- err = errors.New("获取获取当前父级分类下的排序第一条的分类信息失败,Err:" + tErr.Error())
- return
- }
- //如果该分类下存在其他分类,且第一个其他分类的排序等于0,那么需要调整排序
- if firstEdb != nil && firstEdb.ChartClassifyId > 0 && firstEdb.Sort == 0 {
- updateSortStr := ` sort + 1 `
- _ = data_manage.UpdateChartInfoSortByClassifyIdV2(parentClassifyId, 0, firstEdb.ChartInfoId-1, updateSortStr)
- _ = data_manage.UpdateChartClassifySortByParentIdAndSource(parentClassifyId, 0, 0, updateSortStr, source)
- }
- }
- 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
- }
- func moveChart(parentChartClassifyInfo, chartClassifyInfo, prevClassify, nextClassify *data_manage.ChartClassify, chartInfo, prevChartInfo, nextChartInfo *data_manage.ChartInfo, parentClassifyId, prevSort, nextSort, source int) (err error, errMsg string) {
- updateCol := make([]string, 0)
- // 移动对象为分类, 判断分类是否存在
- if chartClassifyInfo != nil {
- oldParentId := chartClassifyInfo.ParentId
- oldLevel := chartClassifyInfo.Level
- var classifyIds []int
- if oldParentId != parentClassifyId {
- //更新子分类对应的level
- childList, e, m := GetChildChartClassifyByClassifyId(chartClassifyInfo.ChartClassifyId)
- if e != nil {
- errMsg = "移动失败"
- err = errors.New("查询子分类失败,Err:" + e.Error() + m)
- return
- }
- if len(childList) > 0 {
- for _, v := range childList {
- if v.ChartClassifyId == chartClassifyInfo.ChartClassifyId {
- continue
- }
- classifyIds = append(classifyIds, v.ChartClassifyId)
- }
- }
- }
- //判断上级id是否一致,如果不一致的话,那么需要移动该分类层级
- if chartClassifyInfo.ParentId != parentClassifyId && parentClassifyId != 0 {
- if chartClassifyInfo.Level != parentChartClassifyInfo.Level+1 { //禁止层级调整
- errMsg = "移动失败"
- err = errors.New("不支持目录层级变更")
- return
- }
- chartClassifyInfo.ParentId = parentChartClassifyInfo.ChartClassifyId
- chartClassifyInfo.RootId = parentChartClassifyInfo.RootId
- chartClassifyInfo.Level = parentChartClassifyInfo.Level + 1
- chartClassifyInfo.ModifyTime = time.Now()
- updateCol = append(updateCol, "ParentId", "RootId", "Level", "ModifyTime")
- } else if chartClassifyInfo.ParentId != parentClassifyId && parentClassifyId == 0 {
- errMsg = "移动失败"
- err = errors.New("不支持目录层级变更")
- return
- }
- if prevSort > 0 {
- //如果是移动在两个兄弟节点之间
- if nextSort > 0 {
- //下一个兄弟节点
- //如果上一个兄弟与下一个兄弟的排序权重是一致的,那么需要将下一个兄弟(以及下个兄弟的同样排序权重)的排序权重+2,自己变成上一个兄弟的排序权重+1
- if prevSort == nextSort || prevSort == chartClassifyInfo.Sort {
- //变更兄弟节点的排序
- updateSortStr := `sort + 2`
- //变更分类
- if prevClassify != nil {
- _ = data_manage.UpdateChartClassifySortByParentIdAndSource(parentClassifyId, prevClassify.ChartClassifyId, prevClassify.Sort, updateSortStr, source)
- } else {
- _ = data_manage.UpdateChartClassifySortByParentIdAndSource(parentClassifyId, 0, prevSort, updateSortStr, source)
- }
- //变更图表
- if prevChartInfo != nil {
- //变更兄弟节点的排序
- _ = data_manage.UpdateChartInfoSortByClassifyIdV2(parentClassifyId, prevSort, prevChartInfo.ChartInfoId, updateSortStr)
- } else {
- _ = data_manage.UpdateChartInfoSortByClassifyIdV2(parentClassifyId, prevSort, 0, updateSortStr)
- }
- } else {
- //如果下一个兄弟的排序权重正好是上个兄弟节点的下一层,那么需要再加一层了
- if nextSort-prevSort == 1 {
- //变更兄弟节点的排序
- updateSortStr := `sort + 1`
- //变更分类
- if prevClassify != nil {
- _ = data_manage.UpdateChartClassifySortByParentIdAndSource(parentClassifyId, prevClassify.ChartClassifyId, prevSort, updateSortStr, source)
- } else {
- _ = data_manage.UpdateChartClassifySortByParentIdAndSource(parentClassifyId, 0, prevSort, updateSortStr, source)
- }
- //变更图表
- if prevChartInfo != nil {
- //变更兄弟节点的排序
- _ = data_manage.UpdateChartInfoSortByClassifyIdV2(parentClassifyId, prevSort, prevChartInfo.ChartInfoId, updateSortStr)
- } else {
- _ = data_manage.UpdateChartInfoSortByClassifyIdV2(parentClassifyId, prevSort, 0, updateSortStr)
- }
- }
- }
- }
- chartClassifyInfo.Sort = prevSort + 1
- chartClassifyInfo.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 = GetChartClassifyMaxSort(parentClassifyId, source)
- if err != nil {
- errMsg = "移动失败"
- err = errors.New("查询组内排序信息失败,Err:" + err.Error())
- return
- }
- chartClassifyInfo.Sort = maxSort + 1 //那就是排在组内最后一位
- chartClassifyInfo.ModifyTime = time.Now()
- updateCol = append(updateCol, "Sort", "ModifyTime")
- } else {
- // 拖动到父级分类的第一位
- firstClassify, tmpErr := data_manage.GetFirstChartClassifyByParentId(parentClassifyId)
- if tmpErr != nil && !utils.IsErrNoRow(tmpErr) {
- errMsg = "移动失败"
- err = errors.New("获取获取当前父级分类下的排序第一条的分类信息失败,Err:" + tmpErr.Error())
- return
- }
- //如果该分类下存在其他分类,且第一个其他分类的排序等于0,那么需要调整排序
- if firstClassify != nil && firstClassify.ChartClassifyId > 0 && firstClassify.Sort == 0 {
- updateSortStr := ` sort + 1 `
- _ = data_manage.UpdateChartClassifySortByParentIdAndSource(parentClassifyId, firstClassify.ChartClassifyId-1, 0, updateSortStr, source)
- //该分类下的所有图表也需要+1
- _ = data_manage.UpdateChartInfoSortByClassifyIdV2(parentClassifyId, 0, 0, updateSortStr)
- } else {
- //如果该分类下存在图表,且第一个图表的排序等于0,那么需要调整排序
- firstEdb, tErr := data_manage.GetFirstChartInfoByClassifyId(parentClassifyId)
- if tErr != nil && !utils.IsErrNoRow(tErr) {
- errMsg = "移动失败"
- err = errors.New("获取获取当前父级分类下的排序第一条的分类信息失败,Err:" + tErr.Error())
- return
- }
- //如果该分类下存在其他分类,且第一个其他分类的排序等于0,那么需要调整排序
- if firstEdb != nil && firstEdb.ChartClassifyId > 0 && firstEdb.Sort == 0 {
- updateSortStr := ` sort + 1 `
- _ = data_manage.UpdateChartInfoSortByClassifyIdV2(parentClassifyId, 0, firstEdb.ChartInfoId-1, updateSortStr)
- _ = data_manage.UpdateChartClassifySortByParentIdAndSource(parentClassifyId, 0, 0, updateSortStr, source)
- }
- }
- chartClassifyInfo.Sort = 0 //那就是排在第一位
- chartClassifyInfo.ModifyTime = time.Now()
- updateCol = append(updateCol, "Sort", "ModifyTime")
- }
- //更新
- if len(updateCol) > 0 {
- err = chartClassifyInfo.Update(updateCol)
- if err != nil {
- errMsg = "移动失败"
- err = errors.New("修改失败,Err:" + err.Error())
- return
- }
- //更新对应分类的root_id和层级
- if oldParentId != parentClassifyId {
- if len(classifyIds) > 0 {
- levelStep := chartClassifyInfo.Level - oldLevel
- err = data_manage.UpdateEdbClassifyChildByParentClassifyId(classifyIds, chartClassifyInfo.RootId, levelStep)
- if err != nil {
- errMsg = "移动失败"
- err = errors.New("更新子分类失败,Err:" + err.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 {
- _ = data_manage.UpdateChartClassifySortByParentIdAndSource(parentClassifyId, prevClassify.ChartClassifyId, prevClassify.Sort, updateSortStr, source)
- } else {
- _ = data_manage.UpdateChartClassifySortByParentIdAndSource(parentClassifyId, 0, prevSort, updateSortStr, source)
- }
- //变更图表
- if prevChartInfo != nil {
- //变更兄弟节点的排序
- _ = data_manage.UpdateChartInfoSortByClassifyIdV2(parentClassifyId, prevSort, prevChartInfo.ChartInfoId, updateSortStr)
- } else {
- _ = data_manage.UpdateChartInfoSortByClassifyIdV2(parentClassifyId, prevSort, 0, updateSortStr)
- }
- } else {
- //如果下一个兄弟的排序权重正好是上个兄弟节点的下一层,那么需要再加一层了
- if nextSort-prevSort == 1 {
- //变更兄弟节点的排序
- updateSortStr := `sort + 1`
- //变更分类
- if prevClassify != nil {
- _ = data_manage.UpdateChartClassifySortByParentIdAndSource(parentClassifyId, prevClassify.ChartClassifyId, prevSort, updateSortStr, source)
- } else {
- _ = data_manage.UpdateChartClassifySortByParentIdAndSource(parentClassifyId, 0, prevSort, updateSortStr, source)
- }
- //变更图表
- if prevChartInfo != nil {
- //变更兄弟节点的排序
- _ = data_manage.UpdateChartInfoSortByClassifyIdV2(parentClassifyId, prevSort, prevChartInfo.ChartInfoId, updateSortStr)
- } else {
- _ = data_manage.UpdateChartInfoSortByClassifyIdV2(parentClassifyId, prevSort, 0, 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 = GetChartClassifyMaxSort(parentClassifyId, source)
- 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 := data_manage.GetFirstChartClassifyByParentId(parentClassifyId)
- if tmpErr != nil && !utils.IsErrNoRow(tmpErr) {
- errMsg = "移动失败"
- err = errors.New("获取获取当前父级分类下的排序第一条的分类信息失败,Err:" + tmpErr.Error())
- return
- }
- //如果该分类下存在其他分类,且第一个其他分类的排序等于0,那么需要调整排序
- if firstClassify != nil && firstClassify.ChartClassifyId > 0 && firstClassify.Sort == 0 {
- updateSortStr := ` sort + 1 `
- _ = data_manage.UpdateChartClassifySortByParentIdAndSource(parentClassifyId, firstClassify.ChartClassifyId-1, 0, updateSortStr, source)
- //该分类下的所有图表也需要+1
- _ = data_manage.UpdateChartInfoSortByClassifyIdV2(parentClassifyId, 0, 0, updateSortStr)
- } else {
- //如果该分类下存在图表,且第一个图表的排序等于0,那么需要调整排序
- firstEdb, tErr := data_manage.GetFirstChartInfoByClassifyId(parentClassifyId)
- if tErr != nil && !utils.IsErrNoRow(tErr) {
- errMsg = "移动失败"
- err = errors.New("获取获取当前父级分类下的排序第一条的分类信息失败,Err:" + tErr.Error())
- return
- }
- //如果该分类下存在其他分类,且第一个其他分类的排序等于0,那么需要调整排序
- if firstEdb != nil && firstEdb.ChartClassifyId > 0 && firstEdb.Sort == 0 {
- updateSortStr := ` sort + 1 `
- _ = data_manage.UpdateChartInfoSortByClassifyIdV2(parentClassifyId, 0, firstEdb.ChartInfoId-1, updateSortStr)
- _ = data_manage.UpdateChartClassifySortByParentIdAndSource(parentClassifyId, 0, 0, updateSortStr, source)
- }
- }
- 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
- }
- // moveChartClassify 移动图表分类
- func moveChartClassify(parentChartClassifyInfo, chartClassifyInfo, prevClassify, nextClassify *data_manage.ChartClassify, prevChartInfo, nextChartInfo *data_manage.ChartInfo, parentClassifyId, prevSort, nextSort, source int) (err error, errMsg string) {
- updateCol := make([]string, 0)
- oldParentId := chartClassifyInfo.ParentId
- oldLevel := chartClassifyInfo.Level
- var classifyIds []int
- if oldParentId != parentClassifyId {
- //更新子分类对应的level
- childList, e, m := GetChildChartClassifyByClassifyId(chartClassifyInfo.ChartClassifyId)
- if e != nil {
- errMsg = "移动失败"
- err = errors.New("查询子分类失败,Err:" + e.Error() + m)
- return
- }
- if len(childList) > 0 {
- for _, v := range childList {
- if v.ChartClassifyId == chartClassifyInfo.ChartClassifyId {
- continue
- }
- classifyIds = append(classifyIds, v.ChartClassifyId)
- }
- }
- }
- //判断上级id是否一致,如果不一致的话,那么需要移动该分类层级
- if chartClassifyInfo.ParentId != parentClassifyId && parentClassifyId != 0 {
- if chartClassifyInfo.Level != parentChartClassifyInfo.Level+1 { //禁止层级调整
- errMsg = "移动失败"
- err = errors.New("不支持目录层级变更")
- return
- }
- chartClassifyInfo.ParentId = parentChartClassifyInfo.ChartClassifyId
- chartClassifyInfo.RootId = parentChartClassifyInfo.RootId
- chartClassifyInfo.Level = parentChartClassifyInfo.Level + 1
- chartClassifyInfo.ModifyTime = time.Now()
- updateCol = append(updateCol, "ParentId", "RootId", "Level", "ModifyTime")
- } else if chartClassifyInfo.ParentId != parentClassifyId && parentClassifyId == 0 {
- errMsg = "移动失败"
- err = errors.New("不支持目录层级变更")
- return
- }
- if prevSort > 0 {
- //如果是移动在两个兄弟节点之间
- if nextSort > 0 {
- //下一个兄弟节点
- //如果上一个兄弟与下一个兄弟的排序权重是一致的,那么需要将下一个兄弟(以及下个兄弟的同样排序权重)的排序权重+2,自己变成上一个兄弟的排序权重+1
- if prevSort == nextSort || prevSort == chartClassifyInfo.Sort {
- //变更兄弟节点的排序
- updateSortStr := `sort + 2`
- //变更分类
- if prevClassify != nil {
- _ = data_manage.UpdateChartClassifySortByParentIdAndSource(parentClassifyId, prevClassify.ChartClassifyId, prevClassify.Sort, updateSortStr, source)
- } else {
- _ = data_manage.UpdateChartClassifySortByParentIdAndSource(parentClassifyId, 0, prevSort, updateSortStr, source)
- }
- //变更图表
- if prevChartInfo != nil {
- //变更兄弟节点的排序
- _ = data_manage.UpdateChartInfoSortByClassifyIdV2(parentClassifyId, prevSort, prevChartInfo.ChartInfoId, updateSortStr)
- } else {
- _ = data_manage.UpdateChartInfoSortByClassifyIdV2(parentClassifyId, prevSort, 0, updateSortStr)
- }
- } else {
- //如果下一个兄弟的排序权重正好是上个兄弟节点的下一层,那么需要再加一层了
- if nextSort-prevSort == 1 {
- //变更兄弟节点的排序
- updateSortStr := `sort + 1`
- //变更分类
- if prevClassify != nil {
- _ = data_manage.UpdateChartClassifySortByParentIdAndSource(parentClassifyId, prevClassify.ChartClassifyId, prevSort, updateSortStr, source)
- } else {
- _ = data_manage.UpdateChartClassifySortByParentIdAndSource(parentClassifyId, 0, prevSort, updateSortStr, source)
- }
- //变更图表
- if prevChartInfo != nil {
- //变更兄弟节点的排序
- _ = data_manage.UpdateChartInfoSortByClassifyIdV2(parentClassifyId, prevSort, prevChartInfo.ChartInfoId, updateSortStr)
- } else {
- _ = data_manage.UpdateChartInfoSortByClassifyIdV2(parentClassifyId, prevSort, 0, updateSortStr)
- }
- }
- }
- }
- chartClassifyInfo.Sort = prevSort + 1
- chartClassifyInfo.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 = GetChartClassifyMaxSort(parentClassifyId, source)
- if err != nil {
- errMsg = "移动失败"
- err = errors.New("查询组内排序信息失败,Err:" + err.Error())
- return
- }
- chartClassifyInfo.Sort = maxSort + 1 //那就是排在组内最后一位
- chartClassifyInfo.ModifyTime = time.Now()
- updateCol = append(updateCol, "Sort", "ModifyTime")
- } else {
- // 拖动到父级分类的第一位
- firstClassify, tmpErr := data_manage.GetFirstChartClassifyByParentId(parentClassifyId)
- if tmpErr != nil && !utils.IsErrNoRow(tmpErr) {
- errMsg = "移动失败"
- err = errors.New("获取获取当前父级分类下的排序第一条的分类信息失败,Err:" + tmpErr.Error())
- return
- }
- //如果该分类下存在其他分类,且第一个其他分类的排序等于0,那么需要调整排序
- if firstClassify != nil && firstClassify.ChartClassifyId > 0 && firstClassify.Sort == 0 {
- updateSortStr := ` sort + 1 `
- _ = data_manage.UpdateChartClassifySortByParentIdAndSource(parentClassifyId, firstClassify.ChartClassifyId-1, 0, updateSortStr, source)
- //该分类下的所有图表也需要+1
- _ = data_manage.UpdateChartInfoSortByClassifyIdV2(parentClassifyId, 0, 0, updateSortStr)
- } else {
- //如果该分类下存在图表,且第一个图表的排序等于0,那么需要调整排序
- firstEdb, tErr := data_manage.GetFirstChartInfoByClassifyId(parentClassifyId)
- if tErr != nil && !utils.IsErrNoRow(tErr) {
- errMsg = "移动失败"
- err = errors.New("获取获取当前父级分类下的排序第一条的分类信息失败,Err:" + tErr.Error())
- return
- }
- //如果该分类下存在其他分类,且第一个其他分类的排序等于0,那么需要调整排序
- if firstEdb != nil && firstEdb.ChartClassifyId > 0 && firstEdb.Sort == 0 {
- updateSortStr := ` sort + 1 `
- _ = data_manage.UpdateChartInfoSortByClassifyIdV2(parentClassifyId, 0, firstEdb.ChartInfoId-1, updateSortStr)
- _ = data_manage.UpdateChartClassifySortByParentIdAndSource(parentClassifyId, 0, 0, updateSortStr, source)
- }
- }
- chartClassifyInfo.Sort = 0 //那就是排在第一位
- chartClassifyInfo.ModifyTime = time.Now()
- updateCol = append(updateCol, "Sort", "ModifyTime")
- }
- oldClassifyIdPath := chartClassifyInfo.ChartClassifyIdPath
- oldClassifyNamePath := chartClassifyInfo.ChartClassifyNamePath
- newClassifyNamePath := fmt.Sprint(chartClassifyInfo.ChartClassifyNamePath, `|`, chartClassifyInfo.ChartClassifyName)
- newClassifyIdPath := fmt.Sprint(chartClassifyInfo.ChartClassifyIdPath, `,`, chartClassifyInfo.ChartClassifyId)
- //更新
- if len(updateCol) > 0 {
- err = chartClassifyInfo.Update(updateCol)
- if err != nil {
- errMsg = "移动失败"
- err = errors.New("修改失败,Err:" + err.Error())
- return
- }
- //更新对应分类的root_id和层级
- if oldParentId != parentClassifyId {
- if len(classifyIds) > 0 {
- levelStep := chartClassifyInfo.Level - oldLevel
- err = data_manage.UpdateEdbClassifyChildByParentClassifyId(classifyIds, chartClassifyInfo.RootId, levelStep)
- if err != nil {
- errMsg = "移动失败"
- err = errors.New("更新子分类失败,Err:" + err.Error())
- return
- }
- }
- // 更改了上级分类,那么需要同步去更改自己的分类全路径
- tmpErr := data_manage.UpdateChartClassifyNameAndNamePathByOldClassifyIdPath(oldClassifyIdPath, newClassifyIdPath, oldClassifyNamePath, newClassifyNamePath)
- if tmpErr != nil {
- utils.FileLog.Error(fmt.Sprintf("更新图表分类全路径失败,分类id:%d;失败原因Err:%s", chartClassifyInfo.ChartClassifyId, tmpErr.Error()))
- }
- }
- }
- return
- }
- func GetChildChartClassifyByClassifyId(targetClassifyId int) (targetList []*data_manage.ChartClassifyIdItems, err error, errMsg string) {
- //判断是否是挂在顶级目录下
- targetClassify, err := data_manage.GetChartClassifyById(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, chart_classify_id asc`
- tmpList, err := data_manage.GetChartClassifyByRootIdLevel(targetClassify.RootId, targetClassify.Source, 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.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(data_manage.ChartClassifyIdItems)
- targetItem.ChartClassifyId = v.ChartClassifyId
- targetItem.ParentId = v.ParentId
- targetItem.RootId = v.RootId
- targetItem.UniqueCode = v.UniqueCode
- targetItem.Level = v.Level
- targetItem.ChartClassifyName = v.ChartClassifyName
- targetItem.IsJoinPermission = v.IsJoinPermission
- targetList = append(targetList, targetItem)
- }
- }
- }
- return
- }
- func GetChartClassifyMaxSort(parentId, source int) (maxSort int, err error) {
- //获取该层级下最大的排序数
- classifyMaxSort, err := data_manage.GetChartClassifyMaxSort(parentId, source)
- if err != nil {
- return
- }
- maxSort = classifyMaxSort
- edbMaxSort, err := data_manage.GetChartInfoMaxSortByClassifyId(parentId)
- if err != nil {
- return
- }
- if maxSort < edbMaxSort {
- maxSort = edbMaxSort
- }
- return
- }
- // GetChartClassifyTreeRecursive 递归获取分类树形结构
- func GetChartClassifyTreeRecursive(list []*data_manage.ChartClassifyItems, parentId int) []*data_manage.ChartClassifyItems {
- res := make([]*data_manage.ChartClassifyItems, 0)
- for _, v := range list {
- if v.ParentId == parentId {
- v.Children = GetChartClassifyTreeRecursive(list, v.ChartClassifyId)
- res = append(res, v)
- }
- }
- return res
- }
- // GetChartClassifyTreeRecursiveAndInsert
- // @Description: 递归获取分类树形结构,同时根据分类id插入对应的图表数据
- // @author: Roc
- // @datetime 2024-12-12 14:42:28
- // @param list []*data_manage.ChartClassifyItems
- // @param parentId int
- // @param chartClassifyItemListMap map[int][]*data_manage.ChartClassifyItems
- // @return []*data_manage.ChartClassifyItems
- func GetChartClassifyTreeRecursiveAndInsert(list []*data_manage.ChartClassifyItems, parentId int, chartClassifyItemListMap map[int][]*data_manage.ChartClassifyItems) []*data_manage.ChartClassifyItems {
- // 先获取树形结构
- list = GetChartClassifyTreeRecursive(list, parentId)
- // 将图表数据插入到分类树中
- InsertChartClassifyItemsToClassifyTree(list, chartClassifyItemListMap)
- return list
- }
- // InsertChartClassifyItemsToClassifyTree
- // @Description: 递归将图表数据插入到分类树中
- // @author: Roc
- // @datetime 2024-12-12 14:42:00
- // @param list []*data_manage.ChartClassifyItems
- // @param chartClassifyItemListMap map[int][]*data_manage.ChartClassifyItems
- // @return []*data_manage.ChartClassifyItems
- func InsertChartClassifyItemsToClassifyTree(list []*data_manage.ChartClassifyItems, chartClassifyItemListMap map[int][]*data_manage.ChartClassifyItems) []*data_manage.ChartClassifyItems {
- for k, v := range list {
- children := v.Children
- if children != nil {
- children = InsertChartClassifyItemsToClassifyTree(children, chartClassifyItemListMap)
- } else {
- children = make([]*data_manage.ChartClassifyItems, 0)
- }
- if chartClassifyItemList, ok := chartClassifyItemListMap[v.ChartClassifyId]; ok {
- children = append(children, chartClassifyItemList...)
- }
- list[k].Children = children
- }
- return list
- }
- // GetChartClassifyParentRecursive 根据子目录递归父级目录
- func GetChartClassifyParentRecursive(list []*data_manage.ChartClassifyItems, classifyId int) []*data_manage.ChartClassifyItems {
- res := make([]*data_manage.ChartClassifyItems, 0)
- for _, v := range list {
- if v.ChartClassifyId == classifyId {
- if v.ParentId > 0 {
- res = GetChartClassifyParentRecursive(list, v.ParentId)
- }
- res = append(res, v)
- }
- }
- return res
- }
- func GetChartClassifyChildIds(classifyId int, classifymap map[int]*data_manage.ChartClassifyItems, childIds *[]int) {
- for _, item := range classifymap {
- if item.ParentId == classifyId {
- *childIds = append(*childIds, item.ChartClassifyId)
- GetChartClassifyChildIds(item.ChartClassifyId, classifymap, childIds)
- }
- }
- }
- // GetAllChartChildClassifyByParentId
- // @Description: 根据上级id获取所有的下级id
- // @author: Roc
- // @datetime 2024-12-02 15:14:13
- // @param parentId int
- // @return targetList []*data_manage.EdbClassifyItems
- // @return err error
- // @return errMsg string
- func GetAllChartChildClassifyByParentId(parentId int) (targetList []*data_manage.ChartClassifyItems, err error, errMsg string) {
- //判断是否是挂在顶级目录下
- parentClassify, err := data_manage.GetChartClassifyById(parentId)
- 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, chart_classify_id asc`
- targetList, err = data_manage.GetChildChartClassifyListByParentId(fmt.Sprint(parentClassify.ChartClassifyIdPath, ",%"), orderStr)
- return
- }
|