package chart

import (
	"context"
	"encoding/json"
	"errors"
	"fmt"
	"github.com/shopspring/decimal"
	"hongze/hongze_yb/global"
	"hongze/hongze_yb/models/request"
	"hongze/hongze_yb/models/response/chart_info"
	chartEdbMappingModel "hongze/hongze_yb/models/tables/chart_edb_mapping"
	chartInfoModel "hongze/hongze_yb/models/tables/chart_info"
	"hongze/hongze_yb/models/tables/chart_series"
	"hongze/hongze_yb/models/tables/chart_series_edb_mapping"
	company2 "hongze/hongze_yb/models/tables/company"
	edbDataModel "hongze/hongze_yb/models/tables/edb_data"
	edbInfoModel "hongze/hongze_yb/models/tables/edb_info"
	"hongze/hongze_yb/models/tables/my_chart_classify"
	"hongze/hongze_yb/models/tables/yb_chart_daily_visit_log"
	"hongze/hongze_yb/models/tables/yb_chart_visit_log"
	"hongze/hongze_yb/services/alarm_msg"
	edbDataService "hongze/hongze_yb/services/edb_data"
	"hongze/hongze_yb/services/user"
	"hongze/hongze_yb/utils"
	"math"
	"sort"
	"strconv"
	"strings"
	"time"
)

// ModifyChartInfoAndMapping 修改图表信息
func ModifyChartInfoAndMapping(edbInfoIdStr string, req *chartInfoModel.SaveChartInfoReq, chartType int, chartEdbInfoList []*chartInfoModel.ChartSaveItem) (err error) {
	// 开启事务
	tx := global.MYSQL["data"].Begin()
	defer func() {
		if err != nil {
			tx.Rollback()
		} else {
			tx.Commit()
		}
	}()

	// 更新图表信息
	if chartType != 2 {
		updateStr := `
            	edb_info_ids = ?,
				modify_time = NOW(),
            	date_type = ?,
				start_date = ?,
				end_date = ?,
 				left_min = ?,
				left_max = ?,
				right_min = ?,
				right_max = ?`
		pars := []interface{}{edbInfoIdStr, req.DateType, req.StartDate, req.EndDate, req.LeftMin, req.LeftMax, req.RightMin, req.RightMax}

		if req.ExtraConfig != `` {
			updateStr += `,extra_config=? `
			pars = append(pars, req.ExtraConfig)
		}
		pars = append(pars, req.ChartInfoId)
		sql := ` UPDATE  chart_info SET ` + updateStr + ` WHERE chart_info_id = ?`
		err = tx.Exec(sql, pars...).Error
	} else {
		updateStr := `edb_info_ids = ?,
				modify_time = NOW(),
				date_type = ?,
				calendar = ?,
				start_date = ?,
				end_date = ?,
				season_start_date = ?,
				season_end_date = ?,
				left_min = ?,
				left_max = ?,
				right_min = ?,
				right_max = ?`
		pars := []interface{}{edbInfoIdStr, req.DateType, req.Calendar, req.StartDate, req.EndDate, req.StartDate, req.EndDate, req.LeftMin, req.LeftMax, req.RightMin, req.RightMax}
		if req.ExtraConfig != `` {
			updateStr += `,extra_config=? `
			pars = append(pars, req.ExtraConfig)
		}
		pars = append(pars, req.ChartInfoId)
		sql := ` UPDATE  chart_info SET ` + updateStr + ` WHERE chart_info_id = ?`
		err = tx.Exec(sql, pars...).Error
	}
	if err != nil {
		fmt.Println("UPDATE chart_info Err:", err.Error())
		return
	}

	// 修改图表关联指标
	chartEdbMappingIdList := make([]string, 0)
	for _, v := range chartEdbInfoList {
		var tmpChartEdbMapping *chartEdbMappingModel.ChartEdbMapping
		csql := `SELECT *  FROM chart_edb_mapping WHERE chart_info_id=? AND edb_info_id=? AND source = ? `
		err = tx.Raw(csql, req.ChartInfoId, v.EdbInfoId, utils.CHART_SOURCE_DEFAULT).First(&tmpChartEdbMapping).Error
		if err != nil && err != utils.ErrNoRow {
			fmt.Println("QueryRow Err:", err.Error())
			return err
		}
		if tmpChartEdbMapping == nil || tmpChartEdbMapping.ChartEdbMappingId == 0 {
			mapItem := new(chartEdbMappingModel.ChartEdbMapping)
			mapItem.ChartInfoId = req.ChartInfoId
			mapItem.EdbInfoId = v.EdbInfoId
			mapItem.CreateTime = time.Now()
			mapItem.ModifyTime = time.Now()
			timestamp := strconv.FormatInt(time.Now().UnixNano(), 10)
			mapItem.UniqueCode = utils.MD5(utils.CHART_PREFIX + "_" + timestamp)
			mapItem.MaxData = v.MaxData
			mapItem.MinData = v.MinData
			mapItem.IsOrder = 0
			if v.IsOrder {
				mapItem.IsOrder = 1
			}
			mapItem.IsAxis = v.IsAxis
			mapItem.EdbInfoType = v.EdbInfoType
			mapItem.LeadValue = v.LeadValue
			mapItem.LeadUnit = v.LeadUnit
			mapItem.ChartStyle = v.ChartStyle
			mapItem.ChartColor = v.ChartColor
			mapItem.ChartWidth = v.ChartWidth
			mapItem.Source = utils.CHART_SOURCE_DEFAULT
			err = tx.Create(mapItem).Error
			if err != nil {
				return
			}
			chartEdbMappingIdList = append(chartEdbMappingIdList, strconv.Itoa(mapItem.ChartEdbMappingId))
		} else {
			chartEdbMappingIdList = append(chartEdbMappingIdList, strconv.Itoa(tmpChartEdbMapping.ChartEdbMappingId))

			tmpChartEdbMapping.ModifyTime = time.Now()
			tmpChartEdbMapping.MaxData = v.MaxData
			tmpChartEdbMapping.MinData = v.MinData
			tmpChartEdbMapping.IsOrder = 0
			if v.IsOrder {
				tmpChartEdbMapping.IsOrder = 1
			}
			tmpChartEdbMapping.IsAxis = v.IsAxis
			tmpChartEdbMapping.EdbInfoType = v.EdbInfoType
			tmpChartEdbMapping.LeadValue = v.LeadValue
			tmpChartEdbMapping.LeadUnit = v.LeadUnit
			tmpChartEdbMapping.ChartStyle = v.ChartStyle
			tmpChartEdbMapping.ChartColor = v.ChartColor
			//tmpChartEdbMapping.PredictChartColor = v.PredictChartColor
			tmpChartEdbMapping.ChartWidth = v.ChartWidth
			err = tx.Model(tmpChartEdbMapping).Select([]string{"ModifyTime", "MaxData", "MinData", "IsOrder", "IsAxis", "EdbInfoType", "LeadValue", "LeadUnit", "ChartStyle", "ChartColor", "PredictChartColor", "ChartWidth"}).Updates(*tmpChartEdbMapping).Error
			if err != nil {
				fmt.Println("chart_edb_mapping Err:" + err.Error())
				return err
			}
		}

	}

	// 清除原图表指标
	if len(chartEdbMappingIdList) > 0 {
		chartEdbMappingIdStr := strings.Join(chartEdbMappingIdList, ",")
		dsql := `DELETE FROM chart_edb_mapping WHERE chart_info_id=? AND chart_edb_mapping_id NOT IN(` + chartEdbMappingIdStr + `)`
		err = tx.Exec(dsql, req.ChartInfoId).Error
		if err != nil {
			fmt.Println("delete err:" + err.Error())
			return err
		}
	}

	// 清除缓存
	{
		key := GetChartInfoDataKey(req.ChartInfoId)
		_ = global.Redis.Del(context.TODO(), key)
	}
	return
}

// GetChartInfoRefreshData 获取需要刷新的指标
func GetChartInfoRefreshData(chartInfoId int) (baseEdbInfoArr, calculateInfoArr []*edbInfoModel.EdbInfo, err error) {
	sql := ` SELECT b.* FROM chart_edb_mapping AS a
		   INNER JOIN edb_info AS b ON a.edb_info_id=b.edb_info_id 
		   WHERE a.chart_info_id=? `
	edbInfoList := make([]*edbInfoModel.EdbInfo, 0)
	err = global.MYSQL["data"].Raw(sql, chartInfoId).Scan(&edbInfoList).Error
	if err != nil {
		return
	}

	for _, v := range edbInfoList {
		fmt.Println(v.EdbInfoId, v.EdbType)
		if v.EdbType == 1 {
			baseEdbInfoArr = append(baseEdbInfoArr, v)
		} else {
			calculateInfoArr = append(calculateInfoArr, v)
			newBaseEdbInfoArr, newCalculateInfoArr, err := GetChartRefreshEdbInfo(v.EdbInfoId, v.Source, 0)
			if err != nil {
				return baseEdbInfoArr, calculateInfoArr, err
			}
			baseEdbInfoArr = append(baseEdbInfoArr, newBaseEdbInfoArr...)
			calculateInfoArr = append(calculateInfoArr, newCalculateInfoArr...)
		}
	}

	return
}

// GetChartRefreshEdbInfo
func GetChartRefreshEdbInfo(edbInfoId, source, n int) (baseEdbInfoArr, calculateInfoArr []*edbInfoModel.EdbInfo, err error) {
	calculateList, err := GetEdbInfoCalculateMap(edbInfoId, source)
	if err != nil && err != utils.ErrNoRow {
		return
	}
	n++
	for _, item := range calculateList {
		fmt.Println(item.EdbInfoId)
		if item.EdbType == 1 {
			baseEdbInfoArr = append(baseEdbInfoArr, item)
		} else {
			calculateInfoArr = append(calculateInfoArr, item)
			if n > 10 {
				return
			}
			newBaseEdbInfoArr, newCalculateInfoArr, _ := GetChartRefreshEdbInfo(item.EdbInfoId, item.Source, n)
			baseEdbInfoArr = append(baseEdbInfoArr, newBaseEdbInfoArr...)
			calculateInfoArr = append(calculateInfoArr, newCalculateInfoArr...)
		}
	}

	return
}

func GetEdbInfoCalculateMap(edbInfoId, source int) (list []*edbInfoModel.EdbInfo, err error) {
	sql := ` SELECT b.* FROM edb_info_calculate_mapping AS a
			INNER JOIN edb_info AS b ON a.from_edb_info_id=b.edb_info_id
			WHERE a.edb_info_id=? ORDER BY sort ASC `
	err = global.MYSQL["data"].Exec(sql, edbInfoId).Error

	return
}

// SaveChartVisitLog 保存图表访问记录
func SaveChartVisitLog(userInfo user.UserInfo, chartInfo *chartInfoModel.ChartInfoView, myChartClassifyId int) (err error) {
	userId := int(userInfo.UserID)
	companyId := int(userInfo.CompanyID)
	chartInfoId := chartInfo.ChartInfoId
	// 访问记录
	visitLog := new(yb_chart_visit_log.YbChartVisitLog)
	visitLog.CompanyId = companyId
	companyInfo, err := company2.GetByCompanyId(userInfo.CompanyID)
	if err == nil {
		visitLog.CompanyName = companyInfo.CompanyName
	}
	visitLog.UserId = userId
	visitLog.RealName = userInfo.RealName
	visitLog.Mobile = userInfo.Mobile
	visitLog.Email = userInfo.Email
	visitLog.ChartInfoId = chartInfoId
	visitLog.ChartName = chartInfo.ChartName
	visitLog.CreateTime = time.Now().Local()
	visitLog.Create()

	// 每日访问记录,用户/天/图仅记录一次
	if myChartClassifyId > 0 {
		// 今日是否已访问
		nowTime := time.Now().Local()
		startTime := time.Date(nowTime.Year(), nowTime.Month(), nowTime.Day(), 0, 0, 0, 0, time.Local)
		endTime := time.Date(nowTime.Year(), nowTime.Month(), nowTime.Day(), 23, 59, 59, 0, time.Local)
		_, tmpErr := yb_chart_daily_visit_log.GetTodayVisitLog(userId, chartInfoId, myChartClassifyId, startTime, endTime)
		if tmpErr == nil {
			return
		}

		myChartClassify, tmpErr := my_chart_classify.GetClassifyById(myChartClassifyId)
		if tmpErr != nil {
			return
		}
		dailyLog := new(yb_chart_daily_visit_log.YbChartDailyVisitLog)
		dailyLog.CompanyId = companyId
		if companyInfo != nil {
			dailyLog.CompanyName = companyInfo.CompanyName
		}
		dailyLog.UserId = userId
		dailyLog.RealName = userInfo.RealName
		dailyLog.Mobile = userInfo.Mobile
		dailyLog.Email = userInfo.Email
		dailyLog.ChartInfoId = chartInfoId
		dailyLog.ChartName = chartInfo.ChartName
		dailyLog.MyChartClassifyId = myChartClassifyId
		dailyLog.MyChartClassifyName = myChartClassify.MyChartClassifyName
		dailyLog.CreateTime = time.Now().Local()
		dailyLog.Create()
	}

	return
}

// RefreshChart 图表刷新
func RefreshChart(chartInfoId int) (err error) {
	var errMsg string
	defer func() {
		if err != nil {
			if global.CONFIG.Serve.RunMode == "release" {
				//go services.SendEmail(utils.APPNAME+"【"+global.CONFIG.Serve.RunMode+"】"+"失败提醒", "RefreshChart:"+errMsg, utils.EmailSendToUsers)
				go alarm_msg.SendAlarmMsg("RefreshChart Err:"+errMsg, 3)
			}
			fmt.Println("RefreshChart Err:" + errMsg)
		}
	}()

	// 获取需要刷新的指标
	baseEdbInfoArr, calculateInfoArr, err := GetChartInfoRefreshData(chartInfoId)
	if err != nil {
		errMsg = "获取需要刷新的指标失败:Err:" + err.Error()
		return
	}

	newBaseEdbInfoArr := make([]*edbInfoModel.EdbInfo, 0)
	baseMap := make(map[int]*edbInfoModel.EdbInfo)
	for _, bv := range baseEdbInfoArr {
		// 如果不是普通指标，那么过滤
		if bv.EdbInfoType != 0 {
			continue
		}
		if _, ok := baseMap[bv.EdbInfoId]; !ok {
			newBaseEdbInfoArr = append(newBaseEdbInfoArr, bv)
		}
		baseMap[bv.EdbInfoId] = bv
	}
	newCalculateInfoArr := make([]*edbInfoModel.EdbInfo, 0)
	calculateMap := make(map[int]*edbInfoModel.EdbInfo)
	var calculateArr []int
	for _, bv := range calculateInfoArr {
		if _, ok := calculateMap[bv.EdbInfoId]; !ok {
			newCalculateInfoArr = append(newCalculateInfoArr, bv)
			calculateArr = append(calculateArr, bv.EdbInfoId)
		}
		calculateMap[bv.EdbInfoId] = bv
	}
	sort.Ints(calculateArr)

	// 刷新指标
	var startDate string
	for _, bv := range newBaseEdbInfoArr {
		source := bv.Source
		subSource := bv.SubSource
		edbInfoId := bv.EdbInfoId
		edbCode := bv.EdbCode
		startDate = bv.StartDate.Format(utils.FormatDate)
		frequency := bv.Frequency
		if startDate == "0000-00-00" {
			continue
		}
		sTime := bv.EndDate

		var limitDay int
		startDate := ""
		switch frequency {
		case "日度":
			limitDay = utils.DATA_START_REFRESH_LIMIT
		case "周度":
			limitDay = utils.DATA_START_REFRESH_LIMIT * 7
		case "月度":
			limitDay = utils.DATA_START_REFRESH_LIMIT * 30
		case "季度":
			limitDay = utils.DATA_START_REFRESH_LIMIT * 90
		case "年度":
			limitDay = utils.DATA_START_REFRESH_LIMIT * 365
		default:
			limitDay = utils.DATA_START_REFRESH_LIMIT
		}
		startDate = sTime.AddDate(0, 0, -limitDay).Format(utils.FormatDate)

		fmt.Println("source:", source)
		respItem, err := RefreshEdbData(edbInfoId, source, subSource, edbCode, startDate)
		if err != nil {
			errMsg = errors.New("RefreshEdbData Err:" + err.Error()).Error()
			return err
		}
		if respItem.Ret != 200 {
			errMsg = errors.New(respItem.ErrMsg + ";EdbCode:" + edbCode).Error()
			return err
		}

		//maxAndMinItem, err := edbDataModel.GetEdbInfoMaxAndMinInfo(source, edbCode)
		//if err != nil {
		//	return err
		//}
		//if maxAndMinItem != nil {
		//	err = edbDataModel.ModifyEdbInfoMaxAndMinInfo(edbInfoId, maxAndMinItem)
		//	if err != nil {
		//		return err
		//	}
		//}
	}

	// 刷新计算指标
	for _, v := range calculateArr {
		edbInfo := calculateMap[v]
		if edbInfo == nil {
			return err
		}
		startDate = edbInfo.StartDate.Format(utils.FormatDate)
		source := edbInfo.Source

		if source == utils.DATA_SOURCE_CALCULATE {
			sTime := edbInfo.EndDate
			startDate = sTime.Format(utils.FormatDate)
		}

		result, err := RefreshEdbCalculateData(edbInfo.EdbInfoId, edbInfo.EdbCode, startDate)
		if err != nil {
			fmt.Println(v, "RefreshEdbCalculateData err", time.Now())
			errMsg = "RefreshEdbCalculateData Err:" + err.Error()
			return err
		}
		if result.Ret != 200 {
			fmt.Println(v, "RefreshEdbCalculateData err;msg:", result.Msg, ";errMsg:", result.ErrMsg)
			errMsg = fmt.Sprint(v, "RefreshEdbCalculateData err;msg:", result.Msg, ";errMsg:", result.ErrMsg)
			return fmt.Errorf("刷新失败")
		}
	}

	return
}

