Browse Source

上下限问题

xyxie 8 months ago
parent
commit
a82c38c4d7

+ 8 - 0
models/data_manage/multiple_graph_config_edb_mapping.go

@@ -51,3 +51,11 @@ func GetMultipleGraphConfigEdbMappingListById(configId int) (items []*MultipleGr
 
 	return
 }
+
+func GetMultipleGraphConfigEdbMappingListByIdAndSource(configId, source int) (items []*MultipleGraphConfigEdbMapping, err error) {
+	o := orm.NewOrmUsingDB("data")
+	sql := `SELECT * FROM multiple_graph_config_edb_mapping WHERE multiple_graph_config_id = ? AND source = ? `
+	_, err = o.Raw(sql, configId, source).QueryRows(&items)
+
+	return
+}

+ 152 - 34
services/data/range_analysis/chart_info.go

@@ -224,6 +224,23 @@ func GetChartDataByEdbInfoList(chartInfoId int, dateType, startYear int, startDa
 		if err != nil {
 			return
 		}
+		dataList := edbInfoMapping.DataList.([]*data_manage.EdbDataList)
+		// 处理上下限
+		var maxData, minData float64
+		if len(dataList) > 0 {
+			maxData = dataList[0].Value
+			minData = dataList[0].Value
+			for _, v := range dataList {
+				if v.Value > maxData {
+					maxData = v.Value
+				}
+				if v.Value < minData {
+					minData = v.Value
+				}
+			}
+		}
+		edbInfoMapping.MaxData = maxData
+		edbInfoMapping.MinData = minData
 		xEdbIdValue = append(xEdbIdValue, edbInfoMapping.EdbInfoId)
 	}
 	//根据时间类型来筛选最终的数据
