Browse Source

面积图 空值填充 前值填充

gmy 5 days ago
parent
commit
fec6bcd203

+ 27 - 7
controllers/data_manage/chart_info.go

@@ -1403,7 +1403,12 @@ func (this *ChartInfoController) PreviewChartInfoDetail() {
 
 	// 面积图 面积堆积 数据处理
 	if req.ChartType == utils.CHART_TYPE_AREA {
-		fillAreaGraphData(extraConfigStr, edbList)
+		err, errMsg = fillAreaGraphData(extraConfigStr, edbList)
+		if err != nil {
+			br.Msg = "面积图处理失败"
+			br.ErrMsg = errMsg
+			return
+		}
 	}
 
 	//图表操作权限
@@ -1498,11 +1503,11 @@ func (this *ChartInfoController) PreviewChartInfoDetail() {
 	br.Data = resp
 }
 
-func fillAreaGraphData(extraConfigStr string, edbDataList []*data_manage.ChartEdbInfoMapping) (edbList []*data_manage.ChartEdbInfoMapping, err error, errMsg string) {
+func fillAreaGraphData(extraConfigStr string, edbDataList []*data_manage.ChartEdbInfoMapping) (err error, errMsg string) {
 
 	var tmpConfig data_manage.AreaExtraConf
 	if extraConfigStr != `` {
-		err := json.Unmarshal([]byte(extraConfigStr), &tmpConfig)
+		err = json.Unmarshal([]byte(extraConfigStr), &tmpConfig)
 		if err != nil {
 			errMsg = "面积图配置异常"
 			err = errors.New(errMsg)
@@ -1511,14 +1516,18 @@ func fillAreaGraphData(extraConfigStr string, edbDataList []*data_manage.ChartEd
 		if tmpConfig.StandardEdbInfoId <= 0 {
 			errMsg = "面积图配置异常"
 			err = errors.New(errMsg)
+			return
 		}
 	}
 	if tmpConfig.IsHeap == 1 {
 		standardIndexMap := make(map[string]*data_manage.EdbDataList)
+		var startDate, endDate string
 		for _, v := range edbDataList {
 			// 判断是否为基准指标
 			if v.EdbInfoId == tmpConfig.StandardEdbInfoId {
 				if dataList, ok := v.DataList.([]*data_manage.EdbDataList); ok {
+					startDate = dataList[0].DataTime
+					endDate = dataList[len(dataList)-1].DataTime
 					for _, dataObject := range dataList {
 						standardIndexMap[dataObject.DataTime] = dataObject
 					}
@@ -1528,15 +1537,15 @@ func fillAreaGraphData(extraConfigStr string, edbDataList []*data_manage.ChartEd
 		}
 		strategy, err := area_graph.CreateStrategy(tmpConfig.NullDealWay)
 		if err != nil {
-			return nil, err, "创建空值处理器失败"
+			return err, "创建空值处理器失败"
 		}
-		err = strategy.Deal(tmpConfig, edbDataList, standardIndexMap)
+		err = strategy.Deal(tmpConfig, edbDataList, standardIndexMap, startDate, endDate)
 		if err != nil {
-			return nil, err, err.Error()
+			return err, err.Error()
 		}
 	}
 
-	return nil, nil, ""
+	return nil, ""
 }
 
 // ChartInfoDetailV2
@@ -2864,6 +2873,17 @@ func GetChartInfoDetailFromUniqueCode(chartInfo *data_manage.ChartInfoView, isCa
 		chartInfo.ChartClassify = append(chartInfo.ChartClassify, chartClassifyParent)
 	}
 	chartInfo.ChartClassify = append(chartInfo.ChartClassify, chartViewClassify)
+
+	// 面积图 面积堆积 数据处理
+	if chartType == utils.CHART_TYPE_AREA {
+		err, errMsg = fillAreaGraphData(extraConfigStr, edbList)
+		if err != nil {
+			msg = "获取失败"
+			errMsg = "获取面积图数据失败,Err:" + err.Error()
+			return
+		}
+	}
+
 	resp.EdbInfoList = edbList
 	//判断是否需要展示英文标识
 	chartInfo.IsEnChart = data.CheckIsEnChart(chartInfo.ChartNameEn, edbList, chartInfo.Source, chartInfo.ChartType)

+ 78 - 6
services/data/area_graph/processor_business_logic.go

@@ -12,7 +12,8 @@ import (
 
 type InterpolateStrategy struct{}
 
-func (i *InterpolateStrategy) Deal(tmpConfig data_manage.AreaExtraConf, edbDataList []*data_manage.ChartEdbInfoMapping, standardIndexMap map[string]*data_manage.EdbDataList) (err error) {
+// Deal 空值填充:插值法填充
+func (i *InterpolateStrategy) Deal(tmpConfig data_manage.AreaExtraConf, edbDataList []*data_manage.ChartEdbInfoMapping, standardIndexMap map[string]*data_manage.EdbDataList, startDate string, endDate string) (err error) {
 	for _, v := range edbDataList {
 		if v.EdbInfoId != tmpConfig.StandardEdbInfoId {
 			if dataList, ok := v.DataList.([]*data_manage.EdbDataList); ok {
@@ -108,11 +109,44 @@ func (i *InterpolateStrategy) Deal(tmpConfig data_manage.AreaExtraConf, edbDataL
 
 type FillWithPreviousStrategy struct{}
 
-func (f *FillWithPreviousStrategy) Deal(tmpConfig data_manage.AreaExtraConf, edbDataList []*data_manage.ChartEdbInfoMapping, standardIndexMap map[string]*data_manage.EdbDataList) (err error) {
+// Deal 空值填充:前值填充
+func (f *FillWithPreviousStrategy) Deal(tmpConfig data_manage.AreaExtraConf, edbDataList []*data_manage.ChartEdbInfoMapping, standardIndexMap map[string]*data_manage.EdbDataList, startDate string, endDate string) (err error) {
 	// 按自然日补充,再根据基准指标取对应数据
 	for _, v := range edbDataList {
 		if v.EdbInfoId != tmpConfig.StandardEdbInfoId {
 			if dataList, ok := v.DataList.([]*data_manage.EdbDataList); ok {
+				// 存放补充数据
+				var replenishDataList []*data_manage.EdbDataList
+
+				// 处理从 startDate 到第一个数据的日期补充
+				if len(dataList) > 0 {
+					firstData := dataList[0]
+					// 将 startDate 到第一个数据日期之间的自然日填充补充数据,值为 0
+					startDataTime, _ := time.ParseInLocation(utils.FormatDate, startDate, time.Local)
+					firstDataTime, _ := time.ParseInLocation(utils.FormatDate, firstData.DataTime, time.Local)
+
+					// 计算两个日期之间的天数差
+					for startDataTime.Before(firstDataTime) {
+						// 补充数据
+						nextDay := startDataTime.Format(utils.FormatDate)
+
+						// 生成补充数据,值为 0
+						replenishIndexData := data_manage.EdbDataList{
+							EdbInfoId:     v.EdbInfoId,
+							DataTime:      nextDay,
+							DataTimestamp: startDataTime.UnixMilli(),
+							Value:         0,
+						}
+
+						// 将补充数据加入补充数据列表
+						replenishDataList = append(replenishDataList, &replenishIndexData)
+
+						// 更新 startDataTime 到下一个日期
+						startDataTime = startDataTime.AddDate(0, 0, 1)
+					}
+				}
+
+				// 处理指标中空值数据
 				for index := 0; index < len(dataList)-1; index++ {
 					// 获取当前数据和下一个数据
 					beforeIndexData := dataList[index]
@@ -121,6 +155,7 @@ func (f *FillWithPreviousStrategy) Deal(tmpConfig data_manage.AreaExtraConf, edb
 					for utils.IsMoreThanOneDay(beforeIndexData.DataTime, afterIndexData.DataTime) {
 						// 创建补充数据
 						nextDay := utils.GetNextDay(beforeIndexData.DataTime)
+
 						toTime := utils.StringToTime(nextDay)
 						replenishIndexData := data_manage.EdbDataList{
 							EdbInfoId:     v.EdbInfoId,
@@ -130,13 +165,47 @@ func (f *FillWithPreviousStrategy) Deal(tmpConfig data_manage.AreaExtraConf, edb
 						}
 
 						// 将补充数据加入补充数据列表
-						dataList = append(dataList[:index+1], append([]*data_manage.EdbDataList{&replenishIndexData}, dataList[index+1:]...)...)
+						replenishDataList = append(replenishDataList, &replenishIndexData)
+						//dataList = append(dataList[:index+1], append([]*data_manage.EdbDataList{&replenishIndexData}, dataList[index+1:]...)...)
 
 						// 更新 beforeIndexData 为新创建的补充数据
 						beforeIndexData = &replenishIndexData
 					}
 				}
 
+				// 处理从最后一个数据到 endDate 的日期补充
+				if len(dataList) > 0 {
+					lastData := dataList[len(dataList)-1]
+					// 将最后一个数据日期到 endDate 之间的自然日填充补充数据,值为 0
+					lastDataTime, _ := time.ParseInLocation(utils.FormatDate, lastData.DataTime, time.Local)
+					endDataTime, _ := time.ParseInLocation(utils.FormatDate, endDate, time.Local)
+
+					// 如果 lastDataTime 不等于 endDate,进行补充
+					if !lastDataTime.Equal(endDataTime) {
+						// 补充数据直到 endDate
+						for !lastDataTime.After(endDataTime) {
+							// 补充数据
+							nextDay := lastDataTime.Format(utils.FormatDate)
+
+							// 生成补充数据,值为 0
+							replenishIndexData := data_manage.EdbDataList{
+								EdbInfoId:     v.EdbInfoId,
+								DataTime:      nextDay,
+								DataTimestamp: lastDataTime.UnixMilli(),
+								Value:         0,
+							}
+
+							// 将补充数据加入补充数据列表
+							replenishDataList = append(replenishDataList, &replenishIndexData)
+
+							// 更新 lastDataTime 到下一个日期
+							lastDataTime = lastDataTime.AddDate(0, 0, 1)
+						}
+					}
+				}
+
+				dataList = append(dataList, replenishDataList...)
+
 				// 根据基准指标筛选出符合数据
 				var resultDataList []*data_manage.EdbDataList
 				for _, dataObject := range dataList {
@@ -161,7 +230,8 @@ func (f *FillWithPreviousStrategy) Deal(tmpConfig data_manage.AreaExtraConf, edb
 
 type FillWithNextStrategy struct{}
 
-func (f *FillWithNextStrategy) Deal(tmpConfig data_manage.AreaExtraConf, edbDataList []*data_manage.ChartEdbInfoMapping, standardIndexMap map[string]*data_manage.EdbDataList) (err error) {
+// Deal 空值填充:后值填充
+func (f *FillWithNextStrategy) Deal(tmpConfig data_manage.AreaExtraConf, edbDataList []*data_manage.ChartEdbInfoMapping, standardIndexMap map[string]*data_manage.EdbDataList, startDate string, endDate string) (err error) {
 	// 按自然日补充,再根据基准指标取对应数据
 	for _, v := range edbDataList {
 		if v.EdbInfoId != tmpConfig.StandardEdbInfoId {
@@ -217,7 +287,8 @@ func (f *FillWithNextStrategy) Deal(tmpConfig data_manage.AreaExtraConf, edbData
 
 type SetToZeroStrategy struct{}
 
-func (s *SetToZeroStrategy) Deal(tmpConfig data_manage.AreaExtraConf, edbDataList []*data_manage.ChartEdbInfoMapping, standardIndexMap map[string]*data_manage.EdbDataList) (err error) {
+// Deal 空值填充:设为0
+func (s *SetToZeroStrategy) Deal(tmpConfig data_manage.AreaExtraConf, edbDataList []*data_manage.ChartEdbInfoMapping, standardIndexMap map[string]*data_manage.EdbDataList, startDate string, endDate string) (err error) {
 	// 按自然日补充,再根据基准指标取对应数据
 	for _, v := range edbDataList {
 		if v.EdbInfoId != tmpConfig.StandardEdbInfoId {
@@ -270,7 +341,8 @@ func (s *SetToZeroStrategy) Deal(tmpConfig data_manage.AreaExtraConf, edbDataLis
 
 type DeleteDateStrategy struct{}
 
-func (d *DeleteDateStrategy) Deal(tmpConfig data_manage.AreaExtraConf, edbDataList []*data_manage.ChartEdbInfoMapping, standardIndexMap map[string]*data_manage.EdbDataList) error {
+// Deal 删除日期
+func (d *DeleteDateStrategy) Deal(tmpConfig data_manage.AreaExtraConf, edbDataList []*data_manage.ChartEdbInfoMapping, standardIndexMap map[string]*data_manage.EdbDataList, startDate string, endDate string) error {
 	// 取所有指标的时间交集
 	for _, v := range edbDataList {
 		if dataList, ok := v.DataList.([]*data_manage.EdbDataList); ok {

+ 2 - 2
services/data/area_graph/processor_factory.go

@@ -6,7 +6,7 @@ import (
 )
 
 type NullDealStrategy interface {
-	Deal(tmpConfig data_manage.AreaExtraConf, edbDataList []*data_manage.ChartEdbInfoMapping, standardIndexMap map[string]*data_manage.EdbDataList) (err error)
+	Deal(tmpConfig data_manage.AreaExtraConf, edbDataList []*data_manage.ChartEdbInfoMapping, standardIndexMap map[string]*data_manage.EdbDataList, startDate string, endDate string) (err error)
 }
 
 func CreateStrategy(dealWay int) (NullDealStrategy, error) {
@@ -35,5 +35,5 @@ func (c *NullDealContext) SetStrategy(strategy NullDealStrategy) {
 }
 
 func (c *NullDealContext) ExecuteStrategy(tmpConfig data_manage.AreaExtraConf, edbDataList []*data_manage.ChartEdbInfoMapping, standardIndexMap map[string]*data_manage.EdbDataList) error {
-	return c.strategy.Deal(tmpConfig, edbDataList, standardIndexMap)
+	return c.strategy.Deal(tmpConfig, edbDataList, standardIndexMap, "", "")
 }