package models

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

// PredictTb 预测同比值
type PredictTb struct {
}

// Add 添加
func (obj PredictTb) Add(params BatchSaveCalculateBatchParams) (edbInfo *EdbInfo, latestDateStr string, latestValue float64, err error, errMsg string) {
	req := params.Req
	fromEdbInfo := params.FromEdbInfo
	edbCode := params.EdbCode

	o := orm.NewOrm()
	to, err := o.Begin()
	if err != nil {
		return
	}
	defer func() {
		if err != nil {
			_ = to.Rollback()
			fmt.Println(reflect.TypeOf(obj).Name(), "；Add,Err:"+err.Error())
		} else {
			_ = to.Commit()
		}
	}()

	edbInfo = &EdbInfo{
		//EdbInfoId:        0,
		SourceName:       obj.GetSourceName(),
		Source:           obj.GetSource(),
		EdbCode:          edbCode,
		EdbName:          req.EdbName,
		EdbNameSource:    req.EdbName,
		Frequency:        req.Frequency,
		Unit:             req.Unit,
		StartDate:        "",
		EndDate:          "",
		ClassifyId:       req.ClassifyId,
		SysUserId:        params.SysUserId,
		SysUserRealName:  params.SysUserRealName,
		UniqueCode:       params.UniqueCode,
		CreateTime:       time.Now(),
		ModifyTime:       time.Now(),
		MinValue:         0,
		MaxValue:         0,
		CalculateFormula: req.Formula,
		EdbType:          2,
		Sort:             0,
		MoveType:         0,
		MoveFrequency:    "",
		NoUpdate:         0,
		ServerUrl:        "",
		EdbInfoType:      1,
		EdbNameEn:        req.EdbName,
		UnitEn:           req.Unit,
		LatestDate:       "",
		LatestValue:      0,
		ChartImage:       "",
		Calendar:         "",
	}

	newEdbInfoId, tmpErr := to.Insert(edbInfo)
	if tmpErr != nil {
		err = tmpErr
		return
	}
	edbInfo.EdbInfoId = int(newEdbInfoId)

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

	//计算数据
	latestDateStr, latestValue, err = obj.refresh(to, edbInfo, fromEdbInfo, edbInfo.EdbCode, "")

	return
}

// Edit 编辑
func (obj PredictTb) Edit(params BatchSaveCalculateBatchParams) (latestDateStr string, latestValue float64, err error, errMsg string) {
	req := params.Req
	edbInfo := params.EdbInfo
	fromEdbInfo := params.FromEdbInfo

	latestDateStr = edbInfo.LatestDate
	latestValue = edbInfo.LatestValue

	if fromEdbInfo.Frequency != req.Frequency {
		errMsg = "生成指标频度与原指标频度不同"
		err = errors.New(errMsg)
		return
	}
	if edbInfo.EdbInfoType != 1 {
		errMsg = `该指标不是预测指标`
		err = errors.New(errMsg)
		return
	}

	latestDateStr = edbInfo.LatestDate
	latestValue = edbInfo.LatestValue

	o := orm.NewOrm()
	to, err := o.Begin()
	if err != nil {
		return
	}
	defer func() {
		if err != nil {
			_ = to.Rollback()
			fmt.Println(reflect.TypeOf(obj).Name(), "；Edit,Err:"+err.Error())
		} else {
			_ = to.Commit()
		}
	}()
	tableName := GetEdbDataTableName(edbInfo.Source, edbInfo.SubSource)

	//修改指标信息
	switch params.Lang {
	case utils.EnLangVersion:
		edbInfo.EdbNameEn = req.EdbName
		edbInfo.UnitEn = req.Unit
	default:
		edbInfo.EdbName = req.EdbName
		edbInfo.Unit = req.Unit
		edbInfo.EdbNameSource = req.EdbName
	}
	edbInfo.Frequency = req.Frequency
	edbInfo.ClassifyId = req.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
	}

	// 删除该指标所有的同比数据
	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:                    obj.GetSource(),
			SourceName:                obj.GetSourceName(),
			EdbCode:                   edbInfo.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 = obj.refresh(to, edbInfo, fromEdbInfo, edbInfo.EdbCode, "")

	return
}

