edb_classify.go 47 KB

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