Kaynağa Gözat

Merge branch 'feature/fix_excel_decimal' of eta_gn_server/eta_chart_lib into dm

baoziqiang 4 ay önce
ebeveyn
işleme
531a85b92f

+ 7 - 6
models/request/mixed_table.go

@@ -160,12 +160,13 @@ type MixDateCalculateTagReq struct {
 
 // MixCellShowStyle 混合表格 单元格样式展示计算
 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"`
+	Pn              int         `description:"小数点位数增加或减少,正数表述增加,负数表示减少" json:"pn"`
+	Nt              string      `description:"变换类型:number 小数点位数改变,percent百分比," json:"nt"`
+	GlObj           interface{} `description:"公式对象:1:数值,2:百分比,3:文本" json:"glObj"`
+	Decimal         *int        `description:"小数点位数" json:"decimal"`
+	Last            string      `description:"起始操作:nt|decimal" json:"last"`
+	Color           string      `description:"颜色值,#RRG" json:"color"`
+	BackgroundColor string      `description:"背景颜色值,#RRG" json:"background-color"`
 }
 
 type DateDataBeforeAfterReq struct {

+ 44 - 8
services/data/excel/mixed_table.go

@@ -9,12 +9,13 @@ import (
 	"eta_gn/eta_chart_lib/services/data"
 	"eta_gn/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"
 )
 
 // BaseCalculate
@@ -1185,10 +1186,13 @@ func handleMixCellShowStyle(showStyleList []string, calculateCellMap map[string]
 
 			cell := config[cellPosition.Column][cellPosition.Row]
 			val := cell.ShowValue
-			isPercent := false
-			if strings.Contains(val, "%") {
-				isPercent = true
-				val = strings.Trim(val, "%")
+			// 如果没有showValue, 则使用value
+			if cell.ShowValue == "" {
+				_, err := strconv.ParseFloat(cell.Value, 64)
+				if err == nil {
+					val = cell.Value
+					cell.ShowValue = cell.Value
+				}
 			}
 			_, e := strconv.ParseFloat(val, 64) // 将字符串转换成float类型
 			if e != nil {                       // 如果没有错误发生则返回true,说明该字符串是一个合法的数字
@@ -1200,13 +1204,45 @@ func handleMixCellShowStyle(showStyleList []string, calculateCellMap map[string]
 				err = fmt.Errorf("日期计算配置json解析失败失败: %s, Err:%s", config, err.Error())
 				return
 			}
+			hasPercent := false
+			if styleConf.Nt == "percent" {
+				hasPercent = true
+			}
 
 			if styleConf.Pn != 0 || styleConf.Nt != "" {
-				val = changePointDecimalPlaces(val, styleConf.Pn, styleConf.Nt, isPercent)
-				cell.ShowFormatValue = val
+				// 修复历史数据,没有保存小数位数,重置小数位数
+				if styleConf.Pn != 0 {
+					if styleConf.Decimal == nil {
+						styleConf.Decimal = new(int)
+					}
+					*styleConf.Decimal += styleConf.Pn
+					if *styleConf.Decimal < 0 {
+						*styleConf.Decimal = 0
+					}
+					cell.ShowFormatValue = utils.RoundNumber(cell.ShowValue, *styleConf.Decimal, hasPercent)
+					styleConf.Pn = 0
+				} else if styleConf.Decimal != nil {
+					cell.ShowFormatValue = utils.RoundNumber(cell.ShowValue, *styleConf.Decimal, hasPercent)
+				} else {
+					if hasPercent {
+						numDecimal, _ := decimal.NewFromString(cell.ShowValue)
+						tmpStr := numDecimal.Mul(decimal.NewFromInt(100)).String()
+						cell.ShowFormatValue = tmpStr + "%"
+					} else {
+						cell.ShowFormatValue = cell.ShowValue
+					}
+				}
+			} else if styleConf.Decimal != nil {
+				cell.ShowFormatValue = utils.RoundNumber(cell.ShowValue, *styleConf.Decimal, hasPercent)
 			} else {
 				cell.ShowFormatValue = cell.ShowValue
 			}
+			// 前端传过来的json中有可能有glObj,需要去掉
+			styleConf.GlObj = nil
+			tmpStyleConf, err := json.Marshal(styleConf)
+			if err == nil {
+				cell.ShowStyle = string(tmpStyleConf)
+			}
 			config[cellPosition.Column][cellPosition.Row] = cell
 		}
 	}

+ 35 - 27
services/excel/lucky_sheet.go

@@ -5,10 +5,11 @@ import (
 	"eta_gn/eta_chart_lib/models/request"
 	"eta_gn/eta_chart_lib/utils"
 	"fmt"
-	"math"
 	"reflect"
 	"sort"
 	"strconv"
+
+	"github.com/shopspring/decimal"
 )
 
 type LuckySheetDataBak struct {
@@ -1223,39 +1224,46 @@ func GetTableDataByMixedTableData(config [][]request.MixedTableCellDataReq, hide
 						tmp.FontColor = styleConfig.Color
 					}
 					tmp.Monitor = cell.ShowFormatValue
-					tmpShowValue, err := strconv.ParseFloat(cell.Value, 64)
+					// 如果没有showValue, 则使用value
+					if cell.ShowValue == "" {
+						_, err := strconv.ParseFloat(cell.Value, 64)
+						if err == nil {
+							cell.ShowValue = cell.Value
+						}
+					}
+					_, err := strconv.ParseFloat(cell.ShowValue, 64)
 					if err == nil {
-						switch styleConfig.Last {
-						case "nt":
-							// 先进行数字的百分比计算,然后保留小数点位数
-							percent := tmpShowValue * 100
+						hasPercent := false
+						if styleConfig.Nt == "percent" {
+							hasPercent = true
+						}
+						// 修复历史数据
+						if styleConfig.Pn != 0 {
 							if styleConfig.Decimal == nil {
-								continue
+								styleConfig.Decimal = new(int)
 							}
-							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
+							*styleConfig.Decimal += styleConfig.Pn
+							if *styleConfig.Decimal < 0 {
+								*styleConfig.Decimal = 0
 							}
-							factor := math.Pow(10, float64(*styleConfig.Decimal))
-							rounded := math.Round(tmpShowValue*factor) / factor
-							if styleConfig.Nt == "percent" {
-								percent := rounded * 100
-								var precisionStr string
-								if *styleConfig.Decimal > 2 {
-									precision := *styleConfig.Decimal - 2
-									precisionStr = strconv.FormatFloat(rounded, 'f', precision, 64)
-								} else {
-									precisionStr = strconv.FormatFloat(math.Round(percent), 'f', -1, 64)
-								}
-								tmp.Monitor = fmt.Sprintf("%s%%", precisionStr)
+						}
+
+						if styleConfig.Decimal != nil {
+							tmp.Monitor = utils.RoundNumber(cell.ShowValue, *styleConfig.Decimal, hasPercent)
+						} else {
+							if hasPercent {
+								numDecimal, _ := decimal.NewFromString(cell.ShowValue)
+								tmpStr := numDecimal.Mul(decimal.NewFromInt(100)).String()
+								tmp.Monitor = tmpStr + "%"
 							} else {
-								tmp.Monitor = fmt.Sprintf("%g", rounded)
+								tmp.Monitor = cell.ShowValue
 							}
 						}
+					} else {
+						if cell.DataType == request.CustomTextDT {
+							tmp.Monitor = cell.Value
+						}
+
 					}
 				}
 				dataCol = append(dataCol, tmp)

+ 14 - 0
utils/common.go

@@ -1426,3 +1426,17 @@ func GormDateStrToDateStr(originalString string) (formatStr string) {
 
 	return
 }
+
+// RoundNumber 保留小数位数
+func RoundNumber(num string, decimalPlaces int, hasPercent bool) string {
+	numDecimal, _ := decimal.NewFromString(num)
+	if hasPercent {
+		numDecimal = numDecimal.Mul(decimal.NewFromInt(100))
+	}
+	numFloat, _ := numDecimal.Round(int32(decimalPlaces)).Float64()
+	numStr := strconv.FormatFloat(numFloat, 'f', decimalPlaces, 64)
+	if hasPercent {
+		numStr += "%"
+	}
+	return numStr
+}