fix.go 20 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712
  1. package controllers
  2. import (
  3. "eta_gn/eta_api/models/data_manage"
  4. "eta_gn/eta_api/services/data"
  5. "eta_gn/eta_api/utils"
  6. "fmt"
  7. "sort"
  8. "strconv"
  9. "strings"
  10. "time"
  11. )
  12. // init
  13. // @Description: 数据修复
  14. // @author: Roc
  15. // @datetime 2024-12-02 09:29:56
  16. //func init() {
  17. // // ### 1、先修复指标分类完整路径
  18. // initFixEdbClassifyPath()
  19. // fmt.Println("修复指标分类完整路径完成")
  20. //
  21. // // ### 2、再修复个人分类、删除不存在指标的分类
  22. // // ### 2.1 数据加工
  23. // initFixSelfEdbClassify(utils.EdbClassifyTypeCalculate)
  24. // fmt.Println("《数据加工》修复个人分类、删除不存在指标的分类完成")
  25. // //
  26. // //// ### 2.2 衍生指标
  27. // initFixSelfEdbClassify(utils.EdbClassifyTypePredict)
  28. // fmt.Println("《衍生指标》修复个人分类、删除不存在指标的分类完成")
  29. // //
  30. //
  31. // // 开始图表数据了
  32. //
  33. // // ### 3、修复图表分类完整路径
  34. // initFixChartClassifyPath()
  35. // fmt.Println("修复图表分类完整路径完成")
  36. //
  37. // // ### 4、修复图表分类完整路径
  38. // initFixSelfChartClassify()
  39. // fmt.Println("修复个人分类、删除不存在图表的分类完成")
  40. //
  41. // // ### 5、修复ES数据
  42. // initEs()
  43. //
  44. // fmt.Println("修复完成")
  45. //}
  46. // initFixEdbClassifyPath
  47. // @Description: 修复分类完整路径
  48. // @author: Roc
  49. // @datetime 2024-11-26 15:40:57
  50. func initFixEdbClassifyPath() {
  51. allList := make([]*data_manage.EdbClassifyItems, 0)
  52. allList, e := data_manage.GetEdbClassifyByClassifyTypes([]int{utils.EdbClassifyTypeBase, utils.EdbClassifyTypePredict, utils.EdbClassifyTypeCalculate}, 0)
  53. if e != nil {
  54. fmt.Println("获取分类失败", e)
  55. return
  56. }
  57. var sortList data_manage.EdbClassifyItemList
  58. allList = data.GetClassifyTreeRecursive(allList, 0)
  59. //根据sort值排序
  60. sortList = allList
  61. sort.Sort(sortList)
  62. for _, v := range sortList {
  63. updateEdbPath(v, "", "")
  64. }
  65. fmt.Println("修复分类路径完成")
  66. }
  67. func updateEdbPath(item *data_manage.EdbClassifyItems, classifyNamePath, classifyIdPath string) {
  68. if classifyNamePath == `` {
  69. classifyNamePath = item.ClassifyName
  70. classifyIdPath = fmt.Sprint(item.ClassifyId)
  71. } else {
  72. classifyNamePath = fmt.Sprint(classifyNamePath, "|", item.ClassifyName)
  73. classifyIdPath = fmt.Sprint(classifyIdPath, ",", item.ClassifyId)
  74. }
  75. err := data_manage.UpdateEdbClassify(item.ClassifyId, classifyNamePath, classifyIdPath)
  76. if err != nil {
  77. fmt.Println(item.ClassifyId, ";更新失败", err)
  78. }
  79. if item.Children != nil {
  80. for _, v := range item.Children {
  81. updateEdbPath(v, classifyNamePath, classifyIdPath)
  82. }
  83. }
  84. }
  85. // initFixSelfEdbClassify
  86. // @Description: initFixUserClassify
  87. // @author: Roc
  88. // @datetime 2024-11-29 16:57:54
  89. func initFixSelfEdbClassify(classifyType int) {
  90. // 指标数据明细
  91. condition := ` AND edb_info_type = 0 and edb_type = 2 `
  92. if classifyType == utils.EdbClassifyTypePredict {
  93. condition = ` AND edb_info_type = 1 `
  94. }
  95. pars := make([]interface{}, 0)
  96. edbList, err := data_manage.GetEdbInfoListByCond(condition, pars)
  97. if err != nil {
  98. fmt.Println("查找指标加工数据失败", err.Error())
  99. return
  100. }
  101. edbClassifyIdMap := make(map[int]bool)
  102. classifyIdList := make([]int, 0)
  103. edbUserMap := make(map[int]string)
  104. for _, v := range edbList {
  105. if _, ok := edbClassifyIdMap[v.ClassifyId]; !ok {
  106. classifyIdList = append(classifyIdList, v.ClassifyId)
  107. edbClassifyIdMap[v.ClassifyId] = true
  108. }
  109. edbUserMap[v.SysUserId] = v.SysUserRealName
  110. }
  111. // 获取所有数据加工的分类
  112. classifyList, err := data_manage.GetAllEdbClassifyByType(classifyType, 0)
  113. if err != nil {
  114. fmt.Println("获取数据加工分类失败", err.Error())
  115. return
  116. }
  117. hasClassifyIdStrList := make([]string, 0)
  118. for _, v := range classifyList {
  119. // 判断当前分类id是否加到指标中,如果加进去了,那么才处理
  120. _, ok := edbClassifyIdMap[v.ClassifyId]
  121. if !ok {
  122. continue
  123. }
  124. hasClassifyIdStrList = append(hasClassifyIdStrList, v.ClassifyIdPath)
  125. }
  126. classifyIdStrList := make([]string, 0)
  127. if len(hasClassifyIdStrList) > 0 {
  128. classifyIdStrList = strings.Split(strings.Join(hasClassifyIdStrList, ","), ",")
  129. }
  130. classifyIdMap := make(map[string]bool)
  131. for _, v := range classifyIdStrList {
  132. classifyIdMap[v] = true
  133. }
  134. newClassifyList := make([]*data_manage.EdbClassifyItems, 0)
  135. newClassifyMap := make(map[string]bool)
  136. for _, v := range classifyList {
  137. classifyIdStr := fmt.Sprint(v.ClassifyId)
  138. // 是否在关联指标的map里面,如果不在的话,那么就过滤
  139. _, ok := classifyIdMap[classifyIdStr]
  140. if !ok {
  141. continue
  142. }
  143. // 是否已经加到需要关联指标分类的map里面,如果已经在了,那么也过滤
  144. _, ok = newClassifyMap[classifyIdStr]
  145. if ok {
  146. continue
  147. }
  148. newClassifyMap[classifyIdStr] = true
  149. newClassifyList = append(newClassifyList, v)
  150. }
  151. rootList, err := data_manage.GetEdbClassifyByParentId(0, int8(classifyType), 0)
  152. nodeAll := make([]*data_manage.EdbClassifyItems, 0)
  153. for k := range rootList {
  154. rootNode := rootList[k]
  155. data.EdbClassifyItemsMakeTree(newClassifyList, rootNode)
  156. nodeAll = append(nodeAll, rootNode)
  157. }
  158. // 创建新的分类
  159. // [用户id][原分类id][新分类id]
  160. userClassifyIdMap := make(map[int]map[int]int)
  161. for userId, sysUserName := range edbUserMap {
  162. classifyRelationMap := make(map[int]int)
  163. for _, v := range nodeAll {
  164. createNewEdbClassify(userId, 0, 0, 0, sysUserName, ``, ``, v, classifyRelationMap)
  165. }
  166. userClassifyIdMap[userId] = classifyRelationMap
  167. }
  168. // ##### 第二步,指标数据迁移
  169. // 指标数据迁移
  170. for _, v := range edbList {
  171. userClassifyMap, ok := userClassifyIdMap[v.SysUserId]
  172. if !ok {
  173. fmt.Println("找不到该用户所属的分类,userId:", v.SysUserId)
  174. continue
  175. }
  176. newClassifyId, ok := userClassifyMap[v.ClassifyId]
  177. if !ok {
  178. fmt.Println("找不到该用户所属的分类,classifyId:", v.ClassifyId)
  179. continue
  180. }
  181. // 开始迁移指标数据
  182. err = data_manage.UpdateClassifyIdByEdbInfoId(v.EdbInfoId, newClassifyId)
  183. if err != nil {
  184. fmt.Println("指标数据迁移失败", err.Error())
  185. }
  186. }
  187. // ##### 3、开始删除没有指标的分类
  188. deleteNullEdbClassify(classifyType)
  189. return
  190. }
  191. func deleteNullEdbClassify(classifyType int) {
  192. // 指标列表数据
  193. condition := ` AND edb_info_type = 0 and edb_type = 2 `
  194. if classifyType == utils.EdbClassifyTypePredict {
  195. condition = ` AND edb_info_type = 1 `
  196. }
  197. pars := make([]interface{}, 0)
  198. edbList, err := data_manage.GetEdbInfoListByCond(condition, pars)
  199. if err != nil {
  200. fmt.Println("查找指标加工数据失败", err.Error())
  201. return
  202. }
  203. edbClassifyIdMap := make(map[int]bool)
  204. classifyIdList := make([]int, 0)
  205. for _, v := range edbList {
  206. if _, ok := edbClassifyIdMap[v.ClassifyId]; !ok {
  207. classifyIdList = append(classifyIdList, v.ClassifyId)
  208. edbClassifyIdMap[v.ClassifyId] = true
  209. }
  210. }
  211. // 获取所有数据加工的分类
  212. classifyList, err := data_manage.GetAllEdbClassifyByType(classifyType, 0)
  213. if err != nil {
  214. fmt.Println("获取数据加工分类失败", err.Error())
  215. return
  216. }
  217. // 存在指标的分类
  218. hasEdbClassifyList := make([]*data_manage.EdbClassifyItems, 0)
  219. for _, v := range classifyList {
  220. if _, ok := edbClassifyIdMap[v.ClassifyId]; ok {
  221. hasEdbClassifyList = append(hasEdbClassifyList, v)
  222. }
  223. }
  224. // 待删除的分类id集合
  225. deleteClassifyIdMap := make(map[int]int)
  226. // 全部的分类id集合
  227. for _, v := range classifyList {
  228. deleteClassifyIdMap[v.ClassifyId] = v.ClassifyId
  229. }
  230. // 存在指标的的分类id集合
  231. hasClassifyIdStrList := make([]string, 0)
  232. for _, v := range hasEdbClassifyList {
  233. hasClassifyIdStrList = append(hasClassifyIdStrList, v.ClassifyIdPath)
  234. }
  235. classifyIdStrList := make([]string, 0)
  236. if len(hasClassifyIdStrList) > 0 {
  237. classifyIdStrList = strings.Split(strings.Join(hasClassifyIdStrList, ","), ",")
  238. }
  239. for _, v := range classifyIdStrList {
  240. classifyId, err := strconv.Atoi(v)
  241. if err != nil {
  242. fmt.Println("分类字符串转int失败,", err.Error())
  243. return
  244. }
  245. delete(deleteClassifyIdMap, classifyId)
  246. }
  247. // 删除空分类
  248. if len(deleteClassifyIdMap) > 0 {
  249. deleteClassifyIdList := make([]int, 0)
  250. for _, v := range deleteClassifyIdMap {
  251. deleteClassifyIdList = append(deleteClassifyIdList, v)
  252. }
  253. err = data_manage.DelEdbClassifyByIdList(deleteClassifyIdList)
  254. if err != nil {
  255. fmt.Println("删除空分类失败", err.Error())
  256. return
  257. }
  258. }
  259. }
  260. // createNewEdbClassify
  261. // @Description: 创建分类
  262. // @author: Roc
  263. // @datetime 2024-11-29 17:57:47
  264. // @param userId int
  265. // @param parentId int
  266. // @param rootId int
  267. // @param level int
  268. // @param sysUserName string
  269. // @param parentClassifyNamePath string
  270. // @param parentClassifyIdPath string
  271. // @param node *data_manage.EdbClassifyItems
  272. // @param classifyRelationMap map[int]int
  273. func createNewEdbClassify(userId, parentId, rootId, level int, sysUserName, parentClassifyNamePath, parentClassifyIdPath string, node *data_manage.EdbClassifyItems, classifyRelationMap map[int]int) {
  274. var classifyNamePath, classifyIdPath string
  275. if parentId > 0 {
  276. classifyNamePath = fmt.Sprint(parentClassifyNamePath, "|", node.ClassifyName)
  277. classifyIdPath = fmt.Sprint(parentClassifyIdPath, ",")
  278. } else {
  279. classifyNamePath = node.ClassifyName
  280. }
  281. timestamp := strconv.FormatInt(time.Now().UnixNano(), 10)
  282. classifyInfo := &data_manage.EdbClassify{
  283. //ClassifyId: 0,
  284. ClassifyType: node.ClassifyType,
  285. ClassifyName: node.ClassifyName,
  286. ClassifyNameEn: node.ClassifyNameEn,
  287. ParentId: parentId,
  288. RootId: rootId,
  289. HasData: 1,
  290. CreateTime: time.Now(),
  291. ModifyTime: time.Now(),
  292. SysUserId: userId,
  293. SysUserRealName: sysUserName,
  294. Level: level + 1,
  295. UniqueCode: utils.MD5(utils.DATA_PREFIX + "_" + timestamp),
  296. Sort: node.Sort,
  297. ClassifyNamePath: classifyNamePath,
  298. }
  299. err := data_manage.AddEdbClassify(classifyInfo)
  300. if err != nil {
  301. fmt.Println("创建分类失败:", err.Error())
  302. return
  303. }
  304. updateCols := []string{"ClassifyIdPath"}
  305. // 更改分类id完整路径path
  306. classifyInfo.ClassifyIdPath = fmt.Sprint(classifyIdPath, classifyInfo.ClassifyId)
  307. if rootId <= 0 {
  308. rootId = classifyInfo.ClassifyId
  309. classifyInfo.RootId = classifyInfo.ClassifyId
  310. updateCols = append(updateCols, "RootId")
  311. }
  312. _ = classifyInfo.Update(updateCols)
  313. classifyRelationMap[node.ClassifyId] = classifyInfo.ClassifyId
  314. level = classifyInfo.Level
  315. if node.Children == nil {
  316. return
  317. }
  318. if len(node.Children) <= 0 {
  319. return
  320. }
  321. for _, child := range node.Children {
  322. createNewEdbClassify(userId, classifyInfo.ClassifyId, rootId, level, sysUserName, classifyNamePath, classifyInfo.ClassifyIdPath, child, classifyRelationMap)
  323. }
  324. return
  325. }
  326. // initFixChartClassifyPath
  327. // @Description: 修复图表分类完整路径
  328. // @author: Roc
  329. // @datetime 2024-11-26 15:40:57
  330. func initFixChartClassifyPath() {
  331. fmt.Println("开始修复图表分类路径,请稍等...")
  332. allList := make([]*data_manage.ChartClassifyItems, 0)
  333. allList, e := data_manage.GetAllChartClassify()
  334. if e != nil {
  335. fmt.Println("获取分类失败", e)
  336. return
  337. }
  338. allList = data.GetChartClassifyTreeRecursive(allList, 0)
  339. //根据sort值排序
  340. for _, v := range allList {
  341. updateChartPath(v, "", "")
  342. }
  343. fmt.Println("修复图表分类路径完成")
  344. }
  345. // initFixSelfChartClassify
  346. // @Description: 修复用户图表分类信息
  347. // @author: Roc
  348. // @datetime 2024-11-29 16:57:54
  349. func initFixSelfChartClassify() {
  350. fmt.Println("修复用户图表分类信息开始,请稍后...")
  351. // 图表数据明细
  352. condition := ``
  353. pars := make([]interface{}, 0)
  354. chartList, err := data_manage.GetChartListByCondition(condition, pars, 0, 1000000)
  355. if err != nil {
  356. fmt.Println("查找用户图表列表数据失败", err.Error())
  357. return
  358. }
  359. chartClassifyIdMap := make(map[int]bool)
  360. classifyIdList := make([]int, 0)
  361. edbUserMap := make(map[int]string)
  362. for _, v := range chartList {
  363. if _, ok := chartClassifyIdMap[v.ChartClassifyId]; !ok {
  364. classifyIdList = append(classifyIdList, v.ChartClassifyId)
  365. chartClassifyIdMap[v.ChartClassifyId] = true
  366. }
  367. edbUserMap[v.SysUserId] = v.SysUserRealName
  368. }
  369. // 获取所有图表的分类
  370. classifyList, err := data_manage.GetAllChartClassify()
  371. if err != nil {
  372. fmt.Println("获取图表分类失败", err.Error())
  373. return
  374. }
  375. hasClassifyIdStrList := make([]string, 0)
  376. for _, v := range classifyList {
  377. // 判断当前分类id是否加到指标中,如果加进去了,那么才处理
  378. _, ok := chartClassifyIdMap[v.ChartClassifyId]
  379. if !ok {
  380. continue
  381. }
  382. hasClassifyIdStrList = append(hasClassifyIdStrList, v.ChartClassifyIdPath)
  383. }
  384. classifyIdStrList := make([]string, 0)
  385. if len(hasClassifyIdStrList) > 0 {
  386. classifyIdStrList = strings.Split(strings.Join(hasClassifyIdStrList, ","), ",")
  387. }
  388. classifyIdMap := make(map[string]bool)
  389. for _, v := range classifyIdStrList {
  390. classifyIdMap[v] = true
  391. }
  392. newClassifyList := make([]*data_manage.ChartClassifyItems, 0)
  393. newClassifyMap := make(map[string]bool)
  394. for _, v := range classifyList {
  395. classifyIdStr := fmt.Sprint(v.ChartClassifyId)
  396. // 是否在关联指标的map里面,如果不在的话,那么就过滤
  397. _, ok := classifyIdMap[classifyIdStr]
  398. if !ok {
  399. continue
  400. }
  401. // 是否已经加到需要关联指标分类的map里面,如果已经在了,那么也过滤
  402. _, ok = newClassifyMap[classifyIdStr]
  403. if ok {
  404. continue
  405. }
  406. newClassifyMap[classifyIdStr] = true
  407. newClassifyList = append(newClassifyList, v)
  408. }
  409. rootList, err := data_manage.GetAllChartClassifyByParentId(0)
  410. nodeAll := make([]*data_manage.ChartClassifyItems, 0)
  411. for k := range rootList {
  412. rootNode := rootList[k]
  413. data.ChartClassifyItemsMakeTreeV2(newClassifyList, rootNode)
  414. nodeAll = append(nodeAll, rootNode)
  415. }
  416. // 创建新的分类
  417. // [用户id][原分类id][新分类id]
  418. userClassifyIdMap := make(map[int]map[int]int)
  419. for userId, sysUserName := range edbUserMap {
  420. classifyRelationMap := make(map[int]int)
  421. for _, v := range nodeAll {
  422. createNewChartClassify(userId, 0, 0, 0, sysUserName, ``, ``, v, classifyRelationMap)
  423. }
  424. userClassifyIdMap[userId] = classifyRelationMap
  425. }
  426. // ##### 第二步,图表数据迁移
  427. // 指标数据迁移
  428. for _, v := range chartList {
  429. userClassifyMap, ok := userClassifyIdMap[v.SysUserId]
  430. if !ok {
  431. fmt.Println("找不到该用户所属的分类,userId:", v.SysUserId)
  432. continue
  433. }
  434. newClassifyId, ok := userClassifyMap[v.ChartClassifyId]
  435. if !ok {
  436. fmt.Println("找不到该用户所属的图表分类,classifyId:", v.ChartClassifyId)
  437. continue
  438. }
  439. // 开始迁移指标数据
  440. err = data_manage.UpdateClassifyIdByChartInfoId(v.ChartInfoId, newClassifyId)
  441. if err != nil {
  442. fmt.Println("图表数据迁移失败", err.Error())
  443. }
  444. }
  445. // ##### 3、开始删除没有图表的分类
  446. deleteNullChartClassify()
  447. fmt.Println("修复用户图表分类信息完成")
  448. return
  449. }
  450. func deleteNullChartClassify() {
  451. // 图表数据明细
  452. condition := ``
  453. pars := make([]interface{}, 0)
  454. chartList, err := data_manage.GetChartListByCondition(condition, pars, 0, 1000000)
  455. if err != nil {
  456. fmt.Println("查找指标加工数据失败", err.Error())
  457. return
  458. }
  459. chartClassifyIdMap := make(map[int]bool)
  460. classifyIdList := make([]int, 0)
  461. for _, v := range chartList {
  462. if _, ok := chartClassifyIdMap[v.ChartClassifyId]; !ok {
  463. classifyIdList = append(classifyIdList, v.ChartClassifyId)
  464. chartClassifyIdMap[v.ChartClassifyId] = true
  465. }
  466. }
  467. // 获取所有数据加工的分类
  468. classifyList, err := data_manage.GetAllChartClassify()
  469. if err != nil {
  470. fmt.Println("获取数据加工分类失败", err.Error())
  471. return
  472. }
  473. // 存在图表的分类
  474. hasEdbClassifyList := make([]*data_manage.ChartClassifyItems, 0)
  475. for _, v := range classifyList {
  476. if _, ok := chartClassifyIdMap[v.ChartClassifyId]; ok {
  477. hasEdbClassifyList = append(hasEdbClassifyList, v)
  478. }
  479. }
  480. // 待删除的分类id集合
  481. deleteClassifyIdMap := make(map[int]int)
  482. // 全部的分类id集合
  483. for _, v := range classifyList {
  484. deleteClassifyIdMap[v.ChartClassifyId] = v.ChartClassifyId
  485. }
  486. // 存在指标的的分类id集合
  487. hasClassifyIdStrList := make([]string, 0)
  488. for _, v := range hasEdbClassifyList {
  489. hasClassifyIdStrList = append(hasClassifyIdStrList, v.ChartClassifyIdPath)
  490. }
  491. classifyIdStrList := make([]string, 0)
  492. if len(hasClassifyIdStrList) > 0 {
  493. classifyIdStrList = strings.Split(strings.Join(hasClassifyIdStrList, ","), ",")
  494. }
  495. for _, v := range classifyIdStrList {
  496. classifyId, err := strconv.Atoi(v)
  497. if err != nil {
  498. fmt.Println("分类字符串转int失败,", err.Error())
  499. return
  500. }
  501. delete(deleteClassifyIdMap, classifyId)
  502. }
  503. // 删除空分类
  504. if len(deleteClassifyIdMap) > 0 {
  505. deleteClassifyIdList := make([]int, 0)
  506. for _, v := range deleteClassifyIdMap {
  507. deleteClassifyIdList = append(deleteClassifyIdList, v)
  508. }
  509. err = data_manage.DelChartClassifyByIdList(deleteClassifyIdList)
  510. if err != nil {
  511. fmt.Println("删除空分类失败", err.Error())
  512. return
  513. }
  514. }
  515. }
  516. // createNewChartClassify
  517. // @Description: 创建图表分类
  518. // @author: Roc
  519. // @datetime 2024-11-29 17:57:47
  520. // @param userId int
  521. // @param parentId int
  522. // @param rootId int
  523. // @param level int
  524. // @param sysUserName string
  525. // @param parentClassifyNamePath string
  526. // @param parentClassifyIdPath string
  527. // @param node *data_manage.EdbClassifyItems
  528. // @param classifyRelationMap map[int]int
  529. func createNewChartClassify(userId, parentId, rootId, level int, sysUserName, parentClassifyNamePath, parentClassifyIdPath string, node *data_manage.ChartClassifyItems, classifyRelationMap map[int]int) {
  530. var classifyNamePath, classifyIdPath string
  531. if parentId > 0 {
  532. classifyNamePath = fmt.Sprint(parentClassifyNamePath, "|", node.ChartClassifyName)
  533. classifyIdPath = fmt.Sprint(parentClassifyIdPath, ",")
  534. } else {
  535. classifyNamePath = node.ChartClassifyName
  536. }
  537. timestamp := strconv.FormatInt(time.Now().UnixNano(), 10)
  538. classifyInfo := &data_manage.ChartClassify{
  539. ChartClassifyId: 0,
  540. ChartClassifyName: node.ChartClassifyName,
  541. ParentId: parentId,
  542. HasData: 1,
  543. CreateTime: time.Now(),
  544. ModifyTime: time.Now(),
  545. SysUserId: userId,
  546. SysUserRealName: sysUserName,
  547. Level: level + 1,
  548. UniqueCode: utils.MD5(utils.CHART_PREFIX + "_" + timestamp),
  549. Sort: node.Sort,
  550. Source: node.Source,
  551. //IsJoinPermission: 0,
  552. ChartClassifyNameEn: node.ChartClassifyNameEn,
  553. RootId: rootId,
  554. ChartClassifyNamePath: classifyNamePath,
  555. ChartClassifyIdPath: "",
  556. }
  557. err := data_manage.AddChartClassify(classifyInfo)
  558. if err != nil {
  559. fmt.Println("创建分类失败:", err.Error())
  560. return
  561. }
  562. updateCols := []string{"ChartClassifyIdPath"}
  563. // 更改分类id完整路径path
  564. classifyInfo.ChartClassifyIdPath = fmt.Sprint(classifyIdPath, classifyInfo.ChartClassifyId)
  565. if rootId <= 0 {
  566. rootId = classifyInfo.ChartClassifyId
  567. classifyInfo.RootId = classifyInfo.ChartClassifyId
  568. updateCols = append(updateCols, "RootId")
  569. }
  570. _ = classifyInfo.Update(updateCols)
  571. classifyRelationMap[node.ChartClassifyId] = classifyInfo.ChartClassifyId
  572. level = classifyInfo.Level
  573. if node.Children == nil {
  574. return
  575. }
  576. if len(node.Children) <= 0 {
  577. return
  578. }
  579. for _, child := range node.Children {
  580. createNewChartClassify(userId, classifyInfo.ChartClassifyId, rootId, level, sysUserName, classifyNamePath, classifyInfo.ChartClassifyIdPath, child, classifyRelationMap)
  581. }
  582. return
  583. }
  584. // updateChartPath
  585. // @Description: 更新图表分类的完整路径
  586. // @author: Roc
  587. // @datetime 2024-12-09 10:30:16
  588. // @param item *data_manage.ChartClassifyItems
  589. // @param classifyNamePath string
  590. // @param classifyIdPath string
  591. func updateChartPath(item *data_manage.ChartClassifyItems, classifyNamePath, classifyIdPath string) {
  592. if classifyNamePath == `` {
  593. classifyNamePath = item.ChartClassifyName
  594. classifyIdPath = fmt.Sprint(item.ChartClassifyId)
  595. } else {
  596. classifyNamePath = fmt.Sprint(classifyNamePath, "|", item.ChartClassifyName)
  597. classifyIdPath = fmt.Sprint(classifyIdPath, ",", item.ChartClassifyId)
  598. }
  599. err := data_manage.UpdateChartClassify(item.ChartClassifyId, classifyNamePath, classifyIdPath)
  600. if err != nil {
  601. fmt.Println(item.ChartClassifyId, ";更新失败", err)
  602. }
  603. if item.Children != nil {
  604. for _, v := range item.Children {
  605. updateChartPath(v, classifyNamePath, classifyIdPath)
  606. }
  607. }
  608. }
  609. // initEs
  610. // @Description: 修复ES中的指标和图表数据
  611. // @author: Roc
  612. // @datetime 2024-11-29 16:18:02
  613. func initEs() {
  614. // 更新ES中的指标数据
  615. data.AddOrEditAllEdbInfoToEs()
  616. fmt.Println("更新ES中的指标数据完成")
  617. // 更新es中的图表数据
  618. data.AddAllChartInfo()
  619. fmt.Println("更新ES中的图表数据完成")
  620. fmt.Println("全部es数据修复完成")
  621. }