edb_info_relation.go 24 KB

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