Browse Source

区间计算

xyxie 7 months ago
parent
commit
23388cc135

+ 3 - 12
controllers/data_manage/range_analysis/chart_info.go

@@ -122,34 +122,25 @@ func (this *RangeChartChartInfoController) Preview() {
 
 	//chartInfo.CorrelationLeadUnit = req.LeadUnit
 	// 获取图表x轴y轴
-	xEdbIdValue, yDataList, e := rangeServ.GetChartDataByEdbInfo(edbInfoMappingA, edbInfoMappingB, req.LeadValue, req.LeadUnit, req.StartDate, req.EndDate, "")
+	xEdbIdValue, dataResp, e := rangeServ.GetChartDataByEdbInfo(edbInfoMapping, &req.ExtraConf)
 	if e != nil {
 		br.Msg = "获取失败"
 		br.ErrMsg = "获取区间计算图表, 图表计算值失败, Err:" + e.Error()
 		return
 	}
 	// 完善指标信息
-	edbList, e := rangeServ.GetChartEdbInfoFormat(chartInfo.ChartInfoId, edbInfoMappingA, edbInfoMappingB)
+	edbList, e := rangeServ.GetChartEdbInfoFormat(chartInfo.ChartInfoId, edbInfoMapping)
 	if e != nil {
 		br.Msg = "获取失败"
 		br.ErrMsg = "获取区间计算图表, 完善指标信息失败, Err:" + e.Error()
 		return
 	}
-	correlationInfo := new(data_manage.CorrelationInfo)
-	correlationInfo.LeadValue = req.LeadValue
-	correlationInfo.LeadUnit = req.LeadUnit
-	correlationInfo.StartDate = req.StartDate
-	correlationInfo.EndDate = req.EndDate
-	correlationInfo.LeadValue = req.LeadValue
-	correlationInfo.EdbInfoIdFirst = edbInfoMappingA.EdbInfoId
-	correlationInfo.EdbInfoIdSecond = edbInfoMappingB.EdbInfoId
 
 	resp := new(data_manage.ChartInfoDetailResp)
 	resp.ChartInfo = chartInfo
 	resp.EdbInfoList = edbList
 	resp.XEdbIdValue = xEdbIdValue
-	resp.YDataList = yDataList
-	resp.DataResp = correlationInfo
+	resp.DataResp = dataResp
 	br.Data = resp
 	br.Ret = 200
 	br.Success = true

+ 2 - 2
models/data_manage/chart_info_range_analysis.go

@@ -1,6 +1,6 @@
 package data_manage
 
-import "go.starlark.net/lib/time"
+import "time"
 
 type ChartRangeAnalysisExtraConf struct {
 	EdbInfoMode          int                                `description:"指标模式 0: 单指标,1: 多指标"`
@@ -16,7 +16,7 @@ type ChartRangeAnalysisExtraConf struct {
 }
 
 type ChartRangeAnalysisAutoDateChangeConf struct {
-	BaseDateType int `description:"基准日期类型:0指标日期,1系统日期,3固定日期"`
+	BaseDateType int `description:"基准日期类型:0指标日期,1系统日期,2固定日期"`
 	MoveForward  int `description:"前移的期数"`
 	DateChange   []*EdbDateConfDateChange
 }

+ 165 - 65
services/data/range_analysis/chart_info.go

@@ -155,97 +155,151 @@ func MoveDataDaysToNewDataList(dataList []*data_manage.EdbDataList, moveDay int)
 }
 
 // GetChartEdbInfoFormat 相关性图表-获取指标信息
-func GetChartEdbInfoFormat(chartInfoId int, edbInfoMappingA, edbInfoMappingB *data_manage.ChartEdbInfoMapping) (edbList []*data_manage.ChartEdbInfoMapping, err error) {
+func GetChartEdbInfoFormat(chartInfoId int, edbInfoMapping *data_manage.ChartEdbInfoMapping) (edbList []*data_manage.ChartEdbInfoMapping, err error) {
 	edbList = make([]*data_manage.ChartEdbInfoMapping, 0)
-	if edbInfoMappingA == nil || edbInfoMappingB == nil {
+	if edbInfoMapping == nil {
 		err = fmt.Errorf("指标信息有误")
 		return
 	}
 
-	edbInfoMappingA.FrequencyEn = data.GetFrequencyEn(edbInfoMappingA.Frequency)
-	if edbInfoMappingA.Unit == `无` {
-		edbInfoMappingA.Unit = ``
-	}
-	if edbInfoMappingB.Unit == `无` {
-		edbInfoMappingB.Unit = ``
+	edbInfoMapping.FrequencyEn = data.GetFrequencyEn(edbInfoMapping.Frequency)
+	if edbInfoMapping.Unit == `无` {
+		edbInfoMapping.Unit = ``
 	}
 	if chartInfoId <= 0 {
-		edbInfoMappingA.IsAxis = 1
-		edbInfoMappingA.LeadValue = 0
-		edbInfoMappingA.LeadUnit = ""
-		edbInfoMappingA.ChartEdbMappingId = 0
-		edbInfoMappingA.ChartInfoId = 0
-		edbInfoMappingA.IsOrder = false
-		edbInfoMappingA.EdbInfoType = 1
-		edbInfoMappingA.ChartStyle = ""
-		edbInfoMappingA.ChartColor = ""
-		edbInfoMappingA.ChartWidth = 0
-
-		edbInfoMappingB.IsAxis = 1
-		edbInfoMappingB.LeadValue = 0
-		edbInfoMappingB.LeadUnit = ""
-		edbInfoMappingB.ChartEdbMappingId = 0
-		edbInfoMappingB.ChartInfoId = 0
-		edbInfoMappingB.IsOrder = false
-		edbInfoMappingB.EdbInfoType = 1
-		edbInfoMappingB.ChartStyle = ""
-		edbInfoMappingB.ChartColor = ""
-		edbInfoMappingB.ChartWidth = 0
+		edbInfoMapping.IsAxis = 1
+		edbInfoMapping.LeadValue = 0
+		edbInfoMapping.LeadUnit = ""
+		edbInfoMapping.ChartEdbMappingId = 0
+		edbInfoMapping.ChartInfoId = 0
+		edbInfoMapping.IsOrder = false
+		edbInfoMapping.EdbInfoType = 1
+		edbInfoMapping.ChartStyle = ""
+		edbInfoMapping.ChartColor = ""
+		edbInfoMapping.ChartWidth = 0
+
 	} else {
-		edbInfoMappingA.LeadUnitEn = data.GetLeadUnitEn(edbInfoMappingA.LeadUnit)
-		edbInfoMappingB.LeadUnitEn = data.GetLeadUnitEn(edbInfoMappingB.LeadUnit)
+		edbInfoMapping.LeadUnitEn = data.GetLeadUnitEn(edbInfoMapping.LeadUnit)
 	}
-	edbList = append(edbList, edbInfoMappingA, edbInfoMappingB)
+	edbList = append(edbList, edbInfoMapping)
 	return
 }
 
 // GetChartDataByEdbInfo 相关性图表-根据指标信息获取x轴和y轴
-func GetChartDataByEdbInfo(edbInfoMapping *data_manage.ChartEdbInfoMapping, req data_manage.ChartRangeAnalysisExtraConf) (xEdbIdValue []int, dataResp data_manage.ChartRangeAnalysisDataResp, err error) {
+func GetChartDataByEdbInfo(edbInfoMapping *data_manage.ChartEdbInfoMapping, req *data_manage.ChartRangeAnalysisExtraConf) (xEdbIdValue []int, dataResp data_manage.ChartRangeAnalysisDataResp, err error) {
 	// 指标的开始日期和结束日期
-	startDateTime, _ := time.ParseInLocation(utils.FormatDate, startDate, time.Local)
-	startDate = startDateTime.AddDate(0, 0, 1).Format(utils.FormatDate)
-
-	// 根据时间区间类型来获取数据的计算窗口,然后再拼接成整段数据
-	// 智能划分得到一个开始日期,和结束日期
-	// 手工划分得到多个开始日期和结束日期(已排序)
-	// 跨年划分得到多个开始日期和结束日期
-
-	//// 2023-03-02 时间序列始终以指标B为基准, 始终是A进行平移
-	//baseEdbInfo := edbInfoMappingB
-	//changeEdbInfo := edbInfoMappingA
-	// 2023-03-17 时间序列始终以指标A为基准, 始终是B进行平移
-	baseEdbInfo := edbInfoMapping
-	changeEdbInfo := edbInfoMappingB
+	edbStartDateTime, _ := time.ParseInLocation(utils.FormatDate, edbInfoMapping.StartDate, time.Local)
+	edbStartDate := edbStartDateTime.AddDate(0, 0, 1).Format(utils.FormatDate)
+	edbEndDateTime, _ := time.ParseInLocation(utils.FormatDate, edbInfoMapping.EndDate, time.Local)
+	edbEndDate := edbEndDateTime.AddDate(0, 0, 1).Format(utils.FormatDate)
 
 	// 获取时间基准指标在时间区间内的值
-	aDataList := make([]*data_manage.EdbDataList, 0)
-	switch baseEdbInfo.EdbInfoCategoryType {
+	dataList := make([]*data_manage.EdbDataList, 0)
+	switch edbInfoMapping.EdbInfoCategoryType {
 	case 0:
-		aDataList, err = data_manage.GetEdbDataList(baseEdbInfo.Source, baseEdbInfo.SubSource, baseEdbInfo.EdbInfoId, startDate, endDate)
+		dataList, err = data_manage.GetEdbDataList(edbInfoMapping.Source, edbInfoMapping.SubSource, edbInfoMapping.EdbInfoId, "", "")
 	case 1:
-		_, aDataList, _, _, err, _ = data.GetPredictDataListByPredictEdbInfoId(baseEdbInfo.EdbInfoId, startDate, endDate, false)
+		_, dataList, _, _, err, _ = data.GetPredictDataListByPredictEdbInfoId(edbInfoMapping.EdbInfoId, "", "", false)
 	default:
 		err = errors.New("指标base类型异常")
 		return
 	}
 
-	// 获取变频指标所有日期的值, 插值法完善数据
-	bDataList := make([]*data_manage.EdbDataList, 0)
-	switch changeEdbInfo.EdbInfoCategoryType {
+	dateList := make([]data_manage.ChartRangeAnalysisDateDataItem, 0)
+	switch req.DateRangeType {
 	case 0:
-		bDataList, err = data_manage.GetEdbDataList(changeEdbInfo.Source, changeEdbInfo.SubSource, changeEdbInfo.EdbInfoId, "", "")
+		// 智能划分得到一个开始日期,和结束日期
+		var startDateTime, endDateTime time.Time
+		if req.AutoDateConf.IsAutoStartDate == 0 { //固定设置
+			startDate := req.AutoDateConf.StartDate
+			if startDate == "" {
+				startDate = "2020-01-01"
+			}
+			startDateTime, _ = time.ParseInLocation(utils.FormatDate, startDate, time.Local)
+			endDateTime = edbEndDateTime
+		} else {
+			startConf := req.AutoDateConf.StartDateConf
+			endConf := req.AutoDateConf.EndDateConf
+			startDate := ""
+			endDate := ""
+			if startConf.BaseDateType == 0 { //
+				startDate = edbStartDate
+			} else if startConf.BaseDateType == 1 {
+				startDate = time.Now().Format(utils.FormatDate)
+			}
+			if startConf.MoveForward > 0 {
+				startDate = GetEdbDateByMoveForward(startConf.MoveForward, dataList)
+			}
+			if len(startConf.DateChange) > 0 {
+				startDate, err = HandleEdbDateChange(startDate, startConf.DateChange)
+				if err != nil {
+					err = fmt.Errorf("智能划分开始日期处理失败:%s", err.Error())
+					return
+				}
+			}
+
+			if endConf.BaseDateType == 0 { //
+				endDate = edbEndDate
+			} else if endConf.BaseDateType == 2 {
+				endDate = "2020-01-01"
+			}
+			if endConf.MoveForward > 0 {
+				endDate = GetEdbDateByMoveForward(endConf.MoveForward, dataList)
+			}
+			if len(endConf.DateChange) > 0 {
+				endDate, err = HandleEdbDateChange(endDate, endConf.DateChange)
+				if err != nil {
+					err = fmt.Errorf("智能划分结束日期处理失败:%s", err.Error())
+					return
+				}
+			}
+
+			startDateTime, _ = time.ParseInLocation(utils.FormatDate, startDate, time.Local)
+			endDateTime, _ = time.ParseInLocation(utils.FormatDate, endDate, time.Local)
+		}
+		dateList = append(dateList, data_manage.ChartRangeAnalysisDateDataItem{
+			StartDate: startDateTime,
+			EndDate:   endDateTime})
 	case 1:
-		_, bDataList, _, _, err, _ = data.GetPredictDataListByPredictEdbInfoId(changeEdbInfo.EdbInfoId, "", "", false)
-	default:
-		err = errors.New("指标change类型异常")
-		return
+		// 手工划分得到多个开始日期和结束日期(已排序)
+		for _, v := range req.ManualDateConf {
+			startDateT, _ := time.ParseInLocation(utils.FormatDate, v.StartDate, time.Local)
+			endDateT, _ := time.ParseInLocation(utils.FormatDate, v.EndDate, time.Local)
+			tmp :=
+				data_manage.ChartRangeAnalysisDateDataItem{
+					StartDate: startDateT,
+					EndDate:   endDateT,
+				}
+			dateList = append(dateList, tmp)
+		}
+	case 2:
+		startYear := edbStartDateTime.Year()
+		endYear := edbEndDateTime.Year()
+		startDay := req.YearDateConf.StartDay
+		endDay := req.YearDateConf.EndDay
+		for year := startYear; year <= endYear; year++ {
+			startDate := fmt.Sprintf("%d-%s", year, startDay)
+			endDate := fmt.Sprintf("%d-%s", year+1, endDay)
+			startDateTime, _ := time.ParseInLocation(utils.FormatDate, startDate, time.Local)
+			endDateTime, _ := time.ParseInLocation(utils.FormatDate, endDate, time.Local)
+			if startDateTime.Before(edbStartDateTime) {
+				break
+			}
+			if endDateTime.After(edbEndDateTime) {
+				endDateTime = edbEndDateTime
+			}
+			tmp := data_manage.ChartRangeAnalysisDateDataItem{
+				StartDate: startDateTime,
+				EndDate:   endDateTime,
+			}
+			dateList = append(dateList, tmp)
+		}
+
 	}
-	//changeDataMap := make(map[string]float64)
-	//newChangeDataList, e := HandleDataByLinearRegression(bDataList, changeDataMap)
-	//if e != nil {
-	//	err = fmt.Errorf("获取变频指标插值法Map失败, Err: %s", e.Error())
-	//	return
-	//}
+	// 根据时间区间类型来获取数据的计算窗口,然后再拼接成整段数据
+
+	// 手工划分得到多个开始日期和结束日期(已排序)
+	// 跨年划分得到多个开始日期和结束日期
 
 	// 2023-03-17 时间序列始终以指标A为基准, 始终是B进行平移
 	baseDataList := make([]*data_manage.EdbDataList, 0)
@@ -1572,3 +1626,49 @@ func FormatChartEdbInfoMappings(chartInfoId int, mappings []*data_manage.ChartEd
 	}
 	return
 }
+
+func GetEdbDateByMoveForward(moveForward int, edbDataList []*data_manage.EdbDataList) (date string) {
+	moveForward = 0
+	// 根据日期进行排序
+	index := len(edbDataList) - 1 - moveForward
+	for k, v := range edbDataList {
+		if k == index {
+			date = v.DataTime
+			return
+		}
+	}
+	return
+}
+
+// HandleEdbDateChange 处理日期变换
+func HandleEdbDateChange(date string, dateChange []*data_manage.EdbDateConfDateChange) (newDate string, err error) {
+	newDate = date
+	if newDate != "" {
+		if len(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 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, _ = utils.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
+}

+ 129 - 0
utils/common.go

@@ -2468,3 +2468,132 @@ func DateConvMysqlConvMongo(dateCon string) string {
 	}
 	return cond
 }
+
+// handleSystemAppointDateT
+// @Description: 处理系统日期相关的指定频率(所在周/旬/月/季/半年/年的最后/最早一天)
+// @author: Roc
+// @datetime2023-10-27 09:31:35
+// @param Frequency string
+// @param Day string
+// @return date string
+// @return err error
+// @return errMsg string
+func HandleSystemAppointDateT(currDate time.Time, appointDay, frequency string) (date string, err error, errMsg string) {
+	//currDate := time.Now()
+	switch frequency {
+	case "本周":
+		day := int(currDate.Weekday())
+		if day == 0 { // 周日
+			day = 7
+		}
+		num := 0
+		switch appointDay {
+		case "周一":
+			num = 1
+		case "周二":
+			num = 2
+		case "周三":
+			num = 3
+		case "周四":
+			num = 4
+		case "周五":
+			num = 5
+		case "周六":
+			num = 6
+		case "周日":
+			num = 7
+		}
+		day = num - day
+		date = currDate.AddDate(0, 0, day).Format(FormatDate)
+	case "本旬":
+		day := currDate.Day()
+		var tmpDate time.Time
+		switch appointDay {
+		case "第一天":
+			if day <= 10 {
+				tmpDate = time.Date(currDate.Year(), currDate.Month(), 1, 0, 0, 0, 0, currDate.Location())
+			} else if day <= 20 {
+				tmpDate = time.Date(currDate.Year(), currDate.Month(), 11, 0, 0, 0, 0, currDate.Location())
+			} else {
+				tmpDate = time.Date(currDate.Year(), currDate.Month(), 21, 0, 0, 0, 0, currDate.Location())
+			}
+		case "最后一天":
+			if day <= 10 {
+				tmpDate = time.Date(currDate.Year(), currDate.Month(), 10, 0, 0, 0, 0, currDate.Location())
+			} else if day <= 20 {
+				tmpDate = time.Date(currDate.Year(), currDate.Month(), 20, 0, 0, 0, 0, currDate.Location())
+			} else {
+				tmpDate = time.Date(currDate.Year(), currDate.Month()+1, 1, 0, 0, 0, 0, currDate.Location()).AddDate(0, 0, -1)
+			}
+		}
+		date = tmpDate.Format(FormatDate)
+	case "本月":
+		var tmpDate time.Time
+		switch appointDay {
+		case "第一天":
+			tmpDate = time.Date(currDate.Year(), currDate.Month(), 1, 0, 0, 0, 0, currDate.Location())
+		case "最后一天":
+			tmpDate = time.Date(currDate.Year(), currDate.Month()+1, 1, 0, 0, 0, 0, currDate.Location()).AddDate(0, 0, -1)
+		}
+		date = tmpDate.Format(FormatDate)
+	case "本季":
+		month := currDate.Month()
+		var tmpDate time.Time
+		switch appointDay {
+		case "第一天":
+			if month <= 3 {
+				tmpDate = time.Date(currDate.Year(), 1, 1, 0, 0, 0, 0, currDate.Location())
+			} else if month <= 6 {
+				tmpDate = time.Date(currDate.Year(), 4, 1, 0, 0, 0, 0, currDate.Location())
+			} else if month <= 9 {
+				tmpDate = time.Date(currDate.Year(), 7, 1, 0, 0, 0, 0, currDate.Location())
+			} else {
+				tmpDate = time.Date(currDate.Year(), 10, 1, 0, 0, 0, 0, currDate.Location())
+			}
+		case "最后一天":
+			if month <= 3 {
+				tmpDate = time.Date(currDate.Year(), 3, 31, 0, 0, 0, 0, currDate.Location())
+			} else if month <= 6 {
+				tmpDate = time.Date(currDate.Year(), 6, 30, 0, 0, 0, 0, currDate.Location())
+			} else if month <= 9 {
+				tmpDate = time.Date(currDate.Year(), 9, 30, 0, 0, 0, 0, currDate.Location())
+			} else {
+				tmpDate = time.Date(currDate.Year(), 12, 31, 0, 0, 0, 0, currDate.Location())
+			}
+		}
+		date = tmpDate.Format(FormatDate)
+	case "本半年":
+		month := currDate.Month()
+		var tmpDate time.Time
+		switch appointDay {
+		case "第一天":
+			if month <= 6 {
+				tmpDate = time.Date(currDate.Year(), 1, 1, 0, 0, 0, 0, currDate.Location())
+			} else {
+				tmpDate = time.Date(currDate.Year(), 7, 1, 0, 0, 0, 0, currDate.Location())
+			}
+		case "最后一天":
+			if month <= 6 {
+				tmpDate = time.Date(currDate.Year(), 6, 30, 0, 0, 0, 0, currDate.Location())
+			} else {
+				tmpDate = time.Date(currDate.Year(), 12, 31, 0, 0, 0, 0, currDate.Location())
+			}
+		}
+		date = tmpDate.Format(FormatDate)
+	case "本年":
+		var tmpDate time.Time
+		switch appointDay {
+		case "第一天":
+			tmpDate = time.Date(currDate.Year(), 1, 1, 0, 0, 0, 0, currDate.Location())
+		case "最后一天":
+			tmpDate = time.Date(currDate.Year(), 12, 31, 0, 0, 0, 0, currDate.Location())
+		}
+		date = tmpDate.Format(FormatDate)
+	default:
+		errMsg = "错误的日期频度:" + frequency
+		err = errors.New(errMsg)
+		return
+	}
+
+	return
+}