瀏覽代碼

fix:表格增加保留小数位功能

zqbao 6 月之前
父節點
當前提交
fafd56fcf0

+ 2 - 0
controllers/data_manage/excel/excel_info.go

@@ -180,6 +180,7 @@ func (c *ExcelInfoController) Add() {
 		}
 		edbInfoIdList = tableDataConfig.EdbInfoIdList
 
+		tableDataConfig.DecimalConfig = tableData.DecimalConfig
 		contentByte, err := json.Marshal(tableDataConfig)
 		if err != nil {
 			br.Msg = "自定义表格数据获取失败"
@@ -955,6 +956,7 @@ func (c *ExcelInfoController) Edit() {
 			br.ErrMsg = "自定义表格数据获取失败,Err:" + err.Error()
 			return
 		}
+		tableDataConfig.DecimalConfig = tableData.DecimalConfig
 		contentByte, err := json.Marshal(tableDataConfig)
 		if err != nil {
 			br.Msg = "自定义表格数据获取失败"

+ 80 - 0
models/data_manage/excel/request/excel_info.go

@@ -1,5 +1,7 @@
 package request
 
+import "encoding/json"
+
 // MoveExcelInfoReq 移动excel表格请求
 type MoveExcelInfoReq struct {
 	ExcelClassifyId int `description:"excel表格分类id"`
@@ -107,6 +109,12 @@ type TableDataReq struct {
 	Sort          int               `description:"日期排序,1:倒序,2:正序"`
 	Data          []EdbInfoData     `description:"数据列表"`
 	TextRowData   [][]ManualDataReq `description:"文本列表"`
+	DecimalConfig []DecimalConfig   `description:"小数位数配置"`
+}
+type DecimalConfig struct {
+	Row     string `description:"行上的索引, 目前仅保存指标的日期"`
+	Col     int    `description:"列上的索引, 目前仅保存edbInfoId"`
+	Decimal int    `description:"小数位数, 从左至右,从上到下的顺序"`
 }
 
 // EdbInfoData 自定义表格的数据
@@ -119,12 +127,54 @@ type EdbInfoData struct {
 	Frequency        string          `description:"频度"`
 	Unit             string          `description:"单位"`
 	UnitEn           string          `description:"英文单位"`
+	Decimal          int             `description:"小数位数"`
+	ClassifyId       int             `description:"所属分类" json:"-"`
+	IsJoinPermission int             `description:"是否加入权限管控,0:不加入;1:加入;默认:0" json:"-"`
+	HaveOperaAuth    bool            `description:"是否有数据权限,默认:false"`
+	Data             []ManualDataReq `description:"单元格数据列表"`
+}
+type EdbInfoDataTemp struct {
+	EdbInfoId        int             `description:"指标ID"`
+	Tag              string          `description:"标签"`
+	EdbName          string          `description:"指标名称"`
+	EdbNameEn        string          `description:"英文指标名称"`
+	EdbAliasName     string          `description:"指标别名"`
+	Frequency        string          `description:"频度"`
+	Unit             string          `description:"单位"`
+	UnitEn           string          `description:"英文单位"`
+	Decimal          *int            `description:"小数位数"`
 	ClassifyId       int             `description:"所属分类" json:"-"`
 	IsJoinPermission int             `description:"是否加入权限管控,0:不加入;1:加入;默认:0" json:"-"`
 	HaveOperaAuth    bool            `description:"是否有数据权限,默认:false"`
 	Data             []ManualDataReq `description:"单元格数据列表"`
 }
 
+func (e *EdbInfoData) UnmarshalJSON(data []byte) error {
+	var alias EdbInfoDataTemp
+
+	if err := json.Unmarshal(data, &alias); err != nil {
+		return err
+	}
+	if alias.Decimal != nil {
+		e.Decimal = *alias.Decimal
+	} else {
+		e.Decimal = -1
+	}
+	e.EdbInfoId = alias.EdbInfoId
+	e.Tag = alias.Tag
+	e.EdbName = alias.EdbName
+	e.EdbNameEn = alias.EdbNameEn
+	e.EdbAliasName = alias.EdbAliasName
+	e.Frequency = alias.Frequency
+	e.Unit = alias.Unit
+	e.UnitEn = alias.UnitEn
+	e.ClassifyId = alias.ClassifyId
+	e.IsJoinPermission = alias.IsJoinPermission
+	e.HaveOperaAuth = alias.HaveOperaAuth
+	e.Data = alias.Data
+	return nil
+}
+
 // ManualDataReq 自定义表格的单元格数据
 type ManualDataReq struct {
 	DataType            int               `description:"数据类型,1:普通的,2:插值法,3:手动输入,4:公式计算,5:预测值"`
@@ -136,6 +186,36 @@ type ManualDataReq struct {
 	RelationEdbInfoList []RelationEdbInfo `description:"关联指标(计算公式中关联的指标,用于计算的时候去匹配)"`
 }
 
+type ManualDataTemp struct {
+	DataType            int               `description:"数据类型,1:普通的,2:插值法,3:手动输入,4:公式计算,5:预测值"`
+	DataTime            string            `description:"所属日期"`
+	DataTimeType        int               `description:"日期类型,1:实际日期;2:未来日期"`
+	ShowValue           string            `description:"展示值"`
+	Value               string            `description:"实际值(计算公式)"`
+	Decimal             *int              `description:"小数位数"`
+	RelationEdbInfoList []RelationEdbInfo `description:"关联指标(计算公式中关联的指标,用于计算的时候去匹配)"`
+}
+
+func (m *ManualDataReq) UnmarshalJSON(data []byte) error {
+	var alias ManualDataTemp
+
+	if err := json.Unmarshal(data, &alias); err != nil {
+		return err
+	}
+	if alias.Decimal != nil {
+		m.Decimal = *alias.Decimal
+	} else {
+		m.Decimal = -1
+	}
+	m.DataType = alias.DataType
+	m.DataTime = alias.DataTime
+	m.DataTimeType = alias.DataTimeType
+	m.ShowValue = alias.ShowValue
+	m.Value = alias.Value
+	m.RelationEdbInfoList = alias.RelationEdbInfoList
+	return nil
+}
+
 // RelationEdbInfo 自定义表格中单元格的关联指标
 type RelationEdbInfo struct {
 	Tag string `description:"指标标签"`

+ 2 - 0
models/data_manage/excel/request/mixed_table.go

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

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

@@ -126,6 +126,7 @@ func formatExcelInfo2Detail(excelInfo *excel.ExcelInfo, sysUserId int, lang stri
 			err = errors.New("获取最新的表格数据失败,Err:" + tmpErr.Error())
 			return
 		}
+		result = SetExcelByDecimalConfig(result, tableDataConfig.DecimalConfig)
 
 		if len(result.EdbInfoIdList) > 0 {
 			classifyIdList := make([]int, 0)
@@ -185,6 +186,39 @@ func formatExcelInfo2Detail(excelInfo *excel.ExcelInfo, sysUserId int, lang stri
 	return
 }
 
+// SetExcelByDecimalConfig 设置表格的小数位
+func SetExcelByDecimalConfig(tableData request.TableDataReq, config []request.DecimalConfig) request.TableDataReq {
+	excelData := tableData.Data
+	edbInfoIndex := make(map[int]int)
+	dateIndex := make(map[string]int)
+	for i, v := range excelData {
+		edbInfoIndex[v.EdbInfoId] = i
+	}
+	for i, v := range excelData[0].Data {
+		dateIndex[v.DataTime] = i
+	}
+	for _, conf := range config {
+		if conf.Col > 0 {
+			if v, ok := edbInfoIndex[conf.Col]; ok {
+				excelData[v].Decimal = conf.Decimal
+				for i := 0; i < len(excelData[v].Data); i++ {
+					excelData[v].Data[i].Decimal = conf.Decimal
+				}
+			}
+		}
+		if conf.Row != "" {
+			if v, ok := dateIndex[conf.Row]; ok {
+				for i := 0; i < len(excelData); i++ {
+					excelData[i].Data[v].Decimal = conf.Decimal
+				}
+			}
+		}
+	}
+	tableData.Data = excelData
+	tableData.DecimalConfig = config
+	return tableData
+}
+
 // GetExcelInfoOpButton 获取ETA表格的操作权限
 func GetExcelInfoOpButton(sysUser *system.Admin, belongUserId, source int, haveOperaAuth bool) (button excel.ExcelInfoDetailButton) {
 	// 如果没有数据权限,那么直接返回

+ 6 - 0
services/data/excel/time_table.go

@@ -27,6 +27,7 @@ type TableDataConfig struct {
 	ManualDate       []string                  `description:"手动配置的日期(未来的日期)"`
 	TableEdbInfoList []TableEdbInfo            `description:"表格内指标信息"`
 	TextRowData      [][]request.ManualDataReq `description:"文本列表"`
+	DecimalConfig    []request.DecimalConfig   `description:"小数位数配置"`
 }
 
 // TableEdbInfo
@@ -40,6 +41,7 @@ type TableEdbInfo struct {
 	Frequency    string `description:"频度"`
 	Unit         string `description:"单位"`
 	UnitEn       string `description:"英文单位"`
+	Decimal      int    `description:"小数位数"`
 }
 
 // ManualData
@@ -250,6 +252,7 @@ func GetDataByTableDataConfig(tableDataConfig TableDataConfig) (resultResp reque
 			EdbNameEn:    tableEdbInfo.EdbNameEn,
 			EdbAliasName: tableEdbInfo.EdbAliasName,
 			Frequency:    tableEdbInfo.Frequency,
+			Decimal:      tableEdbInfo.Decimal,
 			Unit:         tableEdbInfo.Unit,
 			UnitEn:       tableEdbInfo.UnitEn,
 			Data:         manualDataReqList,
@@ -278,6 +281,7 @@ func GetDataByTableDataConfig(tableDataConfig TableDataConfig) (resultResp reque
 				manualDataReqList = append(manualDataReqList, request.ManualDataReq{
 					DataType:            3,
 					DataTime:            tmpDateTimeStr,
+					Decimal:             tableEdbInfo.Decimal,
 					DataTimeType:        dataTimeType,
 					ShowValue:           "",
 					Value:               "",
@@ -291,6 +295,7 @@ func GetDataByTableDataConfig(tableDataConfig TableDataConfig) (resultResp reque
 				manualDataReqList = append(manualDataReqList, request.ManualDataReq{
 					DataType:            3,
 					DataTime:            tmpDateTimeStr,
+					Decimal:             tableEdbInfo.Decimal,
 					DataTimeType:        dataTimeType,
 					ShowValue:           "",
 					Value:               "",
@@ -375,6 +380,7 @@ func GetTableDataConfig(reqData request.TableDataReq) (tableDataConfig TableData
 			Frequency:    v.Frequency,
 			Unit:         v.Unit,
 			UnitEn:       v.UnitEn,
+			Decimal:      v.Decimal,
 		}
 		tableEdbInfoList = append(tableEdbInfoList, tmpTableEdbInfo)
 

+ 30 - 1
services/excel/lucky_sheet.go

@@ -6,6 +6,7 @@ import (
 	"eta/eta_api/models/data_manage/excel/request"
 	"eta/eta_api/utils"
 	"fmt"
+	"math"
 	"os"
 	"reflect"
 	"strconv"
@@ -1749,13 +1750,41 @@ func GetTableDataByMixedTableData(config [][]request.MixedTableCellDataReq, hide
 						utils.FileLog.Info("表格样式showStyle解析失败", err.Error())
 					}
 					showFormatValue := fmt.Sprintf("%v", cell.ShowFormatValue)
-					tmp.Monitor = showFormatValue
 					if styleConfig.BackgroundColor != "" {
 						tmp.Background = styleConfig.BackgroundColor
 					}
 					if styleConfig.Color != "" {
 						tmp.FontColor = styleConfig.Color
 					}
+					tmp.Monitor = 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)
 			}