edb_info_relation.go 21 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623
  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 == 1 {
  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 == 1 {
  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, edbInfoType 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 edbInfoType == 1 { //预测指标
  341. condition += ` AND e.edb_info_type = ? `
  342. pars = append(pars, edbInfoType)
  343. } else if edbInfoType == 2 { //计算指标
  344. condition += ` AND e.edb_type = ? `
  345. pars = append(pars, edbInfoType)
  346. }
  347. if isStop >= 0 {
  348. condition += " AND e.no_update = ? "
  349. pars = append(pars, isStop)
  350. }
  351. if classifyId != `` {
  352. classifyIdSlice := strings.Split(classifyId, ",")
  353. condition += ` AND e.classify_id IN (` + utils.GetOrmInReplace(len(classifyIdSlice)) + `)`
  354. pars = append(pars, classifyIdSlice)
  355. }
  356. if sysUserId != `` {
  357. sysUserIdSlice := strings.Split(sysUserId, ",")
  358. condition += ` AND e.sys_user_id IN (` + utils.GetOrmInReplace(len(sysUserIdSlice)) + `)`
  359. pars = append(pars, sysUserIdSlice)
  360. }
  361. if frequency != `` {
  362. frequencySlice := strings.Split(frequency, ",")
  363. condition += ` AND e.frequency IN (` + utils.GetOrmInReplace(len(frequencySlice)) + `)`
  364. pars = append(pars, frequencySlice)
  365. }
  366. if keyword != `` {
  367. keywordSlice := strings.Split(keyword, " ")
  368. if len(keywordSlice) > 0 {
  369. tmpConditionSlice := make([]string, 0)
  370. tmpConditionSlice = append(tmpConditionSlice, ` e.edb_name like ? or e.edb_code like ? `)
  371. pars = utils.GetLikeKeywordPars(pars, keyword, 2)
  372. for _, v := range keywordSlice {
  373. if v == ` ` || v == `` {
  374. continue
  375. }
  376. tmpConditionSlice = append(tmpConditionSlice, ` e.edb_name like ? or e.edb_code like ? `)
  377. pars = utils.GetLikeKeywordPars(pars, v, 2)
  378. }
  379. condition += ` AND (` + strings.Join(tmpConditionSlice, " or ") + `)`
  380. } else {
  381. condition += ` AND (e.edb_name like ? or e.edb_code like ? )`
  382. pars = utils.GetLikeKeywordPars(pars, keyword, 2)
  383. }
  384. }
  385. sortStr := ``
  386. if sortParam != `` {
  387. sortStr = fmt.Sprintf("%s %s,e.edb_info_id desc ", sortParam, sortType)
  388. }
  389. total, list, err = data_manage.GetEdbInfoRelationList(condition, pars, sortStr, startSize, pageSize)
  390. return
  391. }
  392. func GetEdbListByEdbInfoId(edbInfoList []*data_manage.EdbInfo) (edbMappingListMap map[int]*data_manage.EdbInfoCalculateMapping, edbInfoMappingRootIdsMap map[int][]int, err error) {
  393. if len(edbInfoList) == 0 {
  394. return
  395. }
  396. edbInfoIds := make([]int, 0)
  397. for _, v := range edbInfoList {
  398. if v.EdbType == 2 || v.EdbInfoType == 1 {
  399. edbInfoIds = append(edbInfoIds, v.EdbInfoId)
  400. }
  401. }
  402. if len(edbInfoIds) == 0 {
  403. return
  404. }
  405. //查询指标信息
  406. allEdbMappingMap := make(map[int][]*data_manage.EdbInfoCalculateMappingInfo, 0)
  407. allMappingList, e := data_manage.GetEdbInfoCalculateMappingListByEdbInfoIds(edbInfoIds)
  408. if e != nil {
  409. err = fmt.Errorf("GetEdbInfoCalculateMappingListByEdbInfoIds err: %s", e.Error())
  410. return
  411. }
  412. for _, v := range allMappingList {
  413. if _, ok := allEdbMappingMap[v.EdbInfoId]; !ok {
  414. allEdbMappingMap[v.EdbInfoId] = make([]*data_manage.EdbInfoCalculateMappingInfo, 0)
  415. }
  416. allEdbMappingMap[v.EdbInfoId] = append(allEdbMappingMap[v.EdbInfoId], v)
  417. }
  418. //查询指标映射
  419. //查询所有指标数据
  420. //查询这个指标相关的mapping信息放到数组里,
  421. //将得到的指标ID信息放到数组里
  422. hasFindMap := make(map[int]struct{})
  423. edbInfoIdMap := make(map[int]struct{})
  424. edbMappingList := make([]*data_manage.EdbInfoCalculateMapping, 0)
  425. edbInfoMappingRootIdsMap = make(map[int][]int, 0)
  426. edbMappingMap := make(map[int]struct{})
  427. for _, edbInfo := range edbInfoList {
  428. if edbInfo.EdbType == 2 || edbInfo.EdbInfoType == 1 {
  429. edbInfoId := edbInfo.EdbInfoId
  430. edbMappingList, err = getCalculateEdbInfoByEdbInfoId(allEdbMappingMap, edbInfoId, hasFindMap, edbInfoIdMap, edbMappingList, edbMappingMap, edbInfoMappingRootIdsMap, edbInfoId)
  431. if err != nil {
  432. err = fmt.Errorf(" GetCalculateEdbInfoByEdbInfoId err: %s", err.Error())
  433. return
  434. }
  435. }
  436. }
  437. if len(edbMappingList) == 0 {
  438. return
  439. }
  440. // 查询指标信息
  441. // 指标信息map
  442. edbInfoIdList := make([]int, 0)
  443. for k, _ := range edbInfoIdMap {
  444. edbInfoIdList = append(edbInfoIdList, k)
  445. }
  446. edbMappingListMap = make(map[int]*data_manage.EdbInfoCalculateMapping)
  447. if len(edbMappingList) > 0 {
  448. for _, v := range edbMappingList {
  449. edbMappingListMap[v.EdbInfoCalculateMappingId] = v
  450. }
  451. }
  452. return
  453. }
  454. // getCalculateEdbInfoByEdbInfoId 计算指标追溯
  455. 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) {
  456. newEdbMappingList = edbMappingList
  457. _, ok := hasFindMap[edbInfoId]
  458. if ok {
  459. return
  460. }
  461. if _, ok1 := edbInfoIdMap[edbInfoId]; !ok1 {
  462. edbInfoIdMap[edbInfoId] = struct{}{}
  463. }
  464. edbInfoMappingList := make([]*data_manage.EdbInfoCalculateMappingInfo, 0)
  465. edbInfoMappingList, ok = allEdbMappingMap[edbInfoId]
  466. if !ok {
  467. edbInfoMappingList, err = data_manage.GetEdbInfoCalculateMappingListByEdbInfoId(edbInfoId)
  468. if err != nil {
  469. err = fmt.Errorf("GetEdbInfoCalculateMappingListByEdbInfoId err: %s", err.Error())
  470. return
  471. }
  472. }
  473. hasFindMap[edbInfoId] = struct{}{}
  474. if len(edbInfoMappingList) > 0 {
  475. fromEdbInfoIdList := make([]int, 0)
  476. edbInfoMappingIdList := make([]int, 0)
  477. for _, v := range edbInfoMappingList {
  478. fromEdbInfoIdList = append(fromEdbInfoIdList, v.FromEdbInfoId)
  479. edbInfoMappingIdList = append(edbInfoMappingIdList, v.EdbInfoCalculateMappingId)
  480. if _, ok1 := edbInfoIdMap[v.FromEdbInfoId]; !ok1 {
  481. edbInfoIdMap[v.FromEdbInfoId] = struct{}{}
  482. }
  483. if _, ok2 := edbMappingMap[v.EdbInfoCalculateMappingId]; !ok2 {
  484. edbMappingMap[v.EdbInfoCalculateMappingId] = struct{}{}
  485. tmp := &data_manage.EdbInfoCalculateMapping{
  486. EdbInfoCalculateMappingId: v.EdbInfoCalculateMappingId,
  487. EdbInfoId: v.EdbInfoId,
  488. Source: v.Source,
  489. SourceName: v.SourceName,
  490. EdbCode: v.EdbCode,
  491. FromEdbInfoId: v.FromEdbInfoId,
  492. FromEdbCode: v.FromEdbCode,
  493. FromEdbName: v.FromEdbName,
  494. FromSource: v.FromSource,
  495. FromSourceName: v.FromSourceName,
  496. FromTag: v.FromTag,
  497. Sort: v.Sort,
  498. CreateTime: v.CreateTime,
  499. ModifyTime: v.ModifyTime,
  500. }
  501. newEdbMappingList = append(newEdbMappingList, tmp)
  502. }
  503. if edbInfoId != v.FromEdbInfoId && (v.FromEdbType == 2 || v.FromEdbInfoType == 1) {
  504. // 查过了就不查了
  505. if _, ok2 := hasFindMap[v.FromEdbInfoId]; !ok2 {
  506. newEdbMappingList, err = getCalculateEdbInfoByEdbInfoId(allEdbMappingMap, v.FromEdbInfoId, hasFindMap, edbInfoIdMap, newEdbMappingList, edbMappingMap, edbInfoMappingRootIdsMap, rootEdbInfoId)
  507. if err != nil {
  508. err = fmt.Errorf("traceEdbInfoByEdbInfoId err: %s", err.Error())
  509. return
  510. }
  511. }
  512. }
  513. hasFindMap[v.FromEdbInfoId] = struct{}{}
  514. }
  515. edbInfoMappingRootIdsMap[rootEdbInfoId] = append(edbInfoMappingRootIdsMap[rootEdbInfoId], edbInfoMappingIdList...)
  516. }
  517. return
  518. }
  519. // SaveExcelEdbInfoRelation 添加/编辑表格时同步修改指标引用关联记录
  520. func SaveExcelEdbInfoRelation(excelInfoId, source int, addChildExcel bool) (err error) {
  521. defer func() {
  522. if err != nil {
  523. err = fmt.Errorf("添加/编辑表格时同步修改指标引用关联记录失败,%s", err.Error())
  524. go alarm_msg.SendAlarmMsg(err.Error(), 3)
  525. }
  526. }()
  527. //查询和指标相关的时间序列表格和混合表格
  528. if !utils.InArrayByInt([]int{utils.TIME_TABLE, utils.MIXED_TABLE, utils.BALANCE_TABLE}, source) {
  529. return
  530. }
  531. if addChildExcel && source == utils.BALANCE_TABLE {
  532. //查询excel信息,
  533. excelInfoList, e := excelModel.GetChildExcelInfoByParentId(excelInfoId)
  534. if e != nil {
  535. err = fmt.Errorf("查询excel信息失败,错误:%s", e.Error())
  536. return
  537. }
  538. //查询
  539. if len(excelInfoList) > 0 {
  540. //汇总表格ID
  541. excelIds := make([]int, 0)
  542. for _, v := range excelInfoList {
  543. if v.BalanceType == 0 {
  544. excelIds = append(excelIds, v.ExcelInfoId)
  545. }
  546. }
  547. mappingList, e := excelModel.GetAllExcelEdbMappingByExcelInfoIds(excelIds)
  548. if e != nil {
  549. err = fmt.Errorf("查询和指标相关的表格失败,错误:%s", e.Error())
  550. return
  551. }
  552. //整理map
  553. mappingMap := make(map[int][]int)
  554. for _, v := range mappingList {
  555. mappingMap[v.ExcelInfoId] = append(mappingMap[v.ExcelInfoId], v.EdbInfoId)
  556. }
  557. // 添加指标引用
  558. for _, v := range excelInfoList {
  559. edbInfoIds, ok := mappingMap[v.ExcelInfoId]
  560. if !ok {
  561. continue
  562. }
  563. err = saveEdbInfoRelation(edbInfoIds, v.ExcelInfoId, utils.EDB_RELATION_TABLE, source)
  564. }
  565. //更新
  566. }
  567. }
  568. mappingList, err := excelModel.GetAllExcelEdbMappingByExcelInfoId(excelInfoId)
  569. if err != nil {
  570. err = fmt.Errorf("查询和指标相关的表格失败,错误:%s", err.Error())
  571. return
  572. }
  573. //查询指标ID
  574. edbInfoIds := make([]int, 0)
  575. for _, v := range mappingList {
  576. edbInfoIds = append(edbInfoIds, v.EdbInfoId)
  577. }
  578. //查询指标引用关联记录
  579. //更新指标刷新状态为启用
  580. if len(edbInfoIds) == 0 {
  581. return
  582. }
  583. err = saveEdbInfoRelation(edbInfoIds, excelInfoId, utils.EDB_RELATION_TABLE, source)
  584. return
  585. }