xyxie 9 сар өмнө
parent
commit
02888e9700

+ 11 - 6
controllers/data_manage/edb_info.go

@@ -3842,13 +3842,18 @@ func (this *ChartInfoController) EdbInfoReplace() {
 	}
 
 	// 替换相关性图表配置
-	_, err = data_manage.ReplaceMultipleGraphConfigChartEdb(oldEdbInfo, newEdbInfo)
-	if err != nil {
-		br.Msg = "替换失败"
-		br.ErrMsg = "替换失败 replace err:" + err.Error()
-		return
-	}
+	go func() {
+		_, _ = data_manage.ReplaceMultipleGraphConfigChartEdb(oldEdbInfo, newEdbInfo)
+		/*if err != nil {
+			br.Msg = "替换失败"
+			br.ErrMsg = "替换失败 replace err:" + err.Error()
+			return
+		}*/
+	}()
 
+	go func() {
+		excel2.ReplaceEdbInExcel(oldEdbInfo, newEdbInfo)
+	}()
 	br.Msg = "替换成功"
 	br.ErrMsg = "替换成功"
 	br.Ret = 200

+ 9 - 2
models/data_manage/excel/excel_edb_mapping.go

@@ -31,12 +31,19 @@ func (e *ExcelEdbMapping) Add() (err error) {
 	return
 }
 
+// Update 更新 excel表格基础信息
+func (e *ExcelEdbMapping) Update(cols []string) (err error) {
+	o := orm.NewOrmUsingDB("data")
+	_, err = o.Update(e, cols...)
+	return
+}
+
 // GetExcelEdbMappingByEdbInfoId 根据指标id获取配置关系
