package models

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

// BaseFromBloombergIndex Bloomberg原始指标
type BaseFromBloombergIndex struct {
	BaseFromBloombergIndexId int       `orm:"column(base_from_bloomberg_index_id);pk"`
	IndexCode                string    `description:"指标编码"`
	IndexName                string    `description:"指标名称"`
	Unit                     string    `description:"单位"`
	Source                   int       `description:"来源"`
	Frequency                string    `description:"频度"`
	StartDate                time.Time `description:"开始时间"`
	EndDate                  time.Time `description:"结束时间"`
	Describe                 string    `description:"描述"`
	Sort                     int       `description:"排序"`
	IsStop                   int       `description:"是否停更：0-否；1-停更"`
	EdbExist                 int       `description:"指标库是否已添加：0-否；1-是"`
	TerminalCode             string    `description:"所属终端编码"`
	FilePath                 string    `description:"文件存储路径"`
	CreateTime               time.Time `description:"创建时间"`
	ModifyTime               time.Time `description:"修改时间"`
}

func (m *BaseFromBloombergIndex) TableName() string {
	return "base_from_bloomberg_index"
}

func (m *BaseFromBloombergIndex) Create() (err error) {
	o := orm.NewOrm()
	id, err := o.Insert(m)
	if err != nil {
		return
	}
	m.BaseFromBloombergIndexId = int(id)
	return
}

func (m *BaseFromBloombergIndex) Update(cols []string) (err error) {
	o := orm.NewOrm()
	_, err = o.Update(m, cols...)
	return
}

func GetBaseFromBloombergIndexByCode(indexCode string) (item *BaseFromBloombergIndex, err error) {
	o := orm.NewOrm()
	sql := `SELECT * FROM base_from_bloomberg_index WHERE index_code = ? LIMIT 1`
	err = o.Raw(sql, indexCode).QueryRow(&item)
	return
}

// BaseFromBloombergData Bloomberg原始数据
type BaseFromBloombergData struct {
	BaseFromBloombergDataId  int       `orm:"column(base_from_bloomberg_data_id);pk"`
	BaseFromBloombergIndexId int       `description:"指标ID"`
	IndexCode                string    `description:"指标编码"`
	DataTime                 time.Time `description:"数据日期"`
	Value                    float64   `description:"数据值"`
	CreateTime               time.Time `description:"创建时间"`
	ModifyTime               time.Time `description:"修改时间"`
	DataTimestamp            int       `description:"数据日期时间戳"`
}

func (m *BaseFromBloombergData) TableName() string {
	return "base_from_bloomberg_data"
}

