1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546 |
- package data
- import (
- "errors"
- "eta/eta_api/models"
- "eta/eta_api/models/data_manage"
- "eta/eta_api/models/data_manage/cross_variety"
- "eta/eta_api/models/data_manage/excel"
- "eta/eta_api/models/system"
- "eta/eta_api/services/data/data_manage_permission"
- "eta/eta_api/services/data_stat"
- "eta/eta_api/utils"
- "fmt"
- "strconv"
- "time"
- )
- func EdbClassifyListMakeTree(allNode []*data_manage.EdbClassifyItems, node *data_manage.EdbClassifyItems) {
- childs, _ := edbClassifyHaveChild(allNode, node) //判断节点是否有子节点并返回
- allEdbInfo, _ := GetEdbClassifyEdbInfo()
- if len(childs) > 0 {
- node.Children = append(node.Children, childs[0:]...) //添加子节点
- for _, v := range childs { //查询子节点的子节点,并添加到子节点
- _, has := edbClassifyHaveChild(allNode, v)
- if has {
- EdbClassifyListMakeTree(allNode, v) //递归添加节点
- } else {
- edbInfoList := allEdbInfo[v.ClassifyId]
- v.Children = edbInfoList
- if v.Children == nil {
- childrenArr := make([]*data_manage.EdbClassifyItems, 0)
- v.Children = childrenArr
- }
- }
- }
- } else {
- childrenArr := make([]*data_manage.EdbClassifyItems, 0)
- node.Children = childrenArr
- }
- }
- func edbClassifyHaveChild(allNode []*data_manage.EdbClassifyItems, node *data_manage.EdbClassifyItems) (childs []*data_manage.EdbClassifyItems, yes bool) {
- for _, v := range allNode {
- if v.ParentId == node.ClassifyId {
- childs = append(childs, v)
- }
- }
- if len(childs) > 0 {
- yes = true
- }
- return
- }
- // GetClassifyTreeRecursive 递归获取分类树形结构
- func GetClassifyTreeRecursive(list []*data_manage.EdbClassifyItems, parentId int) []*data_manage.EdbClassifyItems {
- res := make([]*data_manage.EdbClassifyItems, 0)
- for _, v := range list {
- if v.ParentId == parentId {
- v.Children = GetClassifyTreeRecursive(list, v.ClassifyId)
- res = append(res, v)
- }
- }
- return res
- }
- func GetFullClassifyByClassifyId(targetClassifyId int) (targetList []*data_manage.EdbClassifyIdItems, err error, errMsg string) {
- //判断是否是挂在顶级目录下
- targetClassify, err := data_manage.GetEdbClassifyById(targetClassifyId)
- if err != nil {
- if err.Error() == utils.ErrNoRow() {
- errMsg = "当前分类不存在"
- err = errors.New(errMsg)
- return
- }
- errMsg = "获取失败"
- err = errors.New("获取分类信息失败,Err:" + err.Error())
- return
- }
- if targetClassify.ParentId == 0 {
- targetItem := new(data_manage.EdbClassifyIdItems)
- targetItem.ClassifyId = targetClassify.ClassifyId
- targetItem.ParentId = targetClassify.ParentId
- targetItem.RootId = targetClassify.RootId
- targetItem.UniqueCode = targetClassify.UniqueCode
- targetItem.Level = targetClassify.Level
- targetItem.ClassifyName = targetClassify.ClassifyName
- targetItem.ClassifyName = targetClassify.ClassifyName
- targetItem.IsJoinPermission = targetClassify.IsJoinPermission
- targetList = append(targetList, targetItem)
- return
- }
- tmpList, err := data_manage.GetEdbClassifyByRootIdLevel(targetClassify.RootId, targetClassify.ClassifyType, "")
- 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.ClassifyId == targetClassify.ClassifyId {
- idMap[v.ClassifyId] = struct{}{}
- idMap[v.ParentId] = struct{}{}
- }
- }
- for _, v := range tmpList {
- if _, ok := idMap[v.ClassifyId]; ok {
- idMap[v.ParentId] = struct{}{}
- }
- }
- for _, v := range tmpList {
- if _, ok := idMap[v.ClassifyId]; ok {
- targetItem := new(data_manage.EdbClassifyIdItems)
- targetItem.ClassifyId = v.ClassifyId
- targetItem.ParentId = v.ParentId
- targetItem.RootId = v.RootId
- targetItem.UniqueCode = v.UniqueCode
- targetItem.Level = v.Level
- targetItem.ClassifyName = v.ClassifyName
- targetItem.IsJoinPermission = v.IsJoinPermission
- targetList = append(targetList, targetItem)
- }
- }
- }
- return
- }
- // GetFullClassifyByRootId 查询指标列表里的分类信息
- func GetFullClassifyByRootId(targetClassify *data_manage.EdbClassify, tmpList []*data_manage.EdbClassifyItems) (targetList []*data_manage.EdbClassifyIdItems, err error, errMsg string) {
- if targetClassify.ParentId == 0 {
- targetItem := new(data_manage.EdbClassifyIdItems)
- targetItem.ClassifyId = targetClassify.ClassifyId
- targetItem.ParentId = targetClassify.ParentId
- targetItem.RootId = targetClassify.RootId
- targetItem.UniqueCode = targetClassify.UniqueCode
- targetItem.Level = targetClassify.Level
- targetItem.ClassifyName = targetClassify.ClassifyName
- targetItem.ClassifyName = targetClassify.ClassifyName
- targetItem.IsJoinPermission = targetClassify.IsJoinPermission
- targetList = append(targetList, targetItem)
- return
- }
- idMap := make(map[int]struct{})
- if len(tmpList) > 0 {
- for _, v := range tmpList {
- if v.ClassifyId == targetClassify.ClassifyId {
- idMap[v.ClassifyId] = struct{}{}
- idMap[v.ParentId] = struct{}{}
- }
- }
- for _, v := range tmpList {
- if _, ok := idMap[v.ClassifyId]; ok {
- idMap[v.ParentId] = struct{}{}
- }
- }
- for _, v := range tmpList {
- if _, ok := idMap[v.ClassifyId]; ok {
- targetItem := new(data_manage.EdbClassifyIdItems)
- targetItem.ClassifyId = v.ClassifyId
- targetItem.ParentId = v.ParentId
- targetItem.RootId = v.RootId
- targetItem.UniqueCode = v.UniqueCode
- targetItem.Level = v.Level
- targetItem.ClassifyName = v.ClassifyName
- targetItem.IsJoinPermission = v.IsJoinPermission
- targetList = append(targetList, targetItem)
- }
- }
- }
- return
- }
- func GetChildClassifyByClassifyId(targetClassifyId int) (targetList []*data_manage.EdbClassifyIdItems, err error, errMsg string) {
- //判断是否是挂在顶级目录下
- targetClassify, err := data_manage.GetEdbClassifyById(targetClassifyId)
- if err != nil {
- if err.Error() == utils.ErrNoRow() {
- errMsg = "当前分类不存在"
- err = errors.New(errMsg)
- return
- }
- errMsg = "获取失败"
- err = errors.New("获取分类信息失败,Err:" + err.Error())
- return
- }
- orderStr := ` order by level asc, sort asc, classify_id asc`
- tmpList, err := data_manage.GetEdbClassifyByRootIdLevel(targetClassify.RootId, targetClassify.ClassifyType, orderStr)
- if err != nil && err.Error() != utils.ErrNoRow() {
- errMsg = "获取失败"
- err = errors.New("获取数据失败,Err:" + err.Error())
- return
- }
- idMap := make(map[int]struct{})
- if len(tmpList) > 0 {
- for _, v := range tmpList {
- if v.ClassifyId == targetClassify.ClassifyId {
- idMap[v.ClassifyId] = struct{}{}
- }
- }
- for _, v := range tmpList {
- if _, ok := idMap[v.ParentId]; ok {
- idMap[v.ClassifyId] = struct{}{}
- }
- }
- for _, v := range tmpList {
- if _, ok := idMap[v.ClassifyId]; ok {
- targetItem := new(data_manage.EdbClassifyIdItems)
- targetItem.ClassifyId = v.ClassifyId
- targetItem.ParentId = v.ParentId
- targetItem.RootId = v.RootId
- targetItem.UniqueCode = v.UniqueCode
- targetItem.Level = v.Level
- targetItem.ClassifyName = v.ClassifyName
- targetItem.IsJoinPermission = v.IsJoinPermission
- targetList = append(targetList, targetItem)
- }
- }
- }
- return
- }
- func GetEdbClassifyEdbInfo() (result map[int][]*data_manage.EdbClassifyItems, err error) {
- cMap := make(map[int][]*data_manage.EdbClassifyItems)
- items, err := data_manage.GetEdbInfoAll(0)
- if err != nil {
- return
- }
- for _, v := range items {
- childrenArr := make([]*data_manage.EdbClassifyItems, 0)
- v.Children = childrenArr
- if items, ok := cMap[v.ClassifyId]; ok {
- items = append(items, v)
- cMap[v.ClassifyId] = items
- } else {
- infoList := make([]*data_manage.EdbClassifyItems, 0)
- infoList = append(infoList, v)
- cMap[v.ClassifyId] = infoList
- }
- }
- result = cMap
- return
- }
- func EdbClassifyItemsMakeTree(allNode []*data_manage.EdbClassifyItems, node *data_manage.EdbClassifyItems) {
- childs, _ := edbClassifyHaveChild(allNode, node) //判断节点是否有子节点并返回
- if len(childs) > 0 {
- node.Children = append(node.Children, childs[0:]...) //添加子节点
- for _, v := range childs { //查询子节点的子节点,并添加到子节点
- _, has := edbClassifyHaveChild(allNode, v)
- if has {
- EdbClassifyItemsMakeTree(allNode, v) //递归添加节点
- } else {
- childrenArr := make([]*data_manage.EdbClassifyItems, 0)
- v.Children = childrenArr
- }
- }
- } else {
- childrenArr := make([]*data_manage.EdbClassifyItems, 0)
- node.Children = childrenArr
- }
- }
- // GetEdbClassifyListByAdminId 根据账户类型获取分类id集合
- func GetEdbClassifyListByAdminId(adminId int64) (classifyIdList []string, err error) {
- list, err := models.GetEdbdataClassify(adminId)
- if err != nil {
- return
- }
- for _, parent := range list {
- if parent.Child != nil {
- for _, classify := range parent.Child {
- classifyIdList = append(classifyIdList, fmt.Sprint(classify.ClassifyId))
- }
- }
- }
- return
- }
- // GetEdbClassifyNameListByAdminId 根据账户类型获取分类名称集合
- func GetEdbClassifyNameListByAdminId(adminId int64) (classifyNameList []string, edbDataClassifyMap map[string]*models.EdbdataClassify, err error) {
- list, err := models.GetEdbdataClassify(adminId)
- edbDataClassifyMap = make(map[string]*models.EdbdataClassify)
- if err != nil {
- return
- }
- for _, parent := range list {
- if parent.Child != nil {
- for _, classify := range parent.Child {
- classifyNameList = append(classifyNameList, classify.ClassifyName)
- edbDataClassifyMap[classify.ClassifyName] = classify
- }
- }
- }
- return
- }
- // AddEdbClassify 添加指标分类
- func AddEdbClassify(classifyName string, parentId, level int, classifyType uint8, sysUserId int, sysUserName, lang string) (classifyInfo *data_manage.EdbClassify, err error, errMsg string) {
- // 校验分类名称相同的数量
- {
- var count int
- switch lang {
- case utils.EnLangVersion:
- count, err = data_manage.GetEdbClassifyEnCount(classifyName, parentId, classifyType)
- default:
- count, err = data_manage.GetEdbClassifyCount(classifyName, parentId, classifyType)
- }
- if err != nil {
- errMsg = `判断名称是否已存在失败`
- return
- }
- if count > 0 {
- errMsg = `分类名称已存在,请重新输入`
- err = errors.New(errMsg)
- return
- }
- }
- // 层级校验
- if level > 6 {
- errMsg = `最高只支持添加6级分类`
- return
- }
- //获取该层级下最大的排序数
- maxSort, err := GetEdbClassifyMaxSort(parentId, classifyType)
- if err != nil {
- errMsg = "获取失败"
- err = errors.New("查询排序信息失败,Err:" + err.Error())
- return
- }
- //查询顶级rootId
- rootId := 0
- if parentId > 0 {
- parentClassify, tErr := data_manage.GetEdbClassifyById(parentId)
- if tErr != nil {
- if tErr.Error() == utils.ErrNoRow() {
- errMsg = "父级分类不存在"
- err = errors.New(errMsg)
- return
- }
- errMsg = "获取失败"
- err = errors.New("获取分类信息失败,Err:" + tErr.Error())
- return
- }
- rootId = parentClassify.RootId
- }
- timestamp := strconv.FormatInt(time.Now().UnixNano(), 10)
- classifyInfo = &data_manage.EdbClassify{
- //ClassifyId: 0,
- ClassifyType: classifyType,
- ClassifyName: classifyName,
- ClassifyNameEn: classifyName,
- ParentId: parentId,
- RootId: rootId,
- HasData: 0,
- CreateTime: time.Now(),
- ModifyTime: time.Now(),
- SysUserId: sysUserId,
- SysUserRealName: sysUserName,
- Level: level + 1,
- UniqueCode: utils.MD5(utils.DATA_PREFIX + "_" + timestamp),
- Sort: maxSort + 1,
- }
- newId, err := data_manage.AddEdbClassify(classifyInfo)
- if err != nil {
- errMsg = "保存分类失败"
- return
- }
- // 继承分类权限
- {
- source := 3
- if classifyType == 1 {
- source = 4
- }
- go data_manage_permission.InheritParentClassify(source, int(classifyType), int(newId), classifyInfo.ParentId, classifyInfo.ClassifyName)
- }
- if parentId == 0 { //一级目录的rootId等于自己本身
- classifyInfo.ClassifyId = int(newId)
- classifyInfo.RootId = int(newId)
- err = classifyInfo.Update([]string{"RootId"})
- if err != nil {
- errMsg = "更新分类失败"
- return
- }
- }
- return
- }
- // EditEdbClassify 编辑指标分类
- func EditEdbClassify(classifyId int, classifyName, lang string, sysUser *system.Admin) (err error, errMsg string) {
- item, err := data_manage.GetEdbClassifyById(classifyId)
- if err != nil {
- errMsg = `保存失败`
- return
- }
- // 权限校验
- {
- // 已授权分类id
- permissionClassifyIdList, tmpErr := data_manage_permission.GetUserEdbClassifyPermissionList(sysUser.AdminId, classifyId)
- if tmpErr != nil {
- errMsg = "保存失败"
- err = errors.New("获取已授权分类id数据失败,Err:" + tmpErr.Error())
- return
- }
- // 数据权限
- haveOperaAuth := data_manage_permission.CheckEdbClassifyPermissionByPermissionIdList(item.IsJoinPermission, item.ClassifyId, permissionClassifyIdList)
- if item.ClassifyType == 0 { // 普通指标
- button := GetEdbClassifyOpButton(sysUser, item.SysUserId, haveOperaAuth)
- if !button.OpButton {
- errMsg = "无操作权限"
- err = errors.New(errMsg)
- return
- }
- } else if item.ClassifyType == 1 { // 预测指标
- button := GetPredictEdbClassifyOpButton(sysUser, item.SysUserId, haveOperaAuth)
- if !button.OpButton {
- errMsg = "无操作权限"
- err = errors.New(errMsg)
- return
- }
- }
- }
- // 需要变更的字段
- updateCols := make([]string, 0)
- switch lang {
- case utils.EnLangVersion:
- // 名字相同,那么就直接返回
- if item.ClassifyNameEn == classifyName {
- return
- }
- // 判断名称是否已存在
- count, tmpErr := data_manage.GetEdbClassifyEnCount(classifyName, item.ParentId, item.ClassifyType)
- if tmpErr != nil {
- err = tmpErr
- errMsg = "判断名称是否已存在失败"
- return
- }
- if count > 0 {
- errMsg = "分类名称已存在,请重新输入"
- err = errors.New(errMsg)
- return
- }
- item.ClassifyNameEn = classifyName
- item.LastModifyUserId = sysUser.AdminId
- item.LastModifyUserRealName = sysUser.RealName
- updateCols = append(updateCols, "ClassifyNameEn", "LastModifyUserId", "LastModifyUserRealName")
- default:
- // 名字相同,那么就直接返回
- if item.ClassifyName == classifyName {
- return
- }
- // 判断名称是否已存在
- count, tmpErr := data_manage.GetEdbClassifyCount(classifyName, item.ParentId, item.ClassifyType)
- if tmpErr != nil {
- err = tmpErr
- errMsg = "判断名称是否已存在失败"
- return
- }
- if count > 0 {
- errMsg = "分类名称已存在,请重新输入"
- err = errors.New(errMsg)
- return
- }
- item.ClassifyName = classifyName
- item.LastModifyUserId = sysUser.AdminId
- item.LastModifyUserRealName = sysUser.RealName
- updateCols = append(updateCols, "ClassifyName", "LastModifyUserId", "LastModifyUserRealName")
- }
- // 修改数据
- if len(updateCols) > 0 {
- err = item.Update(updateCols)
- if err != nil {
- errMsg = "保存失败"
- }
- }
- return
- }
- // DeleteCheck 删除检测
- func DeleteCheck(classifyId, edbInfoId int, sysUser *system.Admin) (deleteStatus int, tipsMsg string, tableList []*data_manage.ExcelBaseInfo, err error, errMsg string) {
- //删除分类
- if classifyId > 0 && edbInfoId == 0 {
- // 查找分类
- item, tmpErr := data_manage.GetEdbClassifyById(classifyId)
- if tmpErr != nil {
- errMsg = `查找分类失败`
- err = tmpErr
- return
- }
- // 已授权分类id
- permissionClassifyIdList, tmpErr := data_manage_permission.GetUserEdbClassifyPermissionList(sysUser.AdminId, classifyId)
- if tmpErr != nil {
- errMsg = "删除检测失败"
- err = errors.New("获取已授权分类id数据失败,Err:" + tmpErr.Error())
- return
- }
- // 权限校验
- {
- // 数据权限
- haveOperaAuth := data_manage_permission.CheckEdbClassifyPermissionByPermissionIdList(item.IsJoinPermission, item.ClassifyId, permissionClassifyIdList)
- if item.ClassifyType == 0 { // 普通指标
- button := GetEdbClassifyOpButton(sysUser, item.SysUserId, haveOperaAuth)
- if !button.DeleteButton {
- errMsg = "无操作权限"
- err = errors.New(errMsg)
- return
- }
- } else if item.ClassifyType == 1 { // 预测指标
- button := GetPredictEdbClassifyOpButton(sysUser, item.SysUserId, haveOperaAuth)
- if !button.DeleteButton {
- errMsg = "无操作权限"
- err = errors.New(errMsg)
- return
- }
- }
- }
- //判断分类下,是否含有指标
- count, tmpErr := data_manage.GetEdbInfoCountByClassifyId(classifyId)
- if tmpErr != nil {
- errMsg = "删除失败"
- err = errors.New("分类下是否含有指标失败,Err:" + tmpErr.Error())
- return
- }
- if count > 0 {
- deleteStatus = 1
- tipsMsg = "若目录关联指标不可删除"
- }
- }
- if deleteStatus != 1 && edbInfoId == 0 {
- classifyCount, tmpErr := data_manage.GetClassifyCountByClassifyId(classifyId)
- if tmpErr != nil && tmpErr.Error() != utils.ErrNoRow() {
- errMsg = "删除失败"
- err = errors.New("分类下是否含有指标失败,Err:" + tmpErr.Error())
- return
- }
- if classifyCount > 0 {
- deleteStatus = 2
- tipsMsg = "确认删除当前目录及包含的子目录吗"
- }
- }
- //删除指标
- if edbInfoId > 0 {
- edbInfo, tmpErr := data_manage.GetEdbInfoById(edbInfoId)
- if tmpErr != nil {
- if tmpErr.Error() == utils.ErrNoRow() {
- errMsg = "指标已删除,请刷新页面"
- err = errors.New("指标不存在,Err:" + tmpErr.Error())
- return
- } else {
- errMsg = "删除失败"
- err = errors.New("删除失败,获取指标信息失败,Err:" + tmpErr.Error())
- return
- }
- }
- if edbInfo == nil {
- errMsg = "指标已删除,请刷新页面"
- return
- }
- //判断指标是否用于作图,如果用于作图,则不可删除
- chartCount, tmpErr := data_manage.GetChartEdbMappingCount(edbInfoId)
- if tmpErr != nil && tmpErr.Error() != utils.ErrNoRow() {
- errMsg = "删除失败"
- err = errors.New("判断指标是否被用于作图失败,Err:" + tmpErr.Error())
- return
- }
- if chartCount > 0 {
- deleteStatus = 3
- tipsMsg = "当前指标已用作画图,不可删除"
- return
- }
- //判断指标是否用于计算
- {
- calculateCount, tmpErr := data_manage.GetEdbInfoCalculateMappingCount(edbInfoId)
- if tmpErr != nil && tmpErr.Error() != utils.ErrNoRow() {
- errMsg = "删除失败"
- err = errors.New("判断指标是否被用于计算失败,GetEdbInfoCalculateCount Err:" + tmpErr.Error())
- return
- }
- if calculateCount > 0 {
- deleteStatus = 4
- tipsMsg = "当前指标已用作指标运算,不可删除"
- return
- }
- }
- //如果是普通指标,那么还需要判断是否被预测指标作为源指标
- if edbInfo.EdbInfoType == 0 {
- predictEdbInfoCount, tmpErr := data_manage.GetPredictEdbConfCount(edbInfoId)
- if tmpErr != nil && tmpErr.Error() != utils.ErrNoRow() {
- errMsg = "删除失败"
- err = errors.New("判断指标是否被用于预测指标失败,Err:" + tmpErr.Error())
- return
- }
- if predictEdbInfoCount > 0 {
- deleteStatus = 5
- tipsMsg = "当前指标已用作预测指标,不可删除"
- return
- }
- }
- // 判断指标是否用作表格引用
- {
- tableItems, tmpErr := excel.GetNoCustomAnalysisExcelEdbMapping(edbInfoId)
- if tmpErr != nil && tmpErr.Error() != utils.ErrNoRow() {
- errMsg = "删除失败"
- err = errors.New("判断指标是否用作表格引用,GetNoCustomAnalysisExcelEdbMappingCount Err:" + tmpErr.Error())
- return
- }
- //英文翻译:
- //1、当前指标已用作画图,不可删除:The current metric is in use for charting and cannot be deleted
- //2、当前指标已被表格引用,不可删除:The current metric is referenced by a table and cannot be deleted
- //3、删除失败:Deletion failed
- //4、知道了:Understood
- if len(tableItems) > 0 {
- deleteStatus = 6
- tipsMsg = "当前指标已被表格引用,不可删除"
- var excelIds []int
- for _, tableItem := range tableItems {
- if tableItem.ParentId > 0 {
- excelIds = append(excelIds, tableItem.ParentId)
- } else {
- excelIds = append(excelIds, tableItem.ExcelInfoId)
- }
- }
- tableList, tmpErr = excel.GetExcelBaseInfoByExcelInfoIdList(excelIds)
- if tmpErr != nil && tmpErr.Error() != utils.ErrNoRow() {
- errMsg = "删除失败"
- err = errors.New("判断指标是否用作表格引用,GetExcelBaseInfoByExcelInfoIdList Err:" + tmpErr.Error())
- return
- }
- return
- }
- }
- // 判断指标是否用作跨品种图表使用
- {
- // todo 如果绑定的标签未画图,则允许删除绑定
- // 查询跨品种的图表
- tagXList, tmpErr := cross_variety.GetChartInfoCrossVarietyByXEdbInfoId(edbInfoId)
- if tmpErr != nil && tmpErr.Error() != utils.ErrNoRow() {
- errMsg = "删除失败"
- err = errors.New("判断指标是否用作跨品种图表使用,GetChartInfoCrossVarietyByXEdbInfoId Err:" + tmpErr.Error())
- return
- }
- if len(tagXList) > 0 {
- deleteStatus = 7
- tipsMsg = "当前指标已添加到跨品种分析,不可删除"
- return
- }
- tagYList, tmpErr := cross_variety.GetChartInfoCrossVarietyByYEdbInfoId(edbInfoId)
- if tmpErr != nil && tmpErr.Error() != utils.ErrNoRow() {
- errMsg = "删除失败"
- err = errors.New("判断指标是否用作跨品种图表使用,GetChartInfoCrossVarietyByYEdbInfoId Err:" + tmpErr.Error())
- return
- }
- if len(tagYList) > 0 {
- deleteStatus = 7
- tipsMsg = "当前指标已添加到跨品种分析,不可删除"
- return
- }
- /*calculateCount, tmpErr := cross_variety.GetCountByEdbInfoId(edbInfoId)
- if tmpErr != nil && tmpErr.Error() != utils.ErrNoRow() {
- errMsg = "删除失败"
- err = errors.New("判断指标是否用作跨品种图表使用,GetCountByEdbInfoId Err:" + tmpErr.Error())
- return
- }
- if calculateCount > 0 {
- deleteStatus = 3
- tipsMsg = "当前指标已添加到跨品种分析,不可删除"
- return
- }*/
- }
- // 判断指标是否用于因子指标系列
- {
- ob := new(data_manage.FactorEdbSeriesMapping)
- cond := fmt.Sprintf(" AND %s = ?", ob.Cols().EdbInfoId)
- pars := make([]interface{}, 0)
- pars = append(pars, edbInfoId)
- count, e := ob.GetCountByCondition(cond, pars)
- if e != nil {
- errMsg = "删除失败"
- err = fmt.Errorf("获取指标是否用于系列失败, err: %v", e)
- return
- }
- if count > 0 {
- deleteStatus = 3
- tipsMsg = "当前指标已用于因子指标系列, 不可删除"
- return
- }
- }
- }
- return
- }
- // Delete 删除分类/指标
- func Delete(classifyId, edbInfoId int, sysUser *system.Admin, requestBody, requestUrl string) (nextItem *data_manage.EdbInfo, tableList []*data_manage.ExcelBaseInfo, err error, errMsg string) {
- //删除分类
- if classifyId > 0 && edbInfoId == 0 {
- // 查找分类
- item, tmpErr := data_manage.GetEdbClassifyById(classifyId)
- if tmpErr != nil {
- errMsg = `查找分类失败`
- err = tmpErr
- return
- }
- // 已授权分类id
- permissionClassifyIdList, tmpErr := data_manage_permission.GetUserEdbClassifyPermissionList(sysUser.AdminId, classifyId)
- if tmpErr != nil {
- errMsg = "删除失败"
- err = errors.New("获取已授权分类id数据失败,Err:" + tmpErr.Error())
- return
- }
- // 权限校验
- {
- // 数据权限
- haveOperaAuth := data_manage_permission.CheckEdbClassifyPermissionByPermissionIdList(item.IsJoinPermission, item.ClassifyId, permissionClassifyIdList)
- if item.ClassifyType == 0 { // 普通指标
- button := GetEdbClassifyOpButton(sysUser, item.SysUserId, haveOperaAuth)
- if !button.DeleteButton {
- errMsg = "无操作权限"
- err = errors.New(errMsg)
- return
- }
- } else if item.ClassifyType == 1 { // 预测指标
- button := GetPredictEdbClassifyOpButton(sysUser, item.SysUserId, haveOperaAuth)
- if !button.DeleteButton {
- errMsg = "无操作权限"
- err = errors.New(errMsg)
- return
- }
- }
- }
- //判断是否含有指标
- count, tmpErr := data_manage.GetEdbInfoCountByClassifyId(classifyId)
- if tmpErr != nil && tmpErr.Error() != utils.ErrNoRow() {
- errMsg = "删除失败"
- err = errors.New("分类下是否含有指标失败,Err:" + tmpErr.Error())
- return
- }
- if count > 0 {
- errMsg = "该目录下存在关联指标,不可删除"
- return
- }
- tmpErr = data_manage.DeleteEdbClassify(classifyId)
- if tmpErr != nil {
- errMsg = "删除失败"
- err = errors.New("删除失败,Err:" + tmpErr.Error())
- return
- }
- }
- //删除指标
- if edbInfoId > 0 {
- edbInfo, tmpErr := data_manage.GetEdbInfoById(edbInfoId)
- if tmpErr != nil {
- if tmpErr.Error() == utils.ErrNoRow() {
- errMsg = "指标已删除,请刷新页面"
- err = errors.New("指标不存在,Err:" + tmpErr.Error())
- return
- } else {
- errMsg = "删除失败"
- err = errors.New("删除失败,获取指标信息失败,Err:" + tmpErr.Error())
- return
- }
- }
- if edbInfo == nil {
- errMsg = "指标已删除,请刷新页面"
- return
- }
- //判断指标是否用于作图,如果用于作图,则不可删除
- chartCount, tmpErr := data_manage.GetChartEdbMappingCount(edbInfoId)
- if tmpErr != nil && tmpErr.Error() != utils.ErrNoRow() {
- errMsg = "删除失败"
- err = errors.New("判断指标是否被用于作图失败,Err:" + tmpErr.Error())
- return
- }
- if chartCount > 0 {
- errMsg = "当前指标已用作画图,不可删除"
- return
- }
- //如果是普通指标,那么还需要判断是否被预测指标作为源指标
- if edbInfo.EdbInfoType == 0 {
- predictEdbInfoCount, tmpErr := data_manage.GetPredictEdbConfCount(edbInfoId)
- if tmpErr != nil && tmpErr.Error() != utils.ErrNoRow() {
- errMsg = "删除失败"
- err = errors.New("判断指标是否被用于预测指标失败,Err:" + tmpErr.Error())
- return
- }
- if predictEdbInfoCount > 0 {
- errMsg = "当前指标已用作预测指标,不可删除"
- return
- }
- }
- //判断指标是否用作其他指标的计算
- {
- calculateCount, tmpErr := data_manage.GetEdbInfoCalculateMappingCount(edbInfoId)
- if tmpErr != nil && tmpErr.Error() != utils.ErrNoRow() {
- errMsg = "删除失败"
- err = errors.New("判断指标是否被用于计算失败,GetEdbInfoCalculateCount Err:" + tmpErr.Error())
- return
- }
- if calculateCount > 0 {
- errMsg = "当前指标已用作,指标运算,不可删除"
- return
- }
- }
- // 判断指标是否用作表格引用
- {
- tableItems, tmpErr := excel.GetNoCustomAnalysisExcelEdbMapping(edbInfoId)
- if tmpErr != nil && tmpErr.Error() != utils.ErrNoRow() {
- errMsg = "删除失败"
- err = errors.New("判断指标是否用作表格引用,GetNoCustomAnalysisExcelEdbMappingCount Err:" + tmpErr.Error())
- return
- }
- //英文翻译:
- //1、当前指标已用作画图,不可删除:The current metric is in use for charting and cannot be deleted
- //2、当前指标已被表格引用,不可删除:The current metric is referenced by a table and cannot be deleted
- //3、删除失败:Deletion failed
- //4、知道了:Understood
- if len(tableItems) > 0 {
- errMsg = "当前指标已被表格引用,不可删除"
- return
- }
- }
- // 判断指标是否用作跨品种图表使用
- {
- // todo 如果绑定的标签未画图,则允许删除绑定
- // 查询跨品种的图表
- tagXList, tmpErr := cross_variety.GetChartInfoCrossVarietyByXEdbInfoId(edbInfoId)
- if tmpErr != nil && tmpErr.Error() != utils.ErrNoRow() {
- errMsg = "删除失败"
- err = errors.New("判断指标是否用作跨品种图表使用,GetChartInfoCrossVarietyByXEdbInfoId Err:" + tmpErr.Error())
- return
- }
- if len(tagXList) > 0 {
- errMsg = "当前指标已添加到跨品种分析,不可删除"
- return
- }
- tagYList, tmpErr := cross_variety.GetChartInfoCrossVarietyByYEdbInfoId(edbInfoId)
- if tmpErr != nil && tmpErr.Error() != utils.ErrNoRow() {
- errMsg = "删除失败"
- err = errors.New("判断指标是否用作跨品种图表使用,GetChartInfoCrossVarietyByYEdbInfoId Err:" + tmpErr.Error())
- return
- }
- if len(tagYList) > 0 {
- errMsg = "当前指标已添加到跨品种分析,不可删除"
- return
- }
- /*calculateCount, tmpErr := cross_variety.GetCountByEdbInfoId(edbInfoId)
- if tmpErr != nil && tmpErr.Error() != utils.ErrNoRow() {
- errMsg = "删除失败"
- err = errors.New("判断指标是否用作跨品种图表使用,GetCountByEdbInfoId Err:" + tmpErr.Error())
- return
- }
- if calculateCount > 0 {
- errMsg = "当前指标已添加到跨品种分析,不可删除"
- return
- }*/
- }
- //真实删除
- tmpErr = data_manage.DeleteEdbInfoAndData(edbInfo.EdbInfoId, edbInfo.Source, edbInfo.SubSource)
- if tmpErr != nil {
- errMsg = "删除失败"
- err = errors.New("删除失败,Err:" + tmpErr.Error())
- return
- }
- // 如果是同花顺高频数据或类似数据, 还需要删除base_from_edb_mapping对应关系
- baseMappingOb := new(data_manage.BaseFromEdbMapping)
- {
- cond := fmt.Sprintf(" %s = ? AND %s = ? AND %s = ?", baseMappingOb.Cols().EdbCode, baseMappingOb.Cols().Source, baseMappingOb.Cols().SubSource)
- pars := make([]interface{}, 0)
- pars = append(pars, edbInfo.EdbCode, edbInfo.Source, edbInfo.SubSource)
- if e := baseMappingOb.RemoveByCondition(cond, pars); e != nil {
- errMsg = "删除失败"
- err = fmt.Errorf("删除源指标映射失败, %v", e)
- return
- }
- }
- go data_stat.AddEdbDeleteLog(edbInfo, sysUser)
- // 删除指标后的操作
- go handleByDelEdbInfo(edbInfo)
- // 返回下一个表格的信息
- {
- var condition string
- var pars []interface{}
- condition += " AND edb_info_type=? AND classify_id=? "
- pars = append(pars, edbInfo.EdbInfoType, edbInfo.ClassifyId)
- condition += " AND (sort>? OR (sort=? AND edb_info_id<?) ) "
- pars = append(pars, edbInfo.Sort, edbInfo.Sort, edbInfo.EdbInfoId)
- nextItem, tmpErr = data_manage.GetNextEdbInfoByCondition(condition, pars)
- if tmpErr != nil && tmpErr.Error() != utils.ErrNoRow() {
- errMsg = "删除失败"
- err = errors.New("获取下一级预测指标信息失败,Err:" + tmpErr.Error())
- return
- }
- // 如果没找到,那么查找下一个分类的第一个表格
- if nextItem == nil {
- currClassifyInfo, tmpErr := data_manage.GetEdbClassifyById(edbInfo.ClassifyId)
- if tmpErr != nil && tmpErr.Error() != utils.ErrNoRow() {
- errMsg = "删除失败"
- err = errors.New("获取当前预测指标分类信息失败,Err:" + tmpErr.Error())
- return
- }
- nextItem, err = data_manage.GetNextEdbInfo(edbInfo.ClassifyId, currClassifyInfo.Sort, int(currClassifyInfo.ClassifyType), edbInfo.EdbInfoType)
- if err != nil && err.Error() != utils.ErrNoRow() {
- errMsg = "删除失败"
- err = errors.New("获取下一级预测指标信息失败,Err:" + tmpErr.Error())
- return
- }
- }
- }
- //新增操作日志
- {
- edbLog := new(data_manage.EdbInfoLog)
- edbLog.EdbInfoId = edbInfo.EdbInfoId
- edbLog.SourceName = edbInfo.SourceName
- edbLog.Source = edbInfo.Source
- edbLog.EdbCode = edbInfo.EdbCode
- edbLog.EdbName = edbInfo.EdbName
- edbLog.ClassifyId = edbInfo.ClassifyId
- edbLog.SysUserId = sysUser.AdminId
- edbLog.SysUserRealName = sysUser.RealName
- edbLog.CreateTime = time.Now()
- edbLog.Content = requestBody
- edbLog.Status = "删除指标"
- edbLog.Method = requestUrl
- go data_manage.AddEdbInfoLog(edbLog)
- }
- // 删除es中的数据
- DeleteEdbInfoToEs(edbInfoId)
- }
- return
- }
- // MoveEdbClassify 移动指标分类
- func MoveEdbClassify(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
- //首先确定移动的对象是分类还是指标
- //判断上一个节点是分类还是指标
- //判断下一个节点是分类还是指标
- //同时更新分类目录下的分类sort和指标sort
- //更新当前移动的分类或者指标sort
- var parentEdbClassifyInfo *data_manage.EdbClassify
- if parentClassifyId > 0 {
- parentEdbClassifyInfo, err = data_manage.GetEdbClassifyById(parentClassifyId)
- if err != nil {
- errMsg = "移动失败"
- err = errors.New("获取上级分类信息失败,Err:" + err.Error())
- return
- }
- }
- //如果有传入 上一个兄弟节点分类id
- var (
- edbClassifyInfo *data_manage.EdbClassify
- prevClassify *data_manage.EdbClassify
- nextClassify *data_manage.EdbClassify
- edbInfo *data_manage.EdbInfo
- prevEdbInfo *data_manage.EdbInfo
- nextEdbInfo *data_manage.EdbInfo
- prevSort int
- nextSort int
- )
- // 移动对象为分类, 判断权限
- if edbInfoId == 0 {
- edbClassifyInfo, err = data_manage.GetEdbClassifyById(classifyId)
- if err != nil {
- if err.Error() == utils.ErrNoRow() {
- errMsg = "当前分类不存在"
- err = errors.New("获取分类信息失败,Err:" + err.Error())
- return
- }
- errMsg = "移动失败"
- err = errors.New("获取分类信息失败,Err:" + err.Error())
- return
- }
- if parentClassifyId > 0 && parentEdbClassifyInfo.Level == 6 {
- errMsg = "最高只支持添加6级分类"
- err = errors.New(errMsg)
- return
- }
- // 如果是移动目录, 那么校验一下父级目录下是否有重名目录
- exists, e := data_manage.GetEdbClassifyByParentIdAndName(parentClassifyId, edbClassifyInfo.ClassifyName, classifyId)
- if e != nil && e.Error() != utils.ErrNoRow() {
- errMsg = "移动失败"
- err = fmt.Errorf("获取父级分类下的同名分类失败, Err: %s", e.Error())
- return
- }
- if exists != nil {
- errMsg = "移动失败,分类名称已存在"
- return
- }
- // 权限校验
- {
- // 已授权分类id
- permissionClassifyIdList, tmpErr := data_manage_permission.GetUserEdbClassifyPermissionList(sysUser.AdminId, classifyId)
- if tmpErr != nil {
- errMsg = "移动失败"
- err = errors.New("获取已授权分类id数据失败,Err:" + tmpErr.Error())
- return
- }
- // 数据权限
- haveOperaAuth := data_manage_permission.CheckEdbClassifyPermissionByPermissionIdList(edbClassifyInfo.IsJoinPermission, edbClassifyInfo.ClassifyId, permissionClassifyIdList)
- if edbClassifyInfo.ClassifyType == 0 { // 普通指标
- button := GetEdbClassifyOpButton(sysUser, edbClassifyInfo.SysUserId, haveOperaAuth)
- if !button.MoveButton {
- errMsg = "无操作权限"
- err = errors.New(errMsg)
- return
- }
- } else if edbClassifyInfo.ClassifyType == 1 { // 预测指标
- button := GetPredictEdbClassifyOpButton(sysUser, edbClassifyInfo.SysUserId, haveOperaAuth)
- if !button.MoveButton {
- errMsg = "无操作权限"
- err = errors.New(errMsg)
- return
- }
- }
- }
- } else {
- edbInfo, err = data_manage.GetEdbInfoById(req.EdbInfoId)
- 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
- }
- var haveOperaAuth bool
- // 权限校验
- {
- haveOperaAuth, err = data_manage_permission.CheckEdbPermissionByEdbInfoId(edbInfo.EdbInfoId, edbInfo.ClassifyId, edbInfo.IsJoinPermission, sysUser.AdminId)
- if err != nil {
- errMsg = "移动失败"
- err = errors.New("获取指标权限信息失败,Err:" + err.Error())
- return
- }
- }
- // 移动权限校验
- button := GetEdbOpButton(sysUser, edbInfo.SysUserId, edbInfo.EdbType, edbInfo.EdbInfoType, haveOperaAuth)
- if !button.MoveButton {
- errMsg = "无操作权限"
- err = errors.New(errMsg)
- return
- }
- }
- if prevClassifyId > 0 {
- prevClassify, err = data_manage.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.Sort
- }
- if nextClassifyId > 0 {
- //下一个兄弟节点
- nextClassify, err = data_manage.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.Sort
- }
- err, errMsg = moveEdbClassify(parentEdbClassifyInfo, edbClassifyInfo, prevClassify, nextClassify, edbInfo, prevEdbInfo, nextEdbInfo, parentClassifyId, prevSort, nextSort, classifyType)
- return
- }
- // moveEdbClassify 移动指标分类
- func moveEdbClassify(parentEdbClassifyInfo, edbClassifyInfo, prevClassify, nextClassify *data_manage.EdbClassify, edbInfo, prevEdbInfo, nextEdbInfo *data_manage.EdbInfo, parentClassifyId int, prevSort, nextSort int, classifyType uint8) (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 != parentEdbClassifyInfo.Level+1 { //禁止层级调整
- errMsg = "移动失败"
- err = errors.New("不支持目录层级变更")
- return
- }
- edbClassifyInfo.ParentId = parentEdbClassifyInfo.ClassifyId
- 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`
- //变更分类
- if prevClassify != nil {
- _ = data_manage.UpdateEdbClassifySortByParentId(parentClassifyId, prevClassify.ClassifyId, prevClassify.Sort, updateSortStr, classifyType)
- } else {
- _ = data_manage.UpdateEdbClassifySortByParentId(parentClassifyId, 0, prevSort, updateSortStr, classifyType)
- }
- //变更指标
- if prevEdbInfo != nil {
- //变更兄弟节点的排序
- _ = data_manage.UpdateEdbInfoSortByClassifyId(parentClassifyId, prevSort, prevEdbInfo.EdbInfoId, updateSortStr)
- } else {
- _ = data_manage.UpdateEdbInfoSortByClassifyId(parentClassifyId, prevSort, 0, updateSortStr)
- }
- } else {
- //如果下一个兄弟的排序权重正好是上个兄弟节点的下一层,那么需要再加一层了
- if nextSort-prevSort == 1 {
- //变更兄弟节点的排序
- updateSortStr := `sort + 1`
- //变更分类
- if prevClassify != nil {
- _ = data_manage.UpdateEdbClassifySortByParentId(parentClassifyId, prevClassify.ClassifyId, prevSort, updateSortStr, classifyType)
- } else {
- _ = data_manage.UpdateEdbClassifySortByParentId(parentClassifyId, 0, prevSort, updateSortStr, classifyType)
- }
- //变更指标
- if prevEdbInfo != nil {
- //变更兄弟节点的排序
- _ = data_manage.UpdateEdbInfoSortByClassifyId(parentClassifyId, prevSort, prevEdbInfo.EdbInfoId, updateSortStr)
- } else {
- _ = data_manage.UpdateEdbInfoSortByClassifyId(parentClassifyId, prevSort, 0, updateSortStr)
- }
- }
- }
- }
- edbClassifyInfo.Sort = prevSort + 1
- edbClassifyInfo.ModifyTime = time.Now()
- updateCol = append(updateCol, "Sort", "ModifyTime")
- } else if prevClassify == nil && nextClassify == nil && prevEdbInfo == nil && nextEdbInfo == nil && parentClassifyId > 0 {
- //处理只拖动到目录里,默认放到目录底部的情况
- var maxSort int
- maxSort, err = GetEdbClassifyMaxSort(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 := data_manage.GetFirstEdbClassifyByParentId(parentClassifyId)
- if tmpErr != nil && tmpErr.Error() != utils.ErrNoRow() {
- errMsg = "移动失败"
- err = errors.New("获取获取当前父级分类下的排序第一条的分类信息失败,Err:" + tmpErr.Error())
- return
- }
- //如果该分类下存在其他分类,且第一个其他分类的排序等于0,那么需要调整排序
- if firstClassify != nil && firstClassify.Sort == 0 {
- updateSortStr := ` sort + 1 `
- _ = data_manage.UpdateEdbClassifySortByParentId(parentClassifyId, firstClassify.ClassifyId-1, 0, updateSortStr, classifyType)
- //该分类下的所有指标也需要+1
- _ = data_manage.UpdateEdbInfoSortByClassifyId(parentClassifyId, 0, 0, updateSortStr)
- } else {
- //如果该分类下存在指标,且第一个指标的排序等于0,那么需要调整排序
- firstEdb, tErr := data_manage.GetFirstEdbInfoByClassifyId(parentClassifyId)
- if tErr != nil && tErr.Error() != utils.ErrNoRow() {
- errMsg = "移动失败"
- err = errors.New("获取获取当前父级分类下的排序第一条的分类信息失败,Err:" + tErr.Error())
- return
- }
- //如果该分类下存在其他分类,且第一个其他分类的排序等于0,那么需要调整排序
- if firstEdb != nil && firstEdb.Sort == 0 {
- updateSortStr := ` sort + 1 `
- _ = data_manage.UpdateEdbInfoSortByClassifyId(parentClassifyId, 0, firstEdb.EdbInfoId-1, updateSortStr)
- _ = data_manage.UpdateEdbClassifySortByParentId(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
- }
- //更新对应分类的root_id和层级
- if oldParentId != parentClassifyId {
- if len(classifyIds) > 0 {
- levelStep := edbClassifyInfo.Level - oldLevel
- err = data_manage.UpdateEdbClassifyChildByParentClassifyId(classifyIds, edbClassifyInfo.RootId, levelStep)
- if err != nil {
- errMsg = "移动失败"
- err = errors.New("更新子分类失败,Err:" + err.Error())
- return
- }
- }
- }
- }
- } else {
- if edbInfo == nil {
- errMsg = "当前指标不存在"
- err = errors.New(errMsg)
- return
- }
- //如果改变了分类,那么移动该指标数据
- if edbInfo.ClassifyId != parentClassifyId {
- edbInfo.ClassifyId = parentClassifyId
- edbInfo.ModifyTime = time.Now()
- updateCol = append(updateCol, "ClassifyId", "ModifyTime")
- }
- if prevSort > 0 {
- //如果是移动在两个兄弟节点之间
- if nextSort > 0 {
- //下一个兄弟节点
- //如果上一个兄弟与下一个兄弟的排序权重是一致的,那么需要将下一个兄弟(以及下个兄弟的同样排序权重)的排序权重+2,自己变成上一个兄弟的排序权重+1
- if prevSort == nextSort || prevSort == edbInfo.Sort {
- //变更兄弟节点的排序
- updateSortStr := `sort + 2`
- //变更分类
- if prevClassify != nil {
- _ = data_manage.UpdateEdbClassifySortByParentId(parentClassifyId, prevClassify.ClassifyId, prevClassify.Sort, updateSortStr, classifyType)
- } else {
- _ = data_manage.UpdateEdbClassifySortByParentId(parentClassifyId, 0, prevSort, updateSortStr, classifyType)
- }
- //变更指标
- if prevEdbInfo != nil {
- //变更兄弟节点的排序
- _ = data_manage.UpdateEdbInfoSortByClassifyId(parentClassifyId, prevSort, prevEdbInfo.EdbInfoId, updateSortStr)
- } else {
- _ = data_manage.UpdateEdbInfoSortByClassifyId(parentClassifyId, prevSort, 0, updateSortStr)
- }
- } else {
- //如果下一个兄弟的排序权重正好是上个兄弟节点的下一层,那么需要再加一层了
- if nextSort-prevSort == 1 {
- //变更兄弟节点的排序
- updateSortStr := `sort + 1`
- //变更分类
- if prevClassify != nil {
- _ = data_manage.UpdateEdbClassifySortByParentId(parentClassifyId, prevClassify.ClassifyId, prevSort, updateSortStr, classifyType)
- } else {
- _ = data_manage.UpdateEdbClassifySortByParentId(parentClassifyId, 0, prevSort, updateSortStr, classifyType)
- }
- //变更指标
- if prevEdbInfo != nil {
- //变更兄弟节点的排序
- _ = data_manage.UpdateEdbInfoSortByClassifyId(parentClassifyId, prevSort, prevEdbInfo.EdbInfoId, updateSortStr)
- } else {
- _ = data_manage.UpdateEdbInfoSortByClassifyId(parentClassifyId, prevSort, 0, updateSortStr)
- }
- }
- }
- }
- edbInfo.Sort = prevSort + 1
- edbInfo.ModifyTime = time.Now()
- updateCol = append(updateCol, "Sort", "ModifyTime")
- } else if prevClassify == nil && nextClassify == nil && prevEdbInfo == nil && nextEdbInfo == nil && parentClassifyId > 0 {
- //处理只拖动到目录里,默认放到目录底部的情况
- var maxSort int
- maxSort, err = GetEdbClassifyMaxSort(parentClassifyId, classifyType)
- if err != nil {
- errMsg = "移动失败"
- err = errors.New("查询组内排序信息失败,Err:" + err.Error())
- return
- }
- edbInfo.Sort = maxSort + 1 //那就是排在组内最后一位
- edbInfo.ModifyTime = time.Now()
- updateCol = append(updateCol, "Sort", "ModifyTime")
- } else {
- // 拖动到父级分类的第一位
- firstClassify, tmpErr := data_manage.GetFirstEdbClassifyByParentId(parentClassifyId)
- if tmpErr != nil && tmpErr.Error() != utils.ErrNoRow() {
- errMsg = "移动失败"
- err = errors.New("获取获取当前父级分类下的排序第一条的分类信息失败,Err:" + tmpErr.Error())
- return
- }
- //如果该分类下存在其他分类,且第一个其他分类的排序等于0,那么需要调整排序
- if firstClassify != nil && firstClassify.Sort == 0 {
- updateSortStr := ` sort + 1 `
- _ = data_manage.UpdateEdbClassifySortByParentId(parentClassifyId, firstClassify.ClassifyId-1, 0, updateSortStr, classifyType)
- //该分类下的所有指标也需要+1
- _ = data_manage.UpdateEdbInfoSortByClassifyId(parentClassifyId, 0, 0, updateSortStr)
- } else {
- //如果该分类下存在指标,且第一个指标的排序等于0,那么需要调整排序
- firstEdb, tErr := data_manage.GetFirstEdbInfoByClassifyId(parentClassifyId)
- if tErr != nil && tErr.Error() != utils.ErrNoRow() {
- errMsg = "移动失败"
- err = errors.New("获取获取当前父级分类下的排序第一条的分类信息失败,Err:" + tErr.Error())
- return
- }
- //如果该分类下存在其他分类,且第一个其他分类的排序等于0,那么需要调整排序
- if firstEdb != nil && firstEdb.Sort == 0 {
- updateSortStr := ` sort + 1 `
- _ = data_manage.UpdateEdbInfoSortByClassifyId(parentClassifyId, 0, firstEdb.EdbInfoId-1, updateSortStr)
- _ = data_manage.UpdateEdbClassifySortByParentId(parentClassifyId, 0, 0, updateSortStr, classifyType)
- }
- }
- edbInfo.Sort = 0 //那就是排在第一位
- edbInfo.ModifyTime = time.Now()
- updateCol = append(updateCol, "Sort", "ModifyTime")
- }
- //更新
- if len(updateCol) > 0 {
- err = edbInfo.Update(updateCol)
- if err != nil {
- errMsg = "移动失败"
- err = errors.New("修改失败,Err:" + err.Error())
- return
- }
- }
- }
- return
- }
- // GetEdbOpButton 获取ETA指标的操作权限
- func GetEdbOpButton(sysUser *system.Admin, belongUserId, edbType, edbInfoType int, haveOperaAuth bool) (button data_manage.EdbClassifyItemsButton) {
- // 没有数据权限就直接返回
- if !haveOperaAuth {
- return
- }
- //2、用户对于自己添加的分类,有权限编辑、移动和删除该分类;
- //3、ficc管理员和超管对所有分类有编辑、移动和删除权限;
- if sysUser.RoleTypeCode == utils.ROLE_TYPE_CODE_ADMIN || sysUser.RoleTypeCode == utils.ROLE_TYPE_CODE_FICC_ADMIN || sysUser.AdminId == belongUserId || sysUser.EdbPermission == 1 {
- button.AddButton = true
- button.OpButton = true
- button.DeleteButton = true
- button.MoveButton = true
- }
- // ETA指标的基础指标才允许插入值
- if edbInfoType == 0 && edbType == 1 {
- button.InsertNewDataButton = true
- }
- button.ShowEdbRelation = true
- button.ShowChartRelation = true
- return
- }
- // GetEdbClassifyOpButton 获取ETA指标分类的操作权限
- func GetEdbClassifyOpButton(sysUser *system.Admin, belongUserId int, haveOperaAuth bool) (button data_manage.EdbClassifyItemsButton) {
- // 没有数据权限就直接返回
- 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
- }
- // GetPredictEdbOpButton 获取ETA预测指标的操作权限
- func GetPredictEdbOpButton(sysUser *system.Admin, belongUserId int, haveOperaAuth bool) (button data_manage.EdbClassifyItemsButton) {
- // 没有数据权限就直接返回
- 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
- }
- button.ShowEdbRelation = true
- button.ShowChartRelation = true
- return
- }
- // GetPredictEdbClassifyOpButton 获取ETA预测指标分类的操作权限
- func GetPredictEdbClassifyOpButton(sysUser *system.Admin, belongUserId int, haveOperaAuth bool) (button data_manage.EdbClassifyItemsButton) {
- // 没有数据权限就直接返回
- 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
- }
- func GetEdbClassifyMaxSort(parentId int, classifyType uint8) (maxSort int, err error) {
- //获取该层级下最大的排序数
- classifyMaxSort, err := data_manage.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
- }
|