edb_info_replace.go 15 KB

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