Browse Source

Merge branch 'eta_2.2.9_residual_analysis_1104@guomengyuan' into debug

gmy 4 months ago
parent
commit
602920fb4e

+ 2 - 1
models/residual_analysis_model/calculate_residual_analysis_config.go

@@ -22,6 +22,7 @@ type ResidualAnalysisReq struct {
 	EdbInfoIdA       int     `description:"指标A"`
 	EdbInfoIdB       int     `description:"指标B"`
 	EdbInfoId        int     `description:"残差指标id"`
+	ResidualType     int     `description:"残差类型: 1-映射残差 2-拟合残差"`
 	DateType         int     `description:"时间类型 -1-自定义时间 0-至今 n-枚举时间(近n年)"`
 	StartDate        string  `description:"自定义开始日期"`
 	EndDate          string  `description:"自定义结束日期"`
@@ -48,7 +49,7 @@ type ResidualAnalysisResp struct {
 
 type ChartResp struct {
 	ChartInfo   *ResidualAnalysisChartInfo
-	EdbInfoList []*ResidualAnalysisChartEdbInfoMapping
+	EdbInfoList []ResidualAnalysisChartEdbInfoMapping
 }
 
 type ResidualAnalysisChartInfo struct {

+ 28 - 23
services/residual_analysis_service/residual_analysis_service.go

@@ -47,7 +47,7 @@ func ResidualAnalysisPreview(req residual_analysis_model.ResidualAnalysisReq) (r
 		startDate = req.StartDate
 		endDate = ""
 	default:
-		startDate = utils.GetPreYear(req.DateType)
+		startDate = utils.GetPreYearTime(req.DateType)
 		endDate = ""
 	}
 
@@ -63,9 +63,9 @@ func ResidualAnalysisPreview(req residual_analysis_model.ResidualAnalysisReq) (r
 	baseChartInfo.ChartType = utils.CHART_TYPE_CURVE
 
 	// 原始图表信息
-	var OriginalEdbList []*residual_analysis_model.ResidualAnalysisChartEdbInfoMapping
+	originalEdbList := make([]residual_analysis_model.ResidualAnalysisChartEdbInfoMapping, 0)
 
-	OriginalEdbList, err = fillOriginalChart(req, mappingList, startDate, endDate, edbInfoMappingA, edbInfoMappingB, OriginalEdbList)
+	originalEdbList, err = fillOriginalChart(req, mappingList, startDate, endDate, edbInfoMappingA, edbInfoMappingB, originalEdbList)
 	if err != nil {
 		return residual_analysis_model.ResidualAnalysisResp{}, err
 	}
@@ -73,20 +73,20 @@ func ResidualAnalysisPreview(req residual_analysis_model.ResidualAnalysisReq) (r
 
 	resp.OriginalChartData = residual_analysis_model.ChartResp{
 		ChartInfo:   baseChartInfo,
-		EdbInfoList: OriginalEdbList,
+		EdbInfoList: originalEdbList,
 	}
 
-	dataAList, ok := edbInfoMappingA.DataList.([]data_manage.EdbDataList)
+	dataAList, ok := edbInfoMappingA.DataList.([]*data_manage.EdbDataList)
 	if !ok {
 		return residual_analysis_model.ResidualAnalysisResp{}, fmt.Errorf("数据类型转换失败", nil)
 	}
-	indexADataMap := map[string]data_manage.EdbDataList{}
+	indexADataMap := map[string]*data_manage.EdbDataList{}
 	for _, indexData := range dataAList {
 		indexADataMap[indexData.DataTime] = indexData
 	}
 
 	// 映射图表信息
-	mappingEdbList, err := fillMappingChartInfo(req, edbInfoMappingA, edbInfoMappingB, OriginalEdbList, indexADataMap)
+	mappingEdbList, err := fillMappingChartInfo(req, edbInfoMappingA, edbInfoMappingB, originalEdbList, indexADataMap)
 	if err != nil {
 		return residual_analysis_model.ResidualAnalysisResp{}, err
 	}
@@ -113,9 +113,9 @@ func ResidualAnalysisPreview(req residual_analysis_model.ResidualAnalysisReq) (r
 	return resp, nil
 }
 
-func fillResidualChartInfo(edbInfoMappingA *data_manage.ChartEdbInfoMapping, edbInfoMappingB *data_manage.ChartEdbInfoMapping, mappingEdbList []*residual_analysis_model.ResidualAnalysisChartEdbInfoMapping) ([]*residual_analysis_model.ResidualAnalysisChartEdbInfoMapping, error) {
+func fillResidualChartInfo(edbInfoMappingA *data_manage.ChartEdbInfoMapping, edbInfoMappingB *data_manage.ChartEdbInfoMapping, mappingEdbList []residual_analysis_model.ResidualAnalysisChartEdbInfoMapping) ([]residual_analysis_model.ResidualAnalysisChartEdbInfoMapping, error) {
 	// 计算公式 映射残差 = 因变量指标 - 映射指标
-	var edbInfoA, edbInfoB *residual_analysis_model.ResidualAnalysisChartEdbInfoMapping
+	var edbInfoA, edbInfoB residual_analysis_model.ResidualAnalysisChartEdbInfoMapping
 	if mappingEdbList[0].EdbInfoId == edbInfoMappingA.EdbInfoId {
 		edbInfoA = mappingEdbList[0]
 		edbInfoB = mappingEdbList[1]
@@ -123,16 +123,16 @@ func fillResidualChartInfo(edbInfoMappingA *data_manage.ChartEdbInfoMapping, edb
 		edbInfoA = mappingEdbList[1]
 		edbInfoB = mappingEdbList[0]
 	}
-	dataAList, ok := edbInfoA.DataList.([]data_manage.EdbDataList)
+	dataAList, ok := edbInfoA.DataList.([]*data_manage.EdbDataList)
 	if !ok {
 		return nil, fmt.Errorf("数据类型转换失败", nil)
 	}
 	edbData := dataAList
-	dataBList, ok := edbInfoB.DataList.([]data_manage.EdbDataList)
+	dataBList, ok := edbInfoB.DataList.([]*data_manage.EdbDataList)
 	if !ok {
 		return nil, fmt.Errorf("数据类型转换失败", nil)
 	}
-	var indexDataBMap map[string]data_manage.EdbDataList
+	var indexDataBMap = make(map[string]*data_manage.EdbDataList)
 	for _, data := range dataBList {
 		indexDataBMap[data.DataTime] = data
 	}
@@ -155,7 +155,7 @@ func fillResidualChartInfo(edbInfoMappingA *data_manage.ChartEdbInfoMapping, edb
 	return mappingEdbList, nil
 }
 
-func fillMappingChartInfo(req residual_analysis_model.ResidualAnalysisReq, edbInfoMappingA *data_manage.ChartEdbInfoMapping, edbInfoMappingB *data_manage.ChartEdbInfoMapping, originalEdbList []*residual_analysis_model.ResidualAnalysisChartEdbInfoMapping, indexADataMap map[string]data_manage.EdbDataList) ([]*residual_analysis_model.ResidualAnalysisChartEdbInfoMapping, error) {
+func fillMappingChartInfo(req residual_analysis_model.ResidualAnalysisReq, edbInfoMappingA *data_manage.ChartEdbInfoMapping, edbInfoMappingB *data_manage.ChartEdbInfoMapping, originalEdbList []residual_analysis_model.ResidualAnalysisChartEdbInfoMapping, indexADataMap map[string]*data_manage.EdbDataList) ([]residual_analysis_model.ResidualAnalysisChartEdbInfoMapping, error) {
 	// 计算公式:Y=aX+b,Y为映射后的指标,X为自变量指标
 	// 正序:a=(L2-L1)/(R2-R1)	b=L2-R2*a
 	// 逆序:a=(L2-L1)/(R1-R2)	b=L2-R1*a
@@ -169,7 +169,7 @@ func fillMappingChartInfo(req residual_analysis_model.ResidualAnalysisReq, edbIn
 		b = req.LeftIndexMax - req.RightIndexMin*a
 	}
 
-	dataList, ok := edbInfoMappingB.DataList.([]data_manage.EdbDataList)
+	dataList, ok := edbInfoMappingB.DataList.([]*data_manage.EdbDataList)
 	if !ok {
 		return nil, fmt.Errorf("数据类型转换失败", nil)
 	}
@@ -196,24 +196,29 @@ func fillMappingChartInfo(req residual_analysis_model.ResidualAnalysisReq, edbIn
 		}
 	}
 
-	for index, indexData := range dataList {
-		// 计算映射值
-		indexData.Value = a*indexData.Value + b
-
-		// 从最早时间开始 补充时间为自然日
+	for index := 0; index < len(dataList)-1; index++ {
+		// 获取当前数据和下一个数据
 		beforeIndexData := dataList[index]
 		afterIndexData := dataList[index+1]
+
+		// 计算映射值
+		beforeIndexData.Value = a*beforeIndexData.Value + b
+
+		// 从最早时间开始,补充时间为自然日
 		if utils.IsMoreThanOneDay(beforeIndexData.DataTime, afterIndexData.DataTime) {
+			// 创建补充数据
 			replenishIndexData := data_manage.EdbDataList{
 				DataTime:      utils.GetNextDay(beforeIndexData.DataTime),
 				DataTimestamp: time.Now().UnixMilli(),
 				Value:         beforeIndexData.Value,
 			}
-			dataList = append(dataList, replenishIndexData)
+
+			// 将补充数据插入到数据列表
+			dataList = append(dataList, &replenishIndexData)
 		}
 	}
 	// 根据指标A的时间key,在B的映射指标中筛选出对应的值
-	var dataBList []data_manage.EdbDataList
+	var dataBList []*data_manage.EdbDataList
 	for _, indexData := range dataList {
 		if _, ok := indexADataMap[indexData.DataTime]; ok {
 			dataBList = append(dataBList, indexData)
@@ -232,9 +237,9 @@ func fillMappingChartInfo(req residual_analysis_model.ResidualAnalysisReq, edbIn
 	return mappingEdbList, nil
 }
 
-func fillOriginalChart(req residual_analysis_model.ResidualAnalysisReq, mappingList []*data_manage.ChartEdbInfoMapping, startDate string, endDate string, edbInfoMappingA *data_manage.ChartEdbInfoMapping, edbInfoMappingB *data_manage.ChartEdbInfoMapping, OriginalEdbList []*residual_analysis_model.ResidualAnalysisChartEdbInfoMapping) ([]*residual_analysis_model.ResidualAnalysisChartEdbInfoMapping, error) {
+func fillOriginalChart(req residual_analysis_model.ResidualAnalysisReq, mappingList []*data_manage.ChartEdbInfoMapping, startDate string, endDate string, edbInfoMappingA *data_manage.ChartEdbInfoMapping, edbInfoMappingB *data_manage.ChartEdbInfoMapping, OriginalEdbList []residual_analysis_model.ResidualAnalysisChartEdbInfoMapping) ([]residual_analysis_model.ResidualAnalysisChartEdbInfoMapping, error) {
 	for _, v := range mappingList {
-		var edbInfoMapping *residual_analysis_model.ResidualAnalysisChartEdbInfoMapping
+		var edbInfoMapping residual_analysis_model.ResidualAnalysisChartEdbInfoMapping
 		edbInfoMapping.EdbInfoType = 1
 		edbInfoMapping.IsOrder = false
 		edbInfoMapping.IsAxis = 1

+ 7 - 5
utils/date_util.go

@@ -1,7 +1,6 @@
 package utils
 
 import (
-	"strconv"
 	"time"
 )
 
@@ -15,11 +14,14 @@ const (
 	FullDate         = "Monday, 02-Jan-06 15:04:05 PST" // 完整日期:例如:Monday, 02-Jan-06 15:04:05 PST
 )
 
-// GetPreYear 获取当前时间 前n年的时间
-func GetPreYear(n int) string {
+// GetPreYearTime 获取当前时间 前n年的时间 返回yyyy-MM-dd 格式的时间
+func GetPreYearTime(n int) string {
+	// 获取当前时间
 	now := time.Now()
-	year := now.Year() - n
-	return strconv.Itoa(year)
+	// 计算前n年的时间
+	preYearTime := now.AddDate(-n, 0, 0)
+	// 格式化时间
+	return preYearTime.Format("2006-01-02")
 }
 
 // IsMoreThanOneDay 判断两个yyyy-MM-dd类型的时间,相差是否大于1天