-func GetExcelEdbMappingByEdbInfoId(edbInfoId int) (item *ExcelEdbMapping, err error) {
+func GetExcelEdbMappingByEdbInfoId(edbInfoId int) (items []*ExcelEdbMapping, err error) {
 	o := orm.NewOrmUsingDB("data")
 	sql := ` SELECT *  FROM excel_edb_mapping WHERE 1=1 AND edb_info_id = ? `
 
-	err = o.Raw(sql, edbInfoId).QueryRow(&item)
+	_, err = o.Raw(sql, edbInfoId).QueryRows(&items)
 	return
 }
 

+ 51 - 0
models/data_manage/excel/excel_info.go

@@ -1,9 +1,12 @@
 package excel
 
 import (
+	"eta/eta_api/models/data_manage"
+	"eta/eta_api/services/alarm_msg"
 	"eta/eta_api/utils"
 	"fmt"
 	"github.com/beego/beego/v2/client/orm"
+	"strconv"
 	"time"
 )
 
@@ -646,3 +649,51 @@ func GetExcelBaseInfoByExcelInfoIdList(excelInfoIdList []int) (items []*ExcelBas
 
 	return
 }
+
+// ReplaceEdbInExcel 替换表格中的指标
+func ReplaceEdbInExcel(oldEdbInfo, newEdbInfo *data_manage.EdbInfo, updateExcelList []*ExcelInfo) (err error) {
+	var errmsg string
+	logMsg := `` // 记录替换的日志
+	replaceTotal := 0
+	o := orm.NewOrmUsingDB("data")
+	to, err := o.Begin()
+	if err != nil {
+		return
+	}
+	defer func() {
+		if err != nil {
+			_ = to.Rollback()
+		} else {
+			_ = to.Commit()
+			if logMsg != `` {
+				utils.FileLog.Info(fmt.Sprintf("替换表格中的指标记录,替换总表格数:%d,旧的指标id:%d,新的指标id:%d;%s", replaceTotal, oldEdbInfo.EdbInfoId, newEdbInfo.EdbInfoId, logMsg))
+			}
+		}
+		if errmsg != "" {
+			fmt.Println("errmsg:" + errmsg)
+		}
+		if err != nil && errmsg != "" {
+			go alarm_msg.SendAlarmMsg("替换替换配置中的指标记录失败提醒,errmsg:"+errmsg, 3)
+		}
+	}()
+	// 替换表格中的内容
+	for _, excelInfo := range updateExcelList {
+		//更新配置中的指标A
+		sql := `UPDATE excel_info SET content=?, modify_time=? WHERE excel_info_id=?`
+		_, err = to.Raw(sql, excelInfo.Content, time.Now(), excelInfo.ExcelInfoId).Exec()
+		if err != nil {
+			errmsg = "更新表格内容失败:Err:" + err.Error()
+			return
+		}
+		//更新配置中的指标B
+		sql = `UPDATE excel_edb_mapping SET edb_info_id=?, modify_time=? WHERE excel_info_id=? and edb_info_id=?`
+		_, err = to.Raw(sql, newEdbInfo.EdbInfoId, time.Now(), excelInfo.ExcelInfoId, oldEdbInfo.EdbInfoId).Exec()
+		if err != nil {
+			errmsg = "更新指标B关联图表配置信息失败:Err:" + err.Error()
+			return
+		}
+		logMsg += `涉及到的表格id:` + strconv.Itoa(excelInfo.ExcelInfoId) + ";"
+		replaceTotal += 1
+	}
+	return
+}

+ 172 - 0
services/data/excel/excel_info.go

@@ -499,3 +499,175 @@ func GetExcelEdbBatchRefreshKey(source string, reportId, chapterId int) string {
 
 	return fmt.Sprint("batch_refresh_excel_edb:", source, ":", reportId, ":", chapterId)
 }
+
+// ReplaceEdbInExcel 替换表格中的指标
+func ReplaceEdbInExcel(oldEdbInfo, newEdbInfo *data_manage.EdbInfo) (err error) {
+	//查询和指标相关的 表格
+	mappingList, err := excel.GetExcelEdbMappingByEdbInfoId(oldEdbInfo.EdbInfoId)
+	if err != nil {
+		err = fmt.Errorf("查询和指标相关的表格失败,错误:%s", err.Error())
+		return
+	}
+	updateList := make([]*excel.ExcelInfo, 0)
+	// 循环列表,根据表格类型单独处理
+	for _, excelMapping := range mappingList {
+		//查询和指标相关的混合表格
+		excelInfo, tmpErr := excel.GetExcelInfoById(excelMapping.ExcelInfoId)
+		if tmpErr != nil {
+			err = fmt.Errorf("查询和指标相关的混合表格失败,错误:%s", tmpErr.Error())
+			return
+		}
+		// 清除缓存
+		key := utils.HZ_CHART_LIB_EXCEL_TABLE_DETAIL + ":" + excelInfo.UniqueCode
+		if utils.Re == nil {
+			_ = utils.Rc.Delete(key)
+		}
+		// 根据表格类型,调用不同的处理函数
+		switch excelMapping.Source {
+		case utils.TIME_TABLE: // 时间序列表格
+			// 替换余额表格中的指标
+			newExcelInfo, e := replaceEdbInTimeExcel(oldEdbInfo, newEdbInfo, excelInfo)
+			if e != nil {
+				err = fmt.Errorf("替换余额表格中的指标失败,错误:%s", e.Error())
+				return
+			}
+			updateList = append(updateList, newExcelInfo)
+		case utils.MIXED_TABLE:
+			// 替换余额表格中的指标
+			newExcelInfo, e := replaceEdbInBalanceExcel(oldEdbInfo, newEdbInfo, excelInfo)
+			if e != nil {
+				err = fmt.Errorf("替换余额表格中的指标失败,错误:%s", e.Error())
+				return
+			}
+			updateList = append(updateList, newExcelInfo)
+		default:
+			// 其他表格类型的处理逻辑
+		}
+	}
+
+	err = excel.ReplaceEdbInExcel(oldEdbInfo, newEdbInfo, updateList)
+	if err != nil {
+		err = fmt.Errorf("替换表格中的指标失败,错误:%s", err.Error())
+	}
+
+	//todo 是否需要刷新表格中的指标数据
+	return
+}
+
+func replaceEdbInBalanceExcel(oldEdbInfo, newEdbInfo *data_manage.EdbInfo, excelInfo *excel.ExcelInfo) (newExcelInfo *excel.ExcelInfo, err error) {
+	newExcelInfo = excelInfo
+	var mixedTableReq request.MixedTableReq
+	err = json.Unmarshal([]byte(excelInfo.Content), &mixedTableReq)
+	if err != nil {
+		err = fmt.Errorf("表格json转结构体失败,Err:" + err.Error())
+		return
+	}
+	// 处理data
+	configList := mixedTableReq.Data
+	for ck, rowList := range configList {
+		for rk, cell := range rowList {
+			switch cell.DataType {
+			case request.EdbDT: // 指标信息
+				if cell.EdbInfoId == oldEdbInfo.EdbInfoId {
+					//更换成新指标ID
+					configList[ck][rk].EdbInfoId = newEdbInfo.EdbInfoId
+				}
+			case request.InsertDataDT, request.PopInsertDataDT: // 插值、弹框插值
+				if cell.EdbInfoId == oldEdbInfo.EdbInfoId {
+					//更换成新指标ID
+					configList[ck][rk].EdbInfoId = newEdbInfo.EdbInfoId
+				}
+			case request.InsertEdbCalculateDataDT: // 插入指标计算公式生成的值
+				var config request.CalculateConf
+				err = json.Unmarshal([]byte(cell.Value), &config)
+				if err != nil {
+					return
+				}
+				if cell.EdbInfoId == oldEdbInfo.EdbInfoId {
+					//更换成新指标ID
+					configList[ck][rk].EdbInfoId = newEdbInfo.EdbInfoId
+				}
+				if config.EdbInfoId == oldEdbInfo.EdbInfoId {
+					config.EdbInfoId = newEdbInfo.EdbInfoId
+				}
+				var configStr []byte
+				configStr, err = json.Marshal(config)
+				if err != nil {
+					return
+				}
+				configList[ck][rk].Value = string(configStr)
+			case request.DateDT: // 日期类型
+				// 指标日期类型的单元格需要额外将指标id取出来
+				if cell.DataTimeType == request.EdbDateDT {
+					var config request.EdbDateConf
+					err = json.Unmarshal([]byte(cell.Value), &config)
+					if err != nil {
+						return
+					}
+					if config.EdbInfoId == oldEdbInfo.EdbInfoId {
+						config.EdbInfoId = newEdbInfo.EdbInfoId
+						var configStr []byte
+						configStr, err = json.Marshal(config)
+						if err != nil {
+							return
+						}
+						configList[ck][rk].Value = string(configStr)
+					}
+				}
+			}
+		}
+	}
+
+	mixedTableReq.Data = configList
+	var newContentByte []byte
+	newContentByte, err = json.Marshal(mixedTableReq)
+	if err != nil {
+		return
+	}
+	// 生成的新内容替换原先的旧内容
+	excelInfo.Content = string(newContentByte)
+	newExcelInfo = excelInfo
+	return
+}
+
+func replaceEdbInTimeExcel(oldEdbInfo, newEdbInfo *data_manage.EdbInfo, excelInfo *excel.ExcelInfo) (newExcelInfo *excel.ExcelInfo, err error) {
+	newExcelInfo = excelInfo
+
+	var tableDataConfig TableDataConfig
+	err = json.Unmarshal([]byte(excelInfo.Content), &tableDataConfig)
+	if err != nil {
+		err = errors.New("表格json转结构体失败,Err:" + err.Error())
+		return
+	}
+	if len(tableDataConfig.EdbInfoIdList) <= 0 {
+		return
+	}
+	// 实际期数没有的情况下,直接返回吧
+	if tableDataConfig.Num <= 0 {
+		return
+	}
+
+	// 先处理edbInfoList
+	for k, id := range tableDataConfig.EdbInfoIdList {
+		if id == oldEdbInfo.EdbInfoId {
+			tableDataConfig.EdbInfoIdList[k] = newEdbInfo.EdbInfoId
+		}
+	}
+
+	// 先处理tableEdbInfoList
+	for k, tableEdbInfo := range tableDataConfig.TableEdbInfoList {
+		if tableEdbInfo.EdbInfoId == oldEdbInfo.EdbInfoId {
+			tableDataConfig.TableEdbInfoList[k].EdbInfoId = newEdbInfo.EdbInfoId
+		}
+	}
+
+	var newContentByte []byte
+	newContentByte, err = json.Marshal(tableDataConfig)
+	if err != nil {
+		return
+	}
+	// 生成的新内容替换原先的旧内容
+	excelInfo.Content = string(newContentByte)
+	newExcelInfo = excelInfo
+	return
+}