edb_info_relation.go 23 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680
  1. package data
  2. import (
  3. "eta/eta_api/models/data_manage"
  4. excelModel "eta/eta_api/models/data_manage/excel"
  5. "eta/eta_api/models/fe_calendar"
  6. "eta/eta_api/services/alarm_msg"
  7. "eta/eta_api/services/sandbox"
  8. "eta/eta_api/utils"
  9. "fmt"
  10. "strings"
  11. "time"
  12. )
  13. // SaveChartEdbInfoRelation 添加/编辑图表指标引用关联记录
  14. func SaveChartEdbInfoRelation(edbInfoIds []int, chartInfo *data_manage.ChartInfo) (err error) {
  15. //更新指标刷新状态为启用
  16. err = saveEdbInfoRelation(edbInfoIds, chartInfo.ChartInfoId, utils.EDB_RELATION_CHART, chartInfo.Source)
  17. return
  18. }
  19. // saveEdbInfoRelation 添加/编辑图表指标引用关联记录
  20. func saveEdbInfoRelation(edbInfoIds []int, objectId, objectType, objectSubType int) (err error) {
  21. // 实现添加引用记录的逻辑
  22. if len(edbInfoIds) == 0 {
  23. return
  24. }
  25. defer func() {
  26. if err != nil {
  27. tips := "实现添加引用记录的逻辑-添加/编辑图表指标引用关联记录失败, ErrMsg:\n" + err.Error()
  28. utils.FileLog.Info(tips)
  29. go alarm_msg.SendAlarmMsg(tips, 3)
  30. }
  31. }()
  32. refreshIds := make([]int, 0)
  33. indexCodeList := make([]string, 0)
  34. // 查询指标信息
  35. edbInfoListTmp, e := data_manage.GetEdbInfoByIdList(edbInfoIds)
  36. if e != nil {
  37. err = fmt.Errorf("查询指标信息失败,%s", e.Error())
  38. return
  39. }
  40. // 过滤预测指标
  41. edbInfoList := make([]*data_manage.EdbInfo, 0)
  42. for _, v := range edbInfoListTmp {
  43. if v.EdbInfoType == 0 {
  44. edbInfoList = append(edbInfoList, v)
  45. }
  46. }
  47. // 查询计算指标信息,并且建立关联关系
  48. // 查询间接引用的指标信息
  49. calculateEdbMappingListMap, calculateEdbMappingIdsMap, e := GetEdbListByEdbInfoId(edbInfoList)
  50. if e != nil {
  51. err = fmt.Errorf("查询计算指标信息失败,%s", e.Error())
  52. return
  53. }
  54. // 只统计钢联化工和wind来源的指标
  55. for _, edbInfo := range edbInfoList {
  56. /*if edbInfo.Source != utils.DATA_SOURCE_WIND && edbInfo.Source != utils.DATA_SOURCE_MYSTEEL_CHEMICAL {
  57. continue
  58. }*/
  59. refreshIds = append(refreshIds, edbInfo.EdbInfoId)
  60. if edbInfo.Source == utils.DATA_SOURCE_MYSTEEL_CHEMICAL {
  61. indexCodeList = append(indexCodeList, edbInfo.EdbCode)
  62. }
  63. }
  64. // 循转组装引用
  65. // 查询已有的引用关系
  66. existList, e := data_manage.GetEdbInfoRelationByReferObjectId(objectId, objectType)
  67. if e != nil {
  68. err = fmt.Errorf("查询已有的引用关系失败,%s", e.Error())
  69. return
  70. }
  71. deleteMap := make(map[int]bool)
  72. relationMap := make(map[int]bool)
  73. for _, exist := range existList {
  74. deleteMap[exist.EdbInfoId] = true
  75. relationMap[exist.EdbInfoId] = true
  76. }
  77. // 新增不存在的引用关系
  78. // 删除不再需要的引用关系
  79. nowTime := time.Now()
  80. addList := make([]*data_manage.EdbInfoRelation, 0)
  81. deleteEdbInfoIds := make([]int, 0)
  82. for _, edbInfo := range edbInfoList {
  83. /*if edbInfo.Source != utils.DATA_SOURCE_WIND && edbInfo.Source != utils.DATA_SOURCE_MYSTEEL_CHEMICAL {
  84. continue
  85. }*/
  86. if _, ok := relationMap[edbInfo.EdbInfoId]; ok {
  87. delete(deleteMap, edbInfo.EdbInfoId)
  88. } else {
  89. tmp := &data_manage.EdbInfoRelation{
  90. ReferObjectId: objectId,
  91. ReferObjectType: objectType,
  92. ReferObjectSubType: objectSubType,
  93. EdbInfoId: edbInfo.EdbInfoId,
  94. EdbName: edbInfo.EdbName,
  95. Source: edbInfo.Source,
  96. EdbCode: edbInfo.EdbCode,
  97. CreateTime: nowTime,
  98. ModifyTime: nowTime,
  99. RelationTime: nowTime,
  100. }
  101. tmp.RelationCode = fmt.Sprintf("%d_%d_%d_%d", tmp.EdbInfoId, tmp.ReferObjectId, tmp.ReferObjectType, tmp.ReferObjectSubType)
  102. addList = append(addList, tmp)
  103. if edbInfo.EdbType == 2 && edbInfo.EdbInfoType == 0 {
  104. childEdbMappingIds, ok1 := calculateEdbMappingIdsMap[edbInfo.EdbInfoId]
  105. if !ok1 {
  106. continue
  107. }
  108. for _, childEdbMappingId := range childEdbMappingIds {
  109. childEdbMapping, ok2 := calculateEdbMappingListMap[childEdbMappingId]
  110. if !ok2 {
  111. continue
  112. }
  113. if childEdbMapping.FromSource == utils.DATA_SOURCE_MYSTEEL_CHEMICAL {
  114. indexCodeList = append(indexCodeList, childEdbMapping.FromEdbCode)
  115. }
  116. tmp1 := &data_manage.EdbInfoRelation{
  117. ReferObjectId: objectId,
  118. ReferObjectType: objectType,
  119. ReferObjectSubType: objectSubType,
  120. EdbInfoId: childEdbMapping.FromEdbInfoId,
  121. EdbName: childEdbMapping.FromEdbName,
  122. Source: childEdbMapping.FromSource,
  123. EdbCode: childEdbMapping.FromEdbCode,
  124. CreateTime: nowTime,
  125. ModifyTime: nowTime,
  126. RelationTime: nowTime,
  127. RelationType: 1,
  128. RootEdbInfoId: edbInfo.EdbInfoId,
  129. ChildEdbInfoId: childEdbMapping.EdbInfoId,
  130. RelationCode: tmp.RelationCode,
  131. }
  132. addList = append(addList, tmp1)
  133. refreshIds = append(refreshIds, childEdbMapping.FromEdbInfoId)
  134. // todo 防止重复
  135. }
  136. }
  137. }
  138. }
  139. // 删除不再需要的引用关系
  140. for deleteId, _ := range deleteMap {
  141. deleteEdbInfoIds = append(deleteEdbInfoIds, deleteId)
  142. }
  143. //更新指标刷新状态为启用
  144. err = data_manage.AddOrUpdateEdbInfoRelation(objectId, objectType, addList, deleteEdbInfoIds, refreshIds, indexCodeList)
  145. if err != nil {
  146. err = fmt.Errorf("删除不再需要的引用关系失败,%s", err.Error())
  147. return
  148. }
  149. return
  150. }
  151. // SaveSandBoxEdbInfoRelation 添加/编辑 eta逻辑图指标引用
  152. func SaveSandBoxEdbInfoRelation(sandBoxId int, sandBoxContent string) (err error) {
  153. edbInfoIds, err := sandbox.GetSandBoxEdbIdsByContent(sandBoxContent)
  154. if err != nil {
  155. return
  156. }
  157. if len(edbInfoIds) == 0 {
  158. return
  159. }
  160. //更新指标刷新状态为启用
  161. err = saveEdbInfoRelation(edbInfoIds, sandBoxId, utils.EDB_RELATION_SANDBOX, 0)
  162. return
  163. }
  164. // SaveCalendarEdbInfoRelation 添加/编辑 事件日历指标引用
  165. func SaveCalendarEdbInfoRelation(chartPermissionId int, matterDate string, editMatters, removeMatters []*fe_calendar.FeCalendarMatter) (err error) {
  166. defer func() {
  167. if err != nil {
  168. err = fmt.Errorf("添加/编辑 事件日历指标引用失败,%s", err.Error())
  169. go alarm_msg.SendAlarmMsg(err.Error(), 3)
  170. }
  171. }()
  172. //整理相关的事件ID
  173. matterIds := make([]int, 0)
  174. updateMatterMap := make(map[int]*fe_calendar.FeCalendarMatter)
  175. deleteMatterMap := make(map[int]struct{})
  176. for _, matter := range removeMatters {
  177. deleteMatterMap[matter.FeCalendarMatterId] = struct{}{}
  178. matterIds = append(matterIds, matter.FeCalendarMatterId)
  179. }
  180. for _, matter := range editMatters {
  181. updateMatterMap[matter.FeCalendarMatterId] = matter
  182. matterIds = append(matterIds, matter.FeCalendarMatterId)
  183. }
  184. //删除ID,先删除
  185. deleteObjectIds := make([]int, 0)
  186. if len(matterIds) > 0 {
  187. relationList, e := data_manage.GetEdbInfoRelationAllByReferObjectIds(matterIds, utils.EDB_RELATION_CALENDAR)
  188. if e != nil {
  189. err = fmt.Errorf("查询事件日历指标引用失败,%s", e.Error())
  190. return
  191. }
  192. for _, relation := range relationList {
  193. if _, ok := deleteMatterMap[relation.ReferObjectId]; ok {
  194. deleteObjectIds = append(deleteObjectIds, relation.ReferObjectId)
  195. }
  196. if newMatter, ok := updateMatterMap[relation.ReferObjectId]; ok {
  197. if relation.EdbInfoId != newMatter.EdbInfoId {
  198. deleteObjectIds = append(deleteObjectIds, relation.ReferObjectId)
  199. }
  200. }
  201. }
  202. }
  203. if len(deleteObjectIds) > 0 {
  204. err = data_manage.DeleteEdbRelationByObjectIds(deleteObjectIds, utils.EDB_RELATION_CALENDAR)
  205. if err != nil {
  206. err = fmt.Errorf("删除事件日历指标引用失败,%s", err.Error())
  207. return
  208. }
  209. }
  210. // 获取已有事项
  211. matterOb := new(fe_calendar.FeCalendarMatter)
  212. cond := fmt.Sprintf(` AND %s = ? AND %s = ?`, fe_calendar.FeCalendarMatterCols.ChartPermissionId, fe_calendar.FeCalendarMatterCols.MatterDate)
  213. pars := make([]interface{}, 0)
  214. pars = append(pars, chartPermissionId, matterDate)
  215. order := fmt.Sprintf(`%s ASC`, fe_calendar.FeCalendarMatterCols.Sort)
  216. matters, e := matterOb.GetItemsByCondition(cond, pars, []string{}, order)
  217. if e != nil {
  218. err = fmt.Errorf("查询事件日历事项失败,%s", e.Error())
  219. return
  220. }
  221. // 循环查询matters
  222. edbInfoIds := make([]int, 0)
  223. refreshIds := make([]int, 0)
  224. indexCodeList := make([]string, 0)
  225. newMatterIds := make([]int, 0)
  226. for _, matter := range matters {
  227. newMatterIds = append(newMatterIds, matter.FeCalendarMatterId)
  228. edbInfoIds = append(edbInfoIds, matter.EdbInfoId)
  229. }
  230. // 查询指标信息
  231. edbInfoListTmp, e := data_manage.GetEdbInfoByIdList(edbInfoIds)
  232. if e != nil {
  233. err = fmt.Errorf("查询指标信息失败,%s", e.Error())
  234. return
  235. }
  236. // 过滤预测指标
  237. edbInfoList := make([]*data_manage.EdbInfo, 0)
  238. for _, v := range edbInfoListTmp {
  239. if v.EdbInfoType == 0 {
  240. edbInfoList = append(edbInfoList, v)
  241. }
  242. }
  243. // 查询计算指标信息,并且建立关联关系
  244. // 查询间接引用的指标信息
  245. calculateEdbMappingListMap, calculateEdbMappingIdsMap, e := GetEdbListByEdbInfoId(edbInfoList)
  246. if e != nil {
  247. err = fmt.Errorf("查询计算指标信息失败,%s", e.Error())
  248. return
  249. }
  250. addEdbInfoIdMap := make(map[int]*data_manage.EdbInfo)
  251. for _, edbInfo := range edbInfoList {
  252. /*if edbInfo.Source != utils.DATA_SOURCE_WIND && edbInfo.Source != utils.DATA_SOURCE_MYSTEEL_CHEMICAL {
  253. continue
  254. }*/
  255. refreshIds = append(refreshIds, edbInfo.EdbInfoId)
  256. addEdbInfoIdMap[edbInfo.EdbInfoId] = edbInfo
  257. if edbInfo.Source == utils.DATA_SOURCE_MYSTEEL_CHEMICAL {
  258. indexCodeList = append(indexCodeList, edbInfo.EdbCode)
  259. }
  260. }
  261. relationMap := make(map[int]struct{})
  262. if len(newMatterIds) > 0 {
  263. //查询已有的matters
  264. relationList, e := data_manage.GetEdbInfoRelationByReferObjectIds(newMatterIds, utils.EDB_RELATION_CALENDAR)
  265. if e != nil {
  266. err = fmt.Errorf("查询事件日历指标引用失败,%s", e.Error())
  267. return
  268. }
  269. for _, relation := range relationList {
  270. relationMap[relation.ReferObjectId] = struct{}{}
  271. }
  272. }
  273. addList := make([]*data_manage.EdbInfoRelation, 0)
  274. nowTime := time.Now()
  275. for _, matter := range matters {
  276. _, ok1 := relationMap[matter.FeCalendarMatterId]
  277. edbInfo, ok2 := addEdbInfoIdMap[matter.EdbInfoId]
  278. if !ok1 && ok2 {
  279. tmp := &data_manage.EdbInfoRelation{
  280. ReferObjectId: matter.FeCalendarMatterId,
  281. ReferObjectType: utils.EDB_RELATION_CALENDAR,
  282. ReferObjectSubType: 0,
  283. EdbInfoId: edbInfo.EdbInfoId,
  284. EdbName: edbInfo.EdbName,
  285. Source: edbInfo.Source,
  286. EdbCode: edbInfo.EdbCode,
  287. CreateTime: nowTime,
  288. ModifyTime: nowTime,
  289. RelationTime: matter.CreateTime,
  290. }
  291. tmp.RelationCode = fmt.Sprintf("%d_%d_%d_%d", tmp.EdbInfoId, tmp.ReferObjectId, tmp.ReferObjectType, tmp.ReferObjectSubType)
  292. addList = append(addList, tmp)
  293. //添加指标间接引用
  294. if edbInfo.EdbType == 2 && edbInfo.EdbInfoType == 0 {
  295. childEdbMappingIds, ok1 := calculateEdbMappingIdsMap[edbInfo.EdbInfoId]
  296. if !ok1 {
  297. continue
  298. }
  299. for _, childEdbMappingId := range childEdbMappingIds {
  300. childEdbMapping, ok2 := calculateEdbMappingListMap[childEdbMappingId]
  301. if !ok2 {
  302. continue
  303. }
  304. if childEdbMapping.FromSource == utils.DATA_SOURCE_MYSTEEL_CHEMICAL {
  305. indexCodeList = append(indexCodeList, childEdbMapping.FromEdbCode)
  306. }
  307. tmp1 := &data_manage.EdbInfoRelation{
  308. ReferObjectId: matter.FeCalendarMatterId,
  309. ReferObjectType: utils.EDB_RELATION_CALENDAR,
  310. ReferObjectSubType: 0,
  311. EdbInfoId: childEdbMapping.FromEdbInfoId,
  312. EdbName: childEdbMapping.FromEdbName,
  313. Source: childEdbMapping.FromSource,
  314. EdbCode: childEdbMapping.FromEdbCode,
  315. CreateTime: nowTime,
  316. ModifyTime: nowTime,
  317. RelationTime: nowTime,
  318. RelationType: 1,
  319. RootEdbInfoId: edbInfo.EdbInfoId,
  320. ChildEdbInfoId: childEdbMapping.EdbInfoId,
  321. RelationCode: tmp.RelationCode,
  322. }
  323. addList = append(addList, tmp1)
  324. refreshIds = append(refreshIds, childEdbMapping.FromEdbInfoId)
  325. // todo 防止重复
  326. }
  327. }
  328. }
  329. }
  330. //更新指标刷新状态为启用
  331. err = data_manage.AddOrUpdateEdbInfoRelationMulti(addList, refreshIds, indexCodeList)
  332. if err != nil {
  333. err = fmt.Errorf("添加指标引用,%s", err.Error())
  334. return
  335. }
  336. return
  337. }
  338. // GetEdbRelationList 获取指标引用列表
  339. func GetEdbRelationList(source, edbType int, classifyId, sysUserId, frequency, keyword, status string, startSize, pageSize int, sortParam, sortType string) (total int, list []*data_manage.BaseRelationEdbInfo, err error) {
  340. var pars []interface{}
  341. var condition string
  342. list = make([]*data_manage.BaseRelationEdbInfo, 0)
  343. isStop := -1
  344. if status == `暂停` {
  345. isStop = 1
  346. } else if status == "启用" {
  347. isStop = 0
  348. }
  349. switch source {
  350. case utils.DATA_SOURCE_MYSTEEL_CHEMICAL, utils.DATA_SOURCE_WIND:
  351. condition += ` AND e.source = ? `
  352. pars = append(pars, source)
  353. }
  354. if edbType == 2 { //计算指标
  355. condition += ` AND e.edb_type = ? AND e.edb_info_type = 0`
  356. pars = append(pars, edbType)
  357. }
  358. if isStop >= 0 {
  359. condition += " AND e.no_update = ? "
  360. pars = append(pars, isStop)
  361. }
  362. if classifyId != `` {
  363. classifyIdSlice := strings.Split(classifyId, ",")
  364. condition += ` AND e.classify_id IN (` + utils.GetOrmInReplace(len(classifyIdSlice)) + `)`
  365. pars = append(pars, classifyIdSlice)
  366. }
  367. if sysUserId != `` {
  368. sysUserIdSlice := strings.Split(sysUserId, ",")
  369. condition += ` AND e.sys_user_id IN (` + utils.GetOrmInReplace(len(sysUserIdSlice)) + `)`
  370. pars = append(pars, sysUserIdSlice)
  371. }
  372. if frequency != `` {
  373. frequencySlice := strings.Split(frequency, ",")
  374. condition += ` AND e.frequency IN (` + utils.GetOrmInReplace(len(frequencySlice)) + `)`
  375. pars = append(pars, frequencySlice)
  376. }
  377. if keyword != `` {
  378. keywordSlice := strings.Split(keyword, " ")
  379. if len(keywordSlice) > 0 {
  380. tmpConditionSlice := make([]string, 0)
  381. tmpConditionSlice = append(tmpConditionSlice, ` e.edb_name like ? or e.edb_code like ? `)
  382. pars = utils.GetLikeKeywordPars(pars, keyword, 2)
  383. for _, v := range keywordSlice {
  384. if v == ` ` || v == `` {
  385. continue
  386. }
  387. tmpConditionSlice = append(tmpConditionSlice, ` e.edb_name like ? or e.edb_code like ? `)
  388. pars = utils.GetLikeKeywordPars(pars, v, 2)
  389. }
  390. condition += ` AND (` + strings.Join(tmpConditionSlice, " or ") + `)`
  391. } else {
  392. condition += ` AND (e.edb_name like ? or e.edb_code like ? )`
  393. pars = utils.GetLikeKeywordPars(pars, keyword, 2)
  394. }
  395. }
  396. sortStr := ``
  397. if sortParam != `` {
  398. sortStr = fmt.Sprintf("%s %s,e.edb_info_id desc ", sortParam, sortType)
  399. }
  400. total, list, err = data_manage.GetEdbInfoRelationList(condition, pars, sortStr, startSize, pageSize)
  401. return
  402. }
  403. // 查找当前计算指标的所有溯源指标
  404. func GetEdbListByEdbInfoId(edbInfoList []*data_manage.EdbInfo) (edbMappingListMap map[int]*data_manage.EdbInfoCalculateMapping, edbInfoMappingRootIdsMap map[int][]int, err error) {
  405. if len(edbInfoList) == 0 {
  406. return
  407. }
  408. edbInfoIds := make([]int, 0)
  409. for _, v := range edbInfoList {
  410. if v.EdbType == 2 && v.EdbInfoType == 0 { //普通计算指标,排除预算指标
  411. edbInfoIds = append(edbInfoIds, v.EdbInfoId)
  412. }
  413. }
  414. if len(edbInfoIds) == 0 {
  415. return
  416. }
  417. //查询指标信息
  418. allEdbMappingMap := make(map[int][]*data_manage.EdbInfoCalculateMappingInfo, 0)
  419. allMappingList, e := data_manage.GetEdbInfoCalculateMappingListByEdbInfoIds(edbInfoIds)
  420. if e != nil {
  421. err = fmt.Errorf("GetEdbInfoCalculateMappingListByEdbInfoIds err: %s", e.Error())
  422. return
  423. }
  424. for _, v := range allMappingList {
  425. if _, ok := allEdbMappingMap[v.EdbInfoId]; !ok {
  426. allEdbMappingMap[v.EdbInfoId] = make([]*data_manage.EdbInfoCalculateMappingInfo, 0)
  427. }
  428. allEdbMappingMap[v.EdbInfoId] = append(allEdbMappingMap[v.EdbInfoId], v)
  429. }
  430. //查询指标映射
  431. //查询所有指标数据
  432. //查询这个指标相关的mapping信息放到数组里,
  433. //将得到的指标ID信息放到数组里
  434. hasFindMap := make(map[int]struct{})
  435. edbInfoIdMap := make(map[int]struct{})
  436. edbMappingList := make([]*data_manage.EdbInfoCalculateMapping, 0)
  437. edbInfoMappingRootIdsMap = make(map[int][]int, 0)
  438. edbMappingMap := make(map[int]struct{})
  439. for _, edbInfo := range edbInfoList {
  440. if edbInfo.EdbType == 2 && edbInfo.EdbInfoType == 0 {
  441. edbInfoId := edbInfo.EdbInfoId
  442. edbMappingList, err = getCalculateEdbInfoByEdbInfoId(allEdbMappingMap, edbInfoId, hasFindMap, edbInfoIdMap, edbMappingList, edbMappingMap, edbInfoMappingRootIdsMap, edbInfoId)
  443. if err != nil {
  444. err = fmt.Errorf(" GetCalculateEdbInfoByEdbInfoId err: %s", err.Error())
  445. return
  446. }
  447. }
  448. }
  449. if len(edbMappingList) == 0 {
  450. return
  451. }
  452. // 查询指标信息
  453. // 指标信息map
  454. edbInfoIdList := make([]int, 0)
  455. for k, _ := range edbInfoIdMap {
  456. edbInfoIdList = append(edbInfoIdList, k)
  457. }
  458. edbMappingListMap = make(map[int]*data_manage.EdbInfoCalculateMapping)
  459. if len(edbMappingList) > 0 {
  460. for _, v := range edbMappingList {
  461. edbMappingListMap[v.EdbInfoCalculateMappingId] = v
  462. }
  463. }
  464. return
  465. }
  466. // getCalculateEdbInfoByEdbInfoId 计算指标追溯
  467. 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) {
  468. newEdbMappingList = edbMappingList
  469. _, ok := hasFindMap[edbInfoId]
  470. if ok {
  471. return
  472. }
  473. if _, ok1 := edbInfoIdMap[edbInfoId]; !ok1 {
  474. edbInfoIdMap[edbInfoId] = struct{}{}
  475. }
  476. edbInfoMappingList := make([]*data_manage.EdbInfoCalculateMappingInfo, 0)
  477. edbInfoMappingList, ok = allEdbMappingMap[edbInfoId]
  478. if !ok {
  479. edbInfoMappingList, err = data_manage.GetEdbInfoCalculateMappingListByEdbInfoId(edbInfoId)
  480. if err != nil {
  481. err = fmt.Errorf("GetEdbInfoCalculateMappingListByEdbInfoId err: %s", err.Error())
  482. return
  483. }
  484. }
  485. hasFindMap[edbInfoId] = struct{}{}
  486. if len(edbInfoMappingList) > 0 {
  487. fromEdbInfoIdList := make([]int, 0)
  488. edbInfoMappingIdList := make([]int, 0)
  489. for _, v := range edbInfoMappingList {
  490. fromEdbInfoIdList = append(fromEdbInfoIdList, v.FromEdbInfoId)
  491. edbInfoMappingIdList = append(edbInfoMappingIdList, v.EdbInfoCalculateMappingId)
  492. if _, ok1 := edbInfoIdMap[v.FromEdbInfoId]; !ok1 {
  493. edbInfoIdMap[v.FromEdbInfoId] = struct{}{}
  494. }
  495. if _, ok2 := edbMappingMap[v.EdbInfoCalculateMappingId]; !ok2 {
  496. edbMappingMap[v.EdbInfoCalculateMappingId] = struct{}{}
  497. tmp := &data_manage.EdbInfoCalculateMapping{
  498. EdbInfoCalculateMappingId: v.EdbInfoCalculateMappingId,
  499. EdbInfoId: v.EdbInfoId,
  500. Source: v.Source,
  501. SourceName: v.SourceName,
  502. EdbCode: v.EdbCode,
  503. FromEdbInfoId: v.FromEdbInfoId,
  504. FromEdbCode: v.FromEdbCode,
  505. FromEdbName: v.FromEdbName,
  506. FromSource: v.FromSource,
  507. FromSourceName: v.FromSourceName,
  508. FromTag: v.FromTag,
  509. Sort: v.Sort,
  510. CreateTime: v.CreateTime,
  511. ModifyTime: v.ModifyTime,
  512. }
  513. newEdbMappingList = append(newEdbMappingList, tmp)
  514. }
  515. if edbInfoId != v.FromEdbInfoId && (v.FromEdbType == 2 || v.FromEdbInfoType == 1) {
  516. // 查过了就不查了
  517. if _, ok2 := hasFindMap[v.FromEdbInfoId]; !ok2 {
  518. newEdbMappingList, err = getCalculateEdbInfoByEdbInfoId(allEdbMappingMap, v.FromEdbInfoId, hasFindMap, edbInfoIdMap, newEdbMappingList, edbMappingMap, edbInfoMappingRootIdsMap, rootEdbInfoId)
  519. if err != nil {
  520. err = fmt.Errorf("traceEdbInfoByEdbInfoId err: %s", err.Error())
  521. return
  522. }
  523. }
  524. }
  525. hasFindMap[v.FromEdbInfoId] = struct{}{}
  526. }
  527. edbInfoMappingRootIdsMap[rootEdbInfoId] = append(edbInfoMappingRootIdsMap[rootEdbInfoId], edbInfoMappingIdList...)
  528. }
  529. return
  530. }
  531. // SaveExcelEdbInfoRelation 添加/编辑表格时同步修改指标引用关联记录
  532. func SaveExcelEdbInfoRelation(excelInfoId, source int, addChildExcel bool) (err error) {
  533. defer func() {
  534. if err != nil {
  535. err = fmt.Errorf("添加/编辑表格时同步修改指标引用关联记录失败,%s", err.Error())
  536. go alarm_msg.SendAlarmMsg(err.Error(), 3)
  537. }
  538. }()
  539. //查询和指标相关的时间序列表格和混合表格
  540. if !utils.InArrayByInt([]int{utils.TIME_TABLE, utils.MIXED_TABLE, utils.BALANCE_TABLE}, source) {
  541. return
  542. }
  543. if addChildExcel && source == utils.BALANCE_TABLE {
  544. //查询excel信息,
  545. excelInfoList, e := excelModel.GetChildExcelInfoByParentId(excelInfoId)
  546. if e != nil {
  547. err = fmt.Errorf("查询excel信息失败,错误:%s", e.Error())
  548. return
  549. }
  550. //查询
  551. if len(excelInfoList) > 0 {
  552. //汇总表格ID
  553. excelIds := make([]int, 0)
  554. for _, v := range excelInfoList {
  555. if v.BalanceType == 0 {
  556. excelIds = append(excelIds, v.ExcelInfoId)
  557. }
  558. }
  559. mappingList, e := excelModel.GetAllExcelEdbMappingByExcelInfoIds(excelIds)
  560. if e != nil {
  561. err = fmt.Errorf("查询和指标相关的表格失败,错误:%s", e.Error())
  562. return
  563. }
  564. //整理map
  565. mappingMap := make(map[int][]int)
  566. for _, v := range mappingList {
  567. mappingMap[v.ExcelInfoId] = append(mappingMap[v.ExcelInfoId], v.EdbInfoId)
  568. }
  569. // 添加指标引用
  570. for _, v := range excelInfoList {
  571. edbInfoIds, ok := mappingMap[v.ExcelInfoId]
  572. if !ok {
  573. continue
  574. }
  575. err = saveEdbInfoRelation(edbInfoIds, v.ExcelInfoId, utils.EDB_RELATION_TABLE, source)
  576. }
  577. //更新
  578. }
  579. }
  580. mappingList, err := excelModel.GetAllExcelEdbMappingByExcelInfoId(excelInfoId)
  581. if err != nil {
  582. err = fmt.Errorf("查询和指标相关的表格失败,错误:%s", err.Error())
  583. return
  584. }
  585. //查询指标ID
  586. edbInfoIds := make([]int, 0)
  587. for _, v := range mappingList {
  588. edbInfoIds = append(edbInfoIds, v.EdbInfoId)
  589. }
  590. //查询指标引用关联记录
  591. //更新指标刷新状态为启用
  592. if len(edbInfoIds) == 0 {
  593. return
  594. }
  595. err = saveEdbInfoRelation(edbInfoIds, excelInfoId, utils.EDB_RELATION_TABLE, source)
  596. return
  597. }
  598. // GetCalculateEdbByFromEdbInfo 找到依赖于该基础指标的所有计算指标
  599. func GetCalculateEdbByFromEdbInfo(edbInfoIds []int, calculateEdbIds []int, hasFind map[int]struct{}) (newCalculateEdbIds []int, err error) {
  600. if len(edbInfoIds) == 0 {
  601. return
  602. }
  603. newCalculateEdbIds = calculateEdbIds
  604. newEdbInfoIds := make([]int, 0)
  605. for _, v := range edbInfoIds {
  606. if _, ok := hasFind[v]; ok {
  607. continue
  608. }
  609. newEdbInfoIds = append(newEdbInfoIds, v)
  610. }
  611. if len(newEdbInfoIds) == 0 {
  612. return
  613. }
  614. var condition string
  615. var pars []interface{}
  616. // 关联指标
  617. condition += ` AND b.from_edb_info_id in (` + utils.GetOrmInReplace(len(newEdbInfoIds)) + `)`
  618. pars = append(pars, newEdbInfoIds)
  619. //获取关联图表列表
  620. list, err := data_manage.GetRelationEdbInfoListMappingByCondition(condition, pars)
  621. if err != nil && err.Error() != utils.ErrNoRow() {
  622. err = fmt.Errorf("获取关联指标信息失败,Err:%s", err.Error())
  623. return
  624. }
  625. calculateEdbIdsTmp := make([]int, 0)
  626. for _, mapping := range list {
  627. if mapping.EdbType == 2 && mapping.EdbInfoType == 0 { // 如果指标库里的计算指标,则加入,否则继续找
  628. newCalculateEdbIds = append(newCalculateEdbIds, mapping.EdbInfoId)
  629. calculateEdbIdsTmp = append(calculateEdbIdsTmp, mapping.EdbInfoId)
  630. }
  631. }
  632. for _, v := range newEdbInfoIds {
  633. hasFind[v] = struct{}{}
  634. }
  635. if len(calculateEdbIdsTmp) > 0 {
  636. newCalculateEdbIds, err = GetCalculateEdbByFromEdbInfo(calculateEdbIdsTmp, newCalculateEdbIds, hasFind)
  637. if err != nil {
  638. return
  639. }
  640. }
  641. return
  642. }