|
@@ -73,7 +73,7 @@ func GetMixedTableCellData(mixedTableReq request.MixedTableReq) (newMixedTableCe
|
|
|
// 日度指标数据map
|
|
|
edbDayDataListMap := make(map[int]map[string]float64)
|
|
|
// 月度指标数据map
|
|
|
- edbMonthDataListMap := make(map[int]map[string]float64)
|
|
|
+ edbMonthDataListMap := make(map[int]map[string]string)
|
|
|
// 日度指标数据map
|
|
|
edbDataListMap := make(map[int][]*data_manage.EdbDataList)
|
|
|
for _, edbInfo := range edbInfoList {
|
|
@@ -90,18 +90,19 @@ func GetMixedTableCellData(mixedTableReq request.MixedTableReq) (newMixedTableCe
|
|
|
}
|
|
|
|
|
|
dateValMap := make(map[string]float64)
|
|
|
- monthValMap := make(map[string]float64)
|
|
|
+ monthDateMap := make(map[string]string)
|
|
|
for _, tmpData := range dataList {
|
|
|
// 日度数据
|
|
|
dateValMap[tmpData.DataTime] = tmpData.Value
|
|
|
// 月度数据(取该月份的第一个数据)
|
|
|
yearMonth := strings.Join(strings.Split(tmpData.DataTime, "-")[0:2], "-")
|
|
|
- if _, ok := monthValMap[yearMonth]; !ok {
|
|
|
- monthValMap[yearMonth] = tmpData.Value
|
|
|
+ if _, ok := monthDateMap[yearMonth]; !ok {
|
|
|
+ // 存最早的时间
|
|
|
+ monthDateMap[yearMonth] = tmpData.DataTime
|
|
|
}
|
|
|
}
|
|
|
edbDayDataListMap[edbInfo.EdbInfoId] = dateValMap
|
|
|
- edbMonthDataListMap[edbInfo.EdbInfoId] = monthValMap
|
|
|
+ edbMonthDataListMap[edbInfo.EdbInfoId] = monthDateMap
|
|
|
edbDataListMap[edbInfo.EdbInfoId] = dataList
|
|
|
}
|
|
|
|
|
@@ -119,9 +120,19 @@ func GetMixedTableCellData(mixedTableReq request.MixedTableReq) (newMixedTableCe
|
|
|
if err != nil {
|
|
|
return
|
|
|
}
|
|
|
- if edbInfo, ok := edbInfoMap[edbDateConfig.EdbInfoId]; ok {
|
|
|
- cell.ShowValue = edbInfo.EndDate
|
|
|
- cell.DataTime = edbInfo.EndDate
|
|
|
+ if dataList, ok := edbDataListMap[edbDateConfig.EdbInfoId]; ok {
|
|
|
+ // todo 获取配置信息,根据配置信息进行日期变换, 是否需要更新当前记录,将历史记录逐渐转换成新的记录
|
|
|
+ var newDate string
|
|
|
+ newDate, err = GetEdbDateByMoveForward(config[k][i].Value, dataList)
|
|
|
+ if err != nil {
|
|
|
+ return
|
|
|
+ }
|
|
|
+ newDate, err = HandleMixTableDateChange(newDate, config[k][i].Value)
|
|
|
+ if err != nil {
|
|
|
+ return
|
|
|
+ }
|
|
|
+ cell.ShowValue = newDate
|
|
|
+ cell.DataTime = newDate
|
|
|
config[k][i] = cell
|
|
|
}
|
|
|
}
|
|
@@ -133,7 +144,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)
|
|
@@ -141,6 +152,8 @@ 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 {
|
|
@@ -160,7 +173,7 @@ func GetMixedTableCellData(mixedTableReq request.MixedTableReq) (newMixedTableCe
|
|
|
//cell.Value = ``
|
|
|
|
|
|
// 日期关系配置不存在,则默认最新数据
|
|
|
- if relationConf, ok := cellRelationConfMap[cell.Uid]; ok {
|
|
|
+ if relationConf, ok := cellRelationConfMap[cell.Uid]; ok { //表示表格日期
|
|
|
if relationConf.RelationDate.Key == `` {
|
|
|
// 日期关系配置未绑定
|
|
|
continue
|
|
@@ -178,11 +191,21 @@ func GetMixedTableCellData(mixedTableReq request.MixedTableReq) (newMixedTableCe
|
|
|
if dateValMap, ok := edbDayDataListMap[cell.EdbInfoId]; ok {
|
|
|
tmpDateValMap = dateValMap
|
|
|
}
|
|
|
+ // todo 根据配置进行日期变换
|
|
|
+ relationDate := relationCell.DataTime
|
|
|
+ if strings.Contains(cell.Value, "{") {
|
|
|
+ relationDate, err = HandleMixTableDateChange(relationDate, cell.Value)
|
|
|
+ if err != nil {
|
|
|
+ return
|
|
|
+ }
|
|
|
+ } else {
|
|
|
+ cell.Value = ""
|
|
|
+ }
|
|
|
|
|
|
- if val, ok2 := tmpDateValMap[relationCell.DataTime]; ok2 {
|
|
|
+ if val, ok2 := tmpDateValMap[relationDate]; ok2 {
|
|
|
//cell.ShowValue = fmt.Sprint(val)
|
|
|
cellKeyVal[cell.Uid] = val
|
|
|
- cell.ShowValue = utils.FormatTableDataShowValue(val)
|
|
|
+ cell.ShowValue = utils.FormatMixTableDataShowValue(val)
|
|
|
}
|
|
|
} else {
|
|
|
// 如果不是取得一个关联的日期,那么就是指定日期
|
|
@@ -192,38 +215,67 @@ func GetMixedTableCellData(mixedTableReq request.MixedTableReq) (newMixedTableCe
|
|
|
if dateValList, ok := edbDataListMap[cell.EdbInfoId]; ok {
|
|
|
tmpLenData := len(dateValList)
|
|
|
if tmpLenData > 0 {
|
|
|
- cellKeyVal[cell.Uid] = dateValList[tmpLenData-1].Value
|
|
|
- cell.ShowValue = utils.FormatTableDataShowValue(dateValList[tmpLenData-1].Value)
|
|
|
+ //做期数前移动和日期变换
|
|
|
+ if !strings.Contains(cell.Value, "{") {
|
|
|
+ cell.Value = ""
|
|
|
+ }
|
|
|
+ var newDate string
|
|
|
+ newDate, err = GetEdbDateByMoveForward(cell.Value, dateValList)
|
|
|
+ if err != nil {
|
|
|
+ return
|
|
|
+ }
|
|
|
+ newDate, err = HandleMixTableDateChange(newDate, cell.Value)
|
|
|
+ if err != nil {
|
|
|
+ return
|
|
|
+ }
|
|
|
+ var finalVal string
|
|
|
+ for _, v := range dateValList {
|
|
|
+ if v.DataTime == newDate {
|
|
|
+ finalVal = utils.FormatMixTableDataShowValue(v.Value)
|
|
|
+ cellKeyVal[cell.Uid] = v.Value
|
|
|
+ break
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+ cell.ShowValue = finalVal
|
|
|
}
|
|
|
}
|
|
|
} else {
|
|
|
tmpDateList := strings.Split(cell.DataTime, "-")
|
|
|
tmpDateValMap := make(map[string]float64)
|
|
|
+ var newDate string
|
|
|
if len(tmpDateList) == 2 {
|
|
|
//月度数据
|
|
|
- if dateValMap, ok := edbMonthDataListMap[cell.EdbInfoId]; ok {
|
|
|
- tmpDateValMap = dateValMap
|
|
|
+ if dateMap, ok1 := edbMonthDataListMap[cell.EdbInfoId]; ok1 {
|
|
|
+ if d, ok2 := dateMap[cell.DataTime]; ok2 {
|
|
|
+ newDate = d
|
|
|
+ }
|
|
|
}
|
|
|
} else {
|
|
|
// 日度数据
|
|
|
- if dateValMap, ok := edbDayDataListMap[cell.EdbInfoId]; ok {
|
|
|
- tmpDateValMap = dateValMap
|
|
|
- }
|
|
|
+ newDate = cell.DataTime
|
|
|
+ }
|
|
|
+ // 日期变换后才能确定最后的时间
|
|
|
+ //做期数前移动和日期变换
|
|
|
+ if !strings.Contains(cell.Value, "{") {
|
|
|
+ cell.Value = ""
|
|
|
+ }
|
|
|
|
|
|
+ newDate, err = HandleMixTableDateChange(newDate, cell.Value)
|
|
|
+ if err != nil {
|
|
|
+ return
|
|
|
}
|
|
|
- if val, ok2 := tmpDateValMap[cell.DataTime]; ok2 {
|
|
|
- //cell.ShowValue = fmt.Sprint(val)
|
|
|
- cellKeyVal[cell.Uid] = val
|
|
|
- cell.ShowValue = utils.FormatTableDataShowValue(val)
|
|
|
+
|
|
|
+ if dateValMap, ok3 := edbDayDataListMap[cell.EdbInfoId]; ok3 {
|
|
|
+ tmpDateValMap = dateValMap
|
|
|
+ if val, ok2 := tmpDateValMap[cell.DataTime]; ok2 {
|
|
|
+ //cell.ShowValue = fmt.Sprint(val)
|
|
|
+ cellKeyVal[cell.Uid] = val
|
|
|
+ cell.ShowValue = utils.FormatMixTableDataShowValue(val)
|
|
|
+ }
|
|
|
}
|
|
|
}
|
|
|
}
|
|
|
-
|
|
|
- calculateCellMap[cell.Uid] = Cell{
|
|
|
- Column: k,
|
|
|
- Row: i,
|
|
|
- CellInfo: cell,
|
|
|
- }
|
|
|
case request.CustomTextDT: //自定义文本
|
|
|
if cell.Value == `` {
|
|
|
continue
|
|
@@ -233,27 +285,21 @@ func GetMixedTableCellData(mixedTableReq request.MixedTableReq) (newMixedTableCe
|
|
|
if tmpErr == nil {
|
|
|
tmpVal, _ := tmpDeci.Float64()
|
|
|
cellKeyVal[cell.Uid] = tmpVal
|
|
|
-
|
|
|
- calculateCellMap[cell.Uid] = Cell{
|
|
|
- Column: k,
|
|
|
- Row: i,
|
|
|
- CellInfo: cell,
|
|
|
- }
|
|
|
}
|
|
|
|
|
|
case request.FormulateCalculateDataDT: // 公式计算(A+B这种)
|
|
|
- calculateCellMap[cell.Uid] = Cell{
|
|
|
- Column: k,
|
|
|
- Row: i,
|
|
|
- CellInfo: cell,
|
|
|
- }
|
|
|
-
|
|
|
calculateChainList = append(calculateChainList, cell.Uid)
|
|
|
|
|
|
case request.InsertEdbCalculateDataDT: // 插入指标系统计算公式生成的值
|
|
|
+ // 处理value的值
|
|
|
+ if !strings.Contains(cell.Value, "EdbInfoId") && cell.EdbInfoId > 0 {
|
|
|
+ cell.Value, _ = fixCalculateValueConfig(cell.Value, cell.EdbInfoId)
|
|
|
+ row[i] = cell
|
|
|
+ }
|
|
|
// 日期
|
|
|
var cellDateTime string
|
|
|
// 日期关系配置不存在,则默认最新数据
|
|
|
+ // 从绑定的单元格中获取数据的日期
|
|
|
if relationConf, ok := cellRelationConfMap[cell.Uid]; ok {
|
|
|
if relationConf.RelationDate.Key == `` {
|
|
|
// 日期关系配置未绑定
|
|
@@ -271,7 +317,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
|
|
@@ -318,22 +364,59 @@ 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]
|
|
|
+ edbSourceDataMap[key] = respItem.Data
|
|
|
+ respItemData = 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
|
|
|
}
|
|
|
}
|
|
|
+ // 进行日期变换
|
|
|
+ cellDateTime, err = HandleMixTableDateChange(cellDateTime, cell.Value)
|
|
|
+ if err != nil {
|
|
|
+ utils.FileLog.Error(fmt.Sprintf("日期变换失败,配置信息;%s, 日期:%s", cell.Value, cellDateTime))
|
|
|
+ continue
|
|
|
+ }
|
|
|
+ val, ok := tmpDataMap[cellDateTime]
|
|
|
+ if ok {
|
|
|
+ cellKeyVal[cell.Uid] = val
|
|
|
+ cell.ShowValue = utils.FormatMixTableDataShowValue(val)
|
|
|
+ } else {
|
|
|
+ cell.ShowValue = ""
|
|
|
+ }
|
|
|
|
|
|
- val := tmpDataMap[cellDateTime]
|
|
|
- cellKeyVal[cell.Uid] = val
|
|
|
- cell.ShowValue = utils.FormatTableDataShowValue(val)
|
|
|
+ case request.DateCalculateDataDT: //日期计算
|
|
|
+ dateCalculateList = append(dateCalculateList, cell.Uid)
|
|
|
+ // 遍历数组,根据公式进行计算,并将得到的结果放到对应的单元格中
|
|
|
}
|
|
|
+ row[i] = cell
|
|
|
+ }
|
|
|
+ config[k] = row
|
|
|
+ }
|
|
|
|
|
|
+ // 处理指定指标的日期
|
|
|
+ for k, row := range config {
|
|
|
+ for i, cell := range row {
|
|
|
+ calculateCellMap[cell.Uid] = Cell{
|
|
|
+ Column: k,
|
|
|
+ Row: i,
|
|
|
+ CellInfo: cell,
|
|
|
+ }
|
|
|
+ cell.ShowFormatValue = cell.ShowValue
|
|
|
+ if cell.ShowStyle != `` {
|
|
|
+ showStyleList = append(showStyleList, cell.Uid)
|
|
|
+ }
|
|
|
row[i] = cell
|
|
|
+ cellDataRelationMap[cell.Uid] = cell
|
|
|
}
|
|
|
config[k] = row
|
|
|
}
|
|
@@ -364,12 +447,24 @@ func GetMixedTableCellData(mixedTableReq request.MixedTableReq) (newMixedTableCe
|
|
|
}
|
|
|
|
|
|
cellKeyVal[cell.Uid] = val
|
|
|
- cell.ShowValue = utils.FormatTableDataShowValue(val)
|
|
|
+ cell.ShowValue = utils.FormatMixTableDataShowValue(val)
|
|
|
config[cellPosition.Column][cellPosition.Row] = cell
|
|
|
|
|
|
}
|
|
|
}
|
|
|
|
|
|
+ // 日期计算
|
|
|
+ config, err, errMsg = handlerDateCalculate(dateCalculateList, calculateCellMap, config)
|
|
|
+ if err != nil {
|
|
|
+ return
|
|
|
+ }
|
|
|
+
|
|
|
+ // 格式化展示
|
|
|
+ config, err, errMsg = handleMixCellShowStyle(showStyleList, calculateCellMap, config)
|
|
|
+ if err != nil {
|
|
|
+ return
|
|
|
+ }
|
|
|
+
|
|
|
newMixedTableCellDataList = config
|
|
|
|
|
|
return
|
|
@@ -475,11 +570,15 @@ func handleConfig(configList [][]request.MixedTableCellDataReq) (newConfig [][]r
|
|
|
if err != nil {
|
|
|
return
|
|
|
}
|
|
|
- edbInfoIdList = append(edbInfoIdList, config.EdbInfoId)
|
|
|
+ if config.EdbInfoId == 0 && cell.EdbInfoId > 0 {
|
|
|
+ edbInfoIdList = append(edbInfoIdList, cell.EdbInfoId)
|
|
|
+ } else {
|
|
|
+ edbInfoIdList = append(edbInfoIdList, config.EdbInfoId)
|
|
|
+ }
|
|
|
dataEdbInfoIdList = append(dataEdbInfoIdList, cell.EdbInfoId)
|
|
|
|
|
|
case request.DateDT: // 日期类型
|
|
|
- date, tmpErr, tmpErrMsg := handleDate(cell.DataTimeType, cell.Value)
|
|
|
+ date, newVal, tmpErr, tmpErrMsg := handleDate(cell.DataTimeType, cell.Value)
|
|
|
if tmpErr != nil {
|
|
|
err = tmpErr
|
|
|
errMsg = tmpErrMsg
|
|
@@ -487,6 +586,7 @@ func handleConfig(configList [][]request.MixedTableCellDataReq) (newConfig [][]r
|
|
|
}
|
|
|
rowList[rk].DataTime = date
|
|
|
rowList[rk].ShowValue = date
|
|
|
+ rowList[rk].Value = newVal //兼容原有的历史数据中系统导入日期
|
|
|
|
|
|
// 指标日期类型的单元格需要额外将指标id取出来
|
|
|
if cell.DataTimeType == request.EdbDateDT {
|
|
@@ -516,7 +616,7 @@ func handleConfig(configList [][]request.MixedTableCellDataReq) (newConfig [][]r
|
|
|
// @return date string
|
|
|
// @return err error
|
|
|
// @return errMsg string
|
|
|
-func HandleDate(dataTimeType int, val string) (date string, err error, errMsg string) {
|
|
|
+func HandleDate(dataTimeType int, val string) (date string, newVal string, err error, errMsg string) {
|
|
|
return handleDate(dataTimeType, val)
|
|
|
}
|
|
|
|
|
@@ -529,17 +629,39 @@ func HandleDate(dataTimeType int, val string) (date string, err error, errMsg st
|
|
|
// @return date string
|
|
|
// @return err error
|
|
|
// @return errMsg string
|
|
|
-func handleDate(dataTimeType int, val string) (date string, err error, errMsg string) {
|
|
|
+func handleDate(dataTimeType int, val string) (date string, newVal string, err error, errMsg string) {
|
|
|
+ newVal = val
|
|
|
if val == `` {
|
|
|
errMsg = "错误的日期数据"
|
|
|
err = errors.New(errMsg)
|
|
|
return
|
|
|
}
|
|
|
+
|
|
|
switch dataTimeType {
|
|
|
case request.CustomDateT: //手动输入日期
|
|
|
+ /*if !strings.Contains(val, "{") {
|
|
|
+ newVal, err, errMsg = handleOldCustomerDateT(val)
|
|
|
+ if err != nil {
|
|
|
+ return
|
|
|
+ }
|
|
|
+ }
|
|
|
+ date, err = HandleMixTableDateChange("", newVal)
|
|
|
+ if err != nil {
|
|
|
+ return
|
|
|
+ }*/
|
|
|
date = val
|
|
|
+ return
|
|
|
case request.SystemDateT: // 系统日期
|
|
|
- date, err, errMsg = handleSystemDateT(val)
|
|
|
+ date = time.Now().Format(utils.FormatDate)
|
|
|
+ newVal, err, errMsg = handleOldSystemDateT(val)
|
|
|
+ if err != nil {
|
|
|
+ return
|
|
|
+ }
|
|
|
+ date, err = HandleMixTableDateChange(date, newVal)
|
|
|
+ if err != nil {
|
|
|
+ return
|
|
|
+ }
|
|
|
+
|
|
|
case request.EdbDateDT: // 导入指标日期(指标库的最新日期)
|
|
|
default:
|
|
|
errMsg = "错误的日期类型"
|
|
@@ -550,30 +672,134 @@ func handleDate(dataTimeType int, val string) (date string, err error, errMsg st
|
|
|
return
|
|
|
}
|
|
|
|
|
|
-// handleSystemDateT
|
|
|
-// @Description: 处理导入系统日期
|
|
|
+func GetEdbDateByMoveForward(conf string, edbDataList []*data_manage.EdbDataList) (date string, err error) {
|
|
|
+ dateList := make([]string, 0)
|
|
|
+ for _, v := range edbDataList {
|
|
|
+ dateList = append(dateList, v.DataTime)
|
|
|
+ }
|
|
|
+
|
|
|
+ date, err = GetEdbDateByMoveForwardByDateList(conf, dateList)
|
|
|
+ return
|
|
|
+}
|
|
|
+
|
|
|
+func GetEdbDateByMoveForwardByDateList(conf string, dateList []string) (date string, err error) {
|
|
|
+ moveForward := 0
|
|
|
+ if conf != "" {
|
|
|
+ var edbDateConf request.EdbDateChangeConf
|
|
|
+ err = json.Unmarshal([]byte(conf), &edbDateConf)
|
|
|
+ if err != nil {
|
|
|
+ err = fmt.Errorf("日期变换配置json解析失败失败: %s", err.Error())
|
|
|
+ return
|
|
|
+ }
|
|
|
+ moveForward = edbDateConf.MoveForward
|
|
|
+ }
|
|
|
+ // 根据日期进行排序
|
|
|
+ index := len(dateList) - 1 - moveForward
|
|
|
+ for k, v := range dateList {
|
|
|
+ if k == index {
|
|
|
+ date = v
|
|
|
+ return
|
|
|
+ }
|
|
|
+ }
|
|
|
+ return
|
|
|
+}
|
|
|
+
|
|
|
+// HandleMixTableDateChange 处理表格中的日期变换
|
|
|
+func HandleMixTableDateChange(date, conf string) (newDate string, err error) {
|
|
|
+ newDate = date
|
|
|
+ if conf == "" {
|
|
|
+ return
|
|
|
+ }
|
|
|
+ var edbDateConf request.EdbDateConf
|
|
|
+ err = json.Unmarshal([]byte(conf), &edbDateConf)
|
|
|
+ if err != nil {
|
|
|
+ err = fmt.Errorf("日期变换配置json解析失败失败: %s, Err:%s", conf, err.Error())
|
|
|
+ return
|
|
|
+ }
|
|
|
+
|
|
|
+ if newDate != "" {
|
|
|
+ if len(edbDateConf.DateChange) > 0 {
|
|
|
+ var dateTime time.Time
|
|
|
+ dateTime, err = time.ParseInLocation(utils.FormatDate, newDate, time.Local)
|
|
|
+ if err != nil {
|
|
|
+ err = fmt.Errorf("日期解析失败: %s", err.Error())
|
|
|
+ return
|
|
|
+ }
|
|
|
+ for _, v := range edbDateConf.DateChange {
|
|
|
+ if v.ChangeType == 1 {
|
|
|
+ dateTime = dateTime.AddDate(v.Year, v.Month, v.Day)
|
|
|
+ newDate = dateTime.Format(utils.FormatDate)
|
|
|
+ } else if v.ChangeType == 2 {
|
|
|
+ newDate, err, _ = handleSystemAppointDateT(dateTime, v.FrequencyDay, v.Frequency)
|
|
|
+ if err != nil {
|
|
|
+ return
|
|
|
+ }
|
|
|
+ dateTime, err = time.ParseInLocation(utils.FormatDate, newDate, time.Local)
|
|
|
+ if err != nil {
|
|
|
+ err = fmt.Errorf("日期解析失败: %s", err.Error())
|
|
|
+ return
|
|
|
+ }
|
|
|
+ }
|
|
|
+ }
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+ return
|
|
|
+}
|
|
|
+
|
|
|
+// handleOldSystemDateT
|
|
|
+// @Description: 历史数据中的导入系统日期
|
|
|
// @author: Roc
|
|
|
// @datetime2023-10-27 09:36:21
|
|
|
// @param confStr string
|
|
|
// @return date string
|
|
|
// @return err error
|
|
|
// @return errMsg string
|
|
|
-func handleSystemDateT(confStr string) (date string, err error, errMsg string) {
|
|
|
+func handleOldSystemDateT(confStr string) (newConf string, err error, errMsg string) {
|
|
|
+ newConf = confStr
|
|
|
var config request.SystemDateConf
|
|
|
err = json.Unmarshal([]byte(confStr), &config)
|
|
|
if err != nil {
|
|
|
return
|
|
|
}
|
|
|
+
|
|
|
+ newConfig := new(request.EdbDateConf)
|
|
|
+ dateChange := new(request.EdbDateConfDateChange)
|
|
|
+
|
|
|
+ dateChangeList := make([]*request.EdbDateConfDateChange, 0)
|
|
|
+
|
|
|
switch config.Source {
|
|
|
case request.SystemCurrDateT:
|
|
|
- date = time.Now().Format(utils.FormatDate)
|
|
|
+ return
|
|
|
case request.SystemCalculateDateT:
|
|
|
- date, err, errMsg = handleSystemCalculateDateT(config.CalculateNum, config.CalculateFrequency)
|
|
|
+ // todo 是否直接更新该excel记录,
|
|
|
+ dateChange.Day = config.CalculateNum
|
|
|
+ dateChange.ChangeType = 1
|
|
|
+ dateChangeList = append(dateChangeList, dateChange)
|
|
|
+ newConfig.DateChange = dateChangeList
|
|
|
+ newConfByte, e := json.Marshal(newConfig)
|
|
|
+ if e != nil {
|
|
|
+ err = fmt.Errorf("日期计算额外配置,json序列化失败: %s", e.Error())
|
|
|
+ return
|
|
|
+ }
|
|
|
+ newConf = string(newConfByte)
|
|
|
+ return
|
|
|
case request.SystemFrequencyDateT: // 处理系统日期相关的指定频率(所在周/旬/月/季/半年/年的最后/最早一天)
|
|
|
- date, err, errMsg = handleSystemAppointDateT(config.Day, config.Frequency)
|
|
|
+ dateChange.FrequencyDay = config.Day
|
|
|
+ dateChange.Frequency = config.Frequency
|
|
|
+ dateChange.ChangeType = 1
|
|
|
+ dateChangeList = append(dateChangeList, dateChange)
|
|
|
+ newConfig.DateChange = dateChangeList
|
|
|
+ newConfByte, e := json.Marshal(newConfig)
|
|
|
+ if e != nil {
|
|
|
+ err = fmt.Errorf("日期计算额外配置,json序列化失败: %s", e.Error())
|
|
|
+ return
|
|
|
+ }
|
|
|
+ newConf = string(newConfByte)
|
|
|
+ return
|
|
|
default:
|
|
|
- errMsg = "错误的日期日期导入方式"
|
|
|
- err = errors.New(fmt.Sprint("错误的日期日期导入方式:", config.Source))
|
|
|
+ //errMsg = "错误的日期日期导入方式"
|
|
|
+ //err = errors.New(fmt.Sprint("错误的日期日期导入方式:", config.Source))
|
|
|
return
|
|
|
}
|
|
|
|
|
@@ -615,8 +841,8 @@ func handleSystemCalculateDateT(num int, frequency string) (date string, err err
|
|
|
// @return date string
|
|
|
// @return err error
|
|
|
// @return errMsg string
|
|
|
-func handleSystemAppointDateT(appointDay, frequency string) (date string, err error, errMsg string) {
|
|
|
- currDate := time.Now()
|
|
|
+func handleSystemAppointDateT(currDate time.Time, appointDay, frequency string) (date string, err error, errMsg string) {
|
|
|
+ //currDate := time.Now()
|
|
|
switch frequency {
|
|
|
case "本周":
|
|
|
day := int(currDate.Weekday())
|
|
@@ -813,3 +1039,229 @@ func calculateByCellList(calculateFormula string, tagList []utils.CellPosition)
|
|
|
|
|
|
return
|
|
|
}
|
|
|
+
|
|
|
+// handlerDateCalculate 处理日期计算
|
|
|
+func handlerDateCalculate(dateCalculateList []string, calculateCellMap map[string]Cell, oldConfig [][]request.MixedTableCellDataReq) (config [][]request.MixedTableCellDataReq, err error, errMsg string) {
|
|
|
+ config = oldConfig
|
|
|
+ if len(dateCalculateList) == 0 {
|
|
|
+ return
|
|
|
+ }
|
|
|
+ if len(dateCalculateList) > 0 {
|
|
|
+ for _, cellKey := range dateCalculateList {
|
|
|
+ // 查找这个单元格的位置,直接map找了,而不是遍历整个单元格
|
|
|
+ cellPosition, ok := calculateCellMap[cellKey]
|
|
|
+ if !ok {
|
|
|
+ utils.FileLog.Error("找不到单元格位置:", cellKey)
|
|
|
+ continue
|
|
|
+ }
|
|
|
+
|
|
|
+ cell := config[cellPosition.Column][cellPosition.Row]
|
|
|
+ if cell.DataType != request.DateCalculateDataDT { // 判断公式计算(A+B这种)类型,不是的话也过滤了
|
|
|
+ continue
|
|
|
+ }
|
|
|
+
|
|
|
+ val, tmpErr, tmpErrMsg := DateCalculatePrepare(calculateCellMap, cell.Value)
|
|
|
+ if tmpErr != nil {
|
|
|
+ errMsg = tmpErrMsg
|
|
|
+ err = tmpErr
|
|
|
+ return
|
|
|
+ }
|
|
|
+
|
|
|
+ cell.ShowValue = utils.FormatMixTableDataShowValue(val)
|
|
|
+ config[cellPosition.Column][cellPosition.Row] = cell
|
|
|
+ }
|
|
|
+ }
|
|
|
+ return
|
|
|
+}
|
|
|
+
|
|
|
+// DateCalculatePrepare 单个单元格的日期计算
|
|
|
+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 {
|
|
|
+ err = fmt.Errorf("日期计算配置json解析失败失败: %s, Err:%s", config, err.Error())
|
|
|
+ return
|
|
|
+ }
|
|
|
+ if len(edbDateConf.RelationCellList) == 0 {
|
|
|
+ err = fmt.Errorf("日期计算 未配置日期单元格失败: %s", config)
|
|
|
+ return
|
|
|
+ }
|
|
|
+ valMap := make(map[string]int)
|
|
|
+ for _, v := range edbDateConf.RelationCellList {
|
|
|
+ // 查找单元格数据
|
|
|
+ cell, ok := calculateCellMap[v.Uid]
|
|
|
+ if !ok {
|
|
|
+ err = fmt.Errorf("查找单元格:%s 的数据失败", v.Uid)
|
|
|
+ return
|
|
|
+ }
|
|
|
+ colData := cell.CellInfo
|
|
|
+
|
|
|
+ // 如果不是基础计算单元格,直接返回
|
|
|
+ _, err = time.ParseInLocation(utils.FormatDate, colData.ShowValue, time.Local)
|
|
|
+ if err != nil {
|
|
|
+ err = fmt.Errorf("%s 的单元格非日期类型, Err: %s", colData.ShowValue, err.Error())
|
|
|
+ return
|
|
|
+ }
|
|
|
+ // todo 把日期转换成excel里的天数
|
|
|
+ realDiffDay := utils.GetDaysDiff1900(colData.ShowValue)
|
|
|
+
|
|
|
+ valMap[strings.ToUpper(v.Tag)] = realDiffDay
|
|
|
+ }
|
|
|
+
|
|
|
+ // 计算
|
|
|
+ val, errMsg, err = DateCalculateFormula(valMap, strings.ToUpper(edbDateConf.Formula))
|
|
|
+ if err != nil {
|
|
|
+ return
|
|
|
+ }
|
|
|
+ return
|
|
|
+}
|
|
|
+
|
|
|
+func DateCalculateFormula(valTagMap map[string]int, calculateFormula string) (calVal float64, errMsg string, err error) {
|
|
|
+ if calculateFormula == "" {
|
|
|
+ errMsg = "公式异常"
|
|
|
+ err = errors.New(errMsg)
|
|
|
+ return
|
|
|
+ }
|
|
|
+
|
|
|
+ calculateFormula = strings.TrimPrefix(calculateFormula, "=")
|
|
|
+ calculateFormula = strings.Replace(calculateFormula, "(", "(", -1)
|
|
|
+ calculateFormula = strings.Replace(calculateFormula, ")", ")", -1)
|
|
|
+ calculateFormula = strings.Replace(calculateFormula, ",", ",", -1)
|
|
|
+ calculateFormula = strings.Replace(calculateFormula, "。", ".", -1)
|
|
|
+ calculateFormula = strings.Replace(calculateFormula, "%", "*0.01", -1)
|
|
|
+
|
|
|
+ formulaFormStr := utils.ReplaceFormulaByTagMap(valTagMap, calculateFormula)
|
|
|
+
|
|
|
+ if formulaFormStr == `` {
|
|
|
+ errMsg = "公式异常"
|
|
|
+ err = errors.New(errMsg)
|
|
|
+ return
|
|
|
+ }
|
|
|
+ fmt.Println("公式:" + formulaFormStr)
|
|
|
+ expression := formula.NewExpression(formulaFormStr)
|
|
|
+ calResult, err := expression.Evaluate()
|
|
|
+ if err != nil {
|
|
|
+ errMsg = "公式错误,请重新填写"
|
|
|
+ err = errors.New("计算失败:Err:" + err.Error() + ";formulaStr:" + formulaFormStr)
|
|
|
+ // 分母为0的报错
|
|
|
+ if strings.Contains(err.Error(), "divide by zero") {
|
|
|
+ errMsg = "分母不能为0"
|
|
|
+ err = errors.New("分母不能为空,计算公式:" + formulaFormStr)
|
|
|
+ }
|
|
|
+ return
|
|
|
+ }
|
|
|
+ // 如果计算结果是NAN,那么就提示报错
|
|
|
+ if calResult.IsNan() {
|
|
|
+ errMsg = "公式错误,请重新填写"
|
|
|
+ err = errors.New("计算失败:计算结果是:NAN;formulaStr:" + formulaFormStr)
|
|
|
+ return
|
|
|
+ }
|
|
|
+ calVal, err = calResult.Float64()
|
|
|
+ if err != nil {
|
|
|
+ return
|
|
|
+ }
|
|
|
+
|
|
|
+ // 转Decimal然后四舍五入
|
|
|
+ calVal, _ = decimal.NewFromFloat(calVal).Round(4).Float64()
|
|
|
+
|
|
|
+ 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]
|
|
|
+ val := cell.ShowValue
|
|
|
+ isPercent := false
|
|
|
+ if strings.Contains(val, "%") {
|
|
|
+ isPercent = true
|
|
|
+ val = strings.Trim(val, "%")
|
|
|
+ }
|
|
|
+ _, e := strconv.ParseFloat(val, 64) // 将字符串转换成float类型
|
|
|
+ if e != nil { // 如果没有错误发生则返回true,说明该字符串是一个合法的数字
|
|
|
+ continue
|
|
|
+ }
|
|
|
+ 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
|
|
|
+ }
|
|
|
+ if styleConf.Pn != 0 || styleConf.Nt != "" {
|
|
|
+ val = changePointDecimalPlaces(val, styleConf.Pn, styleConf.Nt, isPercent)
|
|
|
+ cell.ShowFormatValue = val
|
|
|
+ } else {
|
|
|
+ cell.ShowFormatValue = cell.ShowValue
|
|
|
+ }
|
|
|
+ config[cellPosition.Column][cellPosition.Row] = cell
|
|
|
+ }
|
|
|
+ }
|
|
|
+ return
|
|
|
+}
|
|
|
+
|
|
|
+// changePointDecimalPlaces 小数点位数加减和百分比格式
|
|
|
+func changePointDecimalPlaces(str string, changeNum int, numberType string, isPercent bool) (newStr string) {
|
|
|
+ newStr = str
|
|
|
+ // 把字符串转成浮点数
|
|
|
+ val, _ := strconv.ParseFloat(str, 64)
|
|
|
+ if isPercent {
|
|
|
+ if numberType == "number" { //百分数转成小数
|
|
|
+ val = val / 100
|
|
|
+ isPercent = false
|
|
|
+ }
|
|
|
+ } else {
|
|
|
+ if numberType == "percent" {
|
|
|
+ val = val * 100
|
|
|
+ }
|
|
|
+ }
|
|
|
+ newStr = fmt.Sprintf("%v", val)
|
|
|
+ var decimalPlaces int // 计算小数位数
|
|
|
+ dotIndex := strings.Index(newStr, ".") // 查找小数点的位置
|
|
|
+ if dotIndex == -1 {
|
|
|
+ decimalPlaces = 0
|
|
|
+ } else {
|
|
|
+ decimalPlaces = len(newStr) - dotIndex - 1
|
|
|
+ }
|
|
|
+ decimalPlaces += changeNum
|
|
|
+
|
|
|
+ if decimalPlaces < 0 {
|
|
|
+ decimalPlaces = 0
|
|
|
+ }
|
|
|
+ val, _ = decimal.NewFromFloat(val).Round(int32(decimalPlaces)).Float64()
|
|
|
+ newStr = strconv.FormatFloat(val, 'f', decimalPlaces, 64)
|
|
|
+ if numberType == "percent" || isPercent {
|
|
|
+ newStr += "%"
|
|
|
+ }
|
|
|
+ return
|
|
|
+}
|
|
|
+
|
|
|
+func fixCalculateValueConfig(conf string, edbInfoId int) (newConf string, err error) {
|
|
|
+ newConf = conf
|
|
|
+ if edbInfoId == 0 {
|
|
|
+ return
|
|
|
+ }
|
|
|
+ var tmpConfig request.CalculateConf
|
|
|
+ err = json.Unmarshal([]byte(conf), &tmpConfig)
|
|
|
+ if err != nil {
|
|
|
+ return
|
|
|
+ }
|
|
|
+ if tmpConfig.EdbInfoId == 0 {
|
|
|
+ tmpConfig.EdbInfoId = edbInfoId
|
|
|
+ newConfByte, _ := json.Marshal(tmpConfig)
|
|
|
+ newConf = string(newConfByte)
|
|
|
+ return
|
|
|
+ }
|
|
|
+ return
|
|
|
+}
|