edb_relation.go 39 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165
  1. package services
  2. import (
  3. "encoding/json"
  4. "eta/eta_task/models/data_manage"
  5. "eta/eta_task/models/data_manage/cross_variety"
  6. "eta/eta_task/models/data_manage/excel"
  7. "eta/eta_task/models/fe_calendar"
  8. "eta/eta_task/models/sandbox"
  9. "eta/eta_task/services/alarm_msg"
  10. "eta/eta_task/utils"
  11. "fmt"
  12. "time"
  13. )
  14. func InitChartEdbRelation() {
  15. fmt.Println("开始处理图表中的指标引用")
  16. var err error
  17. var addNum int
  18. defer func() {
  19. if err != nil {
  20. msg := fmt.Sprintf("初始化指标在图表中的引用失败 InitChartEdbRelation err: %v", err)
  21. utils.FileLog.Info(msg)
  22. fmt.Println(msg)
  23. go alarm_msg.SendAlarmMsg(msg, 3)
  24. }
  25. }()
  26. //查询chart_edb_mapping 表
  27. total, err := data_manage.GetChartEdbMappingTotal()
  28. if err != nil {
  29. err = fmt.Errorf("查询图表关联指标失败 err: %v", err)
  30. return
  31. }
  32. if total == 0 {
  33. return
  34. }
  35. //分页查询,每次处理500条记录
  36. pageSize := 500
  37. totalPage := (total + pageSize - 1) / pageSize // 使用整数除法,并添加一页以防有余数
  38. addList := make([]*data_manage.EdbInfoRelation, 0)
  39. //查询图表列表
  40. for i := 0; i < totalPage; i += 1 {
  41. startSize := i * pageSize
  42. list, e := data_manage.GetChartEdbMappingList(startSize, pageSize)
  43. if e != nil {
  44. err = fmt.Errorf("查询图表关联指标列表失败 Err:%s", e)
  45. return
  46. }
  47. if len(list) == 0 {
  48. break
  49. }
  50. edbInfoIds := make([]int, 0)
  51. for _, v := range list {
  52. edbInfoIds = append(edbInfoIds, v.EdbInfoId)
  53. }
  54. // 查询指标信息表
  55. edbInfoList, e := data_manage.GetEdbInfoByIdList(edbInfoIds)
  56. if e != nil {
  57. err = fmt.Errorf("查询指标信息列表失败 Err:%s", e)
  58. return
  59. }
  60. if len(edbInfoList) == 0 {
  61. continue
  62. }
  63. // 查询计算指标信息,并且建立关联关系
  64. // 查询间接引用的指标信息
  65. calculateEdbMappingListMap, calculateEdbMappingIdsMap, e := GetEdbListByEdbInfoId(edbInfoList)
  66. if e != nil {
  67. err = fmt.Errorf("查询计算指标信息失败,%s", e.Error())
  68. return
  69. }
  70. // 查询指标间接引用
  71. edbInfoMap := make(map[int]*data_manage.EdbInfo)
  72. for _, v := range edbInfoList {
  73. edbInfoMap[v.EdbInfoId] = v
  74. }
  75. // 筛选有用的图表
  76. finalList := make([]*data_manage.ChartEdbMapping, 0)
  77. chartIds := make([]int, 0)
  78. for _, v := range list {
  79. if _, ok2 := edbInfoMap[v.EdbInfoId]; !ok2 {
  80. continue
  81. }
  82. finalList = append(finalList, v)
  83. chartIds = append(chartIds, v.ChartInfoId)
  84. }
  85. if len(chartIds) == 0 {
  86. continue
  87. }
  88. // 查询图表信息
  89. chartInfoList, e := data_manage.GetChartInfoByChartInfoIds(chartIds)
  90. if e != nil {
  91. err = fmt.Errorf("查询图表信息列表失败 Err:%s", e)
  92. return
  93. }
  94. chartInfoMap := make(map[int]*data_manage.ChartInfo)
  95. for _, v := range chartInfoList {
  96. chartInfoMap[v.ChartInfoId] = v
  97. }
  98. //查询引用关系列表,
  99. chartEdbRelationList, e := data_manage.GetEdbInfoRelationByReferObjectIds(chartIds, utils.EDB_RELATION_CHART)
  100. if e != nil {
  101. err = fmt.Errorf("查询图表引用关系列表失败 Err:%s", e)
  102. return
  103. }
  104. existRelationMap := make(map[string]struct{})
  105. for _, v := range chartEdbRelationList {
  106. name := fmt.Sprintf("%d-%d", v.ReferObjectId, v.EdbInfoId)
  107. existRelationMap[name] = struct{}{}
  108. }
  109. for _, v := range finalList {
  110. nowTime := time.Now()
  111. name := fmt.Sprintf("%d-%d", v.ChartInfoId, v.EdbInfoId)
  112. if _, ok := existRelationMap[name]; !ok {
  113. //查询图表信息
  114. chartInfo, ok1 := chartInfoMap[v.ChartInfoId]
  115. if !ok1 {
  116. continue
  117. }
  118. edbInfo, ok2 := edbInfoMap[v.EdbInfoId]
  119. if !ok2 {
  120. continue
  121. }
  122. //todo 引用时间
  123. tmp := &data_manage.EdbInfoRelation{
  124. ReferObjectId: v.ChartInfoId,
  125. ReferObjectType: utils.EDB_RELATION_CHART,
  126. ReferObjectSubType: chartInfo.Source,
  127. EdbInfoId: v.EdbInfoId,
  128. EdbName: edbInfo.EdbName,
  129. Source: edbInfo.Source,
  130. EdbCode: edbInfo.EdbCode,
  131. CreateTime: nowTime,
  132. ModifyTime: nowTime,
  133. RelationTime: v.CreateTime,
  134. }
  135. tmp.RelationCode = fmt.Sprintf("%d_%d_%d_%d", tmp.EdbInfoId, tmp.ReferObjectId, tmp.ReferObjectType, tmp.ReferObjectSubType)
  136. addList = append(addList, tmp)
  137. existRelationMap[name] = struct{}{}
  138. // 添加间接引用记录
  139. if edbInfo.EdbType == 2 && edbInfo.EdbInfoType == 0 {
  140. childEdbMappingIds, ok1 := calculateEdbMappingIdsMap[edbInfo.EdbInfoId]
  141. if !ok1 {
  142. continue
  143. }
  144. for _, childEdbMappingId := range childEdbMappingIds {
  145. childEdbMapping, ok2 := calculateEdbMappingListMap[childEdbMappingId]
  146. if !ok2 {
  147. continue
  148. }
  149. name1 := fmt.Sprintf("%d-%d", v.ChartInfoId, childEdbMapping.FromEdbInfoId)
  150. if _, ok2 := existRelationMap[name1]; !ok2 { //如果已经被直接引用了,则无需添加到间接引用记录中
  151. continue
  152. }
  153. tmp1 := &data_manage.EdbInfoRelation{
  154. ReferObjectId: v.ChartInfoId,
  155. ReferObjectType: utils.EDB_RELATION_CHART,
  156. ReferObjectSubType: chartInfo.Source,
  157. EdbInfoId: childEdbMapping.FromEdbInfoId,
  158. EdbName: childEdbMapping.FromEdbName,
  159. Source: childEdbMapping.FromSource,
  160. EdbCode: childEdbMapping.FromEdbCode,
  161. CreateTime: nowTime,
  162. ModifyTime: nowTime,
  163. RelationTime: v.CreateTime,
  164. RelationType: 1,
  165. RootEdbInfoId: edbInfo.EdbInfoId,
  166. ChildEdbInfoId: childEdbMapping.EdbInfoId,
  167. RelationCode: tmp.RelationCode,
  168. }
  169. addList = append(addList, tmp1)
  170. // todo 防止重复
  171. }
  172. }
  173. if len(addList) > pageSize {
  174. err = data_manage.AddEdbInfoRelationMulti(addList)
  175. if err != nil {
  176. err = fmt.Errorf("新增引用记录失败 Err:%s", err)
  177. return
  178. }
  179. addNum += len(addList)
  180. addList = make([]*data_manage.EdbInfoRelation, 0)
  181. }
  182. }
  183. }
  184. }
  185. //拿到500个数据ID,判断相关的引用记录,如果已存在则直接过滤,不存在则新增
  186. if len(addList) > 0 {
  187. err = data_manage.AddEdbInfoRelationMulti(addList)
  188. if err != nil {
  189. err = fmt.Errorf("新增引用记录失败 Err:%s", err)
  190. return
  191. }
  192. addNum += len(addList)
  193. }
  194. fmt.Printf("图表指标引用记录处理完成, 新增%d条记录\n", addNum)
  195. return
  196. }
  197. // InitChartCrossVariety 处理特殊图表,跨品种分析图表
  198. func InitChartCrossVariety() {
  199. fmt.Println("开始跨品种分析图表中的指标引用")
  200. var addNum int
  201. var err error
  202. defer func() {
  203. if err != nil {
  204. msg := fmt.Sprintf("初始化指标在跨品种分析图表中的引用失败 InitChartCrossVariety err: %v", err)
  205. utils.FileLog.Info(msg)
  206. fmt.Println(msg)
  207. go alarm_msg.SendAlarmMsg(msg, 3)
  208. }
  209. }()
  210. total, err := cross_variety.GetChartInfoCrossVarietyTotal()
  211. if err != nil {
  212. err = fmt.Errorf("查询图表关联指标失败 err: %v", err)
  213. return
  214. }
  215. if total == 0 {
  216. return
  217. }
  218. //分页查询,每次处理500条记录
  219. pageSize := 500
  220. totalPage := (total + pageSize - 1) / pageSize // 使用整数除法,并添加一页以防有余数
  221. addList := make([]*data_manage.EdbInfoRelation, 0)
  222. //查询图表列表
  223. for i := 0; i < totalPage; i += 1 {
  224. startSize := i * pageSize
  225. list, e := cross_variety.GetChartInfoCrossVarietyList(startSize, pageSize)
  226. if e != nil {
  227. err = fmt.Errorf("查询图表关联指标列表失败 Err:%s", e)
  228. return
  229. }
  230. if len(list) == 0 {
  231. break
  232. }
  233. chartIds := make([]int, 0)
  234. tagIds := make([]int, 0)
  235. tagIdsMap := make(map[int]struct{})
  236. tagChartMap := make(map[int][]*cross_variety.ChartInfoCrossVariety)
  237. for _, v := range list {
  238. if _, ok := tagIdsMap[v.ChartXTagId]; !ok {
  239. tagIds = append(tagIds, v.ChartXTagId)
  240. tagIdsMap[v.ChartXTagId] = struct{}{}
  241. }
  242. if _, ok := tagIdsMap[v.ChartYTagId]; !ok {
  243. tagIds = append(tagIds, v.ChartYTagId)
  244. tagIdsMap[v.ChartYTagId] = struct{}{}
  245. }
  246. if chartCross, ok := tagChartMap[v.ChartXTagId]; ok {
  247. chartCross = append(chartCross, v)
  248. tagChartMap[v.ChartXTagId] = chartCross
  249. } else {
  250. chartCross = make([]*cross_variety.ChartInfoCrossVariety, 0)
  251. chartCross = append(chartCross, v)
  252. tagChartMap[v.ChartXTagId] = chartCross
  253. }
  254. if chartCross, ok := tagChartMap[v.ChartYTagId]; ok {
  255. chartCross = append(chartCross, v)
  256. tagChartMap[v.ChartYTagId] = chartCross
  257. } else {
  258. chartCross = make([]*cross_variety.ChartInfoCrossVariety, 0)
  259. chartCross = append(chartCross, v)
  260. tagChartMap[v.ChartYTagId] = chartCross
  261. }
  262. }
  263. chartTagVarietyList, e := cross_variety.GetChartTagVarietyEdbInfoIdsByTagIds(tagIds)
  264. if e != nil {
  265. err = fmt.Errorf("查询指标信息列表失败 Err:%s", e)
  266. return
  267. }
  268. edbInfoIds := make([]int, 0)
  269. chartTagVarietyMap := make(map[int][]*cross_variety.ChartTagVariety)
  270. for _, v := range chartTagVarietyList {
  271. if tagList, ok := chartTagVarietyMap[v.EdbInfoId]; ok {
  272. tagList = append(tagList, v)
  273. chartTagVarietyMap[v.EdbInfoId] = tagList
  274. } else {
  275. tagList = make([]*cross_variety.ChartTagVariety, 0)
  276. tagList = append(tagList, v)
  277. chartTagVarietyMap[v.EdbInfoId] = tagList
  278. }
  279. edbInfoIds = append(edbInfoIds, v.EdbInfoId)
  280. }
  281. // 查询指标信息表
  282. edbInfoList, e := data_manage.GetEdbInfoByIdList(edbInfoIds)
  283. if e != nil {
  284. err = fmt.Errorf("查询指标信息列表失败 Err:%s", e)
  285. return
  286. }
  287. if len(edbInfoList) == 0 {
  288. continue
  289. }
  290. // 查询计算指标信息,并且建立关联关系
  291. // 查询间接引用的指标信息
  292. calculateEdbMappingListMap, calculateEdbMappingIdsMap, e := GetEdbListByEdbInfoId(edbInfoList)
  293. if e != nil {
  294. err = fmt.Errorf("查询计算指标信息失败,%s", e.Error())
  295. return
  296. }
  297. edbInfoMap := make(map[int]*data_manage.EdbInfo)
  298. chartInfoCrossMap := make(map[int]struct{})
  299. chartInfoCrossList := make([]*cross_variety.ChartInfoCrossVariety, 0)
  300. edbCrossMap := make(map[int][]*cross_variety.ChartInfoCrossVariety)
  301. for _, v := range edbInfoList {
  302. edbInfoMap[v.EdbInfoId] = v
  303. if tagList, ok := chartTagVarietyMap[v.EdbInfoId]; ok {
  304. for _, tag := range tagList {
  305. if chartCross, ok2 := tagChartMap[tag.ChartTagId]; ok2 {
  306. for _, crossItem := range chartCross {
  307. if _, ok3 := chartInfoCrossMap[crossItem.ChartInfoId]; !ok3 {
  308. chartInfoCrossMap[crossItem.ChartInfoId] = struct{}{}
  309. chartInfoCrossList = append(chartInfoCrossList, crossItem)
  310. chartIds = append(chartIds, crossItem.ChartInfoId)
  311. }
  312. }
  313. }
  314. }
  315. }
  316. edbCrossMap[v.EdbInfoId] = chartInfoCrossList
  317. chartInfoCrossMap = make(map[int]struct{})
  318. chartInfoCrossList = make([]*cross_variety.ChartInfoCrossVariety, 0)
  319. }
  320. /*// 查询图表信息
  321. chartInfoList, e := data_manage.GetChartInfoByChartInfoIds(chartIds)
  322. if e != nil {
  323. err = fmt.Errorf("查询图表信息列表失败 Err:%s", e)
  324. return
  325. }
  326. chartInfoMap := make(map[int]*data_manage.ChartInfo)
  327. for _, v := range chartInfoList {
  328. chartInfoMap[v.ChartInfoId] = v
  329. }
  330. */
  331. //查询引用关系列表,
  332. chartEdbRelationList, e := data_manage.GetEdbInfoRelationByReferObjectIds(chartIds, utils.EDB_RELATION_CHART)
  333. if e != nil {
  334. err = fmt.Errorf("查询图表引用关系列表失败 Err:%s", e)
  335. return
  336. }
  337. existRelationMap := make(map[string]struct{})
  338. for _, v := range chartEdbRelationList {
  339. name := fmt.Sprintf("%d-%d", v.ReferObjectId, v.EdbInfoId)
  340. existRelationMap[name] = struct{}{}
  341. }
  342. for edbInfoId, chartCrossList := range edbCrossMap {
  343. nowTime := time.Now()
  344. for _, item := range chartCrossList {
  345. name := fmt.Sprintf("%d-%d", item.ChartInfoId, edbInfoId)
  346. if _, ok1 := existRelationMap[name]; !ok1 {
  347. edbInfo, ok2 := edbInfoMap[edbInfoId]
  348. if !ok2 {
  349. continue
  350. }
  351. //todo 引用时间
  352. tmp := &data_manage.EdbInfoRelation{
  353. ReferObjectId: item.ChartInfoId,
  354. ReferObjectType: utils.EDB_RELATION_CHART,
  355. ReferObjectSubType: utils.CHART_SOURCE_CROSS_HEDGING,
  356. EdbInfoId: edbInfoId,
  357. EdbName: edbInfo.EdbName,
  358. Source: edbInfo.Source,
  359. EdbCode: edbInfo.EdbCode,
  360. CreateTime: nowTime,
  361. ModifyTime: nowTime,
  362. RelationTime: item.CreateTime,
  363. }
  364. tmp.RelationCode = fmt.Sprintf("%d_%d_%d_%d", tmp.EdbInfoId, tmp.ReferObjectId, tmp.ReferObjectType, tmp.ReferObjectSubType)
  365. addList = append(addList, tmp)
  366. existRelationMap[name] = struct{}{}
  367. // 添加间接引用记录
  368. if edbInfo.EdbType == 2 && edbInfo.EdbInfoType == 0 {
  369. childEdbMappingIds, ok1 := calculateEdbMappingIdsMap[edbInfo.EdbInfoId]
  370. if !ok1 {
  371. continue
  372. }
  373. for _, childEdbMappingId := range childEdbMappingIds {
  374. childEdbMapping, ok2 := calculateEdbMappingListMap[childEdbMappingId]
  375. if !ok2 {
  376. continue
  377. }
  378. name1 := fmt.Sprintf("%d-%d", item.ChartInfoId, childEdbMapping.FromEdbInfoId)
  379. if _, ok2 := existRelationMap[name1]; !ok2 { //如果已经被直接引用了,则无需添加到间接引用记录中
  380. continue
  381. }
  382. tmp1 := &data_manage.EdbInfoRelation{
  383. ReferObjectId: item.ChartInfoId,
  384. ReferObjectType: utils.EDB_RELATION_CHART,
  385. ReferObjectSubType: utils.CHART_SOURCE_CROSS_HEDGING,
  386. EdbInfoId: childEdbMapping.FromEdbInfoId,
  387. EdbName: childEdbMapping.FromEdbName,
  388. Source: childEdbMapping.FromSource,
  389. EdbCode: childEdbMapping.FromEdbCode,
  390. CreateTime: nowTime,
  391. ModifyTime: nowTime,
  392. RelationTime: item.CreateTime,
  393. RelationType: 1,
  394. RootEdbInfoId: edbInfo.EdbInfoId,
  395. ChildEdbInfoId: childEdbMapping.EdbInfoId,
  396. RelationCode: tmp.RelationCode,
  397. }
  398. addList = append(addList, tmp1)
  399. // todo 防止重复
  400. }
  401. }
  402. if len(addList) > pageSize {
  403. err = data_manage.AddEdbInfoRelationMulti(addList)
  404. if err != nil {
  405. err = fmt.Errorf("新增引用记录失败 Err:%s", err)
  406. return
  407. }
  408. addNum += len(addList)
  409. addList = make([]*data_manage.EdbInfoRelation, 0)
  410. }
  411. }
  412. }
  413. }
  414. }
  415. //拿到500个数据ID,判断相关的引用记录,如果已存在则直接过滤,不存在则新增
  416. if len(addList) > 0 {
  417. err = data_manage.AddEdbInfoRelationMulti(addList)
  418. if err != nil {
  419. err = fmt.Errorf("新增引用记录失败 Err:%s", err)
  420. return
  421. }
  422. addNum += len(addList)
  423. }
  424. fmt.Printf("跨品种分析图表指标引用记录处理完成, 新增%d条记录\n", addNum)
  425. return
  426. }
  427. // 初始化事件日历中的指标引用
  428. func InitCalendarIndicatorRelation() {
  429. fmt.Println("开始处理事件日历中的指标引用")
  430. var addNum int
  431. var err error
  432. defer func() {
  433. if err != nil {
  434. msg := fmt.Sprintf("初始化指标在事件日历中的引用失败 initCalendarIndicatorRelation err: %v", err)
  435. utils.FileLog.Info(msg)
  436. fmt.Println(msg)
  437. go alarm_msg.SendAlarmMsg(msg, 3)
  438. }
  439. }()
  440. //查询chart_edb_mapping 表
  441. obj := new(fe_calendar.FeCalendarMatter)
  442. condition := " AND edb_info_id > 0"
  443. total, err := obj.GetCountByCondition(condition, []interface{}{})
  444. if err != nil {
  445. err = fmt.Errorf("查询事件日历关联指标失败 err: %v", err)
  446. return
  447. }
  448. if total == 0 {
  449. return
  450. }
  451. //分页查询,每次处理500条记录
  452. pageSize := 500
  453. totalPage := (total + pageSize - 1) / pageSize // 使用整数除法,并添加一页以防有余数
  454. addList := make([]*data_manage.EdbInfoRelation, 0)
  455. //查询图表列表
  456. for i := 0; i < totalPage; i += 1 {
  457. startSize := i * pageSize
  458. list, e := obj.GetPageItemsByCondition(condition, []interface{}{}, []string{}, "", startSize, pageSize)
  459. if e != nil {
  460. err = fmt.Errorf("查询事件日历关联指标列表失败 Err:%s", e)
  461. return
  462. }
  463. if len(list) == 0 {
  464. break
  465. }
  466. edbInfoIds := make([]int, 0)
  467. edbInfoMatterMap := make(map[int][]*fe_calendar.FeCalendarMatter)
  468. for _, v := range list {
  469. edbInfoIds = append(edbInfoIds, v.EdbInfoId)
  470. items, ok := edbInfoMatterMap[v.EdbInfoId]
  471. if ok {
  472. items = append(items, v)
  473. edbInfoMatterMap[v.EdbInfoId] = items
  474. } else {
  475. items = make([]*fe_calendar.FeCalendarMatter, 0)
  476. items = append(items, v)
  477. edbInfoMatterMap[v.EdbInfoId] = items
  478. }
  479. }
  480. // 查询指标信息表
  481. edbInfoList, e := data_manage.GetEdbInfoByIdList(edbInfoIds)
  482. if e != nil {
  483. err = fmt.Errorf("查询指标信息列表失败 Err:%s", e)
  484. return
  485. }
  486. if len(edbInfoList) == 0 {
  487. continue
  488. }
  489. // 查询计算指标信息,并且建立关联关系
  490. // 查询间接引用的指标信息
  491. calculateEdbMappingListMap, calculateEdbMappingIdsMap, e := GetEdbListByEdbInfoId(edbInfoList)
  492. if e != nil {
  493. err = fmt.Errorf("查询计算指标信息失败,%s", e.Error())
  494. return
  495. }
  496. edbInfoMap := make(map[int]*data_manage.EdbInfo)
  497. matterIds := make([]int, 0)
  498. for _, v := range edbInfoList {
  499. edbInfoMap[v.EdbInfoId] = v
  500. items, ok := edbInfoMatterMap[v.EdbInfoId]
  501. if ok {
  502. for _, item := range items {
  503. matterIds = append(matterIds, item.FeCalendarMatterId)
  504. }
  505. }
  506. }
  507. //查询引用关系列表,
  508. chartEdbRelationList, e := data_manage.GetEdbInfoRelationByReferObjectIds(matterIds, utils.EDB_RELATION_CALENDAR)
  509. if e != nil {
  510. err = fmt.Errorf("查询图表引用关系列表失败 Err:%s", e)
  511. return
  512. }
  513. existRelationMap := make(map[string]struct{})
  514. for _, v := range chartEdbRelationList {
  515. name := fmt.Sprintf("%d-%d", v.ReferObjectId, v.EdbInfoId)
  516. existRelationMap[name] = struct{}{}
  517. }
  518. for edbInfoId, edbInfo := range edbInfoMap {
  519. nowTime := time.Now()
  520. items, ok := edbInfoMatterMap[edbInfoId]
  521. if ok {
  522. for _, v := range items {
  523. name := fmt.Sprintf("%d-%d", v.FeCalendarMatterId, v.EdbInfoId)
  524. if _, ok1 := existRelationMap[name]; !ok1 {
  525. //todo 引用时间
  526. tmp := &data_manage.EdbInfoRelation{
  527. ReferObjectId: v.FeCalendarMatterId,
  528. ReferObjectType: utils.EDB_RELATION_CALENDAR,
  529. EdbInfoId: v.EdbInfoId,
  530. EdbName: edbInfo.EdbName,
  531. Source: edbInfo.Source,
  532. EdbCode: edbInfo.EdbCode,
  533. CreateTime: nowTime,
  534. ModifyTime: nowTime,
  535. RelationTime: v.CreateTime,
  536. }
  537. tmp.RelationCode = fmt.Sprintf("%d_%d_%d_%d", tmp.EdbInfoId, tmp.ReferObjectId, tmp.ReferObjectType, tmp.ReferObjectSubType)
  538. addList = append(addList, tmp)
  539. existRelationMap[name] = struct{}{}
  540. // 添加间接引用记录
  541. if edbInfo.EdbType == 2 && edbInfo.EdbInfoType == 0 {
  542. childEdbMappingIds, ok1 := calculateEdbMappingIdsMap[edbInfo.EdbInfoId]
  543. if !ok1 {
  544. continue
  545. }
  546. for _, childEdbMappingId := range childEdbMappingIds {
  547. childEdbMapping, ok2 := calculateEdbMappingListMap[childEdbMappingId]
  548. if !ok2 {
  549. continue
  550. }
  551. name1 := fmt.Sprintf("%d-%d", v.FeCalendarMatterId, childEdbMapping.FromEdbInfoId)
  552. if _, ok2 := existRelationMap[name1]; !ok2 { //如果已经被直接引用了,则无需添加到间接引用记录中
  553. continue
  554. }
  555. tmp1 := &data_manage.EdbInfoRelation{
  556. ReferObjectId: v.FeCalendarMatterId,
  557. ReferObjectType: utils.EDB_RELATION_CALENDAR,
  558. EdbInfoId: childEdbMapping.FromEdbInfoId,
  559. EdbName: childEdbMapping.FromEdbName,
  560. Source: childEdbMapping.FromSource,
  561. EdbCode: childEdbMapping.FromEdbCode,
  562. CreateTime: nowTime,
  563. ModifyTime: nowTime,
  564. RelationTime: v.CreateTime,
  565. RelationType: 1,
  566. RootEdbInfoId: edbInfo.EdbInfoId,
  567. ChildEdbInfoId: childEdbMapping.EdbInfoId,
  568. RelationCode: tmp.RelationCode,
  569. }
  570. addList = append(addList, tmp1)
  571. // todo 防止重复
  572. }
  573. }
  574. if len(addList) > pageSize {
  575. err = data_manage.AddEdbInfoRelationMulti(addList)
  576. if err != nil {
  577. err = fmt.Errorf("新增引用记录失败 Err:%s", err)
  578. return
  579. }
  580. addNum += len(addList)
  581. addList = make([]*data_manage.EdbInfoRelation, 0)
  582. }
  583. }
  584. }
  585. }
  586. }
  587. }
  588. //拿到500个数据ID,判断相关的引用记录,如果已存在则直接过滤,不存在则新增
  589. if len(addList) > 0 {
  590. err = data_manage.AddEdbInfoRelationMulti(addList)
  591. if err != nil {
  592. err = fmt.Errorf("新增引用记录失败 Err:%s", err)
  593. return
  594. }
  595. addNum += len(addList)
  596. }
  597. fmt.Printf("事件日历指标引用记录处理完成, 新增%d条记录\n", addNum)
  598. return
  599. }
  600. // 初始化表格中的指标引用
  601. func InitExcelEdbRelation() {
  602. fmt.Println("开始处理表格中的指标引用")
  603. var err error
  604. var addNum int
  605. defer func() {
  606. if err != nil {
  607. msg := fmt.Sprintf("初始化指标在表格中的引用失败 InitChartEdbRelation err: %v", err)
  608. utils.FileLog.Info(msg)
  609. fmt.Println(msg)
  610. go alarm_msg.SendAlarmMsg(msg, 3)
  611. }
  612. }()
  613. //查询表格指标绑定表
  614. sources := []int{utils.TIME_TABLE, utils.MIXED_TABLE, utils.BALANCE_TABLE}
  615. total, err := excel.GetExcelEdbMappingTotalBySource(sources)
  616. if err != nil {
  617. err = fmt.Errorf("查询表格关联指标失败 err: %v", err)
  618. return
  619. }
  620. if total == 0 {
  621. return
  622. }
  623. //分页查询,每次处理100条记录
  624. pageSize := 100
  625. totalPage := (total + pageSize - 1) / pageSize // 使用整数除法,并添加一页以防有余数
  626. addList := make([]*data_manage.EdbInfoRelation, 0)
  627. //查询表格列表
  628. for i := 0; i < totalPage; i += 1 {
  629. startSize := i * pageSize
  630. list, e := excel.GetExcelEdbMappingListBySource(sources, startSize, pageSize)
  631. if e != nil {
  632. err = fmt.Errorf("查询表格关联指标列表失败 Err:%s", e)
  633. return
  634. }
  635. if len(list) == 0 {
  636. break
  637. }
  638. edbInfoIds := make([]int, 0)
  639. for _, v := range list {
  640. edbInfoIds = append(edbInfoIds, v.EdbInfoId)
  641. }
  642. // 查询指标信息表
  643. edbInfoList, e := data_manage.GetEdbInfoByIdList(edbInfoIds)
  644. if e != nil {
  645. err = fmt.Errorf("查询指标信息列表失败 Err:%s", e)
  646. return
  647. }
  648. if len(edbInfoList) == 0 {
  649. continue
  650. }
  651. // 查询计算指标信息,并且建立关联关系
  652. // 查询间接引用的指标信息
  653. calculateEdbMappingListMap, calculateEdbMappingIdsMap, e := GetEdbListByEdbInfoId(edbInfoList)
  654. if e != nil {
  655. err = fmt.Errorf("查询计算指标信息失败,%s", e.Error())
  656. return
  657. }
  658. // 查询指标间接引用
  659. edbInfoMap := make(map[int]*data_manage.EdbInfo)
  660. for _, v := range edbInfoList {
  661. edbInfoMap[v.EdbInfoId] = v
  662. }
  663. // 筛选有用的表格
  664. excelIds := make([]int, 0)
  665. for _, v := range list {
  666. excelIds = append(excelIds, v.ExcelInfoId)
  667. }
  668. //查询引用关系列表,
  669. chartEdbRelationList, e := data_manage.GetEdbInfoRelationByReferObjectIds(excelIds, utils.EDB_RELATION_TABLE)
  670. if e != nil {
  671. err = fmt.Errorf("查询表格引用关系列表失败 Err:%s", e)
  672. return
  673. }
  674. existRelationMap := make(map[string]struct{})
  675. for _, v := range chartEdbRelationList {
  676. name := fmt.Sprintf("%d-%d", v.ReferObjectId, v.EdbInfoId)
  677. existRelationMap[name] = struct{}{}
  678. }
  679. for _, v := range list {
  680. nowTime := time.Now()
  681. name := fmt.Sprintf("%d-%d", v.ExcelInfoId, v.EdbInfoId)
  682. if _, ok := existRelationMap[name]; !ok {
  683. edbInfo, ok2 := edbInfoMap[v.EdbInfoId]
  684. if !ok2 {
  685. continue
  686. }
  687. //todo 引用时间
  688. tmp := &data_manage.EdbInfoRelation{
  689. ReferObjectId: v.ExcelInfoId,
  690. ReferObjectType: utils.EDB_RELATION_TABLE,
  691. ReferObjectSubType: v.Source,
  692. EdbInfoId: v.EdbInfoId,
  693. EdbName: edbInfo.EdbName,
  694. Source: edbInfo.Source,
  695. EdbCode: edbInfo.EdbCode,
  696. CreateTime: nowTime,
  697. ModifyTime: nowTime,
  698. RelationTime: v.CreateTime,
  699. }
  700. tmp.RelationCode = fmt.Sprintf("%d_%d_%d_%d", tmp.EdbInfoId, tmp.ReferObjectId, tmp.ReferObjectType, tmp.ReferObjectSubType)
  701. addList = append(addList, tmp)
  702. existRelationMap[name] = struct{}{}
  703. // 添加间接引用记录
  704. if edbInfo.EdbType == 2 && edbInfo.EdbInfoType == 0 {
  705. childEdbMappingIds, ok1 := calculateEdbMappingIdsMap[edbInfo.EdbInfoId]
  706. if !ok1 {
  707. continue
  708. }
  709. for _, childEdbMappingId := range childEdbMappingIds {
  710. childEdbMapping, ok2 := calculateEdbMappingListMap[childEdbMappingId]
  711. if !ok2 {
  712. continue
  713. }
  714. name1 := fmt.Sprintf("%d-%d", v.ExcelInfoId, childEdbMapping.FromEdbInfoId)
  715. if _, ok2 := existRelationMap[name1]; !ok2 { //如果已经被直接引用了,则无需添加到间接引用记录中
  716. continue
  717. }
  718. tmp1 := &data_manage.EdbInfoRelation{
  719. ReferObjectId: v.ExcelInfoId,
  720. ReferObjectType: utils.EDB_RELATION_TABLE,
  721. ReferObjectSubType: v.Source,
  722. EdbInfoId: childEdbMapping.FromEdbInfoId,
  723. EdbName: childEdbMapping.FromEdbName,
  724. Source: childEdbMapping.FromSource,
  725. EdbCode: childEdbMapping.FromEdbCode,
  726. CreateTime: nowTime,
  727. ModifyTime: nowTime,
  728. RelationTime: v.CreateTime,
  729. RelationType: 1,
  730. RootEdbInfoId: edbInfo.EdbInfoId,
  731. ChildEdbInfoId: childEdbMapping.EdbInfoId,
  732. RelationCode: tmp.RelationCode,
  733. }
  734. addList = append(addList, tmp1)
  735. // todo 防止重复
  736. }
  737. }
  738. if len(addList) > pageSize {
  739. err = data_manage.AddEdbInfoRelationMulti(addList)
  740. if err != nil {
  741. err = fmt.Errorf("新增引用记录失败 Err:%s", err)
  742. return
  743. }
  744. addNum += len(addList)
  745. addList = make([]*data_manage.EdbInfoRelation, 0)
  746. }
  747. }
  748. }
  749. }
  750. //拿到500个数据ID,判断相关的引用记录,如果已存在则直接过滤,不存在则新增
  751. if len(addList) > 0 {
  752. err = data_manage.AddEdbInfoRelationMulti(addList)
  753. if err != nil {
  754. err = fmt.Errorf("新增引用记录失败 Err:%s", err)
  755. return
  756. }
  757. addNum += len(addList)
  758. }
  759. fmt.Printf("表格指标引用记录处理完成, 新增%d条记录\n", addNum)
  760. return
  761. }
  762. // 处理逻辑图中的指标引用
  763. func InitSandBoxEdbRelation() {
  764. fmt.Println("开始处理逻辑图中的指标引用")
  765. var err error
  766. var addNum int
  767. defer func() {
  768. if err != nil {
  769. msg := fmt.Sprintf("初始化指标在逻辑图中的引用失败 initSandBoxEdbRelation err: %v", err)
  770. utils.FileLog.Info(msg)
  771. fmt.Println(msg)
  772. go alarm_msg.SendAlarmMsg(msg, 3)
  773. }
  774. }()
  775. condition := " AND is_delete = 0"
  776. total, err := sandbox.GetSandboxListCountByCondition(condition, []interface{}{})
  777. if err != nil {
  778. err = fmt.Errorf("查询逻辑图总数失败 err: %v", err)
  779. return
  780. }
  781. if total == 0 {
  782. return
  783. }
  784. //分页查询,每次处理500条记录
  785. pageSize := 100
  786. totalPage := (total + pageSize - 1) / pageSize // 使用整数除法,并添加一页以防有余数
  787. addList := make([]*data_manage.EdbInfoRelation, 0)
  788. //查询图表列表
  789. for i := 0; i < totalPage; i += 1 {
  790. startSize := i * pageSize
  791. list, e := sandbox.GetSandboxListByCondition(condition, []interface{}{}, startSize, pageSize)
  792. if e != nil {
  793. err = fmt.Errorf("查询逻辑图列表失败 Err:%s", e)
  794. return
  795. }
  796. if len(list) == 0 {
  797. break
  798. }
  799. edbInfoIds := make([]int, 0)
  800. edbSandboxMap := make(map[int][]*sandbox.Sandbox)
  801. for _, v := range list {
  802. if v.Content == "" {
  803. continue
  804. }
  805. edbInfoIdsTmp, e := getSandBoxEdbIdsByContent(v.Content)
  806. if e != nil {
  807. continue
  808. //err = fmt.Errorf("查询逻辑图关联的指标Id失败 Err:%s", e)
  809. //return
  810. }
  811. for _, edbId := range edbInfoIdsTmp {
  812. edbInfoIds = append(edbInfoIds, edbId)
  813. edbSandboxMap[edbId] = append(edbSandboxMap[edbId], v)
  814. }
  815. }
  816. if len(edbInfoIds) <= 0 {
  817. continue
  818. }
  819. // 查询指标信息表
  820. edbInfoList, e := data_manage.GetEdbInfoByIdList(edbInfoIds)
  821. if e != nil {
  822. err = fmt.Errorf("查询指标信息列表失败 Err:%s", e)
  823. return
  824. }
  825. if len(edbInfoList) == 0 {
  826. continue
  827. }
  828. // 查询计算指标信息,并且建立关联关系
  829. // 查询间接引用的指标信息
  830. calculateEdbMappingListMap, calculateEdbMappingIdsMap, e := GetEdbListByEdbInfoId(edbInfoList)
  831. if e != nil {
  832. err = fmt.Errorf("查询计算指标信息失败,%s", e.Error())
  833. return
  834. }
  835. edbInfoMap := make(map[int]*data_manage.EdbInfo)
  836. sandboxIds := make([]int, 0)
  837. for _, v := range edbInfoList {
  838. edbInfoMap[v.EdbInfoId] = v
  839. if items, ok := edbSandboxMap[v.EdbInfoId]; ok {
  840. for _, item := range items {
  841. sandboxIds = append(sandboxIds, item.SandboxId)
  842. }
  843. }
  844. }
  845. //查询引用关系列表,
  846. chartEdbRelationList, e := data_manage.GetEdbInfoRelationByReferObjectIds(sandboxIds, utils.EDB_RELATION_SANDBOX)
  847. if e != nil {
  848. err = fmt.Errorf("查询逻辑图引用关系列表失败 Err:%s", e)
  849. return
  850. }
  851. existRelationMap := make(map[string]struct{})
  852. for _, v := range chartEdbRelationList {
  853. name := fmt.Sprintf("%d-%d", v.ReferObjectId, v.EdbInfoId)
  854. existRelationMap[name] = struct{}{}
  855. }
  856. for edbInfoId, sandboxList := range edbSandboxMap {
  857. nowTime := time.Now()
  858. for _, v := range sandboxList {
  859. name := fmt.Sprintf("%d-%d", v.SandboxId, edbInfoId)
  860. if _, ok := existRelationMap[name]; !ok {
  861. edbInfo, ok2 := edbInfoMap[edbInfoId]
  862. if !ok2 {
  863. continue
  864. }
  865. //todo 引用时间
  866. tmp := &data_manage.EdbInfoRelation{
  867. ReferObjectId: v.SandboxId,
  868. ReferObjectType: utils.EDB_RELATION_SANDBOX,
  869. EdbInfoId: edbInfoId,
  870. EdbName: edbInfo.EdbName,
  871. Source: edbInfo.Source,
  872. EdbCode: edbInfo.EdbCode,
  873. CreateTime: nowTime,
  874. ModifyTime: nowTime,
  875. RelationTime: v.CreateTime,
  876. }
  877. tmp.RelationCode = fmt.Sprintf("%d_%d_%d_%d", tmp.EdbInfoId, tmp.ReferObjectId, tmp.ReferObjectType, tmp.ReferObjectSubType)
  878. addList = append(addList, tmp)
  879. existRelationMap[name] = struct{}{}
  880. // 添加间接引用记录
  881. if edbInfo.EdbType == 2 && edbInfo.EdbInfoType == 0 {
  882. childEdbMappingIds, ok1 := calculateEdbMappingIdsMap[edbInfo.EdbInfoId]
  883. if !ok1 {
  884. continue
  885. }
  886. for _, childEdbMappingId := range childEdbMappingIds {
  887. childEdbMapping, ok2 := calculateEdbMappingListMap[childEdbMappingId]
  888. if !ok2 {
  889. continue
  890. }
  891. name1 := fmt.Sprintf("%d-%d", v.SandboxId, childEdbMapping.FromEdbInfoId)
  892. if _, ok2 := existRelationMap[name1]; !ok2 { //如果已经被直接引用了,则无需添加到间接引用记录中
  893. continue
  894. }
  895. tmp1 := &data_manage.EdbInfoRelation{
  896. ReferObjectId: v.SandboxId,
  897. ReferObjectType: utils.EDB_RELATION_SANDBOX,
  898. EdbInfoId: childEdbMapping.FromEdbInfoId,
  899. EdbName: childEdbMapping.FromEdbName,
  900. Source: childEdbMapping.FromSource,
  901. EdbCode: childEdbMapping.FromEdbCode,
  902. CreateTime: nowTime,
  903. ModifyTime: nowTime,
  904. RelationTime: v.CreateTime,
  905. RelationType: 1,
  906. RootEdbInfoId: edbInfo.EdbInfoId,
  907. ChildEdbInfoId: childEdbMapping.EdbInfoId,
  908. RelationCode: tmp.RelationCode,
  909. }
  910. addList = append(addList, tmp1)
  911. // todo 防止重复
  912. }
  913. }
  914. if len(addList) > pageSize {
  915. err = data_manage.AddEdbInfoRelationMulti(addList)
  916. if err != nil {
  917. err = fmt.Errorf("新增引用记录失败 Err:%s", err)
  918. return
  919. }
  920. addNum += len(addList)
  921. addList = make([]*data_manage.EdbInfoRelation, 0)
  922. }
  923. }
  924. }
  925. }
  926. }
  927. //拿到500个数据ID,判断相关的引用记录,如果已存在则直接过滤,不存在则新增
  928. if len(addList) > 0 {
  929. err = data_manage.AddEdbInfoRelationMulti(addList)
  930. if err != nil {
  931. err = fmt.Errorf("新增引用记录失败 Err:%s", err)
  932. return
  933. }
  934. addNum += len(addList)
  935. }
  936. fmt.Printf("逻辑图指标引用记录处理完成, 新增%d条记录\n", addNum)
  937. return
  938. }
  939. func getSandBoxEdbIdsByContent(content string) (edbInfoIds []int, err error) {
  940. var contentInfo sandbox.ContentDataStruct
  941. err = json.Unmarshal([]byte(content), &contentInfo)
  942. if err != nil {
  943. err = fmt.Errorf("json.Unmarshal err:%s", err.Error())
  944. return
  945. }
  946. // 遍历所有节点
  947. for _, node := range contentInfo.Cells {
  948. if node.Data == nil {
  949. continue
  950. }
  951. for _, v := range node.Data.LinkData {
  952. if v.Type == 1 {
  953. edbInfoIds = append(edbInfoIds, v.Id)
  954. }
  955. }
  956. }
  957. return
  958. }
  959. func GetEdbListByEdbInfoId(edbInfoList []*data_manage.EdbInfo) (edbMappingListMap map[int]*data_manage.EdbInfoCalculateMapping, edbInfoMappingRootIdsMap map[int][]int, err error) {
  960. if len(edbInfoList) == 0 {
  961. return
  962. }
  963. edbInfoIds := make([]int, 0)
  964. for _, v := range edbInfoList {
  965. if v.EdbType == 2 && v.EdbInfoType == 0 { //普通计算指标,排除预算指标
  966. edbInfoIds = append(edbInfoIds, v.EdbInfoId)
  967. }
  968. }
  969. if len(edbInfoIds) == 0 {
  970. return
  971. }
  972. //查询指标信息
  973. allEdbMappingMap := make(map[int][]*data_manage.EdbInfoCalculateMappingInfo, 0)
  974. allMappingList, e := data_manage.GetEdbInfoCalculateMappingListByEdbInfoIds(edbInfoIds)
  975. if e != nil {
  976. err = fmt.Errorf("GetEdbInfoCalculateMappingListByEdbInfoIds err: %s", e.Error())
  977. return
  978. }
  979. for _, v := range allMappingList {
  980. if _, ok := allEdbMappingMap[v.EdbInfoId]; !ok {
  981. allEdbMappingMap[v.EdbInfoId] = make([]*data_manage.EdbInfoCalculateMappingInfo, 0)
  982. }
  983. allEdbMappingMap[v.EdbInfoId] = append(allEdbMappingMap[v.EdbInfoId], v)
  984. }
  985. //查询指标映射
  986. //查询所有指标数据
  987. //查询这个指标相关的mapping信息放到数组里,
  988. //将得到的指标ID信息放到数组里
  989. hasFindMap := make(map[int]struct{})
  990. edbInfoIdMap := make(map[int]struct{})
  991. edbMappingList := make([]*data_manage.EdbInfoCalculateMapping, 0)
  992. edbInfoMappingRootIdsMap = make(map[int][]int, 0)
  993. edbMappingMap := make(map[int]struct{})
  994. for _, edbInfo := range edbInfoList {
  995. if edbInfo.EdbType == 2 && edbInfo.EdbInfoType == 0 {
  996. edbInfoId := edbInfo.EdbInfoId
  997. edbMappingList, err = getCalculateEdbInfoByEdbInfoId(allEdbMappingMap, edbInfoId, hasFindMap, edbInfoIdMap, edbMappingList, edbMappingMap, edbInfoMappingRootIdsMap, edbInfoId)
  998. if err != nil {
  999. err = fmt.Errorf(" GetCalculateEdbInfoByEdbInfoId err: %s", err.Error())
  1000. return
  1001. }
  1002. }
  1003. }
  1004. if len(edbMappingList) == 0 {
  1005. return
  1006. }
  1007. // 查询指标信息
  1008. // 指标信息map
  1009. edbInfoIdList := make([]int, 0)
  1010. for k, _ := range edbInfoIdMap {
  1011. edbInfoIdList = append(edbInfoIdList, k)
  1012. }
  1013. edbMappingListMap = make(map[int]*data_manage.EdbInfoCalculateMapping)
  1014. if len(edbMappingList) > 0 {
  1015. for _, v := range edbMappingList {
  1016. edbMappingListMap[v.EdbInfoCalculateMappingId] = v
  1017. }
  1018. }
  1019. return
  1020. }
  1021. // getCalculateEdbInfoByEdbInfoId 计算指标追溯
  1022. func getCalculateEdbInfoByEdbInfoId(allEdbMappingMap map[int][]*data_manage.EdbInfoCalculateMappingInfo, edbInfoId int, hasFindMap map[int]struct{}, edbInfoIdMap map[int]struct{}, edbMappingList []*data_manage.EdbInfoCalculateMapping, edbMappingMap map[int]struct{}, edbInfoMappingRootIdsMap map[int][]int, rootEdbInfoId int) (newEdbMappingList []*data_manage.EdbInfoCalculateMapping, err error) {
  1023. newEdbMappingList = edbMappingList
  1024. _, ok := hasFindMap[edbInfoId]
  1025. if ok {
  1026. return
  1027. }
  1028. if _, ok1 := edbInfoIdMap[edbInfoId]; !ok1 {
  1029. edbInfoIdMap[edbInfoId] = struct{}{}
  1030. }
  1031. edbInfoMappingList := make([]*data_manage.EdbInfoCalculateMappingInfo, 0)
  1032. edbInfoMappingList, ok = allEdbMappingMap[edbInfoId]
  1033. if !ok {
  1034. edbInfoMappingList, err = data_manage.GetEdbInfoCalculateMappingListByEdbInfoId(edbInfoId)
  1035. if err != nil {
  1036. err = fmt.Errorf("GetEdbInfoCalculateMappingListByEdbInfoId err: %s", err.Error())
  1037. return
  1038. }
  1039. }
  1040. hasFindMap[edbInfoId] = struct{}{}
  1041. if len(edbInfoMappingList) > 0 {
  1042. fromEdbInfoIdList := make([]int, 0)
  1043. edbInfoMappingIdList := make([]int, 0)
  1044. for _, v := range edbInfoMappingList {
  1045. fromEdbInfoIdList = append(fromEdbInfoIdList, v.FromEdbInfoId)
  1046. edbInfoMappingIdList = append(edbInfoMappingIdList, v.EdbInfoCalculateMappingId)
  1047. if _, ok1 := edbInfoIdMap[v.FromEdbInfoId]; !ok1 {
  1048. edbInfoIdMap[v.FromEdbInfoId] = struct{}{}
  1049. }
  1050. if _, ok2 := edbMappingMap[v.EdbInfoCalculateMappingId]; !ok2 {
  1051. edbMappingMap[v.EdbInfoCalculateMappingId] = struct{}{}
  1052. tmp := &data_manage.EdbInfoCalculateMapping{
  1053. EdbInfoCalculateMappingId: v.EdbInfoCalculateMappingId,
  1054. EdbInfoId: v.EdbInfoId,
  1055. Source: v.Source,
  1056. SourceName: v.SourceName,
  1057. EdbCode: v.EdbCode,
  1058. FromEdbInfoId: v.FromEdbInfoId,
  1059. FromEdbCode: v.FromEdbCode,
  1060. FromEdbName: v.FromEdbName,
  1061. FromSource: v.FromSource,
  1062. FromSourceName: v.FromSourceName,
  1063. FromTag: v.FromTag,
  1064. Sort: v.Sort,
  1065. CreateTime: v.CreateTime,
  1066. ModifyTime: v.ModifyTime,
  1067. }
  1068. newEdbMappingList = append(newEdbMappingList, tmp)
  1069. }
  1070. if edbInfoId != v.FromEdbInfoId && (v.FromEdbType == 2 || v.FromEdbInfoType == 1) {
  1071. // 查过了就不查了
  1072. if _, ok2 := hasFindMap[v.FromEdbInfoId]; !ok2 {
  1073. newEdbMappingList, err = getCalculateEdbInfoByEdbInfoId(allEdbMappingMap, v.FromEdbInfoId, hasFindMap, edbInfoIdMap, newEdbMappingList, edbMappingMap, edbInfoMappingRootIdsMap, rootEdbInfoId)
  1074. if err != nil {
  1075. err = fmt.Errorf("traceEdbInfoByEdbInfoId err: %s", err.Error())
  1076. return
  1077. }
  1078. }
  1079. }
  1080. hasFindMap[v.FromEdbInfoId] = struct{}{}
  1081. }
  1082. edbInfoMappingRootIdsMap[rootEdbInfoId] = append(edbInfoMappingRootIdsMap[rootEdbInfoId], edbInfoMappingIdList...)
  1083. }
  1084. return
  1085. }
  1086. // GetCalculateEdbByFromEdbInfo 找到依赖于该基础指标的所有计算指标
  1087. func GetCalculateEdbByFromEdbInfo(edbInfoIds []int, calculateEdbIds []int, hasFind map[int]struct{}) (newCalculateEdbIds []int, err error) {
  1088. if len(edbInfoIds) == 0 {
  1089. return
  1090. }
  1091. newCalculateEdbIds = calculateEdbIds
  1092. newEdbInfoIds := make([]int, 0)
  1093. for _, v := range edbInfoIds {
  1094. if _, ok := hasFind[v]; ok {
  1095. continue
  1096. }
  1097. newEdbInfoIds = append(newEdbInfoIds, v)
  1098. }
  1099. if len(newEdbInfoIds) == 0 {
  1100. return
  1101. }
  1102. var condition string
  1103. var pars []interface{}
  1104. // 关联指标
  1105. condition += ` AND b.from_edb_info_id in (` + utils.GetOrmInReplace(len(newEdbInfoIds)) + `)`
  1106. pars = append(pars, newEdbInfoIds)
  1107. //获取关联图表列表
  1108. list, err := data_manage.GetRelationEdbInfoListMappingByCondition(condition, pars)
  1109. if err != nil && err.Error() != utils.ErrNoRow() {
  1110. err = fmt.Errorf("获取关联指标信息失败,Err:%s", err.Error())
  1111. return
  1112. }
  1113. calculateEdbIdsTmp := make([]int, 0)
  1114. for _, mapping := range list {
  1115. if mapping.EdbType == 2 && mapping.EdbInfoType == 0 { // 如果指标库里的计算指标,则加入,否则继续找
  1116. newCalculateEdbIds = append(newCalculateEdbIds, mapping.EdbInfoId)
  1117. calculateEdbIdsTmp = append(calculateEdbIdsTmp, mapping.EdbInfoId)
  1118. }
  1119. }
  1120. for _, v := range newEdbInfoIds {
  1121. hasFind[v] = struct{}{}
  1122. }
  1123. if len(calculateEdbIdsTmp) > 0 {
  1124. newCalculateEdbIds, err = GetCalculateEdbByFromEdbInfo(calculateEdbIdsTmp, newCalculateEdbIds, hasFind)
  1125. if err != nil {
  1126. return
  1127. }
  1128. }
  1129. return
  1130. }