package models

import (
	"errors"
	"eta/eta_index_lib/utils"
	"fmt"
	"github.com/beego/beego/v2/client/orm"
	"github.com/shopspring/decimal"
	"strconv"
	"strings"
	"time"
)

// SavePredictCalculateJp 预测降频值
func SavePredictCalculateJp(reqEdbInfoId, classifyId int, edbName, frequency, unit, formula string, fromEdbInfo *EdbInfo, edbCode, uniqueCode string, sysUserId int, sysUserRealName, lang string) (edbInfo *EdbInfo, latestDateStr string, latestValue float64, err error, errMsg string) {
	o := orm.NewOrm()
	to, err := o.Begin()
	if err != nil {
		return
	}
	defer func() {
		if err != nil {
			fmt.Println("SavePredictCalculateJp,Err:" + err.Error())
			_ = to.Rollback()
		} else {
			_ = to.Commit()
		}
	}()
	fmt.Println("reqEdbInfoId:", reqEdbInfoId)

	if reqEdbInfoId <= 0 {
		edbInfo = &EdbInfo{
			//EdbInfoId:        0,
			EdbInfoType:   1,
			SourceName:    utils.DATA_SOURCE_NAME_PREDICT_CALCULATE_JP,
			Source:        utils.DATA_SOURCE_PREDICT_CALCULATE_JP,
			EdbCode:       edbCode,
			EdbName:       edbName,
			EdbNameSource: edbName,
			Frequency:     frequency,
			Unit:          unit,
			//StartDate:        "",
			//EndDate:          "",
			ClassifyId:       classifyId,
			SysUserId:        sysUserId,
			SysUserRealName:  sysUserRealName,
			UniqueCode:       uniqueCode,
			CreateTime:       time.Now(),
			ModifyTime:       time.Now(),
			MinValue:         0,
			MaxValue:         0,
			CalculateFormula: formula,
			EdbType:          2,
			Sort:             0,
			MoveType:         0,
			MoveFrequency:    "",
			NoUpdate:         0,
			ServerUrl:        "",
			EdbNameEn:        edbName,
			UnitEn:           unit,
			LatestDate:       "",
			LatestValue:      0,
			ChartImage:       "",
		}
		newEdbInfoId, tmpErr := to.Insert(edbInfo)
		if tmpErr != nil {
			err = tmpErr
			return
		}
		edbInfo.EdbInfoId = int(newEdbInfoId)

		// 添加关联关系
		{
			calculateMappingItem := &EdbInfoCalculateMapping{
				EdbInfoCalculateMappingId: 0,
				EdbInfoId:                 edbInfo.EdbInfoId,
				Source:                    edbInfo.Source,
				SourceName:                edbInfo.SourceName,
				EdbCode:                   edbCode,
				FromEdbInfoId:             fromEdbInfo.EdbInfoId,
				FromEdbCode:               fromEdbInfo.EdbCode,
				FromEdbName:               fromEdbInfo.EdbName,
				FromSource:                fromEdbInfo.Source,
				FromSourceName:            fromEdbInfo.SourceName,
				FromTag:                   "",
				Sort:                      1,
				CreateTime:                time.Now(),
				ModifyTime:                time.Now(),
			}
			_, err = to.Insert(calculateMappingItem)
			if err != nil {
				return
			}
		}
	} else {
		edbInfo, err = GetEdbInfoById(reqEdbInfoId)
		if err != nil {
			if err.Error() == utils.ErrNoRow() {
				errMsg = `获取指标信息失败`
			}
			return
		}
		if edbInfo.EdbInfoType != 1 {
			errMsg = `该指标不是预测指标`
			err = errors.New(errMsg)
			return
		}

		latestDateStr = edbInfo.LatestDate
		latestValue = edbInfo.LatestValue
		oldCalculateFormula := edbInfo.CalculateFormula
		//修改指标信息
		switch lang {
		case utils.EnLangVersion:
			edbInfo.EdbNameEn = edbName
			edbInfo.UnitEn = unit
		default:
			edbInfo.EdbName = edbName
			edbInfo.Unit = unit
			edbInfo.EdbNameSource = edbName
		}
		edbInfo.Frequency = frequency
		edbInfo.ClassifyId = classifyId
		edbInfo.CalculateFormula = formula
		edbInfo.ModifyTime = time.Now()
		_, err = to.Update(edbInfo, "EdbName", "EdbNameSource", "Frequency", "Unit", "ClassifyId", "CalculateFormula", "ModifyTime", "EdbNameEn", "UnitEn")
		if err != nil {
			return
		}

		//判断计算指标是否被更换
		var existCondition string
		var existPars []interface{}
		existCondition += " AND edb_info_id=? AND from_edb_info_id=? "
		existPars = append(existPars, edbInfo.EdbInfoId, fromEdbInfo.EdbInfoId)

		count, tmpErr := GetEdbInfoCalculateCountByCondition(existCondition, existPars)
		if tmpErr != nil {
			err = errors.New("判断指标是否改变失败,Err:" + tmpErr.Error())
			return
		}
		if count > 0 && formula == oldCalculateFormula { // 指标未被替换，无需重新计算
			return
		}

		//删除，计算指标关联的，基础指标的关联关系
		sql := ` DELETE FROM edb_info_calculate_mapping WHERE edb_info_id = ? `
		_, err = to.Raw(sql, edbInfo.EdbInfoId).Exec()
		if err != nil {
			err = errors.New("删除计算指标关联关系失败,Err:" + err.Error())
			return
		}

		//清空原有已经入库的数据
		tableName := GetEdbDataTableName(edbInfo.Source, edbInfo.SubSource)
		sql = ` DELETE FROM ` + tableName + ` WHERE edb_info_id = ? `
		_, err = to.Raw(sql, edbInfo.EdbInfoId).Exec()
		if err != nil {
			return
		}

		//关联关系
		{
			calculateMappingItem := &EdbInfoCalculateMapping{
				EdbInfoCalculateMappingId: 0,
				EdbInfoId:                 edbInfo.EdbInfoId,
				Source:                    edbInfo.Source,
				SourceName:                edbInfo.SourceName,
				EdbCode:                   edbCode,
				FromEdbInfoId:             fromEdbInfo.EdbInfoId,
				FromEdbCode:               fromEdbInfo.EdbCode,
				FromEdbName:               fromEdbInfo.EdbName,
				FromSource:                fromEdbInfo.Source,
				FromSourceName:            fromEdbInfo.SourceName,
				FromTag:                   "",
				Sort:                      1,
				CreateTime:                time.Now(),
				ModifyTime:                time.Now(),
			}
			_, err = to.Insert(calculateMappingItem)
			if err != nil {
				return
			}
		}

	}

	// 计算数据
	latestDateStr, latestValue, err = refreshAllPredictCalculateJp(to, edbInfo.EdbInfoId, edbInfo.Source, edbInfo.SubSource, fromEdbInfo, edbCode, edbInfo.Frequency, formula)

	return
}

