package chart

import (
	"errors"
	"fmt"
	"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"
	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"
	"sort"
	"strconv"
	"strings"
	"time"
)

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

	// 更新图表信息
	if chartType == 1 {
		sql := ` UPDATE chart_info
			SET
            	edb_info_ids = ?,
				modify_time = NOW(),
            	date_type = ?,
				start_date = ?,
				end_date = ?,
 				left_min = ?,
				left_max = ?,
				right_min = ?,
				right_max = ?
			WHERE chart_info_id = ?`
		err = tx.Exec(sql, edbInfoIdStr, req.DateType, req.StartDate, req.EndDate, req.LeftMin, req.LeftMax, req.RightMin, req.RightMax, req.ChartInfoId).Error
	} else {
		sql := ` UPDATE chart_info
			SET
            	edb_info_ids = ?,
				modify_time = NOW(),
				calendar = ?,
				season_start_date = ?,
				season_end_date = ?,
				left_min = ?,
				left_max = ?,
				right_min = ?,
				right_max = ?
			WHERE chart_info_id = ?`
		err = tx.Exec(sql, edbInfoIdStr, req.Calendar, req.SeasonStartDate, req.SeasonEndDate, req.LeftMin, req.LeftMax, req.RightMin, req.RightMax, req.ChartInfoId).Error
	}
	if err != nil {
		fmt.Println("UPDATE chart_info Err:", err.Error())
		return
	}

	var edbInfoIdArr []string
	mapList := make([]*chartEdbMappingModel.ChartEdbMapping, 0)
	for _, v := range req.ChartEdbInfoList {
		edbInfoIdArr = append(edbInfoIdArr, strconv.Itoa(v.EdbInfoId))
		var count int
		csql := ` SELECT COUNT(1) AS count FROM chart_edb_mapping WHERE chart_info_id = ? AND edb_info_id = ? `
		err = tx.Raw(csql, req.ChartInfoId, v.EdbInfoId).Scan(&count).Error
		if err != nil {
			fmt.Println("QueryRow Err:", err.Error())
			return err
		}
		if count > 0 {
			// 更新指标
			msql := `UPDATE chart_edb_mapping
				SET
                 	modify_time = NOW(),
				 	max_data = ?,
				 	min_data = ?,
				 	is_order = ?,
				 	is_axis = ?,
				 	edb_info_type = ?,
				 	lead_value = ?,
				 	lead_unit = ?,
				 	chart_style = ?,
				 	chart_color = ?,
				 	chart_width = ? 
				WHERE chart_info_id =? AND edb_info_id=? `

			err = tx.Exec(msql, v.MaxData, v.MinData, v.IsOrder, v.IsAxis, v.EdbInfoType, v.LeadValue, v.LeadUnit, v.ChartStyle, v.ChartColor, v.ChartWidth, req.ChartInfoId, v.EdbInfoId).Error
			if err != nil {
				fmt.Println("chart_edb_mapping Err:" + err.Error())
				return err
			}
		} else {
			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
			mapList = append(mapList, mapItem)
		}
	}

	// 批量新增指标
	if len(mapList) > 0 {
		err = tx.Create(mapList).Error
		if err != nil {
			fmt.Println("AddChartEdbMapping Err:" + err.Error())
			return err
		}
	}

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

	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
		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, 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
}

