package models

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

// SavePredictCalculateTcz 同差值
func SavePredictCalculateTcz(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("AddCalculateTcz,Err:" + err.Error())
			_ = to.Rollback()
		} else {
			_ = to.Commit()
		}
	}()
	fmt.Println("reqEdbInfoId:", reqEdbInfoId)

	if reqEdbInfoId <= 0 {
		edbInfo = &EdbInfo{
			//EdbInfoId:        0,
			EdbInfoType:   1,
			SourceName:    "预测同差",
			Source:        utils.DATA_SOURCE_PREDICT_CALCULATE_TCZ,
			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
		//修改指标信息
		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.ModifyTime = time.Now()
		_, err = to.Update(edbInfo, "EdbName", "EdbNameSource", "Frequency", "Unit", "ClassifyId", "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)

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

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

		// 删除该指标所有的同差数据
		dataTableName := GetEdbDataTableName(utils.DATA_SOURCE_PREDICT_CALCULATE_TCZ, utils.DATA_SUB_SOURCE_EDB)
		fmt.Println("dataTableName:", dataTableName)
		deleteSql := ` DELETE FROM %s WHERE edb_info_id=? `
		deleteSql = fmt.Sprintf(deleteSql, dataTableName)
		_, err = to.Raw(deleteSql, edbInfo.EdbInfoId).Exec()
		if err != nil {
			return
		}
	}

	edbInfoIdStr := strconv.Itoa(edbInfo.EdbInfoId)

	//获取源指标数据
	fmt.Println("EdbInfoId:", fromEdbInfo.EdbInfoId)
	dataList, err := GetPredictEdbDataListAll(fromEdbInfo, 0)
	if err != nil {
		return
	}

	latestDateStr = fromEdbInfo.LatestDate
	// 计算数据
	var dateArr []string
	dataMap := make(map[string]*EdbInfoSearchData)
	for _, v := range dataList {
		dateArr = append(dateArr, v.DataTime)
		dataMap[v.DataTime] = v
	}
	//fmt.Println("Frequency:", fromEdbInfo.Frequency)

	addSql := ` INSERT INTO edb_data_predict_calculate_tcz (edb_info_id,edb_code,data_time,value,create_time,modify_time,data_timestamp) values `
	var isAdd bool
	existMap := make(map[string]string)
	for _, av := range dateArr {
		//fmt.Println("dateArr:", ak, av)
		currentItem := dataMap[av]
		if currentItem != nil {
			//当前日期
			currentDate, tmpErr := time.ParseInLocation(utils.FormatDate, av, time.Local)
			if tmpErr != nil {
				err = tmpErr
				return
			}
			//上一年的日期
			preDate := currentDate.AddDate(-1, 0, 0)
			preDateStr := preDate.Format(utils.FormatDate)
			if findItem, ok := dataMap[preDateStr]; ok { //上一年同期找到
				//dataTime, _ := time.ParseInLocation(utils.FormatDate, date, time.Local)
				if _, ok := existMap[edbCode+av]; !ok {
					timestamp := currentDate.UnixNano() / 1e6
					timestampStr := fmt.Sprintf("%d", timestamp)

					val := TczSub(currentItem.Value, findItem.Value)
					addSql += GetAddSql(edbInfoIdStr, edbCode, av, timestampStr, val)
					isAdd = true

					// 实际数据的值
					if fromEdbInfo.LatestDate == av {
						latestValueDecimal, tmpErr := decimal.NewFromString(val)
						if tmpErr != nil {
							return
						}
						latestValue, _ = latestValueDecimal.Truncate(4).Float64() //保留4位小数
					}
				}
				existMap[edbCode+av] = av
			} else {
				if fromEdbInfo.Frequency == "月度" { //向上和向下，各找一个月
					nextDateDay := preDate
					preDateDay := preDate
					for i := 0; i <= 35; i++ {
						nextDateDayStr := nextDateDay.Format(utils.FormatDate)
						if findItem, ok := dataMap[nextDateDayStr]; ok { //下一年同期->下一个月找到
							if _, ok := existMap[edbCode+av]; !ok {
								timestamp := currentDate.UnixNano() / 1e6
								timestampStr := fmt.Sprintf("%d", timestamp)
								val := TczSub(currentItem.Value, findItem.Value)

								addSql += GetAddSql(edbInfoIdStr, edbCode, av, timestampStr, val)
								isAdd = true

								// 实际数据的值
								if fromEdbInfo.LatestDate == av {
									latestValueDecimal, tmpErr := decimal.NewFromString(val)
									if tmpErr != nil {
										return
									}
									latestValue, _ = latestValueDecimal.Truncate(4).Float64() //保留4位小数
								}
							}
							existMap[edbCode+av] = av
							break
						} else {
							preDateDayStr := preDateDay.Format(utils.FormatDate)
							if findItem, ok := dataMap[preDateDayStr]; ok { //上一年同期->上一个月找到
								if _, ok := existMap[edbCode+av]; !ok {
									timestamp := currentDate.UnixNano() / 1e6
									timestampStr := fmt.Sprintf("%d", timestamp)
									val := TczSub(currentItem.Value, findItem.Value)

									addSql += GetAddSql(edbInfoIdStr, edbCode, av, timestampStr, val)
									isAdd = true

									// 实际数据的值
									if fromEdbInfo.LatestDate == av {
										latestValueDecimal, tmpErr := decimal.NewFromString(val)
										if tmpErr != nil {
											return
										}
										latestValue, _ = latestValueDecimal.Truncate(4).Float64() //保留4位小数
									}
								}
								existMap[edbCode+av] = av
								break
							}
						}
						nextDateDay = nextDateDay.AddDate(0, 0, 1)
						preDateDay = preDateDay.AddDate(0, 0, -1)
					}
				} else if fromEdbInfo.Frequency == "季度" || fromEdbInfo.Frequency == "年度" {
					if findItem, ok := dataMap[preDateStr]; ok { //上一年同期->下一个月找到
						if _, ok := existMap[edbCode+av]; !ok {
							timestamp := currentDate.UnixNano() / 1e6
							timestampStr := fmt.Sprintf("%d", timestamp)
							val := TczSub(currentItem.Value, findItem.Value)
							addSql += GetAddSql(edbInfoIdStr, edbCode, av, timestampStr, val)
							isAdd = true

							// 实际数据的值
							if fromEdbInfo.LatestDate == av {
								latestValueDecimal, tmpErr := decimal.NewFromString(val)
								if tmpErr != nil {
									return
								}
								latestValue, _ = latestValueDecimal.Truncate(4).Float64() //保留4位小数
							}
						}
						existMap[edbCode+av] = av
						break
					}
				} else {
					nextDateDay := preDate
					preDateDay := preDate
					for i := 0; i < 35; i++ {
						nextDateDayStr := nextDateDay.Format(utils.FormatDate)
						if findItem, ok := dataMap[nextDateDayStr]; ok { //上一年同期->下一个月找到
							if _, ok := existMap[edbCode+av]; !ok {
								timestamp := currentDate.UnixNano() / 1e6
								timestampStr := fmt.Sprintf("%d", timestamp)
								val := TczSub(currentItem.Value, findItem.Value)

								addSql += GetAddSql(edbInfoIdStr, edbCode, av, timestampStr, val)
								isAdd = true

								// 实际数据的值
								if fromEdbInfo.LatestDate == av {
									latestValueDecimal, tmpErr := decimal.NewFromString(val)
									if tmpErr != nil {
										return
									}
									latestValue, _ = latestValueDecimal.Truncate(4).Float64() //保留4位小数
								}
							}
							existMap[edbCode+av] = av
							break
						} else {
							preDateDayStr := preDateDay.Format(utils.FormatDate)
							if findItem, ok := dataMap[preDateDayStr]; ok { //上一年同期->上一个月找到
								if _, ok := existMap[edbCode+av]; !ok {
									timestamp := currentDate.UnixNano() / 1e6
									timestampStr := fmt.Sprintf("%d", timestamp)
									val := TczSub(currentItem.Value, findItem.Value)

									addSql += GetAddSql(edbInfoIdStr, edbCode, av, timestampStr, val)
									isAdd = true

									// 实际数据的值
									if fromEdbInfo.LatestDate == av {
										latestValueDecimal, tmpErr := decimal.NewFromString(val)
										if tmpErr != nil {
											return
										}
										latestValue, _ = latestValueDecimal.Truncate(4).Float64() //保留4位小数
									}
								}
								existMap[edbCode+av] = av
								break
							} else {

							}
						}
						nextDateDay = nextDateDay.AddDate(0, 0, 1)
						preDateDay = preDateDay.AddDate(0, 0, -1)
					}
				}
			}
		}
	}
	if isAdd {
		addSql = strings.TrimRight(addSql, ",")
		_, err = to.Raw(addSql).Exec()
		if err != nil {
			return
		}
	}
	return
}

// RefreshAllPredictCalculateTcz 刷新全部同差值数据
func RefreshAllPredictCalculateTcz(edbInfoId, source, subSource int, fromEdbInfo *EdbInfo, edbCode, startDate 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("RefreshAllCalculateTcz,Err:" + err.Error())
			_ = to.Rollback()
		} else {
			_ = to.Commit()
		}
	}()

	if err != nil {
		return
	}
	edbInfoIdStr := strconv.Itoa(edbInfoId)

	// 获取关联指标数据
	dataList, err := GetPredictEdbDataListAllByStartDate(fromEdbInfo, 0, startDate)
	if err != nil {
		return
	}

	latestDateStr = fromEdbInfo.LatestDate
	//计算数据
	var dateArr []string
	dataMap := make(map[string]*EdbInfoSearchData)
	for _, v := range dataList {
		dateArr = append(dateArr, v.DataTime)
		dataMap[v.DataTime] = v
	}
	//获取指标所有数据
	existDataList := make([]*EdbData, 0)
	dataTableName := GetEdbDataTableName(source, subSource)
	sql := `SELECT * FROM %s WHERE edb_info_id=? `
	sql = fmt.Sprintf(sql, dataTableName)
	_, err = o.Raw(sql, edbInfoId).QueryRows(&existDataList)
	if err != nil {
		return
	}
	existDataMap := make(map[string]string)
	for _, v := range existDataList {
		existDataMap[v.DataTime] = v.Value
	}

	addSql := ` INSERT INTO edb_data_predict_calculate_tcz(edb_info_id,edb_code,data_time,value,create_time,modify_time,data_timestamp) values `
	var isAdd bool
	existAddDataMap := make(map[string]string)
	for _, av := range dateArr {
		currentItem := dataMap[av]
		if currentItem != nil {
			//当前日期
			currentDate, tmpErr := time.ParseInLocation(utils.FormatDate, av, time.Local)
			if tmpErr != nil {
				err = tmpErr
				return
			}
			//上一年的日期
			preDate := currentDate.AddDate(-1, 0, 0)
			preDateStr := preDate.Format(utils.FormatDate)
			if findItem, ok := dataMap[preDateStr]; ok { //上一年同期找到
				//dataTime, _ := time.ParseInLocation(utils.FormatDate, date, time.Local)
				timestamp := currentDate.UnixNano() / 1e6
				timestampStr := fmt.Sprintf("%d", timestamp)
				val := TczSub(currentItem.Value, findItem.Value)

				if existVal, ok := existDataMap[av]; !ok {
					if _, existOk := existAddDataMap[av]; !existOk {
						addSql += GetAddSql(edbInfoIdStr, edbCode, av, timestampStr, val)
						isAdd = true

						// 实际数据的值
						if fromEdbInfo.LatestDate == av {
							latestValueDecimal, tmpErr := decimal.NewFromString(val)
							if tmpErr != nil {
								return
							}
							latestValue, _ = latestValueDecimal.Truncate(4).Float64() //保留4位小数
						}
					}
					existAddDataMap[av] = av
				} else {
					existValDecimal, tmpErr := decimal.NewFromString(existVal)
					if tmpErr != nil {
						err = tmpErr
						return
					}
					existStr := existValDecimal.String()
					if existStr != val {
						sql := ` UPDATE %s SET value=?,modify_time=NOW() WHERE edb_info_id=? AND data_time=? `
						sql = fmt.Sprintf(sql, dataTableName)
						_, err = o.Raw(sql, val, edbInfoId, av).Exec()
						if err != nil {
							return
						}
					}
				}
				utils.FileLog.Info("同期找到:" + av + ";" + preDateStr)
				continue
			} else {
				if fromEdbInfo.Frequency == "月度" { //向上和向下，各找一个月
					for i := 0; i <= 35; i++ {
						nextDateDay := preDate.AddDate(0, 0, 1)
						nextDateDayStr := nextDateDay.Format(utils.FormatDate)
						if findItem, ok := dataMap[nextDateDayStr]; ok { //上一年同期->下一个月找到
							timestamp := currentDate.UnixNano() / 1e6
							timestampStr := fmt.Sprintf("%d", timestamp)
							val := TczSub(currentItem.Value, findItem.Value)

							if existVal, ok := existDataMap[av]; !ok {
								if _, existOk := existAddDataMap[av]; !existOk {
									addSql += GetAddSql(edbInfoIdStr, edbCode, av, timestampStr, val)
									isAdd = true

									// 实际数据的值
									if fromEdbInfo.LatestDate == av {
										latestValueDecimal, tmpErr := decimal.NewFromString(val)
										if tmpErr != nil {
											return
										}
										latestValue, _ = latestValueDecimal.Truncate(4).Float64() //保留4位小数
									}
								}
								existAddDataMap[av] = av
							} else {
								existValDecimal, tmpErr := decimal.NewFromString(existVal)
								if tmpErr != nil {
									err = tmpErr
									return
								}
								existStr := existValDecimal.String()
								if existStr != val {
									sql := ` UPDATE %s SET value=?,modify_time=NOW() WHERE edb_info_id=? AND data_time=? `
									sql = fmt.Sprintf(sql, dataTableName)
									_, err = o.Raw(sql, val, edbInfoId, av).Exec()
									if err != nil {
										return
									}
								}
							}
							break
						} else {
							preDateDay := preDate.AddDate(0, 0, -1)
							preDateDayStr := preDateDay.Format(utils.FormatDate)
							if findItem, ok := dataMap[preDateDayStr]; ok { //上一年同期->上一个月找到
								timestamp := currentDate.UnixNano() / 1e6
								timestampStr := fmt.Sprintf("%d", timestamp)
								val := TczSub(currentItem.Value, findItem.Value)

								if existVal, ok := existDataMap[av]; !ok {
									if _, existOk := existAddDataMap[av]; !existOk {
										addSql += GetAddSql(edbInfoIdStr, edbCode, av, timestampStr, val)
										isAdd = true

										// 实际数据的值
										if fromEdbInfo.LatestDate == av {
											latestValueDecimal, tmpErr := decimal.NewFromString(val)
											if tmpErr != nil {
												return
											}
											latestValue, _ = latestValueDecimal.Truncate(4).Float64() //保留4位小数
										}
									}
									existAddDataMap[av] = av
								} else {
									existValDecimal, tmpErr := decimal.NewFromString(existVal)
									if tmpErr != nil {
										err = tmpErr
										return
									}
									existStr := existValDecimal.String()
									if existStr != val {
										sql := ` UPDATE %s SET value=?,modify_time=NOW() WHERE edb_info_id=? AND data_time=? `
										sql = fmt.Sprintf(sql, dataTableName)
										_, err = o.Raw(sql, val, edbInfoId, av).Exec()
										if err != nil {
											return
										}
									}
								}
								break
							}
						}
					}
				} else if fromEdbInfo.Frequency == "季度" || fromEdbInfo.Frequency == "年度" {
					if findItem, ok := dataMap[preDateStr]; ok { //上一年同期->下一个月找到
						timestamp := currentDate.UnixNano() / 1e6
						timestampStr := fmt.Sprintf("%d", timestamp)
						val := TczSub(currentItem.Value, findItem.Value)

						if existVal, ok := existDataMap[av]; !ok {
							if _, existOk := existAddDataMap[av]; !existOk {
								addSql += GetAddSql(edbInfoIdStr, edbCode, av, timestampStr, val)
								isAdd = true

								// 实际数据的值
								if fromEdbInfo.LatestDate == av {
									latestValueDecimal, tmpErr := decimal.NewFromString(val)
									if tmpErr != nil {
										return
									}
									latestValue, _ = latestValueDecimal.Truncate(4).Float64() //保留4位小数
								}
							}
							existAddDataMap[av] = av
						} else {
							existValDecimal, tmpErr := decimal.NewFromString(existVal)
							if tmpErr != nil {
								err = tmpErr
								return
							}
							existStr := existValDecimal.String()
							if existStr != val {
								sql := ` UPDATE %s SET value=?,modify_time=NOW() WHERE edb_info_id=? AND data_time=? `
								sql = fmt.Sprintf(sql, dataTableName)
								_, err = o.Raw(sql, val, edbInfoId, av).Exec()
								if err != nil {
									return
								}
							}
						}
						break
					}
				} else {
					nextDateDay := preDate
					preDateDay := preDate
					for i := 0; i < 35; i++ {
						nextDateDayStr := nextDateDay.Format(utils.FormatDate)
						if findItem, ok := dataMap[nextDateDayStr]; ok { //上一年同期->下一个月找到
							timestamp := currentDate.UnixNano() / 1e6
							timestampStr := fmt.Sprintf("%d", timestamp)
							val := TczSub(currentItem.Value, findItem.Value)

							if existVal, ok := existDataMap[av]; !ok {
								if _, existOk := existAddDataMap[av]; !existOk {
									addSql += GetAddSql(edbInfoIdStr, edbCode, av, timestampStr, val)
									isAdd = true

									// 实际数据的值
									if fromEdbInfo.LatestDate == av {
										latestValueDecimal, tmpErr := decimal.NewFromString(val)
										if tmpErr != nil {
											return
										}
										latestValue, _ = latestValueDecimal.Truncate(4).Float64() //保留4位小数
									}
								}
								existAddDataMap[av] = av
							} else {
								existValDecimal, tmpErr := decimal.NewFromString(existVal)
								if tmpErr != nil {
									err = tmpErr
									return
								}
								existStr := existValDecimal.String()
								if existStr != val {
									sql := ` UPDATE %s SET value=?,modify_time=NOW() WHERE edb_info_id=? AND data_time=? `
									sql = fmt.Sprintf(sql, dataTableName)
									_, err = o.Raw(sql, val, edbInfoId, av).Exec()
									if err != nil {
										return
									}
								}
							}
							break
						} else {
							preDateDayStr := preDateDay.Format(utils.FormatDate)
							if findItem, ok := dataMap[preDateDayStr]; ok { //上一年同期->上一个月找到
								timestamp := currentDate.UnixNano() / 1e6
								timestampStr := fmt.Sprintf("%d", timestamp)
								val := TczSub(currentItem.Value, findItem.Value)

								if existVal, ok := existDataMap[av]; !ok {
									if _, existOk := existAddDataMap[av]; !existOk {
										addSql += GetAddSql(edbInfoIdStr, edbCode, av, timestampStr, val)
										isAdd = true

										// 实际数据的值
										if fromEdbInfo.LatestDate == av {
											latestValueDecimal, tmpErr := decimal.NewFromString(val)
											if tmpErr != nil {
												return
											}
											latestValue, _ = latestValueDecimal.Truncate(4).Float64() //保留4位小数
										}
									}
									existAddDataMap[av] = av
								} else {
									existValDecimal, tmpErr := decimal.NewFromString(existVal)
									if tmpErr != nil {
										err = tmpErr
										return
									}
									existStr := existValDecimal.String()
									if existStr != val {
										sql := ` UPDATE %s SET value=?,modify_time=NOW() WHERE edb_info_id=? AND data_time=? `
										sql = fmt.Sprintf(sql, dataTableName)
										_, err = o.Raw(sql, val, edbInfoId, av).Exec()
										if err != nil {
											return
										}
									}
								}
								break
							}
						}
						nextDateDay = nextDateDay.AddDate(0, 0, 1)
						preDateDay = preDateDay.AddDate(0, 0, -1)
					}
				}
			}
		}
	}
	if isAdd {
		addSql = strings.TrimRight(addSql, ",")
		_, err = o.Raw(addSql).Exec()
		if err != nil {
			return
		}
	}
	return
}