@@ -231,15 +248,17 @@ func GetChartDataByEdbInfoList(chartInfoId int, dateType, startYear int, startDa
 	if dateType == utils.DateTypeNYears {
 		for _, v := range edbInfoMappingList {
 			dataList := v.DataList.([]*data_manage.EdbDataList)
-			latestDate := dataList[0].DataTime
-			if latestDate != "" {
-				lastDateT, tErr := time.Parse(utils.FormatDate, latestDate)
-				if tErr != nil {
-					err = fmt.Errorf("获取图表日期信息失败,Err:" + tErr.Error())
-					return
-				}
-				if lastDateT.Year() > yearMax {
-					yearMax = lastDateT.Year()
+			if len(dataList) > 0 {
+				latestDate := dataList[0].DataTime
+				if latestDate != "" {
+					lastDateT, tErr := time.Parse(utils.FormatDate, latestDate)
+					if tErr != nil {
+						err = fmt.Errorf("获取图表日期信息失败,Err:" + tErr.Error())
+						return
+					}
+					if lastDateT.Year() > yearMax {
+						yearMax = lastDateT.Year()
+					}
 				}
 			}
 		}
@@ -248,11 +267,13 @@ func GetChartDataByEdbInfoList(chartInfoId int, dateType, startYear int, startDa
 
 	if startDate != "" {
 		for k, v := range edbInfoMappingList {
+			var maxData, minData float64
 			dataList := v.DataList.([]*data_manage.EdbDataList)
 			newDataList := make([]*data_manage.EdbDataList, 0)
 			if len(dataList) == 0 {
 				newDataList = dataList
 			} else {
+				firstFlag := true
 				for _, d := range dataList {
 					if endDate != "" && d.DataTime > endDate {
 						continue
@@ -261,9 +282,23 @@ func GetChartDataByEdbInfoList(chartInfoId int, dateType, startYear int, startDa
 						continue
 					}
 					newDataList = append(newDataList, d)
+					if firstFlag {
+						maxData = d.Value
+						minData = d.Value
+						firstFlag = false
+					} else {
+						if d.Value > maxData {
+							maxData = d.Value
+						}
+						if d.Value < minData {
+							minData = d.Value
+						}
+					}
 				}
 			}
 			edbInfoMappingList[k].DataList = newDataList
+			edbInfoMappingList[k].MinData = minData
+			edbInfoMappingList[k].MaxData = maxData
 		}
 	}
 	dataResp = data_manage.ChartRangeAnalysisDataResp{ChartRangeAnalysisExtraConf: req}
@@ -298,6 +333,22 @@ func GetChartDataByEdbInfoListBySeries(chartInfoId int, dateType, startYear int,
 			return
 		}
 		edbInfoMapping.DataList = dataList
+		// 处理上下限
+		var maxData, minData float64
+		if len(dataList) > 0 {
+			maxData = dataList[0].Value
+			minData = dataList[0].Value
+			for _, v := range dataList {
+				if v.Value > maxData {
+					maxData = v.Value
+				}
+				if v.Value < minData {
+					minData = v.Value
+				}
+			}
+		}
+		edbInfoMapping.MaxData = maxData
+		edbInfoMapping.MinData = minData
 		xEdbIdValue = append(xEdbIdValue, edbInfoMapping.EdbInfoId)
 	}
 	yearMax := 0
@@ -321,11 +372,13 @@ func GetChartDataByEdbInfoListBySeries(chartInfoId int, dateType, startYear int,
 
 	if startDate != "" {
 		for k, v := range edbInfoMappingList {
+			var maxData, minData float64
 			dataList := v.DataList.([]*data_manage.EdbDataList)
 			newDataList := make([]*data_manage.EdbDataList, 0)
 			if len(dataList) == 0 {
 				newDataList = dataList
 			} else {
+				firstFlag := true
 				for _, d := range dataList {
 					if endDate != "" && d.DataTime > endDate {
 						continue
@@ -334,9 +387,23 @@ func GetChartDataByEdbInfoListBySeries(chartInfoId int, dateType, startYear int,
 						continue
 					}
 					newDataList = append(newDataList, d)
+					if firstFlag {
+						maxData = d.Value
+						minData = d.Value
+						firstFlag = false
+					} else {
+						if d.Value > maxData {
+							maxData = d.Value
+						}
+						if d.Value < minData {
+							minData = d.Value
+						}
+					}
 				}
 			}
 			edbInfoMappingList[k].DataList = newDataList
+			edbInfoMappingList[k].MinData = minData
+			edbInfoMappingList[k].MaxData = maxData
 		}
 	}
 	dataResp = data_manage.ChartRangeAnalysisDataResp{ChartRangeAnalysisExtraConf: req, SeriesId: seriesMappingItem.FactorEdbSeriesId}
@@ -668,14 +735,6 @@ func AddChartInfo(req data_manage.AddChartInfoReq, source int, sysUser *system.A
 		isSendEmail = false
 		return
 	}
-
-	extraConfig.SeriesName = strings.TrimSpace(extraConfig.SeriesName)
-	if extraConfig.SeriesName == "" && extraConfig.EdbInfoMode == 1 {
-		errMsg = "请输入指标系列名称"
-		err = errors.New(errMsg)
-		isSendEmail = false
-		return
-	}
 	if len(req.ChartEdbInfoList) > 100 {
 		errMsg = "添加指标总数量不得超过100"
 		err = errors.New(errMsg)
@@ -683,14 +742,11 @@ func AddChartInfo(req data_manage.AddChartInfoReq, source int, sysUser *system.A
 		return
 	}
 
-	if extraConfig.CalculateType > 5 || extraConfig.CalculateType < 0 {
-		errMsg = "计算方式参数错误"
-		err = errors.New(errMsg)
-		isSendEmail = false
+	err, errMsg, isSendEmail = CheckChartRangeExtraConfig(extraConfig)
+	if err != nil {
 		return
 	}
 
-	//todo 校验时间区间设置
 	chartClassify, err := data_manage.GetChartClassifyById(req.ChartClassifyId)
 	if err != nil {
 		if err.Error() == utils.ErrNoRow() {
@@ -913,26 +969,16 @@ func EditChartInfo(req data_manage.EditChartInfoReq, sysUser *system.Admin, lang
 	}
 
 	extraConfig.SeriesName = strings.TrimSpace(extraConfig.SeriesName)
-	if extraConfig.SeriesName == "" && extraConfig.EdbInfoMode == 1 {
-		errMsg = "请输入指标系列名称"
-		err = errors.New(errMsg)
-		isSendEmail = false
-		return
-	}
 	if len(req.ChartEdbInfoList) > 100 {
 		errMsg = "添加指标总数量不得超过100"
 		err = errors.New(errMsg)
 		isSendEmail = false
 		return
 	}
-
-	if extraConfig.CalculateType > 5 || extraConfig.CalculateType < 0 {
-		errMsg = "计算方式参数错误"
-		err = errors.New(errMsg)
-		isSendEmail = false
+	err, errMsg, isSendEmail = CheckChartRangeExtraConfig(extraConfig)
+	if err != nil {
 		return
 	}
-
 	chartClassify, err := data_manage.GetChartClassifyById(req.ChartClassifyId)
 	if err != nil {
 		if err.Error() == utils.ErrNoRow() {
@@ -1767,3 +1813,75 @@ func FactorEdbStepCalculateRange(seriesId int, edbArr []int, extraConf data_mana
 	}
 	return
 }
+
+func CheckChartRangeExtraConfig(extraConfig data_manage.ChartRangeAnalysisExtraConf) (err error, errMsg string, isSendEmail bool) {
+	extraConfig.SeriesName = strings.TrimSpace(extraConfig.SeriesName)
+	if extraConfig.SeriesName == "" && extraConfig.EdbInfoMode == 1 {
+		errMsg = "请输入指标系列名称"
+		err = errors.New(errMsg)
+		isSendEmail = false
+		return
+	}
+
+	if extraConfig.CalculateType > 5 || extraConfig.CalculateType < 0 {
+		errMsg = "计算方式参数错误"
+		err = errors.New(errMsg)
+		isSendEmail = false
+		return
+	}
+	switch extraConfig.DateRangeType {
+	case 0:
+	case 1:
+		if len(extraConfig.ManualDateConf) == 0 {
+			errMsg = "请选择时间区间"
+			err = errors.New(errMsg)
+			return
+		}
+		// 先按开始时间排序
+		sort.Sort(data_manage.ChartRangeAnalysisManualDateConfList(extraConfig.ManualDateConf))
+		// 校验日期
+		// 1.如果截止时间小于指标的截止日期,需要重置为指标的截止日期
+		// 2.时间区间不能重叠
+		for i := 1; i < len(extraConfig.ManualDateConf); i++ {
+			start1, e := time.Parse(utils.FormatDate, extraConfig.ManualDateConf[i-1].EndDate)
+			if e != nil {
+				err = e
+				errMsg = "截止日期格式有误"
+				return
+			}
+			start2, e := time.Parse(utils.FormatDate, extraConfig.ManualDateConf[i].EndDate)
+			if e != nil {
+				err = e
+				errMsg = "截止日期格式有误"
+				return
+			}
+
+			start3, e := time.Parse(utils.FormatDate, extraConfig.ManualDateConf[i].StartDate)
+			if e != nil {
+				err = e
+				errMsg = "截止日期格式有误"
+				return
+			}
+			// 如果当前区间的开始时间小于等于前一个区间的结束时间,则存在重叠
+			if !start2.After(start1) || start3.Before(start1) {
+				errMsg = "日期区间存在重叠"
+				return
+			}
+		}
+		//如果截止时间大于指标的截止日期,需要重置为指标的截止日期
+	case 2:
+		if extraConfig.YearDateConf.StartDay == "" || extraConfig.YearDateConf.EndDay == "" {
+			errMsg = "请选择时间区间"
+			return
+		}
+		if _, e := time.Parse(utils.FormatMonthDay, extraConfig.YearDateConf.StartDay); e != nil {
+			errMsg = "开始日期格式有误"
+			return
+		}
+		if _, e := time.Parse(utils.FormatMonthDay, extraConfig.YearDateConf.EndDay); e != nil {
+			errMsg = "结束日期格式有误"
+			return
+		}
+	}
+	return
+}