Browse Source

Merge branch 'master' into feature/eta_forum3_chart_classify

xyxie 4 days ago
parent
commit
bac3b15b52
4 changed files with 58 additions and 171 deletions
  1. 9 4
      controllers/chart_info.go
  2. 0 160
      services/chart_info_show.go
  3. 40 6
      utils/common.go
  4. 9 1
      utils/constants.go

+ 9 - 4
controllers/chart_info.go

@@ -462,7 +462,7 @@ func GetChartInfoDetailFromUniqueCode(chartInfo *models.ChartInfoView, key strin
 			dateType = 3
 		}
 	}
-	yearMax := 0
+	var dateMax time.Time
 	if dateType == utils.DateTypeNYears {
 		for _, v := range mappingList {
 			if v.LatestDate != "" && v.LatestDate != "0000-00-00" {
@@ -472,13 +472,18 @@ func GetChartInfoDetailFromUniqueCode(chartInfo *models.ChartInfoView, key strin
 					errMsg = "获取图表日期信息失败,Err:" + tErr.Error()
 					return
 				}
-				if lastDateT.Year() > yearMax {
-					yearMax = lastDateT.Year()
+				if lastDateT.After(dateMax) {
+					dateMax = lastDateT
 				}
 			}
 		}
 	}
-	startDate, endDate = utils.GetDateByDateTypeV2(dateType, startDate, endDate, startYear, yearMax)
+	if chartType == utils.CHART_TYPE_SEASON && dateType == utils.DateTypeNYears {
+		// 季度图,的至今N年,需要特殊处理,将日期范围扩大到下一年
+		dateMax = time.Date(dateMax.Year()+1, 1, 1, 0, 0, 0, 0, time.Local)
+	}
+	// 开始/结束日期
+	startDate, endDate = utils.GetDateByDateTypeV2(dateType, startDate, endDate, startYear, dateMax)
 	if chartInfo.ChartType == 2 {
 		chartInfo.StartDate = startDate
 		chartInfo.EndDate = endDate

+ 0 - 160
services/chart_info_show.go

@@ -11,7 +11,6 @@ import (
 	"math"
 	"sort"
 	"strconv"
-	"strings"
 	"time"
 )
 
@@ -2397,162 +2396,3 @@ func SeasonChartData(dataList []*models.ChartEdbInfoMapping, seasonExtraConfig s
 
 	return
 }
-
-// GetChartDetailDataByChartInfoId 根据图表信息id获取图表详情数据
-func GetChartDetailDataByChartInfoId(chartInfo *models.ChartInfoView, isCache bool) (resp *models.ChartInfoDetailResp, err error, errMsg string) {
-	// 添加至缓存中
-	//是否走缓存
-	chartInfoId := chartInfo.ChartInfoId
-	resp = new(models.ChartInfoDetailResp)
-
-	//判断是否存在缓存,如果存在缓存,那么直接从缓存中获取
-	key := fmt.Sprint(utils.CACHE_CHART_INFO_DATA, chartInfo.ChartInfoId)
-	if utils.Re == nil && isCache {
-		if utils.Re == nil && utils.Rc.IsExist(key) {
-			if chartData, err1 := utils.Rc.RedisBytes(key); err1 == nil {
-				err = json.Unmarshal(chartData, &resp)
-				if err != nil || resp == nil {
-					return
-				}
-				return
-			}
-		}
-	}
-	chartType := chartInfo.ChartType
-
-	startDate := chartInfo.StartDate
-	endDate := chartInfo.EndDate
-	seasonStartDate := chartInfo.SeasonStartDate
-	seasonEndDate := chartInfo.SeasonEndDate
-	startYear := chartInfo.StartYear
-
-	calendar := chartInfo.Calendar
-
-	dateType := chartInfo.DateType
-
-	// 获取主题样式
-	chartTheme, err := GetChartThemeConfig(chartInfo.ChartThemeId, chartInfo.Source, chartInfo.ChartType)
-	if err != nil {
-		errMsg = "获取失败"
-		err = fmt.Errorf("获取主题信息失败,Err:" + err.Error())
-		return
-	}
-	chartInfo.ChartThemeStyle = chartTheme.Config
-	chartInfo.ChartThemeId = chartTheme.ChartThemeId
-
-	mappingList := make([]*models.ChartEdbInfoMapping, 0)
-
-	mappingList, err = models.GetChartEdbMappingList(chartInfoId)
-	if err != nil {
-		errMsg = "获取失败"
-		err = fmt.Errorf("获取图表,指标信息失败,Err:" + err.Error())
-		return
-	}
-
-	// 图表额外数据参数
-	extraConfigStr := chartInfo.ExtraConfig
-	// 柱方图的一些配置
-	var barConfig models.BarChartInfoReq
-	if chartInfo != nil && chartInfo.ChartType == 7 {
-		if chartInfo.BarConfig == `` {
-			errMsg = "柱方图未配置"
-			err = fmt.Errorf("柱方图未配置")
-			return
-		}
-		err = json.Unmarshal([]byte(chartInfo.BarConfig), &barConfig)
-		if err != nil {
-			errMsg = "柱方图配置异常"
-			err = fmt.Errorf("柱方图配置异常")
-			return
-		}
-		extraConfigStr = chartInfo.BarConfig
-	}
-	if chartType == 2 {
-		startDate = seasonStartDate
-		endDate = seasonEndDate
-		if dateType <= 0 {
-			if startDate != "" {
-				dateType = 5
-			} else {
-				dateType = utils.DateTypeNYears
-			}
-		}
-	} else {
-		if dateType <= 0 {
-			dateType = 3
-		}
-	}
-	yearMax := 0
-	if dateType == utils.DateTypeNYears {
-		for _, v := range mappingList {
-			if v.LatestDate != "" {
-				lastDateT, tErr := time.Parse(utils.FormatDate, v.LatestDate)
-				if tErr != nil {
-					errMsg = "获取失败"
-					err = fmt.Errorf("获取图表日期信息失败,Err:" + tErr.Error())
-					return
-				}
-				if lastDateT.Year() > yearMax {
-					yearMax = lastDateT.Year()
-				}
-			}
-		}
-	}
-	startDate, endDate = utils.GetDateByDateTypeV2(dateType, startDate, endDate, startYear, yearMax)
-
-	if chartInfo.ChartType == 2 {
-		chartInfo.StartDate = startDate
-		chartInfo.EndDate = endDate
-		chartInfo.Calendar = calendar
-	}
-
-	// 获取图表中的指标数据
-	edbList, xEdbIdValue, yDataList, dataResp, err, errMsg := GetChartEdbData(chartInfoId, chartType, calendar, startDate, endDate, mappingList, extraConfigStr, chartInfo.SeasonExtraConfig)
-	if err != nil {
-		if errMsg == `` {
-			errMsg = "获取失败"
-		}
-		err = fmt.Errorf("获取图表,指标信息失败,Err:" + err.Error())
-		return
-	}
-	// 单位
-	if chartType == utils.CHART_TYPE_BAR && len(yDataList) > 0 {
-		chartInfo.Unit = yDataList[0].Unit
-		chartInfo.UnitEn = yDataList[0].UnitEn
-	}
-	warnEdbList := make([]string, 0)
-	for _, v := range edbList {
-		if v.IsNullData {
-			warnEdbList = append(warnEdbList, v.EdbName+"("+v.EdbCode+")")
-		}
-	}
-	if len(warnEdbList) > 0 {
-		chartInfo.WarnMsg = `图表引用指标异常,异常指标:` + strings.Join(warnEdbList, ",")
-	}
-	// 图表的指标来源
-	sourceNameList, sourceNameEnList := GetEdbSourceByEdbInfoIdList(edbList)
-	chartInfo.ChartSource = strings.Join(sourceNameList, ",")
-	chartInfo.ChartSourceEn = strings.Join(sourceNameEnList, ",")
-	//判断是否需要展示英文标识
-	chartInfo.IsEnChart = CheckIsEnChart(chartInfo.ChartNameEn, edbList, chartInfo.Source, chartInfo.ChartType)
-
-	resp.EdbInfoList = edbList
-	resp.XEdbIdValue = xEdbIdValue
-	resp.YDataList = yDataList
-	resp.DataResp = dataResp
-
-	chartInfo.Button = models.ChartViewButton{
-		IsEdit:    chartInfo.IsEdit,
-		IsEnChart: chartInfo.IsEnChart,
-		IsAdd:     chartInfo.IsAdd,
-		IsCopy:    true,
-		IsSetName: chartInfo.IsSetName,
-	}
-	resp.ChartInfo = chartInfo
-	// 将数据加入缓存
-	if utils.Re == nil {
-		d, _ := json.Marshal(resp)
-		_ = utils.Rc.Put(key, d, 2*time.Hour)
-	}
-	return
-}

+ 40 - 6
utils/common.go

@@ -1213,7 +1213,7 @@ func GetLikeKeyword(keyword string) string {
 	return `%` + keyword + `%`
 }
 
-func GetDateByDateTypeV2(dateType int, tmpStartDate, tmpEndDate string, startYear, yearMax int) (startDate, endDate string) {
+func GetDateByDateTypeV2(dateType int, tmpStartDate, tmpEndDate string, startYear int, latestDate time.Time) (startDate, endDate string) {
 	startDate = tmpStartDate
 	endDate = tmpEndDate
 	switch dateType {
@@ -1248,16 +1248,46 @@ func GetDateByDateTypeV2(dateType int, tmpStartDate, tmpEndDate string, startYea
 	case 11:
 		startDate = "2022-01-01"
 		endDate = ""
+	case 12:
+		startDate = "2023-01-01"
+		endDate = ""
+	case 13:
+		startDate = "2024-01-01"
+		endDate = ""
 	case DateTypeNYears:
 		if startYear == 0 { //默认取最近5年
 			startYear = 5
 		}
-		if yearMax == 0 {
+		if latestDate.IsZero() {
 			return
 		}
-		startYear = startYear - 1
-		baseDate, _ := time.Parse(FormatDate, fmt.Sprintf("%d-01-01", yearMax))
-		startDate = baseDate.AddDate(-startYear, 0, 0).Format(FormatDate)
+		startDate = latestDate.AddDate(-startYear, 0, 0).Format(FormatDate)
+		endDate = ""
+	case DateTypeOneWeek:
+		//if startDate != "" {
+		//	st, e := time.ParseInLocation(FormatDate, startDate, time.Local)
+		//	if e != nil {
+		//		FileLog.Info(fmt.Sprintf("日期格式解析失败, %s, %v", startDate, e))
+		//		return
+		//	}
+		//	startDate = st.AddDate(0, 0, -7).Format(FormatDate)
+		//}
+		startDate = time.Now().AddDate(0, 0, -7).Format(FormatDate)
+		endDate = ""
+	case DateTypeOneMonth:
+		startDate = time.Now().AddDate(0, -1, 0).Format(FormatDate)
+		endDate = ""
+	case DateTypeTwoMonth:
+		startDate = time.Now().AddDate(0, -2, 0).Format(FormatDate)
+		endDate = ""
+	case DateTypeThreeMonth:
+		startDate = time.Now().AddDate(0, -3, 0).Format(FormatDate)
+		endDate = ""
+	case DateTypeNMonth:
+		if startYear == 0 {
+			startYear = 6
+		}
+		startDate = time.Now().AddDate(0, -startYear, 0).Format(FormatDate)
 		endDate = ""
 	}
 
@@ -1267,7 +1297,11 @@ func GetDateByDateTypeV2(dateType int, tmpStartDate, tmpEndDate string, startYea
 			startDate = startDate + "-01"
 		}
 		if strings.Count(endDate, "-") == 1 {
-			endDate = endDate + "-01"
+			endTime, err := time.Parse(FormatYearMonthDate, endDate)
+			if err != nil {
+				return
+			}
+			endDate = endTime.AddDate(0, 1, -1).Format(FormatDate)
 		}
 	}
 

+ 9 - 1
utils/constants.go

@@ -19,7 +19,15 @@ const (
 	PageSize20                 = 20
 	PageSize30                 = 30
 )
-const DateTypeNYears = 20 //时间类型为最近N年
+const (
+	DateTypeNYears     = 20 //时间类型为最近N年
+	DateTypeOneWeek    = 21 // 时间类型-近1周
+	DateTypeOneMonth   = 22 // 时间类型-近1月
+	DateTypeTwoMonth   = 23 // 时间类型-近2月
+	DateTypeThreeMonth = 24 // 时间类型-近3月
+	DateTypeNMonth     = 25 // 时间类型-近N月
+)
+
 // 数据来源渠道
 const (
 	DATA_SOURCE_THS                                  = iota + 1 //同花顺