edb_classify.go 60 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747
  1. package data
  2. import (
  3. "errors"
  4. "eta_gn/eta_api/models"
  5. "eta_gn/eta_api/models/data_manage"
  6. "eta_gn/eta_api/models/data_manage/cross_variety"
  7. "eta_gn/eta_api/models/data_manage/excel"
  8. "eta_gn/eta_api/models/system"
  9. "eta_gn/eta_api/services/data/data_manage_permission"
  10. "eta_gn/eta_api/utils"
  11. "fmt"
  12. "strconv"
  13. "time"
  14. )
  15. func edbClassifyHaveChild(allNode []*data_manage.EdbClassifyItems, node *data_manage.EdbClassifyItems) (childs []*data_manage.EdbClassifyItems, yes bool) {
  16. for _, v := range allNode {
  17. if v.ParentId == node.ClassifyId {
  18. childs = append(childs, v)
  19. }
  20. }
  21. if len(childs) > 0 {
  22. yes = true
  23. }
  24. return
  25. }
  26. // GetClassifyTreeRecursive 递归获取分类树形结构
  27. func GetClassifyTreeRecursive(list []*data_manage.EdbClassifyItems, parentId int) []*data_manage.EdbClassifyItems {
  28. res := make([]*data_manage.EdbClassifyItems, 0)
  29. for _, v := range list {
  30. if v.ParentId == parentId {
  31. v.Children = GetClassifyTreeRecursive(list, v.ClassifyId)
  32. res = append(res, v)
  33. }
  34. }
  35. return res
  36. }
  37. // GetClassifyTreeRecursiveAndInsert
  38. // @Description: 递归获取分类树形结构,同时根据分类id插入对应的指标数据
  39. // @author: Roc
  40. // @datetime 2024-12-12 14:39:25
  41. // @param list []*data_manage.EdbClassifyItems
  42. // @param parentId int
  43. // @param edbClassifyItemListMap map[int][]*data_manage.EdbClassifyItems
  44. // @return []*data_manage.EdbClassifyItems
  45. func GetClassifyTreeRecursiveAndInsert(list []*data_manage.EdbClassifyItems, parentId int, edbClassifyItemListMap map[int][]*data_manage.EdbClassifyItems) []*data_manage.EdbClassifyItems {
  46. // 先获取树形结构
  47. list = GetClassifyTreeRecursive(list, parentId)
  48. // 将指标数据插入到分类树中
  49. InsertEdbClassifyItemsToClassifyTree(list, edbClassifyItemListMap)
  50. return list
  51. }
  52. // InsertEdbClassifyItemsToClassifyTree
  53. // @Description: 递归将指标数据插入到分类树中
  54. // @author: Roc
  55. // @datetime 2024-12-12 14:39:56
  56. // @param list []*data_manage.EdbClassifyItems
  57. // @param edbClassifyItemListMap map[int][]*data_manage.EdbClassifyItems
  58. // @return []*data_manage.EdbClassifyItems
  59. func InsertEdbClassifyItemsToClassifyTree(list []*data_manage.EdbClassifyItems, edbClassifyItemListMap map[int][]*data_manage.EdbClassifyItems) []*data_manage.EdbClassifyItems {
  60. for k, v := range list {
  61. children := v.Children
  62. if children != nil {
  63. children = InsertEdbClassifyItemsToClassifyTree(children, edbClassifyItemListMap)
  64. } else {
  65. children = make([]*data_manage.EdbClassifyItems, 0)
  66. }
  67. if edbClassifyItemList, ok := edbClassifyItemListMap[v.ClassifyId]; ok {
  68. children = append(children, edbClassifyItemList...)
  69. }
  70. list[k].Children = children
  71. }
  72. return list
  73. }
  74. func GetFullClassifyByClassifyId(targetClassifyId int) (targetList []*data_manage.EdbClassifyIdItems, err error, errMsg string) {
  75. //判断是否是挂在顶级目录下
  76. targetClassify, err := data_manage.GetEdbClassifyById(targetClassifyId)
  77. if err != nil {
  78. if utils.IsErrNoRow(err) {
  79. errMsg = "当前分类不存在"
  80. err = errors.New(errMsg)
  81. return
  82. }
  83. errMsg = "获取失败"
  84. err = errors.New("获取分类信息失败,Err:" + err.Error())
  85. return
  86. }
  87. if targetClassify.ParentId == 0 {
  88. targetItem := new(data_manage.EdbClassifyIdItems)
  89. targetItem.ClassifyId = targetClassify.ClassifyId
  90. targetItem.ParentId = targetClassify.ParentId
  91. targetItem.RootId = targetClassify.RootId
  92. targetItem.UniqueCode = targetClassify.UniqueCode
  93. targetItem.Level = targetClassify.Level
  94. targetItem.ClassifyName = targetClassify.ClassifyName
  95. targetItem.ClassifyNameEn = targetClassify.ClassifyNameEn
  96. targetItem.IsJoinPermission = targetClassify.IsJoinPermission
  97. targetList = append(targetList, targetItem)
  98. return
  99. }
  100. tmpList, err := data_manage.GetEdbClassifyByRootIdLevel(targetClassify.RootId, targetClassify.ClassifyType, "")
  101. if err != nil && !utils.IsErrNoRow(err) {
  102. errMsg = "获取失败"
  103. err = errors.New("获取数据失败,Err:" + err.Error())
  104. return
  105. }
  106. idMap := make(map[int]struct{})
  107. if len(tmpList) > 0 {
  108. for _, v := range tmpList {
  109. if v.ClassifyId == targetClassify.ClassifyId {
  110. idMap[v.ClassifyId] = struct{}{}
  111. idMap[v.ParentId] = struct{}{}
  112. }
  113. }
  114. for _, v := range tmpList {
  115. if _, ok := idMap[v.ClassifyId]; ok {
  116. idMap[v.ParentId] = struct{}{}
  117. }
  118. }
  119. for _, v := range tmpList {
  120. if _, ok := idMap[v.ClassifyId]; ok {
  121. targetItem := new(data_manage.EdbClassifyIdItems)
  122. targetItem.ClassifyId = v.ClassifyId
  123. targetItem.ParentId = v.ParentId
  124. targetItem.RootId = v.RootId
  125. targetItem.UniqueCode = v.UniqueCode
  126. targetItem.Level = v.Level
  127. targetItem.ClassifyName = v.ClassifyName
  128. targetItem.ClassifyNameEn = v.ClassifyNameEn
  129. targetItem.IsJoinPermission = v.IsJoinPermission
  130. targetList = append(targetList, targetItem)
  131. }
  132. }
  133. }
  134. return
  135. }
  136. // GetFullClassifyByRootId 查询指标列表里的分类信息
  137. func GetFullClassifyByRootId(targetClassify *data_manage.EdbClassify, tmpList []*data_manage.EdbClassifyItems) (targetList []*data_manage.EdbClassifyIdItems, err error, errMsg string) {
  138. if targetClassify.ParentId == 0 {
  139. targetItem := new(data_manage.EdbClassifyIdItems)
  140. targetItem.ClassifyId = targetClassify.ClassifyId
  141. targetItem.ParentId = targetClassify.ParentId
  142. targetItem.RootId = targetClassify.RootId
  143. targetItem.UniqueCode = targetClassify.UniqueCode
  144. targetItem.Level = targetClassify.Level
  145. targetItem.ClassifyName = targetClassify.ClassifyName
  146. targetItem.ClassifyName = targetClassify.ClassifyName
  147. targetItem.IsJoinPermission = targetClassify.IsJoinPermission
  148. targetList = append(targetList, targetItem)
  149. return
  150. }
  151. idMap := make(map[int]struct{})
  152. if len(tmpList) > 0 {
  153. for _, v := range tmpList {
  154. if v.ClassifyId == targetClassify.ClassifyId {
  155. idMap[v.ClassifyId] = struct{}{}
  156. idMap[v.ParentId] = struct{}{}
  157. }
  158. }
  159. for _, v := range tmpList {
  160. if _, ok := idMap[v.ClassifyId]; ok {
  161. idMap[v.ParentId] = struct{}{}
  162. }
  163. }
  164. for _, v := range tmpList {
  165. if _, ok := idMap[v.ClassifyId]; ok {
  166. targetItem := new(data_manage.EdbClassifyIdItems)
  167. targetItem.ClassifyId = v.ClassifyId
  168. targetItem.ParentId = v.ParentId
  169. targetItem.RootId = v.RootId
  170. targetItem.UniqueCode = v.UniqueCode
  171. targetItem.Level = v.Level
  172. targetItem.ClassifyName = v.ClassifyName
  173. targetItem.IsJoinPermission = v.IsJoinPermission
  174. targetList = append(targetList, targetItem)
  175. }
  176. }
  177. }
  178. return
  179. }
  180. func GetChildClassifyByClassifyId(targetClassifyId int) (targetList []*data_manage.EdbClassifyIdItems, err error, errMsg string) {
  181. //判断是否是挂在顶级目录下
  182. targetClassify, err := data_manage.GetEdbClassifyById(targetClassifyId)
  183. if err != nil {
  184. if utils.IsErrNoRow(err) {
  185. errMsg = "当前分类不存在"
  186. err = errors.New(errMsg)
  187. return
  188. }
  189. errMsg = "获取失败"
  190. err = errors.New("获取分类信息失败,Err:" + err.Error())
  191. return
  192. }
  193. orderStr := ` order by level asc, sort asc, classify_id asc`
  194. tmpList, err := data_manage.GetEdbClassifyByRootIdLevel(targetClassify.RootId, targetClassify.ClassifyType, orderStr)
  195. if err != nil && !utils.IsErrNoRow(err) {
  196. errMsg = "获取失败"
  197. err = errors.New("获取数据失败,Err:" + err.Error())
  198. return
  199. }
  200. idMap := make(map[int]struct{})
  201. if len(tmpList) > 0 {
  202. for _, v := range tmpList {
  203. if v.ClassifyId == targetClassify.ClassifyId {
  204. idMap[v.ClassifyId] = struct{}{}
  205. }
  206. }
  207. for _, v := range tmpList {
  208. if _, ok := idMap[v.ParentId]; ok {
  209. idMap[v.ClassifyId] = struct{}{}
  210. }
  211. }
  212. for _, v := range tmpList {
  213. if _, ok := idMap[v.ClassifyId]; ok {
  214. targetItem := new(data_manage.EdbClassifyIdItems)
  215. targetItem.ClassifyId = v.ClassifyId
  216. targetItem.ParentId = v.ParentId
  217. targetItem.RootId = v.RootId
  218. targetItem.UniqueCode = v.UniqueCode
  219. targetItem.Level = v.Level
  220. targetItem.ClassifyName = v.ClassifyName
  221. targetItem.IsJoinPermission = v.IsJoinPermission
  222. targetList = append(targetList, targetItem)
  223. }
  224. }
  225. }
  226. return
  227. }
  228. func EdbClassifyItemsMakeTree(allNode []*data_manage.EdbClassifyItems, node *data_manage.EdbClassifyItems) {
  229. childs, _ := edbClassifyHaveChild(allNode, node) //判断节点是否有子节点并返回
  230. if len(childs) > 0 {
  231. node.Children = append(node.Children, childs[0:]...) //添加子节点
  232. for _, v := range childs { //查询子节点的子节点,并添加到子节点
  233. _, has := edbClassifyHaveChild(allNode, v)
  234. if has {
  235. EdbClassifyItemsMakeTree(allNode, v) //递归添加节点
  236. } else {
  237. childrenArr := make([]*data_manage.EdbClassifyItems, 0)
  238. v.Children = childrenArr
  239. }
  240. }
  241. } else {
  242. childrenArr := make([]*data_manage.EdbClassifyItems, 0)
  243. node.Children = childrenArr
  244. }
  245. }
  246. // GetEdbClassifyListByAdminId 根据账户类型获取分类id集合
  247. func GetEdbClassifyListByAdminId(adminId int64) (classifyIdList []string, err error) {
  248. list, err := models.GetEdbdataClassify(adminId)
  249. if err != nil {
  250. return
  251. }
  252. for _, parent := range list {
  253. if parent.Child != nil {
  254. for _, classify := range parent.Child {
  255. classifyIdList = append(classifyIdList, fmt.Sprint(classify.ClassifyId))
  256. }
  257. }
  258. }
  259. return
  260. }
  261. // GetEdbClassifyNameListByAdminId 根据账户类型获取分类名称集合
  262. func GetEdbClassifyNameListByAdminId(adminId int64) (classifyNameList []string, edbDataClassifyMap map[string]*models.EdbdataClassify, err error) {
  263. list, err := models.GetEdbdataClassify(adminId)
  264. edbDataClassifyMap = make(map[string]*models.EdbdataClassify)
  265. if err != nil {
  266. return
  267. }
  268. for _, parent := range list {
  269. if parent.Child != nil {
  270. for _, classify := range parent.Child {
  271. classifyNameList = append(classifyNameList, classify.ClassifyName)
  272. edbDataClassifyMap[classify.ClassifyName] = classify
  273. }
  274. }
  275. }
  276. return
  277. }
  278. // AddEdbClassify 添加指标分类
  279. func AddEdbClassify(classifyName string, parentId, level int, classifyType uint8, sysUserId int, sysUserName, lang string) (classifyInfo *data_manage.EdbClassify, err error, errMsg string) {
  280. // 校验分类名称相同的数量
  281. {
  282. var count int
  283. switch lang {
  284. case utils.EnLangVersion:
  285. count, err = data_manage.GetEdbClassifyEnCount(classifyName, parentId, sysUserId, classifyType)
  286. default:
  287. count, err = data_manage.GetEdbClassifyCount(classifyName, 0, parentId, sysUserId, classifyType)
  288. }
  289. if err != nil {
  290. errMsg = `判断名称是否已存在失败`
  291. return
  292. }
  293. if count > 0 {
  294. errMsg = `分类名称已存在,请重新输入`
  295. err = errors.New(errMsg)
  296. return
  297. }
  298. }
  299. // 层级校验
  300. if level > utils.EdbClassifyMaxLevel {
  301. errMsg = fmt.Sprintf("最高只支持添加%d级分类", level)
  302. return
  303. }
  304. //获取该层级下最大的排序数
  305. maxSort, err := GetEdbClassifyMaxSort(parentId, classifyType)
  306. if err != nil {
  307. errMsg = "获取失败"
  308. err = errors.New("查询排序信息失败,Err:" + err.Error())
  309. return
  310. }
  311. //查询顶级rootId
  312. rootId := 0
  313. var classifyNamePath, classifyIdPath string
  314. if parentId > 0 {
  315. parentClassify, tErr := data_manage.GetEdbClassifyById(parentId)
  316. if tErr != nil {
  317. if utils.IsErrNoRow(tErr) {
  318. errMsg = "父级分类不存在"
  319. err = errors.New(errMsg)
  320. return
  321. }
  322. errMsg = "获取失败"
  323. err = errors.New("获取分类信息失败,Err:" + tErr.Error())
  324. return
  325. }
  326. rootId = parentClassify.RootId
  327. classifyNamePath = fmt.Sprint(parentClassify.ClassifyNamePath, "|", classifyName)
  328. classifyIdPath = fmt.Sprint(parentClassify.ClassifyIdPath, ",")
  329. } else {
  330. classifyNamePath = classifyName
  331. }
  332. timestamp := strconv.FormatInt(time.Now().UnixNano(), 10)
  333. classifyInfo = &data_manage.EdbClassify{
  334. //ClassifyId: 0,
  335. ClassifyType: classifyType,
  336. ClassifyName: classifyName,
  337. ClassifyNameEn: classifyName,
  338. ParentId: parentId,
  339. RootId: rootId,
  340. HasData: 0,
  341. CreateTime: time.Now(),
  342. ModifyTime: time.Now(),
  343. SysUserId: sysUserId,
  344. SysUserRealName: sysUserName,
  345. Level: level + 1,
  346. UniqueCode: utils.MD5(utils.DATA_PREFIX + "_" + timestamp),
  347. Sort: maxSort + 1,
  348. ClassifyNamePath: classifyNamePath,
  349. }
  350. err = data_manage.AddEdbClassify(classifyInfo)
  351. if err != nil {
  352. errMsg = "保存分类失败"
  353. return
  354. }
  355. // 更改分类id完整路径path
  356. updateCols := []string{"ClassifyIdPath"}
  357. classifyInfo.ClassifyIdPath = fmt.Sprint(classifyIdPath, classifyInfo.ClassifyId)
  358. if parentId == 0 { //一级目录的rootId等于自己本身
  359. classifyInfo.RootId = classifyInfo.ClassifyId
  360. updateCols = append(updateCols, "RootId")
  361. }
  362. err = classifyInfo.Update(updateCols)
  363. if err != nil {
  364. errMsg = "更新分类失败"
  365. return
  366. }
  367. // 继承分类权限
  368. {
  369. source := utils.EdbPermissionSourceBase
  370. if classifyType == 1 {
  371. source = utils.EdbPermissionSourcePredict
  372. }
  373. if classifyType == utils.EdbClassifyTypeCalculate {
  374. source = utils.EdbPermissionSourceCalculate
  375. }
  376. go data_manage_permission.InheritParentClassify(source, int(classifyType), classifyInfo.ClassifyId, classifyInfo.ParentId, classifyInfo.ClassifyName)
  377. }
  378. return
  379. }
  380. // EditEdbClassify 编辑指标分类
  381. func EditEdbClassify(classifyId int, classifyName, lang string, sysUser *system.Admin) (err error, errMsg string) {
  382. item, err := data_manage.GetEdbClassifyById(classifyId)
  383. if err != nil {
  384. errMsg = `保存失败`
  385. return
  386. }
  387. // 权限校验
  388. {
  389. // 已授权分类id
  390. permissionClassifyIdList, tmpErr := data_manage_permission.GetUserEdbClassifyPermissionList(sysUser.AdminId, classifyId)
  391. if tmpErr != nil {
  392. errMsg = "保存失败"
  393. err = errors.New("获取已授权分类id数据失败,Err:" + tmpErr.Error())
  394. return
  395. }
  396. // 数据权限
  397. haveOperaAuth := data_manage_permission.CheckEdbClassifyPermissionByPermissionIdList(item.IsJoinPermission, item.ClassifyId, permissionClassifyIdList)
  398. if item.ClassifyType == 0 { // 普通指标
  399. button := GetEdbClassifyOpButton(sysUser, item.SysUserId, haveOperaAuth)
  400. if !button.OpButton {
  401. errMsg = "无操作权限"
  402. err = errors.New(errMsg)
  403. return
  404. }
  405. } else if item.ClassifyType == 1 { // 预测指标
  406. button := GetPredictEdbClassifyOpButton(sysUser, item.SysUserId, haveOperaAuth)
  407. if !button.OpButton {
  408. errMsg = "无操作权限"
  409. err = errors.New(errMsg)
  410. return
  411. }
  412. }
  413. }
  414. // 需要变更的字段
  415. updateCols := make([]string, 0)
  416. // 旧完整路径 , 新的完整路径
  417. var oldClassifyNamePath, newClassifyNamePath string
  418. switch lang {
  419. case utils.EnLangVersion:
  420. // 名字相同,那么就直接返回
  421. if item.ClassifyNameEn == classifyName {
  422. return
  423. }
  424. // 判断名称是否已存在
  425. count, tmpErr := data_manage.GetEdbClassifyEnCount(classifyName, item.ParentId, sysUser.AdminId, item.ClassifyType)
  426. if tmpErr != nil {
  427. err = tmpErr
  428. errMsg = "判断名称是否已存在失败"
  429. return
  430. }
  431. if count > 0 {
  432. errMsg = "分类名称已存在,请重新输入"
  433. err = errors.New(errMsg)
  434. return
  435. }
  436. item.ClassifyNameEn = classifyName
  437. item.LastModifyUserId = sysUser.AdminId
  438. item.LastModifyUserRealName = sysUser.RealName
  439. updateCols = append(updateCols, "ClassifyNameEn", "LastModifyUserId", "LastModifyUserRealName")
  440. default:
  441. // 名字相同,那么就直接返回
  442. if item.ClassifyName == classifyName {
  443. return
  444. }
  445. //// 判断名称是否已存在
  446. count, tmpErr := data_manage.GetEdbClassifyCount(classifyName, item.ClassifyId, item.ParentId, sysUser.AdminId, item.ClassifyType)
  447. if tmpErr != nil {
  448. err = tmpErr
  449. errMsg = "判断名称是否已存在失败"
  450. return
  451. }
  452. if count > 0 {
  453. errMsg = "分类名称已存在,请重新输入"
  454. err = errors.New(errMsg)
  455. return
  456. }
  457. // 旧完整路径
  458. oldClassifyNamePath = item.ClassifyNamePath
  459. if item.ParentId > 0 {
  460. parentItem, tmpErr := data_manage.GetEdbClassifyById(item.ParentId)
  461. if tmpErr != nil {
  462. err = tmpErr
  463. return
  464. }
  465. newClassifyNamePath = fmt.Sprint(parentItem.ClassifyNamePath, "|", classifyName)
  466. } else {
  467. newClassifyNamePath = classifyName
  468. }
  469. // 更新自己的信息
  470. item.ClassifyName = classifyName
  471. item.ClassifyNamePath = newClassifyNamePath
  472. item.LastModifyUserId = sysUser.AdminId
  473. item.LastModifyUserRealName = sysUser.RealName
  474. updateCols = append(updateCols, "ClassifyName", "ClassifyNamePath", "LastModifyUserId", "LastModifyUserRealName")
  475. }
  476. // 修改数据
  477. if len(updateCols) > 0 {
  478. err = item.UpdateEdbClassifyNameAndNamePath(updateCols, oldClassifyNamePath, newClassifyNamePath)
  479. if err != nil {
  480. errMsg = "保存失败"
  481. }
  482. }
  483. return
  484. }
  485. // DeleteCheck 删除检测
  486. func DeleteCheck(classifyId, edbInfoId int, sysUser *system.Admin) (deleteStatus int, tipsMsg string, tableList []*data_manage.ExcelBaseInfo, err error, errMsg string) {
  487. // 自己以及子级的分类id列表
  488. selfAndSelfClassifyIdList := make([]int, 0)
  489. //删除分类
  490. if classifyId > 0 && edbInfoId == 0 {
  491. // 查找分类
  492. item, tmpErr := data_manage.GetEdbClassifyById(classifyId)
  493. if tmpErr != nil {
  494. errMsg = `查找分类失败`
  495. err = tmpErr
  496. return
  497. }
  498. // 已授权分类id
  499. permissionClassifyIdList, tmpErr := data_manage_permission.GetUserEdbClassifyPermissionList(sysUser.AdminId, classifyId)
  500. if tmpErr != nil {
  501. errMsg = "删除检测失败"
  502. err = errors.New("获取已授权分类id数据失败,Err:" + tmpErr.Error())
  503. return
  504. }
  505. // 权限校验
  506. {
  507. // 数据权限
  508. haveOperaAuth := data_manage_permission.CheckEdbClassifyPermissionByPermissionIdList(item.IsJoinPermission, item.ClassifyId, permissionClassifyIdList)
  509. if item.ClassifyType == 0 { // 普通指标
  510. button := GetEdbClassifyOpButton(sysUser, item.SysUserId, haveOperaAuth)
  511. if !button.DeleteButton {
  512. errMsg = "无操作权限"
  513. err = errors.New(errMsg)
  514. return
  515. }
  516. } else if item.ClassifyType == 1 { // 预测指标
  517. button := GetPredictEdbClassifyOpButton(sysUser, item.SysUserId, haveOperaAuth)
  518. if !button.DeleteButton {
  519. errMsg = "无操作权限"
  520. err = errors.New(errMsg)
  521. return
  522. }
  523. }
  524. }
  525. // 获取所有的分类(自己+所有子分类)
  526. allChildClassifyItemList, tmpErr, _ := GetAllChildClassifyByParentId(classifyId)
  527. if tmpErr != nil {
  528. err = tmpErr
  529. return
  530. }
  531. selfAndSelfClassifyIdList = append(selfAndSelfClassifyIdList, classifyId)
  532. for _, v := range allChildClassifyItemList {
  533. selfAndSelfClassifyIdList = append(selfAndSelfClassifyIdList, v.ClassifyId)
  534. }
  535. //判断分类下,是否含有指标
  536. count, tmpErr := data_manage.GetEdbInfoCountByClassifyIdList(selfAndSelfClassifyIdList)
  537. if tmpErr != nil {
  538. errMsg = "删除失败"
  539. err = errors.New("分类下是否含有指标失败,Err:" + tmpErr.Error())
  540. return
  541. }
  542. if count > 0 {
  543. deleteStatus = 1
  544. tipsMsg = "若目录关联指标不可删除"
  545. }
  546. }
  547. if deleteStatus != 1 && edbInfoId == 0 {
  548. classifyCount, tmpErr := data_manage.GetClassifyCountByClassifyId(classifyId)
  549. if tmpErr != nil && !utils.IsErrNoRow(tmpErr) {
  550. errMsg = "删除失败"
  551. err = errors.New("分类下是否含有指标失败,Err:" + tmpErr.Error())
  552. return
  553. }
  554. if classifyCount > 0 {
  555. deleteStatus = 2
  556. tipsMsg = "确认删除当前目录及包含的子目录吗"
  557. }
  558. }
  559. //删除指标
  560. if edbInfoId > 0 {
  561. edbInfo, tmpErr := data_manage.GetEdbInfoById(edbInfoId)
  562. if tmpErr != nil {
  563. if utils.IsErrNoRow(tmpErr) {
  564. errMsg = "指标已删除,请刷新页面"
  565. err = errors.New("指标不存在,Err:" + tmpErr.Error())
  566. return
  567. } else {
  568. errMsg = "删除失败"
  569. err = errors.New("删除失败,获取指标信息失败,Err:" + tmpErr.Error())
  570. return
  571. }
  572. }
  573. if edbInfo == nil {
  574. errMsg = "指标已删除,请刷新页面"
  575. return
  576. }
  577. // 公开状态校验
  578. switch edbInfo.PublicStatus {
  579. case utils.DataPublicSuccess:
  580. deleteStatus = 4
  581. tipsMsg = "当前指标已经公开,请撤销公开后删除"
  582. return
  583. case utils.DataPublicCommit:
  584. deleteStatus = 4
  585. tipsMsg = "当前指标已经提交申请公开,请撤销审批单后删除"
  586. return
  587. }
  588. //判断指标是否用于作图,如果用于作图,则不可删除
  589. chartCount, tmpErr := data_manage.GetChartEdbMappingCount(edbInfoId)
  590. if tmpErr != nil && !utils.IsErrNoRow(tmpErr) {
  591. errMsg = "删除失败"
  592. err = errors.New("判断指标是否被用于作图失败,Err:" + tmpErr.Error())
  593. return
  594. }
  595. if chartCount > 0 {
  596. deleteStatus = 3
  597. tipsMsg = "当前指标已用作画图,不可删除"
  598. return
  599. }
  600. //判断指标是否用于计算
  601. {
  602. calculateCount, tmpErr := data_manage.GetEdbInfoCalculateMappingCount(edbInfoId)
  603. if tmpErr != nil && !utils.IsErrNoRow(tmpErr) {
  604. errMsg = "删除失败"
  605. err = errors.New("判断指标是否被用于计算失败,GetEdbInfoCalculateCount Err:" + tmpErr.Error())
  606. return
  607. }
  608. if calculateCount > 0 {
  609. deleteStatus = 4
  610. tipsMsg = "当前指标已用作指标运算,不可删除"
  611. return
  612. }
  613. }
  614. //如果是普通指标,那么还需要判断是否被预测指标作为源指标
  615. if edbInfo.EdbInfoType == 0 {
  616. predictEdbInfoCount, tmpErr := data_manage.GetPredictEdbConfCount(edbInfoId)
  617. if tmpErr != nil && !utils.IsErrNoRow(tmpErr) {
  618. errMsg = "删除失败"
  619. err = errors.New("判断指标是否被用于预测指标失败,Err:" + tmpErr.Error())
  620. return
  621. }
  622. if predictEdbInfoCount > 0 {
  623. deleteStatus = 5
  624. tipsMsg = "当前指标已用作预测指标,不可删除"
  625. return
  626. }
  627. }
  628. // 判断指标是否用作表格引用
  629. {
  630. tableItems, tmpErr := excel.GetNoCustomAnalysisExcelEdbMapping(edbInfoId)
  631. if tmpErr != nil && !utils.IsErrNoRow(tmpErr) {
  632. errMsg = "删除失败"
  633. err = errors.New("判断指标是否用作表格引用,GetNoCustomAnalysisExcelEdbMappingCount Err:" + tmpErr.Error())
  634. return
  635. }
  636. //英文翻译:
  637. //1、当前指标已用作画图,不可删除:The current metric is in use for charting and cannot be deleted
  638. //2、当前指标已被表格引用,不可删除:The current metric is referenced by a table and cannot be deleted
  639. //3、删除失败:Deletion failed
  640. //4、知道了:Understood
  641. if len(tableItems) > 0 {
  642. deleteStatus = 6
  643. tipsMsg = "当前指标已被表格引用,不可删除"
  644. var excelIds []int
  645. for _, tableItem := range tableItems {
  646. if tableItem.ParentId > 0 {
  647. excelIds = append(excelIds, tableItem.ParentId)
  648. } else {
  649. excelIds = append(excelIds, tableItem.ExcelInfoId)
  650. }
  651. }
  652. tableList, tmpErr = excel.GetExcelBaseInfoByExcelInfoIdList(excelIds)
  653. if tmpErr != nil && !utils.IsErrNoRow(tmpErr) {
  654. errMsg = "删除失败"
  655. err = errors.New("判断指标是否用作表格引用,GetExcelBaseInfoByExcelInfoIdList Err:" + tmpErr.Error())
  656. return
  657. }
  658. return
  659. }
  660. }
  661. // 判断指标是否用作跨品种图表使用
  662. {
  663. // todo 如果绑定的标签未画图,则允许删除绑定
  664. // 查询跨品种的图表
  665. tagXList, tmpErr := cross_variety.GetChartInfoCrossVarietyByXEdbInfoId(edbInfoId)
  666. if tmpErr != nil && !utils.IsErrNoRow(tmpErr) {
  667. errMsg = "删除失败"
  668. err = errors.New("判断指标是否用作跨品种图表使用,GetChartInfoCrossVarietyByXEdbInfoId Err:" + tmpErr.Error())
  669. return
  670. }
  671. if len(tagXList) > 0 {
  672. deleteStatus = 7
  673. tipsMsg = "当前指标已添加到跨品种分析,不可删除"
  674. return
  675. }
  676. tagYList, tmpErr := cross_variety.GetChartInfoCrossVarietyByYEdbInfoId(edbInfoId)
  677. if tmpErr != nil && !utils.IsErrNoRow(tmpErr) {
  678. errMsg = "删除失败"
  679. err = errors.New("判断指标是否用作跨品种图表使用,GetChartInfoCrossVarietyByYEdbInfoId Err:" + tmpErr.Error())
  680. return
  681. }
  682. if len(tagYList) > 0 {
  683. deleteStatus = 7
  684. tipsMsg = "当前指标已添加到跨品种分析,不可删除"
  685. return
  686. }
  687. /*calculateCount, tmpErr := cross_variety.GetCountByEdbInfoId(edbInfoId)
  688. if tmpErr != nil && !utils.IsErrNoRow(tmpErr) {
  689. errMsg = "删除失败"
  690. err = errors.New("判断指标是否用作跨品种图表使用,GetCountByEdbInfoId Err:" + tmpErr.Error())
  691. return
  692. }
  693. if calculateCount > 0 {
  694. deleteStatus = 3
  695. tipsMsg = "当前指标已添加到跨品种分析,不可删除"
  696. return
  697. }*/
  698. }
  699. // 判断指标是否用于因子指标系列
  700. {
  701. ob := new(data_manage.FactorEdbSeriesMapping)
  702. cond := fmt.Sprintf(" AND %s = ?", ob.Cols().EdbInfoId)
  703. pars := make([]interface{}, 0)
  704. pars = append(pars, edbInfoId)
  705. count, e := ob.GetCountByCondition(cond, pars)
  706. if e != nil {
  707. errMsg = "删除失败"
  708. err = fmt.Errorf("获取指标是否用于系列失败, err: %v", e)
  709. return
  710. }
  711. if count > 0 {
  712. deleteStatus = 3
  713. tipsMsg = "当前指标已用于因子指标系列, 不可删除"
  714. return
  715. }
  716. }
  717. }
  718. return
  719. }
  720. // Delete 删除分类/指标
  721. func Delete(classifyId, edbInfoId int, sysUser *system.Admin, requestBody, requestUrl string) (nextItem *data_manage.EdbInfo, tableList []*data_manage.ExcelBaseInfo, err error, errMsg string) {
  722. // 自己以及子级的分类id列表
  723. selfAndSelfClassifyIdList := make([]int, 0)
  724. //删除分类
  725. if classifyId > 0 && edbInfoId == 0 {
  726. // 查找分类
  727. item, tmpErr := data_manage.GetEdbClassifyById(classifyId)
  728. if tmpErr != nil {
  729. errMsg = `查找分类失败`
  730. err = tmpErr
  731. return
  732. }
  733. // 已授权分类id
  734. permissionClassifyIdList, tmpErr := data_manage_permission.GetUserEdbClassifyPermissionList(sysUser.AdminId, classifyId)
  735. if tmpErr != nil {
  736. errMsg = "删除失败"
  737. err = errors.New("获取已授权分类id数据失败,Err:" + tmpErr.Error())
  738. return
  739. }
  740. // 权限校验
  741. {
  742. // 数据权限
  743. haveOperaAuth := data_manage_permission.CheckEdbClassifyPermissionByPermissionIdList(item.IsJoinPermission, item.ClassifyId, permissionClassifyIdList)
  744. if item.ClassifyType == 0 { // 普通指标
  745. button := GetEdbClassifyOpButton(sysUser, item.SysUserId, haveOperaAuth)
  746. if !button.DeleteButton {
  747. errMsg = "无操作权限"
  748. err = errors.New(errMsg)
  749. return
  750. }
  751. } else if item.ClassifyType == 1 { // 预测指标
  752. button := GetPredictEdbClassifyOpButton(sysUser, item.SysUserId, haveOperaAuth)
  753. if !button.DeleteButton {
  754. errMsg = "无操作权限"
  755. err = errors.New(errMsg)
  756. return
  757. }
  758. }
  759. }
  760. // 获取所有的分类(自己+所有子分类)
  761. allChildClassifyItemList, tmpErr, _ := GetAllChildClassifyByParentId(classifyId)
  762. if tmpErr != nil {
  763. err = tmpErr
  764. return
  765. }
  766. selfAndSelfClassifyIdList = append(selfAndSelfClassifyIdList, classifyId)
  767. for _, v := range allChildClassifyItemList {
  768. selfAndSelfClassifyIdList = append(selfAndSelfClassifyIdList, v.ClassifyId)
  769. }
  770. //判断分类下,是否含有指标
  771. count, tmpErr := data_manage.GetEdbInfoCountByClassifyIdList(selfAndSelfClassifyIdList)
  772. if tmpErr != nil && !utils.IsErrNoRow(tmpErr) {
  773. errMsg = "删除失败"
  774. err = errors.New("分类下是否含有指标失败,Err:" + tmpErr.Error())
  775. return
  776. }
  777. if count > 0 {
  778. errMsg = "该目录下存在关联指标,不可删除"
  779. return
  780. }
  781. tmpErr = data_manage.DeleteEdbClassify(classifyId)
  782. if tmpErr != nil {
  783. errMsg = "删除失败"
  784. err = errors.New("删除失败,Err:" + tmpErr.Error())
  785. return
  786. }
  787. }
  788. //删除指标
  789. if edbInfoId > 0 {
  790. edbInfo, tmpErr := data_manage.GetEdbInfoById(edbInfoId)
  791. if tmpErr != nil {
  792. if utils.IsErrNoRow(tmpErr) {
  793. errMsg = "指标已删除,请刷新页面"
  794. err = errors.New("指标不存在,Err:" + tmpErr.Error())
  795. return
  796. } else {
  797. errMsg = "删除失败"
  798. err = errors.New("删除失败,获取指标信息失败,Err:" + tmpErr.Error())
  799. return
  800. }
  801. }
  802. if edbInfo == nil {
  803. errMsg = "指标已删除,请刷新页面"
  804. return
  805. }
  806. // 公开状态校验
  807. switch edbInfo.PublicStatus {
  808. case utils.DataPublicSuccess:
  809. errMsg = "当前指标已经公开,请撤销公开后删除"
  810. return
  811. case utils.DataPublicCommit:
  812. errMsg = "当前指标已经提交申请公开,请撤销审批单后删除"
  813. return
  814. }
  815. //判断指标是否用于作图,如果用于作图,则不可删除
  816. chartCount, tmpErr := data_manage.GetChartEdbMappingCount(edbInfoId)
  817. if tmpErr != nil && !utils.IsErrNoRow(tmpErr) {
  818. errMsg = "删除失败"
  819. err = errors.New("判断指标是否被用于作图失败,Err:" + tmpErr.Error())
  820. return
  821. }
  822. if chartCount > 0 {
  823. errMsg = "当前指标已用作画图,不可删除"
  824. return
  825. }
  826. //如果是普通指标,那么还需要判断是否被预测指标作为源指标
  827. if edbInfo.EdbInfoType == 0 {
  828. predictEdbInfoCount, tmpErr := data_manage.GetPredictEdbConfCount(edbInfoId)
  829. if tmpErr != nil && !utils.IsErrNoRow(tmpErr) {
  830. errMsg = "删除失败"
  831. err = errors.New("判断指标是否被用于预测指标失败,Err:" + tmpErr.Error())
  832. return
  833. }
  834. if predictEdbInfoCount > 0 {
  835. errMsg = "当前指标已用作预测指标,不可删除"
  836. return
  837. }
  838. }
  839. //判断指标是否用作其他指标的计算
  840. {
  841. calculateCount, tmpErr := data_manage.GetEdbInfoCalculateMappingCount(edbInfoId)
  842. if tmpErr != nil && !utils.IsErrNoRow(tmpErr) {
  843. errMsg = "删除失败"
  844. err = errors.New("判断指标是否被用于计算失败,GetEdbInfoCalculateCount Err:" + tmpErr.Error())
  845. return
  846. }
  847. if calculateCount > 0 {
  848. errMsg = "当前指标已用作,指标运算,不可删除"
  849. return
  850. }
  851. }
  852. // 判断指标是否用作表格引用
  853. {
  854. tableItems, tmpErr := excel.GetNoCustomAnalysisExcelEdbMapping(edbInfoId)
  855. if tmpErr != nil && !utils.IsErrNoRow(tmpErr) {
  856. errMsg = "删除失败"
  857. err = errors.New("判断指标是否用作表格引用,GetNoCustomAnalysisExcelEdbMappingCount Err:" + tmpErr.Error())
  858. return
  859. }
  860. //英文翻译:
  861. //1、当前指标已用作画图,不可删除:The current metric is in use for charting and cannot be deleted
  862. //2、当前指标已被表格引用,不可删除:The current metric is referenced by a table and cannot be deleted
  863. //3、删除失败:Deletion failed
  864. //4、知道了:Understood
  865. if len(tableItems) > 0 {
  866. errMsg = "当前指标已被表格引用,不可删除"
  867. return
  868. }
  869. }
  870. // 判断指标是否用作跨品种图表使用
  871. {
  872. // todo 如果绑定的标签未画图,则允许删除绑定
  873. // 查询跨品种的图表
  874. tagXList, tmpErr := cross_variety.GetChartInfoCrossVarietyByXEdbInfoId(edbInfoId)
  875. if tmpErr != nil && !utils.IsErrNoRow(tmpErr) {
  876. errMsg = "删除失败"
  877. err = errors.New("判断指标是否用作跨品种图表使用,GetChartInfoCrossVarietyByXEdbInfoId Err:" + tmpErr.Error())
  878. return
  879. }
  880. if len(tagXList) > 0 {
  881. errMsg = "当前指标已添加到跨品种分析,不可删除"
  882. return
  883. }
  884. tagYList, tmpErr := cross_variety.GetChartInfoCrossVarietyByYEdbInfoId(edbInfoId)
  885. if tmpErr != nil && !utils.IsErrNoRow(tmpErr) {
  886. errMsg = "删除失败"
  887. err = errors.New("判断指标是否用作跨品种图表使用,GetChartInfoCrossVarietyByYEdbInfoId Err:" + tmpErr.Error())
  888. return
  889. }
  890. if len(tagYList) > 0 {
  891. errMsg = "当前指标已添加到跨品种分析,不可删除"
  892. return
  893. }
  894. /*calculateCount, tmpErr := cross_variety.GetCountByEdbInfoId(edbInfoId)
  895. if tmpErr != nil && !utils.IsErrNoRow(tmpErr) {
  896. errMsg = "删除失败"
  897. err = errors.New("判断指标是否用作跨品种图表使用,GetCountByEdbInfoId Err:" + tmpErr.Error())
  898. return
  899. }
  900. if calculateCount > 0 {
  901. errMsg = "当前指标已添加到跨品种分析,不可删除"
  902. return
  903. }*/
  904. }
  905. //真实删除
  906. tmpErr = data_manage.DeleteEdbInfoAndData(edbInfo.EdbInfoId, edbInfo.Source, edbInfo.SubSource)
  907. if tmpErr != nil {
  908. errMsg = "删除失败"
  909. err = errors.New("删除失败,Err:" + tmpErr.Error())
  910. return
  911. }
  912. // 如果是同花顺高频数据或类似数据, 还需要删除base_from_edb_mapping对应关系
  913. baseMappingOb := new(data_manage.BaseFromEdbMapping)
  914. {
  915. cond := fmt.Sprintf(" %s = ? AND %s = ? AND %s = ?", baseMappingOb.Cols().EdbCode, baseMappingOb.Cols().Source, baseMappingOb.Cols().SubSource)
  916. pars := make([]interface{}, 0)
  917. pars = append(pars, edbInfo.EdbCode, edbInfo.Source, edbInfo.SubSource)
  918. if e := baseMappingOb.RemoveByCondition(cond, pars); e != nil {
  919. errMsg = "删除失败"
  920. err = fmt.Errorf("删除源指标映射失败, %v", e)
  921. return
  922. }
  923. }
  924. // 删除指标后的操作
  925. go handleByDelEdbInfo(edbInfo)
  926. // 返回下一个表格的信息
  927. {
  928. var condition string
  929. var pars []interface{}
  930. condition += " AND edb_info_type=? AND classify_id=? "
  931. pars = append(pars, edbInfo.EdbInfoType, edbInfo.ClassifyId)
  932. condition += " AND (sort>? OR (sort=? AND edb_info_id<?) ) "
  933. pars = append(pars, edbInfo.Sort, edbInfo.Sort, edbInfo.EdbInfoId)
  934. nextItem, tmpErr = data_manage.GetNextEdbInfoByCondition(condition, pars)
  935. if tmpErr != nil && !utils.IsErrNoRow(tmpErr) {
  936. errMsg = "删除失败"
  937. err = errors.New("获取下一级预测指标信息失败,Err:" + tmpErr.Error())
  938. return
  939. }
  940. // 如果没找到,那么查找下一个分类的第一个表格
  941. if nextItem == nil {
  942. currClassifyInfo, tmpErr := data_manage.GetEdbClassifyById(edbInfo.ClassifyId)
  943. if tmpErr != nil && !utils.IsErrNoRow(tmpErr) {
  944. errMsg = "删除失败"
  945. err = errors.New("获取当前预测指标分类信息失败,Err:" + tmpErr.Error())
  946. return
  947. }
  948. nextItem, err = data_manage.GetNextEdbInfo(edbInfo.ClassifyId, currClassifyInfo.Sort, int(currClassifyInfo.ClassifyType), edbInfo.EdbInfoType)
  949. if err != nil && !utils.IsErrNoRow(err) {
  950. errMsg = "删除失败"
  951. err = errors.New("获取下一级预测指标信息失败,Err:" + tmpErr.Error())
  952. return
  953. }
  954. }
  955. }
  956. //新增操作日志
  957. {
  958. edbLog := new(data_manage.EdbInfoLog)
  959. edbLog.EdbInfoId = edbInfo.EdbInfoId
  960. edbLog.SourceName = edbInfo.SourceName
  961. edbLog.Source = edbInfo.Source
  962. edbLog.EdbCode = edbInfo.EdbCode
  963. edbLog.EdbName = edbInfo.EdbName
  964. edbLog.ClassifyId = edbInfo.ClassifyId
  965. edbLog.SysUserId = sysUser.AdminId
  966. edbLog.SysUserRealName = sysUser.RealName
  967. edbLog.CreateTime = time.Now()
  968. edbLog.Content = requestBody
  969. edbLog.Status = "删除指标"
  970. edbLog.Method = requestUrl
  971. go data_manage.AddEdbInfoLog(edbLog)
  972. }
  973. // 删除es中的数据
  974. DeleteEdbInfoToEs(edbInfoId)
  975. }
  976. return
  977. }
  978. // MoveEdbClassify 移动指标分类
  979. func MoveEdbClassify(req data_manage.MoveEdbClassifyReq, sysUser *system.Admin, classifyType uint8) (err error, errMsg string) {
  980. // req.ClassifyId, req.ParentClassifyId, req.PrevClassifyId, req.NextClassifyId
  981. classifyId := req.ClassifyId
  982. parentClassifyId := req.ParentClassifyId
  983. prevClassifyId := req.PrevClassifyId
  984. nextClassifyId := req.NextClassifyId
  985. edbInfoId := req.EdbInfoId
  986. prevEdbInfoId := req.PrevEdbInfoId
  987. nextEdbInfoId := req.NextEdbInfoId
  988. //首先确定移动的对象是分类还是指标
  989. //判断上一个节点是分类还是指标
  990. //判断下一个节点是分类还是指标
  991. //同时更新分类目录下的分类sort和指标sort
  992. //更新当前移动的分类或者指标sort
  993. var parentEdbClassifyInfo *data_manage.EdbClassify
  994. if parentClassifyId > 0 {
  995. parentEdbClassifyInfo, err = data_manage.GetEdbClassifyById(parentClassifyId)
  996. if err != nil {
  997. errMsg = "移动失败"
  998. err = errors.New("获取上级分类信息失败,Err:" + err.Error())
  999. return
  1000. }
  1001. }
  1002. //如果有传入 上一个兄弟节点分类id
  1003. var (
  1004. edbClassifyInfo *data_manage.EdbClassify
  1005. prevClassify *data_manage.EdbClassify
  1006. nextClassify *data_manage.EdbClassify
  1007. edbInfo *data_manage.EdbInfo
  1008. prevEdbInfo *data_manage.EdbInfo
  1009. nextEdbInfo *data_manage.EdbInfo
  1010. prevSort int
  1011. nextSort int
  1012. )
  1013. // 移动对象为分类, 判断权限
  1014. if edbInfoId == 0 {
  1015. edbClassifyInfo, err = data_manage.GetEdbClassifyById(classifyId)
  1016. if err != nil {
  1017. if utils.IsErrNoRow(err) {
  1018. errMsg = "当前分类不存在"
  1019. err = errors.New("获取分类信息失败,Err:" + err.Error())
  1020. return
  1021. }
  1022. errMsg = "移动失败"
  1023. err = errors.New("获取分类信息失败,Err:" + err.Error())
  1024. return
  1025. }
  1026. if edbClassifyInfo.SysUserId != sysUser.AdminId {
  1027. errMsg = "不是本人目录,您没有操作权限"
  1028. err = errors.New(errMsg)
  1029. return
  1030. }
  1031. if parentClassifyId > 0 && parentEdbClassifyInfo.Level == 6 {
  1032. errMsg = "最高只支持添加6级分类"
  1033. err = errors.New(errMsg)
  1034. return
  1035. }
  1036. // 如果是移动目录, 那么校验一下父级目录下是否有重名目录
  1037. exists, e := data_manage.GetEdbClassifyByParentIdAndName(parentClassifyId, edbClassifyInfo.ClassifyName, classifyId, classifyType)
  1038. if e != nil && !utils.IsErrNoRow(e) {
  1039. errMsg = "移动失败"
  1040. err = fmt.Errorf("获取父级分类下的同名分类失败, Err: %s", e.Error())
  1041. return
  1042. }
  1043. if exists != nil && exists.ClassifyId > 0 {
  1044. errMsg = "移动失败,分类名称已存在"
  1045. return
  1046. }
  1047. // 权限校验
  1048. {
  1049. // 已授权分类id
  1050. permissionClassifyIdList, tmpErr := data_manage_permission.GetUserEdbClassifyPermissionList(sysUser.AdminId, classifyId)
  1051. if tmpErr != nil {
  1052. errMsg = "移动失败"
  1053. err = errors.New("获取已授权分类id数据失败,Err:" + tmpErr.Error())
  1054. return
  1055. }
  1056. // 数据权限
  1057. haveOperaAuth := data_manage_permission.CheckEdbClassifyPermissionByPermissionIdList(edbClassifyInfo.IsJoinPermission, edbClassifyInfo.ClassifyId, permissionClassifyIdList)
  1058. if edbClassifyInfo.ClassifyType == 0 { // 普通指标
  1059. button := GetEdbClassifyOpButton(sysUser, edbClassifyInfo.SysUserId, haveOperaAuth)
  1060. if !button.MoveButton {
  1061. errMsg = "无操作权限"
  1062. err = errors.New(errMsg)
  1063. return
  1064. }
  1065. } else if edbClassifyInfo.ClassifyType == 1 { // 预测指标
  1066. button := GetPredictEdbClassifyOpButton(sysUser, edbClassifyInfo.SysUserId, haveOperaAuth)
  1067. if !button.MoveButton {
  1068. errMsg = "无操作权限"
  1069. err = errors.New(errMsg)
  1070. return
  1071. }
  1072. }
  1073. }
  1074. } else {
  1075. edbInfo, err = data_manage.GetEdbInfoById(req.EdbInfoId)
  1076. if err != nil {
  1077. if utils.IsErrNoRow(err) {
  1078. errMsg = "当前指标不存在"
  1079. err = errors.New("获取分类信息失败,Err:" + err.Error())
  1080. return
  1081. }
  1082. errMsg = "移动失败"
  1083. err = errors.New("获取分类信息失败,Err:" + err.Error())
  1084. return
  1085. }
  1086. if edbInfo.SysUserId != sysUser.AdminId {
  1087. errMsg = "不是本人指标,您没有操作权限"
  1088. err = errors.New(errMsg)
  1089. return
  1090. }
  1091. if parentClassifyId == 0 {
  1092. errMsg = "移动失败,指标必须挂在分类下"
  1093. err = errors.New(errMsg)
  1094. return
  1095. }
  1096. var haveOperaAuth bool
  1097. // 权限校验
  1098. {
  1099. haveOperaAuth, err = data_manage_permission.CheckEdbPermissionByEdbInfoId(edbInfo.EdbInfoId, edbInfo.ClassifyId, edbInfo.IsJoinPermission, sysUser.AdminId)
  1100. if err != nil {
  1101. errMsg = "移动失败"
  1102. err = errors.New("获取指标权限信息失败,Err:" + err.Error())
  1103. return
  1104. }
  1105. }
  1106. editShareEdbInfoIdMap, tmpErr := GetAllEditSharedEdbInfoIdMapByReceivedUserId(sysUser.AdminId)
  1107. if tmpErr != nil {
  1108. errMsg = "移动失败"
  1109. err = errors.New("获取分享出来有操作权限的指标id列表失败,Err:" + tmpErr.Error())
  1110. return
  1111. }
  1112. // 移动权限校验
  1113. button := GetEdbOpButton(sysUser, edbInfo.SysUserId, edbInfo.EdbInfoId, edbInfo.EdbType, edbInfo.EdbInfoType, haveOperaAuth, editShareEdbInfoIdMap)
  1114. if !button.MoveButton {
  1115. errMsg = "无操作权限"
  1116. err = errors.New(errMsg)
  1117. return
  1118. }
  1119. }
  1120. if prevClassifyId > 0 {
  1121. prevClassify, err = data_manage.GetEdbClassifyById(prevClassifyId)
  1122. if err != nil {
  1123. errMsg = "移动失败"
  1124. err = errors.New("获取上一个兄弟节点分类信息失败,Err:" + err.Error())
  1125. return
  1126. }
  1127. prevSort = prevClassify.Sort
  1128. } else if prevEdbInfoId > 0 {
  1129. prevEdbInfo, err = data_manage.GetEdbInfoById(prevEdbInfoId)
  1130. if err != nil {
  1131. errMsg = "移动失败"
  1132. err = errors.New("获取上一个兄弟节点分类信息失败,Err:" + err.Error())
  1133. return
  1134. }
  1135. prevSort = prevEdbInfo.Sort
  1136. }
  1137. if nextClassifyId > 0 {
  1138. //下一个兄弟节点
  1139. nextClassify, err = data_manage.GetEdbClassifyById(nextClassifyId)
  1140. if err != nil {
  1141. errMsg = "移动失败"
  1142. err = errors.New("获取下一个兄弟节点分类信息失败,Err:" + err.Error())
  1143. return
  1144. }
  1145. nextSort = nextClassify.Sort
  1146. } else if nextEdbInfoId > 0 {
  1147. //下一个兄弟节点
  1148. nextEdbInfo, err = data_manage.GetEdbInfoById(nextEdbInfoId)
  1149. if err != nil {
  1150. errMsg = "移动失败"
  1151. err = errors.New("获取下一个兄弟节点分类信息失败,Err:" + err.Error())
  1152. return
  1153. }
  1154. nextSort = nextEdbInfo.Sort
  1155. }
  1156. err, errMsg = moveEdbOrClassify(parentEdbClassifyInfo, edbClassifyInfo, prevClassify, nextClassify, edbInfo, prevEdbInfo, nextEdbInfo, parentClassifyId, prevSort, nextSort, classifyType)
  1157. return
  1158. }
  1159. // moveEdbOrClassify 移动指标分类
  1160. func moveEdbOrClassify(parentEdbClassifyInfo, edbClassifyInfo, prevClassify, nextClassify *data_manage.EdbClassify, edbInfo, prevEdbInfo, nextEdbInfo *data_manage.EdbInfo, parentClassifyId int, prevSort, nextSort int, classifyType uint8) (err error, errMsg string) {
  1161. if edbClassifyInfo != nil {
  1162. // 移动分类
  1163. return moveEdbClassify(parentEdbClassifyInfo, edbClassifyInfo, prevClassify, nextClassify, prevEdbInfo, nextEdbInfo, parentClassifyId, prevSort, nextSort, classifyType)
  1164. } else {
  1165. // 移动指标
  1166. return moveEdb(prevClassify, nextClassify, edbInfo, prevEdbInfo, nextEdbInfo, parentClassifyId, prevSort, nextSort, classifyType)
  1167. }
  1168. return
  1169. }
  1170. // moveEdb
  1171. // @Description: 移动指标
  1172. // @author: Roc
  1173. // @datetime 2024-11-26 16:07:37
  1174. // @param prevClassify *data_manage.EdbClassify
  1175. // @param nextClassify *data_manage.EdbClassify
  1176. // @param edbInfo *data_manage.EdbInfo
  1177. // @param prevEdbInfo *data_manage.EdbInfo
  1178. // @param nextEdbInfo *data_manage.EdbInfo
  1179. // @param parentClassifyId int
  1180. // @param prevSort int
  1181. // @param nextSort int
  1182. // @param classifyType uint8
  1183. // @return err error
  1184. // @return errMsg string
  1185. func moveEdb(prevClassify, nextClassify *data_manage.EdbClassify, edbInfo, prevEdbInfo, nextEdbInfo *data_manage.EdbInfo, parentClassifyId int, prevSort, nextSort int, classifyType uint8) (err error, errMsg string) {
  1186. updateCol := make([]string, 0)
  1187. if edbInfo == nil {
  1188. errMsg = "当前指标不存在"
  1189. err = errors.New(errMsg)
  1190. return
  1191. }
  1192. //如果改变了分类,那么移动该指标数据
  1193. if edbInfo.ClassifyId != parentClassifyId {
  1194. edbInfo.ClassifyId = parentClassifyId
  1195. edbInfo.ModifyTime = time.Now()
  1196. updateCol = append(updateCol, "ClassifyId", "ModifyTime")
  1197. }
  1198. if prevSort > 0 {
  1199. //如果是移动在两个兄弟节点之间
  1200. if nextSort > 0 {
  1201. //下一个兄弟节点
  1202. //如果上一个兄弟与下一个兄弟的排序权重是一致的,那么需要将下一个兄弟(以及下个兄弟的同样排序权重)的排序权重+2,自己变成上一个兄弟的排序权重+1
  1203. if prevSort == nextSort || prevSort == edbInfo.Sort {
  1204. //变更兄弟节点的排序
  1205. updateSortStr := `sort + 2`
  1206. //变更分类
  1207. if prevClassify != nil {
  1208. _ = data_manage.UpdateEdbClassifySortByParentId(parentClassifyId, prevClassify.ClassifyId, prevClassify.Sort, updateSortStr, classifyType)
  1209. } else {
  1210. _ = data_manage.UpdateEdbClassifySortByParentId(parentClassifyId, 0, prevSort, updateSortStr, classifyType)
  1211. }
  1212. //变更指标
  1213. if prevEdbInfo != nil {
  1214. //变更兄弟节点的排序
  1215. _ = data_manage.UpdateEdbInfoSortByClassifyId(parentClassifyId, prevSort, prevEdbInfo.EdbInfoId, updateSortStr)
  1216. } else {
  1217. _ = data_manage.UpdateEdbInfoSortByClassifyId(parentClassifyId, prevSort, 0, updateSortStr)
  1218. }
  1219. } else {
  1220. //如果下一个兄弟的排序权重正好是上个兄弟节点的下一层,那么需要再加一层了
  1221. if nextSort-prevSort == 1 {
  1222. //变更兄弟节点的排序
  1223. updateSortStr := `sort + 1`
  1224. //变更分类
  1225. if prevClassify != nil {
  1226. _ = data_manage.UpdateEdbClassifySortByParentId(parentClassifyId, prevClassify.ClassifyId, prevSort, updateSortStr, classifyType)
  1227. } else {
  1228. _ = data_manage.UpdateEdbClassifySortByParentId(parentClassifyId, 0, prevSort, updateSortStr, classifyType)
  1229. }
  1230. //变更指标
  1231. if prevEdbInfo != nil {
  1232. //变更兄弟节点的排序
  1233. _ = data_manage.UpdateEdbInfoSortByClassifyId(parentClassifyId, prevSort, prevEdbInfo.EdbInfoId, updateSortStr)
  1234. } else {
  1235. _ = data_manage.UpdateEdbInfoSortByClassifyId(parentClassifyId, prevSort, 0, updateSortStr)
  1236. }
  1237. }
  1238. }
  1239. }
  1240. edbInfo.Sort = prevSort + 1
  1241. edbInfo.ModifyTime = time.Now()
  1242. updateCol = append(updateCol, "Sort", "ModifyTime")
  1243. } else if prevClassify == nil && nextClassify == nil && prevEdbInfo == nil && nextEdbInfo == nil && parentClassifyId > 0 {
  1244. //处理只拖动到目录里,默认放到目录底部的情况
  1245. var maxSort int
  1246. maxSort, err = GetEdbClassifyMaxSort(parentClassifyId, classifyType)
  1247. if err != nil {
  1248. errMsg = "移动失败"
  1249. err = errors.New("查询组内排序信息失败,Err:" + err.Error())
  1250. return
  1251. }
  1252. edbInfo.Sort = maxSort + 1 //那就是排在组内最后一位
  1253. edbInfo.ModifyTime = time.Now()
  1254. updateCol = append(updateCol, "Sort", "ModifyTime")
  1255. } else {
  1256. // 拖动到父级分类的第一位
  1257. firstClassify, tmpErr := data_manage.GetFirstEdbClassifyByParentId(parentClassifyId)
  1258. if tmpErr != nil && !utils.IsErrNoRow(tmpErr) {
  1259. errMsg = "移动失败"
  1260. err = errors.New("获取获取当前父级分类下的排序第一条的分类信息失败,Err:" + tmpErr.Error())
  1261. return
  1262. }
  1263. //如果该分类下存在其他分类,且第一个其他分类的排序等于0,那么需要调整排序
  1264. if firstClassify != nil && firstClassify.ClassifyId > 0 && firstClassify.Sort == 0 {
  1265. updateSortStr := ` sort + 1 `
  1266. _ = data_manage.UpdateEdbClassifySortByParentId(parentClassifyId, firstClassify.ClassifyId-1, 0, updateSortStr, classifyType)
  1267. //该分类下的所有指标也需要+1
  1268. _ = data_manage.UpdateEdbInfoSortByClassifyId(parentClassifyId, 0, 0, updateSortStr)
  1269. } else {
  1270. //如果该分类下存在指标,且第一个指标的排序等于0,那么需要调整排序
  1271. firstEdb, tErr := data_manage.GetFirstEdbInfoByClassifyId(parentClassifyId)
  1272. if tErr != nil && !utils.IsErrNoRow(tErr) {
  1273. errMsg = "移动失败"
  1274. err = errors.New("获取获取当前父级分类下的排序第一条的分类信息失败,Err:" + tErr.Error())
  1275. return
  1276. }
  1277. //如果该分类下存在其他分类,且第一个其他分类的排序等于0,那么需要调整排序
  1278. if firstEdb != nil && firstEdb.ClassifyId > 0 && firstEdb.Sort == 0 {
  1279. updateSortStr := ` sort + 1 `
  1280. _ = data_manage.UpdateEdbInfoSortByClassifyId(parentClassifyId, 0, firstEdb.EdbInfoId-1, updateSortStr)
  1281. _ = data_manage.UpdateEdbClassifySortByParentId(parentClassifyId, 0, 0, updateSortStr, classifyType)
  1282. }
  1283. }
  1284. edbInfo.Sort = 0 //那就是排在第一位
  1285. edbInfo.ModifyTime = time.Now()
  1286. updateCol = append(updateCol, "Sort", "ModifyTime")
  1287. }
  1288. //更新
  1289. if len(updateCol) > 0 {
  1290. err = edbInfo.Update(updateCol)
  1291. if err != nil {
  1292. errMsg = "移动失败"
  1293. err = errors.New("修改失败,Err:" + err.Error())
  1294. return
  1295. }
  1296. }
  1297. return
  1298. }
  1299. // moveEdbClassify
  1300. // @Description: 移动指标分类
  1301. // @author: Roc
  1302. // @datetime 2024-11-26 16:07:44
  1303. // @param parentEdbClassifyInfo *data_manage.EdbClassify
  1304. // @param edbClassifyInfo *data_manage.EdbClassify
  1305. // @param prevClassify *data_manage.EdbClassify
  1306. // @param nextClassify *data_manage.EdbClassify
  1307. // @param edbInfo *data_manage.EdbInfo
  1308. // @param prevEdbInfo *data_manage.EdbInfo
  1309. // @param nextEdbInfo *data_manage.EdbInfo
  1310. // @param parentClassifyId int
  1311. // @param prevSort int
  1312. // @param nextSort int
  1313. // @param classifyType uint8
  1314. // @return err error
  1315. // @return errMsg string
  1316. func moveEdbClassify(parentEdbClassifyInfo, edbClassifyInfo, prevClassify, nextClassify *data_manage.EdbClassify, prevEdbInfo, nextEdbInfo *data_manage.EdbInfo, parentClassifyId int, prevSort, nextSort int, classifyType uint8) (err error, errMsg string) {
  1317. updateCol := make([]string, 0)
  1318. // 移动对象为分类, 判断分类是否存在
  1319. oldParentId := edbClassifyInfo.ParentId
  1320. oldLevel := edbClassifyInfo.Level
  1321. var classifyIds []int
  1322. if oldParentId != parentClassifyId {
  1323. //更新子分类对应的level
  1324. childList, e, m := GetChildClassifyByClassifyId(edbClassifyInfo.ClassifyId)
  1325. if e != nil {
  1326. errMsg = "移动失败"
  1327. err = errors.New("查询子分类失败,Err:" + e.Error() + m)
  1328. return
  1329. }
  1330. if len(childList) > 0 {
  1331. for _, v := range childList {
  1332. if v.ClassifyId == edbClassifyInfo.ClassifyId {
  1333. continue
  1334. }
  1335. classifyIds = append(classifyIds, v.ClassifyId)
  1336. }
  1337. }
  1338. }
  1339. //判断上级id是否一致,如果不一致的话,那么需要移动该分类层级
  1340. if edbClassifyInfo.ParentId != parentClassifyId && parentClassifyId != 0 {
  1341. if edbClassifyInfo.Level != parentEdbClassifyInfo.Level+1 { //禁止层级调整
  1342. errMsg = "移动失败"
  1343. err = errors.New("不支持目录层级变更")
  1344. return
  1345. }
  1346. edbClassifyInfo.ParentId = parentEdbClassifyInfo.ClassifyId
  1347. edbClassifyInfo.RootId = parentEdbClassifyInfo.RootId
  1348. edbClassifyInfo.Level = parentEdbClassifyInfo.Level + 1
  1349. edbClassifyInfo.ModifyTime = time.Now()
  1350. updateCol = append(updateCol, "ParentId", "RootId", "Level", "ModifyTime")
  1351. } else if edbClassifyInfo.ParentId != parentClassifyId && parentClassifyId == 0 {
  1352. errMsg = "移动失败"
  1353. err = errors.New("不支持目录层级变更")
  1354. return
  1355. }
  1356. if prevSort > 0 {
  1357. //如果是移动在两个兄弟节点之间
  1358. if nextSort > 0 {
  1359. //下一个兄弟节点
  1360. //如果上一个兄弟与下一个兄弟的排序权重是一致的,那么需要将下一个兄弟(以及下个兄弟的同样排序权重)的排序权重+2,自己变成上一个兄弟的排序权重+1
  1361. if prevSort == nextSort || prevSort == edbClassifyInfo.Sort {
  1362. //变更兄弟节点的排序
  1363. updateSortStr := `sort + 2`
  1364. //变更分类
  1365. if prevClassify != nil {
  1366. _ = data_manage.UpdateEdbClassifySortByParentId(parentClassifyId, prevClassify.ClassifyId, prevClassify.Sort, updateSortStr, classifyType)
  1367. } else {
  1368. _ = data_manage.UpdateEdbClassifySortByParentId(parentClassifyId, 0, prevSort, updateSortStr, classifyType)
  1369. }
  1370. //变更指标
  1371. if prevEdbInfo != nil {
  1372. //变更兄弟节点的排序
  1373. _ = data_manage.UpdateEdbInfoSortByClassifyId(parentClassifyId, prevSort, prevEdbInfo.EdbInfoId, updateSortStr)
  1374. } else {
  1375. _ = data_manage.UpdateEdbInfoSortByClassifyId(parentClassifyId, prevSort, 0, updateSortStr)
  1376. }
  1377. } else {
  1378. //如果下一个兄弟的排序权重正好是上个兄弟节点的下一层,那么需要再加一层了
  1379. if nextSort-prevSort == 1 {
  1380. //变更兄弟节点的排序
  1381. updateSortStr := `sort + 1`
  1382. //变更分类
  1383. if prevClassify != nil {
  1384. _ = data_manage.UpdateEdbClassifySortByParentId(parentClassifyId, prevClassify.ClassifyId, prevSort, updateSortStr, classifyType)
  1385. } else {
  1386. _ = data_manage.UpdateEdbClassifySortByParentId(parentClassifyId, 0, prevSort, updateSortStr, classifyType)
  1387. }
  1388. //变更指标
  1389. if prevEdbInfo != nil {
  1390. //变更兄弟节点的排序
  1391. _ = data_manage.UpdateEdbInfoSortByClassifyId(parentClassifyId, prevSort, prevEdbInfo.EdbInfoId, updateSortStr)
  1392. } else {
  1393. _ = data_manage.UpdateEdbInfoSortByClassifyId(parentClassifyId, prevSort, 0, updateSortStr)
  1394. }
  1395. }
  1396. }
  1397. }
  1398. edbClassifyInfo.Sort = prevSort + 1
  1399. edbClassifyInfo.ModifyTime = time.Now()
  1400. updateCol = append(updateCol, "Sort", "ModifyTime")
  1401. } else if prevClassify == nil && nextClassify == nil && prevEdbInfo == nil && nextEdbInfo == nil && parentClassifyId > 0 {
  1402. //处理只拖动到目录里,默认放到目录底部的情况
  1403. var maxSort int
  1404. maxSort, err = GetEdbClassifyMaxSort(parentClassifyId, classifyType)
  1405. if err != nil {
  1406. errMsg = "移动失败"
  1407. err = errors.New("查询组内排序信息失败,Err:" + err.Error())
  1408. return
  1409. }
  1410. edbClassifyInfo.Sort = maxSort + 1 //那就是排在组内最后一位
  1411. edbClassifyInfo.ModifyTime = time.Now()
  1412. updateCol = append(updateCol, "Sort", "ModifyTime")
  1413. } else {
  1414. // 拖动到父级分类的第一位
  1415. firstClassify, tmpErr := data_manage.GetFirstEdbClassifyByParentId(parentClassifyId)
  1416. if tmpErr != nil && !utils.IsErrNoRow(tmpErr) {
  1417. errMsg = "移动失败"
  1418. err = errors.New("获取获取当前父级分类下的排序第一条的分类信息失败,Err:" + tmpErr.Error())
  1419. return
  1420. }
  1421. //如果该分类下存在其他分类,且第一个其他分类的排序等于0,那么需要调整排序
  1422. if firstClassify != nil && firstClassify.ClassifyId > 0 && firstClassify.Sort == 0 {
  1423. updateSortStr := ` sort + 1 `
  1424. _ = data_manage.UpdateEdbClassifySortByParentId(parentClassifyId, firstClassify.ClassifyId-1, 0, updateSortStr, classifyType)
  1425. //该分类下的所有指标也需要+1
  1426. _ = data_manage.UpdateEdbInfoSortByClassifyId(parentClassifyId, 0, 0, updateSortStr)
  1427. } else {
  1428. //如果该分类下存在指标,且第一个指标的排序等于0,那么需要调整排序
  1429. firstEdb, tErr := data_manage.GetFirstEdbInfoByClassifyId(parentClassifyId)
  1430. if tErr != nil && !utils.IsErrNoRow(tErr) {
  1431. errMsg = "移动失败"
  1432. err = errors.New("获取获取当前父级分类下的排序第一条的分类信息失败,Err:" + tErr.Error())
  1433. return
  1434. }
  1435. //如果该分类下存在其他分类,且第一个其他分类的排序等于0,那么需要调整排序
  1436. if firstEdb != nil && firstEdb.ClassifyId > 0 && firstEdb.Sort == 0 {
  1437. updateSortStr := ` sort + 1 `
  1438. _ = data_manage.UpdateEdbInfoSortByClassifyId(parentClassifyId, 0, firstEdb.EdbInfoId-1, updateSortStr)
  1439. _ = data_manage.UpdateEdbClassifySortByParentId(parentClassifyId, 0, 0, updateSortStr, classifyType)
  1440. }
  1441. }
  1442. edbClassifyInfo.Sort = 0 //那就是排在第一位
  1443. edbClassifyInfo.ModifyTime = time.Now()
  1444. updateCol = append(updateCol, "Sort", "ModifyTime")
  1445. }
  1446. oldClassifyIdPath := edbClassifyInfo.ClassifyIdPath
  1447. oldClassifyNamePath := edbClassifyInfo.ClassifyNamePath
  1448. newClassifyNamePath := fmt.Sprint(parentEdbClassifyInfo.ClassifyNamePath, `|`, edbClassifyInfo.ClassifyName)
  1449. newClassifyIdPath := fmt.Sprint(parentEdbClassifyInfo.ClassifyIdPath, `,`, edbClassifyInfo.ClassifyId)
  1450. //更新
  1451. if len(updateCol) > 0 {
  1452. edbClassifyInfo.ClassifyNamePath = newClassifyNamePath
  1453. edbClassifyInfo.ClassifyIdPath = newClassifyIdPath
  1454. updateCol = append(updateCol, "ClassifyNamePath", "ClassifyIdPath")
  1455. err = edbClassifyInfo.Update(updateCol)
  1456. if err != nil {
  1457. errMsg = "移动失败"
  1458. err = errors.New("修改失败,Err:" + err.Error())
  1459. return
  1460. }
  1461. //更新对应分类的root_id和层级
  1462. if oldParentId != parentClassifyId {
  1463. if len(classifyIds) > 0 {
  1464. levelStep := edbClassifyInfo.Level - oldLevel
  1465. err = data_manage.UpdateEdbClassifyChildByParentClassifyId(classifyIds, edbClassifyInfo.RootId, levelStep)
  1466. if err != nil {
  1467. errMsg = "移动失败"
  1468. err = errors.New("更新子分类失败,Err:" + err.Error())
  1469. return
  1470. }
  1471. }
  1472. // 更改了上级分类,那么需要同步去更改自己的分类全路径
  1473. tmpErr := data_manage.UpdateEdbClassifyNameAndNamePathByOldClassifyIdPath(oldClassifyIdPath, newClassifyIdPath, oldClassifyNamePath, newClassifyNamePath)
  1474. if tmpErr != nil {
  1475. utils.FileLog.Error(fmt.Sprintf("更新分类全路径失败,分类id:%d;失败原因Err:%s", edbClassifyInfo.ClassifyId, tmpErr.Error()))
  1476. }
  1477. }
  1478. }
  1479. return
  1480. }
  1481. // GetEdbOpButton 获取ETA指标的操作权限
  1482. func GetEdbOpButton(sysUser *system.Admin, belongUserId, edbInfoId, edbType, edbInfoType int, haveOperaAuth bool, editShareEdbMap map[int]bool) (button data_manage.EdbClassifyItemsButton) {
  1483. // 没有数据权限就直接返回
  1484. if !haveOperaAuth {
  1485. return
  1486. }
  1487. //2、用户对于自己添加的分类,有权限编辑、移动和删除该分类;
  1488. //3、ficc管理员和超管对所有分类有编辑、移动和删除权限;
  1489. //if sysUser.RoleTypeCode == utils.ROLE_TYPE_CODE_ADMIN || sysUser.RoleTypeCode == utils.ROLE_TYPE_CODE_FICC_ADMIN || sysUser.AdminId == belongUserId || sysUser.EdbPermission == 1 {
  1490. // button.AddButton = true
  1491. // button.OpButton = true
  1492. // button.DeleteButton = true
  1493. // button.MoveButton = true
  1494. //}
  1495. if sysUser.AdminId == belongUserId {
  1496. //button.AddButton = true
  1497. button.OpButton = true
  1498. button.DeleteButton = true
  1499. button.MoveButton = true
  1500. } else {
  1501. // 拥有别人分享的操作权限
  1502. if _, ok := editShareEdbMap[edbInfoId]; ok {
  1503. button.OpButton = true
  1504. }
  1505. }
  1506. // ETA指标的基础指标才允许插入值
  1507. if edbInfoType == 0 && edbType == 1 {
  1508. button.InsertNewDataButton = true
  1509. }
  1510. // 关联指标、关联图的权限
  1511. button.ShowEdbRelation = false
  1512. button.ShowChartRelation = false
  1513. return
  1514. }
  1515. // GetEdbClassifyOpButton 获取ETA指标分类的操作权限
  1516. func GetEdbClassifyOpButton(sysUser *system.Admin, belongUserId int, haveOperaAuth bool) (button data_manage.EdbClassifyItemsButton) {
  1517. // 没有数据权限就直接返回
  1518. if !haveOperaAuth {
  1519. return
  1520. }
  1521. //ficc管理员和超管和ficc研究员有权限创建和管理分类,可以编辑分类名称(分类名称不允许重复),可以拖动分类,改变分类顺序,可以拖动分类下模型,改变顺序,可以删除分类,若分类下有预测指标,则不允许删除;
  1522. //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) {
  1523. button.AddButton = true
  1524. button.OpButton = true
  1525. button.DeleteButton = true
  1526. button.MoveButton = true
  1527. //}
  1528. return
  1529. }
  1530. // GetPredictEdbOpButton 获取ETA预测指标的操作权限
  1531. func GetPredictEdbOpButton(sysUser *system.Admin, belongUserId int, haveOperaAuth bool) (button data_manage.EdbClassifyItemsButton) {
  1532. // 没有数据权限就直接返回
  1533. if !haveOperaAuth {
  1534. return
  1535. }
  1536. //预测指标的添加人对该预测指标有全部操作权限,ficc管理员、超管对所有预测指标有全部操作权限;
  1537. if sysUser.RoleTypeCode == utils.ROLE_TYPE_CODE_ADMIN || sysUser.RoleTypeCode == utils.ROLE_TYPE_CODE_FICC_ADMIN || sysUser.AdminId == belongUserId || sysUser.PredictEdbPermission == 1 {
  1538. button.AddButton = true
  1539. button.OpButton = true
  1540. button.DeleteButton = true
  1541. button.MoveButton = true
  1542. }
  1543. button.ShowEdbRelation = true
  1544. button.ShowChartRelation = true
  1545. return
  1546. }
  1547. // GetPredictEdbClassifyOpButton 获取ETA预测指标分类的操作权限
  1548. func GetPredictEdbClassifyOpButton(sysUser *system.Admin, belongUserId int, haveOperaAuth bool) (button data_manage.EdbClassifyItemsButton) {
  1549. // 没有数据权限就直接返回
  1550. if !haveOperaAuth {
  1551. return
  1552. }
  1553. //ficc管理员和超管和ficc研究员有权限创建和管理分类,可以编辑分类名称(分类名称不允许重复),可以拖动分类,改变分类顺序,可以拖动分类下模型,改变顺序,可以删除分类,若分类下有预测指标,则不允许删除;
  1554. //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) {
  1555. button.AddButton = true
  1556. button.OpButton = true
  1557. button.DeleteButton = true
  1558. button.MoveButton = true
  1559. //}
  1560. return
  1561. }
  1562. func GetEdbClassifyMaxSort(parentId int, classifyType uint8) (maxSort int, err error) {
  1563. //获取该层级下最大的排序数
  1564. classifyMaxSort, err := data_manage.GetEdbClassifyMaxSort(parentId, classifyType)
  1565. if err != nil {
  1566. return
  1567. }
  1568. maxSort = classifyMaxSort
  1569. edbMaxSort, err := data_manage.GetEdbInfoMaxSortByClassifyId(parentId)
  1570. if err != nil {
  1571. return
  1572. }
  1573. if maxSort < edbMaxSort {
  1574. maxSort = edbMaxSort
  1575. }
  1576. return
  1577. }
  1578. // GetAllChildClassifyByParentId
  1579. // @Description: 根据上级id获取所有的下级id
  1580. // @author: Roc
  1581. // @datetime 2024-12-02 15:14:13
  1582. // @param parentId int
  1583. // @return targetList []*data_manage.EdbClassifyItems
  1584. // @return err error
  1585. // @return errMsg string
  1586. func GetAllChildClassifyByParentId(parentId int) (targetList []*data_manage.EdbClassifyItems, err error, errMsg string) {
  1587. //判断是否是挂在顶级目录下
  1588. parentClassify, err := data_manage.GetEdbClassifyById(parentId)
  1589. if err != nil {
  1590. if utils.IsErrNoRow(err) {
  1591. errMsg = "当前分类不存在"
  1592. err = errors.New(errMsg)
  1593. return
  1594. }
  1595. errMsg = "获取失败"
  1596. err = errors.New("获取分类信息失败,Err:" + err.Error())
  1597. return
  1598. }
  1599. orderStr := ` order by level asc, sort asc, classify_id asc`
  1600. targetList, err = data_manage.GetChildEdbClassifyListByParentId(fmt.Sprint(parentClassify.ClassifyIdPath, ",%"), orderStr)
  1601. return
  1602. }