// GetChartInfoDataKey 获取图表缓存的key
func GetChartInfoDataKey(chartInfoId int) string {
	key := fmt.Sprint(utils.CACHE_CHART_INFO_DATA, chartInfoId)
	return key
}

// GetChartEdbData 获取图表的指标数据
func GetChartEdbData(chartInfoId, chartType int, calendar, startDate, endDate string, mappingList []*chartEdbMappingModel.ChartEdbInfoMapping, extraConfigStr string, seasonExtraConfig string) (edbList []*chartEdbMappingModel.ChartEdbInfoMappingList, xEdbIdValue []int, yDataList []chart_info.YData, sourceArr []string, dataResp interface{}, err error, errMsg string) {
	edbList = make([]*chartEdbMappingModel.ChartEdbInfoMappingList, 0)
	xEdbIdValue = make([]int, 0)
	yDataList = make([]chart_info.YData, 0)

	var extraConfig interface{}
	switch chartType {
	case 6: //时序组合图
		var tmpConfig chart_info.ChartTimeCombineExtraConf
		if extraConfigStr != `` {
			err = json.Unmarshal([]byte(extraConfigStr), &tmpConfig)
			if err != nil {
				errMsg = "雷达图配置异常"
				err = errors.New(errMsg)
				return
			}
		}

		extraConfig = tmpConfig
	case 7: // 柱形图
		var barConfig request.BarChartInfoReq
		if extraConfigStr == `` {
			errMsg = "柱方图未配置"
			err = errors.New(errMsg)
			return
		}
		err = json.Unmarshal([]byte(extraConfigStr), &barConfig)
		if err != nil {
			errMsg = "柱方图配置异常"
			err = errors.New(errMsg)
			return
		}
		extraConfig = barConfig
	case 10: // 截面散点图
		var tmpExtraConfig request.SectionScatterReq
		if extraConfigStr == `` {
			errMsg = "截面散点图未配置"
			err = errors.New(errMsg)
			return
		}
		err = json.Unmarshal([]byte(extraConfigStr), &tmpExtraConfig)
		if err != nil {
			errMsg = "截面散点配置异常"
			err = errors.New(errMsg)
			return
		}
		extraConfig = tmpExtraConfig
	case utils.CHART_TYPE_RADAR:
		var barConfig request.RadarChartInfoReq
		if extraConfigStr == `` {
			errMsg = "雷达图未配置"
			err = errors.New(errMsg)
			return
		}
		err = json.Unmarshal([]byte(extraConfigStr), &barConfig)
		if err != nil {
			errMsg = "雷达图配置异常"
			err = errors.New(errMsg)
			return
		}
		extraConfig = barConfig
	case utils.CHART_TYPE_SECTION_COMBINE:
		// 预览和详情都走这个接口
		var sectionExtraConfig chart_info.ChartSectionAllExtraConf
		if extraConfigStr == `` {
			errMsg = "截面组合图未配置"
			err = errors.New(errMsg)
			return
		}
		if chartInfoId > 0 {
			var sectionExtraConfigTmp chart_info.ChartSectionExtraConf
			err = json.Unmarshal([]byte(extraConfigStr), &sectionExtraConfigTmp)
			if err != nil {
				errMsg = "截面组合图配置异常"
				err = errors.New(errMsg)
				return
			}
			sectionExtraConfig.XDataList = sectionExtraConfigTmp.XDataList
			sectionExtraConfig.BaseChartSeriesName = sectionExtraConfigTmp.BaseChartSeriesName
			sectionExtraConfig.UnitList = sectionExtraConfigTmp.UnitList
			sectionExtraConfig.DateConfList = sectionExtraConfigTmp.DateConfList
			sectionExtraConfig.IsHeap = sectionExtraConfigTmp.IsHeap
			sectionExtraConfig.SortType = sectionExtraConfigTmp.SortType
			// 查询所有的seriesEdb
			seriesEdbList, e := chart_series_edb_mapping.GetChartSeriesEdbByChartInfoId(chartInfoId)
			if e != nil {
				errMsg = "查询seriesEdb失败"
				err = errors.New(errMsg + e.Error())
				return
			}
			// todo 是否有必要返回单位信息
			// 组装成map
			seriesEdbMap := make(map[int][]*chart_info.ChartSectionSeriesEdbConf)
			for _, v := range seriesEdbList {
				var dateConf *chart_info.ChartSectionSeriesDateConfItem
				if v.DateConf != "" {
					err = json.Unmarshal([]byte(v.DateConf), &dateConf)
					if err != nil {
						errMsg = "截面组合图配置异常"
						err = errors.New(errMsg + err.Error())
						return
					}
				}
				tmp := &chart_info.ChartSectionSeriesEdbConf{
					ChartSeriesEdbMappingId: v.ChartSeriesEdbMappingId,
					ChartSeriesId:           v.ChartSeriesId,
					//ChartInfoId:             v.ChartInfoId,
					EdbInfoId:    v.EdbInfoId,
					DateConf:     dateConf,
					EdbName:      "",
					EdbNameEn:    "",
					Unit:         "",
					UnitEn:       "",
					DateConfName: v.DateConfName,
					DateConfType: v.DateConfType,
				}
				seriesEdbMap[v.ChartSeriesId] = append(seriesEdbMap[v.ChartSeriesId], tmp)
			}
			//查询series
			seriesListTmp, e := chart_series.GetChartSeriesByChartInfoId(chartInfoId)
			if e != nil {
				errMsg = "查询series失败"
				err = errors.New(errMsg + e.Error())
				return
			}
			seriesList := make([]*chart_info.ChartSectionSeriesItem, 0)
			for _, v := range seriesListTmp {

				tmpSeries := &chart_info.ChartSectionSeriesItem{
					ChartSeriesId: v.ChartSeriesId,
					SeriesName:    v.SeriesName,
					SeriesNameEn:  v.SeriesNameEn,
					ChartStyle:    v.ChartStyle,
					ChartColor:    v.ChartColor,
					ChartWidth:    v.ChartWidth,
					IsPoint:       v.IsPoint,
					IsNumber:      v.IsNumber,
					IsAxis:        v.IsAxis,
					MaxData:       v.MaxData,
					MinData:       v.MinData,
					//IsOrder:         false,
					EdbInfoList: nil,
					DataList:    nil,
				}
				edbInfoList, ok := seriesEdbMap[v.ChartSeriesId]
				if ok {
					tmpSeries.EdbInfoList = edbInfoList
				}
				seriesList = append(seriesList, tmpSeries)
			}
			sectionExtraConfig.SeriesList = seriesList

		} else {
			err = json.Unmarshal([]byte(extraConfigStr), &sectionExtraConfig)
			if err != nil {
				errMsg = "截面组合图配置异常"
				err = errors.New(errMsg)
				return
			}
		}

		//校验引用日期名称是否重复
		dateNameMap := make(map[string]int)
		dateNameEnMap := make(map[string]int)
		for _, v := range sectionExtraConfig.DateConfList {
			if _, ok := dateNameMap[v.DateConfName]; ok {
				errMsg = "截面组合图引用日期名称设置重复"
				err = errors.New(errMsg + v.DateConfName)
				return
			}
			if _, ok := dateNameEnMap[v.DateConfNameEn]; ok {
				errMsg = "截面组合图引用日期名称设置重复"
				err = errors.New(errMsg + v.DateConfNameEn)
				return
			}
			if v.DateConfName != "" {
				dateNameMap[v.DateConfName] = 1
			}
			if v.DateConfNameEn != "" {
				dateNameEnMap[v.DateConfNameEn] = 1
			}

		}

		//检查系列名称是否重复
		seriesNameMap := make(map[string]int)
		seriesNameEnMap := make(map[string]int)
		for _, v := range sectionExtraConfig.SeriesList {
			if _, ok := seriesNameMap[v.SeriesName]; ok {
				errMsg = "截面组合图系列名称设置重复"
				err = errors.New(errMsg + v.SeriesName)
				return
			}
			if _, ok := seriesNameEnMap[v.SeriesNameEn]; ok {
				errMsg = "截面组合图系列名称设置重复"
				err = errors.New(errMsg + v.SeriesNameEn)
				return
			}
			if v.SeriesName != "" {
				seriesNameMap[v.SeriesName] = 1
			}
			if v.SeriesNameEn != "" {
				seriesNameEnMap[v.SeriesNameEn] = 1
			}
		}

		// 检查基准系列是否设置
		if sectionExtraConfig.BaseChartSeriesName == "" {
			errMsg = "截面组合图基准系列名称未设置"
			err = errors.New(errMsg)
			return
		}
		//todo 如果是详情接口，应该要从其他表里查询配置
		extraConfig = sectionExtraConfig
	default:
		xEdbIdValue = make([]int, 0)
		yDataList = make([]chart_info.YData, 0)
	}

	// 指标对应的所有数据
	edbDataListMap, edbList, sourceArr, err := getEdbDataMapList(chartInfoId, chartType, calendar, startDate, endDate, mappingList, seasonExtraConfig)
	if err != nil {
		return
	}

	// 特殊图形数据处理
	switch chartType {
	case 6: //时序组合图
		//判断是否堆积
		timeConf := extraConfig.(chart_info.ChartTimeCombineExtraConf)
		if extraConfigStr == "" { //历史数据，默认开启堆积
			timeConf = chart_info.ChartTimeCombineExtraConf{
				IsHeap: 1,
			}
		}
		dataResp = chart_info.ChartTimeCombineDataResp{IsHeap: timeConf.IsHeap}
	case 2: // 季节性图
		if seasonExtraConfig != "" {
			// 季节性图计算不管图上数据时间，拿所有数据
			_, tempEdbList, e := getEdbDataMapListForSeason(chartInfoId, chartType, calendar, "1990-01-01", "", mappingList, seasonExtraConfig)
			if e != nil {
				err = e
				return
			}
			dataResp, err = SeasonChartData(tempEdbList, seasonExtraConfig)
		} else {
			// 兼容无配置的老图
			dataResp = new(edbDataModel.SeasonChartResp)
		}
	case 7: // 柱形图
		barChartConf := extraConfig.(request.BarChartInfoReq)
		xEdbIdValue, yDataList, err = BarChartData(mappingList, edbDataListMap, barChartConf.DateList, barChartConf.Sort)

		for _, v := range edbList {
			// 指标别名
			if barChartConf.EdbInfoIdList != nil && len(barChartConf.EdbInfoIdList) > 0 {
				for _, reqEdb := range barChartConf.EdbInfoIdList {
					if v.EdbInfoId == reqEdb.EdbInfoId {
						v.EdbAliasName = reqEdb.Name
					}
				}
			}
		}

		for k := range yDataList {
			yDataList[k].Unit = barChartConf.Unit
			yDataList[k].UnitEn = barChartConf.UnitEn
		}

	case 10: // 截面散点图
		sectionScatterConf := extraConfig.(request.SectionScatterReq)
		xEdbIdValue, dataResp, err = GetSectionScatterChartData(mappingList, edbDataListMap, sectionScatterConf)

		// 这个数据没有必要返回给前端
		for _, v := range edbList {
			v.DataList = nil
		}
	case utils.CHART_TYPE_SECTION_COMBINE: // 截面组合图
		sectionConf := extraConfig.(chart_info.ChartSectionAllExtraConf)
		var chartInfo *chartInfoModel.ChartInfo
		if chartInfoId > 0 {
			chartInfo, err = chartInfoModel.GetChartInfoById(chartInfoId)
			if err != nil {
				errMsg = "获取图表信息失败"
				return
			}
		}

		xEdbIdValue, dataResp, err = GetChartSectionCombineData(chartInfo, mappingList, edbDataListMap, sectionConf)

		// 这个数据没有必要返回给前端
		for _, v := range edbList {
			v.DataList = nil
		}
	case utils.CHART_TYPE_RADAR: //雷达图
		radarConf := extraConfig.(request.RadarChartInfoReq)
		xEdbIdValue, dataResp, err = RadarChartData(mappingList, edbDataListMap, radarConf)
	}

	return
}

