edb_classify.go 47 KB

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