func GetBaseFromBloombergDataByCondition(condition string, pars []interface{}) (items []*BaseFromBloombergData, err error) {
	sub := `SELECT * FROM base_from_bloomberg_data WHERE 1=1  `
	o := orm.NewOrm()
	if condition != "" {
		sub += condition
	}
	sql := `SELECT * FROM (` + sub + ` 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 MultiInsertOrUpdateBaseFromBloombergData(inserts, updates []*BaseFromBloombergData) (err error) {
	o := orm.NewOrm()
	if len(inserts) > 0 {
		_, e := o.InsertMulti(1000, inserts)
		if e != nil {
			err = fmt.Errorf("insert multi err: %s", e.Error())
			return
		}
	}
	if len(updates) > 0 {
		p, e := o.Raw("UPDATE base_from_bloomberg_data SET value = ?, modify_time = NOW() WHERE index_code = ? AND data_time = ?").Prepare()
		if e != nil {
			err = fmt.Errorf("prepare err: %s", e.Error())
			return
		}
		defer func() {
			_ = p.Close()
		}()
		for _, v := range updates {
			_, e = p.Exec(v.Value, v.IndexCode, v.DataTime.Format(utils.FormatDate))
			if e != nil {
				err = fmt.Errorf("update err: %s", e.Error())
				return
			}
		}
	}
	return
}

func GetBaseFromBloombergIndexMinMax(indexCode string) (item *EdbInfoMaxAndMinInfo, err error) {
	o := orm.NewOrm()
	sql := `SELECT MIN(data_time) AS min_date,MAX(data_time) AS max_date,MIN(value) AS min_value,MAX(value) AS max_value FROM base_from_bloomberg_data WHERE index_code = ? `
	err = o.Raw(sql, indexCode).QueryRow(&item)
	if err != nil {
		return
	}

	// 获取最新值
	var lastVal float64
	sql = ` SELECT value AS latest_value FROM base_from_bloomberg_data WHERE index_code = ? ORDER BY data_time DESC LIMIT 1 `
	err = o.Raw(sql, indexCode).QueryRow(&lastVal)
	if err != nil {
		return
	}
	item.LatestValue = lastVal
	return
}

func ModifyBaseFromBloombergIndexMinMax(indexCode string, item *EdbInfoMaxAndMinInfo) (err error) {
	o := orm.NewOrm()
	sql := ` UPDATE base_from_bloomberg_index SET start_date = ?, end_date = ?, modify_time = NOW() WHERE index_code = ? `
	_, err = o.Raw(sql, item.MinDate, item.MaxDate, indexCode).Exec()
	return
}

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

func GetBloombergDataByCondition(condition string, pars []interface{}) (item []*BloombergData, err error) {
	sql1 := ` SELECT * FROM base_from_bloomberg_data WHERE 1=1  `
	o := orm.NewOrm()
	if condition != "" {
		sql1 += condition
	}
	sql := `select * 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
}

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

	var condition string
	var pars []interface{}

	if edbCode != "" {
		condition += " AND index_code = ? "
		pars = append(pars, edbCode)
	}

	bloombergDataList, err := GetBloombergDataByCondition(condition, pars)
	if err != nil {
		return
	}

	dataLen := len(bloombergDataList)

	existMap := make(map[string]string)
	if dataLen > 0 {
		var isAdd bool
		addSql := ` INSERT INTO edb_data_bloomberg (edb_info_id,edb_code,data_time,value,create_time,modify_time,data_timestamp) values `
		for i := 0; i < dataLen; i++ {
			item := bloombergDataList[i]
			eDate := item.DataTime
			sValue := utils.SubFloatToString(item.InputValue, 4)
			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
}

// RefreshEdbDataFromBloomberg 刷新Bloomberg指标数据
func RefreshEdbDataFromBloomberg(edbInfoId int, edbCode, startDate string) (err error) {
	source := utils.DATA_SOURCE_BLOOMBERG
	subSource := utils.DATA_SUB_SOURCE_EDB

	o := orm.NewOrm()
	if err != nil {
		return
	}
	edbInfoIdStr := strconv.Itoa(edbInfoId)
	//计算数据
	var condition string
	var pars []interface{}

	if edbCode != "" {
		condition += " AND index_code=? "
		pars = append(pars, edbCode)
	}

	if startDate != "" {
		condition += " AND data_time>=? "
		pars = append(pars, startDate)
	}

	bloombergDataList, err := GetBloombergDataByCondition(condition, pars)
	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_bloomberg(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 bloombergDataList {
		item := v
		eDate := item.DataTime
		sValue := utils.SubFloatToString(item.InputValue, 4)

		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, 4) != 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
}

// BridgePCSGBloombergResultData 中石油新加坡-指标接口响应体
type BridgePCSGBloombergResultData struct {
	Code int                                `json:"code" description:"状态码"`
	Msg  string                             `json:"msg" description:"提示信息"`
	Data []BaseFromBloombergApiIndexAndData `json:"data" description:"返回数据"`
}

// BaseFromBloombergApiIndexAndData Bloomberg原始指标及数据
type BaseFromBloombergApiIndexAndData struct {
	BaseFromBloombergIndexId int                             `description:"指标ID"`
	IndexCode                string                          `description:"指标编码"`
	IndexName                string                          `description:"指标名称"`
	Unit                     string                          `description:"单位"`
	Source                   string                          `description:"来源"`
	Frequency                string                          `description:"频度"`
	CreateTime               time.Time                       `description:"创建时间"`
	ModifyTime               time.Time                       `description:"修改时间"`
	Data                     []BaseFromBloombergApiIndexData `description:"数据列表"`
}

// BaseFromBloombergApiIndexData Bloomberg原始指标数据
type BaseFromBloombergApiIndexData struct {
	DataTime time.Time `description:"数据日期"`
	Value    float64   `description:"数据值"`
}

// PCSGImportHistoryDataReq 导入历史数据
type PCSGImportHistoryDataReq struct {
	IndexCode string                `description:"指标编码"`
	DataMap   map[time.Time]float64 `description:"数据日期/值"`
}
