package models

import (
	"eta/eta_index_lib/utils"
	"fmt"
	"strconv"
	"strings"
	"time"

	"github.com/beego/beego/v2/client/orm"
)

// UN 联合国商品贸易数据指标

// ComTradeCodeMapping 联合国商品贸易数据指标编码与分类编码的关系表
type ComTradeCodeMapping struct {
	Code       int       `orm:"column(code);pk"`
	IndexCode  string    `description:"联合国商品贸易编码"`
	Type       int       `description:"1:价格；2：重量"`
	Name       string    `description:"指标名称"`
	CreateTime time.Time `description:"创建时间"`
}

// GetComTradeIndex 根据指标code获取指标信息
func GetComTradeIndex(indexCode string) (item *ComTradeCodeMapping, err error) {
	o := orm.NewOrm()
	sql := `SELECT * FROM com_trade_code_mapping WHERE code=? `
	err = o.Raw(sql, indexCode).QueryRow(&item)
	return
}

type ComTradeData struct {
	Value    float64 `orm:"column(value)" description:"值"`
	DataTime string  `orm:"column(data_time)" description:"日期"`
}

func GetComTradeDataByTradeCode(tradeCode, startDate string) (items []*ComTradeData, err error) {
	o := orm.NewOrm()
	pars := []interface{}{tradeCode}
	sql1 := ` SELECT * FROM com_trade_data WHERE 1=1 AND index_trade_code = ? `
	if startDate != `` {
		sql1 += `AND data_time>=?`
		pars = append(pars, startDate)
	}
	sql := `select trade_value AS value,data_time from (` + sql1 + ` having 1 order by modify_time DESC ) tmp GROUP BY data_time ORDER BY data_time DESC `
	_, err = o.Raw(sql, pars).QueryRows(&items)
	return
}

func GetComTradeDataByWeightCode(weightCode, startDate string) (items []*ComTradeData, err error) {
	o := orm.NewOrm()
	pars := []interface{}{weightCode}
	sql1 := ` SELECT * FROM com_trade_data WHERE 1=1  AND index_net_weight_code = ? `
	if startDate != `` {
		sql1 += `AND data_time>=?`
		pars = append(pars, startDate)
	}
	sql := `select net_weight_value AS value,data_time from (` + sql1 + ` having 1 order by modify_time DESC ) tmp GROUP BY data_time ORDER BY data_time DESC `
	_, err = o.Raw(sql, pars).QueryRows(&items)
	return
}

func GetComTradeDataByCondition(condition string, pars []interface{}) (item []*ComTradeData, err error) {
	sql1 := ` SELECT * FROM com_trade_data WHERE 1=1  `
	o := orm.NewOrm()
	if condition != "" {
		sql1 += condition
	}
	sql := `select trade_value AS value,data_time from (` + sql1 + ` having 1 order by modify_time DESC ) tmp GROUP BY data_time ORDER BY data_time DESC `
	_, err = o.Raw(sql, pars).QueryRows(&item)
	return
}

// AddEdbDataFromComTrade 新增钢联指标数据
func AddEdbDataFromComTrade(edbCode string) (err error) {
	o := orm.NewOrm()

	// 获取指标关系
	codeMapping, err := GetComTradeIndex(edbCode)
	if err != nil {
		return
	}

	var ComTradeDataList []*ComTradeData
	if codeMapping.Type == 1 || codeMapping.Type == 3 {
		ComTradeDataList, err = GetComTradeDataByTradeCode(edbCode, "")
	} else {
		ComTradeDataList, err = GetComTradeDataByWeightCode(edbCode, "")
	}

	dataLen := len(ComTradeDataList)

	existMap := make(map[string]string)
	if dataLen > 0 {
		var isAdd bool
		addSql := ` INSERT INTO edb_data_com_trade (edb_info_id,edb_code,data_time,value,create_time,modify_time,data_timestamp) values `
		for i := 0; i < dataLen; i++ {
			item := ComTradeDataList[i]
			eDate := item.DataTime
			sValue := utils.SubFloatToString(item.Value, 30)
			if sValue != "" {
				if _, ok := existMap[eDate]; !ok {
					dataTime, err := time.ParseInLocation(utils.FormatDate, eDate, time.Local)
					if err != nil {
						return err
					}
					timestamp := dataTime.UnixNano() / 1e6
					timeStr := fmt.Sprintf("%d", timestamp)
					addSql += GetAddSql("0", edbCode, eDate, timeStr, sValue)
					isAdd = true
				}
			}
			existMap[eDate] = eDate
		}
		if isAdd {
			addSql = strings.TrimRight(addSql, ",")
			utils.FileLog.Info("addSql:" + addSql)
			_, err = o.Raw(addSql).Exec()
			if err != nil {
				return err
			}
		}
	}
	return
}