// getEdbDataMapListForSeason 获取指标最后的基础数据-插值补充好数据-季节性图用
func getEdbDataMapListForSeason(chartInfoId, chartType int, calendar, startDate, endDate string, mappingList []*chartEdbMappingModel.ChartEdbInfoMapping, seasonExtraConfig string) (edbDataListMap map[int][]*edbDataModel.EdbDataList, edbList []*chartEdbMappingModel.ChartEdbInfoMappingList, err error) {
	// 指标对应的所有数据
	edbDataListMap = make(map[int][]*edbDataModel.EdbDataList)
	seasonXStartDateWithYear := ""
	seasonXEndDateWithYear := ""
	if chartType == 2 {
		sort.Slice(mappingList, func(i, j int) bool {
			return mappingList[i].IsAxis > mappingList[j].IsAxis
		})
	}
	for _, v := range mappingList {
		//fmt.Println("v:", v.EdbInfoId)
		item := new(chartEdbMappingModel.ChartEdbInfoMappingList)
		item.EdbInfoId = v.EdbInfoId
		item.SourceName = v.SourceName
		item.Source = v.Source
		item.EdbCode = v.EdbCode
		item.EdbName = v.EdbName
		item.EdbNameEn = v.EdbNameEn
		item.Frequency = v.Frequency
		item.EdbType = v.EdbType
		item.FrequencyEn = GetFrequencyEn(v.Frequency)
		if v.Unit != `无` {
			item.Unit = v.Unit
		}
		item.UnitEn = v.UnitEn
		item.StartDate = v.StartDate
		item.EndDate = v.EndDate
		item.ModifyTime = v.ModifyTime
		item.EdbInfoCategoryType = v.EdbInfoCategoryType
		item.PredictChartColor = v.PredictChartColor
		item.ClassifyId = v.ClassifyId
		if chartInfoId <= 0 {
			item.IsAxis = 1
			item.LeadValue = 0
			item.LeadUnit = ""
			item.ChartEdbMappingId = 0
			item.ChartInfoId = 0
			item.IsOrder = false
			item.EdbInfoType = 1
			item.ChartStyle = ""
			item.ChartColor = ""
			item.ChartWidth = 0
			item.MaxData = v.MaxValue
			item.MinData = v.MinValue
		} else {
			item.IsAxis = v.IsAxis
			item.EdbInfoType = v.EdbInfoType
			item.LeadValue = v.LeadValue
			item.LeadUnit = v.LeadUnit
			item.LeadUnitEn = GetLeadUnitEn(v.LeadUnit)
			item.ChartEdbMappingId = v.ChartEdbMappingId
			item.ChartInfoId = v.ChartInfoId
			item.ChartStyle = v.ChartStyle
			item.ChartColor = v.ChartColor
			item.ChartWidth = v.ChartWidth
			item.IsOrder = v.IsOrder
			item.MaxData = v.MaxData
			item.MinData = v.MinData
		}
		item.LatestValue = v.LatestValue
		item.LatestDate = v.LatestDate
		item.UniqueCode = v.UniqueCode
		item.MoveLatestDate = v.LatestDate
		item.EdbAliasName = v.EdbAliasName
		item.IsConvert = v.IsConvert
		item.ConvertType = v.ConvertType
		item.ConvertValue = v.ConvertValue
		item.ConvertUnit = v.ConvertUnit
		item.ConvertEnUnit = v.ConvertEnUnit
		item.IsJoinPermission = v.IsJoinPermission

		var startDateReal string
		var diffSeconds int64
		if chartType == 2 { //季节性图
			startDateReal = startDate
			if len(mappingList) > 1 {
				item.IsAxis = v.IsAxis
			}
		} else {
			if v.EdbInfoType == 0 && v.LeadUnit != "" && v.LeadValue > 0 { //领先指标
				var startTimeRealTemp time.Time
				startDateParse, _ := time.Parse(utils.FormatDate, startDate)
				switch v.LeadUnit {
				case "天":
					startTimeRealTemp = startDateParse.AddDate(0, 0, -v.LeadValue)
				case "月":
					startTimeRealTemp = startDateParse.AddDate(0, -v.LeadValue, 0)
				case "季":
					startTimeRealTemp = startDateParse.AddDate(0, -3*v.LeadValue, 0)
				case "周":
					startTimeRealTemp = startDateParse.AddDate(0, 0, -7*v.LeadValue)
				case "年":
					startTimeRealTemp = startDateParse.AddDate(-v.LeadValue, 0, 0)
				}
				if startTimeRealTemp.Before(startDateParse) {
					startDateReal = startTimeRealTemp.Format(utils.FormatDate)
					diffSeconds = (int64(startTimeRealTemp.UnixNano()) - int64(startDateParse.UnixNano())) / 1e6
				} else {
					startDateReal = startDate
					diffSeconds = 0
				}

				// 预测指标的开始日期也要偏移
				{
					day, tmpErr := utils.GetDaysBetween2Date(utils.FormatDate, startDate, startDateReal)
					if tmpErr != nil {
						err = tmpErr
						return
					}
					moveLatestDateTime := item.MoveLatestDate.Local()
					if tmpErr != nil {
						err = tmpErr
						return
					}
					item.MoveLatestDate = moveLatestDateTime.AddDate(0, 0, day)
				}
			} else {
				startDateReal = startDate
			}
		}
		//fmt.Println("line 1011 chart:", v.Source, v.EdbInfoId, startDateReal, endDate)
		calendarPreYear := 0
		if calendar == "农历" {
			newStartDateReal, err := time.Parse(utils.FormatDate, startDateReal)
			if err != nil {
				fmt.Println("time.Parse:" + err.Error())
			}
			calendarPreYear = newStartDateReal.Year() - 1
			newStartDateReal = newStartDateReal.AddDate(-1, 0, 0)
			startDateReal = newStartDateReal.Format(utils.FormatDate)
		}
		dataList := make([]*edbDataModel.EdbDataList, 0)
		//fmt.Println("chart:", v.Source, v.EdbInfoId, startDateReal, endDate)
		//fmt.Println("calendarPreYear:", calendarPreYear)
		//var newEdbInfo *data_manage.EdbInfo
		switch v.EdbInfoCategoryType {
		case 0:
			dataList, err = edbDataModel.GetEdbDataList(v.Source, v.SubSource, v.EdbInfoId, startDateReal, endDate)
		case 1:
			_, dataList, _, _, err, _ = GetPredictDataListByPredictEdbInfoId(v.EdbInfoId, startDateReal, endDate, true)
		default:
			err = errors.New(fmt.Sprint("获取失败,指标类型异常", v.EdbInfoCategoryType))
		}
		if err != nil {
			return
		}

		handleDataMap := make(map[string]float64)
		newDataList, _, _, e := HandleDataByLinearRegressionToListV3(dataList, handleDataMap)
		if e != nil {
			err = e
			return
		}

		if v.IsConvert == 1 {
			switch v.ConvertType {
			case 1:
				for i, data := range newDataList {
					newDataList[i].Value = data.Value * v.ConvertValue
				}
				//item.MaxData = item.MaxData * v.ConvertValue
				//item.MinData = item.MinData * v.ConvertValue
			case 2:
				for i, data := range newDataList {
					newDataList[i].Value = data.Value / v.ConvertValue
				}
				//item.MaxData = item.MaxData / v.ConvertValue
				//item.MinData = item.MinData / v.ConvertValue
			case 3:
				for i, data := range newDataList {
					if data.Value <= 0 {
						err = errors.New("数据中含有负数或0，无法对数运算")
						return
					}
					newDataList[i].Value = math.Log(data.Value) / math.Log(v.ConvertValue)
				}
				//item.MaxData = math.Log(item.MaxData) / math.Log(v.ConvertValue)
				//item.MinData = math.Log(item.MinData) / math.Log(v.ConvertValue)
			}
		}

		edbDataListMap[v.EdbInfoId] = newDataList

		if diffSeconds != 0 && v.EdbInfoType == 0 {
			newDataListLen := len(newDataList)
			for i := 0; i < newDataListLen; i++ {
				newDataList[i].DataTimestamp = newDataList[i].DataTimestamp - diffSeconds
			}
		}

		if chartType == 2 && item.IsAxis == 1 {
			latestDate := v.LatestDate
			if latestDate.IsZero() {
				//item.DataList = newDataList
				item.IsNullData = true
				edbList = append(edbList, item)
				continue
			}

			if calendar == "农历" {
				if len(newDataList) <= 0 {
					result := new(edbDataModel.EdbDataResult)
					item.DataList = result
				} else {
					result, tmpErr := edbDataService.AddCalculateQuarterV6(newDataList)
					if tmpErr != nil {
						err = errors.New("获取农历数据失败,Err:" + tmpErr.Error())
						return
					}
					quarterDataList, seasonXStartDateWithYearTmp, seasonXEndDateWithYearTmp, tErr := GetSeasonEdbInfoDataListByXDateNong(result, latestDate, seasonExtraConfig, calendarPreYear)
					if tErr != nil {
						err = errors.New("获取季节性图表数据失败,Err:" + tErr.Error())
						return
					}
					item.DataList = quarterDataList
					seasonXStartDateWithYear = seasonXStartDateWithYearTmp
					seasonXEndDateWithYear = seasonXEndDateWithYearTmp
				}

			} else {
				quarterDataList, seasonXStartDateWithYearTmp, seasonXEndDateWithYearTmp, tErr := GetSeasonEdbInfoDataListByXDate(newDataList, latestDate, seasonExtraConfig)
				if tErr != nil {
					err = errors.New("获取季节性图表数据失败,Err:" + tErr.Error())
					return
				}
				item.DataList = quarterDataList
				seasonXStartDateWithYear = seasonXStartDateWithYearTmp
				seasonXEndDateWithYear = seasonXEndDateWithYearTmp
			}

		} else if chartType == 2 && item.IsAxis == 0 {
			newDataList := make([]*edbDataModel.EdbDataList, 0)
			for _, v := range dataList {
				dataTime, e := time.Parse(utils.FormatDate, v.DataTime)
				if e != nil {
					err = errors.New("季节性图处理右轴指标数据转换日期失败,Err:" + e.Error())
					return
				}
				seasonXStartDateWithYearT, _ := time.Parse(utils.FormatDate, seasonXStartDateWithYear)
				seasonXEndDateWithYearT, _ := time.Parse(utils.FormatDate, seasonXEndDateWithYear)
				if dataTime.Equal(seasonXStartDateWithYearT) || (dataTime.After(seasonXStartDateWithYearT) && dataTime.Before(seasonXEndDateWithYearT)) || dataTime.Equal(seasonXEndDateWithYearT) {
					newDataList = append(newDataList, v)
				}
			}
			item.DataList = newDataList
		} else if chartType == 7 || chartType == utils.CHART_TYPE_RADAR { //柱方图
			//item.DataList = newDataList
		} else {
			item.DataList = newDataList
		}
		edbList = append(edbList, item)
	}

	return
}

