edb_info_relation.go 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381
  1. package services
  2. import (
  3. "eta_gn/eta_index_lib/models"
  4. "eta_gn/eta_index_lib/services/alarm_msg"
  5. "eta_gn/eta_index_lib/utils"
  6. "fmt"
  7. "sort"
  8. "strconv"
  9. "time"
  10. )
  11. // 重置单个计算指标中的引用关系
  12. func ResetEdbRelation(edbInfoId int) {
  13. var logMsg string
  14. var replaceTotal int
  15. var err error
  16. defer func() {
  17. if err != nil {
  18. msg := fmt.Sprintf(" 重置单个计算指标中的引用关系失败 ResetEdbRelation err: %v", err)
  19. utils.FileLog.Info(msg)
  20. fmt.Println(msg)
  21. go alarm_msg.SendAlarmMsg(msg, 3)
  22. }
  23. if logMsg != `` {
  24. utils.FileLog.Info(fmt.Sprintf("重置单个计算指标中的引用关系失败 重置总数%d,涉及到的引用id:%s", replaceTotal, logMsg))
  25. }
  26. }()
  27. //查询与该计算指标相关的间接引用或者间接引用关系,如果记录不存在,则不处理
  28. _, err = models.GetEdbInfoRelationByChildEdbInfoId(edbInfoId)
  29. if err != nil {
  30. if err.Error() == utils.ErrNoRow() {
  31. err = nil
  32. return
  33. }
  34. err = fmt.Errorf("查询与该计算指标相关的间接引用或者间接引用关系失败,错误信息:%s", err.Error())
  35. return
  36. }
  37. //查询当前计算指标最新的引用指标列表
  38. newMappingList, err := models.GetEdbInfoCalculateDetailList(edbInfoId)
  39. if err != nil {
  40. err = fmt.Errorf("查询当前计算指标最新的指标列表失败,错误信息:%s", err.Error())
  41. return
  42. }
  43. //整理关联的来源指标ID
  44. newEdbIdList := make([]string, 0)
  45. newMappingListMap := make(map[int]*models.EdbInfoCalculateDetail)
  46. for _, v := range newMappingList {
  47. newEdbIdList = append(newEdbIdList, strconv.Itoa(v.FromEdbInfoId))
  48. newMappingListMap[v.FromEdbInfoId] = v
  49. }
  50. //对指标ID进行排序
  51. sort.Strings(newEdbIdList)
  52. newEdbIdStr := ""
  53. for _, v := range newEdbIdList {
  54. newEdbIdStr += v + ","
  55. }
  56. //二者匹配一下,如果相同,则不处理,如果不同,先查询所有旧的间接引用记录,整理并分组,则删除旧的间接引用记录,新增新的间接引用记录,
  57. tmpList, err := models.GetEdbInfoRelationListByChildEdbInfoId(edbInfoId)
  58. if err != nil {
  59. err = fmt.Errorf("查询当前计算指标的间接引用关系失败,错误信息:%s", err.Error())
  60. return
  61. }
  62. parentRelationIds := make([]int, 0)
  63. for _, v := range tmpList {
  64. parentRelationIds = append(parentRelationIds, v.ParentRelationId)
  65. }
  66. if len(parentRelationIds) > 0 {
  67. // 查询单个项目的引用列表作为判断依据
  68. oldEdbIdList, err := models.GetEdbInfoRelationEdbIdsByParentRelationId(parentRelationIds[0], edbInfoId)
  69. if err != nil {
  70. err = fmt.Errorf("查询当前计算指标的间接引用关系失败,错误信息:%s", err.Error())
  71. return
  72. }
  73. sort.Ints(oldEdbIdList)
  74. oldEdbIdStr := ""
  75. for _, v := range oldEdbIdList {
  76. oldEdbIdStr += strconv.Itoa(v) + ","
  77. }
  78. // 把切片转成字符串
  79. if newEdbIdStr == oldEdbIdStr {
  80. return
  81. }
  82. list, e := models.GetEdbInfoRelationByRelationIds(parentRelationIds)
  83. if e != nil {
  84. err = fmt.Errorf("查询图表关联指标列表失败 Err:%s", e)
  85. return
  86. }
  87. //查询直接引用指标关联关系
  88. edbInfoListMap := make(map[int]struct{})
  89. edbInfoIds := make([]int, 0)
  90. for _, v := range list {
  91. if _, ok := edbInfoListMap[v.EdbInfoId]; !ok {
  92. edbInfoListMap[v.EdbInfoId] = struct{}{}
  93. edbInfoIds = append(edbInfoIds, v.EdbInfoId)
  94. }
  95. }
  96. edbInfoList := make([]*models.EdbInfo, 0)
  97. if len(edbInfoIds) > 0 {
  98. // 查询指标信息
  99. edbInfoList, err = models.GetEdbInfoByIdList(edbInfoIds)
  100. if err != nil {
  101. err = fmt.Errorf("查询指标信息失败 Err:%s", err)
  102. return
  103. }
  104. }
  105. calculateEdbMappingListMap, calculateEdbMappingIdsMap, err := GetEdbListByEdbInfoId(edbInfoList)
  106. if err != nil {
  107. err = fmt.Errorf("查询指标关联指标列表失败 Err:%s", err)
  108. return
  109. }
  110. //如何过滤掉只有间接引用,没有直接引用的
  111. replaceTotal1, logMsg1, e := UpdateSecondEdbInRelation(list, calculateEdbMappingListMap, calculateEdbMappingIdsMap, edbInfoList)
  112. if e != nil {
  113. err = e
  114. return
  115. }
  116. replaceTotal += replaceTotal1
  117. logMsg += logMsg1
  118. }
  119. return
  120. }
  121. // 更新间接引用
  122. func UpdateSecondEdbInRelation(list []*models.EdbInfoRelation, calculateEdbMappingListMap map[int]*models.EdbInfoCalculateMapping, calculateEdbMappingIdsMap map[int][]int, edbInfoList []*models.EdbInfo) (replaceTotal int, logMsg string, err error) {
  123. nowTime := time.Now()
  124. edbInfoRelationIds := make([]int, 0)
  125. indexCodeList := make([]string, 0)
  126. addList := make([]*models.EdbInfoRelation, 0)
  127. refreshIds := make([]int, 0)
  128. edbInfoMap := make(map[int]*models.EdbInfo)
  129. for _, v := range edbInfoList {
  130. edbInfoMap[v.EdbInfoId] = v
  131. }
  132. // 查询所有的直接引用,删除所有的间接引用,添加所有直接引用的间接引用
  133. for _, v := range list {
  134. if v.RelationType == 0 {
  135. edbInfoRelationIds = append(edbInfoRelationIds, v.EdbInfoRelationId)
  136. edbInfo, ok := edbInfoMap[v.EdbInfoId]
  137. if !ok {
  138. err = fmt.Errorf("查询指标信息失败 EdbInfoId:%d", v.EdbInfoId)
  139. return
  140. }
  141. if edbInfo.EdbType == 2 { //计算指标
  142. childEdbMappingIds, ok := calculateEdbMappingIdsMap[edbInfo.EdbInfoId]
  143. if !ok {
  144. err = fmt.Errorf("查询%d指标关联指标列表为空", edbInfo.EdbInfoId)
  145. return
  146. }
  147. for _, childEdbMappingId := range childEdbMappingIds {
  148. childEdbMapping, ok2 := calculateEdbMappingListMap[childEdbMappingId]
  149. if !ok2 {
  150. continue
  151. }
  152. if childEdbMapping.FromSource == utils.DATA_SOURCE_MYSTEEL_CHEMICAL {
  153. indexCodeList = append(indexCodeList, childEdbMapping.FromEdbCode)
  154. }
  155. tmp1 := &models.EdbInfoRelation{
  156. ReferObjectId: v.ReferObjectId,
  157. ReferObjectType: v.ReferObjectType,
  158. ReferObjectSubType: v.ReferObjectSubType,
  159. EdbInfoId: childEdbMapping.FromEdbInfoId,
  160. EdbName: childEdbMapping.FromEdbName,
  161. Source: childEdbMapping.FromSource,
  162. EdbCode: childEdbMapping.FromEdbCode,
  163. CreateTime: nowTime,
  164. ModifyTime: nowTime,
  165. RelationTime: childEdbMapping.CreateTime,
  166. RelationType: 1,
  167. RootEdbInfoId: edbInfo.EdbInfoId,
  168. ChildEdbInfoId: childEdbMapping.EdbInfoId,
  169. }
  170. tmp1.RelationCode = fmt.Sprintf("%d_%d_%d_%d", tmp1.RootEdbInfoId, tmp1.ReferObjectId, tmp1.ReferObjectType, tmp1.ReferObjectSubType)
  171. addList = append(addList, tmp1)
  172. refreshIds = append(refreshIds, childEdbMapping.FromEdbInfoId)
  173. }
  174. }
  175. }
  176. }
  177. if len(edbInfoRelationIds) > 0 {
  178. err = models.UpdateSecondRelationEdbInfoId(edbInfoRelationIds, addList, refreshIds, indexCodeList)
  179. if err != nil {
  180. logMsg = ""
  181. err = fmt.Errorf("替换指标引用表中的指标ID失败 Err:%s", err)
  182. return
  183. }
  184. replaceTotal = len(edbInfoRelationIds)
  185. }
  186. return
  187. }
  188. // 设置成禁用状态
  189. func DisableEdbInfoNoUpdate(edbInfo *models.EdbInfo) (err error) {
  190. // 如果一个计算指标里,包涵的基础指标是停用状态,那么计算指标也要是停用状态停用状态
  191. newBaseEdbInfoList := make([]int, 0)
  192. hasFind := make(map[int]struct{})
  193. newBaseEdbInfoIds, err := FindBaseEdbInfo(edbInfo.EdbInfoId, newBaseEdbInfoList, hasFind)
  194. if err != nil {
  195. err = fmt.Errorf("查找基础指标信息失败,err:%v", err)
  196. return
  197. }
  198. // 查询是否存在停用指标,如果存在,则计算指标也要是停用状态
  199. total, err := models.GetEdbInfoNoUpdateTotalByIdList(newBaseEdbInfoIds)
  200. if err != nil {
  201. err = fmt.Errorf("查询基础指标信息失败,err:%v", err)
  202. return
  203. }
  204. if total > 0 {
  205. edbInfo.NoUpdate = 1
  206. edbInfo.ModifyTime = time.Now()
  207. err = edbInfo.Update([]string{"NoUpdate", "ModifyTime"})
  208. if err != nil {
  209. err = fmt.Errorf("更新计算指标刷新状态失败,err:%v", err)
  210. return
  211. }
  212. }
  213. return
  214. }
  215. // 找到基础指标的过程
  216. func FindBaseEdbInfo(edbInfoId int, baseEdbInfoList []int, hasFind map[int]struct{}) (newBaseEdbInfoList []int, err error) {
  217. newBaseEdbInfoList = baseEdbInfoList
  218. if _, ok := hasFind[edbInfoId]; ok {
  219. return
  220. }
  221. // 先找到所有的引用关系
  222. //查询当前计算指标最新的引用指标列表
  223. newMappingList, err := models.GetEdbInfoCalculateDetailList(edbInfoId)
  224. if err != nil {
  225. err = fmt.Errorf("查询当前计算指标最新的指标列表失败,错误信息:%s", err.Error())
  226. return
  227. }
  228. hasFind[edbInfoId] = struct{}{}
  229. for _, mapping := range newMappingList {
  230. newBaseEdbInfoList = append(newBaseEdbInfoList, mapping.FromEdbInfoId)
  231. if mapping.EdbType == 1 { // 如果是基础指标,则加入,否则继续找
  232. } else {
  233. newBaseEdbInfoList, err = FindBaseEdbInfo(mapping.FromEdbInfoId, newBaseEdbInfoList, hasFind)
  234. }
  235. }
  236. return
  237. }
  238. // 查找当前计算指标的所有溯源指标
  239. func GetEdbListByEdbInfoId(edbInfoList []*models.EdbInfo) (edbMappingListMap map[int]*models.EdbInfoCalculateMapping, edbInfoMappingRootIdsMap map[int][]int, err error) {
  240. if len(edbInfoList) == 0 {
  241. return
  242. }
  243. edbInfoIds := make([]int, 0)
  244. for _, v := range edbInfoList {
  245. if v.EdbType == 2 && v.EdbInfoType == 0 { //普通计算指标,排除预算指标
  246. edbInfoIds = append(edbInfoIds, v.EdbInfoId)
  247. }
  248. }
  249. if len(edbInfoIds) == 0 {
  250. return
  251. }
  252. //查询指标信息
  253. allEdbMappingMap := make(map[int][]*models.EdbInfoCalculateMappingInfo, 0)
  254. allMappingList, e := models.GetEdbInfoCalculateMappingListByEdbInfoIds(edbInfoIds)
  255. if e != nil {
  256. err = fmt.Errorf("GetEdbInfoCalculateMappingListByEdbInfoIds err: %s", e.Error())
  257. return
  258. }
  259. for _, v := range allMappingList {
  260. if _, ok := allEdbMappingMap[v.EdbInfoId]; !ok {
  261. allEdbMappingMap[v.EdbInfoId] = make([]*models.EdbInfoCalculateMappingInfo, 0)
  262. }
  263. allEdbMappingMap[v.EdbInfoId] = append(allEdbMappingMap[v.EdbInfoId], v)
  264. }
  265. //查询指标映射
  266. //查询所有指标数据
  267. //查询这个指标相关的mapping信息放到数组里,
  268. //将得到的指标ID信息放到数组里
  269. hasFindMap := make(map[int]struct{})
  270. edbInfoIdMap := make(map[int]struct{})
  271. edbMappingList := make([]*models.EdbInfoCalculateMapping, 0)
  272. edbInfoMappingRootIdsMap = make(map[int][]int, 0)
  273. edbMappingMap := make(map[int]struct{})
  274. for _, edbInfo := range edbInfoList {
  275. if edbInfo.EdbType == 2 && edbInfo.EdbInfoType == 0 {
  276. edbInfoId := edbInfo.EdbInfoId
  277. edbMappingList, err = getCalculateEdbInfoByEdbInfoId(allEdbMappingMap, edbInfoId, hasFindMap, edbInfoIdMap, edbMappingList, edbMappingMap, edbInfoMappingRootIdsMap, edbInfoId)
  278. if err != nil {
  279. err = fmt.Errorf(" GetCalculateEdbInfoByEdbInfoId err: %s", err.Error())
  280. return
  281. }
  282. }
  283. }
  284. if len(edbMappingList) == 0 {
  285. return
  286. }
  287. // 查询指标信息
  288. // 指标信息map
  289. edbInfoIdList := make([]int, 0)
  290. for k, _ := range edbInfoIdMap {
  291. edbInfoIdList = append(edbInfoIdList, k)
  292. }
  293. edbMappingListMap = make(map[int]*models.EdbInfoCalculateMapping)
  294. if len(edbMappingList) > 0 {
  295. for _, v := range edbMappingList {
  296. edbMappingListMap[v.EdbInfoCalculateMappingId] = v
  297. }
  298. }
  299. return
  300. }
  301. // getCalculateEdbInfoByEdbInfoId 计算指标追溯
  302. func getCalculateEdbInfoByEdbInfoId(allEdbMappingMap map[int][]*models.EdbInfoCalculateMappingInfo, edbInfoId int, hasFindMap map[int]struct{}, edbInfoIdMap map[int]struct{}, edbMappingList []*models.EdbInfoCalculateMapping, edbMappingMap map[int]struct{}, edbInfoMappingRootIdsMap map[int][]int, rootEdbInfoId int) (newEdbMappingList []*models.EdbInfoCalculateMapping, err error) {
  303. newEdbMappingList = edbMappingList
  304. _, ok := hasFindMap[edbInfoId]
  305. if ok {
  306. return
  307. }
  308. if _, ok1 := edbInfoIdMap[edbInfoId]; !ok1 {
  309. edbInfoIdMap[edbInfoId] = struct{}{}
  310. }
  311. edbInfoMappingList := make([]*models.EdbInfoCalculateMappingInfo, 0)
  312. edbInfoMappingList, ok = allEdbMappingMap[edbInfoId]
  313. if !ok {
  314. edbInfoMappingList, err = models.GetEdbInfoCalculateMappingListByEdbInfoId(edbInfoId)
  315. if err != nil {
  316. err = fmt.Errorf("GetEdbInfoCalculateMappingListByEdbInfoId err: %s", err.Error())
  317. return
  318. }
  319. }
  320. hasFindMap[edbInfoId] = struct{}{}
  321. if len(edbInfoMappingList) > 0 {
  322. fromEdbInfoIdList := make([]int, 0)
  323. edbInfoMappingIdList := make([]int, 0)
  324. for _, v := range edbInfoMappingList {
  325. fromEdbInfoIdList = append(fromEdbInfoIdList, v.FromEdbInfoId)
  326. edbInfoMappingIdList = append(edbInfoMappingIdList, v.EdbInfoCalculateMappingId)
  327. if _, ok1 := edbInfoIdMap[v.FromEdbInfoId]; !ok1 {
  328. edbInfoIdMap[v.FromEdbInfoId] = struct{}{}
  329. }
  330. if _, ok2 := edbMappingMap[v.EdbInfoCalculateMappingId]; !ok2 {
  331. edbMappingMap[v.EdbInfoCalculateMappingId] = struct{}{}
  332. tmp := &models.EdbInfoCalculateMapping{
  333. EdbInfoCalculateMappingId: v.EdbInfoCalculateMappingId,
  334. EdbInfoId: v.EdbInfoId,
  335. Source: v.Source,
  336. SourceName: v.SourceName,
  337. EdbCode: v.EdbCode,
  338. FromEdbInfoId: v.FromEdbInfoId,
  339. FromEdbCode: v.FromEdbCode,
  340. FromEdbName: v.FromEdbName,
  341. FromSource: v.FromSource,
  342. FromSourceName: v.FromSourceName,
  343. FromTag: v.FromTag,
  344. Sort: v.Sort,
  345. CreateTime: v.CreateTime,
  346. ModifyTime: v.ModifyTime,
  347. }
  348. newEdbMappingList = append(newEdbMappingList, tmp)
  349. }
  350. if edbInfoId != v.FromEdbInfoId && (v.FromEdbType == 2 || v.FromEdbInfoType == 1) {
  351. // 查过了就不查了
  352. if _, ok2 := hasFindMap[v.FromEdbInfoId]; !ok2 {
  353. newEdbMappingList, err = getCalculateEdbInfoByEdbInfoId(allEdbMappingMap, v.FromEdbInfoId, hasFindMap, edbInfoIdMap, newEdbMappingList, edbMappingMap, edbInfoMappingRootIdsMap, rootEdbInfoId)
  354. if err != nil {
  355. err = fmt.Errorf("traceEdbInfoByEdbInfoId err: %s", err.Error())
  356. return
  357. }
  358. }
  359. }
  360. hasFindMap[v.FromEdbInfoId] = struct{}{}
  361. }
  362. edbInfoMappingRootIdsMap[rootEdbInfoId] = append(edbInfoMappingRootIdsMap[rootEdbInfoId], edbInfoMappingIdList...)
  363. }
  364. return
  365. }