// Refresh 刷新
func (obj PredictTb) Refresh(params RefreshParams) (latestDateStr string, latestValue float64, err error, errMsg string) {
	calculateMapping, err := GetEdbInfoCalculateMappingDetail(params.EdbInfo.EdbInfoId)
	if err != nil {
		return
	}
	fromEdbInfo, err := GetEdbInfoById(calculateMapping.FromEdbInfoId)
	if err != nil {
		errMsg = "GetEdbInfoById Err:" + err.Error()
		return
	}

	o := orm.NewOrm()
	to, err := o.Begin()
	if err != nil {
		return
	}
	defer func() {
		if err != nil {
			_ = to.Rollback()
			fmt.Println(reflect.TypeOf(obj).Name(), "；Refresh,Err:"+err.Error())
		} else {
			_ = to.Commit()
		}
	}()

	// 计算数据
	latestDateStr, latestValue, err = obj.refresh(to, params.EdbInfo, fromEdbInfo, params.EdbInfo.EdbCode, params.StartDate)

	return
}

// GetSource 获取来源编码id
func (obj PredictTb) GetSource() int {
	return utils.DATA_SOURCE_PREDICT_CALCULATE_TBZ
}

// GetSourceName 获取来源名称
func (obj PredictTb) GetSourceName() string {
	return utils.DATA_SOURCE_NAME_PREDICT_CALCULATE_TBZ
}

