edb_info_relation.go 20 KB

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