edb_relation.go 39 KB

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