edb_info_relation.go 27 KB

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