// RefreshAllPredictCalculateJp 刷新全部预测降频值数据
func RefreshAllPredictCalculateJp(edbInfoId, source, subSource int, fromEdbInfo *EdbInfo, edbCode, edbFrequency, formula string) (latestDateStr string, latestValue float64, err error) {
	o := orm.NewOrm()
	to, err := o.Begin()
	if err != nil {
		return
	}
	defer func() {
		if err != nil {
			fmt.Println("RefreshAllCalculateJp,Err:" + err.Error())
			_ = to.Rollback()
		} else {
			_ = to.Commit()
		}
	}()

	// 计算数据
	latestDateStr, latestValue, err = refreshAllPredictCalculateJp(to, edbInfoId, source, subSource, fromEdbInfo, edbCode, edbFrequency, formula)
	return
}

// refreshAllPredictCalculateJp 刷新预测降频数据
func refreshAllPredictCalculateJp(to orm.TxOrmer, edbInfoId, source, subSource int, fromEdbInfo *EdbInfo, edbCode, edbFrequency, formula string) (latestDateStr string, latestValue float64, err error) {
	edbInfoIdStr := strconv.Itoa(edbInfoId)
	//获取源指标数据
	fmt.Println("EdbInfoId:", fromEdbInfo.EdbInfoId)
	dataList, err := GetPredictEdbDataListAll(fromEdbInfo, 1)
	if err != nil {
		return
	}
	var dateArr []string
	dataMap := make(map[string]*EdbInfoSearchData)
	fromDataMap := make(map[string]float64)
	//来源指指标数据
	for _, v := range dataList {
		dateArr = append(dateArr, v.DataTime)
		dataMap[v.DataTime] = v
		fromDataMap[v.DataTime] = v.Value
	}
	fmt.Println("source:", source)

	//获取降频指标所有数据
	existDataList, err := GetAllEdbDataListByTo(to, edbInfoId, source, subSource)
	if err != nil {
		return
	}
	//计算指标的map
	existDataMap := make(map[string]*EdbData, 0)
	for _, v := range existDataList {
		existDataMap[v.DataTime] = v
	}

	latestDateStr = fromEdbInfo.LatestDate

	tableName := GetEdbDataTableName(utils.DATA_SOURCE_PREDICT_CALCULATE_JP, subSource)
	addSql := ` INSERT INTO ` + tableName + ` (edb_info_id,edb_code,data_time,value,create_time,modify_time,data_timestamp) values `
	var isAdd bool

	//existMap := make(map[string]string)
	dataLen := len(dataList)
	if dataLen <= 0 {
		return
	}
	startDataTime, _ := time.ParseInLocation(utils.FormatDate, dataList[0].DataTime, time.Local)
	endDataTime, _ := time.ParseInLocation(utils.FormatDate, dataList[dataLen-1].DataTime, time.Local)

	var lastValue float64     // 最近的值
	var nextEndDate time.Time // 下一个节点的日期
	weekDayDataList := make([]float64, 0)
	for currStartDataTime := startDataTime; !currStartDataTime.After(endDataTime); currStartDataTime = currStartDataTime.AddDate(0, 0, 1) {
		// 将当前数据加入到 weekDayDataList
		if tmpData, ok := dataMap[currStartDataTime.Format(utils.FormatDate)]; ok {
			tmpValue := decimal.NewFromFloat(tmpData.Value)
			tmpValueFloat, _ := tmpValue.Round(4).Float64()
			weekDayDataList = append(weekDayDataList, tmpValueFloat)
		}
		// 如果下个节点的日期不存在,那么就先给赋值(兼容时间区间内只有一组数据的情况)
		if nextEndDate.IsZero() {
			nextEndDate = utils.GetFrequencyEndDay(currStartDataTime, edbFrequency)
		}

		// 日期处理过滤
		switch edbFrequency {
		case "周度":
			if currStartDataTime.Weekday() != 0 {
				//不是周日,代表需要进入下一个循环获取数据并计算
				continue
			} else {
				//记录下一个结束节点的日期
				nextEndDate = currStartDataTime.AddDate(0, 0, 7)
			}
		case "旬度":
			nextDay := currStartDataTime.AddDate(0, 0, 1)
			if nextDay.Day() != 1 && nextDay.Day() != 11 && nextDay.Day() != 21 {
				//不是每月10、20、最后一天,代表需要进入下一个循环获取数据并计算
				continue
			} else {
				//记录下一个结束节点的日期
				if nextDay.Day() == 1 || nextDay.Day() == 11 {
					//月初或者月末的时候,加10天就好了
					nextEndDate = nextDay.AddDate(0, 0, 9)
				} else {
					tmpNextMonth := nextDay.AddDate(0, 1, 0)
					nextEndDate = time.Date(tmpNextMonth.Year(), tmpNextMonth.Month(), 1, 0, 0, 0, 0, time.Local).AddDate(0, 0, -1)
				}
			}
		case "月度":
			nextDay := currStartDataTime.AddDate(0, 0, 1)
			if nextDay.Day() != 1 {
				//不是每月最后一天,代表需要进入下一个循环获取数据并计算
				continue
			} else {
				//记录下一个结束节点的日期
				nextEndDate = nextDay.AddDate(0, 1, -1)
			}
		case "季度":
			nextDay := currStartDataTime.AddDate(0, 0, 1)
			if (nextDay.Month() == 1 || nextDay.Month() == 4 || nextDay.Month() == 7 || nextDay.Month() == 10) && nextDay.Day() == 1 {
				//记录下一个结束节点的日期
				nextEndDate = nextDay.AddDate(0, 3, -1)
			} else {
				//不是3,6,9,12 月份的最后一天,代表需要进入下一个循环获取数据并计算
				continue
			}
		case "年度":
			if currStartDataTime.Month() == 12 && currStartDataTime.Day() == 31 {
				//记录下一个结束节点的日期
				nextEndDate = currStartDataTime.AddDate(1, 0, 0)
			} else {
				//不是每年的12-31日,代表需要进入下一个循环获取数据并计算
				continue
			}
		default:
			err = errors.New("错误的频度:" + edbFrequency)
			return
		}

		// 当前时间段内的数据计算,得出实际值
		var currVal float64
		lenWeekDayDataList := len(weekDayDataList)
		// 如果这个时间区间内没有数据,那么就采用上一个时间区间的值
		if len(weekDayDataList) <= 0 {
			currVal = lastValue
		} else {
			if formula == "期末值" {
				currVal = weekDayDataList[lenWeekDayDataList-1]
			} else {
				// 平均值
				sumValDeci := decimal.NewFromFloat(0)
				for _, v := range weekDayDataList {
					tmpValDeci := decimal.NewFromFloat(v)
					sumValDeci = sumValDeci.Add(tmpValDeci)
				}
				lenDeci := decimal.NewFromInt(int64(lenWeekDayDataList))
				currVal, _ = sumValDeci.Div(lenDeci).Round(4).Float64()
			}
		}

		// 给实际日期数据的值赋值
		if fromEdbInfo.LatestDate == currStartDataTime.Format(utils.FormatDate) {
			latestValue = currVal
		}

		// 判断降频指标是否存在数据
		if existData, ok := existDataMap[currStartDataTime.Format(utils.FormatDate)]; ok {
			// 处理降频数据的值
			existValStr := existData.Value
			existValDeci, tmpErr := decimal.NewFromString(existValStr)
			if tmpErr != nil {
				err = tmpErr
				return
			}
			existVal, _ := existValDeci.Round(4).Float64()
			// 判断降频数据的值 与 当前计算出来的结果, 如果两个数据结果不相等的话,那么就修改咯
			if existVal != currVal {
				err = ModifyEdbDataById(source, subSource, existData.EdbDataId, fmt.Sprint(currVal))
				if err != nil {
					return
				}
			}
		} else {
			// 直接入库
			timestamp := currStartDataTime.UnixNano() / 1e6
			timestampStr := fmt.Sprintf("%d", timestamp)
			addSql += GetAddSql(edbInfoIdStr, edbCode, currStartDataTime.Format(utils.FormatDate), timestampStr, fmt.Sprint(currVal))
			isAdd = true
		}

		// 一轮结束后,数据清空
		weekDayDataList = make([]float64, 0)
	}

	// 最后已有的日期处理完成后,需要对剩余不在时间段内的数据做处理
	if len(weekDayDataList) > 0 {
		// 当前时间段内的数据计算,得出实际值
		var currVal float64
		lenWeekDayDataList := len(weekDayDataList)
		// 如果这个时间区间内没有数据,那么就采用上一个时间区间的值
		if len(weekDayDataList) < 0 {
			currVal = lastValue
		} else {
			if formula == "期末值" {
				currVal = weekDayDataList[lenWeekDayDataList-1]
			} else {
				// 平均值
				sumValDeci := decimal.NewFromFloat(0)
				for _, v := range weekDayDataList {
					tmpValDeci := decimal.NewFromFloat(v)
					sumValDeci = sumValDeci.Add(tmpValDeci)
				}
				lenDeci := decimal.NewFromInt(int64(lenWeekDayDataList))
				currVal, _ = sumValDeci.Div(lenDeci).Round(4).Float64()
			}
		}

		// 判断降频指标是否存在数据
		if existData, ok := existDataMap[nextEndDate.Format(utils.FormatDate)]; ok {
			// 处理降频数据的值
			existValStr := existData.Value
			existValDeci, tmpErr := decimal.NewFromString(existValStr)
			if tmpErr != nil {
				err = tmpErr
				return
			}
			existVal, _ := existValDeci.Round(4).Float64()
			// 判断降频数据的值 与 当前计算出来的结果, 如果两个数据结果不相等的话,那么就修改咯
			if existVal != currVal {
				err = ModifyEdbDataById(source, subSource, existData.EdbDataId, fmt.Sprint(currVal))
				if err != nil {
					return
				}
			}
		} else {
			// 直接入库
			timestamp := nextEndDate.UnixNano() / 1e6
			timestampStr := fmt.Sprintf("%d", timestamp)
			addSql += GetAddSql(edbInfoIdStr, edbCode, nextEndDate.Format(utils.FormatDate), timestampStr, fmt.Sprint(currVal))
			isAdd = true
		}
	}

	if isAdd {
		addSql = strings.TrimRight(addSql, ",")
		_, err = to.Raw(addSql).Exec()
	}

	return
}
