edb_classify.go 52 KB

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