edb_info_relation.go 26 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762
  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, false)
  17. return
  18. }
  19. // saveEdbInfoRelation 添加/编辑图表指标引用关联记录
  20. func saveEdbInfoRelation(edbInfoIds []int, objectId, objectType, objectSubType int, needPredict bool) (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 || (v.EdbType == 1 && v.EdbInfoType == 1 && needPredict) {
  44. edbInfoList = append(edbInfoList, v)
  45. }
  46. }
  47. // 查询计算指标信息,并且建立关联关系
  48. // 查询间接引用的指标信息
  49. calculateEdbMappingListMap, calculateEdbMappingIdsMap, e := GetEdbListByEdbInfoId(edbInfoList, needPredict)
  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) || (edbInfo.EdbType == 1 && edbInfo.EdbInfoType == 1 && needPredict) {
  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, false)
  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, false)
  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 0: // 计算指标,不校验source
  356. default:
  357. condition += ` AND e.source = ? `
  358. pars = append(pars, source)
  359. }
  360. if edbType == 2 { //计算指标
  361. condition += ` AND e.edb_type = ? AND e.edb_info_type = 0`
  362. pars = append(pars, edbType)
  363. }
  364. switch isStop {
  365. case -1:
  366. // 供应商停用
  367. if source == utils.DATA_SOURCE_MYSTEEL_CHEMICAL {
  368. joinTableStr = ` LEFT JOIN base_from_mysteel_chemical_index z ON e.edb_code = z.index_code `
  369. addFieldStr = ` ,z.is_supplier_stop `
  370. }
  371. case 0, 1:
  372. condition += " AND e.no_update = ? "
  373. pars = append(pars, isStop)
  374. // 供应商停用
  375. if source == utils.DATA_SOURCE_MYSTEEL_CHEMICAL {
  376. condition += " AND z.is_supplier_stop = ? "
  377. pars = append(pars, 0)
  378. joinTableStr = ` LEFT JOIN base_from_mysteel_chemical_index z ON e.edb_code = z.index_code `
  379. addFieldStr = ` ,z.is_supplier_stop `
  380. }
  381. case 3:
  382. // 供应商停用
  383. if source == utils.DATA_SOURCE_MYSTEEL_CHEMICAL {
  384. condition += " AND z.is_supplier_stop = ? "
  385. pars = append(pars, 1)
  386. joinTableStr = ` LEFT JOIN base_from_mysteel_chemical_index z ON e.edb_code = z.index_code `
  387. addFieldStr = ` ,z.is_supplier_stop `
  388. }
  389. }
  390. if classifyId != `` {
  391. classifyIdSlice := strings.Split(classifyId, ",")
  392. condition += ` AND e.classify_id IN (` + utils.GetOrmInReplace(len(classifyIdSlice)) + `)`
  393. pars = append(pars, classifyIdSlice)
  394. }
  395. if sysUserId != `` {
  396. sysUserIdSlice := strings.Split(sysUserId, ",")
  397. condition += ` AND e.sys_user_id IN (` + utils.GetOrmInReplace(len(sysUserIdSlice)) + `)`
  398. pars = append(pars, sysUserIdSlice)
  399. }
  400. if frequency != `` {
  401. frequencySlice := strings.Split(frequency, ",")
  402. condition += ` AND e.frequency IN (` + utils.GetOrmInReplace(len(frequencySlice)) + `)`
  403. pars = append(pars, frequencySlice)
  404. }
  405. if keyword != `` {
  406. keywordSlice := strings.Split(keyword, " ")
  407. if len(keywordSlice) > 0 {
  408. tmpConditionSlice := make([]string, 0)
  409. tmpConditionSlice = append(tmpConditionSlice, ` e.edb_name like ? or e.edb_code like ? `)
  410. pars = utils.GetLikeKeywordPars(pars, keyword, 2)
  411. for _, v := range keywordSlice {
  412. if v == ` ` || v == `` {
  413. continue
  414. }
  415. tmpConditionSlice = append(tmpConditionSlice, ` e.edb_name like ? or e.edb_code like ? `)
  416. pars = utils.GetLikeKeywordPars(pars, v, 2)
  417. }
  418. condition += ` AND (` + strings.Join(tmpConditionSlice, " or ") + `)`
  419. } else {
  420. condition += ` AND (e.edb_name like ? or e.edb_code like ? )`
  421. pars = utils.GetLikeKeywordPars(pars, keyword, 2)
  422. }
  423. }
  424. sortStr := ``
  425. if sortParam != `` {
  426. sortStr = fmt.Sprintf("%s %s,e.edb_info_id desc ", sortParam, sortType)
  427. }
  428. total, list, err = data_manage.GetEdbInfoRelationList(condition, pars, addFieldStr, joinTableStr, sortStr, startSize, pageSize)
  429. return
  430. }
  431. // 查找当前计算指标的所有溯源指标
  432. func GetEdbListByEdbInfoId(edbInfoList []*data_manage.EdbInfo, needPredict bool) (edbMappingListMap map[int]*data_manage.EdbInfoCalculateMapping, edbInfoMappingRootIdsMap map[int][]int, err error) {
  433. if len(edbInfoList) == 0 {
  434. return
  435. }
  436. edbInfoIds := make([]int, 0)
  437. for _, v := range edbInfoList {
  438. if (v.EdbType == 2 && v.EdbInfoType == 0) || (v.EdbType == 1 && v.EdbInfoType == 1 && needPredict) { //普通计算指标,或者是基础预测指标
  439. edbInfoIds = append(edbInfoIds, v.EdbInfoId)
  440. }
  441. }
  442. if len(edbInfoIds) == 0 {
  443. return
  444. }
  445. //查询指标信息
  446. allEdbMappingMap := make(map[int][]*data_manage.EdbInfoCalculateMappingInfo, 0)
  447. allMappingList, e := data_manage.GetEdbInfoCalculateMappingListByEdbInfoIds(edbInfoIds)
  448. if e != nil {
  449. err = fmt.Errorf("GetEdbInfoCalculateMappingListByEdbInfoIds err: %s", e.Error())
  450. return
  451. }
  452. for _, v := range allMappingList {
  453. if _, ok := allEdbMappingMap[v.EdbInfoId]; !ok {
  454. allEdbMappingMap[v.EdbInfoId] = make([]*data_manage.EdbInfoCalculateMappingInfo, 0)
  455. }
  456. allEdbMappingMap[v.EdbInfoId] = append(allEdbMappingMap[v.EdbInfoId], v)
  457. }
  458. //查询指标映射
  459. //查询所有指标数据
  460. //查询这个指标相关的mapping信息放到数组里,
  461. //将得到的指标ID信息放到数组里
  462. hasFindMap := make(map[int]struct{})
  463. edbInfoIdMap := make(map[int]struct{})
  464. edbMappingList := make([]*data_manage.EdbInfoCalculateMapping, 0)
  465. edbInfoMappingRootIdsMap = make(map[int][]int, 0)
  466. edbMappingMap := make(map[int]struct{})
  467. for _, edbInfo := range edbInfoList {
  468. if (edbInfo.EdbType == 2 && edbInfo.EdbInfoType == 0) || (edbInfo.EdbType == 1 && edbInfo.EdbInfoType == 1 && needPredict) {
  469. edbInfoId := edbInfo.EdbInfoId
  470. edbMappingList, err = getCalculateEdbInfoByEdbInfoId(allEdbMappingMap, edbInfoId, hasFindMap, edbInfoIdMap, edbMappingList, edbMappingMap, edbInfoMappingRootIdsMap, edbInfoId)
  471. if err != nil {
  472. err = fmt.Errorf(" GetCalculateEdbInfoByEdbInfoId err: %s", err.Error())
  473. return
  474. }
  475. }
  476. }
  477. if len(edbMappingList) == 0 {
  478. return
  479. }
  480. // 查询指标信息
  481. // 指标信息map
  482. edbInfoIdList := make([]int, 0)
  483. for k, _ := range edbInfoIdMap {
  484. edbInfoIdList = append(edbInfoIdList, k)
  485. }
  486. edbMappingListMap = make(map[int]*data_manage.EdbInfoCalculateMapping)
  487. if len(edbMappingList) > 0 {
  488. for _, v := range edbMappingList {
  489. edbMappingListMap[v.EdbInfoCalculateMappingId] = v
  490. }
  491. }
  492. return
  493. }
  494. // getCalculateEdbInfoByEdbInfoId 计算指标追溯
  495. 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) {
  496. newEdbMappingList = edbMappingList
  497. _, ok := hasFindMap[edbInfoId]
  498. if ok {
  499. return
  500. }
  501. if _, ok1 := edbInfoIdMap[edbInfoId]; !ok1 {
  502. edbInfoIdMap[edbInfoId] = struct{}{}
  503. }
  504. edbInfoMappingList := make([]*data_manage.EdbInfoCalculateMappingInfo, 0)
  505. edbInfoMappingList, ok = allEdbMappingMap[edbInfoId]
  506. if !ok {
  507. edbInfoMappingList, err = data_manage.GetEdbInfoCalculateMappingListByEdbInfoId(edbInfoId)
  508. if err != nil {
  509. err = fmt.Errorf("GetEdbInfoCalculateMappingListByEdbInfoId err: %s", err.Error())
  510. return
  511. }
  512. }
  513. hasFindMap[edbInfoId] = struct{}{}
  514. if len(edbInfoMappingList) > 0 {
  515. fromEdbInfoIdList := make([]int, 0)
  516. edbInfoMappingIdList := make([]int, 0)
  517. for _, v := range edbInfoMappingList {
  518. fromEdbInfoIdList = append(fromEdbInfoIdList, v.FromEdbInfoId)
  519. edbInfoMappingIdList = append(edbInfoMappingIdList, v.EdbInfoCalculateMappingId)
  520. if _, ok1 := edbInfoIdMap[v.FromEdbInfoId]; !ok1 {
  521. edbInfoIdMap[v.FromEdbInfoId] = struct{}{}
  522. }
  523. if _, ok2 := edbMappingMap[v.EdbInfoCalculateMappingId]; !ok2 {
  524. edbMappingMap[v.EdbInfoCalculateMappingId] = struct{}{}
  525. tmp := &data_manage.EdbInfoCalculateMapping{
  526. EdbInfoCalculateMappingId: v.EdbInfoCalculateMappingId,
  527. EdbInfoId: v.EdbInfoId,
  528. Source: v.Source,
  529. SourceName: v.SourceName,
  530. EdbCode: v.EdbCode,
  531. FromEdbInfoId: v.FromEdbInfoId,
  532. FromEdbCode: v.FromEdbCode,
  533. FromEdbName: v.FromEdbName,
  534. FromSource: v.FromSource,
  535. FromSourceName: v.FromSourceName,
  536. FromTag: v.FromTag,
  537. Sort: v.Sort,
  538. CreateTime: v.CreateTime,
  539. ModifyTime: v.ModifyTime,
  540. }
  541. newEdbMappingList = append(newEdbMappingList, tmp)
  542. }
  543. if edbInfoId != v.FromEdbInfoId && (v.FromEdbType == 2 || v.FromEdbInfoType == 1) {
  544. // 查过了就不查了
  545. if _, ok2 := hasFindMap[v.FromEdbInfoId]; !ok2 {
  546. newEdbMappingList, err = getCalculateEdbInfoByEdbInfoId(allEdbMappingMap, v.FromEdbInfoId, hasFindMap, edbInfoIdMap, newEdbMappingList, edbMappingMap, edbInfoMappingRootIdsMap, rootEdbInfoId)
  547. if err != nil {
  548. err = fmt.Errorf("traceEdbInfoByEdbInfoId err: %s", err.Error())
  549. return
  550. }
  551. }
  552. }
  553. hasFindMap[v.FromEdbInfoId] = struct{}{}
  554. }
  555. edbInfoMappingRootIdsMap[rootEdbInfoId] = append(edbInfoMappingRootIdsMap[rootEdbInfoId], edbInfoMappingIdList...)
  556. }
  557. return
  558. }
  559. // SaveExcelEdbInfoRelation 添加/编辑表格时同步修改指标引用关联记录
  560. func SaveExcelEdbInfoRelation(excelInfoId, source int, addChildExcel bool) (err error) {
  561. defer func() {
  562. if err != nil {
  563. err = fmt.Errorf("添加/编辑表格时同步修改指标引用关联记录失败,%s", err.Error())
  564. go alarm_msg.SendAlarmMsg(err.Error(), 3)
  565. }
  566. }()
  567. //查询和指标相关的时间序列表格和混合表格
  568. if !utils.InArrayByInt([]int{utils.TIME_TABLE, utils.MIXED_TABLE, utils.BALANCE_TABLE}, source) {
  569. return
  570. }
  571. if addChildExcel && source == utils.BALANCE_TABLE {
  572. //查询excel信息,
  573. excelInfoList, e := excelModel.GetChildExcelInfoByParentId(excelInfoId)
  574. if e != nil {
  575. err = fmt.Errorf("查询excel信息失败,错误:%s", e.Error())
  576. return
  577. }
  578. //查询
  579. if len(excelInfoList) > 0 {
  580. //汇总表格ID
  581. excelIds := make([]int, 0)
  582. for _, v := range excelInfoList {
  583. if v.BalanceType == 0 {
  584. excelIds = append(excelIds, v.ExcelInfoId)
  585. }
  586. }
  587. mappingList, e := excelModel.GetAllExcelEdbMappingByExcelInfoIds(excelIds)
  588. if e != nil {
  589. err = fmt.Errorf("查询和指标相关的表格失败,错误:%s", e.Error())
  590. return
  591. }
  592. //整理map
  593. mappingMap := make(map[int][]int)
  594. for _, v := range mappingList {
  595. mappingMap[v.ExcelInfoId] = append(mappingMap[v.ExcelInfoId], v.EdbInfoId)
  596. }
  597. // 添加指标引用
  598. for _, v := range excelInfoList {
  599. edbInfoIds, ok := mappingMap[v.ExcelInfoId]
  600. if !ok {
  601. continue
  602. }
  603. err = saveEdbInfoRelation(edbInfoIds, v.ExcelInfoId, utils.EDB_RELATION_TABLE, source, false)
  604. }
  605. //更新
  606. }
  607. }
  608. mappingList, err := excelModel.GetAllExcelEdbMappingByExcelInfoId(excelInfoId)
  609. if err != nil {
  610. err = fmt.Errorf("查询和指标相关的表格失败,错误:%s", err.Error())
  611. return
  612. }
  613. //查询指标ID
  614. edbInfoIds := make([]int, 0)
  615. for _, v := range mappingList {
  616. edbInfoIds = append(edbInfoIds, v.EdbInfoId)
  617. }
  618. //查询指标引用关联记录
  619. //更新指标刷新状态为启用
  620. if len(edbInfoIds) == 0 {
  621. return
  622. }
  623. err = saveEdbInfoRelation(edbInfoIds, excelInfoId, utils.EDB_RELATION_TABLE, source, false)
  624. return
  625. }
  626. // SavePredictEdbInfoRelation 添加/编辑预测指标引用关联记录
  627. func SavePredictEdbInfoRelation(edbInfoIds []int, edbInfoId int) (err error) {
  628. //更新指标刷新状态为启用
  629. err = saveEdbInfoRelation(edbInfoIds, edbInfoId, utils.EDB_RELATION_PREDICT_EDB, 1, true)
  630. return
  631. }
  632. // GetCalculateEdbByFromEdbInfo 找到依赖于该基础指标的所有计算指标
  633. func GetCalculateEdbByFromEdbInfo(edbInfoIds []int, calculateEdbIds []int, hasFind map[int]struct{}) (newCalculateEdbIds []int, err error) {
  634. if len(edbInfoIds) == 0 {
  635. return
  636. }
  637. newCalculateEdbIds = calculateEdbIds
  638. newEdbInfoIds := make([]int, 0)
  639. for _, v := range edbInfoIds {
  640. if _, ok := hasFind[v]; ok {
  641. continue
  642. }
  643. newEdbInfoIds = append(newEdbInfoIds, v)
  644. }
  645. if len(newEdbInfoIds) == 0 {
  646. return
  647. }
  648. var condition string
  649. var pars []interface{}
  650. // 关联指标
  651. condition += ` AND b.from_edb_info_id in (` + utils.GetOrmInReplace(len(newEdbInfoIds)) + `)`
  652. pars = append(pars, newEdbInfoIds)
  653. //获取关联图表列表
  654. list, err := data_manage.GetRelationEdbInfoListMappingByCondition(condition, pars)
  655. if err != nil && err.Error() != utils.ErrNoRow() {
  656. err = fmt.Errorf("获取关联指标信息失败,Err:%s", err.Error())
  657. return
  658. }
  659. calculateEdbIdsTmp := make([]int, 0)
  660. for _, mapping := range list {
  661. if mapping.EdbType == 2 && mapping.EdbInfoType == 0 { // 如果指标库里的计算指标,则加入,否则继续找
  662. newCalculateEdbIds = append(newCalculateEdbIds, mapping.EdbInfoId)
  663. calculateEdbIdsTmp = append(calculateEdbIdsTmp, mapping.EdbInfoId)
  664. }
  665. }
  666. for _, v := range newEdbInfoIds {
  667. hasFind[v] = struct{}{}
  668. }
  669. if len(calculateEdbIdsTmp) > 0 {
  670. newCalculateEdbIds, err = GetCalculateEdbByFromEdbInfo(calculateEdbIdsTmp, newCalculateEdbIds, hasFind)
  671. if err != nil {
  672. return
  673. }
  674. }
  675. return
  676. }
  677. // 查询两个指标是否存在循环引用关系
  678. func CheckTwoEdbInfoRelation(edbInfoA, edbInfoB *data_manage.EdbInfo) (hasRelation bool, err error) {
  679. //查询指标信息
  680. allEdbMappingMap := make(map[int][]*data_manage.EdbInfoCalculateMappingInfo, 0)
  681. allMappingList, e := data_manage.GetEdbInfoCalculateMappingListByEdbInfoId(edbInfoA.EdbInfoId)
  682. if e != nil {
  683. err = fmt.Errorf("GetEdbInfoCalculateMappingListByEdbInfoIds err: %s", e.Error())
  684. return
  685. }
  686. for _, v := range allMappingList {
  687. if v.EdbInfoId == edbInfoB.EdbInfoId {
  688. hasRelation = true
  689. return
  690. }
  691. if _, ok := allEdbMappingMap[v.EdbInfoId]; !ok {
  692. allEdbMappingMap[v.EdbInfoId] = make([]*data_manage.EdbInfoCalculateMappingInfo, 0)
  693. }
  694. allEdbMappingMap[v.EdbInfoId] = append(allEdbMappingMap[v.EdbInfoId], v)
  695. }
  696. //查询指标映射
  697. //查询所有指标数据
  698. //查询这个指标相关的mapping信息放到数组里,
  699. //将得到的指标ID信息放到数组里
  700. hasFindMap := make(map[int]struct{})
  701. edbInfoIdMap := make(map[int]struct{})
  702. edbMappingList := make([]*data_manage.EdbInfoCalculateMapping, 0)
  703. edbInfoMappingRootIdsMap := make(map[int][]int, 0)
  704. edbMappingMap := make(map[int]struct{})
  705. if edbInfoA.EdbType == 2 {
  706. edbInfoId := edbInfoA.EdbInfoId
  707. edbMappingList, err = getCalculateEdbInfoByEdbInfoId(allEdbMappingMap, edbInfoId, hasFindMap, edbInfoIdMap, edbMappingList, edbMappingMap, edbInfoMappingRootIdsMap, edbInfoId)
  708. if err != nil {
  709. err = fmt.Errorf(" GetCalculateEdbInfoByEdbInfoId err: %s", err.Error())
  710. return
  711. }
  712. // 判断其中是否包含指标B
  713. if _, ok := edbInfoIdMap[edbInfoB.EdbInfoId]; ok { // 如果包含,则说明存在循环引用关系
  714. hasRelation = true
  715. return
  716. }
  717. }
  718. return
  719. }