// SeasonChartData 季节性图的数据处理
func SeasonChartData(dataList []*chartEdbMappingModel.ChartEdbInfoMappingList, seasonExtraConfig string) (dataResp edbDataModel.SeasonChartResp, err error) {
	var seasonConfig edbDataModel.SeasonExtraItem
	err = json.Unmarshal([]byte(seasonExtraConfig), &seasonConfig)
	if err != nil {
		err = errors.New("季节性图配置异常, Err:" + err.Error())
		return
	}

	for _, mappingItem := range dataList {
		if mappingItem.IsAxis == 0 {
			continue
		}
		quarterDataList, ok := mappingItem.DataList.(edbDataModel.QuarterDataList)
		if !ok {
			continue
		}
		// 上下限区间
		if seasonConfig.MaxMinLimits.Year > 0 {
			startYear := time.Now().AddDate(-seasonConfig.MaxMinLimits.Year, 0, 0).Year()
			dataResp.MaxMinLimits.List = make([]*edbDataModel.MaxMinLimitsData, 0)
			dataTimeMap := make(map[time.Time]time.Time)
			dataTimeList := make([]string, 0)
			maxValueMap := make(map[time.Time]float64)
			minValueMap := make(map[time.Time]float64)

			maxMinDataList := make([]*edbDataModel.MaxMinLimitsData, 0)
			var startDateStr string
			var endDateStr string
			var newDate time.Time

			startDateStr = fmt.Sprintf("%d-%s", time.Now().Year(), seasonConfig.XStartDate)
			endDateStr = fmt.Sprintf("%d-%s", time.Now().Year(), seasonConfig.XEndDate)
			startDate, e := time.Parse(utils.FormatDate, startDateStr)
			if e != nil {
				err = e
				return
			}
			endDate, e := time.Parse(utils.FormatDate, endDateStr)
			if e != nil {
				err = e
				return
			}
			// 日度 周度插值
			for _, v := range quarterDataList {
				if mappingItem.Frequency == "日度" || mappingItem.Frequency == "周度" {
					handleDataMap := make(map[string]float64)
					dataTimeList, _, err = HandleDataByLinearRegressionToList(v.DataList, handleDataMap)
					if err != nil {
						err = errors.New("插值处理数据异常, Err:" + err.Error())
						return
					}
					// 不包含当年
					if v.ChartLegend == strconv.Itoa(time.Now().Year()) {
						continue
					}
					// 农历时dataTimeList的年份为最新年份，图例时间的年份才是真实年份
					chartLegend, _ := strconv.Atoi(v.ChartLegend)
					if chartLegend < startYear {
						continue
					}
					for _, date := range dataTimeList {
						dateTime, e := time.Parse(utils.FormatDate, date)
						if e != nil {
							err = errors.New("时间格式化异常, Err:" + e.Error())
							return
						}
						if dateTime.Year() < startYear {
							continue
						}
						// 不包含2月29号
						if dateTime.Month() == 2 && dateTime.Day() == 29 {
							continue
						}
						newDate = dateTime.AddDate(time.Now().Year()-dateTime.Year(), 0, 0)
						if seasonConfig.JumpYear == 1 {
							if startDate.After(endDate) {
								// 如果跨年且不到一年
								// 全年截取一部分
								if newDate.Before(startDate.AddDate(0, 0, 1)) && newDate.After(endDate) {
									continue
								}
								if newDate.After(startDate.AddDate(0, 0, -1)) {
									// 减一年
									newDate = dateTime.AddDate(time.Now().Year()-dateTime.Year()-1, 0, 0)
								}
								// 处理上下限列表
								if value, ok := maxValueMap[newDate]; ok {
									if value < handleDataMap[date] {
										maxValueMap[newDate] = handleDataMap[date]
									}
								} else {
									maxValueMap[newDate] = handleDataMap[date]
								}

								if value, ok := minValueMap[newDate]; ok {
									if value > handleDataMap[date] {
										minValueMap[newDate] = handleDataMap[date]
									}
								} else {
									minValueMap[newDate] = handleDataMap[date]
								}

								dataTimeMap[newDate] = newDate
							} else {
								// 如果跨年且大于等于一年
								// double后截取
								if newDate.After(startDate) {
									// 处理上下限列表
									if value, ok := maxValueMap[newDate]; ok {
										if value < handleDataMap[date] {
											maxValueMap[newDate] = handleDataMap[date]
										}
									} else {
										maxValueMap[newDate] = handleDataMap[date]
									}

									if value, ok := minValueMap[newDate]; ok {
										if value > handleDataMap[date] {
											minValueMap[newDate] = handleDataMap[date]
										}
									} else {
										minValueMap[newDate] = handleDataMap[date]
									}

									dataTimeMap[newDate] = newDate
								}

								newDate = dateTime.AddDate(time.Now().Year()-dateTime.Year()+1, 0, 0)
								newEndDate := endDate.AddDate(1, 0, 0)
								if newDate.Before(newEndDate) {
									// 处理上下限列表
									if value, ok := maxValueMap[newDate]; ok {
										if value < handleDataMap[date] {
											maxValueMap[newDate] = handleDataMap[date]
										}
									} else {
										maxValueMap[newDate] = handleDataMap[date]
									}

									if value, ok := minValueMap[newDate]; ok {
										if value > handleDataMap[date] {
											minValueMap[newDate] = handleDataMap[date]
										}
									} else {
										minValueMap[newDate] = handleDataMap[date]
									}

									dataTimeMap[newDate] = newDate
								}

							}
						} else {
							// 如果不跨年 正常获取
							// 获取当前日期所在区间
							// 处理上下限列表
							if value, ok := maxValueMap[newDate]; ok {
								if value < handleDataMap[date] {
									maxValueMap[newDate] = handleDataMap[date]
								}
							} else {
								maxValueMap[newDate] = handleDataMap[date]
							}

							if value, ok := minValueMap[newDate]; ok {
								if value > handleDataMap[date] {
									minValueMap[newDate] = handleDataMap[date]
								}
							} else {
								minValueMap[newDate] = handleDataMap[date]
							}

							dataTimeMap[newDate] = newDate
						}
					}
				} else {
					// 旬度、月度、季度、半年度 不插值，需要先把日期列表和数据map取出来
					for _, vv := range v.DataList {
						dateTime, e := time.Parse(utils.FormatDate, vv.DataTime)
						if e != nil {
							err = errors.New("时间格式化异常, Err:" + e.Error())
							return
						}

						// 不包含当年
						if v.ChartLegend == strconv.Itoa(time.Now().Year()) {
							continue
						}

						if dateTime.Year() < startYear {
							continue
						}
						// 农历时dataTimeList的年份为最新年份，图例时间的年份才是真实年份
						chartLegend, _ := strconv.Atoi(v.ChartLegend)
						if chartLegend < startYear {
							continue
						}
						// 月度的2月29号日期改为2月28日
						if dateTime.Month() == 2 && dateTime.Day() == 29 {
							dateTime = dateTime.AddDate(0, 0, -1)
						}
						newDate = dateTime.AddDate(time.Now().Year()-dateTime.Year(), 0, 0)
						if seasonConfig.JumpYear == 1 {
							if startDate.After(endDate) {
								// 如果跨年且不到一年
								// 全年截取一部分
								if newDate.Before(startDate.AddDate(0, 0, 1)) && newDate.After(endDate) {
									continue
								}
								if newDate.After(startDate.AddDate(0, 0, -1)) {
									// 减一年
									newDate = dateTime.AddDate(time.Now().Year()-dateTime.Year()-1, 0, 0)
								}
								// 处理上下限列表
								if value, ok := maxValueMap[newDate]; ok {
									if value < vv.Value {
										maxValueMap[newDate] = vv.Value
									}
								} else {
									maxValueMap[newDate] = vv.Value
								}

								if value, ok := minValueMap[newDate]; ok {
									if value > vv.Value {
										minValueMap[newDate] = vv.Value
									}
								} else {
									minValueMap[newDate] = vv.Value
								}

								dataTimeMap[newDate] = newDate
							} else {
								// 如果跨年且大于等于一年
								// double后截取
								if newDate.After(startDate) {
									// 处理上下限列表
									if value, ok := maxValueMap[newDate]; ok {
										if value < vv.Value {
											maxValueMap[newDate] = vv.Value
										}
									} else {
										maxValueMap[newDate] = vv.Value
									}

									if value, ok := minValueMap[newDate]; ok {
										if value > vv.Value {
											minValueMap[newDate] = vv.Value
										}
									} else {
										minValueMap[newDate] = vv.Value
									}

									dataTimeMap[newDate] = newDate
								}

								newDate = dateTime.AddDate(time.Now().Year()-dateTime.Year()+1, 0, 0)
								newEndDate := endDate.AddDate(1, 0, 0)
								if newDate.Before(newEndDate) {
									// 处理上下限列表
									if value, ok := maxValueMap[newDate]; ok {
										if value < vv.Value {
											maxValueMap[newDate] = vv.Value
										}
									} else {
										maxValueMap[newDate] = vv.Value
									}

									if value, ok := minValueMap[newDate]; ok {
										if value > vv.Value {
											minValueMap[newDate] = vv.Value
										}
									} else {
										minValueMap[newDate] = vv.Value
									}

									dataTimeMap[newDate] = newDate
								}

							}
						} else {
							// 如果不跨年 正常获取
							// 获取当前日期所在区间
							// 处理上下限列表
							if value, ok := maxValueMap[newDate]; ok {
								if value < vv.Value {
									maxValueMap[newDate] = vv.Value
								}
							} else {
								maxValueMap[newDate] = vv.Value
							}

							if value, ok := minValueMap[newDate]; ok {
								if value > vv.Value {
									minValueMap[newDate] = vv.Value
								}
							} else {
								minValueMap[newDate] = vv.Value
							}

							dataTimeMap[newDate] = newDate
						}

					}
				}
			}

			for _, v := range dataTimeMap {
				maxMinItem := &edbDataModel.MaxMinLimitsData{}
				if maxValue, ok := maxValueMap[v]; ok {
					maxMinItem.MaxValue = maxValue
				} else {
					maxMinItem.MaxValue = minValueMap[v]
				}

				if minValue, ok := minValueMap[v]; ok {
					maxMinItem.MinValue = minValue
				} else {
					maxMinItem.MinValue = maxValueMap[v]
				}

				maxMinItem.DataTime = v.Format(utils.FormatDate)
				maxMinItem.DataTimestamp = v.UnixNano() / 1e6

				maxMinDataList = append(maxMinDataList, maxMinItem)

			}

			// 排序
			sort.Slice(maxMinDataList, func(i, j int) bool {
				return maxMinDataList[i].DataTime < maxMinDataList[j].DataTime
			})

			dataResp.MaxMinLimits.List = maxMinDataList
			dataResp.MaxMinLimits.Color = seasonConfig.MaxMinLimits.Color
			dataResp.MaxMinLimits.Legend = seasonConfig.MaxMinLimits.Legend
			dataResp.MaxMinLimits.IsShow = seasonConfig.MaxMinLimits.IsShow
			dataResp.MaxMinLimits.IsAdd = seasonConfig.MaxMinLimits.IsAdd
			dataResp.MaxMinLimits.Year = seasonConfig.MaxMinLimits.Year
		}

		// 自定义同期均线
		if seasonConfig.SamePeriodAverage.Year > 0 {
			handleDataMap := make(map[string]float64)
			dataTimeMap := make(map[time.Time]time.Time)
			dataTimeList := make([]string, 0)
			valueMap := make(map[time.Time]float64)
			averageDataList := make([]*edbDataModel.SamePeriodAverageData, 0)
			dateNumMap := make(map[time.Time]float64)
			for i := len(quarterDataList) - 1; i >= 0; i-- {
				// 插值成日度
				dataTimeList, _, err = HandleDataByLinearRegressionToList(quarterDataList[i].DataList, handleDataMap)
				if err != nil {
					err = errors.New("插值处理数据异常, Err:" + err.Error())
					return
				}
				for _, date := range dataTimeList {
					dateTime, e := time.Parse(utils.FormatDate, date)
					if e != nil {
						err = errors.New("时间格式化异常, Err:" + e.Error())
						return
					}
					startYear := time.Now().AddDate(-seasonConfig.SamePeriodAverage.Year, 0, 0).Year()
					if dateTime.Year() < startYear {
						continue
					}
					// 农历时dataTimeList的年份为最新年份，图例时间的年份才是真实年份
					chartLegend, _ := strconv.Atoi(quarterDataList[i].ChartLegend)
					if chartLegend < startYear {
						continue
					}
					// 不包含2月29号
					if dateTime.Month() == 2 && dateTime.Day() == 29 {
						continue
					}
					// 不包含当年
					if quarterDataList[i].ChartLegend == strconv.Itoa(time.Now().Year()) {
						continue
					}
					newDate := dateTime.AddDate(time.Now().Year()-dateTime.Year(), 0, 0)
					// 处理均值
					if value, ok := valueMap[newDate]; ok {
						valueMap[newDate] = handleDataMap[date] + value
					} else {
						valueMap[newDate] = handleDataMap[date]
					}
					dateNumMap[newDate] += 1

					dataTimeMap[newDate] = newDate
				}
			}

			//year := float64(seasonConfig.SamePeriodAverage.Year)
			for _, v := range dataTimeMap {
				averageItem := &edbDataModel.SamePeriodAverageData{}
				year := dateNumMap[v]
				if value, ok := valueMap[v]; ok {
					if year > 0 {
						averageItem.Value = value / year
					} else {
						averageItem.Value = value
					}
				}
				averageItem.DataTime = v.Format(utils.FormatDate)
				averageItem.DataTimestamp = v.UnixNano() / 1e6

				averageDataList = append(averageDataList, averageItem)

			}

			// 排序
			sort.Slice(averageDataList, func(i, j int) bool {
				return averageDataList[i].DataTime < averageDataList[j].DataTime
			})

			dataResp.SamePeriodAverage.List = averageDataList
			dataResp.SamePeriodAverage.Year = seasonConfig.SamePeriodAverage.Year
			dataResp.SamePeriodAverage.Color = seasonConfig.SamePeriodAverage.Color
			dataResp.SamePeriodAverage.Legend = seasonConfig.SamePeriodAverage.Legend
			dataResp.SamePeriodAverage.IsShow = seasonConfig.SamePeriodAverage.IsShow
			dataResp.SamePeriodAverage.LineType = seasonConfig.SamePeriodAverage.LineType
			dataResp.SamePeriodAverage.LineWidth = seasonConfig.SamePeriodAverage.LineWidth
			dataResp.SamePeriodAverage.IsAdd = seasonConfig.SamePeriodAverage.IsAdd

		}

		// 自定义同期标准差
		if seasonConfig.SamePeriodStandardDeviation.Year > 1 && seasonConfig.SamePeriodStandardDeviation.Multiple > 0 {
			startYear := time.Now().AddDate(-seasonConfig.SamePeriodStandardDeviation.Year, 0, 0).Year()

			// 先算均值，再算标准差
			handleDataMap := make(map[string]float64)
			dataTimeMap := make(map[time.Time]time.Time)
			dataTimeValueMap := make(map[time.Time][]float64)
			dataTimeList := make([]string, 0)
			valueMap := make(map[time.Time]float64)

			samePeriodStandardDeviationList := make([]*edbDataModel.MaxMinLimitsData, 0)
			for i := len(quarterDataList) - 1; i > len(quarterDataList)-seasonConfig.SamePeriodAverage.Year-1 && i > 0; i-- {
				// 插值成日度
				dataTimeList, _, err = HandleDataByLinearRegressionToListV2(quarterDataList[i].DataList, handleDataMap)
				if err != nil {
					err = errors.New("插值处理数据异常, Err:" + err.Error())
					return
				}
				for _, date := range dataTimeList {
					dateTime, e := time.Parse(utils.FormatDate, date)
					if e != nil {
						err = errors.New("时间格式化异常, Err:" + e.Error())
						return
					}
					if dateTime.Year() < startYear {
						continue
					}
					// 农历时dataTimeList的年份为最新年份，图例时间的年份才是真实年份
					chartLegend, _ := strconv.Atoi(quarterDataList[i].ChartLegend)
					if chartLegend < startYear {
						continue
					}
					// 不包含2月29号
					if dateTime.Month() == 2 && dateTime.Day() == 29 {
						continue
					}
					newDate := dateTime.AddDate(time.Now().Year()-dateTime.Year(), 0, 0)
					// 处理均值
					if value, ok := valueMap[newDate]; ok {
						valueMap[newDate] = (handleDataMap[date] + value) / 2
					} else {
						valueMap[newDate] = handleDataMap[date]
					}

					dataTimeMap[newDate] = newDate
					valueList := dataTimeValueMap[newDate]
					valueList = append(valueList, handleDataMap[date])
					dataTimeValueMap[newDate] = valueList
				}
			}

			for _, v := range dataTimeMap {
				valueList := dataTimeValueMap[v]
				stdev := utils.CalculateStandardDeviation(valueList)
				stdev, _ = decimal.NewFromFloat(stdev).Round(4).Float64()

				maxMinItem := &edbDataModel.MaxMinLimitsData{}

				if value, ok := valueMap[v]; ok {
					maxMinItem.MaxValue = value + stdev*seasonConfig.SamePeriodStandardDeviation.Multiple
					maxMinItem.MinValue = value - stdev*seasonConfig.SamePeriodStandardDeviation.Multiple
				}

				maxMinItem.DataTime = v.Format(utils.FormatDate)
				maxMinItem.DataTimestamp = v.UnixNano() / 1e6

				samePeriodStandardDeviationList = append(samePeriodStandardDeviationList, maxMinItem)

			}

			// 排序
			sort.Slice(samePeriodStandardDeviationList, func(i, j int) bool {
				return samePeriodStandardDeviationList[i].DataTime < samePeriodStandardDeviationList[j].DataTime
			})

			dataResp.SamePeriodStandardDeviation.List = samePeriodStandardDeviationList
			dataResp.SamePeriodStandardDeviation.Color = seasonConfig.SamePeriodStandardDeviation.Color
			dataResp.SamePeriodStandardDeviation.Legend = seasonConfig.SamePeriodStandardDeviation.Legend
			dataResp.SamePeriodStandardDeviation.IsShow = seasonConfig.SamePeriodStandardDeviation.IsShow
			dataResp.SamePeriodStandardDeviation.Multiple = seasonConfig.SamePeriodStandardDeviation.Multiple
			dataResp.SamePeriodStandardDeviation.Year = seasonConfig.SamePeriodStandardDeviation.Year
			dataResp.SamePeriodStandardDeviation.IsAdd = seasonConfig.SamePeriodStandardDeviation.IsAdd
		}

		// 自定义右轴
		if seasonConfig.RightAxis.IndicatorType != 0 {
			if seasonConfig.RightAxis.IndicatorType == 1 {
				startTime, _ := time.Parse(utils.FormatDate, mappingItem.StartDate)
				for i := len(quarterDataList) - 1; i > len(quarterDataList)-2 && i > 0; i-- {
					var rightMappingItem chartEdbMappingModel.ChartEdbInfoMappingList
					rightMappingItem = *mappingItem
					// 计算同比值
					tbzDataList, minValue, maxValue, e := GetEdbDataTbzForSeason(mappingItem.Frequency, quarterDataList[i].DataList, startTime)
					if e != nil {
						err = errors.New("计算同比值失败, Err:" + e.Error())
						return
					}
					rightMappingItem.DataList = tbzDataList
					rightMappingItem.MaxData = maxValue
					rightMappingItem.MinData = minValue
					dataResp.RightAxis.EdbInfoList = append(dataResp.RightAxis.EdbInfoList, &rightMappingItem)
				}
			}
			dataResp.RightAxis.SeasonRightAxis = seasonConfig.RightAxis
		}
	}

	return
}

// GetEdbDataMapList 获取指标最后的基础数据
func GetEdbDataMapList(chartInfoId, chartType int, calendar, startDate, endDate string, mappingList []*chartEdbMappingModel.ChartEdbInfoMapping, seasonExtraConfig string) (edbDataListMap map[int][]*edbDataModel.EdbDataList, edbList []*chartEdbMappingModel.ChartEdbInfoMappingList, sourceArr []string, err error) {
	edbDataListMap, edbList, sourceArr, err = getEdbDataMapList(chartInfoId, chartType, calendar, startDate, endDate, mappingList, seasonExtraConfig)
	return
}