// GetChartEdbData 获取图表的指标数据
func GetChartEdbData(chartInfoId, chartType int, calendar, startDate, endDate string, mappingList []*chartEdbMappingModel.ChartEdbInfoMapping, barChartInfoDateList []request.BarChartInfoDateReq, barChartInfoSort request.BarChartInfoSortReq) (edbList []*chartEdbMappingModel.ChartEdbInfoMappingList, xEdbIdValue []int, yDataList []chart_info.YData, sourceArr []string, err error) {
	edbList = make([]*chartEdbMappingModel.ChartEdbInfoMappingList, 0)
	// 指标对应的所有数据
	edbDataListMap := make(map[int][]*edbDataModel.EdbDataList)
	sourceArr = make([]string, 0)

	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

		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.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

		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
				}
			} 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.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
		}
		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 {
			latestDateStr := v.LatestDate.Format(utils.FormatDate) //实际数据的截止日期
			latestDate := v.LatestDate
			//latestDate, tmpErr := time.Parse(utils.FormatDate, v.LatestDate)
			//if tmpErr != nil {
			//	err = errors.New(fmt.Sprint("获取最后实际数据的日期失败,Err:" + tmpErr.Error() + ";LatestDate:" + v.LatestDate))
			//	return
			//}
			latestDateYear := latestDate.Year() //实际数据截止年份

			if calendar == "农历" {
				if len(dataList) <= 0 {
					result := new(edbDataModel.EdbDataResult)
					item.DataList = result
				} else {
					result, tmpErr := edbDataService.AddCalculateQuarter(dataList)
					if tmpErr != nil {
						err = errors.New("获取农历数据失败,Err:" + tmpErr.Error())
						return
					}

					// 处理季节图的截止日期
					for k, edbDataItems := range result.List {
						var cuttingDataTimestamp int64

						// 切割的日期时间字符串
						cuttingDataTimeStr := latestDate.AddDate(0, 0, edbDataItems.BetweenDay).Format(utils.FormatDate)
						//如果等于最后的实际日期，那么遍历找到该日期对应的时间戳，并将其赋值为 切割时间戳
						if edbDataItems.Year >= latestDateYear {
							for _, tmpData := range edbDataItems.Items {
								if tmpData.DataTime == cuttingDataTimeStr {
									cuttingDataTimestamp = tmpData.DataTimestamp
									break
								}
							}
						}
						edbDataItems.CuttingDataTimestamp = cuttingDataTimestamp
						result.List[k] = edbDataItems
					}

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

						newResult := new(edbDataModel.EdbDataResult)
						newResult.List = append(newResult.List, items)
						newResult.List = append(newResult.List, result.List...)
						item.DataList = newResult
					} else {
						item.DataList = result
					}
				}

			} else {
				currentYear := time.Now().Year()

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

				for _, v := range dataList {
					itemDate, tmpErr := time.Parse(utils.FormatDate, v.DataTime)
					if tmpErr != nil {
						err = errors.New("季度指标日期转换,Err:" + tmpErr.Error() + ";DataTime:" + v.DataTime)
						return
					}
					year := itemDate.Year()
					newItemDate := itemDate.AddDate(currentYear-year, 0, 0)
					timestamp := newItemDate.UnixNano() / 1e6
					v.DataTimestamp = timestamp
					if findVal, ok := quarterMap[year]; !ok {
						quarterArr = append(quarterArr, year)
						findVal = append(findVal, v)
						quarterMap[year] = findVal
					} else {
						findVal = append(findVal, v)
						quarterMap[year] = findVal
					}
				}
				for _, v := range quarterArr {
					itemList := quarterMap[v]
					quarterItem := new(edbDataModel.QuarterData)
					quarterItem.Year = v
					quarterItem.DataList = itemList

					//如果等于最后的实际日期，那么将切割时间戳记录
					if v == latestDateYear {
						var cuttingDataTimestamp int64
						for _, tmpData := range itemList {
							if tmpData.DataTime == latestDateStr {
								cuttingDataTimestamp = tmpData.DataTimestamp
								break
							}
						}
						quarterItem.CuttingDataTimestamp = cuttingDataTimestamp
					} else if v > latestDateYear {
						//如果大于最后的实际日期，那么第一个点就是切割的时间戳
						if len(itemList) > 0 {
							quarterItem.CuttingDataTimestamp = itemList[0].DataTimestamp - 100
						}
					}
					quarterDataList = append(quarterDataList, quarterItem)
				}
				item.DataList = quarterDataList
			}
		} else if chartType == 7 { //柱方图
			//item.DataList = dataList
		} else {
			item.DataList = dataList
		}
		edbList = append(edbList, item)
	}

	// 柱方图
	if chartType == 7 {
		xEdbIdValue, yDataList, err = BarChartData(mappingList, edbDataListMap, barChartInfoDateList, barChartInfoSort)
	} else {
		xEdbIdValue = make([]int, 0)
		yDataList = make([]chart_info.YData, 0)
	}

	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
}
