edb_classify.go 53 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556
  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. // 维护粮油数据源指标领取状态
  848. if edbInfo.Source == utils.DATA_SOURCE_LY {
  849. tmpErr = data_manage.UpdateLyIndexEdbExist(edbInfo.EdbCode, utils.IS_NO)
  850. if err != nil {
  851. errMsg = "删除失败"
  852. err = errors.New("删除失败,Err:" + tmpErr.Error())
  853. return
  854. }
  855. }
  856. // 如果是同花顺高频数据或类似数据, 还需要删除base_from_edb_mapping对应关系
  857. baseMappingOb := new(data_manage.BaseFromEdbMapping)
  858. {
  859. cond := fmt.Sprintf(" %s = ? AND %s = ? AND %s = ?", baseMappingOb.Cols().EdbCode, baseMappingOb.Cols().Source, baseMappingOb.Cols().SubSource)
  860. pars := make([]interface{}, 0)
  861. pars = append(pars, edbInfo.EdbCode, edbInfo.Source, edbInfo.SubSource)
  862. if e := baseMappingOb.RemoveByCondition(cond, pars); e != nil {
  863. errMsg = "删除失败"
  864. err = fmt.Errorf("删除源指标映射失败, %v", e)
  865. return
  866. }
  867. }
  868. go data_stat.AddEdbDeleteLog(edbInfo, sysUser)
  869. // 删除指标后的操作
  870. go handleByDelEdbInfo(edbInfo)
  871. // 返回下一个表格的信息
  872. {
  873. var condition string
  874. var pars []interface{}
  875. condition += " AND edb_info_type=? AND classify_id=? "
  876. pars = append(pars, edbInfo.EdbInfoType, edbInfo.ClassifyId)
  877. condition += " AND (sort>? OR (sort=? AND edb_info_id<?) ) "
  878. pars = append(pars, edbInfo.Sort, edbInfo.Sort, edbInfo.EdbInfoId)
  879. nextItem, tmpErr = data_manage.GetNextEdbInfoByCondition(condition, pars)
  880. if tmpErr != nil && tmpErr.Error() != utils.ErrNoRow() {
  881. errMsg = "删除失败"
  882. err = errors.New("获取下一级预测指标信息失败,Err:" + tmpErr.Error())
  883. return
  884. }
  885. // 如果没找到,那么查找下一个分类的第一个表格
  886. if nextItem == nil {
  887. currClassifyInfo, tmpErr := data_manage.GetEdbClassifyById(edbInfo.ClassifyId)
  888. if tmpErr != nil && tmpErr.Error() != utils.ErrNoRow() {
  889. errMsg = "删除失败"
  890. err = errors.New("获取当前预测指标分类信息失败,Err:" + tmpErr.Error())
  891. return
  892. }
  893. nextItem, err = data_manage.GetNextEdbInfo(edbInfo.ClassifyId, currClassifyInfo.Sort, int(currClassifyInfo.ClassifyType), edbInfo.EdbInfoType)
  894. if err != nil && err.Error() != utils.ErrNoRow() {
  895. errMsg = "删除失败"
  896. err = errors.New("获取下一级预测指标信息失败,Err:" + tmpErr.Error())
  897. return
  898. }
  899. }
  900. }
  901. //新增操作日志
  902. {
  903. edbLog := new(data_manage.EdbInfoLog)
  904. edbLog.EdbInfoId = edbInfo.EdbInfoId
  905. edbLog.SourceName = edbInfo.SourceName
  906. edbLog.Source = edbInfo.Source
  907. edbLog.EdbCode = edbInfo.EdbCode
  908. edbLog.EdbName = edbInfo.EdbName
  909. edbLog.ClassifyId = edbInfo.ClassifyId
  910. edbLog.SysUserId = sysUser.AdminId
  911. edbLog.SysUserRealName = sysUser.RealName
  912. edbLog.CreateTime = time.Now()
  913. edbLog.Content = requestBody
  914. edbLog.Status = "删除指标"
  915. edbLog.Method = requestUrl
  916. go data_manage.AddEdbInfoLog(edbLog)
  917. }
  918. // 删除es中的数据
  919. DeleteEdbInfoToEs(edbInfoId)
  920. }
  921. return
  922. }
  923. // MoveEdbClassify 移动指标分类
  924. func MoveEdbClassify(req data_manage.MoveEdbClassifyReq, sysUser *system.Admin, classifyType uint8) (err error, errMsg string) {
  925. // req.ClassifyId, req.ParentClassifyId, req.PrevClassifyId, req.NextClassifyId
  926. classifyId := req.ClassifyId
  927. parentClassifyId := req.ParentClassifyId
  928. prevClassifyId := req.PrevClassifyId
  929. nextClassifyId := req.NextClassifyId
  930. edbInfoId := req.EdbInfoId
  931. prevEdbInfoId := req.PrevEdbInfoId
  932. nextEdbInfoId := req.NextEdbInfoId
  933. //首先确定移动的对象是分类还是指标
  934. //判断上一个节点是分类还是指标
  935. //判断下一个节点是分类还是指标
  936. //同时更新分类目录下的分类sort和指标sort
  937. //更新当前移动的分类或者指标sort
  938. var parentEdbClassifyInfo *data_manage.EdbClassify
  939. if parentClassifyId > 0 {
  940. parentEdbClassifyInfo, err = data_manage.GetEdbClassifyById(parentClassifyId)
  941. if err != nil {
  942. errMsg = "移动失败"
  943. err = errors.New("获取上级分类信息失败,Err:" + err.Error())
  944. return
  945. }
  946. }
  947. //如果有传入 上一个兄弟节点分类id
  948. var (
  949. edbClassifyInfo *data_manage.EdbClassify
  950. prevClassify *data_manage.EdbClassify
  951. nextClassify *data_manage.EdbClassify
  952. edbInfo *data_manage.EdbInfo
  953. prevEdbInfo *data_manage.EdbInfo
  954. nextEdbInfo *data_manage.EdbInfo
  955. prevSort int
  956. nextSort int
  957. )
  958. // 移动对象为分类, 判断权限
  959. if edbInfoId == 0 {
  960. edbClassifyInfo, err = data_manage.GetEdbClassifyById(classifyId)
  961. if err != nil {
  962. if err.Error() == utils.ErrNoRow() {
  963. errMsg = "当前分类不存在"
  964. err = errors.New("获取分类信息失败,Err:" + err.Error())
  965. return
  966. }
  967. errMsg = "移动失败"
  968. err = errors.New("获取分类信息失败,Err:" + err.Error())
  969. return
  970. }
  971. if parentClassifyId > 0 && parentEdbClassifyInfo.Level == 6 {
  972. errMsg = "最高只支持添加6级分类"
  973. err = errors.New(errMsg)
  974. return
  975. }
  976. // 如果是移动目录, 那么校验一下父级目录下是否有重名目录
  977. exists, e := data_manage.GetEdbClassifyByParentIdAndName(parentClassifyId, edbClassifyInfo.ClassifyName, classifyId)
  978. if e != nil && e.Error() != utils.ErrNoRow() {
  979. errMsg = "移动失败"
  980. err = fmt.Errorf("获取父级分类下的同名分类失败, Err: %s", e.Error())
  981. return
  982. }
  983. if exists != nil {
  984. errMsg = "移动失败,分类名称已存在"
  985. return
  986. }
  987. // 权限校验
  988. {
  989. // 已授权分类id
  990. permissionClassifyIdList, tmpErr := data_manage_permission.GetUserEdbClassifyPermissionList(sysUser.AdminId, classifyId)
  991. if tmpErr != nil {
  992. errMsg = "移动失败"
  993. err = errors.New("获取已授权分类id数据失败,Err:" + tmpErr.Error())
  994. return
  995. }
  996. // 数据权限
  997. haveOperaAuth := data_manage_permission.CheckEdbClassifyPermissionByPermissionIdList(edbClassifyInfo.IsJoinPermission, edbClassifyInfo.ClassifyId, permissionClassifyIdList)
  998. if edbClassifyInfo.ClassifyType == 0 { // 普通指标
  999. button := GetEdbClassifyOpButton(sysUser, edbClassifyInfo.SysUserId, haveOperaAuth)
  1000. if !button.MoveButton {
  1001. errMsg = "无操作权限"
  1002. err = errors.New(errMsg)
  1003. return
  1004. }
  1005. } else if edbClassifyInfo.ClassifyType == 1 { // 预测指标
  1006. button := GetPredictEdbClassifyOpButton(sysUser, edbClassifyInfo.SysUserId, haveOperaAuth)
  1007. if !button.MoveButton {
  1008. errMsg = "无操作权限"
  1009. err = errors.New(errMsg)
  1010. return
  1011. }
  1012. }
  1013. }
  1014. } else {
  1015. edbInfo, err = data_manage.GetEdbInfoById(req.EdbInfoId)
  1016. if err != nil {
  1017. if err.Error() == utils.ErrNoRow() {
  1018. errMsg = "当前指标不存在"
  1019. err = errors.New("获取分类信息失败,Err:" + err.Error())
  1020. return
  1021. }
  1022. errMsg = "移动失败"
  1023. err = errors.New("获取分类信息失败,Err:" + err.Error())
  1024. return
  1025. }
  1026. if parentClassifyId == 0 {
  1027. errMsg = "移动失败,指标必须挂在分类下"
  1028. err = errors.New(errMsg)
  1029. return
  1030. }
  1031. var haveOperaAuth bool
  1032. // 权限校验
  1033. {
  1034. haveOperaAuth, err = data_manage_permission.CheckEdbPermissionByEdbInfoId(edbInfo.EdbInfoId, edbInfo.ClassifyId, edbInfo.IsJoinPermission, sysUser.AdminId)
  1035. if err != nil {
  1036. errMsg = "移动失败"
  1037. err = errors.New("获取指标权限信息失败,Err:" + err.Error())
  1038. return
  1039. }
  1040. }
  1041. // 移动权限校验
  1042. button := GetEdbOpButton(sysUser, edbInfo.SysUserId, edbInfo.EdbType, edbInfo.EdbInfoType, haveOperaAuth)
  1043. if !button.MoveButton {
  1044. errMsg = "无操作权限"
  1045. err = errors.New(errMsg)
  1046. return
  1047. }
  1048. }
  1049. if prevClassifyId > 0 {
  1050. prevClassify, err = data_manage.GetEdbClassifyById(prevClassifyId)
  1051. if err != nil {
  1052. errMsg = "移动失败"
  1053. err = errors.New("获取上一个兄弟节点分类信息失败,Err:" + err.Error())
  1054. return
  1055. }
  1056. prevSort = prevClassify.Sort
  1057. } else if prevEdbInfoId > 0 {
  1058. prevEdbInfo, err = data_manage.GetEdbInfoById(prevEdbInfoId)
  1059. if err != nil {
  1060. errMsg = "移动失败"
  1061. err = errors.New("获取上一个兄弟节点分类信息失败,Err:" + err.Error())
  1062. return
  1063. }
  1064. prevSort = prevEdbInfo.Sort
  1065. }
  1066. if nextClassifyId > 0 {
  1067. //下一个兄弟节点
  1068. nextClassify, err = data_manage.GetEdbClassifyById(nextClassifyId)
  1069. if err != nil {
  1070. errMsg = "移动失败"
  1071. err = errors.New("获取下一个兄弟节点分类信息失败,Err:" + err.Error())
  1072. return
  1073. }
  1074. nextSort = nextClassify.Sort
  1075. } else if nextEdbInfoId > 0 {
  1076. //下一个兄弟节点
  1077. nextEdbInfo, err = data_manage.GetEdbInfoById(nextEdbInfoId)
  1078. if err != nil {
  1079. errMsg = "移动失败"
  1080. err = errors.New("获取下一个兄弟节点分类信息失败,Err:" + err.Error())
  1081. return
  1082. }
  1083. nextSort = nextEdbInfo.Sort
  1084. }
  1085. err, errMsg = moveEdbClassify(parentEdbClassifyInfo, edbClassifyInfo, prevClassify, nextClassify, edbInfo, prevEdbInfo, nextEdbInfo, parentClassifyId, prevSort, nextSort, classifyType)
  1086. return
  1087. }
  1088. // moveEdbClassify 移动指标分类
  1089. 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) {
  1090. updateCol := make([]string, 0)
  1091. // 移动对象为分类, 判断分类是否存在
  1092. if edbClassifyInfo != nil {
  1093. oldParentId := edbClassifyInfo.ParentId
  1094. oldLevel := edbClassifyInfo.Level
  1095. var classifyIds []int
  1096. if oldParentId != parentClassifyId {
  1097. //更新子分类对应的level
  1098. childList, e, m := GetChildClassifyByClassifyId(edbClassifyInfo.ClassifyId)
  1099. if e != nil {
  1100. errMsg = "移动失败"
  1101. err = errors.New("查询子分类失败,Err:" + e.Error() + m)
  1102. return
  1103. }
  1104. if len(childList) > 0 {
  1105. for _, v := range childList {
  1106. if v.ClassifyId == edbClassifyInfo.ClassifyId {
  1107. continue
  1108. }
  1109. classifyIds = append(classifyIds, v.ClassifyId)
  1110. }
  1111. }
  1112. }
  1113. //判断上级id是否一致,如果不一致的话,那么需要移动该分类层级
  1114. if edbClassifyInfo.ParentId != parentClassifyId && parentClassifyId != 0 {
  1115. if edbClassifyInfo.Level != parentEdbClassifyInfo.Level+1 { //禁止层级调整
  1116. errMsg = "移动失败"
  1117. err = errors.New("不支持目录层级变更")
  1118. return
  1119. }
  1120. edbClassifyInfo.ParentId = parentEdbClassifyInfo.ClassifyId
  1121. edbClassifyInfo.RootId = parentEdbClassifyInfo.RootId
  1122. edbClassifyInfo.Level = parentEdbClassifyInfo.Level + 1
  1123. edbClassifyInfo.ModifyTime = time.Now()
  1124. updateCol = append(updateCol, "ParentId", "RootId", "Level", "ModifyTime")
  1125. } else if edbClassifyInfo.ParentId != parentClassifyId && parentClassifyId == 0 {
  1126. errMsg = "移动失败"
  1127. err = errors.New("不支持目录层级变更")
  1128. return
  1129. }
  1130. if prevSort > 0 {
  1131. //如果是移动在两个兄弟节点之间
  1132. if nextSort > 0 {
  1133. //下一个兄弟节点
  1134. //如果上一个兄弟与下一个兄弟的排序权重是一致的,那么需要将下一个兄弟(以及下个兄弟的同样排序权重)的排序权重+2,自己变成上一个兄弟的排序权重+1
  1135. if prevSort == nextSort || prevSort == edbClassifyInfo.Sort {
  1136. //变更兄弟节点的排序
  1137. updateSortStr := `sort + 2`
  1138. //变更分类
  1139. if prevClassify != nil {
  1140. _ = data_manage.UpdateEdbClassifySortByParentId(parentClassifyId, prevClassify.ClassifyId, prevClassify.Sort, updateSortStr, classifyType)
  1141. } else {
  1142. _ = data_manage.UpdateEdbClassifySortByParentId(parentClassifyId, 0, prevSort, updateSortStr, classifyType)
  1143. }
  1144. //变更指标
  1145. if prevEdbInfo != nil {
  1146. //变更兄弟节点的排序
  1147. _ = data_manage.UpdateEdbInfoSortByClassifyId(parentClassifyId, prevSort, prevEdbInfo.EdbInfoId, updateSortStr)
  1148. } else {
  1149. _ = data_manage.UpdateEdbInfoSortByClassifyId(parentClassifyId, prevSort, 0, updateSortStr)
  1150. }
  1151. } else {
  1152. //如果下一个兄弟的排序权重正好是上个兄弟节点的下一层,那么需要再加一层了
  1153. if nextSort-prevSort == 1 {
  1154. //变更兄弟节点的排序
  1155. updateSortStr := `sort + 1`
  1156. //变更分类
  1157. if prevClassify != nil {
  1158. _ = data_manage.UpdateEdbClassifySortByParentId(parentClassifyId, prevClassify.ClassifyId, prevSort, updateSortStr, classifyType)
  1159. } else {
  1160. _ = data_manage.UpdateEdbClassifySortByParentId(parentClassifyId, 0, prevSort, updateSortStr, classifyType)
  1161. }
  1162. //变更指标
  1163. if prevEdbInfo != nil {
  1164. //变更兄弟节点的排序
  1165. _ = data_manage.UpdateEdbInfoSortByClassifyId(parentClassifyId, prevSort, prevEdbInfo.EdbInfoId, updateSortStr)
  1166. } else {
  1167. _ = data_manage.UpdateEdbInfoSortByClassifyId(parentClassifyId, prevSort, 0, updateSortStr)
  1168. }
  1169. }
  1170. }
  1171. }
  1172. edbClassifyInfo.Sort = prevSort + 1
  1173. edbClassifyInfo.ModifyTime = time.Now()
  1174. updateCol = append(updateCol, "Sort", "ModifyTime")
  1175. } else if prevClassify == nil && nextClassify == nil && prevEdbInfo == nil && nextEdbInfo == nil && parentClassifyId > 0 {
  1176. //处理只拖动到目录里,默认放到目录底部的情况
  1177. var maxSort int
  1178. maxSort, err = GetEdbClassifyMaxSort(parentClassifyId, classifyType)
  1179. if err != nil {
  1180. errMsg = "移动失败"
  1181. err = errors.New("查询组内排序信息失败,Err:" + err.Error())
  1182. return
  1183. }
  1184. edbClassifyInfo.Sort = maxSort + 1 //那就是排在组内最后一位
  1185. edbClassifyInfo.ModifyTime = time.Now()
  1186. updateCol = append(updateCol, "Sort", "ModifyTime")
  1187. } else {
  1188. // 拖动到父级分类的第一位
  1189. firstClassify, tmpErr := data_manage.GetFirstEdbClassifyByParentId(parentClassifyId)
  1190. if tmpErr != nil && tmpErr.Error() != utils.ErrNoRow() {
  1191. errMsg = "移动失败"
  1192. err = errors.New("获取获取当前父级分类下的排序第一条的分类信息失败,Err:" + tmpErr.Error())
  1193. return
  1194. }
  1195. //如果该分类下存在其他分类,且第一个其他分类的排序等于0,那么需要调整排序
  1196. if firstClassify != nil && firstClassify.Sort == 0 {
  1197. updateSortStr := ` sort + 1 `
  1198. _ = data_manage.UpdateEdbClassifySortByParentId(parentClassifyId, firstClassify.ClassifyId-1, 0, updateSortStr, classifyType)
  1199. //该分类下的所有指标也需要+1
  1200. _ = data_manage.UpdateEdbInfoSortByClassifyId(parentClassifyId, 0, 0, updateSortStr)
  1201. } else {
  1202. //如果该分类下存在指标,且第一个指标的排序等于0,那么需要调整排序
  1203. firstEdb, tErr := data_manage.GetFirstEdbInfoByClassifyId(parentClassifyId)
  1204. if tErr != nil && tErr.Error() != utils.ErrNoRow() {
  1205. errMsg = "移动失败"
  1206. err = errors.New("获取获取当前父级分类下的排序第一条的分类信息失败,Err:" + tErr.Error())
  1207. return
  1208. }
  1209. //如果该分类下存在其他分类,且第一个其他分类的排序等于0,那么需要调整排序
  1210. if firstEdb != nil && firstEdb.Sort == 0 {
  1211. updateSortStr := ` sort + 1 `
  1212. _ = data_manage.UpdateEdbInfoSortByClassifyId(parentClassifyId, 0, firstEdb.EdbInfoId-1, updateSortStr)
  1213. _ = data_manage.UpdateEdbClassifySortByParentId(parentClassifyId, 0, 0, updateSortStr, classifyType)
  1214. }
  1215. }
  1216. edbClassifyInfo.Sort = 0 //那就是排在第一位
  1217. edbClassifyInfo.ModifyTime = time.Now()
  1218. updateCol = append(updateCol, "Sort", "ModifyTime")
  1219. }
  1220. //更新
  1221. if len(updateCol) > 0 {
  1222. err = edbClassifyInfo.Update(updateCol)
  1223. if err != nil {
  1224. errMsg = "移动失败"
  1225. err = errors.New("修改失败,Err:" + err.Error())
  1226. return
  1227. }
  1228. //更新对应分类的root_id和层级
  1229. if oldParentId != parentClassifyId {
  1230. if len(classifyIds) > 0 {
  1231. levelStep := edbClassifyInfo.Level - oldLevel
  1232. err = data_manage.UpdateEdbClassifyChildByParentClassifyId(classifyIds, edbClassifyInfo.RootId, levelStep)
  1233. if err != nil {
  1234. errMsg = "移动失败"
  1235. err = errors.New("更新子分类失败,Err:" + err.Error())
  1236. return
  1237. }
  1238. }
  1239. }
  1240. }
  1241. } else {
  1242. if edbInfo == nil {
  1243. errMsg = "当前指标不存在"
  1244. err = errors.New(errMsg)
  1245. return
  1246. }
  1247. //如果改变了分类,那么移动该指标数据
  1248. if edbInfo.ClassifyId != parentClassifyId {
  1249. edbInfo.ClassifyId = parentClassifyId
  1250. edbInfo.ModifyTime = time.Now()
  1251. updateCol = append(updateCol, "ClassifyId", "ModifyTime")
  1252. }
  1253. if prevSort > 0 {
  1254. //如果是移动在两个兄弟节点之间
  1255. if nextSort > 0 {
  1256. //下一个兄弟节点
  1257. //如果上一个兄弟与下一个兄弟的排序权重是一致的,那么需要将下一个兄弟(以及下个兄弟的同样排序权重)的排序权重+2,自己变成上一个兄弟的排序权重+1
  1258. if prevSort == nextSort || prevSort == edbInfo.Sort {
  1259. //变更兄弟节点的排序
  1260. updateSortStr := `sort + 2`
  1261. //变更分类
  1262. if prevClassify != nil {
  1263. _ = data_manage.UpdateEdbClassifySortByParentId(parentClassifyId, prevClassify.ClassifyId, prevClassify.Sort, updateSortStr, classifyType)
  1264. } else {
  1265. _ = data_manage.UpdateEdbClassifySortByParentId(parentClassifyId, 0, prevSort, updateSortStr, classifyType)
  1266. }
  1267. //变更指标
  1268. if prevEdbInfo != nil {
  1269. //变更兄弟节点的排序
  1270. _ = data_manage.UpdateEdbInfoSortByClassifyId(parentClassifyId, prevSort, prevEdbInfo.EdbInfoId, updateSortStr)
  1271. } else {
  1272. _ = data_manage.UpdateEdbInfoSortByClassifyId(parentClassifyId, prevSort, 0, updateSortStr)
  1273. }
  1274. } else {
  1275. //如果下一个兄弟的排序权重正好是上个兄弟节点的下一层,那么需要再加一层了
  1276. if nextSort-prevSort == 1 {
  1277. //变更兄弟节点的排序
  1278. updateSortStr := `sort + 1`
  1279. //变更分类
  1280. if prevClassify != nil {
  1281. _ = data_manage.UpdateEdbClassifySortByParentId(parentClassifyId, prevClassify.ClassifyId, prevSort, updateSortStr, classifyType)
  1282. } else {
  1283. _ = data_manage.UpdateEdbClassifySortByParentId(parentClassifyId, 0, prevSort, updateSortStr, classifyType)
  1284. }
  1285. //变更指标
  1286. if prevEdbInfo != nil {
  1287. //变更兄弟节点的排序
  1288. _ = data_manage.UpdateEdbInfoSortByClassifyId(parentClassifyId, prevSort, prevEdbInfo.EdbInfoId, updateSortStr)
  1289. } else {
  1290. _ = data_manage.UpdateEdbInfoSortByClassifyId(parentClassifyId, prevSort, 0, updateSortStr)
  1291. }
  1292. }
  1293. }
  1294. }
  1295. edbInfo.Sort = prevSort + 1
  1296. edbInfo.ModifyTime = time.Now()
  1297. updateCol = append(updateCol, "Sort", "ModifyTime")
  1298. } else if prevClassify == nil && nextClassify == nil && prevEdbInfo == nil && nextEdbInfo == nil && parentClassifyId > 0 {
  1299. //处理只拖动到目录里,默认放到目录底部的情况
  1300. var maxSort int
  1301. maxSort, err = GetEdbClassifyMaxSort(parentClassifyId, classifyType)
  1302. if err != nil {
  1303. errMsg = "移动失败"
  1304. err = errors.New("查询组内排序信息失败,Err:" + err.Error())
  1305. return
  1306. }
  1307. edbInfo.Sort = maxSort + 1 //那就是排在组内最后一位
  1308. edbInfo.ModifyTime = time.Now()
  1309. updateCol = append(updateCol, "Sort", "ModifyTime")
  1310. } else {
  1311. // 拖动到父级分类的第一位
  1312. firstClassify, tmpErr := data_manage.GetFirstEdbClassifyByParentId(parentClassifyId)
  1313. if tmpErr != nil && tmpErr.Error() != utils.ErrNoRow() {
  1314. errMsg = "移动失败"
  1315. err = errors.New("获取获取当前父级分类下的排序第一条的分类信息失败,Err:" + tmpErr.Error())
  1316. return
  1317. }
  1318. //如果该分类下存在其他分类,且第一个其他分类的排序等于0,那么需要调整排序
  1319. if firstClassify != nil && firstClassify.Sort == 0 {
  1320. updateSortStr := ` sort + 1 `
  1321. _ = data_manage.UpdateEdbClassifySortByParentId(parentClassifyId, firstClassify.ClassifyId-1, 0, updateSortStr, classifyType)
  1322. //该分类下的所有指标也需要+1
  1323. _ = data_manage.UpdateEdbInfoSortByClassifyId(parentClassifyId, 0, 0, updateSortStr)
  1324. } else {
  1325. //如果该分类下存在指标,且第一个指标的排序等于0,那么需要调整排序
  1326. firstEdb, tErr := data_manage.GetFirstEdbInfoByClassifyId(parentClassifyId)
  1327. if tErr != nil && tErr.Error() != utils.ErrNoRow() {
  1328. errMsg = "移动失败"
  1329. err = errors.New("获取获取当前父级分类下的排序第一条的分类信息失败,Err:" + tErr.Error())
  1330. return
  1331. }
  1332. //如果该分类下存在其他分类,且第一个其他分类的排序等于0,那么需要调整排序
  1333. if firstEdb != nil && firstEdb.Sort == 0 {
  1334. updateSortStr := ` sort + 1 `
  1335. _ = data_manage.UpdateEdbInfoSortByClassifyId(parentClassifyId, 0, firstEdb.EdbInfoId-1, updateSortStr)
  1336. _ = data_manage.UpdateEdbClassifySortByParentId(parentClassifyId, 0, 0, updateSortStr, classifyType)
  1337. }
  1338. }
  1339. edbInfo.Sort = 0 //那就是排在第一位
  1340. edbInfo.ModifyTime = time.Now()
  1341. updateCol = append(updateCol, "Sort", "ModifyTime")
  1342. }
  1343. //更新
  1344. if len(updateCol) > 0 {
  1345. err = edbInfo.Update(updateCol)
  1346. if err != nil {
  1347. errMsg = "移动失败"
  1348. err = errors.New("修改失败,Err:" + err.Error())
  1349. return
  1350. }
  1351. }
  1352. }
  1353. return
  1354. }
  1355. // GetEdbOpButton 获取ETA指标的操作权限
  1356. func GetEdbOpButton(sysUser *system.Admin, belongUserId, edbType, edbInfoType int, haveOperaAuth bool) (button data_manage.EdbClassifyItemsButton) {
  1357. // 没有数据权限就直接返回
  1358. if !haveOperaAuth {
  1359. return
  1360. }
  1361. //2、用户对于自己添加的分类,有权限编辑、移动和删除该分类;
  1362. //3、ficc管理员和超管对所有分类有编辑、移动和删除权限;
  1363. if sysUser.RoleTypeCode == utils.ROLE_TYPE_CODE_ADMIN || sysUser.RoleTypeCode == utils.ROLE_TYPE_CODE_FICC_ADMIN || sysUser.AdminId == belongUserId || sysUser.EdbPermission == 1 {
  1364. button.AddButton = true
  1365. button.OpButton = true
  1366. button.DeleteButton = true
  1367. button.MoveButton = true
  1368. }
  1369. // ETA指标的基础指标才允许插入值
  1370. if edbInfoType == 0 && edbType == 1 {
  1371. button.InsertNewDataButton = true
  1372. }
  1373. button.ShowEdbRelation = true
  1374. button.ShowChartRelation = true
  1375. return
  1376. }
  1377. // GetEdbClassifyOpButton 获取ETA指标分类的操作权限
  1378. func GetEdbClassifyOpButton(sysUser *system.Admin, belongUserId int, haveOperaAuth bool) (button data_manage.EdbClassifyItemsButton) {
  1379. // 没有数据权限就直接返回
  1380. if !haveOperaAuth {
  1381. return
  1382. }
  1383. //ficc管理员和超管和ficc研究员有权限创建和管理分类,可以编辑分类名称(分类名称不允许重复),可以拖动分类,改变分类顺序,可以拖动分类下模型,改变顺序,可以删除分类,若分类下有预测指标,则不允许删除;
  1384. //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) {
  1385. button.AddButton = true
  1386. button.OpButton = true
  1387. button.DeleteButton = true
  1388. button.MoveButton = true
  1389. //}
  1390. return
  1391. }
  1392. // GetPredictEdbOpButton 获取ETA预测指标的操作权限
  1393. func GetPredictEdbOpButton(sysUser *system.Admin, belongUserId int, haveOperaAuth bool) (button data_manage.EdbClassifyItemsButton) {
  1394. // 没有数据权限就直接返回
  1395. if !haveOperaAuth {
  1396. return
  1397. }
  1398. //预测指标的添加人对该预测指标有全部操作权限,ficc管理员、超管对所有预测指标有全部操作权限;
  1399. if sysUser.RoleTypeCode == utils.ROLE_TYPE_CODE_ADMIN || sysUser.RoleTypeCode == utils.ROLE_TYPE_CODE_FICC_ADMIN || sysUser.AdminId == belongUserId || sysUser.PredictEdbPermission == 1 {
  1400. button.AddButton = true
  1401. button.OpButton = true
  1402. button.DeleteButton = true
  1403. button.MoveButton = true
  1404. }
  1405. button.ShowEdbRelation = true
  1406. button.ShowChartRelation = true
  1407. return
  1408. }
  1409. // GetPredictEdbClassifyOpButton 获取ETA预测指标分类的操作权限
  1410. func GetPredictEdbClassifyOpButton(sysUser *system.Admin, belongUserId int, haveOperaAuth bool) (button data_manage.EdbClassifyItemsButton) {
  1411. // 没有数据权限就直接返回
  1412. if !haveOperaAuth {
  1413. return
  1414. }
  1415. //ficc管理员和超管和ficc研究员有权限创建和管理分类,可以编辑分类名称(分类名称不允许重复),可以拖动分类,改变分类顺序,可以拖动分类下模型,改变顺序,可以删除分类,若分类下有预测指标,则不允许删除;
  1416. //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) {
  1417. button.AddButton = true
  1418. button.OpButton = true
  1419. button.DeleteButton = true
  1420. button.MoveButton = true
  1421. //}
  1422. return
  1423. }
  1424. func GetEdbClassifyMaxSort(parentId int, classifyType uint8) (maxSort int, err error) {
  1425. //获取该层级下最大的排序数
  1426. classifyMaxSort, err := data_manage.GetEdbClassifyMaxSort(parentId, classifyType)
  1427. if err != nil {
  1428. return
  1429. }
  1430. maxSort = classifyMaxSort
  1431. edbMaxSort, err := data_manage.GetEdbInfoMaxSortByClassifyId(parentId)
  1432. if err != nil {
  1433. return
  1434. }
  1435. if maxSort < edbMaxSort {
  1436. maxSort = edbMaxSort
  1437. }
  1438. return
  1439. }