edb_classify.go 52 KB

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