// getEdbDataMapList 获取指标最后的基础数据
func getEdbDataMapList(chartInfoId, chartType int, calendar, startDate, endDate string, mappingList []*chartEdbMappingModel.ChartEdbInfoMapping, seasonExtraConfig string) (edbDataListMap map[int][]*edbDataModel.EdbDataList, edbList []*chartEdbMappingModel.ChartEdbInfoMappingList, sourceArr []string, err error) {
	// 关联指标来源
	sourceArr = make([]string, 0)
	// 指标对应的所有数据
	edbDataListMap = make(map[int][]*edbDataModel.EdbDataList)
	seasonXStartDateWithYear := ""
	seasonXEndDateWithYear := ""
	if chartType == 2 {
		sort.Slice(mappingList, func(i, j int) bool {
			return mappingList[i].IsAxis > mappingList[j].IsAxis
		})
	}

	for _, v := range mappingList {
		//fmt.Println("v:", v.EdbInfoId)
		item := new(chartEdbMappingModel.ChartEdbInfoMappingList)
		item.EdbInfoId = v.EdbInfoId
		item.SourceName = v.SourceName
		item.Source = v.Source
		item.EdbCode = v.EdbCode
		item.EdbName = v.EdbName
		item.EdbNameEn = v.EdbNameEn
		item.Frequency = v.Frequency
		item.FrequencyEn = GetFrequencyEn(v.Frequency)
		if v.Unit != `无` {
			item.Unit = v.Unit
		}
		item.UnitEn = v.UnitEn
		item.StartDate = v.StartDate
		item.EndDate = v.EndDate
		item.ModifyTime = v.ModifyTime
		item.EdbInfoCategoryType = v.EdbInfoCategoryType
		item.PredictChartColor = v.PredictChartColor
		item.EdbAliasName = v.EdbAliasName

		if !utils.InArray(v.Source, utils.SystemSourceList) { //来源于系统的指标，都展示为弘则研究
			if !utils.InArray(v.SourceName, sourceArr) {
				sourceArr = append(sourceArr, v.SourceName)
			}
		}
		if chartInfoId <= 0 {
			item.IsAxis = 1
			item.LeadValue = 0
			item.LeadUnit = ""
			item.ChartEdbMappingId = 0
			item.ChartInfoId = 0
			item.IsOrder = false
			item.EdbInfoType = 1
			item.ChartStyle = ""
			item.ChartColor = ""
			item.ChartWidth = 0
			item.ChartScale = 0
			item.MaxData = v.MaxValue
			item.MinData = v.MinValue
		} else {
			item.IsAxis = v.IsAxis
			item.EdbInfoType = v.EdbInfoType
			item.LeadValue = v.LeadValue
			item.LeadUnit = v.LeadUnit
			item.LeadUnitEn = GetLeadUnitEn(v.LeadUnit)
			item.ChartEdbMappingId = v.ChartEdbMappingId
			item.ChartInfoId = v.ChartInfoId
			item.ChartStyle = v.ChartStyle
			item.ChartColor = v.ChartColor
			item.ChartWidth = v.ChartWidth
			item.ChartScale = v.ChartScale
			item.IsOrder = v.IsOrder
			item.MaxData = v.MaxData
			item.MinData = v.MinData
		}
		item.LatestValue = v.LatestValue
		item.LatestDate = v.LatestDate
		item.UniqueCode = v.UniqueCode
		item.MoveLatestDate = item.LatestDate
		item.EdbAliasName = v.EdbAliasName
		item.IsConvert = v.IsConvert
		item.ConvertType = v.ConvertType
		item.ConvertValue = v.ConvertValue
		item.ConvertUnit = v.ConvertUnit
		item.ConvertEnUnit = v.ConvertEnUnit

		var startDateReal string
		var diffSeconds int64
		if chartType == 2 { //季节性图
			startDateReal = startDate
		} else {
			if v.EdbInfoType == 0 && v.LeadUnit != "" && v.LeadValue > 0 { //领先指标
				var startTimeRealTemp time.Time
				startDateParse, _ := time.Parse(utils.FormatDate, startDate)
				switch v.LeadUnit {
				case "天":
					startTimeRealTemp = startDateParse.AddDate(0, 0, -v.LeadValue)
				case "月":
					startTimeRealTemp = startDateParse.AddDate(0, -v.LeadValue, 0)
				case "季":
					startTimeRealTemp = startDateParse.AddDate(0, -3*v.LeadValue, 0)
				case "周":
					startTimeRealTemp = startDateParse.AddDate(0, 0, -7*v.LeadValue)
				case "年":
					startTimeRealTemp = startDateParse.AddDate(-v.LeadValue, 0, 0)
				}
				if startTimeRealTemp.Before(startDateParse) {
					startDateReal = startTimeRealTemp.Format(utils.FormatDate)
					diffSeconds = (int64(startTimeRealTemp.UnixNano()) - int64(startDateParse.UnixNano())) / 1e6
				} else {
					startDateReal = startDate
					diffSeconds = 0
				}

				// 预测指标的开始日期也要偏移
				{
					day, tmpErr := utils.GetDaysBetween2Date(utils.FormatDate, startDate, startDateReal)
					if tmpErr != nil {
						err = tmpErr
						return
					}
					item.MoveLatestDate = item.MoveLatestDate.AddDate(0, 0, day)
				}
			} else {
				startDateReal = startDate
			}
		}
		//fmt.Println("line 1011 chart:", v.Source, v.EdbInfoId, startDateReal, endDate)
		calendarPreYear := 0
		if calendar == "农历" {
			newStartDateReal, err := time.Parse(utils.FormatDate, startDateReal)
			if err != nil {
				fmt.Println("time.Parse:" + err.Error())
			}
			calendarPreYear = newStartDateReal.Year() - 1
			newStartDateReal = newStartDateReal.AddDate(-1, 0, 0)
			startDateReal = newStartDateReal.Format(utils.FormatDate)
		}
		dataList := make([]*edbDataModel.EdbDataList, 0)
		//fmt.Println("chart:", v.Source, v.EdbInfoId, startDateReal, endDate, ";EdbInfoCategoryType:", v.EdbInfoCategoryType)
		//var newEdbInfo *edbInfoModel.EdbInfo
		switch v.EdbInfoCategoryType {
		case 0:
			dataList, err = edbDataModel.GetEdbDataList(v.Source, v.SubSource, v.EdbInfoId, startDateReal, endDate)
		case 1:
			_, dataList, _, _, err, _ = GetPredictDataListByPredictEdbInfoId(v.EdbInfoId, startDateReal, endDate, false)
			//if item.MaxValue < newEdbInfo.MaxValue {
			//	item.MaxData = newEdbInfo.MaxValue
			//}
			//if item.MinData > newEdbInfo.MinValue {
			//	item.MinData = newEdbInfo.MinValue
			//}
		default:
			err = errors.New(fmt.Sprint("获取失败,指标类型异常", v.EdbInfoCategoryType))
		}
		if err != nil {
			return
		}

		if v.IsConvert == 1 {
			switch v.ConvertType {
			case 1:
				for i, data := range dataList {
					dataList[i].Value = data.Value * v.ConvertValue
				}
				//item.MaxData = item.MaxData * v.ConvertValue
				//item.MinData = item.MinData * v.ConvertValue
			case 2:
				for i, data := range dataList {
					dataList[i].Value = data.Value / v.ConvertValue
				}
				//item.MaxData = item.MaxData / v.ConvertValue
				//item.MinData = item.MinData / v.ConvertValue
			case 3:
				for i, data := range dataList {
					dataList[i].Value = math.Log(data.Value) / math.Log(v.ConvertValue)
				}
				//item.MaxData = math.Log(item.MaxData) / math.Log(v.ConvertValue)
				//item.MinData = math.Log(item.MinData) / math.Log(v.ConvertValue)
			}
		}

		edbDataListMap[v.EdbInfoId] = dataList

		if diffSeconds != 0 && v.EdbInfoType == 0 {
			dataListLen := len(dataList)
			for i := 0; i < dataListLen; i++ {
				dataList[i].DataTimestamp = dataList[i].DataTimestamp - diffSeconds
			}
		}

		if chartType == 2 && v.IsAxis == 1 {
			//latestDateStr := v.LatestDate.Format(utils.FormatDate) //实际数据的截止日期
			latestDate := v.LatestDate

			if calendar == "农历" {
				if len(dataList) <= 0 {
					result := new(edbDataModel.EdbDataResult)
					item.DataList = result
				} else {
					result, tmpErr := edbDataService.AddCalculateQuarterV6(dataList)
					if tmpErr != nil {
						err = errors.New("获取农历数据失败,Err:" + tmpErr.Error())
						return
					}
					quarterDataList, seasonXStartDateWithYearTmp, seasonXEndDateWithYearTmp, tErr := GetSeasonEdbInfoDataListByXDateNong(result, latestDate, seasonExtraConfig, calendarPreYear)
					if tErr != nil {
						err = errors.New("获取季节性图表数据失败,Err:" + tErr.Error())
						return
					}
					item.DataList = quarterDataList
					seasonXStartDateWithYear = seasonXStartDateWithYearTmp
					seasonXEndDateWithYear = seasonXEndDateWithYearTmp
				}

			} else {
				quarterDataList, seasonXStartDateWithYearTmp, seasonXEndDateWithYearTmp, tErr := GetSeasonEdbInfoDataListByXDate(dataList, latestDate, seasonExtraConfig)
				if tErr != nil {
					err = errors.New("获取季节性图表数据失败,Err:" + tErr.Error())
					return
				}
				item.DataList = quarterDataList
				seasonXStartDateWithYear = seasonXStartDateWithYearTmp
				seasonXEndDateWithYear = seasonXEndDateWithYearTmp
			}
		} else if chartType == 2 && v.IsAxis == 0 { //季节性图的非横轴指标
			newDataList := make([]*edbDataModel.EdbDataList, 0)
			for _, v := range dataList {
				dataTime, e := time.Parse(utils.FormatDate, v.DataTime)
				if e != nil {
					err = errors.New("季节性图处理右轴指标数据转换日期失败,Err:" + e.Error())
					return
				}
				seasonXStartDateWithYearT, _ := time.Parse(utils.FormatDate, seasonXStartDateWithYear)
				seasonXEndDateWithYearT, _ := time.Parse(utils.FormatDate, seasonXEndDateWithYear)
				if dataTime.Equal(seasonXStartDateWithYearT) || (dataTime.After(seasonXStartDateWithYearT) && dataTime.Before(seasonXEndDateWithYearT)) || dataTime.Equal(seasonXEndDateWithYearT) {
					newDataList = append(newDataList, v)
				}
			}
			item.DataList = newDataList
		} else if chartType == 7 || chartType == utils.CHART_TYPE_RADAR { //柱方图
			//item.DataList = dataList
		} else {
			item.DataList = dataList
		}
		edbList = append(edbList, item)
	}

	return
}

// GetSeasonEdbInfoDataListByXDate 季节性图的指标数据根据横轴展示
func GetSeasonEdbInfoDataListByXDate(dataList []*edbDataModel.EdbDataList, latestDate time.Time, seasonExtraConfig string) (quarterDataListSort edbDataModel.QuarterDataList, xStartDateWithYear string, xEndDateWithYear string, err error) {
	xStartDate := "01-01"
	xEndDate := "12-31"
	jumpYear := 0
	legends := make([]edbDataModel.SeasonChartLegend, 0)
	var seasonExtra edbDataModel.SeasonExtraItem
	if seasonExtraConfig != "" {
		err = json.Unmarshal([]byte(seasonExtraConfig), &seasonExtra)
		if err != nil {
			return
		}
	}

	if seasonExtra.XStartDate != "" {
		xStartDate = seasonExtra.XStartDate
		xEndDate = seasonExtra.XEndDate
		jumpYear = seasonExtra.JumpYear
		legends = seasonExtra.ChartLegend
	}

	length := len(dataList)
	if length == 0 {
		return
	}
	legendMap := make(map[string]string, 0)
	if len(legends) > 0 {
		for _, v := range legends {
			legendMap[v.Name] = v.Value
		}
	}
	latestDateStr := latestDate.Format(utils.FormatDate)

	//判断横轴的两个时间之间是不是跨年了，如果跨年了，则横轴截止年份比起始年份+1，如果不跨年，截止年份等于起始年份
	//根据数据确定最早的年份，和最近年份
	//根据横轴的日期，汇总所有的年份
	startDate := dataList[0].DataTime
	startDateT, tmpErr := time.Parse(utils.FormatDate, startDate)
	if tmpErr != nil {
		err = tmpErr
		return
	}
	startYear := startDateT.Year()
	//获取数据的最新日期
	lastDate := dataList[length-1].DataTime
	lastDateT, tmpErr := time.Parse(utils.FormatDate, lastDate)
	if tmpErr != nil {
		err = tmpErr
		return
	}
	endYear := lastDateT.Year()
	nowYear := endYear
	chartLegendMaxYear := 0
	dataMap := make(map[string]edbDataModel.QuarterXDateItem, 0)

	quarterDataList := make([]*edbDataModel.QuarterData, 0)
	quarterMap := make(map[string][]*edbDataModel.EdbDataList, 0)

	//整理出日期
	idx := 1
	chartLegendMap := make(map[string]int, 0)
	for currentStartYear := startYear; currentStartYear <= endYear; currentStartYear++ {
		startStr := fmt.Sprintf("%d-%s", currentStartYear, xStartDate)
		currentEndYear := currentStartYear
		if jumpYear == 1 {
			currentEndYear = currentStartYear + 1
		}
		endStr := fmt.Sprintf("%d-%s", currentEndYear, xEndDate)
		name := fmt.Sprintf("%s_%s", startStr, endStr)
		showName := fmt.Sprintf("%d_%d", currentStartYear, currentEndYear)

		startT, tEr := time.Parse(utils.FormatDate, startStr)
		if tEr != nil {
			err = tEr
			return
		}

		endT, tEr := time.Parse(utils.FormatDate, endStr)
		if tEr != nil {
			err = tEr
			return
		}

		if lastDateT.Before(startT) {
			//如果最新的日期在起始日之前，则跳出循环
			break
		}

		if endT.Year() > nowYear {
			//如果最新的日期比真实年份要大，则数据全部按照最大的年份补齐
			nowYear = endT.Year()
		}
		item := edbDataModel.QuarterXDateItem{
			StartDate: startT,
			EndDate:   endT,
			ShowName:  showName,
		}
		chartLegendMaxYear = endT.Year()
		dataMap[name] = item
		chartLegendMap[name] = idx
		idx++
		xStartDateWithYear = startStr
		xEndDateWithYear = endStr
		if lastDateT.Before(endT) {
			//如果最新的日期在起始日之前，则跳出循环
			break
		}
	}
	lenYear := len(dataMap)
	if chartLegendMaxYear > endYear {
		chartLegendMaxYear = endYear
	}
	for k, v := range dataMap {
		if i, ok := chartLegendMap[k]; ok {
			//v.ChartLegend = strconv.Itoa(endYear - lenYear + i)
			v.ChartLegend = strconv.Itoa(chartLegendMaxYear - lenYear + i)
		}
		dataMap[k] = v
	}

	for _, v := range dataList {
		dataTimeT, _ := time.Parse(utils.FormatDate, v.DataTime)
		year := dataTimeT.Year()
		newItemDate := dataTimeT.AddDate(nowYear-year, 0, 0)
		for k, dateItem := range dataMap {
			tmpVal := edbDataModel.EdbDataList{
				EdbDataId:     v.EdbDataId,
				EdbInfoId:     v.EdbInfoId,
				DataTime:      v.DataTime,
				DataTimestamp: v.DataTimestamp,
				Value:         v.Value,
			}
			if (dateItem.StartDate.Before(dataTimeT) && dateItem.EndDate.After(dataTimeT)) || dateItem.StartDate == dataTimeT || dateItem.EndDate == dataTimeT {
				if jumpYear == 1 {
					//计算前一年最大的日期, 只补齐数据到去年
					beforeYearMaxDate := fmt.Sprintf("%d-12-31", dateItem.StartDate.Year())
					beforeYearMaxDateT, _ := time.Parse(utils.FormatDate, beforeYearMaxDate)
					if dataTimeT.Before(beforeYearMaxDateT) || dataTimeT == beforeYearMaxDateT {
						newItemDate = dataTimeT.AddDate(nowYear-year-1, 0, 0)
					} else {
						newItemDate = dataTimeT.AddDate(nowYear-year, 0, 0)
					}
				} else {
					newItemDate = dataTimeT.AddDate(nowYear-year, 0, 0)
				}
				timestamp := newItemDate.UnixNano() / 1e6
				tmpVal.DataTimestamp = timestamp
				tmpV := &tmpVal
				if findVal, ok := quarterMap[k]; !ok {
					findVal = append(findVal, tmpV)
					quarterMap[k] = findVal
				} else {
					findVal = append(findVal, tmpV)
					quarterMap[k] = findVal
				}

				if v.DataTime == latestDateStr {
					dateItem.CuttingDataTimestamp = timestamp
					dataMap[k] = dateItem
				}
				//break
			}
		}
	}
	for k, v := range dataMap {
		itemList := quarterMap[k]
		quarterItem := new(edbDataModel.QuarterData)
		quarterItem.Years = v.ShowName
		quarterItem.Year = v.ChartLegend
		quarterItem.ChartLegend = v.ChartLegend
		if le, ok := legendMap[v.ShowName]; ok {
			if le != strconv.Itoa(v.StartDate.Year()) && le != strconv.Itoa(v.EndDate.Year()) {
				quarterItem.ChartLegend = le
			}
		}
		quarterItem.DataList = itemList
		quarterItem.CuttingDataTimestamp = v.CuttingDataTimestamp

		//如果等于最后的实际日期，那么将切割时间戳记录
		if quarterItem.CuttingDataTimestamp == 0 {
			//如果大于最后的实际日期，那么第一个点就是切割的时间戳
			if latestDate.Before(v.StartDate) && len(itemList) > 0 {
				quarterItem.CuttingDataTimestamp = itemList[0].DataTimestamp - 100
			}
		}
		quarterDataList = append(quarterDataList, quarterItem)
	}

	if len(quarterDataList) > 0 {
		quarterDataListSort = quarterDataList
		sort.Sort(quarterDataListSort)
	}
	return
}

