edb_info_relation.go 22 KB

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