Browse Source

增加混合表格样式

xyxie 1 year ago
parent
commit
b2dd6da2ad
2 changed files with 93 additions and 22 deletions
  1. 13 1
      models/data_manage/excel/request/mixed_table.go
  2. 80 21
      services/data/excel/mixed_table.go

+ 13 - 1
models/data_manage/excel/request/mixed_table.go

@@ -17,6 +17,7 @@ const (
 	CustomDateT = iota //手动输入日期
 	SystemDateT        // 系统日期
 	EdbDateDT          // 导入指标日期(指标库的最新日期)
+
 )
 
 // 单元格的日期类型类型
@@ -40,9 +41,10 @@ type MixedTableCellDataReq struct {
 	DataTimeType int    `description:"日期类型:0:手动输入日期(固定日期);1:导入系统日期;;3:导入指标日期(指标库的最新日期);"`
 	EdbInfoId    int    `description:"指标id"`
 	ShowValue    string `description:"展示值"`
-	Value        string `description:"实际值"`
+	Value        string `description:"实际值和配置"`
 	Extra        string `description:"额外参数"`
 	ShowStyle    string `description:"展示的样式配置"`
+	RealValue    string `description:"实际的值"`
 }
 
 // CellRelationConf
@@ -114,6 +116,16 @@ type MixDateCalculateTag struct {
 	Tag string `description:"指标对应标签"`
 }
 