// GetSeasonEdbInfoDataListByXDateNong 季节性图的指标数据根据横轴选择农历时展示
func GetSeasonEdbInfoDataListByXDateNong(result *edbDataModel.EdbDataResult, latestDate time.Time, seasonExtraConfig string, calendarPreYear int) (quarterDataListSort edbDataModel.QuarterDataList, xStartDateWithYear string, xEndDateWithYear string, err error) {
	xStartDate := "01-01"
	xEndDate := "12-31"
	jumpYear := 0
	legends := make([]edbDataModel.SeasonChartLegend, 0)
	var seasonExtra edbDataModel.SeasonExtraItem
	if seasonExtraConfig != "" {
		err = json.Unmarshal([]byte(seasonExtraConfig), &seasonExtra)
		if err != nil {
			return
		}
	}

	if seasonExtra.XStartDate != "" {
		xStartDate = seasonExtra.XStartDate
		xEndDate = seasonExtra.XEndDate
		jumpYear = seasonExtra.JumpYear
		legends = seasonExtra.ChartLegend
	}

	length := len(result.List)
	if length == 0 {
		return
	}
	legendMap := make(map[string]string, 0)
	if len(legends) > 0 {
		for _, v := range legends {
			legendMap[v.Name] = v.Value
		}
	}
	latestDateYear := latestDate.Year()
	//判断横轴的两个时间之间是不是跨年了，如果跨年了，则横轴截止年份比起始年份+1，如果不跨年，截止年份等于起始年份
	//根据数据确定最早的年份，和最近年份
	//根据横轴的日期，汇总所有的年份

	startYear := result.List[0].Year
	/*if jumpYear == 1 {
		if startYear != calendarPreYear {
			startYear = startYear - 1
		}
	}*/

	itemLength := len(result.List[length-1].Items)
	//获取数据的最新日期
	lastDate := result.List[length-1].Items[itemLength-1].DataTime
	maxY := result.List[length-1].Year
	lastDateT, tmpErr := time.Parse(utils.FormatDate, lastDate)
	if tmpErr != nil {
		err = tmpErr
		return
	}
	endYear := lastDateT.Year()
	nowYear := endYear
	chartLegendMaxYear := 0
	dataMap := make(map[string]edbDataModel.QuarterXDateItem, 0)

	quarterDataList := make([]*edbDataModel.QuarterData, 0)
	resultData := make([]*edbDataModel.QuarterData, 0)
	quarterMap := make(map[string][]*edbDataModel.EdbDataList, 0)

	//整理出日期
	var startTmpT, endTmpT time.Time
	idx := 1
	chartLegendMap := make(map[string]int, 0)
	for currentStartYear := startYear; currentStartYear <= endYear; currentStartYear++ {
		startStr := fmt.Sprintf("%d-%s", currentStartYear, xStartDate)
		currentEndYear := currentStartYear
		if jumpYear == 1 {
			currentEndYear = currentStartYear + 1
		}
		endStr := fmt.Sprintf("%d-%s", currentEndYear, xEndDate)
		showName := fmt.Sprintf("%d_%d", currentStartYear, currentEndYear)

		startT, tEr := time.Parse(utils.FormatDate, startStr)
		if tEr != nil {
			err = tEr
			return
		}

		endT, tEr := time.Parse(utils.FormatDate, endStr)
		if tEr != nil {
			err = tEr
			return
		}

		if lastDateT.Before(startT) {
			//如果最新的日期在起始日之前，则跳出循环
			break
		}
		if endT.Year() > nowYear {
			//如果最新的日期比真实年份要大，则数据全部按照最大的年份补齐
			nowYear = endT.Year()
		}
		item := edbDataModel.QuarterXDateItem{
			StartDate: startT,
			EndDate:   endT,
			ShowName:  showName,
		}
		chartLegendMaxYear = endT.Year()
		dataMap[showName] = item
		fmt.Println("年份" + showName + "日期" + startStr + " " + endStr)
		startTmpT = startT
		endTmpT = endT
		chartLegendMap[showName] = idx
		idx++
		xStartDateWithYear = startStr
		xEndDateWithYear = endStr
		if lastDateT.Before(endT) {
			//如果最新的日期在起始日之前，则跳出循环
			break
		}
	}
	lenYear := len(dataMap)
	if chartLegendMaxYear > endYear {
		chartLegendMaxYear = endYear
	}
	for k, v := range dataMap {
		if i, ok := chartLegendMap[k]; ok {
			//v.ChartLegend = strconv.Itoa(endYear - lenYear + i)
			v.ChartLegend = strconv.Itoa(chartLegendMaxYear - lenYear + i)
		}
		dataMap[k] = v
	}

	yearDataListMap := make(map[int]*edbDataModel.EdbDataItems, 0)

	for _, lv := range result.List {
		yearDataListMap[lv.Year] = lv
	}

	fmt.Println("横轴截取日" + startTmpT.Format(utils.FormatDate) + " " + endTmpT.Format(utils.FormatDate))
	fmt.Printf("lastDateT.Year() 为%d \n", lastDateT.Year())
	fmt.Printf("maxY 为%d \n", maxY)
	for name, dateItem := range dataMap {
		tY := dateItem.EndDate.Year()
		if lastDateT.Month() >= 11 {
			if maxY > endTmpT.Year() {
				tY = tY + 1
			}
		}
		lv, ok1 := yearDataListMap[tY]
		fmt.Printf("name %s yearDataListMap[%d]\n", name, tY)
		if !ok1 {
			continue
		}
		for _, item := range lv.Items {
			tmpVal := edbDataModel.EdbDataList{
				EdbDataId:     item.EdbDataId,
				EdbInfoId:     item.EdbInfoId,
				DataTime:      item.DataTime,
				DataTimestamp: item.DataTimestamp,
				Value:         item.Value,
			}
			dataTimeT, _ := time.Parse(utils.FormatDate, item.DataTime)
			if (startTmpT.Before(dataTimeT) && endTmpT.After(dataTimeT)) || startTmpT == dataTimeT || endTmpT == dataTimeT {
				tmpV := &tmpVal
				if findVal, ok := quarterMap[name]; !ok {
					findVal = append(findVal, tmpV)
					quarterMap[name] = findVal
				} else {
					findVal = append(findVal, tmpV)
					quarterMap[name] = findVal
				}
				if lv.Year >= latestDateYear {
					// 切割的日期时间字符串
					cuttingDataTimeStr := latestDate.AddDate(0, 0, lv.BetweenDay).Format(utils.FormatDate)
					if item.DataTime == cuttingDataTimeStr {
						dateItem.CuttingDataTimestamp = tmpVal.DataTimestamp
						dataMap[name] = dateItem
					}
				}
			}
		}
	}

	for k, v := range dataMap {
		itemList := quarterMap[k]
		quarterItem := new(edbDataModel.QuarterData)
		quarterItem.Years = v.ShowName
		quarterItem.Year = v.ChartLegend
		quarterItem.ChartLegend = v.ChartLegend
		if le, ok := legendMap[v.ShowName]; ok {
			if le != strconv.Itoa(v.StartDate.Year()) && le != strconv.Itoa(v.EndDate.Year()) {
				quarterItem.ChartLegend = le
			}
		}
		quarterItem.DataList = itemList
		quarterItem.CuttingDataTimestamp = v.CuttingDataTimestamp
		//如果等于最后的实际日期，那么将切割时间戳记录
		if quarterItem.CuttingDataTimestamp == 0 {
			//如果大于最后的实际日期，那么第一个点就是切割的时间戳
			if latestDate.Before(v.StartDate) && len(itemList) > 0 {
				quarterItem.CuttingDataTimestamp = itemList[0].DataTimestamp - 100
			}
		}
		quarterDataList = append(quarterDataList, quarterItem)
	}

	if result.List[0].Year != calendarPreYear {
		itemList := make([]*edbDataModel.EdbDataList, 0)
		items := new(edbDataModel.QuarterData)
		//items.Year = calendarPreYear
		items.DataList = itemList

		newResult := make([]*edbDataModel.QuarterData, 0)
		newResult = append(newResult, items)
		newResult = append(newResult, quarterDataList...)
		resultData = newResult
	} else {
		resultData = quarterDataList
	}

	if len(quarterDataList) > 0 {
		quarterDataListSort = resultData
		sort.Sort(quarterDataListSort)
	}
	return
}

// BarChartData 柱方图的数据处理
func BarChartData(mappingList []*chartEdbMappingModel.ChartEdbInfoMapping, edbDataListMap map[int][]*edbDataModel.EdbDataList, barChartInfoDateList []request.BarChartInfoDateReq, barChartInfoSort request.BarChartInfoSortReq) (edbIdList []int, yDataList []chart_info.YData, err error) {
	// 指标数据数组（10086:{"2022-12-02":100.01,"2022-12-01":102.3}）
	edbDataMap := make(map[int]map[string]float64)
	for edbInfoId, edbDataList := range edbDataListMap {
		edbDateData := make(map[string]float64)
		for _, edbData := range edbDataList {
			edbDateData[edbData.DataTime] = edbData.Value
		}
		edbDataMap[edbInfoId] = edbDateData
	}

	// edbIdList 指标展示顺序；x轴的指标顺序
	edbIdList = make([]int, 0)
	//Sort int    `description:"排序类型，0：默认，1：升序，2：降序"`
	dateData := make(map[int]float64)
	if barChartInfoSort.Sort == 0 {
		for _, v := range mappingList {
			edbIdList = append(edbIdList, v.EdbInfoId)
		}
	} else {
		lenBarChartInfoDateList := len(barChartInfoDateList)
		if barChartInfoSort.DateIndex >= lenBarChartInfoDateList {
			err = errors.New("排序日期异常")
			return
		}

		notDataEdbIdList := make([]int, 0) //没有数据的指标id
		// 日期配置
		barChartInfoDate := barChartInfoDateList[barChartInfoSort.DateIndex]
		for edbInfoId, dataList := range edbDataListMap {
			if len(dataList) <= 0 {
				// 没有数据的指标id
				notDataEdbIdList = append(notDataEdbIdList, edbInfoId)
				continue
			}
			findDate := barChartInfoDate.Date
			switch barChartInfoDate.Type {
			case 1: //最新值
				findDate = dataList[len(dataList)-1].DataTime
			case 2: //近期几天
				findDateTime, tmpErr := time.ParseInLocation(utils.FormatDate, dataList[len(dataList)-1].DataTime, time.Local)
				if tmpErr != nil {
					err = tmpErr
					return
				}
				findDateTime = findDateTime.AddDate(0, 0, -barChartInfoDate.Value)

				lenData := len(dataList) - 1

				for i := lenData; i >= 0; i-- {
					currDateTime, tmpErr := time.ParseInLocation(utils.FormatDate, dataList[i].DataTime, time.Local)
					if tmpErr != nil {
						err = tmpErr
						return
					}
					if currDateTime.Equal(findDateTime) || currDateTime.Before(findDateTime) {
						findDate = dataList[i].DataTime
						break
					}
				}
			case 3: // 固定日期
				//最早的日期
				minDateTime, tmpErr := time.ParseInLocation(utils.FormatDate, dataList[0].DataTime, time.Local)
				if tmpErr != nil {
					err = tmpErr
					return
				}
				//寻找固定日期的数据
				findDateTime, tmpErr := time.ParseInLocation(utils.FormatDate, barChartInfoDate.Date, time.Local)
				if tmpErr != nil {
					err = tmpErr
					return
				}
				for tmpDateTime := findDateTime; tmpDateTime.After(minDateTime) || tmpDateTime.Equal(minDateTime); tmpDateTime = tmpDateTime.AddDate(0, 0, -1) {
					tmpDate := tmpDateTime.Format(utils.FormatDate)
					if _, ok := edbDataMap[edbInfoId][tmpDate]; ok { //如果能找到数据，那么就返回
						findDate = tmpDate
						break
					}
				}
			default:
				err = errors.New(fmt.Sprint("日期类型异常,Type:", barChartInfoDate.Type))
				return
			}
			if tmpValue, ok := edbDataMap[edbInfoId][findDate]; ok {
				dateData[edbInfoId] = tmpValue
			} else {
				// 没有数据的指标id
				notDataEdbIdList = append(notDataEdbIdList, edbInfoId)
			}
		}

		//Sort int    `description:"排序类型，0：默认，1：升序，2：降序"`
		// 排序
		dateDataSort := utils.NewMapSorter(dateData)
		sort.Sort(dateDataSort)
		if barChartInfoSort.Sort == 1 {
			// 先将没有数据的指标id放在最前面
			if len(notDataEdbIdList) > 0 {
				edbIdList = append(edbIdList, notDataEdbIdList...)
			}
			for _, v := range dateDataSort {
				edbIdList = append(edbIdList, v.Key)
			}
		} else {
			for i := len(dateDataSort) - 1; i >= 0; i-- {
				edbIdList = append(edbIdList, dateDataSort[i].Key)
			}
			// 再将没有数据的指标id放在最后面
			if len(notDataEdbIdList) > 0 {
				edbIdList = append(edbIdList, notDataEdbIdList...)
			}
		}
	}

	yDataList = make([]chart_info.YData, 0) //y轴的数据列表

	for _, barChartInfoDate := range barChartInfoDateList {
		var maxDate time.Time

		findDataList := make([]float64, 0) // 当前日期的数据值
		for _, edbInfoId := range edbIdList {
			findDate := barChartInfoDate.Date     //需要的日期值
			dataList := edbDataListMap[edbInfoId] //指标的所有数据值
			if len(dataList) <= 0 {
				// 没有数据的指标id
				findDataList = append(findDataList, 0)
				continue
			}
			switch barChartInfoDate.Type {
			case 1: //最新值
				dataList := edbDataListMap[edbInfoId]
				findDate = dataList[len(dataList)-1].DataTime
			case 2: //近期几天
				findDateTime, tmpErr := time.ParseInLocation(utils.FormatDate, dataList[len(dataList)-1].DataTime, time.Local)
				if tmpErr != nil {
					err = tmpErr
					return
				}
				findDateTime = findDateTime.AddDate(0, 0, -barChartInfoDate.Value)

				lenData := len(dataList) - 1
				for i := lenData; i >= 0; i-- {
					currDateTime, tmpErr := time.ParseInLocation(utils.FormatDate, dataList[i].DataTime, time.Local)
					if tmpErr != nil {
						err = tmpErr
						return
					}
					if currDateTime.Equal(findDateTime) || currDateTime.Before(findDateTime) {
						findDate = dataList[i].DataTime
						break
					}
				}
			case 3: // 固定日期
				//最早的日期
				minDateTime, tmpErr := time.ParseInLocation(utils.FormatDate, dataList[0].DataTime, time.Local)
				if tmpErr != nil {
					err = tmpErr
					return
				}
				//寻找固定日期的数据
				findDateTime, tmpErr := time.ParseInLocation(utils.FormatDate, barChartInfoDate.Date, time.Local)
				if tmpErr != nil {
					err = tmpErr
					return
				}
				for tmpDateTime := findDateTime; tmpDateTime.After(minDateTime) || tmpDateTime.Equal(minDateTime); tmpDateTime = tmpDateTime.AddDate(0, 0, -1) {
					tmpDate := tmpDateTime.Format(utils.FormatDate)
					if _, ok := edbDataMap[edbInfoId][tmpDate]; ok { //如果能找到数据，那么就返回
						findDate = tmpDate
						break
					}
				}
			default:
				err = errors.New(fmt.Sprint("日期类型异常,Type:", barChartInfoDate.Type))
				return
			}
			findDateTime, _ := time.ParseInLocation(utils.FormatDate, findDate, time.Local)
			if maxDate.IsZero() {
				maxDate = findDateTime
			} else {
				if findDateTime.After(maxDate) {
					maxDate = findDateTime
				}
			}
			if tmpValue, ok := edbDataMap[edbInfoId][findDate]; ok {
				findDataList = append(findDataList, tmpValue)
			} else {
				findDataList = append(findDataList, 0)
			}
		}
		yDate := "0000-00-00"
		if !maxDate.IsZero() {
			yDate = maxDate.Format(utils.FormatDate)
		}

		yDataList = append(yDataList, chart_info.YData{
			Date:  yDate,
			Value: findDataList,
			Color: barChartInfoDate.Color,
			Name:  barChartInfoDate.Name,
		})
	}

	return
}

