package data

import (
	"encoding/json"
	"errors"
	"eta/eta_chart_lib/models"
	"eta/eta_chart_lib/models/data_manage"
	"eta/eta_chart_lib/utils"
	"github.com/shopspring/decimal"
	"strconv"
	"time"
)

// GetPredictEdbDayList 获取预测指标日期列表
func getPredictEdbDayList(startDate, endDate time.Time, frequency, dataDateType string) (dayList []time.Time) {
	if dataDateType == `` {
		dataDateType = `交易日`
	}
	switch frequency {
	case "日度":
		for currDate := startDate.AddDate(0, 0, 1); currDate.Before(endDate) || currDate.Equal(endDate); currDate = currDate.AddDate(0, 0, 1) {
			// 如果日期类型是交易日的时候,那么需要将周六、日排除
			if dataDateType == `交易日` && (currDate.Weekday() == time.Sunday || currDate.Weekday() == time.Saturday) {
				continue
			}
			dayList = append(dayList, currDate)
		}
	case "周度":
		//nextDate := startDate.AddDate(0, 0, 7)
		for currDate := startDate.AddDate(0, 0, 7); currDate.Before(endDate) || currDate.Equal(endDate); currDate = currDate.AddDate(0, 0, 7) {
			dayList = append(dayList, currDate)
		}
	case "旬度":
		for currDate := startDate.AddDate(0, 0, 1); currDate.Before(endDate) || currDate.Equal(endDate); {
			nextDate := currDate.AddDate(0, 0, 1)
			//每个月的10号、20号、最后一天,那么就写入
			if nextDate.Day() == 11 || nextDate.Day() == 21 || nextDate.Day() == 1 {
				dayList = append(dayList, currDate)
			}
			currDate = nextDate
		}
	case "月度":
		for currDate := startDate; currDate.Before(endDate) || currDate.Equal(endDate); {
			currDate = time.Date(currDate.Year(), currDate.Month(), 1, 0, 0, 0, 0, time.Now().Location()).AddDate(0, 1, -1)
			if !currDate.After(endDate) && !currDate.Equal(startDate) {
				dayList = append(dayList, currDate)
			}
			currDate = currDate.AddDate(0, 0, 1)
		}
	case "季度":
		for currDate := startDate; currDate.Before(endDate) || currDate.Equal(endDate); {
			// 每月的最后一天
			currDate = time.Date(currDate.Year(), currDate.Month(), 1, 0, 0, 0, 0, time.Now().Location()).AddDate(0, 1, -1)
			if !currDate.After(endDate) && !currDate.Equal(startDate) {
				// 季度日期就写入,否则不写入
				if currDate.Month() == 3 || currDate.Month() == 6 || currDate.Month() == 9 || currDate.Month() == 12 {
					dayList = append(dayList, currDate)
				}
			}
			currDate = currDate.AddDate(0, 0, 1)
		}
	case "半年度":
		for currDate := startDate; currDate.Before(endDate) || currDate.Equal(endDate); {
			// 每月的最后一天
			currDate = time.Date(currDate.Year(), currDate.Month(), 1, 0, 0, 0, 0, time.Now().Location()).AddDate(0, 1, -1)
			if !currDate.After(endDate) && !currDate.Equal(startDate) {
				// 半年度日期就写入,否则不写入
				if currDate.Month() == 6 || currDate.Month() == 12 {
					dayList = append(dayList, currDate)
				}
			}
			currDate = currDate.AddDate(0, 0, 1)
		}
	case "年度":
		for currDate := startDate; currDate.Before(endDate) || currDate.Equal(endDate); {
			currDate = time.Date(currDate.Year()+1, 12, 31, 0, 0, 0, 0, time.Now().Location())
			if !currDate.After(endDate) && !currDate.Equal(startDate) {
				dayList = append(dayList, currDate)
			}
		}
	}
	return
}

// GetPredictDataListByPredictEdbInfoId 根据预测指标id获取预测指标的数据(日期正序返回)
func GetPredictDataListByPredictEdbInfoId(edbInfoId int, startDate, endDate string, isTimeBetween bool) (edbInfo *data_manage.EdbInfo, dataList []*models.EdbDataList, sourceEdbInfoItem *data_manage.EdbInfo, predictEdbConf *data_manage.PredictEdbConf, err error, errMsg string) {
	edbInfo, err = data_manage.GetEdbInfoById(edbInfoId)
	if err != nil {
		errMsg = `获取预测指标信息失败`
		return
	}
	dataList, sourceEdbInfoItem, predictEdbConf, err, errMsg = GetPredictDataListByPredictEdbInfo(edbInfo, startDate, endDate, isTimeBetween)
	return
}

