edb_info_relation.go 23 KB

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