// 获取频度的英文版
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
}

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
}

// ChartInfoRefreshV2 图表刷新
// @author Roc
// @datetime 2022-09-16 10:15:38
// @description 将原来自己写的一套获取所有关联指标，然后刷新指标逻辑 改成  只获取使用的指标id，然后遍历去调用“指标刷新服务”
func ChartInfoRefreshV2(chartInfoId int) (err error) {
	var errmsg string
	defer func() {
		if err != nil {
			go alarm_msg.SendAlarmMsg("ChartInfoRefresh:"+errmsg, 3)
			//go utils.SendEmail(utils.APPNAME+"【"+utils.RunMode+"】"+"失败提醒", "ChartInfoRefresh:"+errmsg, utils.EmailSendToUsers)
			fmt.Println("ChartInfoRefresh Err:" + errmsg)
		}
	}()

	edbMappingList, err := chartEdbMappingModel.GetMappingListByChartInfoId(chartInfoId)
	if err != nil {
		errmsg = "获取需要刷新的指标失败:Err:" + err.Error()
		return
	}

	edbIdList := make([]int, 0)
	for _, v := range edbMappingList {
		edbIdList = append(edbIdList, v.EdbInfoId)
	}

	// 批量刷新
	err, errmsg = EdbInfoRefreshAllFromBase(edbIdList, false)
	if err != nil {
		return
	}

	return
}

// CheckChartExtraConfig 校验图表额外配置的信息,并且获取相关联的指标id
func CheckChartExtraConfig(chartType int, extraConfigStr string) (edbIdList []int, err error, errMsg string) {
	switch chartType {
	case 10: //截面散点
		var extraConfig request.SectionScatterReq
		err = json.Unmarshal([]byte(extraConfigStr), &extraConfig)
		if err != nil {
			return
		}

		// 判断是否有配置日期序列
		if len(extraConfig.SeriesList) <= 0 {
			errMsg = `请配置序列`
			err = errors.New(errMsg)
			return
		}

		// 判断是否有填写指标
		if len(extraConfig.SeriesList[0].EdbInfoList) <= 0 {
			errMsg = `请选择指标`
			err = errors.New(errMsg)
			return
		}

		// 遍历指标列表获取指标id
		edbIdMap := make(map[int]int)
		for _, v := range extraConfig.SeriesList[0].EdbInfoList {
			// X 轴的指标id
			if _, ok := edbIdMap[v.XEdbInfoId]; !ok {
				edbIdMap[v.XEdbInfoId] = v.XEdbInfoId
				edbIdList = append(edbIdList, v.XEdbInfoId)
			}

			// Y 轴的指标id
			if _, ok := edbIdMap[v.YEdbInfoId]; !ok {
				edbIdMap[v.YEdbInfoId] = v.YEdbInfoId
				edbIdList = append(edbIdList, v.YEdbInfoId)
			}
		}
	}

	return
}

// GetSectionScatterChartData 柱方图的数据处理
func GetSectionScatterChartData(mappingList []*chartEdbMappingModel.ChartEdbInfoMapping, edbDataListMap map[int][]*edbDataModel.EdbDataList, extraConfig request.SectionScatterReq) (edbIdList []int, chartDataResp request.SectionScatterInfoResp, err error) {
	// 指标数据数组（10086:{"2022-12-02":100.01,"2022-12-01":102.3}）
	edbDataMap := make(map[int]map[string]float64)
	for edbInfoId, edbDataList := range edbDataListMap {
		edbDateData := make(map[string]float64)
		for _, edbData := range edbDataList {
			edbDateData[edbData.DataTime] = edbData.Value
		}
		edbDataMap[edbInfoId] = edbDateData
	}

	// edbIdList 指标展示顺序；x轴的指标顺序
	edbIdList = make([]int, 0)
	edbMappingMap := make(map[int]*chartEdbMappingModel.ChartEdbInfoMapping)
	for _, v := range mappingList {
		edbIdList = append(edbIdList, v.EdbInfoId)
		edbMappingMap[v.EdbInfoId] = v
	}
	//SectionScatterSeriesInfoResp

	dataListResp := make([]request.SectionScatterSeriesItemResp, 0) //y轴的数据列表

	for _, seriesItem := range extraConfig.SeriesList {
		var maxDate time.Time
		// 系列中的指标数据
		tmpSeriesEdbInfoList := make([]request.SectionScatterEdbItemResp, 0)

		var minXVal, maxXVal, minYVal, maxYVal float64
		for _, edbConf := range seriesItem.EdbInfoList {
			tmpItem := request.SectionScatterEdbItemResp{
				IsShow: edbConf.IsShow,
				Name:   edbConf.Name,
				NameEn: edbConf.NameEn,
			} //单个坐标点的数据

			//X轴的数据
			{
				edbInfoId := edbConf.XEdbInfoId //X轴的指标
				edbMappingInfo, ok := edbMappingMap[edbInfoId]
				if !ok {
					continue
				}
				findDate := edbConf.XDate             //需要的日期值
				dataList := edbDataListMap[edbInfoId] //指标的所有数据值
				if len(dataList) <= 0 {
					// 没有数据的指标id
					//findDataList = append(findDataList, 0)
					continue
				}

				tmpItem.XEdbInfoId = edbInfoId
				tmpItem.XName = edbMappingInfo.EdbName
				tmpItem.XNameEn = edbMappingInfo.EdbNameEn

				switch edbConf.XDateType {
				case 1: //最新值
					dataList := edbDataListMap[edbInfoId]
					findDate = dataList[len(dataList)-1].DataTime
				case 2: //近期几天
					findDateTime, tmpErr := time.ParseInLocation(utils.FormatDate, dataList[len(dataList)-1].DataTime, time.Local)
					if tmpErr != nil {
						err = tmpErr
						return
					}
					findDateTime = findDateTime.AddDate(0, 0, -edbConf.XDateValue)

					lenData := len(dataList) - 1
					for i := lenData; i >= 0; i-- {
						currDateTime, tmpErr := time.ParseInLocation(utils.FormatDate, dataList[i].DataTime, time.Local)
						if tmpErr != nil {
							err = tmpErr
							return
						}
						if currDateTime.Equal(findDateTime) || currDateTime.Before(findDateTime) {
							findDate = dataList[i].DataTime
							break
						}
					}
				case 3: // 固定日期
					//最早的日期
					minDateTime, tmpErr := time.ParseInLocation(utils.FormatDate, dataList[0].DataTime, time.Local)
					if tmpErr != nil {
						err = tmpErr
						return
					}
					//寻找固定日期的数据
					findDateTime, tmpErr := time.ParseInLocation(utils.FormatDate, edbConf.XDate, time.Local)
					if tmpErr != nil {
						err = tmpErr
						return
					}
					for tmpDateTime := findDateTime; tmpDateTime.After(minDateTime) || tmpDateTime.Equal(minDateTime); tmpDateTime = tmpDateTime.AddDate(0, 0, -1) {
						tmpDate := tmpDateTime.Format(utils.FormatDate)
						if _, ok := edbDataMap[edbInfoId][tmpDate]; ok { //如果能找到数据，那么就返回
							findDate = tmpDate
							break
						}
					}
				default:
					err = errors.New(fmt.Sprint("日期类型异常,Type:", edbConf.XDate))
					return
				}
				findDateTime, _ := time.ParseInLocation(utils.FormatDate, findDate, time.Local)
				if maxDate.IsZero() {
					maxDate = findDateTime
				} else {
					if findDateTime.After(maxDate) {
						maxDate = findDateTime
					}
				}
				if tmpValue, ok := edbDataMap[edbInfoId][findDate]; ok {
					tmpItem.XDate = findDate
					tmpItem.XValue = tmpValue
				} else {
					continue
				}
			}

			//Y轴的数据
			{
				edbInfoId := edbConf.YEdbInfoId //Y轴的指标
				edbMappingInfo, ok := edbMappingMap[edbInfoId]
				if !ok {
					continue
				}
				findDate := edbConf.YDate             //需要的日期值
				dataList := edbDataListMap[edbInfoId] //指标的所有数据值
				if len(dataList) <= 0 {
					// 没有数据的指标id
					//findDataList = append(findDataList, 0)
					continue
				}

				tmpItem.YEdbInfoId = edbInfoId
				tmpItem.YName = edbMappingInfo.EdbName
				tmpItem.YNameEn = edbMappingInfo.EdbNameEn

				switch edbConf.YDateType {
				case 1: //最新值
					dataList := edbDataListMap[edbInfoId]
					findDate = dataList[len(dataList)-1].DataTime
				case 2: //近期几天
					findDateTime, tmpErr := time.ParseInLocation(utils.FormatDate, dataList[len(dataList)-1].DataTime, time.Local)
					if tmpErr != nil {
						err = tmpErr
						return
					}
					findDateTime = findDateTime.AddDate(0, 0, -edbConf.YDateValue)

					lenData := len(dataList) - 1
					for i := lenData; i >= 0; i-- {
						currDateTime, tmpErr := time.ParseInLocation(utils.FormatDate, dataList[i].DataTime, time.Local)
						if tmpErr != nil {
							err = tmpErr
							return
						}
						if currDateTime.Equal(findDateTime) || currDateTime.Before(findDateTime) {
							findDate = dataList[i].DataTime
							break
						}
					}
				case 3: // 固定日期
					//最早的日期
					minDateTime, tmpErr := time.ParseInLocation(utils.FormatDate, dataList[0].DataTime, time.Local)
					if tmpErr != nil {
						err = tmpErr
						return
					}
					//寻找固定日期的数据
					findDateTime, tmpErr := time.ParseInLocation(utils.FormatDate, edbConf.YDate, time.Local)
					if tmpErr != nil {
						err = tmpErr
						return
					}
					for tmpDateTime := findDateTime; tmpDateTime.After(minDateTime) || tmpDateTime.Equal(minDateTime); tmpDateTime = tmpDateTime.AddDate(0, 0, -1) {
						tmpDate := tmpDateTime.Format(utils.FormatDate)
						if _, ok := edbDataMap[edbInfoId][tmpDate]; ok { //如果能找到数据，那么就返回
							findDate = tmpDate
							break
						}
					}
				default:
					err = errors.New(fmt.Sprint("日期类型异常,Type:", edbConf.YDate))
					return
				}
				findDateTime, _ := time.ParseInLocation(utils.FormatDate, findDate, time.Local)
				if maxDate.IsZero() {
					maxDate = findDateTime
				} else {
					if findDateTime.After(maxDate) {
						maxDate = findDateTime
					}
				}
				if tmpValue, ok := edbDataMap[edbInfoId][findDate]; ok {
					tmpItem.YDate = findDate
					tmpItem.YValue = tmpValue
				} else {
					continue
				}
			}

			// 获取当前系列的X轴的最大最小值
			{
				if tmpItem.XValue < minXVal {
					minXVal = tmpItem.XValue
				}
				if tmpItem.XValue > maxXVal {
					maxXVal = tmpItem.XValue
				}
				if tmpItem.YValue < minYVal {
					minYVal = tmpItem.YValue
				}
				if tmpItem.YValue > maxYVal {
					maxYVal = tmpItem.YValue
				}
			}
			tmpSeriesEdbInfoList = append(tmpSeriesEdbInfoList, tmpItem)
		}

		trendLimitData := make([]request.CoordinatePoint, 0) //趋势线的前后坐标点
		var trendLine, rSquare string
		// 生成线性方程式
		var a, b float64
		{
			coordinateData := make([]utils.Coordinate, 0)
			for _, tmpSeriesEdbInfo := range tmpSeriesEdbInfoList {
				tmpCoordinate1 := utils.Coordinate{
					X: tmpSeriesEdbInfo.XValue,
					Y: tmpSeriesEdbInfo.YValue,
				}
				coordinateData = append(coordinateData, tmpCoordinate1)
			}

			// 只有存在两个坐标点的时候,才能去计算线性方程和R平方
			if len(coordinateData) >= 2 {
				a, b = utils.GetLinearResult(coordinateData)
				if !math.IsNaN(a) && !math.IsNaN(b) {
					if b > 0 {
						trendLine = fmt.Sprintf("y=%sx+%s", utils.SubFloatToString(a, 4), utils.SubFloatToString(b, 4))
					} else {
						trendLine = fmt.Sprintf("y=%sx%s", utils.SubFloatToString(a, 4), utils.SubFloatToString(b, 4))
					}
					minYVal, _ = decimal.NewFromFloat(a).Mul(decimal.NewFromFloat(minXVal)).Add(decimal.NewFromFloat(b)).Round(4).Float64()
					maxYVal, _ = decimal.NewFromFloat(a).Mul(decimal.NewFromFloat(maxXVal)).Add(decimal.NewFromFloat(b)).Round(4).Float64()
				}

				// 计算R平方
				rSquare = fmt.Sprint(utils.CalculationDecisive(coordinateData))
			}

			trendLimitData = append(trendLimitData, request.CoordinatePoint{
				X: minXVal,
				Y: minYVal,
			}, request.CoordinatePoint{
				X: maxXVal,
				Y: maxYVal,
			})
		}

		dataListResp = append(dataListResp, request.SectionScatterSeriesItemResp{
			Name:            seriesItem.Name,
			NameEn:          seriesItem.NameEn,
			Color:           seriesItem.Color,
			EdbInfoList:     tmpSeriesEdbInfoList,
			ShowTrendLine:   seriesItem.ShowTrendLine,
			ShowFitEquation: seriesItem.ShowFitEquation,
			ShowRSquare:     seriesItem.ShowRSquare,
			TrendLine:       trendLine,
			RSquare:         rSquare,
			TrendLimitData:  trendLimitData,
		})
	}

	chartDataResp = request.SectionScatterInfoResp{
		XName:       extraConfig.XName,
		XNameEn:     extraConfig.XNameEn,
		XUnitName:   extraConfig.XUnitName,
		XUnitNameEn: extraConfig.XUnitNameEn,
		YName:       extraConfig.YName,
		YNameEn:     extraConfig.YNameEn,
		YUnitName:   extraConfig.YUnitName,
		YUnitNameEn: extraConfig.YUnitNameEn,
		XMinValue:   extraConfig.XMinValue,
		XMaxValue:   extraConfig.XMaxValue,
		YMinValue:   extraConfig.YMinValue,
		YMaxValue:   extraConfig.YMaxValue,
		DataList:    dataListResp,
	}
	return
}

