edb_info_relation.go 21 KB

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