edb_classify.go 51 KB

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