+// MixCellShowStyle 混合表格 单元格样式展示计算
+type MixCellShowStyle struct {
+	NumberStyle MixNumberShowStyle `description:"小数点位数增加或减少,百分比"`
+}
+
+type MixNumberShowStyle struct {
+	AddPointNum int `description:"显示的计算类型:小数点位数增加或减少,正数表述增加,负数表示减少"`
+	IsPercent   int `description:"是否是百分比1 是,0否, 将选中的单元格的样式设置成百分比"`
+}
+
 // CalculateConf
 // @Description: 计算公式
 type CalculateConf struct {

+ 80 - 21
services/data/excel/mixed_table.go

@@ -143,7 +143,7 @@ func GetMixedTableCellData(mixedTableReq request.MixedTableReq) (newMixedTableCe
 	}
 
 	// 指标计算的结果map
-	edbSourceDataMap := make(map[string]map[string]float64)
+	edbSourceDataMap := make(map[string]data.BaseCalculateDataResp)
 
 	// 单元格对应的key与他的值(只处理数据类型)
 	cellKeyVal := make(map[string]float64)
@@ -152,9 +152,11 @@ func GetMixedTableCellData(mixedTableReq request.MixedTableReq) (newMixedTableCe
 	calculateCellMap := make(map[string]Cell)
 	calculateChainList := make([]string, 0)
 	dateCalculateList := make([]string, 0)
+	showStyleList := make([]string, 0)
 	// 处理单元格中的数据类型(除去基础计算,因为这个是依赖于其他)
 	for k, row := range config {
 		for i, cell := range row {
+			cell.RealValue = cell.ShowValue
 			switch cell.DataType {
 			case request.DateDT: // 日期类型
 				calculateCellMap[cell.Uid] = Cell{
@@ -165,6 +167,7 @@ func GetMixedTableCellData(mixedTableReq request.MixedTableReq) (newMixedTableCe
 			case request.EdbDT: // 指标类型
 				if edbInfo, ok := edbInfoMap[cell.EdbInfoId]; ok {
 					cell.ShowValue = edbInfo.EdbName
+					cell.RealValue = cell.ShowValue
 				}
 			case request.InsertDataDT, request.PopInsertDataDT: // 数据类型
 				// 数值先清空
@@ -205,6 +208,7 @@ func GetMixedTableCellData(mixedTableReq request.MixedTableReq) (newMixedTableCe
 						//cell.ShowValue = fmt.Sprint(val)
 						cellKeyVal[cell.Uid] = val
 						cell.ShowValue = utils.FormatTableDataShowValue(val)
+						cell.RealValue = cell.ShowValue
 					}
 				} else {
 					// 如果不是取得一个关联的日期,那么就是指定日期
@@ -236,10 +240,10 @@ func GetMixedTableCellData(mixedTableReq request.MixedTableReq) (newMixedTableCe
 								}
 								cellKeyVal[cell.Uid] = finalVal
 								cell.ShowValue = utils.FormatTableDataShowValue(dateValList[tmpLenData-1].Value)
+								cell.RealValue = cell.ShowValue
 							}
 						}
 					} else {
-						// todo 既没有绑定单元格,又存在一个日期数据,是否需要做日期变换
 						tmpDateList := strings.Split(cell.DataTime, "-")
 						tmpDateValMap := make(map[string]float64)
 						if len(tmpDateList) == 2 {
@@ -258,6 +262,7 @@ func GetMixedTableCellData(mixedTableReq request.MixedTableReq) (newMixedTableCe
 							//cell.ShowValue = fmt.Sprint(val)
 							cellKeyVal[cell.Uid] = val
 							cell.ShowValue = utils.FormatTableDataShowValue(val)
+							cell.RealValue = cell.ShowValue
 						}
 					}
 				}
@@ -315,7 +320,7 @@ func GetMixedTableCellData(mixedTableReq request.MixedTableReq) (newMixedTableCe
 				var tmpDataMap map[string]float64
 
 				key := utils.MD5(cell.Value)
-				tmpDataMap, ok := edbSourceDataMap[key]
+				respItemData, ok := edbSourceDataMap[key]
 				if !ok {
 					// 对应的配置值
 					var tmpConfig request.CalculateConf
@@ -362,19 +367,19 @@ func GetMixedTableCellData(mixedTableReq request.MixedTableReq) (newMixedTableCe
 						continue
 					}
 
-					tmpDataMap = respItem.Data.DataMap
+					//tmpDataMap = respItem.Data.DataMap
 					// 计算结果存一份,万一存在重复的计算方式,那么省的重新计算一下
-					edbSourceDataMap[key] = tmpDataMap
-
-					lenDataList := len(respItem.Data.DateList)
-					if cellDateTime == `` && lenDataList > 0 {
-						//判断是否需要做期数前移动
-						cellDateTime = respItem.Data.DateList[lenDataList-1]
-						cellDateTime, err = GetEdbDateByMoveForwardByDateList(cell.Value, respItem.Data.DateList)
-						if err != nil {
-							utils.FileLog.Error(fmt.Sprintf("日期前移失败,配置信息;%s", cell.Value))
-							continue
-						}
+					edbSourceDataMap[key] = respItem.Data
+				}
+				lenDataList := len(respItemData.DateList)
+				tmpDataMap = respItemData.DataMap
+				if cellDateTime == `` && lenDataList > 0 {
+					//判断是否需要做期数前移动
+					cellDateTime = respItemData.DateList[lenDataList-1]
+					cellDateTime, err = GetEdbDateByMoveForwardByDateList(cell.Value, respItemData.DateList)
+					if err != nil {
+						utils.FileLog.Error(fmt.Sprintf("日期前移失败,配置信息;%s", cell.Value))
+						continue
 					}
 				}
 				// 进行日期变换
@@ -386,6 +391,7 @@ func GetMixedTableCellData(mixedTableReq request.MixedTableReq) (newMixedTableCe
 				val := tmpDataMap[cellDateTime]
 				cellKeyVal[cell.Uid] = val
 				cell.ShowValue = utils.FormatTableDataShowValue(val)
+				cell.RealValue = cell.ShowValue
 			case request.DateCalculateDataDT: //日期计算
 				// 把关联的单元格存在数组里
 				calculateCellMap[cell.Uid] = Cell{
@@ -397,6 +403,14 @@ func GetMixedTableCellData(mixedTableReq request.MixedTableReq) (newMixedTableCe
 				// 遍历数组,根据公式进行计算,并将得到的结果放到对应的单元格中
 			}
 
+			if cell.ShowValue != `` {
+				calculateCellMap[cell.Uid] = Cell{
+					Column:   k,
+					Row:      i,
+					CellInfo: cell,
+				}
+				showStyleList = append(showStyleList, cell.Uid)
+			}
 			row[i] = cell
 		}
 		config[k] = row
@@ -429,6 +443,7 @@ func GetMixedTableCellData(mixedTableReq request.MixedTableReq) (newMixedTableCe
 
 			cellKeyVal[cell.Uid] = val
 			cell.ShowValue = utils.FormatTableDataShowValue(val)
+			cell.RealValue = cell.ShowValue
 			config[cellPosition.Column][cellPosition.Row] = cell
 
 		}
@@ -439,6 +454,13 @@ func GetMixedTableCellData(mixedTableReq request.MixedTableReq) (newMixedTableCe
 	if err != nil {
 		return
 	}
+
+	// todo 样式调整
+	config, err, errMsg = handleMixCellShowStyle(showStyleList, calculateCellMap, config)
+	if err != nil {
+		return
+	}
+
 	newMixedTableCellDataList = config
 
 	return
@@ -609,7 +631,7 @@ func handleDate(dataTimeType int, val string) (date string, newVal string, err e
 
 	switch dataTimeType {
 	case request.CustomDateT: //手动输入日期
-		if !strings.Contains(val, "{") {
+		/*if !strings.Contains(val, "{") {
 			newVal, err, errMsg = handleOldCustomerDateT(val)
 			if err != nil {
 				return
@@ -618,7 +640,8 @@ func handleDate(dataTimeType int, val string) (date string, newVal string, err e
 		date, err = HandleMixTableDateChange("", newVal)
 		if err != nil {
 			return
-		}
+		}*/
+		date = val
 		return
 	case request.SystemDateT: // 系统日期
 		date = time.Now().Format(utils.FormatDate)
@@ -1049,7 +1072,7 @@ func handlerDateCalculate(dateCalculateList []string, calculateCellMap map[strin
 				continue
 			}
 
-			val, tmpErr, tmpErrMsg := dateCalculate(calculateCellMap, cell.Value)
+			val, tmpErr, tmpErrMsg := dateCalculatePrepare(calculateCellMap, cell.Value)
 			if tmpErr != nil {
 				errMsg = tmpErrMsg
 				err = tmpErr
@@ -1057,13 +1080,14 @@ func handlerDateCalculate(dateCalculateList []string, calculateCellMap map[strin
 			}
 
 			cell.ShowValue = utils.FormatTableDataShowValue(val)
+			cell.RealValue = cell.ShowValue
 			config[cellPosition.Column][cellPosition.Row] = cell
 		}
 	}
 	return
 }
 
-func dateCalculate(calculateCellMap map[string]Cell, config string) (val float64, err error, errMsg string) {
+func dateCalculatePrepare(calculateCellMap map[string]Cell, config string) (val float64, err error, errMsg string) {
 	var edbDateConf request.MixDateCalculateConf
 	err = json.Unmarshal([]byte(config), &edbDateConf)
 	if err != nil {
@@ -1097,14 +1121,14 @@ func dateCalculate(calculateCellMap map[string]Cell, config string) (val float64
 	}
 
 	// 计算
-	val, errMsg, err = dateFormulaCalculate(valMap, edbDateConf.Formula)
+	val, errMsg, err = dateCalculateFormula(valMap, strings.ToUpper(edbDateConf.Formula))
 	if err != nil {
 		return
 	}
 	return
 }
 
-func dateFormulaCalculate(valTagMap map[string]int, calculateFormula string) (calVal float64, errMsg string, err error) {
+func dateCalculateFormula(valTagMap map[string]int, calculateFormula string) (calVal float64, errMsg string, err error) {
 	if calculateFormula == "" {
 		errMsg = "公式异常"
 		err = errors.New(errMsg)
@@ -1154,3 +1178,38 @@ func dateFormulaCalculate(valTagMap map[string]int, calculateFormula string) (ca
 
 	return
 }
+
+// handleMixCellShowStyle 处理混合表格中,显示计算的逻辑
+func handleMixCellShowStyle(showStyleList []string, calculateCellMap map[string]Cell, oldConfig [][]request.MixedTableCellDataReq) (config [][]request.MixedTableCellDataReq, err error, errMsg string) {
+	config = oldConfig
+	if len(showStyleList) == 0 {
+		return
+	}
+	if len(showStyleList) > 0 {
+		for _, cellKey := range showStyleList {
+			// 查找这个单元格的位置,直接map找了,而不是遍历整个单元格
+			cellPosition, ok := calculateCellMap[cellKey]
+			if !ok {
+				utils.FileLog.Error("找不到单元格位置:", cellKey)
+				continue
+			}
+
+			cell := config[cellPosition.Column][cellPosition.Row]
+			_, e := strconv.ParseFloat(cell.ShowStyle, 64) // 将字符串转换成float类型
+			if e != nil {                                  // 如果没有错误发生则返回true,说明该字符串是一个合法的数字
+				continue
+			}
+			val := cell.ShowStyle
+			var styleConf request.MixCellShowStyle
+			err = json.Unmarshal([]byte(cell.ShowStyle), &styleConf)
+			if err != nil {
+				err = fmt.Errorf("日期计算配置json解析失败失败: %s, Err:%s", config, err.Error())
+				return
+			}
+			// todo 处理百分号,和小数点位数增加或者减少
+			cell.ShowValue = val
+			config[cellPosition.Column][cellPosition.Row] = cell
+		}
+	}
+	return
+}