// RadarChartData 雷达图的数据处理
func RadarChartData(mappingList []*chartEdbMappingModel.ChartEdbInfoMapping, edbDataListMap map[int][]*edbDataModel.EdbDataList, extraConfig request.RadarChartInfoReq) (edbIdList []int, chartDataResp chart_info.RadarChartInfoResp, err error) {
	// 指标数据数组（10086:{"2022-12-02":100.01,"2022-12-01":102.3}）
	edbDataMap := make(map[int]map[string]float64)
	for edbInfoId, edbDataList := range edbDataListMap {
		edbDateData := make(map[string]float64)
		for _, edbData := range edbDataList {
			edbDateData[edbData.DataTime] = edbData.Value
		}
		edbDataMap[edbInfoId] = edbDateData
	}
	// edbIdList 指标展示顺序；x轴的指标顺序
	edbIdList = make([]int, 0)
	for _, v := range mappingList {
		edbIdList = append(edbIdList, v.EdbInfoId)
	}
	chartDateList := extraConfig.DateList

	yDataList := make([]chart_info.RadarYData, 0) //y轴的数据列表

	for _, chartDate := range chartDateList {
		var maxDate time.Time

		findDataList := make([]float64, 0) // 当前日期的数据值
		for _, edbInfoId := range edbIdList {
			findDate := chartDate.Date            //需要的日期值
			dataList := edbDataListMap[edbInfoId] //指标的所有数据值
			if len(dataList) <= 0 {
				// 没有数据的指标id
				findDataList = append(findDataList, 0)
				continue
			}
			switch chartDate.Type {
			case 1: //最新值
				findDate = dataList[len(dataList)-1].DataTime
			case 2: //近期几天
				findDateTime, tmpErr := time.ParseInLocation(utils.FormatDate, dataList[len(dataList)-1].DataTime, time.Local)
				if tmpErr != nil {
					err = tmpErr
					return
				}
				findDateTime = findDateTime.AddDate(0, 0, -chartDate.Value)

				lenData := len(dataList) - 1
				for i := lenData; i >= 0; i-- {
					currDateTime, e := time.ParseInLocation(utils.FormatDate, dataList[i].DataTime, time.Local)
					if e != nil {
						err = e
						return
					}
					if currDateTime.Equal(findDateTime) || currDateTime.Before(findDateTime) {
						findDate = dataList[i].DataTime
						break
					}
				}
			case 3: // 固定日期
				//最早的日期
				minDateTime, tmpErr := time.ParseInLocation(utils.FormatDate, dataList[0].DataTime, time.Local)
				if tmpErr != nil {
					err = tmpErr
					return
				}
				//寻找固定日期的数据
				findDateTime, tmpErr := time.ParseInLocation(utils.FormatDate, chartDate.Date, time.Local)
				if tmpErr != nil {
					err = tmpErr
					return
				}
				for tmpDateTime := findDateTime; tmpDateTime.After(minDateTime) || tmpDateTime.Equal(minDateTime); tmpDateTime = tmpDateTime.AddDate(0, 0, -1) {
					tmpDate := tmpDateTime.Format(utils.FormatDate)
					if _, ok := edbDataMap[edbInfoId][tmpDate]; ok { //如果能找到数据，那么就返回
						findDate = tmpDate
						break
					}
				}
			default:
				err = errors.New(fmt.Sprint("日期类型异常,Type:", chartDate.Type))
				return
			}
			findDateTime, _ := time.ParseInLocation(utils.FormatDate, findDate, time.Local)
			if maxDate.IsZero() {
				maxDate = findDateTime
			} else {
				if findDateTime.After(maxDate) {
					maxDate = findDateTime
				}
			}
			if tmpValue, ok := edbDataMap[edbInfoId][findDate]; ok {
				tmpValue, _ = decimal.NewFromFloat(tmpValue).Round(4).Float64()
				findDataList = append(findDataList, tmpValue)
			} else {
				findDataList = append(findDataList, 0)
			}
		}

		yDate := "0000-00-00"
		if !maxDate.IsZero() {
			yDate = maxDate.Format(utils.FormatDate)
		}
		yDataList = append(yDataList, chart_info.RadarYData{
			Date:  yDate,
			Value: findDataList,
			Color: chartDate.Color,
			Name:  chartDate.Name,
		})
	}

	chartDataResp = chart_info.RadarChartInfoResp{
		YDataList:   yDataList,
		XEdbIdValue: edbIdList,
	}
	return
}
// HandleDataByLinearRegressionToList 插值法补充数据(线性方程式)
func HandleDataByLinearRegressionToList(edbInfoDataList []*edbDataModel.EdbDataList, handleDataMap map[string]float64) (dataTimeList []string, valueList []float64, err error) {
	if len(edbInfoDataList) < 2 {
		return
	}

	var startEdbInfoData *edbDataModel.EdbDataList
	for _, v := range edbInfoDataList {
		handleDataMap[v.DataTime] = v.Value
		dataTimeList = append(dataTimeList, v.DataTime)
		// 第一个数据就给过滤了,给后面的试用
		if startEdbInfoData == nil {
			startEdbInfoData = v
			//startEdbInfoData.DataTime = startEdbInfoData.DataTime[:5]+ "01-01"
			continue
		}

		// 获取两条数据之间相差的天数
		startDataTime, _ := time.ParseInLocation(utils.FormatDate, startEdbInfoData.DataTime, time.Local)
		currDataTime, _ := time.ParseInLocation(utils.FormatDate, v.DataTime, time.Local)
		betweenHour := int(currDataTime.Sub(startDataTime).Hours())
		betweenDay := betweenHour / 24

		// 如果相差一天,那么过滤
		if betweenDay <= 1 {
			startEdbInfoData = v
			continue
		}

		// 生成线性方程式
		var a, b float64
		{
			coordinateData := make([]utils.Coordinate, 0)
			tmpCoordinate1 := utils.Coordinate{
				X: 1,
				Y: startEdbInfoData.Value,
			}
			coordinateData = append(coordinateData, tmpCoordinate1)
			tmpCoordinate2 := utils.Coordinate{
				X: float64(betweenDay) + 1,
				Y: v.Value,
			}
			coordinateData = append(coordinateData, tmpCoordinate2)

			a, b = utils.GetLinearResult(coordinateData)
			if math.IsNaN(a) || math.IsNaN(b) {
				err = errors.New("线性方程公式生成失败")
				return
			}
		}

		// 生成对应的值
		{
			for i := 1; i < betweenDay; i++ {
				tmpDataTime := startDataTime.AddDate(0, 0, i)
				aDecimal := decimal.NewFromFloat(a)
				xDecimal := decimal.NewFromInt(int64(i) + 1)
				bDecimal := decimal.NewFromFloat(b)

				val, _ := aDecimal.Mul(xDecimal).Add(bDecimal).Round(4).Float64()
				handleDataMap[tmpDataTime.Format(utils.FormatDate)] = val
				dataTimeList = append(dataTimeList, tmpDataTime.Format(utils.FormatDate))
				valueList = append(valueList, val)
			}
		}

		startEdbInfoData = v
	}

	return
}

// HandleDataByLinearRegressionToList 保证生成365个数据点的线性插值法
func HandleDataByLinearRegressionToListV2(edbInfoDataList []*edbDataModel.EdbDataList, handleDataMap map[string]float64) (dataTimeList []string, valueList []float64, err error) {
	if len(edbInfoDataList) < 2 {
		return
	}

	// 确保至少有两天数据来生成线性方程
	if len(edbInfoDataList) < 2 {
		err = errors.New("至少需要两天的数据来执行线性插值")
		return
	}

	// 对数据按日期排序，确保顺序正确
	sort.Slice(edbInfoDataList, func(i, j int) bool {
		t1, _ := time.ParseInLocation(utils.FormatDate, edbInfoDataList[i].DataTime, time.Local)
		t2, _ := time.ParseInLocation(utils.FormatDate, edbInfoDataList[j].DataTime, time.Local)
		return t1.Before(t2)
	})

	startEdbInfoData := edbInfoDataList[0]
	endEdbInfoData := edbInfoDataList[len(edbInfoDataList)-1]

	// 计算起始和结束日期间实际的天数
	startDate, _ := time.ParseInLocation(utils.FormatDate, startEdbInfoData.DataTime, time.Local)
	endDate, _ := time.ParseInLocation(utils.FormatDate, endEdbInfoData.DataTime, time.Local)
	actualDays := endDate.Sub(startDate).Hours() / 24

	// 生成365个数据点，首先处理已有数据
	for _, v := range edbInfoDataList {
		handleDataMap[v.DataTime] = v.Value
		dataTimeList = append(dataTimeList, v.DataTime)
		valueList = append(valueList, v.Value)
	}

	// 如果已有数据跨越天数不足365天，则对缺失的日期进行线性插值
	if actualDays < 365 {
		// 使用已有数据点生成线性方程（这里简化处理，实际可能需更细致处理边界情况）
		var a, b float64
		coordinateData := []utils.Coordinate{
			{X: 1, Y: startEdbInfoData.Value},
			{X: float64(len(edbInfoDataList)), Y: endEdbInfoData.Value},
		}
		a, b = utils.GetLinearResult(coordinateData)
		if math.IsNaN(a) || math.IsNaN(b) {
			err = errors.New("线性方程公式生成失败")
			return
		}

		// 对剩余日期进行插值
		for i := 1; i < 365; i++ {
			day := startDate.AddDate(0, 0, i)
			if _, exists := handleDataMap[day.Format(utils.FormatDate)]; !exists {
				aDecimal := decimal.NewFromFloat(a)
				xDecimal := decimal.NewFromInt(int64(i) + 1)
				bDecimal := decimal.NewFromFloat(b)

				val, _ := aDecimal.Mul(xDecimal).Add(bDecimal).Round(4).Float64()
				handleDataMap[day.Format(utils.FormatDate)] = val
				dataTimeList = append(dataTimeList, day.Format(utils.FormatDate))
				valueList = append(valueList, val)
			}
		}
	}

	return
}

// HandleDataByLinearRegressionToListV3 插值法补充数据(线性方程式)-直接补充指标起始日期间的所有数据
func HandleDataByLinearRegressionToListV3(edbInfoDataList []*edbDataModel.EdbDataList, handleDataMap map[string]float64) (newEdbInfoDataList []*edbDataModel.EdbDataList, dataTimeList []string, valueList []float64, err error) {
	if len(edbInfoDataList) < 2 {
		return
	}

	var startEdbInfoData *edbDataModel.EdbDataList
	for _, v := range edbInfoDataList {
		handleDataMap[v.DataTime] = v.Value
		newEdbInfoDataList = append(newEdbInfoDataList, v)
		dataTimeList = append(dataTimeList, v.DataTime)
		// 第一个数据就给过滤了,给后面的试用
		if startEdbInfoData == nil {
			startEdbInfoData = v
			//startEdbInfoData.DataTime = startEdbInfoData.DataTime[:5]+ "01-01"
			continue
		}

		// 获取两条数据之间相差的天数
		startDataTime, _ := time.ParseInLocation(utils.FormatDate, startEdbInfoData.DataTime, time.Local)
		currDataTime, _ := time.ParseInLocation(utils.FormatDate, v.DataTime, time.Local)
		betweenHour := int(currDataTime.Sub(startDataTime).Hours())
		betweenDay := betweenHour / 24

		// 如果相差一天,那么过滤
		if betweenDay <= 1 {
			startEdbInfoData = v
			continue
		}

		// 生成线性方程式
		var a, b float64
		{
			coordinateData := make([]utils.Coordinate, 0)
			tmpCoordinate1 := utils.Coordinate{
				X: 1,
				Y: startEdbInfoData.Value,
			}
			coordinateData = append(coordinateData, tmpCoordinate1)
			tmpCoordinate2 := utils.Coordinate{
				X: float64(betweenDay) + 1,
				Y: v.Value,
			}
			coordinateData = append(coordinateData, tmpCoordinate2)

			a, b = utils.GetLinearResult(coordinateData)
			if math.IsNaN(a) || math.IsNaN(b) {
				err = errors.New("线性方程公式生成失败")
				return
			}
		}

		// 生成对应的值
		{
			for i := 1; i < betweenDay; i++ {
				tmpDataTime := startDataTime.AddDate(0, 0, i)
				aDecimal := decimal.NewFromFloat(a)
				xDecimal := decimal.NewFromInt(int64(i) + 1)
				bDecimal := decimal.NewFromFloat(b)

				val, _ := aDecimal.Mul(xDecimal).Add(bDecimal).Round(4).Float64()
				handleDataMap[tmpDataTime.Format(utils.FormatDate)] = val
				dataTimeList = append(dataTimeList, tmpDataTime.Format(utils.FormatDate))
				valueList = append(valueList, val)
				newEdbInfoDataList = append(newEdbInfoDataList, &edbDataModel.EdbDataList{
					EdbDataId:     v.EdbDataId,
					EdbInfoId:     v.EdbInfoId,
					DataTime:      tmpDataTime.Format(utils.FormatDate),
					DataTimestamp: tmpDataTime.UnixNano() / 1e6,
					Value:         val,
				})
			}
		}

		startEdbInfoData = v
	}

	return
}

// GetEdbDataTbzForSeason 获取指标的同比值数据
func GetEdbDataTbzForSeason(frequency string, tmpDataList []*edbDataModel.EdbDataList, startDateTime time.Time) (dataList []*edbDataModel.EdbDataList, minValue, maxValue float64, err error) {
	dataList = make([]*edbDataModel.EdbDataList, 0)

	// 数据处理
	var dateArr []string
	dataMap := make(map[string]*edbDataModel.EdbDataList)
	for _, v := range tmpDataList {
		dateArr = append(dateArr, v.DataTime)
		dataMap[v.DataTime] = v
	}
	for _, av := range dateArr {
		currentItem, ok := dataMap[av]
		// 如果找不到当前日期的数据,那么终止当前循环,进入下一循环
		if !ok {
			continue
		}
		tmpItem := *currentItem
		var isOk bool //是否计算出来结果

		//当前日期
		currentDate, tmpErr := time.ParseInLocation(utils.FormatDate, av, time.Local)
		if tmpErr != nil {
			err = tmpErr
			return
		}
		// 如果存在开始日期,同时,当前日期早于开始日期,那么终止当前循环,进入下一循环
		if !startDateTime.IsZero() && currentDate.Before(startDateTime) {
			continue
		}
		//上一年的日期
		preDate := currentDate.AddDate(-1, 0, 0)
		preDateStr := preDate.Format(utils.FormatDate)
		if findItem, ok := dataMap[preDateStr]; ok { //上一年同期找到
			tmpItem.Value = TbzDiv(currentItem.Value, findItem.Value)
			isOk = true
		} else {
			if frequency == "月度" { //向上和向下，各找一个月
				for i := 0; i <= 35; i++ {
					nextDateDay := preDate.AddDate(0, 0, i)
					nextDateDayStr := nextDateDay.Format(utils.FormatDate)
					if findItem, ok := dataMap[nextDateDayStr]; ok { //上一年同期->下一个月找到
						tmpItem.Value = TbzDiv(currentItem.Value, findItem.Value)
						isOk = true
						break
					} else {
						preDateDay := preDate.AddDate(0, 0, -i)
						preDateDayStr := preDateDay.Format(utils.FormatDate)
						if findItem, ok := dataMap[preDateDayStr]; ok { //上一年同期->上一个月找到
							tmpItem.Value = TbzDiv(currentItem.Value, findItem.Value)
							isOk = true
							break
						}
					}
				}
			} else if frequency == "季度" || frequency == "年度" {
				if findItem, ok := dataMap[preDateStr]; ok { //上一年同期->下一个月找到
					tmpItem.Value = TbzDiv(currentItem.Value, findItem.Value)
					isOk = true
					break
				}
			} else {
				nextDateDay := preDate.AddDate(0, 0, 1)
				nextDateDayStr := nextDateDay.Format(utils.FormatDate)

				preDateDay := preDate.AddDate(0, 0, -1)
				preDateDayStr := preDateDay.Format(utils.FormatDate)

				for i := 0; i < 35; i++ {
					if i >= 1 {
						nextDateDay = nextDateDay.AddDate(0, 0, i)
						nextDateDayStr = nextDateDay.Format(utils.FormatDate)
					}
					if findItem, ok := dataMap[nextDateDayStr]; ok { //上一年同期->下一个月找到
						tmpItem.Value = TbzDiv(currentItem.Value, findItem.Value)
						isOk = true
						break
					} else {
						if i >= 1 {
							preDateDay = preDate.AddDate(0, 0, -i)
							preDateDayStr = nextDateDay.Format(utils.FormatDate)
						}
						if findItem, ok := dataMap[preDateDayStr]; ok { //上一年同期->上一个月找到
							tmpItem.Value = TbzDiv(currentItem.Value, findItem.Value)
							isOk = true
							break
						}
					}
				}
			}
		}

		if isOk {
			if tmpItem.Value > maxValue {
				maxValue = tmpItem.Value
			}
			if tmpItem.Value < minValue {
				minValue = tmpItem.Value
			}
			dataList = append(dataList, &tmpItem)
		}
	}

	return
}