// GetPredictDataListByPredictEdbInfo 根据预测指标信息获取预测指标的数据
func GetPredictDataListByPredictEdbInfo(edbInfo *data_manage.EdbInfo, startDate, endDate string, isTimeBetween bool) (dataList []*models.EdbDataList, sourceEdbInfoItem *data_manage.EdbInfo, predictEdbConf *data_manage.PredictEdbConf, err error, errMsg string) {
	// 非计算指标,直接从表里获取数据
	if !isTimeBetween {
		endDate = ``
	}
	return GetPredictCalculateDataListByPredictEdbInfo(edbInfo, startDate, endDate)
}

// GetPredictCalculateDataListByPredictEdbInfo 根据预测运算指标信息获取预测指标的数据
func GetPredictCalculateDataListByPredictEdbInfo(edbInfo *data_manage.EdbInfo, startDate, endDate string) (dataList []*models.EdbDataList, sourceEdbInfoItem *data_manage.EdbInfo, predictEdbConf *data_manage.PredictEdbConf, err error, errMsg string) {
	dataList, err = models.GetEdbDataList(edbInfo.Source, edbInfo.SubSource, edbInfo.EdbInfoId, startDate, endDate)
	return
}

// GetChartPredictEdbInfoDataListByConfList 获取图表的预测指标的未来数据
func GetChartPredictEdbInfoDataListByConfList(predictEdbConfList []*data_manage.PredictEdbConf, filtrateStartDateStr, latestDateStr, endDateStr, frequency, dataDateType string, realPredictEdbInfoData []*models.EdbDataList) (predictEdbInfoData []*models.EdbDataList, minValue, maxValue float64, err error) {
	endDate, err := time.ParseInLocation(utils.FormatDate, endDateStr, time.Local)
	if err != nil {
		return
	}

	latestDate, err := time.ParseInLocation(utils.FormatDate, latestDateStr, time.Local)
	if err != nil {
		return
	}

	// 开始预测数据的时间
	startDate := latestDate

	// 如果有筛选时间的话
	if filtrateStartDateStr != `` {
		filtrateStartDate, tmpErr := time.ParseInLocation(utils.FormatDate, filtrateStartDateStr, time.Local)
		if tmpErr != nil {
			err = tmpErr
			return
		}
		//如果筛选时间晚于实际数据时间,那么就以筛选时间作为获取预测数据的时间
		if filtrateStartDate.After(latestDate) {
			startDate = filtrateStartDate.AddDate(0, 0, -1)
		}
	}

	//var dateArr []string
	// 对应日期的值
	existMap := make(map[string]float64)
	for _, v := range realPredictEdbInfoData {
		//dateArr = append(dateArr, v.DataTime)
		existMap[v.DataTime] = v.Value
	}

	predictEdbInfoData = make([]*models.EdbDataList, 0)
	//dataValue := lastDataValue
	//预测规则,1:最新,2:固定值,3:同比,4:同差,5:环比,6:环差,7:N期移动均值,8:N期段线性外推值

	for _, predictEdbConf := range predictEdbConfList {
		dataEndTime := endDate
		if predictEdbConf.EndDate.Before(dataEndTime) {
			dataEndTime = predictEdbConf.EndDate
		}

		var tmpMinValue, tmpMaxValue float64 // 当前预测结果中的最大/最小值

		dayList := getPredictEdbDayList(startDate, dataEndTime, frequency, dataDateType)
		if len(dayList) <= 0 { // 如果未来没有日期的话,那么就退出当前循环,进入下一个循环
			continue
		}

		switch predictEdbConf.RuleType {
		case 1: //1:最新
			var lastDataValue float64 //最新值
			tmpAllData := make([]*models.EdbDataList, 0)
			tmpAllData = append(tmpAllData, realPredictEdbInfoData...)
			tmpAllData = append(tmpAllData, predictEdbInfoData...)
			lenTmpAllData := len(tmpAllData)
			if lenTmpAllData > 0 {
				lastDataValue = tmpAllData[lenTmpAllData-1].Value
			}
			predictEdbInfoData = GetChartPredictEdbInfoDataListByRule1(predictEdbConf.PredictEdbInfoId, lastDataValue, dayList, predictEdbInfoData, existMap)
			tmpMaxValue = lastDataValue
			tmpMinValue = lastDataValue
		case 2: //2:固定值
			tmpValDecimal, tmpErr := decimal.NewFromString(predictEdbConf.Value)
			if tmpErr != nil {
				err = tmpErr
				return
			}
			dataValue, _ := tmpValDecimal.Float64()
			predictEdbInfoData = GetChartPredictEdbInfoDataListByRule1(predictEdbConf.PredictEdbInfoId, dataValue, dayList, predictEdbInfoData, existMap)

			tmpMaxValue = dataValue
			tmpMinValue = dataValue
		case 3: //3:同比
			tmpValDecimal, tmpErr := decimal.NewFromString(predictEdbConf.Value)
			if tmpErr != nil {
				err = tmpErr
				return
			}
			tbValue, _ := tmpValDecimal.Float64()
			predictEdbInfoData, tmpMinValue, tmpMaxValue = GetChartPredictEdbInfoDataListByRuleTb(predictEdbConf.PredictEdbInfoId, tbValue, dayList, frequency, realPredictEdbInfoData, predictEdbInfoData, existMap)
		case 4: //4:同差
			tmpValDecimal, tmpErr := decimal.NewFromString(predictEdbConf.Value)
			if tmpErr != nil {
				err = tmpErr
				return
			}
			tcValue, _ := tmpValDecimal.Float64()
			predictEdbInfoData, tmpMinValue, tmpMaxValue = GetChartPredictEdbInfoDataListByRuleTc(predictEdbConf.PredictEdbInfoId, tcValue, dayList, frequency, realPredictEdbInfoData, predictEdbInfoData, existMap)
		case 5: //5:环比
			tmpValDecimal, tmpErr := decimal.NewFromString(predictEdbConf.Value)
			if tmpErr != nil {
				err = tmpErr
				return
			}
			hbValue, _ := tmpValDecimal.Float64()
			predictEdbInfoData, tmpMinValue, tmpMaxValue = GetChartPredictEdbInfoDataListByRuleHb(predictEdbConf.PredictEdbInfoId, hbValue, dayList, realPredictEdbInfoData, predictEdbInfoData, existMap)
		case 6: //6:环差
			tmpValDecimal, tmpErr := decimal.NewFromString(predictEdbConf.Value)
			if tmpErr != nil {
				err = tmpErr
				return
			}
			hcValue, _ := tmpValDecimal.Float64()
			predictEdbInfoData, tmpMinValue, tmpMaxValue = GetChartPredictEdbInfoDataListByRuleHc(predictEdbConf.PredictEdbInfoId, hcValue, dayList, realPredictEdbInfoData, predictEdbInfoData, existMap)
		case 7: //7:N期移动均值
			nValue, tmpErr := strconv.Atoi(predictEdbConf.Value)
			if tmpErr != nil {
				err = tmpErr
				return
			}
			predictEdbInfoData, tmpMinValue, tmpMaxValue = GetChartPredictEdbInfoDataListByRuleNMoveMeanValue(predictEdbConf.PredictEdbInfoId, nValue, dayList, realPredictEdbInfoData, predictEdbInfoData, existMap)
		case 8: //8:N期段线性外推值
			nValue, tmpErr := strconv.Atoi(predictEdbConf.Value)
			if tmpErr != nil {
				err = tmpErr
				return
			}
			predictEdbInfoData, tmpMinValue, tmpMaxValue = GetChartPredictEdbInfoDataListByRuleNLinearRegression(predictEdbConf.PredictEdbInfoId, nValue, dayList, realPredictEdbInfoData, predictEdbInfoData, existMap)
		case 9: //9:动态环差”预测规则;
			predictEdbInfoData, tmpMinValue, tmpMaxValue = GetChartPredictEdbInfoDataListByRuleTrendsHC(predictEdbConf.PredictEdbInfoId, predictEdbConf.ConfigId, startDate, dataEndTime, dayList, realPredictEdbInfoData, predictEdbInfoData, existMap)
		case 10: //10:根据 给定终值后插值 规则获取预测数据
			tmpValDecimal, tmpErr := decimal.NewFromString(predictEdbConf.Value)
			if tmpErr != nil {
				err = tmpErr
				return
			}
			finalValue, _ := tmpValDecimal.Float64()
			predictEdbInfoData, tmpMinValue, tmpMaxValue = GetChartPredictEdbInfoDataListByRuleFinalValueHc(predictEdbConf.PredictEdbInfoId, finalValue, dayList, realPredictEdbInfoData, predictEdbInfoData, existMap)
		case 11: //11:根据 季节性 规则获取预测数据
			var seasonConf SeasonConf
			tmpErr := json.Unmarshal([]byte(predictEdbConf.Value), &seasonConf)
			if tmpErr != nil {
				err = errors.New("季节性配置信息异常:" + tmpErr.Error())
				return
			}
			calendar := "公历"
			if seasonConf.Calendar == "农历" {
				calendar = "农历"
			}
			yearList := make([]int, 0)
			//选择方式,1:连续N年;2:指定年份
			if seasonConf.YearType == 1 {
				if seasonConf.NValue < 1 {
					err = errors.New("连续N年不允许小于1")
					return
				}

				currYear := time.Now().Year()
				for i := 0; i < seasonConf.NValue; i++ {
					yearList = append(yearList, currYear-i-1)
				}
			} else {
				yearList = seasonConf.YearList
			}
			predictEdbInfoData, tmpMinValue, tmpMaxValue, err = GetChartPredictEdbInfoDataListByRuleSeason(predictEdbConf.PredictEdbInfoId, yearList, calendar, dayList, realPredictEdbInfoData, predictEdbInfoData, existMap)
			if err != nil {
				return
			}
		case 12: //12:根据 移动平均同比 规则获取预测数据
			var moveAverageConf MoveAverageConf
			tmpErr := json.Unmarshal([]byte(predictEdbConf.Value), &moveAverageConf)
			if tmpErr != nil {
				err = errors.New("季节性配置信息异常:" + tmpErr.Error())
				return
			}
			predictEdbInfoData, tmpMinValue, tmpMaxValue, err = GetChartPredictEdbInfoDataListByRuleMoveAverageTb(predictEdbConf.PredictEdbInfoId, moveAverageConf.NValue, moveAverageConf.Year, dayList, realPredictEdbInfoData, predictEdbInfoData, existMap)
			if err != nil {
				return
			}
		case 13: //13:根据 同比增速差值 规则获取预测数据
			tmpValDecimal, tmpErr := decimal.NewFromString(predictEdbConf.Value)
			if tmpErr != nil {
				err = tmpErr
				return
			}
			tbEndValue, _ := tmpValDecimal.Float64()
			predictEdbInfoData, tmpMinValue, tmpMaxValue = GetChartPredictEdbInfoDataListByRuleTbzscz(predictEdbConf.PredictEdbInfoId, tbEndValue, dayList, frequency, realPredictEdbInfoData, predictEdbInfoData, existMap)
		case 14: //14:根据 一元线性拟合 规则获取预测数据
			var ruleConf RuleLineNhConf
			err = json.Unmarshal([]byte(predictEdbConf.Value), &ruleConf)
			if err != nil {
				err = errors.New("一元线性拟合配置信息异常:" + err.Error())
				return
			}

			// 规则计算的拟合残差值map
			newNhccDataMap := make(map[string]float64)
			if predictEdbConf.PredictEdbInfoId > 0 { //已经生成的动态数据
				tmpPredictEdbRuleDataList, tmpErr := data_manage.GetPredictEdbRuleDataList(predictEdbConf.PredictEdbInfoId, predictEdbConf.ConfigId, "", "")
				if tmpErr != nil {
					err = tmpErr
					return
				}
				for _, v := range tmpPredictEdbRuleDataList {
					newNhccDataMap[v.DataTime] = v.Value
				}
			} else { //未生成的动态数据,需要使用外部传入的数据进行计算
				newNhccDataMap, err = getCalculateNhccData(append(realPredictEdbInfoData, predictEdbInfoData...), ruleConf)
				if err != nil {
					return
				}
			}

			predictEdbInfoData, tmpMinValue, tmpMaxValue, err = GetChartPredictEdbInfoDataListByRuleLineNh(predictEdbConf.PredictEdbInfoId, dayList, realPredictEdbInfoData, predictEdbInfoData, newNhccDataMap, existMap)
			if err != nil {
				return
			}

		case 15: //15:N年均值:过去N年同期均值。过去N年可以连续或者不连续,指标数据均用线性插值补全为日度数据后计算;
			predictEdbInfoData, tmpMinValue, tmpMaxValue, err = GetChartPredictEdbInfoDataListByRuleNAnnualAverage(predictEdbConf.PredictEdbInfoId, predictEdbConf.Value, dayList, realPredictEdbInfoData, predictEdbInfoData, existMap)
			if err != nil {
				return
			}

		case 16: //16:年度值倒推
			predictEdbInfoData, tmpMinValue, tmpMaxValue, err = GetChartPredictEdbInfoDataListByRuleAnnualValueInversion(predictEdbConf.PredictEdbInfoId, predictEdbConf.Value, dayList, frequency, realPredictEdbInfoData, predictEdbInfoData, existMap)
			if err != nil {
				return
			}
		}

		// 下一个规则的开始日期
		{
			lenPredictEdbInfoData := len(predictEdbInfoData)
			if lenPredictEdbInfoData > 0 {
				tmpDataEndTime, _ := time.ParseInLocation(utils.FormatDate, predictEdbInfoData[lenPredictEdbInfoData-1].DataTime, time.Local)
				if startDate.Before(tmpDataEndTime) {
					startDate = tmpDataEndTime
				}
			}
		}

		if tmpMinValue < minValue {
			minValue = tmpMinValue
		}
		if tmpMaxValue < maxValue {
			maxValue = tmpMaxValue
		}
	}

	return
}