edb_relation.go 45 KB

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