edb_info_relation.go 20 KB

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