Browse Source

fix: 滚动相关性图表

hsun 1 year ago
parent
commit
a7789be9f6

+ 2 - 26
controllers/data_manage/correlation/correlation_chart_info.go

@@ -111,32 +111,8 @@ func GetChartInfoDetailFromUniqueCode(chartInfo *data_manage.ChartInfoView, isCa
 			return
 		}
 	case utils.CHART_SOURCE_ROLLING_CORRELATION: // 滚动相关性图
-		multipleGraphConfigChartMapping, e := data_manage.GetMultipleGraphConfigChartMappingByChartId(chartInfo.ChartInfoId)
-		if e != nil {
-			msg = "获取失败"
-			errMsg = "获取滚动相关性图表的配置信息失败, Err:" + e.Error()
-			return
-		}
-		baseChartMultipleGraphConfigChartMapping, e := data_manage.GetMultipleGraphConfigChartMappingByIdAndSource(multipleGraphConfigChartMapping.MultipleGraphConfigId, 2)
-		if e != nil {
-			msg = "获取失败"
-			errMsg = "获取相关性图表的配置信息失败, Err:" + e.Error()
-			return
-		}
-		baseChartCorrelationChart := new(data_manage.ChartInfoCorrelation)
-		if e := baseChartCorrelationChart.GetItemById(baseChartMultipleGraphConfigChartMapping.ChartInfoId); e != nil {
-			msg = "获取失败"
-			errMsg = "获取基础相关性图表信息失败, Err:" + e.Error()
-			return
-		}
-		moveUnitDays, ok := utils.FrequencyDaysMap[baseChartCorrelationChart.CalculateUnit]
-		if !ok {
-			msg = "错误的分析周期"
-			errMsg = "错误的分析周期"
-			return
-		}
-		startDate := time.Now().AddDate(0, 0, -baseChartCorrelationChart.CalculateValue*moveUnitDays).Format(utils.FormatDate)
-		endDate := time.Now().Format(utils.FormatDate)
+		startDate, endDate := utils.GetDateByDateType(correlationChart.DateType, correlationChart.StartDate.Format(utils.FormatDate), correlationChart.EndDate.Format(utils.FormatDate))
+
 		xDateTimeValue, yDataList, e = correlationServ.GetRollingCorrelationChartDataByEdbInfo(edbInfoMappingA, edbInfoMappingB, correlationChart.LeadValue, correlationChart.LeadUnit, correlationChart.CalculateValue, correlationChart.CalculateUnit, startDate, endDate)
 		dataResp = data_manage.RollingCorrelationChartDataResp{
 			XDateTimeValue: xDateTimeValue,

+ 1 - 0
models/data_manage/chart_info_correlation.go

@@ -16,6 +16,7 @@ type ChartInfoCorrelation struct {
 	CalculateUnit          string    `description:"计算频度"`
 	BaseCalculateValue     int       `description:"基础计算窗口(滚动相关性的时候用到)"`
 	BaseCalculateUnit      string    `description:"基础计算频度(滚动相关性的时候用到)"`
+	DateType               int       `description:"日期类型:1:00年至今,2:10年至今,3:15年至今,4:年初至今,5:自定义时间,6:起始日期至今"`
 	StartDate              time.Time `description:"开始日期"`
 	EndDate                time.Time `description:"结束日期"`
 	EdbInfoIdFirst         int       `description:"A指标ID"`

+ 296 - 3
utils/common.go

@@ -635,6 +635,17 @@ func TrimStr(str string) (str2 string) {
 	return strings.Replace(str, " ", "", -1)
 }
 
+// TrimLRStr 移除字符串前后的空格
+func TrimLRStr(str string) (str2 string) {
+	if str == "" {
+		return str
+	}
+	str2 = strings.TrimLeft(str, " ")
+	str2 = strings.TrimRight(str2, " ")
+
+	return
+}
+
 // 字符串转换为time
 func StrTimeToTime(strTime string) time.Time {
 	timeLayout := "2006-01-02 15:04:05"  //转化所需模板
@@ -695,7 +706,11 @@ func TimeRemoveHms(strTime string) string {
 	year := resultTime.Year()
 	month := resultTime.Format("01")
 	day1 := resultTime.Day()
-	Ymd = strconv.Itoa(year) + "." + month + "." + strconv.Itoa(day1)
+	if day1 < 10 {
+		Ymd = strconv.Itoa(year) + "." + month + ".0" + strconv.Itoa(day1)
+	} else {
+		Ymd = strconv.Itoa(year) + "." + month + "." + strconv.Itoa(day1)
+	}
 	return Ymd
 }
 
@@ -706,7 +721,11 @@ func TimeRemoveHms2(strTime string) string {
 	year := resultTime.Year()
 	month := resultTime.Format("01")
 	day1 := resultTime.Day()
-	Ymd = strconv.Itoa(year) + "-" + month + "-" + strconv.Itoa(day1)
+	if day1 < 10 {
+		Ymd = strconv.Itoa(year) + "-" + month + "-0" + strconv.Itoa(day1)
+	} else {
+		Ymd = strconv.Itoa(year) + "-" + month + "-" + strconv.Itoa(day1)
+	}
 	return Ymd
 }
 
@@ -1329,7 +1348,7 @@ func StrDateToDate(strTime string) time.Time {
 	return resultTime
 }
 
-// GetTimeSubDay 计算两个时间的自然日期差
+// GetTimeSubDay 计算两个时间的自然日期差(后面减去前面)
 func GetTimeSubDay(t1, t2 time.Time) int {
 	var day int
 	swap := false
@@ -1389,6 +1408,25 @@ func MinusInt(a []int, b []int) []int {
 	return diff
 }
 
+// IntersectInt 获取两个[]int交集
+func IntersectInt(nums1 []int, nums2 []int) []int {
+	m := make(map[int]int)
+	var res []int
+
+	for _, num := range nums1 {
+		m[num]++
+	}
+
+	for _, num := range nums2 {
+		if m[num] > 0 {
+			res = append(res, num)
+			m[num]--
+		}
+	}
+
+	return res
+}
+
 // MapSorter 对于map 排序
 type MapSorter []Item
 
@@ -1476,6 +1514,112 @@ func GetDaysBetween2Date(format, date1Str, date2Str string) (int, error) {
 	return int(date1.Sub(date2).Hours() / 24), nil
 }
 
+// GetFrequencyEn 获取频度的英文版
+func GetFrequencyEn(frequency string) (frequencyEn string) {
+	switch frequency {
+	case "日度":
+		frequencyEn = "day"
+		return
+	case "周度":
+		frequencyEn = "week"
+		return
+	case "旬度":
+		frequencyEn = "ten days"
+		return
+	case "月度":
+		frequencyEn = "month"
+		return
+	case "季度":
+		frequencyEn = "quarter"
+		return
+	case "年度":
+		frequencyEn = "year"
+		return
+	}
+	return
+}
+
+// GetLeadUnitEn 获取移动单位的英文版
+func GetLeadUnitEn(unit string) (unitEn string) {
+	switch unit {
+	case "天":
+		unitEn = "day"
+		return
+	case "周":
+		unitEn = "week"
+		return
+	case "月":
+		unitEn = "month"
+		return
+	case "季":
+		unitEn = "quarter"
+		return
+	case "年":
+		unitEn = "year"
+		return
+	}
+	return
+}
+
+// GetDateByDateType 通过dateType获取需要的开始/结束日期
+func GetDateByDateType(dateType int, tmpStartDate, tmpEndDate string) (startDate, endDate string) {
+	startDate = tmpStartDate
+	endDate = tmpEndDate
+	switch dateType {
+	case 1:
+		startDate = "2000-01-01"
+		endDate = ""
+	case 2:
+		startDate = "2010-01-01"
+		endDate = ""
+	case 3:
+		startDate = "2015-01-01"
+		endDate = ""
+	case 4:
+		//startDate = strconv.Itoa(time.Now().Year()) + "-01-01"
+		startDate = "2021-01-01"
+		endDate = ""
+	case 5:
+		//startDate = startDate + "-01"
+		//endDate = endDate + "-01"
+	case 6:
+		//startDate = startDate + "-01"
+		endDate = ""
+	case 7:
+		startDate = "2018-01-01"
+		endDate = ""
+	case 8:
+		startDate = "2019-01-01"
+		endDate = ""
+	case 9:
+		startDate = "2020-01-01"
+		endDate = ""
+	case 11:
+		startDate = "2022-01-01"
+		endDate = ""
+	}
+
+	// 兼容日期错误
+	{
+		if strings.Count(startDate, "-") == 1 {
+			startDate = startDate + "-01"
+		}
+		if strings.Count(endDate, "-") == 1 {
+			endDate = endDate + "-01"
+		}
+	}
+
+	return
+}
+
+func TimeTransferString(format string, t time.Time) string {
+	str := t.Format(format)
+	if t.IsZero() {
+		return ""
+	}
+	return str
+}
+
 // GetDateByDateType2 通过dateType获取需要的开始/结束日期(日期类型:1:最近3月;2:最近6月;3:最近1年;4:最近2年;5:最近3年;6:最近5年;7:最近10年,8:自定义时间)
 func GetDateByDateType2(dateType int, currDate time.Time) (startDate time.Time) {
 	switch dateType {
@@ -1682,3 +1826,152 @@ func removeDecimalPoint(str string) string {
 	str = str[strings.Index(str, ".")+1:]
 	return str
 }
+
+// GetPredictEdbDayListByEndDate 根据截止日期获取预测指标日期列表
+func GetPredictEdbDayListByEndDate(startDate, endDate time.Time, frequency string) (dayList []time.Time) {
+	//if !utils.InArrayByStr([]string{"日度", "周度", "月度"}, frequency)
+	switch frequency {
+	case "日度":
+		for currDate := startDate.AddDate(0, 0, 1); currDate.Before(endDate) || currDate.Equal(endDate); currDate = currDate.AddDate(0, 0, 1) {
+			//周六、日排除
+			if currDate.Weekday() == time.Sunday || currDate.Weekday() == time.Saturday {
+				continue
+			}
+			dayList = append(dayList, currDate)
+		}
+	case "周度":
+		//nextDate := startDate.AddDate(0, 0, 7)
+		for currDate := startDate.AddDate(0, 0, 7); currDate.Before(endDate) || currDate.Equal(endDate); currDate = currDate.AddDate(0, 0, 7) {
+			dayList = append(dayList, currDate)
+		}
+	case "旬度":
+		for currDate := startDate.AddDate(0, 0, 1); currDate.Before(endDate) || currDate.Equal(endDate); {
+			nextDate := currDate.AddDate(0, 0, 1)
+			//每个月的10号、20号、最后一天,那么就写入
+			if nextDate.Day() == 11 || nextDate.Day() == 21 || nextDate.Day() == 1 {
+				dayList = append(dayList, currDate)
+			}
+			currDate = nextDate
+		}
+	case "月度":
+		for currDate := startDate; currDate.Before(endDate) || currDate.Equal(endDate); {
+			currDate = time.Date(currDate.Year(), currDate.Month(), 1, 0, 0, 0, 0, time.Now().Location()).AddDate(0, 1, -1)
+			if !currDate.After(endDate) && !currDate.Equal(startDate) {
+				dayList = append(dayList, currDate)
+			}
+			currDate = currDate.AddDate(0, 0, 1)
+		}
+	case "季度":
+		for currDate := startDate; currDate.Before(endDate) || currDate.Equal(endDate); {
+			// 每月的最后一天
+			currDate = time.Date(currDate.Year(), currDate.Month(), 1, 0, 0, 0, 0, time.Now().Location()).AddDate(0, 1, -1)
+			if !currDate.After(endDate) && !currDate.Equal(startDate) {
+				// 季度日期就写入,否则不写入
+				if currDate.Month() == 3 || currDate.Month() == 6 || currDate.Month() == 9 || currDate.Month() == 12 {
+					dayList = append(dayList, currDate)
+				}
+			}
+			currDate = currDate.AddDate(0, 0, 1)
+		}
+	case "半年度":
+		for currDate := startDate; currDate.Before(endDate) || currDate.Equal(endDate); {
+			// 每月的最后一天
+			currDate = time.Date(currDate.Year(), currDate.Month(), 1, 0, 0, 0, 0, time.Now().Location()).AddDate(0, 1, -1)
+			if !currDate.After(endDate) && !currDate.Equal(startDate) {
+				// 半年度日期就写入,否则不写入
+				if currDate.Month() == 6 || currDate.Month() == 12 {
+					dayList = append(dayList, currDate)
+				}
+			}
+			currDate = currDate.AddDate(0, 0, 1)
+		}
+	case "年度":
+		for currDate := startDate; currDate.Before(endDate) || currDate.Equal(endDate); {
+			currDate = time.Date(currDate.Year()+1, 12, 31, 0, 0, 0, 0, time.Now().Location())
+			if !currDate.After(endDate) && !currDate.Equal(startDate) {
+				dayList = append(dayList, currDate)
+			}
+		}
+	}
+	return
+}
+
+// GetPredictEdbDayListByNum 根据期数获取预测指标日期列表
+func GetPredictEdbDayListByNum(startDate time.Time, num int, frequency string) (dayList []time.Time) {
+	switch frequency {
+	case "日度":
+		for i := 1; i <= num; {
+			currDate := startDate.AddDate(0, 0, i)
+			//周六、日排除
+			if currDate.Weekday() == time.Sunday || currDate.Weekday() == time.Saturday {
+				continue
+			}
+			dayList = append(dayList, currDate)
+			i++
+		}
+	case "周度":
+		for i := 1; i <= num; {
+			dayList = append(dayList, startDate.AddDate(0, 0, i*7))
+			i++
+		}
+	case "旬度":
+		currDate := startDate
+		for i := 1; i <= num; {
+			nextDate := currDate.AddDate(0, 0, 1)
+			//每个月的10号、20号、最后一天,那么就写入
+			if nextDate.Day() == 11 || nextDate.Day() == 21 || nextDate.Day() == 1 {
+				dayList = append(dayList, currDate)
+				i++
+			}
+			currDate = nextDate
+		}
+	case "月度":
+		currDate := startDate
+		for i := 1; i <= num; {
+			currDate = time.Date(currDate.Year(), currDate.Month(), 1, 0, 0, 0, 0, time.Now().Location()).AddDate(0, 1, -1)
+			if !currDate.Equal(startDate) {
+				dayList = append(dayList, currDate)
+				i++
+			}
+			currDate = currDate.AddDate(0, 0, 1)
+		}
+	case "季度":
+		currDate := startDate
+		for i := 1; i <= num; {
+			// 每月的最后一天
+			currDate = time.Date(currDate.Year(), currDate.Month(), 1, 0, 0, 0, 0, time.Now().Location()).AddDate(0, 1, -1)
+			if !currDate.Equal(startDate) {
+				// 季度日期就写入,否则不写入
+				if currDate.Month() == 3 || currDate.Month() == 6 || currDate.Month() == 9 || currDate.Month() == 12 {
+					dayList = append(dayList, currDate)
+					i++
+				}
+			}
+			currDate = currDate.AddDate(0, 0, 1)
+		}
+	case "半年度":
+		currDate := startDate
+		for i := 1; i <= num; {
+			// 每月的最后一天
+			currDate = time.Date(currDate.Year(), currDate.Month(), 1, 0, 0, 0, 0, time.Now().Location()).AddDate(0, 1, -1)
+			if !currDate.Equal(startDate) {
+				// 半年度日期就写入,否则不写入
+				if currDate.Month() == 6 || currDate.Month() == 12 {
+					dayList = append(dayList, currDate)
+					i++
+				}
+			}
+			currDate = currDate.AddDate(0, 0, 1)
+		}
+	case "年度":
+		currDate := startDate
+		for i := 1; i <= num; {
+			currDate = time.Date(currDate.Year()+1, 12, 31, 0, 0, 0, 0, time.Now().Location())
+			if !currDate.Equal(startDate) {
+				dayList = append(dayList, currDate)
+				i++
+			}
+		}
+	}
+	return
+}