// RefreshEdbDataFromComTrade 刷新UN世贸组织指标数据
func RefreshEdbDataFromComTrade(edbInfoId int, edbCode, startDate string) (err error) {
	source := utils.DATA_SOURCE_COM_TRADE
	subSource := utils.DATA_SUB_SOURCE_EDB

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

	// 获取指标关系
	codeMapping, err := GetComTradeIndex(edbCode)
	if err != nil {
		return
	}

	var ComTradeDataList []*ComTradeData
	if codeMapping.Type == 1 || codeMapping.Type == 3 {
		ComTradeDataList, err = GetComTradeDataByTradeCode(edbCode, startDate)
	} else {
		ComTradeDataList, err = GetComTradeDataByWeightCode(edbCode, startDate)
	}
	if err != nil {
		return
	}

	// 真实数据的最大日期  ， 插入规则配置的日期
	var realDataMaxDate, edbDataInsertConfigDate time.Time
	var edbDataInsertConfig *EdbDataInsertConfig
	var isFindConfigDateRealData bool //是否找到配置日期的实际数据的值
	{
		edbDataInsertConfig, err = GetEdbDataInsertConfigByEdbId(edbInfoId)
		if err != nil && err.Error() != utils.ErrNoRow() {
			return
		}
		if edbDataInsertConfig != nil {
			edbDataInsertConfigDate = edbDataInsertConfig.Date
		}
	}

	var existCondition string
	var existPars []interface{}

	existCondition += " AND edb_info_id=? "
	existPars = append(existPars, edbInfoId)
	if startDate != "" {
		existCondition += " AND data_time>=? "
		existPars = append(existPars, startDate)
	}
	//获取指标所有数据
	existList, err := GetEdbDataByCondition(source, subSource, existCondition, existPars)
	if err != nil {
		return err
	}
	existMap := make(map[string]*EdbInfoSearchData)
	for _, v := range existList {
		existMap[v.DataTime] = v
	}

	addSql := ` INSERT INTO edb_data_com_trade(edb_info_id,edb_code,data_time,value,create_time,modify_time,data_timestamp) values `
	var isAdd bool
	addMap := make(map[string]string)
	for _, v := range ComTradeDataList {
		item := v
		eDate := item.DataTime
		sValue := utils.SubFloatToString(item.Value, 30)

		dataTime, err := time.ParseInLocation(utils.FormatDate, eDate, time.Local)
		if err != nil {
			return err
		}
		if findItem, ok := existMap[v.DataTime]; !ok {
			if sValue != "" {
				timestamp := dataTime.UnixNano() / 1e6
				timeStr := fmt.Sprintf("%d", timestamp)
				saveValue := sValue

				if _, addOk := addMap[eDate]; !addOk {
					addSql += GetAddSql(edbInfoIdStr, edbCode, eDate, timeStr, saveValue)
					isAdd = true
				}
			}
		} else {
			if findItem != nil && utils.SubFloatToString(findItem.Value, 30) != sValue {
				err = ModifyEdbDataById(source, subSource, findItem.EdbDataId, sValue)
				if err != nil {
					return err
				}
			}
		}
		addMap[v.DataTime] = v.DataTime

		// 下面代码主要目的是处理掉手动插入的数据判断
		{
			if realDataMaxDate.IsZero() || dataTime.After(realDataMaxDate) {
				realDataMaxDate = dataTime
			}
			if edbDataInsertConfigDate.IsZero() || dataTime.Equal(edbDataInsertConfigDate) {
				isFindConfigDateRealData = true
			}
		}
	}

	// 处理手工数据补充的配置
	HandleConfigInsertEdbData(realDataMaxDate, edbDataInsertConfig, edbInfoId, source, subSource, existMap, isFindConfigDateRealData)

	if isAdd {
		addSql = strings.TrimRight(addSql, ",")
		_, err = o.Raw(addSql).Exec()
		if err != nil {
			return err
		}
	}
	return
}
