edb_info_replace.go 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484
  1. package services
  2. import (
  3. "encoding/json"
  4. "errors"
  5. "eta_gn/eta_api/models/data_manage"
  6. excelModel "eta_gn/eta_api/models/data_manage/excel"
  7. "eta_gn/eta_api/models/data_manage/excel/request"
  8. "eta_gn/eta_api/services/alarm_msg"
  9. "eta_gn/eta_api/services/data"
  10. "eta_gn/eta_api/services/sandbox"
  11. "eta_gn/eta_api/utils"
  12. "fmt"
  13. "strconv"
  14. "time"
  15. )
  16. func DealReplaceEdbCache() {
  17. var err error
  18. for {
  19. utils.Rc.Brpop(utils.CACHE_KEY_REPLACE_EDB, func(b []byte) {
  20. defer func() {
  21. if err != nil {
  22. utils.FileLog.Info("DealReplaceEdbCache err:" + err.Error())
  23. go alarm_msg.SendAlarmMsg("替换表格中的指标失败提醒,errmsg:"+err.Error(), 3)
  24. }
  25. }()
  26. record := new(data_manage.ReplaceEdbInfoItem)
  27. if err = json.Unmarshal(b, &record); err != nil {
  28. fmt.Println("json unmarshal wrong!")
  29. return
  30. }
  31. oldEdbInfo := record.OldEdbInfo
  32. newEdbInfo := record.NewEdbInfo
  33. utils.FileLog.Info(fmt.Sprintf("指标开始替换 DealReplaceEdbCache: 旧指标ID:%d,新指标ID:%d", oldEdbInfo.EdbInfoId, newEdbInfo.EdbInfoId))
  34. deleteCache := true
  35. setNxKey := fmt.Sprintf("EDB_INFO_REPLACE:%d-%d", oldEdbInfo.EdbInfoId, newEdbInfo.EdbInfoId)
  36. defer func() {
  37. if deleteCache {
  38. utils.Rc.Delete(setNxKey)
  39. }
  40. }()
  41. if !utils.Rc.SetNX(setNxKey, 1, 30*time.Minute) {
  42. deleteCache = false
  43. err = fmt.Errorf("替换表格中的指标失败旧指标:%d为新指标%d:正在处理中", oldEdbInfo.EdbInfoId, newEdbInfo.EdbInfoId)
  44. return
  45. }
  46. _, err = data_manage.ReplaceMultipleGraphConfigChartEdb(oldEdbInfo, newEdbInfo)
  47. if err != nil {
  48. err = fmt.Errorf("替换相关性图表配置失败,errmsg:%s", err.Error())
  49. return
  50. }
  51. _, err = data_manage.ReplaceEdbInfoInLineEquationMultipleGraphConfig(oldEdbInfo, newEdbInfo)
  52. if err != nil {
  53. err = fmt.Errorf("替换拟合方程指标失败,errmsg:%s", err.Error())
  54. return
  55. }
  56. err = ReplaceEdbInExcel(oldEdbInfo, newEdbInfo)
  57. if err != nil {
  58. err = fmt.Errorf("替换表格中的指标失败,errmsg:%s", err.Error())
  59. return
  60. }
  61. err = sandbox.ReplaceEdbInSandbox(oldEdbInfo.EdbInfoId, newEdbInfo.EdbInfoId)
  62. if err != nil {
  63. err = fmt.Errorf("替换逻辑图中的指标失败,errmsg:%s", err.Error())
  64. return
  65. }
  66. ReplaceEdbInRelation(oldEdbInfo, newEdbInfo)
  67. })
  68. }
  69. }
  70. func ReplaceEdbInExcel(oldEdbInfo, newEdbInfo *data_manage.EdbInfo) (err error) {
  71. defer func() {
  72. if err != nil {
  73. go alarm_msg.SendAlarmMsg("替换表格中的指标失败提醒,errmsg:"+err.Error(), 3)
  74. }
  75. }()
  76. mappingList, err := excelModel.GetExcelEdbMappingByEdbInfoIdAndSource(oldEdbInfo.EdbInfoId, []int{utils.TIME_TABLE, utils.MIXED_TABLE})
  77. if err != nil {
  78. err = fmt.Errorf("查询和指标相关的表格失败,错误:%s", err.Error())
  79. return
  80. }
  81. updateList := make([]*excelModel.ExcelInfo, 0)
  82. for _, excelMapping := range mappingList {
  83. excelInfo, tmpErr := excelModel.GetExcelInfoById(excelMapping.ExcelInfoId)
  84. if tmpErr != nil {
  85. err = fmt.Errorf("查询和指标相关的混合表格失败,错误:%s", tmpErr.Error())
  86. return
  87. }
  88. key := utils.HZ_CHART_LIB_EXCEL_TABLE_DETAIL + ":" + excelInfo.UniqueCode
  89. if utils.Re == nil {
  90. _ = utils.Rc.Delete(key)
  91. }
  92. switch excelMapping.Source {
  93. case utils.TIME_TABLE: // 时间序列表格
  94. newExcelInfo, e := replaceEdbInTimeExcel(oldEdbInfo, newEdbInfo, excelInfo)
  95. if e != nil {
  96. err = fmt.Errorf("替换余额表格中的指标失败,错误:%s", e.Error())
  97. return
  98. }
  99. updateList = append(updateList, newExcelInfo)
  100. case utils.MIXED_TABLE, utils.BALANCE_TABLE:
  101. newExcelInfo, e := replaceEdbInBalanceExcel(oldEdbInfo, newEdbInfo, excelInfo)
  102. if e != nil {
  103. err = fmt.Errorf("替换余额表格中的指标失败,错误:%s", e.Error())
  104. return
  105. }
  106. updateList = append(updateList, newExcelInfo)
  107. default:
  108. }
  109. }
  110. err = excelModel.ReplaceEdbInExcel(oldEdbInfo.EdbInfoId, newEdbInfo.EdbInfoId, updateList)
  111. if err != nil {
  112. err = fmt.Errorf("替换表格中的指标失败,错误:%s", err.Error())
  113. return
  114. }
  115. return
  116. }
  117. func replaceEdbInBalanceExcel(oldEdbInfo, newEdbInfo *data_manage.EdbInfo, excelInfo *excelModel.ExcelInfo) (newExcelInfo *excelModel.ExcelInfo, err error) {
  118. newExcelInfo = excelInfo
  119. var mixedTableReq request.MixedTableReq
  120. err = json.Unmarshal([]byte(excelInfo.Content), &mixedTableReq)
  121. if err != nil {
  122. err = fmt.Errorf("表格json转结构体失败,Err:" + err.Error())
  123. return
  124. }
  125. configList := mixedTableReq.Data
  126. for ck, rowList := range configList {
  127. for rk, cell := range rowList {
  128. switch cell.DataType {
  129. case request.EdbDT: // 指标信息
  130. if cell.EdbInfoId == oldEdbInfo.EdbInfoId {
  131. configList[ck][rk].EdbInfoId = newEdbInfo.EdbInfoId
  132. }
  133. case request.InsertDataDT, request.PopInsertDataDT: // 插值、弹框插值
  134. if cell.EdbInfoId == oldEdbInfo.EdbInfoId {
  135. configList[ck][rk].EdbInfoId = newEdbInfo.EdbInfoId
  136. }
  137. case request.InsertEdbCalculateDataDT: // 插入指标计算公式生成的值
  138. var config request.CalculateConf
  139. err = json.Unmarshal([]byte(cell.Value), &config)
  140. if err != nil {
  141. return
  142. }
  143. if cell.EdbInfoId == oldEdbInfo.EdbInfoId {
  144. configList[ck][rk].EdbInfoId = newEdbInfo.EdbInfoId
  145. }
  146. if config.EdbInfoId == oldEdbInfo.EdbInfoId {
  147. config.EdbInfoId = newEdbInfo.EdbInfoId
  148. var configStr []byte
  149. configStr, err = json.Marshal(config)
  150. if err != nil {
  151. return
  152. }
  153. configList[ck][rk].Value = string(configStr)
  154. }
  155. case request.DateDT: // 日期类型
  156. if cell.DataTimeType == request.EdbDateDT {
  157. var config request.EdbDateConf
  158. err = json.Unmarshal([]byte(cell.Value), &config)
  159. if err != nil {
  160. return
  161. }
  162. if config.EdbInfoId == oldEdbInfo.EdbInfoId {
  163. config.EdbInfoId = newEdbInfo.EdbInfoId
  164. var configStr []byte
  165. configStr, err = json.Marshal(config)
  166. if err != nil {
  167. return
  168. }
  169. configList[ck][rk].Value = string(configStr)
  170. }
  171. }
  172. }
  173. }
  174. }
  175. mixedTableReq.Data = configList
  176. var newContentByte []byte
  177. newContentByte, err = json.Marshal(mixedTableReq)
  178. if err != nil {
  179. return
  180. }
  181. excelInfo.Content = string(newContentByte)
  182. newExcelInfo = excelInfo
  183. return
  184. }
  185. func replaceEdbInTimeExcel(oldEdbInfo, newEdbInfo *data_manage.EdbInfo, excelInfo *excelModel.ExcelInfo) (newExcelInfo *excelModel.ExcelInfo, err error) {
  186. newExcelInfo = excelInfo
  187. var tableDataConfig request.TimeTableDataConfig
  188. err = json.Unmarshal([]byte(excelInfo.Content), &tableDataConfig)
  189. if err != nil {
  190. err = errors.New("表格json转结构体失败,Err:" + err.Error())
  191. return
  192. }
  193. if len(tableDataConfig.EdbInfoIdList) <= 0 {
  194. return
  195. }
  196. if tableDataConfig.Num <= 0 {
  197. return
  198. }
  199. for k, id := range tableDataConfig.EdbInfoIdList {
  200. if id == oldEdbInfo.EdbInfoId {
  201. tableDataConfig.EdbInfoIdList[k] = newEdbInfo.EdbInfoId
  202. }
  203. }
  204. for k, tableEdbInfo := range tableDataConfig.TableEdbInfoList {
  205. if tableEdbInfo.EdbInfoId == oldEdbInfo.EdbInfoId {
  206. tableDataConfig.TableEdbInfoList[k].EdbInfoId = newEdbInfo.EdbInfoId
  207. }
  208. }
  209. var newContentByte []byte
  210. newContentByte, err = json.Marshal(tableDataConfig)
  211. if err != nil {
  212. return
  213. }
  214. excelInfo.Content = string(newContentByte)
  215. newExcelInfo = excelInfo
  216. return
  217. }
  218. func ReplaceEdbInRelation(oldEdbInfo, newEdbInfo *data_manage.EdbInfo) {
  219. var err error
  220. var logMsg string
  221. var replaceTotal int
  222. defer func() {
  223. if err != nil {
  224. msg := fmt.Sprintf(" 替换指标引用表中的指标,并修改引用时间 replaceEdbInRelation err: %v", err)
  225. utils.FileLog.Info(msg)
  226. fmt.Println(msg)
  227. go alarm_msg.SendAlarmMsg(msg, 3)
  228. }
  229. if logMsg != `` {
  230. utils.FileLog.Info(fmt.Sprintf("替换指标引用表中的指标记录 替换总数%d,旧的指标id:%d,新的指标id:%d;涉及到的引用id:%s", replaceTotal, oldEdbInfo.EdbInfoId, newEdbInfo.EdbInfoId, logMsg))
  231. }
  232. }()
  233. calculateEdbMappingListMap := make(map[int]*data_manage.EdbInfoCalculateMapping)
  234. calculateEdbMappingIdsMap := make(map[int][]int)
  235. childEdbMappingIds := make([]int, 0)
  236. pageSize := 500
  237. if newEdbInfo.EdbType == 2 {
  238. edbInfoList := make([]*data_manage.EdbInfo, 0)
  239. edbInfoList = append(edbInfoList, newEdbInfo)
  240. calculateEdbMappingListMap, calculateEdbMappingIdsMap, err = data.GetEdbListByEdbInfoId(edbInfoList)
  241. if err != nil {
  242. err = fmt.Errorf("查询指标关联指标列表失败 Err:%s", err)
  243. return
  244. }
  245. var ok bool
  246. childEdbMappingIds, ok = calculateEdbMappingIdsMap[newEdbInfo.EdbInfoId]
  247. if !ok {
  248. err = fmt.Errorf("查询%d指标关联指标列表为空", newEdbInfo.EdbInfoId)
  249. return
  250. }
  251. }
  252. total, err := data_manage.GetReplaceEdbInfoRelationTotal(oldEdbInfo.EdbInfoId)
  253. if err != nil {
  254. err = fmt.Errorf("查询引用表中关联的指标总数失败 err: %v", err)
  255. return
  256. }
  257. totalPage := 0
  258. if total > 0 {
  259. totalPage = (total + pageSize - 1) / pageSize // 使用整数除法,并添加一页以防有余数
  260. for i := 0; i < totalPage; i += 1 {
  261. startSize := i * pageSize
  262. list, e := data_manage.GetReplaceEdbInfoRelationList(oldEdbInfo.EdbInfoId, startSize, pageSize)
  263. if e != nil {
  264. err = fmt.Errorf("查询图表关联指标列表失败 Err:%s", e)
  265. return
  266. }
  267. if len(list) == 0 {
  268. break
  269. }
  270. replaceTotal1, logMsg1, e := replaceEdbInRelation(oldEdbInfo, newEdbInfo, list, childEdbMappingIds, calculateEdbMappingListMap)
  271. if e != nil {
  272. err = e
  273. return
  274. }
  275. replaceTotal += replaceTotal1
  276. logMsg += logMsg1
  277. }
  278. }
  279. total, err = data_manage.GetReplaceChildEdbInfoRelationTotal(oldEdbInfo.EdbInfoId)
  280. if err != nil {
  281. if utils.IsErrNoRow(err) {
  282. err = nil
  283. } else {
  284. err = fmt.Errorf("查询引用表中关联的指标总数失败 err: %v", err)
  285. return
  286. }
  287. }
  288. if total > 0 {
  289. totalPage = (total + pageSize - 1) / pageSize // 使用整数除法,并添加一页以防有余数
  290. for i := 0; i < totalPage; i += 1 {
  291. startSize := i * pageSize
  292. tmpList, e := data_manage.GetReplaceChildEdbInfoRelationList(oldEdbInfo.EdbInfoId, startSize, pageSize)
  293. if e != nil {
  294. err = fmt.Errorf("查询图表关联指标列表失败 Err:%s", e)
  295. return
  296. }
  297. relationIds := make([]int, 0)
  298. for _, v := range tmpList {
  299. relationIds = append(relationIds, v.ParentRelationId)
  300. }
  301. if len(relationIds) > 0 {
  302. list, e := data_manage.GetEdbInfoRelationByRelationIds(relationIds)
  303. if e != nil {
  304. err = fmt.Errorf("查询图表关联指标列表失败 Err:%s", e)
  305. return
  306. }
  307. edbInfoListMap := make(map[int]struct{})
  308. edbInfoIds := make([]int, 0)
  309. for _, v := range list {
  310. if _, ok := edbInfoListMap[v.EdbInfoId]; !ok {
  311. edbInfoListMap[v.EdbInfoId] = struct{}{}
  312. edbInfoIds = append(edbInfoIds, v.EdbInfoId)
  313. }
  314. }
  315. edbInfoList := make([]*data_manage.EdbInfo, 0)
  316. if len(edbInfoIds) > 0 {
  317. edbInfoList, err = data_manage.GetEdbInfoByIdList(edbInfoIds)
  318. if err != nil {
  319. err = fmt.Errorf("查询指标信息失败 Err:%s", err)
  320. return
  321. }
  322. }
  323. calculateEdbMappingListMap, calculateEdbMappingIdsMap, err = data.GetEdbListByEdbInfoId(edbInfoList)
  324. if err != nil {
  325. err = fmt.Errorf("查询指标关联指标列表失败 Err:%s", err)
  326. return
  327. }
  328. replaceTotal1, logMsg1, e := UpdateSecondEdbInRelation(list, calculateEdbMappingListMap, calculateEdbMappingIdsMap, edbInfoList)
  329. if e != nil {
  330. err = e
  331. return
  332. }
  333. replaceTotal += replaceTotal1
  334. logMsg += logMsg1
  335. }
  336. }
  337. }
  338. return
  339. }
  340. func replaceEdbInRelation(oldEdbInfo, newEdbInfo *data_manage.EdbInfo, list []*data_manage.EdbInfoRelation, childEdbMappingIds []int, calculateEdbMappingListMap map[int]*data_manage.EdbInfoCalculateMapping) (replaceTotal int, logMsg string, err error) {
  341. replaceEdbIds := make([]int, 0)
  342. indexCodeList := make([]string, 0)
  343. addList := make([]*data_manage.EdbInfoRelation, 0)
  344. refreshIds := make([]int, 0)
  345. nowTime := time.Now()
  346. for _, v := range list {
  347. replaceEdbIds = append(replaceEdbIds, v.EdbInfoRelationId)
  348. if newEdbInfo.EdbType == 2 {
  349. for _, childEdbMappingId := range childEdbMappingIds {
  350. childEdbMapping, ok2 := calculateEdbMappingListMap[childEdbMappingId]
  351. if !ok2 {
  352. continue
  353. }
  354. if childEdbMapping.FromSource == utils.DATA_SOURCE_MYSTEEL_CHEMICAL {
  355. indexCodeList = append(indexCodeList, childEdbMapping.FromEdbCode)
  356. }
  357. tmp1 := &data_manage.EdbInfoRelation{
  358. ReferObjectId: v.ReferObjectId,
  359. ReferObjectType: v.ReferObjectType,
  360. ReferObjectSubType: v.ReferObjectSubType,
  361. EdbInfoId: childEdbMapping.FromEdbInfoId,
  362. EdbName: childEdbMapping.FromEdbName,
  363. Source: childEdbMapping.FromSource,
  364. EdbCode: childEdbMapping.FromEdbCode,
  365. CreateTime: nowTime,
  366. ModifyTime: nowTime,
  367. RelationTime: nowTime,
  368. RelationType: 1,
  369. RootEdbInfoId: newEdbInfo.EdbInfoId,
  370. ChildEdbInfoId: childEdbMapping.EdbInfoId,
  371. }
  372. tmp1.RelationCode = fmt.Sprintf("%d_%d_%d_%d", tmp1.RootEdbInfoId, tmp1.ReferObjectId, tmp1.ReferObjectType, tmp1.ReferObjectSubType)
  373. addList = append(addList, tmp1)
  374. refreshIds = append(refreshIds, childEdbMapping.FromEdbInfoId)
  375. }
  376. }
  377. logMsg += strconv.Itoa(v.EdbInfoRelationId) + ";"
  378. }
  379. if len(replaceEdbIds) > 0 {
  380. err = data_manage.ReplaceRelationEdbInfoId(oldEdbInfo, newEdbInfo, replaceEdbIds, addList, refreshIds, indexCodeList)
  381. if err != nil {
  382. logMsg = ""
  383. err = fmt.Errorf("替换指标引用表中的指标ID失败 Err:%s", err)
  384. return
  385. }
  386. replaceTotal = len(replaceEdbIds)
  387. }
  388. return
  389. }
  390. func UpdateSecondEdbInRelation(list []*data_manage.EdbInfoRelation, calculateEdbMappingListMap map[int]*data_manage.EdbInfoCalculateMapping, calculateEdbMappingIdsMap map[int][]int, edbInfoList []*data_manage.EdbInfo) (replaceTotal int, logMsg string, err error) {
  391. nowTime := time.Now()
  392. edbInfoRelationIds := make([]int, 0)
  393. indexCodeList := make([]string, 0)
  394. addList := make([]*data_manage.EdbInfoRelation, 0)
  395. refreshIds := make([]int, 0)
  396. edbInfoMap := make(map[int]*data_manage.EdbInfo)
  397. for _, v := range edbInfoList {
  398. edbInfoMap[v.EdbInfoId] = v
  399. }
  400. for _, v := range list {
  401. if v.RelationType == 0 {
  402. edbInfoRelationIds = append(edbInfoRelationIds, v.EdbInfoRelationId)
  403. edbInfo, ok := edbInfoMap[v.EdbInfoId]
  404. if !ok {
  405. err = fmt.Errorf("查询指标信息失败 EdbInfoId:%d", v.EdbInfoId)
  406. return
  407. }
  408. if edbInfo.EdbType == 2 { //计算指标
  409. childEdbMappingIds, ok := calculateEdbMappingIdsMap[edbInfo.EdbInfoId]
  410. if !ok {
  411. err = fmt.Errorf("查询%d指标关联指标列表为空", edbInfo.EdbInfoId)
  412. return
  413. }
  414. for _, childEdbMappingId := range childEdbMappingIds {
  415. childEdbMapping, ok2 := calculateEdbMappingListMap[childEdbMappingId]
  416. if !ok2 {
  417. continue
  418. }
  419. if childEdbMapping.FromSource == utils.DATA_SOURCE_MYSTEEL_CHEMICAL {
  420. indexCodeList = append(indexCodeList, childEdbMapping.FromEdbCode)
  421. }
  422. tmp1 := &data_manage.EdbInfoRelation{
  423. ReferObjectId: v.ReferObjectId,
  424. ReferObjectType: v.ReferObjectType,
  425. ReferObjectSubType: v.ReferObjectSubType,
  426. EdbInfoId: childEdbMapping.FromEdbInfoId,
  427. EdbName: childEdbMapping.FromEdbName,
  428. Source: childEdbMapping.FromSource,
  429. EdbCode: childEdbMapping.FromEdbCode,
  430. CreateTime: nowTime,
  431. ModifyTime: nowTime,
  432. RelationTime: nowTime,
  433. RelationType: 1,
  434. RootEdbInfoId: edbInfo.EdbInfoId,
  435. ChildEdbInfoId: childEdbMapping.EdbInfoId,
  436. }
  437. tmp1.RelationCode = fmt.Sprintf("%d_%d_%d_%d", tmp1.RootEdbInfoId, tmp1.ReferObjectId, tmp1.ReferObjectType, tmp1.ReferObjectSubType)
  438. addList = append(addList, tmp1)
  439. refreshIds = append(refreshIds, childEdbMapping.FromEdbInfoId)
  440. }
  441. }
  442. logMsg += strconv.Itoa(v.EdbInfoRelationId) + ";"
  443. }
  444. }
  445. if len(edbInfoRelationIds) > 0 {
  446. err = data_manage.UpdateSecondRelationEdbInfoId(edbInfoRelationIds, addList, refreshIds, indexCodeList)
  447. if err != nil {
  448. logMsg = ""
  449. err = fmt.Errorf("替换指标引用表中的指标ID失败 Err:%s", err)
  450. return
  451. }
  452. replaceTotal = len(edbInfoRelationIds)
  453. }
  454. return
  455. }