edb_info_relation.go 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122
  1. package data
  2. import (
  3. "eta/eta_mobile/models/data_manage"
  4. "fmt"
  5. )
  6. // 查询两个指标是否存在循环引用关系
  7. func CheckTwoEdbInfoRelation(edbInfoA, edbInfoB *data_manage.EdbInfo) (hasRelation bool, err error) {
  8. //查询指标信息
  9. allEdbMappingMap := make(map[int][]*data_manage.EdbInfoCalculateMappingInfo, 0)
  10. allMappingList, e := data_manage.GetEdbInfoCalculateMappingListByEdbInfoId(edbInfoA.EdbInfoId)
  11. if e != nil {
  12. err = fmt.Errorf("GetEdbInfoCalculateMappingListByEdbInfoIds err: %s", e.Error())
  13. return
  14. }
  15. for _, v := range allMappingList {
  16. if v.EdbInfoId == edbInfoB.EdbInfoId {
  17. hasRelation = true
  18. return
  19. }
  20. if _, ok := allEdbMappingMap[v.EdbInfoId]; !ok {
  21. allEdbMappingMap[v.EdbInfoId] = make([]*data_manage.EdbInfoCalculateMappingInfo, 0)
  22. }
  23. allEdbMappingMap[v.EdbInfoId] = append(allEdbMappingMap[v.EdbInfoId], v)
  24. }
  25. //查询指标映射
  26. //查询所有指标数据
  27. //查询这个指标相关的mapping信息放到数组里,
  28. //将得到的指标ID信息放到数组里
  29. hasFindMap := make(map[int]struct{})
  30. edbInfoIdMap := make(map[int]struct{})
  31. edbMappingList := make([]*data_manage.EdbInfoCalculateMapping, 0)
  32. edbInfoMappingRootIdsMap := make(map[int][]int, 0)
  33. edbMappingMap := make(map[int]struct{})
  34. if edbInfoA.EdbType == 2 {
  35. edbInfoId := edbInfoA.EdbInfoId
  36. edbMappingList, err = getCalculateEdbInfoByEdbInfoId(allEdbMappingMap, edbInfoId, hasFindMap, edbInfoIdMap, edbMappingList, edbMappingMap, edbInfoMappingRootIdsMap, edbInfoId)
  37. if err != nil {
  38. err = fmt.Errorf(" GetCalculateEdbInfoByEdbInfoId err: %s", err.Error())
  39. return
  40. }
  41. // 判断其中是否包含指标B
  42. if _, ok := edbInfoIdMap[edbInfoB.EdbInfoId]; ok { // 如果包含,则说明存在循环引用关系
  43. hasRelation = true
  44. return
  45. }
  46. }
  47. return
  48. }
  49. // getCalculateEdbInfoByEdbInfoId 计算指标追溯
  50. 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) {
  51. newEdbMappingList = edbMappingList
  52. _, ok := hasFindMap[edbInfoId]
  53. if ok {
  54. return
  55. }
  56. if _, ok1 := edbInfoIdMap[edbInfoId]; !ok1 {
  57. edbInfoIdMap[edbInfoId] = struct{}{}
  58. }
  59. edbInfoMappingList := make([]*data_manage.EdbInfoCalculateMappingInfo, 0)
  60. edbInfoMappingList, ok = allEdbMappingMap[edbInfoId]
  61. if !ok {
  62. edbInfoMappingList, err = data_manage.GetEdbInfoCalculateMappingListByEdbInfoId(edbInfoId)
  63. if err != nil {
  64. err = fmt.Errorf("GetEdbInfoCalculateMappingListByEdbInfoId err: %s", err.Error())
  65. return
  66. }
  67. }
  68. hasFindMap[edbInfoId] = struct{}{}
  69. if len(edbInfoMappingList) > 0 {
  70. fromEdbInfoIdList := make([]int, 0)
  71. edbInfoMappingIdList := make([]int, 0)
  72. for _, v := range edbInfoMappingList {
  73. fromEdbInfoIdList = append(fromEdbInfoIdList, v.FromEdbInfoId)
  74. edbInfoMappingIdList = append(edbInfoMappingIdList, v.EdbInfoCalculateMappingId)
  75. if _, ok1 := edbInfoIdMap[v.FromEdbInfoId]; !ok1 {
  76. edbInfoIdMap[v.FromEdbInfoId] = struct{}{}
  77. }
  78. if _, ok2 := edbMappingMap[v.EdbInfoCalculateMappingId]; !ok2 {
  79. edbMappingMap[v.EdbInfoCalculateMappingId] = struct{}{}
  80. tmp := &data_manage.EdbInfoCalculateMapping{
  81. EdbInfoCalculateMappingId: v.EdbInfoCalculateMappingId,
  82. EdbInfoId: v.EdbInfoId,
  83. Source: v.Source,
  84. SourceName: v.SourceName,
  85. EdbCode: v.EdbCode,
  86. FromEdbInfoId: v.FromEdbInfoId,
  87. FromEdbCode: v.FromEdbCode,
  88. FromEdbName: v.FromEdbName,
  89. FromSource: v.FromSource,
  90. FromSourceName: v.FromSourceName,
  91. FromTag: v.FromTag,
  92. Sort: v.Sort,
  93. CreateTime: v.CreateTime,
  94. ModifyTime: v.ModifyTime,
  95. }
  96. newEdbMappingList = append(newEdbMappingList, tmp)
  97. }
  98. if edbInfoId != v.FromEdbInfoId && (v.FromEdbType == 2 || v.FromEdbInfoType == 1) {
  99. // 查过了就不查了
  100. if _, ok2 := hasFindMap[v.FromEdbInfoId]; !ok2 {
  101. newEdbMappingList, err = getCalculateEdbInfoByEdbInfoId(allEdbMappingMap, v.FromEdbInfoId, hasFindMap, edbInfoIdMap, newEdbMappingList, edbMappingMap, edbInfoMappingRootIdsMap, rootEdbInfoId)
  102. if err != nil {
  103. err = fmt.Errorf("traceEdbInfoByEdbInfoId err: %s", err.Error())
  104. return
  105. }
  106. }
  107. }
  108. hasFindMap[v.FromEdbInfoId] = struct{}{}
  109. }
  110. edbInfoMappingRootIdsMap[rootEdbInfoId] = append(edbInfoMappingRootIdsMap[rootEdbInfoId], edbInfoMappingIdList...)
  111. }
  112. return
  113. }