func (obj PredictTb) refresh(to orm.TxOrmer, edbInfo, fromEdbInfo *EdbInfo, edbCode, startDate string) (latestDateStr string, latestValue float64, err error) {
	edbInfoId := edbInfo.EdbInfoId
	dataTableName := GetEdbDataTableName(edbInfo.Source, edbInfo.SubSource)

	edbInfoIdStr := strconv.Itoa(edbInfoId)

	// 获取关联指标数据
	dataList, err := GetPredictEdbDataListAllByStartDate(fromEdbInfo, 1, 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)
	fmt.Println("dataTableName:", dataTableName)

	sql := `SELECT * FROM %s WHERE edb_info_id=? `
	sql = fmt.Sprintf(sql, dataTableName)
	_, err = to.Raw(sql, edbInfoId).QueryRows(&existDataList)
	if err != nil {
		return
	}
	existDataMap := make(map[string]string)
	removeDataTimeMap := make(map[string]int) //需要移除的日期数据
	for _, v := range existDataList {
		existDataMap[v.DataTime] = v.Value
		removeDataTimeMap[v.DataTime] = 1
	}

	fmt.Println("existDataMap:", existDataMap)
	addSql := ` INSERT INTO edb_data_predict_calculate_tbz (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 {
		//校验待删除日期数据里面是否存在该元素，如果存在的话，那么移除该日期
		delete(removeDataTimeMap, av)

		currentItem, ok := dataMap[av]
		if !ok || currentItem == nil {
			continue
		}

		//当前日期
		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 := TbzDiv(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 = to.Raw(sql, val, edbInfoId, av).Exec()
					if err != nil {
						return
					}
				}
			}
			continue
		} else {
			if fromEdbInfo.Frequency == "月度" { //向上和向下，各找一个月
				for i := 0; i <= 35; i++ {
					nextDateDay := preDate.AddDate(0, 0, i)
					nextDateDayStr := nextDateDay.Format(utils.FormatDate)
					if findItem, ok := dataMap[nextDateDayStr]; ok { //上一年同期->下一个月找到
						timestamp := currentDate.UnixNano() / 1e6
						timestampStr := fmt.Sprintf("%d", timestamp)
						val := TbzDiv(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 = to.Raw(sql, val, edbInfoId, av).Exec()
								if err != nil {
									return
								}
							}
						}
						break
					} else {
						preDateDay := preDate.AddDate(0, 0, -i)
						preDateDayStr := preDateDay.Format(utils.FormatDate)
						if findItem, ok := dataMap[preDateDayStr]; ok { //上一年同期->上一个月找到
							timestamp := currentDate.UnixNano() / 1e6
							timestampStr := fmt.Sprintf("%d", timestamp)
							val := TbzDiv(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 = to.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 := TbzDiv(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 = to.Raw(sql, val, edbInfoId, av).Exec()
							if err != nil {
								return
							}
						}
					}
					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 { //上一年同期->下一个月找到
						timestamp := currentDate.UnixNano() / 1e6
						timestampStr := fmt.Sprintf("%d", timestamp)
						val := TbzDiv(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 = to.Raw(sql, val, edbInfoId, av).Exec()
								if err != nil {
									return
								}
							}
						}
						break
					} else {
						if i >= 1 {
							preDateDay = preDateDay.AddDate(0, 0, -i)
							preDateDayStr = preDateDay.Format(utils.FormatDate)
						}
						if findItem, ok := dataMap[preDateDayStr]; ok { //上一年同期->上一个月找到
							timestamp := currentDate.UnixNano() / 1e6
							timestampStr := fmt.Sprintf("%d", timestamp)
							val := TbzDiv(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 = to.Raw(sql, val, edbInfoId, av).Exec()
									if err != nil {
										return
									}
								}
							}
							break
						}
					}
				}
			}
		}
	}

	//删除已经不存在的指标数据（由于该指标当日的数据删除了）
	{
		removeDateList := make([]string, 0)
		for dateTime := range removeDataTimeMap {
			removeDateList = append(removeDateList, dateTime)
		}
		removeNum := len(removeDateList)
		if removeNum > 0 {
			sql := fmt.Sprintf(` DELETE FROM %s WHERE edb_info_id = ? and data_time in (`+utils.GetOrmInReplace(removeNum)+`) `, dataTableName)
			_, err = to.Raw(sql, edbInfo.EdbInfoId, removeDateList).Exec()
			if err != nil {
				fmt.Println(reflect.TypeOf(obj).Name(), " add data ;delete Err", err.Error())
				err = fmt.Errorf("删除不存在的指标数据失败,Err:" + err.Error())
				return
			}
		}
	}

	// 批量插入
	if isAdd {
		addSql = strings.TrimRight(addSql, ",")
		_, err = to.Raw(addSql).Exec()
		if err != nil {
			return
		}
	}

	//确定实际数据的最终值
	{
		finalLast, tmpErr := GetFinalLastByTo(to, edbInfoId, edbInfo.Source, edbInfo.SubSource, fromEdbInfo.LatestDate)
		if tmpErr != nil && tmpErr.Error() != utils.ErrNoRow() {
			return
		}
		if tmpErr == nil {
			latestDateStr = finalLast.DataTime
			latestValue = finalLast.Value
		}
	}

	return
}

// SavePredictCalculateTbz 新增/编辑 预测同比指标
func SavePredictCalculateTbz(reqEdbInfoId, classifyId int, edbName, frequency, unit, formula string, fromEdbInfo *EdbInfo, edbCode, uniqueCode string, sysUserId int, sysUserRealName 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("AddCalculateTbz,Err:" + err.Error())
			_ = to.Rollback()
		} else {
			_ = to.Commit()
		}
	}()

	if reqEdbInfoId <= 0 {
		edbInfo = &EdbInfo{
			//EdbInfoId:        0,
			EdbInfoType:   1,
			SourceName:    "预测同比",
			Source:        utils.DATA_SOURCE_PREDICT_CALCULATE_TBZ,
			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:        "",
			UnitEn:           "",
			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
		//修改指标信息
		edbInfo.EdbName = edbName
		edbInfo.EdbNameSource = edbName
		edbInfo.Frequency = frequency
		edbInfo.Unit = unit
		edbInfo.ClassifyId = classifyId
		edbInfo.ModifyTime = time.Now()
		_, err = to.Update(edbInfo, "EdbName", "EdbNameSource", "Frequency", "Unit", "ClassifyId", "ModifyTime")
		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_TBZ, utils.DATA_SUB_SOURCE_EDB)
		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)
	// 获取源指标数据
	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
	}

	addSql := ` INSERT INTO edb_data_predict_calculate_tbz(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(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 := TbzDiv(currentItem.Value, findItem.Value)
					addSql += GetAddSql(edbInfoIdStr, edbCode, av, timestampStr, val)
					isAdd = true
					utils.FileLog.Info("同期找到:" + av + ";" + preDateStr)

					// 实际数据的值
					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 := TbzDiv(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 := TbzDiv(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 := TbzDiv(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 := TbzDiv(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 := TbzDiv(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 {
								//fmt.Println("pre not find:", preDateStr, "i:", i)
							}
						}
						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
}
