edb_classify.go 47 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401
  1. package data
  2. import (
  3. "errors"
  4. "eta/eta_mobile/models"
  5. "eta/eta_mobile/models/data_manage"
  6. "eta/eta_mobile/models/data_manage/cross_variety"
  7. "eta/eta_mobile/models/data_manage/excel"
  8. "eta/eta_mobile/models/system"
  9. "eta/eta_mobile/services/alarm_msg"
  10. "eta/eta_mobile/services/data/data_manage_permission"
  11. "eta/eta_mobile/services/data_stat"
  12. "eta/eta_mobile/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. if level > 6 {
  264. errMsg = `最高只支持添加6级分类`
  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 tErr.Error() == utils.ErrNoRow() {
  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 := 3
  315. if classifyType == 1 {
  316. source = 4
  317. }
  318. go data_manage_permission.InheritParentClassify(source, int(classifyType), int(newId), classifyInfo.ParentId, classifyInfo.ClassifyName)
  319. }
  320. if parentId == 0 { //一级目录的rootId等于自己本身
  321. classifyInfo.ClassifyId = int(newId)
  322. classifyInfo.RootId = int(newId)
  323. err = classifyInfo.Update([]string{"RootId"})
  324. if err != nil {
  325. errMsg = "更新分类失败"
  326. return
  327. }
  328. }
  329. return
  330. }
  331. // EditEdbClassify 编辑指标分类
  332. func EditEdbClassify(classifyId int, classifyName, lang string, sysUser *system.Admin) (err error, errMsg string) {
  333. item, err := data_manage.GetEdbClassifyById(classifyId)
  334. if err != nil {
  335. errMsg = `保存失败`
  336. return
  337. }
  338. // 权限校验
  339. {
  340. // 已授权分类id
  341. permissionClassifyIdList, tmpErr := data_manage_permission.GetUserEdbClassifyPermissionList(sysUser.AdminId, classifyId)
  342. if tmpErr != nil {
  343. errMsg = "保存失败"
  344. err = errors.New("获取已授权分类id数据失败,Err:" + tmpErr.Error())
  345. return
  346. }
  347. // 数据权限
  348. haveOperaAuth := data_manage_permission.CheckEdbClassifyPermissionByPermissionIdList(item.IsJoinPermission, item.ClassifyId, permissionClassifyIdList)
  349. if item.ClassifyType == 0 { // 普通指标
  350. button := GetEdbClassifyOpButton(sysUser, item.SysUserId, haveOperaAuth)
  351. if !button.OpButton {
  352. errMsg = "无操作权限"
  353. err = errors.New(errMsg)
  354. return
  355. }
  356. } else if item.ClassifyType == 1 { // 预测指标
  357. button := GetPredictEdbClassifyOpButton(sysUser, item.SysUserId, haveOperaAuth)
  358. if !button.OpButton {
  359. errMsg = "无操作权限"
  360. err = errors.New(errMsg)
  361. return
  362. }
  363. }
  364. }
  365. // 需要变更的字段
  366. updateCols := make([]string, 0)
  367. switch lang {
  368. case utils.EnLangVersion:
  369. // 名字相同,那么就直接返回
  370. if item.ClassifyNameEn == classifyName {
  371. return
  372. }
  373. // 判断名称是否已存在
  374. count, tmpErr := data_manage.GetEdbClassifyEnCount(classifyName, item.ParentId, item.ClassifyType)
  375. if tmpErr != nil {
  376. err = tmpErr
  377. errMsg = "判断名称是否已存在失败"
  378. return
  379. }
  380. if count > 0 {
  381. errMsg = "分类名称已存在,请重新输入"
  382. err = errors.New(errMsg)
  383. return
  384. }
  385. item.ClassifyNameEn = classifyName
  386. item.LastModifyUserId = sysUser.AdminId
  387. item.LastModifyUserRealName = sysUser.RealName
  388. updateCols = append(updateCols, "ClassifyNameEn", "LastModifyUserId", "LastModifyUserRealName")
  389. default:
  390. // 名字相同,那么就直接返回
  391. if item.ClassifyName == classifyName {
  392. return
  393. }
  394. // 判断名称是否已存在
  395. count, tmpErr := data_manage.GetEdbClassifyCount(classifyName, item.ParentId, item.ClassifyType)
  396. if tmpErr != nil {
  397. err = tmpErr
  398. errMsg = "判断名称是否已存在失败"
  399. return
  400. }
  401. if count > 0 {
  402. errMsg = "分类名称已存在,请重新输入"
  403. err = errors.New(errMsg)
  404. return
  405. }
  406. item.ClassifyName = classifyName
  407. item.LastModifyUserId = sysUser.AdminId
  408. item.LastModifyUserRealName = sysUser.RealName
  409. updateCols = append(updateCols, "ClassifyName", "LastModifyUserId", "LastModifyUserRealName")
  410. }
  411. // 修改数据
  412. if len(updateCols) > 0 {
  413. err = item.Update(updateCols)
  414. if err != nil {
  415. errMsg = "保存失败"
  416. }
  417. }
  418. return
  419. }
  420. // DeleteCheck 删除检测
  421. func DeleteCheck(classifyId, edbInfoId int, sysUser *system.Admin) (deleteStatus int, tipsMsg string, err error, errMsg string) {
  422. //删除分类
  423. if classifyId > 0 && edbInfoId == 0 {
  424. // 查找分类
  425. item, tmpErr := data_manage.GetEdbClassifyById(classifyId)
  426. if tmpErr != nil {
  427. errMsg = `查找分类失败`
  428. err = tmpErr
  429. return
  430. }
  431. // 已授权分类id
  432. permissionClassifyIdList, tmpErr := data_manage_permission.GetUserEdbClassifyPermissionList(sysUser.AdminId, classifyId)
  433. if tmpErr != nil {
  434. errMsg = "删除检测失败"
  435. err = errors.New("获取已授权分类id数据失败,Err:" + tmpErr.Error())
  436. return
  437. }
  438. // 权限校验
  439. {
  440. // 数据权限
  441. haveOperaAuth := data_manage_permission.CheckEdbClassifyPermissionByPermissionIdList(item.IsJoinPermission, item.ClassifyId, permissionClassifyIdList)
  442. if item.ClassifyType == 0 { // 普通指标
  443. button := GetEdbClassifyOpButton(sysUser, item.SysUserId, haveOperaAuth)
  444. if !button.DeleteButton {
  445. errMsg = "无操作权限"
  446. err = errors.New(errMsg)
  447. return
  448. }
  449. } else if item.ClassifyType == 1 { // 预测指标
  450. button := GetPredictEdbClassifyOpButton(sysUser, item.SysUserId, haveOperaAuth)
  451. if !button.DeleteButton {
  452. errMsg = "无操作权限"
  453. err = errors.New(errMsg)
  454. return
  455. }
  456. }
  457. }
  458. //判断分类下,是否含有指标
  459. count, tmpErr := data_manage.GetEdbInfoCountByClassifyId(classifyId)
  460. if tmpErr != nil {
  461. errMsg = "删除失败"
  462. err = errors.New("分类下是否含有指标失败,Err:" + tmpErr.Error())
  463. return
  464. }
  465. if count > 0 {
  466. deleteStatus = 1
  467. tipsMsg = "若目录关联指标不可删除"
  468. }
  469. }
  470. if deleteStatus != 1 && edbInfoId == 0 {
  471. classifyCount, tmpErr := data_manage.GetClassifyCountByClassifyId(classifyId)
  472. if tmpErr != nil && tmpErr.Error() != utils.ErrNoRow() {
  473. errMsg = "删除失败"
  474. err = errors.New("分类下是否含有指标失败,Err:" + tmpErr.Error())
  475. return
  476. }
  477. if classifyCount > 0 {
  478. deleteStatus = 2
  479. tipsMsg = "确认删除当前目录及包含的子目录吗"
  480. }
  481. }
  482. //删除指标
  483. if edbInfoId > 0 {
  484. edbInfo, tmpErr := data_manage.GetEdbInfoById(edbInfoId)
  485. if tmpErr != nil {
  486. if tmpErr.Error() == utils.ErrNoRow() {
  487. errMsg = "指标已删除,请刷新页面"
  488. err = errors.New("指标不存在,Err:" + tmpErr.Error())
  489. return
  490. } else {
  491. errMsg = "删除失败"
  492. err = errors.New("删除失败,获取指标信息失败,Err:" + tmpErr.Error())
  493. return
  494. }
  495. }
  496. if edbInfo == nil {
  497. errMsg = "指标已删除,请刷新页面"
  498. return
  499. }
  500. //判断指标是否用于作图,如果用于作图,则不可删除
  501. chartCount, tmpErr := data_manage.GetChartEdbMappingCount(edbInfoId)
  502. if tmpErr != nil && tmpErr.Error() != utils.ErrNoRow() {
  503. errMsg = "删除失败"
  504. err = errors.New("判断指标是否被用于作图失败,Err:" + tmpErr.Error())
  505. return
  506. }
  507. if chartCount > 0 {
  508. deleteStatus = 3
  509. tipsMsg = "当前指标已用作画图,不可删除"
  510. return
  511. }
  512. //判断指标是否用于计算
  513. {
  514. calculateCount, tmpErr := data_manage.GetEdbInfoCalculateMappingCount(edbInfoId)
  515. if tmpErr != nil && tmpErr.Error() != utils.ErrNoRow() {
  516. errMsg = "删除失败"
  517. err = errors.New("判断指标是否被用于计算失败,GetEdbInfoCalculateCount Err:" + tmpErr.Error())
  518. return
  519. }
  520. if calculateCount > 0 {
  521. deleteStatus = 4
  522. tipsMsg = "当前指标已用作,指标运算,不可删除"
  523. return
  524. }
  525. }
  526. //如果是普通指标,那么还需要判断是否被预测指标作为源指标
  527. if edbInfo.EdbInfoType == 0 {
  528. predictEdbInfoCount, tmpErr := data_manage.GetPredictEdbConfCount(edbInfoId)
  529. if tmpErr != nil && tmpErr.Error() != utils.ErrNoRow() {
  530. errMsg = "删除失败"
  531. err = errors.New("判断指标是否被用于预测指标失败,Err:" + tmpErr.Error())
  532. return
  533. }
  534. if predictEdbInfoCount > 0 {
  535. deleteStatus = 3
  536. tipsMsg = "当前指标已用作预测指标,不可删除"
  537. return
  538. }
  539. }
  540. // 判断指标是否用作表格引用
  541. {
  542. calculateCount, tmpErr := excel.GetNoCustomAnalysisExcelEdbMappingCount(edbInfoId)
  543. if tmpErr != nil && tmpErr.Error() != utils.ErrNoRow() {
  544. errMsg = "删除失败"
  545. err = errors.New("判断指标是否用作表格引用,GetNoCustomAnalysisExcelEdbMappingCount Err:" + tmpErr.Error())
  546. return
  547. }
  548. if calculateCount > 0 {
  549. deleteStatus = 3
  550. tipsMsg = "当前指标已添加到表格,不可删除"
  551. return
  552. }
  553. }
  554. // 判断指标是否用作跨品种图表使用
  555. {
  556. calculateCount, tmpErr := cross_variety.GetCountByEdbInfoId(edbInfoId)
  557. if tmpErr != nil && tmpErr.Error() != utils.ErrNoRow() {
  558. errMsg = "删除失败"
  559. err = errors.New("判断指标是否用作跨品种图表使用,GetCountByEdbInfoId Err:" + tmpErr.Error())
  560. return
  561. }
  562. if calculateCount > 0 {
  563. deleteStatus = 3
  564. tipsMsg = "当前指标已添加到跨品种分析,不可删除"
  565. return
  566. }
  567. }
  568. }
  569. return
  570. }
  571. // Delete 删除分类/指标
  572. func Delete(classifyId, edbInfoId int, sysUser *system.Admin, requestBody, requestUrl string) (nextItem *data_manage.EdbInfo, err error, errMsg string) {
  573. //删除分类
  574. if classifyId > 0 && edbInfoId == 0 {
  575. // 查找分类
  576. item, tmpErr := data_manage.GetEdbClassifyById(classifyId)
  577. if tmpErr != nil {
  578. errMsg = `查找分类失败`
  579. err = tmpErr
  580. return
  581. }
  582. // 已授权分类id
  583. permissionClassifyIdList, tmpErr := data_manage_permission.GetUserEdbClassifyPermissionList(sysUser.AdminId, classifyId)
  584. if tmpErr != nil {
  585. errMsg = "删除失败"
  586. err = errors.New("获取已授权分类id数据失败,Err:" + tmpErr.Error())
  587. return
  588. }
  589. // 权限校验
  590. {
  591. // 数据权限
  592. haveOperaAuth := data_manage_permission.CheckEdbClassifyPermissionByPermissionIdList(item.IsJoinPermission, item.ClassifyId, permissionClassifyIdList)
  593. if item.ClassifyType == 0 { // 普通指标
  594. button := GetEdbClassifyOpButton(sysUser, item.SysUserId, haveOperaAuth)
  595. if !button.DeleteButton {
  596. errMsg = "无操作权限"
  597. err = errors.New(errMsg)
  598. return
  599. }
  600. } else if item.ClassifyType == 1 { // 预测指标
  601. button := GetPredictEdbClassifyOpButton(sysUser, item.SysUserId, haveOperaAuth)
  602. if !button.DeleteButton {
  603. errMsg = "无操作权限"
  604. err = errors.New(errMsg)
  605. return
  606. }
  607. }
  608. }
  609. //判断是否含有指标
  610. count, tmpErr := data_manage.GetEdbInfoCountByClassifyId(classifyId)
  611. if tmpErr != nil && tmpErr.Error() != utils.ErrNoRow() {
  612. errMsg = "删除失败"
  613. err = errors.New("分类下是否含有指标失败,Err:" + tmpErr.Error())
  614. return
  615. }
  616. if count > 0 {
  617. errMsg = "该目录下存在关联指标,不可删除"
  618. return
  619. }
  620. tmpErr = data_manage.DeleteEdbClassify(classifyId)
  621. if tmpErr != nil {
  622. errMsg = "删除失败"
  623. err = errors.New("删除失败,Err:" + tmpErr.Error())
  624. return
  625. }
  626. }
  627. //删除指标
  628. if edbInfoId > 0 {
  629. edbInfo, tmpErr := data_manage.GetEdbInfoById(edbInfoId)
  630. if tmpErr != nil {
  631. if tmpErr.Error() == utils.ErrNoRow() {
  632. errMsg = "指标已删除,请刷新页面"
  633. err = errors.New("指标不存在,Err:" + tmpErr.Error())
  634. return
  635. } else {
  636. errMsg = "删除失败"
  637. err = errors.New("删除失败,获取指标信息失败,Err:" + tmpErr.Error())
  638. return
  639. }
  640. }
  641. if edbInfo == nil {
  642. errMsg = "指标已删除,请刷新页面"
  643. return
  644. }
  645. //判断指标是否用于作图,如果用于作图,则不可删除
  646. chartCount, tmpErr := data_manage.GetChartEdbMappingCount(edbInfoId)
  647. if tmpErr != nil && tmpErr.Error() != utils.ErrNoRow() {
  648. errMsg = "删除失败"
  649. err = errors.New("判断指标是否被用于作图失败,Err:" + tmpErr.Error())
  650. return
  651. }
  652. if chartCount > 0 {
  653. errMsg = "当前指标已用作画图,不可删除"
  654. return
  655. }
  656. //如果是普通指标,那么还需要判断是否被预测指标作为源指标
  657. if edbInfo.EdbInfoType == 0 {
  658. predictEdbInfoCount, tmpErr := data_manage.GetPredictEdbConfCount(edbInfoId)
  659. if tmpErr != nil && tmpErr.Error() != utils.ErrNoRow() {
  660. errMsg = "删除失败"
  661. err = errors.New("判断指标是否被用于预测指标失败,Err:" + tmpErr.Error())
  662. return
  663. }
  664. if predictEdbInfoCount > 0 {
  665. errMsg = "当前指标已用作预测指标,不可删除"
  666. return
  667. }
  668. }
  669. //判断指标是否用作其他指标的计算
  670. {
  671. calculateCount, tmpErr := data_manage.GetEdbInfoCalculateMappingCount(edbInfoId)
  672. if tmpErr != nil && tmpErr.Error() != utils.ErrNoRow() {
  673. errMsg = "删除失败"
  674. err = errors.New("判断指标是否被用于计算失败,GetEdbInfoCalculateCount Err:" + tmpErr.Error())
  675. return
  676. }
  677. if calculateCount > 0 {
  678. errMsg = "当前指标已用作,指标运算,不可删除"
  679. return
  680. }
  681. }
  682. // 判断指标是否用作表格引用
  683. {
  684. calculateCount, tmpErr := excel.GetNoCustomAnalysisExcelEdbMappingCount(edbInfoId)
  685. if tmpErr != nil && tmpErr.Error() != utils.ErrNoRow() {
  686. errMsg = "删除失败"
  687. err = errors.New("判断指标是否用作表格引用,GetNoCustomAnalysisExcelEdbMappingCount Err:" + tmpErr.Error())
  688. return
  689. }
  690. if calculateCount > 0 {
  691. errMsg = "当前指标已添加到表格,不可删除"
  692. return
  693. }
  694. }
  695. // 判断指标是否用作跨品种图表使用
  696. {
  697. calculateCount, tmpErr := cross_variety.GetCountByEdbInfoId(edbInfoId)
  698. if tmpErr != nil && tmpErr.Error() != utils.ErrNoRow() {
  699. errMsg = "删除失败"
  700. err = errors.New("判断指标是否用作跨品种图表使用,GetCountByEdbInfoId Err:" + tmpErr.Error())
  701. return
  702. }
  703. if calculateCount > 0 {
  704. errMsg = "当前指标已添加到跨品种分析,不可删除"
  705. return
  706. }
  707. }
  708. //真实删除
  709. tmpErr = data_manage.DeleteEdbInfoAndData(edbInfo.EdbInfoId, edbInfo.Source, edbInfo.SubSource)
  710. if tmpErr != nil {
  711. errMsg = "删除失败"
  712. err = errors.New("删除失败,Err:" + tmpErr.Error())
  713. return
  714. }
  715. go data_stat.AddEdbDeleteLog(edbInfo, sysUser)
  716. // 如果删除的指标是自定义分析的来源,那么还需要删除指标与excel的关系
  717. if edbInfo.Source == utils.DATA_SOURCE_CALCULATE_ZDYFX {
  718. tmpErr = excel.DeleteCustomAnalysisExcelEdbMappingByEdbInfoId(edbInfo.EdbInfoId)
  719. if tmpErr != nil {
  720. alarm_msg.SendAlarmMsg(fmt.Sprintf("删除指标时,需要删除与自定义分析的关系失败,指标ID:%d,Err:%s", edbInfo.EdbInfoId, tmpErr.Error()), 3)
  721. }
  722. }
  723. // 返回下一个表格的信息
  724. {
  725. var condition string
  726. var pars []interface{}
  727. condition += " AND edb_info_type=? AND classify_id=? "
  728. pars = append(pars, edbInfo.EdbInfoType, edbInfo.ClassifyId)
  729. condition += " AND (sort>? OR (sort=? AND edb_info_id<?) ) "
  730. pars = append(pars, edbInfo.Sort, edbInfo.Sort, edbInfo.EdbInfoId)
  731. nextItem, tmpErr = data_manage.GetNextEdbInfoByCondition(condition, pars)
  732. if tmpErr != nil && tmpErr.Error() != utils.ErrNoRow() {
  733. errMsg = "删除失败"
  734. err = errors.New("获取下一级预测指标信息失败,Err:" + tmpErr.Error())
  735. return
  736. }
  737. // 如果没找到,那么查找下一个分类的第一个表格
  738. if nextItem == nil {
  739. currClassifyInfo, tmpErr := data_manage.GetEdbClassifyById(edbInfo.ClassifyId)
  740. if tmpErr != nil && tmpErr.Error() != utils.ErrNoRow() {
  741. errMsg = "删除失败"
  742. err = errors.New("获取当前预测指标分类信息失败,Err:" + tmpErr.Error())
  743. return
  744. }
  745. nextItem, err = data_manage.GetNextEdbInfo(edbInfo.ClassifyId, currClassifyInfo.Sort, int(currClassifyInfo.ClassifyType), edbInfo.EdbInfoType)
  746. if err != nil && err.Error() != utils.ErrNoRow() {
  747. errMsg = "删除失败"
  748. err = errors.New("获取下一级预测指标信息失败,Err:" + tmpErr.Error())
  749. return
  750. }
  751. }
  752. }
  753. //新增操作日志
  754. {
  755. edbLog := new(data_manage.EdbInfoLog)
  756. edbLog.EdbInfoId = edbInfo.EdbInfoId
  757. edbLog.SourceName = edbInfo.SourceName
  758. edbLog.Source = edbInfo.Source
  759. edbLog.EdbCode = edbInfo.EdbCode
  760. edbLog.EdbName = edbInfo.EdbName
  761. edbLog.ClassifyId = edbInfo.ClassifyId
  762. edbLog.SysUserId = sysUser.AdminId
  763. edbLog.SysUserRealName = sysUser.RealName
  764. edbLog.CreateTime = time.Now()
  765. edbLog.Content = requestBody
  766. edbLog.Status = "删除指标"
  767. edbLog.Method = requestUrl
  768. go data_manage.AddEdbInfoLog(edbLog)
  769. }
  770. // 删除es中的数据
  771. DeleteEdbInfoToEs(edbInfoId)
  772. }
  773. return
  774. }
  775. // MoveEdbClassify 移动指标分类
  776. func MoveEdbClassify(req data_manage.MoveEdbClassifyReq, sysUser *system.Admin, classifyType uint8) (err error, errMsg string) {
  777. // req.ClassifyId, req.ParentClassifyId, req.PrevClassifyId, req.NextClassifyId
  778. classifyId := req.ClassifyId
  779. parentClassifyId := req.ParentClassifyId
  780. prevClassifyId := req.PrevClassifyId
  781. nextClassifyId := req.NextClassifyId
  782. edbInfoId := req.EdbInfoId
  783. prevEdbInfoId := req.PrevEdbInfoId
  784. nextEdbInfoId := req.NextEdbInfoId
  785. //首先确定移动的对象是分类还是指标
  786. //判断上一个节点是分类还是指标
  787. //判断下一个节点是分类还是指标
  788. //同时更新分类目录下的分类sort和指标sort
  789. //更新当前移动的分类或者指标sort
  790. var parentEdbClassifyInfo *data_manage.EdbClassify
  791. if parentClassifyId > 0 {
  792. parentEdbClassifyInfo, err = data_manage.GetEdbClassifyById(parentClassifyId)
  793. if err != nil {
  794. errMsg = "移动失败"
  795. err = errors.New("获取上级分类信息失败,Err:" + err.Error())
  796. return
  797. }
  798. }
  799. //如果有传入 上一个兄弟节点分类id
  800. var (
  801. edbClassifyInfo *data_manage.EdbClassify
  802. prevClassify *data_manage.EdbClassify
  803. nextClassify *data_manage.EdbClassify
  804. edbInfo *data_manage.EdbInfo
  805. prevEdbInfo *data_manage.EdbInfo
  806. nextEdbInfo *data_manage.EdbInfo
  807. prevSort int
  808. nextSort int
  809. )
  810. // 移动对象为分类, 判断权限
  811. if edbInfoId == 0 {
  812. edbClassifyInfo, err = data_manage.GetEdbClassifyById(classifyId)
  813. if err != nil {
  814. if err.Error() == utils.ErrNoRow() {
  815. errMsg = "当前分类不存在"
  816. err = errors.New("获取分类信息失败,Err:" + err.Error())
  817. return
  818. }
  819. errMsg = "移动失败"
  820. err = errors.New("获取分类信息失败,Err:" + err.Error())
  821. return
  822. }
  823. if parentClassifyId > 0 && parentEdbClassifyInfo.Level == 6 {
  824. errMsg = "最高只支持添加6级分类"
  825. err = errors.New(errMsg)
  826. return
  827. }
  828. // 如果是移动目录, 那么校验一下父级目录下是否有重名目录
  829. exists, e := data_manage.GetEdbClassifyByParentIdAndName(parentClassifyId, edbClassifyInfo.ClassifyName, classifyId)
  830. if e != nil && e.Error() != utils.ErrNoRow() {
  831. errMsg = "移动失败"
  832. err = fmt.Errorf("获取父级分类下的同名分类失败, Err: %s", e.Error())
  833. return
  834. }
  835. if exists != nil {
  836. errMsg = "移动失败,分类名称已存在"
  837. return
  838. }
  839. // 权限校验
  840. {
  841. // 已授权分类id
  842. permissionClassifyIdList, tmpErr := data_manage_permission.GetUserEdbClassifyPermissionList(sysUser.AdminId, classifyId)
  843. if tmpErr != nil {
  844. errMsg = "移动失败"
  845. err = errors.New("获取已授权分类id数据失败,Err:" + tmpErr.Error())
  846. return
  847. }
  848. // 数据权限
  849. haveOperaAuth := data_manage_permission.CheckEdbClassifyPermissionByPermissionIdList(edbClassifyInfo.IsJoinPermission, edbClassifyInfo.ClassifyId, permissionClassifyIdList)
  850. if edbClassifyInfo.ClassifyType == 0 { // 普通指标
  851. button := GetEdbClassifyOpButton(sysUser, edbClassifyInfo.SysUserId, haveOperaAuth)
  852. if !button.MoveButton {
  853. errMsg = "无操作权限"
  854. err = errors.New(errMsg)
  855. return
  856. }
  857. } else if edbClassifyInfo.ClassifyType == 1 { // 预测指标
  858. button := GetPredictEdbClassifyOpButton(sysUser, edbClassifyInfo.SysUserId, haveOperaAuth)
  859. if !button.MoveButton {
  860. errMsg = "无操作权限"
  861. err = errors.New(errMsg)
  862. return
  863. }
  864. }
  865. }
  866. } else {
  867. edbInfo, err = data_manage.GetEdbInfoById(req.EdbInfoId)
  868. if err != nil {
  869. if err.Error() == utils.ErrNoRow() {
  870. errMsg = "当前指标不存在"
  871. err = errors.New("获取分类信息失败,Err:" + err.Error())
  872. return
  873. }
  874. errMsg = "移动失败"
  875. err = errors.New("获取分类信息失败,Err:" + err.Error())
  876. return
  877. }
  878. if parentClassifyId == 0 {
  879. errMsg = "移动失败,指标必须挂在分类下"
  880. err = errors.New(errMsg)
  881. return
  882. }
  883. var haveOperaAuth bool
  884. // 权限校验
  885. {
  886. haveOperaAuth, err = data_manage_permission.CheckEdbPermissionByEdbInfoId(edbInfo.EdbInfoId, edbInfo.ClassifyId, edbInfo.IsJoinPermission, sysUser.AdminId)
  887. if err != nil {
  888. errMsg = "移动失败"
  889. err = errors.New("获取指标权限信息失败,Err:" + err.Error())
  890. return
  891. }
  892. }
  893. // 移动权限校验
  894. button := GetEdbOpButton(sysUser, edbInfo.SysUserId, edbInfo.EdbType, edbInfo.EdbInfoType, haveOperaAuth)
  895. if !button.MoveButton {
  896. errMsg = "无操作权限"
  897. err = errors.New(errMsg)
  898. return
  899. }
  900. }
  901. if prevClassifyId > 0 {
  902. prevClassify, err = data_manage.GetEdbClassifyById(prevClassifyId)
  903. if err != nil {
  904. errMsg = "移动失败"
  905. err = errors.New("获取上一个兄弟节点分类信息失败,Err:" + err.Error())
  906. return
  907. }
  908. prevSort = prevClassify.Sort
  909. } else if prevEdbInfoId > 0 {
  910. prevEdbInfo, err = data_manage.GetEdbInfoById(prevEdbInfoId)
  911. if err != nil {
  912. errMsg = "移动失败"
  913. err = errors.New("获取上一个兄弟节点分类信息失败,Err:" + err.Error())
  914. return
  915. }
  916. prevSort = prevEdbInfo.Sort
  917. }
  918. if nextClassifyId > 0 {
  919. //下一个兄弟节点
  920. nextClassify, err = data_manage.GetEdbClassifyById(nextClassifyId)
  921. if err != nil {
  922. errMsg = "移动失败"
  923. err = errors.New("获取下一个兄弟节点分类信息失败,Err:" + err.Error())
  924. return
  925. }
  926. nextSort = nextClassify.Sort
  927. } else if nextEdbInfoId > 0 {
  928. //下一个兄弟节点
  929. nextEdbInfo, err = data_manage.GetEdbInfoById(nextEdbInfoId)
  930. if err != nil {
  931. errMsg = "移动失败"
  932. err = errors.New("获取下一个兄弟节点分类信息失败,Err:" + err.Error())
  933. return
  934. }
  935. nextSort = nextEdbInfo.Sort
  936. }
  937. err, errMsg = moveEdbClassify(parentEdbClassifyInfo, edbClassifyInfo, prevClassify, nextClassify, edbInfo, prevEdbInfo, nextEdbInfo, parentClassifyId, prevSort, nextSort, classifyType)
  938. return
  939. }
  940. // moveEdbClassify 移动指标分类
  941. 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) {
  942. updateCol := make([]string, 0)
  943. // 移动对象为分类, 判断分类是否存在
  944. if edbClassifyInfo != nil {
  945. oldParentId := edbClassifyInfo.ParentId
  946. oldLevel := edbClassifyInfo.Level
  947. var classifyIds []int
  948. if oldParentId != parentClassifyId {
  949. //更新子分类对应的level
  950. childList, e, m := GetChildClassifyByClassifyId(edbClassifyInfo.ClassifyId)
  951. if e != nil {
  952. errMsg = "移动失败"
  953. err = errors.New("查询子分类失败,Err:" + e.Error() + m)
  954. return
  955. }
  956. if len(childList) > 0 {
  957. for _, v := range childList {
  958. if v.ClassifyId == edbClassifyInfo.ClassifyId {
  959. continue
  960. }
  961. classifyIds = append(classifyIds, v.ClassifyId)
  962. }
  963. }
  964. }
  965. //判断上级id是否一致,如果不一致的话,那么需要移动该分类层级
  966. if edbClassifyInfo.ParentId != parentClassifyId && parentClassifyId != 0 {
  967. if edbClassifyInfo.Level != parentEdbClassifyInfo.Level+1 { //禁止层级调整
  968. errMsg = "移动失败"
  969. err = errors.New("不支持目录层级变更")
  970. return
  971. }
  972. edbClassifyInfo.ParentId = parentEdbClassifyInfo.ClassifyId
  973. edbClassifyInfo.RootId = parentEdbClassifyInfo.RootId
  974. edbClassifyInfo.Level = parentEdbClassifyInfo.Level + 1
  975. edbClassifyInfo.ModifyTime = time.Now()
  976. updateCol = append(updateCol, "ParentId", "RootId", "Level", "ModifyTime")
  977. } else if edbClassifyInfo.ParentId != parentClassifyId && parentClassifyId == 0 {
  978. errMsg = "移动失败"
  979. err = errors.New("不支持目录层级变更")
  980. return
  981. }
  982. if prevSort > 0 {
  983. //如果是移动在两个兄弟节点之间
  984. if nextSort > 0 {
  985. //下一个兄弟节点
  986. //如果上一个兄弟与下一个兄弟的排序权重是一致的,那么需要将下一个兄弟(以及下个兄弟的同样排序权重)的排序权重+2,自己变成上一个兄弟的排序权重+1
  987. if prevSort == nextSort || prevSort == edbClassifyInfo.Sort {
  988. //变更兄弟节点的排序
  989. updateSortStr := `sort + 2`
  990. //变更分类
  991. if prevClassify != nil {
  992. _ = data_manage.UpdateEdbClassifySortByParentId(parentClassifyId, prevClassify.ClassifyId, prevClassify.Sort, updateSortStr, classifyType)
  993. } else {
  994. _ = data_manage.UpdateEdbClassifySortByParentId(parentClassifyId, 0, prevSort, updateSortStr, classifyType)
  995. }
  996. //变更指标
  997. if prevEdbInfo != nil {
  998. //变更兄弟节点的排序
  999. _ = data_manage.UpdateEdbInfoSortByClassifyId(parentClassifyId, prevSort, prevEdbInfo.EdbInfoId, updateSortStr)
  1000. } else {
  1001. _ = data_manage.UpdateEdbInfoSortByClassifyId(parentClassifyId, prevSort, 0, updateSortStr)
  1002. }
  1003. } else {
  1004. //如果下一个兄弟的排序权重正好是上个兄弟节点的下一层,那么需要再加一层了
  1005. if nextSort-prevSort == 1 {
  1006. //变更兄弟节点的排序
  1007. updateSortStr := `sort + 1`
  1008. //变更分类
  1009. if prevClassify != nil {
  1010. _ = data_manage.UpdateEdbClassifySortByParentId(parentClassifyId, prevClassify.ClassifyId, prevSort, updateSortStr, classifyType)
  1011. } else {
  1012. _ = data_manage.UpdateEdbClassifySortByParentId(parentClassifyId, 0, prevSort, updateSortStr, classifyType)
  1013. }
  1014. //变更指标
  1015. if prevEdbInfo != nil {
  1016. //变更兄弟节点的排序
  1017. _ = data_manage.UpdateEdbInfoSortByClassifyId(parentClassifyId, prevSort, prevEdbInfo.EdbInfoId, updateSortStr)
  1018. } else {
  1019. _ = data_manage.UpdateEdbInfoSortByClassifyId(parentClassifyId, prevSort, 0, updateSortStr)
  1020. }
  1021. }
  1022. }
  1023. }
  1024. edbClassifyInfo.Sort = prevSort + 1
  1025. edbClassifyInfo.ModifyTime = time.Now()
  1026. updateCol = append(updateCol, "Sort", "ModifyTime")
  1027. } else if prevClassify == nil && nextClassify == nil && prevEdbInfo == nil && nextEdbInfo == nil && parentClassifyId > 0 {
  1028. //处理只拖动到目录里,默认放到目录底部的情况
  1029. var maxSort int
  1030. maxSort, err = GetEdbClassifyMaxSort(parentClassifyId, classifyType)
  1031. if err != nil {
  1032. errMsg = "移动失败"
  1033. err = errors.New("查询组内排序信息失败,Err:" + err.Error())
  1034. return
  1035. }
  1036. edbClassifyInfo.Sort = maxSort + 1 //那就是排在组内最后一位
  1037. edbClassifyInfo.ModifyTime = time.Now()
  1038. updateCol = append(updateCol, "Sort", "ModifyTime")
  1039. } else {
  1040. // 拖动到父级分类的第一位
  1041. firstClassify, tmpErr := data_manage.GetFirstEdbClassifyByParentId(parentClassifyId)
  1042. if tmpErr != nil && tmpErr.Error() != utils.ErrNoRow() {
  1043. errMsg = "移动失败"
  1044. err = errors.New("获取获取当前父级分类下的排序第一条的分类信息失败,Err:" + tmpErr.Error())
  1045. return
  1046. }
  1047. //如果该分类下存在其他分类,且第一个其他分类的排序等于0,那么需要调整排序
  1048. if firstClassify != nil && firstClassify.Sort == 0 {
  1049. updateSortStr := ` sort + 1 `
  1050. _ = data_manage.UpdateEdbClassifySortByParentId(parentClassifyId, firstClassify.ClassifyId-1, 0, updateSortStr, classifyType)
  1051. //该分类下的所有指标也需要+1
  1052. _ = data_manage.UpdateEdbInfoSortByClassifyId(parentClassifyId, 0, 0, updateSortStr)
  1053. } else {
  1054. //如果该分类下存在指标,且第一个指标的排序等于0,那么需要调整排序
  1055. firstEdb, tErr := data_manage.GetFirstEdbInfoByClassifyId(parentClassifyId)
  1056. if tErr != nil && tErr.Error() != utils.ErrNoRow() {
  1057. errMsg = "移动失败"
  1058. err = errors.New("获取获取当前父级分类下的排序第一条的分类信息失败,Err:" + tErr.Error())
  1059. return
  1060. }
  1061. //如果该分类下存在其他分类,且第一个其他分类的排序等于0,那么需要调整排序
  1062. if firstEdb != nil && firstEdb.Sort == 0 {
  1063. updateSortStr := ` sort + 1 `
  1064. _ = data_manage.UpdateEdbInfoSortByClassifyId(parentClassifyId, 0, firstEdb.EdbInfoId-1, updateSortStr)
  1065. _ = data_manage.UpdateEdbClassifySortByParentId(parentClassifyId, 0, 0, updateSortStr, classifyType)
  1066. }
  1067. }
  1068. edbClassifyInfo.Sort = 0 //那就是排在第一位
  1069. edbClassifyInfo.ModifyTime = time.Now()
  1070. updateCol = append(updateCol, "Sort", "ModifyTime")
  1071. }
  1072. //更新
  1073. if len(updateCol) > 0 {
  1074. err = edbClassifyInfo.Update(updateCol)
  1075. if err != nil {
  1076. errMsg = "移动失败"
  1077. err = errors.New("修改失败,Err:" + err.Error())
  1078. return
  1079. }
  1080. //更新对应分类的root_id和层级
  1081. if oldParentId != parentClassifyId {
  1082. if len(classifyIds) > 0 {
  1083. levelStep := edbClassifyInfo.Level - oldLevel
  1084. err = data_manage.UpdateEdbClassifyChildByParentClassifyId(classifyIds, edbClassifyInfo.RootId, levelStep)
  1085. if err != nil {
  1086. errMsg = "移动失败"
  1087. err = errors.New("更新子分类失败,Err:" + err.Error())
  1088. return
  1089. }
  1090. }
  1091. }
  1092. }
  1093. } else {
  1094. if edbInfo == nil {
  1095. errMsg = "当前指标不存在"
  1096. err = errors.New(errMsg)
  1097. return
  1098. }
  1099. //如果改变了分类,那么移动该指标数据
  1100. if edbInfo.ClassifyId != parentClassifyId {
  1101. edbInfo.ClassifyId = parentClassifyId
  1102. edbInfo.ModifyTime = time.Now()
  1103. updateCol = append(updateCol, "ClassifyId", "ModifyTime")
  1104. }
  1105. if prevSort > 0 {
  1106. //如果是移动在两个兄弟节点之间
  1107. if nextSort > 0 {
  1108. //下一个兄弟节点
  1109. //如果上一个兄弟与下一个兄弟的排序权重是一致的,那么需要将下一个兄弟(以及下个兄弟的同样排序权重)的排序权重+2,自己变成上一个兄弟的排序权重+1
  1110. if prevSort == nextSort || prevSort == edbInfo.Sort {
  1111. //变更兄弟节点的排序
  1112. updateSortStr := `sort + 2`
  1113. //变更分类
  1114. if prevClassify != nil {
  1115. _ = data_manage.UpdateEdbClassifySortByParentId(parentClassifyId, prevClassify.ClassifyId, prevClassify.Sort, updateSortStr, classifyType)
  1116. } else {
  1117. _ = data_manage.UpdateEdbClassifySortByParentId(parentClassifyId, 0, prevSort, updateSortStr, classifyType)
  1118. }
  1119. //变更指标
  1120. if prevEdbInfo != nil {
  1121. //变更兄弟节点的排序
  1122. _ = data_manage.UpdateEdbInfoSortByClassifyId(parentClassifyId, prevSort, prevEdbInfo.EdbInfoId, updateSortStr)
  1123. } else {
  1124. _ = data_manage.UpdateEdbInfoSortByClassifyId(parentClassifyId, prevSort, 0, updateSortStr)
  1125. }
  1126. } else {
  1127. //如果下一个兄弟的排序权重正好是上个兄弟节点的下一层,那么需要再加一层了
  1128. if nextSort-prevSort == 1 {
  1129. //变更兄弟节点的排序
  1130. updateSortStr := `sort + 1`
  1131. //变更分类
  1132. if prevClassify != nil {
  1133. _ = data_manage.UpdateEdbClassifySortByParentId(parentClassifyId, prevClassify.ClassifyId, prevSort, updateSortStr, classifyType)
  1134. } else {
  1135. _ = data_manage.UpdateEdbClassifySortByParentId(parentClassifyId, 0, prevSort, updateSortStr, classifyType)
  1136. }
  1137. //变更指标
  1138. if prevEdbInfo != nil {
  1139. //变更兄弟节点的排序
  1140. _ = data_manage.UpdateEdbInfoSortByClassifyId(parentClassifyId, prevSort, prevEdbInfo.EdbInfoId, updateSortStr)
  1141. } else {
  1142. _ = data_manage.UpdateEdbInfoSortByClassifyId(parentClassifyId, prevSort, 0, updateSortStr)
  1143. }
  1144. }
  1145. }
  1146. }
  1147. edbInfo.Sort = prevSort + 1
  1148. edbInfo.ModifyTime = time.Now()
  1149. updateCol = append(updateCol, "Sort", "ModifyTime")
  1150. } else if prevClassify == nil && nextClassify == nil && prevEdbInfo == nil && nextEdbInfo == nil && parentClassifyId > 0 {
  1151. //处理只拖动到目录里,默认放到目录底部的情况
  1152. var maxSort int
  1153. maxSort, err = GetEdbClassifyMaxSort(parentClassifyId, classifyType)
  1154. if err != nil {
  1155. errMsg = "移动失败"
  1156. err = errors.New("查询组内排序信息失败,Err:" + err.Error())
  1157. return
  1158. }
  1159. edbInfo.Sort = maxSort + 1 //那就是排在组内最后一位
  1160. edbInfo.ModifyTime = time.Now()
  1161. updateCol = append(updateCol, "Sort", "ModifyTime")
  1162. } else {
  1163. // 拖动到父级分类的第一位
  1164. firstClassify, tmpErr := data_manage.GetFirstEdbClassifyByParentId(parentClassifyId)
  1165. if tmpErr != nil && tmpErr.Error() != utils.ErrNoRow() {
  1166. errMsg = "移动失败"
  1167. err = errors.New("获取获取当前父级分类下的排序第一条的分类信息失败,Err:" + tmpErr.Error())
  1168. return
  1169. }
  1170. //如果该分类下存在其他分类,且第一个其他分类的排序等于0,那么需要调整排序
  1171. if firstClassify != nil && firstClassify.Sort == 0 {
  1172. updateSortStr := ` sort + 1 `
  1173. _ = data_manage.UpdateEdbClassifySortByParentId(parentClassifyId, firstClassify.ClassifyId-1, 0, updateSortStr, classifyType)
  1174. //该分类下的所有指标也需要+1
  1175. _ = data_manage.UpdateEdbInfoSortByClassifyId(parentClassifyId, 0, 0, updateSortStr)
  1176. } else {
  1177. //如果该分类下存在指标,且第一个指标的排序等于0,那么需要调整排序
  1178. firstEdb, tErr := data_manage.GetFirstEdbInfoByClassifyId(parentClassifyId)
  1179. if tErr != nil && tErr.Error() != utils.ErrNoRow() {
  1180. errMsg = "移动失败"
  1181. err = errors.New("获取获取当前父级分类下的排序第一条的分类信息失败,Err:" + tErr.Error())
  1182. return
  1183. }
  1184. //如果该分类下存在其他分类,且第一个其他分类的排序等于0,那么需要调整排序
  1185. if firstEdb != nil && firstEdb.Sort == 0 {
  1186. updateSortStr := ` sort + 1 `
  1187. _ = data_manage.UpdateEdbInfoSortByClassifyId(parentClassifyId, 0, firstEdb.EdbInfoId-1, updateSortStr)
  1188. _ = data_manage.UpdateEdbClassifySortByParentId(parentClassifyId, 0, 0, updateSortStr, classifyType)
  1189. }
  1190. }
  1191. edbInfo.Sort = 0 //那就是排在第一位
  1192. edbInfo.ModifyTime = time.Now()
  1193. updateCol = append(updateCol, "Sort", "ModifyTime")
  1194. }
  1195. //更新
  1196. if len(updateCol) > 0 {
  1197. err = edbInfo.Update(updateCol)
  1198. if err != nil {
  1199. errMsg = "移动失败"
  1200. err = errors.New("修改失败,Err:" + err.Error())
  1201. return
  1202. }
  1203. }
  1204. }
  1205. return
  1206. }
  1207. // GetEdbOpButton 获取ETA指标的操作权限
  1208. func GetEdbOpButton(sysUser *system.Admin, belongUserId, edbType, edbInfoType int, haveOperaAuth bool) (button data_manage.EdbClassifyItemsButton) {
  1209. // 没有数据权限就直接返回
  1210. if !haveOperaAuth {
  1211. return
  1212. }
  1213. //2、用户对于自己添加的分类,有权限编辑、移动和删除该分类;
  1214. //3、ficc管理员和超管对所有分类有编辑、移动和删除权限;
  1215. if sysUser.RoleTypeCode == utils.ROLE_TYPE_CODE_ADMIN || sysUser.RoleTypeCode == utils.ROLE_TYPE_CODE_FICC_ADMIN || sysUser.AdminId == belongUserId || sysUser.EdbPermission == 1 {
  1216. button.AddButton = true
  1217. button.OpButton = true
  1218. button.DeleteButton = true
  1219. button.MoveButton = true
  1220. }
  1221. // ETA指标的基础指标才允许插入值
  1222. if edbInfoType == 0 && edbType == 1 {
  1223. button.InsertNewDataButton = true
  1224. }
  1225. button.ShowEdbRelation = true
  1226. button.ShowChartRelation = true
  1227. return
  1228. }
  1229. // GetEdbClassifyOpButton 获取ETA指标分类的操作权限
  1230. func GetEdbClassifyOpButton(sysUser *system.Admin, belongUserId int, haveOperaAuth bool) (button data_manage.EdbClassifyItemsButton) {
  1231. // 没有数据权限就直接返回
  1232. if !haveOperaAuth {
  1233. return
  1234. }
  1235. //ficc管理员和超管和ficc研究员有权限创建和管理分类,可以编辑分类名称(分类名称不允许重复),可以拖动分类,改变分类顺序,可以拖动分类下模型,改变顺序,可以删除分类,若分类下有预测指标,则不允许删除;
  1236. //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) {
  1237. button.AddButton = true
  1238. button.OpButton = true
  1239. button.DeleteButton = true
  1240. button.MoveButton = true
  1241. //}
  1242. return
  1243. }
  1244. // GetPredictEdbOpButton 获取ETA预测指标的操作权限
  1245. func GetPredictEdbOpButton(sysUser *system.Admin, belongUserId int, haveOperaAuth bool) (button data_manage.EdbClassifyItemsButton) {
  1246. // 没有数据权限就直接返回
  1247. if !haveOperaAuth {
  1248. return
  1249. }
  1250. //预测指标的添加人对该预测指标有全部操作权限,ficc管理员、超管对所有预测指标有全部操作权限;
  1251. if sysUser.RoleTypeCode == utils.ROLE_TYPE_CODE_ADMIN || sysUser.RoleTypeCode == utils.ROLE_TYPE_CODE_FICC_ADMIN || sysUser.AdminId == belongUserId || sysUser.PredictEdbPermission == 1 {
  1252. button.AddButton = true
  1253. button.OpButton = true
  1254. button.DeleteButton = true
  1255. button.MoveButton = true
  1256. }
  1257. button.ShowEdbRelation = true
  1258. button.ShowChartRelation = true
  1259. return
  1260. }
  1261. // GetPredictEdbClassifyOpButton 获取ETA预测指标分类的操作权限
  1262. func GetPredictEdbClassifyOpButton(sysUser *system.Admin, belongUserId int, haveOperaAuth bool) (button data_manage.EdbClassifyItemsButton) {
  1263. // 没有数据权限就直接返回
  1264. if !haveOperaAuth {
  1265. return
  1266. }
  1267. //ficc管理员和超管和ficc研究员有权限创建和管理分类,可以编辑分类名称(分类名称不允许重复),可以拖动分类,改变分类顺序,可以拖动分类下模型,改变顺序,可以删除分类,若分类下有预测指标,则不允许删除;
  1268. //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) {
  1269. button.AddButton = true
  1270. button.OpButton = true
  1271. button.DeleteButton = true
  1272. button.MoveButton = true
  1273. //}
  1274. return
  1275. }
  1276. func GetEdbClassifyMaxSort(parentId int, classifyType uint8) (maxSort int, err error) {
  1277. //获取该层级下最大的排序数
  1278. classifyMaxSort, err := data_manage.GetEdbClassifyMaxSort(parentId, classifyType)
  1279. if err != nil {
  1280. return
  1281. }
  1282. maxSort = classifyMaxSort
  1283. edbMaxSort, err := data_manage.GetEdbInfoMaxSortByClassifyId(parentId)
  1284. if err != nil {
  1285. return
  1286. }
  1287. if maxSort < edbMaxSort {
  1288. maxSort = edbMaxSort
  1289. }
  1290. return
  1291. }