Преглед изворни кода

fix:表格PPT和研报样式兼容

zqbao пре 5 месеци
родитељ
комит
cc39b69e7b
3 измењених фајлова са 89 додато и 4 уклоњено
  1. 2 0
      models/request/mixed_table.go
  2. 58 3
      services/data/excel/time_table.go
  3. 29 1
      services/excel/lucky_sheet.go

+ 2 - 0
models/request/mixed_table.go

@@ -162,6 +162,8 @@ type MixDateCalculateTagReq struct {
 type MixCellShowStyle struct {
 	Pn              int    `description:"小数点位数增加或减少,正数表述增加,负数表示减少" json:"pn"`
 	Nt              string `description:"变换类型:number 小数点位数改变,percent百分比," json:"nt"`
+	Decimal         *int   `description:"小数点位数"`
+	Last            string `description:"起始操作:nt|decimal"`
 	Color           string `description:"颜色值,#RRG" json:"color"`
 	BackgroundColor string `description:"背景颜色值,#RRG" json:"background-color"`
 }

+ 58 - 3
services/data/excel/time_table.go

@@ -8,12 +8,13 @@ import (
 	"eta/eta_chart_lib/services/data"
 	"eta/eta_chart_lib/utils"
 	"fmt"
-	"github.com/shopspring/decimal"
-	"github.com/yidane/formula"
 	"sort"
 	"strconv"
 	"strings"
 	"time"
+
+	"github.com/shopspring/decimal"
+	"github.com/yidane/formula"
 )
 
 // TableDataConfig
@@ -27,6 +28,13 @@ type TableDataConfig struct {
 	ManualDate       []string                  `description:"手动配置的日期(未来的日期)"`
 	TableEdbInfoList []TableEdbInfo            `description:"表格内指标信息"`
 	TextRowData      [][]request.ManualDataReq `description:"文本列表"`
+	DecimalConfig    []DecimalConfig           `description:"小数位数配置"`
+}
+
+type DecimalConfig struct {
+	Row     string `description:"行上的索引, 目前仅保存指标的日期"`
+	Col     int    `description:"列上的索引, 目前仅保存edbInfoId"`
+	Decimal int    `description:"小数位数, 从左至右,从上到下的顺序"`
 }
 
 // TableEdbInfo
@@ -329,7 +337,7 @@ func GetDataByTableDataConfig(tableDataConfig TableDataConfig) (resultResp reque
 			d[k2].ShowValue = utils.FormatTableDataShowValue(vf)
 		}
 	}
-
+	data = SetExcelShowValueByDecimalConfig(data, tableDataConfig.DecimalConfig)
 	resultResp = request.TableDataReq{
 		EdbInfoIdList: edbInfoIdList,
 		Sort:          tableDataConfig.Sort,
@@ -340,6 +348,53 @@ func GetDataByTableDataConfig(tableDataConfig TableDataConfig) (resultResp reque
 	return
 }
 
+func SetExcelShowValueByDecimalConfig(edbData []request.EdbInfoData, decimalConfig []DecimalConfig) []request.EdbInfoData {
+	if len(decimalConfig) <= 0 || len(edbData) <= 0 {
+		return edbData
+	}
+	edbInfoIndexMap := make(map[int]int)
+	dateIndexMap := make(map[string]int)
+	for i, edbInfo := range edbData {
+		edbInfoIndexMap[edbInfo.EdbInfoId] = i
+	}
+	for i, date := range edbData[0].Data {
+		dateIndexMap[date.DataTime] = i
+	}
+	for _, conf := range decimalConfig {
+		if conf.Col > 0 {
+			if edbIndex, ok := edbInfoIndexMap[conf.Col]; ok {
+				for i := 0; i < len(edbData[edbIndex].Data); i++ {
+					f, err := strconv.ParseFloat(edbData[edbIndex].Data[i].Value, 64)
+					if err != nil {
+						continue
+					}
+					if conf.Decimal == -1 {
+						continue
+					}
+					formatStr := fmt.Sprintf("%%.%df", conf.Decimal)
+					edbData[edbIndex].Data[i].ShowValue = fmt.Sprintf(formatStr, f)
+				}
+			}
+		}
+		if conf.Row != `` {
+			if dateIndex, ok := dateIndexMap[conf.Row]; ok {
+				for i := 0; i < len(edbData); i++ {
+					f, err := strconv.ParseFloat(edbData[i].Data[dateIndex].Value, 64)
+					if err != nil {
+						continue
+					}
+					if conf.Decimal == -1 {
+						continue
+					}
+					formatStr := fmt.Sprintf("%%.%df", conf.Decimal)
+					edbData[i].Data[dateIndex].ShowValue = fmt.Sprintf(formatStr, f)
+				}
+			}
+		}
+	}
+	return edbData
+}
+
 // GetTableDataConfig 根据TableDataReq配置获取相关数据配置
 func GetTableDataConfig(reqData request.TableDataReq) (tableDataConfig TableDataConfig, err error) {
 	// 指标数据

+ 29 - 1
services/excel/lucky_sheet.go

@@ -5,6 +5,7 @@ import (
 	"eta/eta_chart_lib/models/request"
 	"eta/eta_chart_lib/utils"
 	"fmt"
+	"math"
 	"reflect"
 	"sort"
 	"strconv"
@@ -1215,13 +1216,40 @@ func GetTableDataByMixedTableData(config [][]request.MixedTableCellDataReq, hide
 					if err := json.Unmarshal([]byte(cell.ShowStyle), &styleConfig); err != nil {
 						utils.FileLog.Info("表格样式showStyle解析失败", err.Error())
 					}
-					tmp.Monitor = cell.ShowFormatValue
 					if styleConfig.BackgroundColor != "" {
 						tmp.Background = styleConfig.BackgroundColor
 					}
 					if styleConfig.Color != "" {
 						tmp.FontColor = styleConfig.Color
 					}
+					tmp.Monitor = cell.ShowFormatValue
+					tmpShowValue, err := strconv.ParseFloat(cell.Value, 64)
+					if err == nil {
+						switch styleConfig.Last {
+						case "nt":
+							// 先进行数字的百分比计算,然后保留小数点位数
+							percent := tmpShowValue * 100
+							if styleConfig.Decimal == nil {
+								continue
+							}
+							factor := math.Pow(10, float64(*styleConfig.Decimal))
+							rounded := math.Round(percent*factor) / factor
+							tmp.Monitor = fmt.Sprintf("%g%%", rounded)
+						case "decimal":
+							// 先保留小数点位数,再进行百分比计算
+							if styleConfig.Decimal == nil {
+								continue
+							}
+							factor := math.Pow(10, float64(*styleConfig.Decimal))
+							rounded := math.Round(tmpShowValue*factor) / factor
+							if styleConfig.Nt == "percent" {
+								percent := rounded * 100
+								tmp.Monitor = fmt.Sprintf("%g%%", percent)
+							} else {
+								tmp.Monitor = fmt.Sprintf("%g", rounded)
+							}
+						}
+					}
 				}
 				dataCol = append(dataCol, tmp)
 			}