package data_manage

import (
	"encoding/json"
	"fmt"
	"github.com/beego/beego/v2/client/orm"
	"github.com/rdlucklib/rdluck_tools/http"
	"hongze/hz_crm_api/utils"
	"reflect"
	"strconv"
	"strings"
	"time"
)

type EdbDataLt struct {
	EdbDataId     int `orm:"column(edb_data_id);pk"`
	EdbInfoId     int
	EdbCode       string
	DataTime      string
	Value         float64
	Status        int
	CreateTime    time.Time
	ModifyTime    time.Time
	DataTimestamp int64
}

// AddEdbDataLtBySql 执行添加数据的sql
func AddEdbDataLtBySql(sqlStr string) (err error) {
	o := orm.NewOrmUsingDB("data")
	_, err = o.Raw(sqlStr).Exec()
	return
}

// ModifyEdbDataLt 修改路透社数据
func ModifyEdbDataLt(edbInfoId int64, dataTime string, value float64) (err error) {
	o := orm.NewOrmUsingDB("data")
	sql := ` UPDATE edb_data_lt SET value=?,modify_time=NOW() WHERE edb_info_id=? AND data_time=? `
	_, err = o.Raw(sql, value, edbInfoId, dataTime).Exec()
	return
}

func GetEdbDataLtMaxOrMinDate(edbCode string) (min_date, max_date string, err error) {
	o := orm.NewOrmUsingDB("data")
	sql := ` SELECT MIN(data_time) AS min_date,MAX(data_time) AS max_date FROM edb_data_lt WHERE edb_code=? `
	err = o.Raw(sql, edbCode).QueryRow(&min_date, &max_date)
	return
}

func GetEdbDataLtByCodeAndDate(edbCode string, startDate string) (count int, err error) {
	o := orm.NewOrmUsingDB("data")
	sql := ` SELECT COUNT(1) AS count FROM edb_data_lt WHERE edb_code=? AND data_time=? `
	err = o.Raw(sql, edbCode, startDate).QueryRow(&count)
	return
}

func GetEdbDataLtByCode(edbCode string, size int) (items []*EdbInfoSearchData, err error) {
	o := orm.NewOrmUsingDB("data")
	sql := ` SELECT * FROM edb_data_lt WHERE edb_code=? ORDER BY data_time DESC LIMIT ? `
	_, err = o.Raw(sql, edbCode, size).QueryRows(&items)
	return
}

// 刷新所有数据
func RefreshAllEdbDataByLt(edbInfoId, source int, edbCode, startDate, endDate string) (err error) {
	// 获取路透社数据
	ltDataList, err := QueryEdbDataByLt(edbCode, startDate, endDate)
	if err != nil {
		return
	}

	o := orm.NewOrmUsingDB("data")
	to, err := o.Begin()
	if err != nil {
		return
	}
	defer func() {
		if err != nil {
			_ = to.Rollback()
		} else {
			_ = to.Commit()
		}
	}()

	//获取指标所有数据
	dataList := make([]*EdbDataBase, 0)
	dataTableName := GetEdbDataTableName(source)
	sql := `SELECT * FROM %s WHERE edb_info_id=? `
	sql = fmt.Sprintf(sql, dataTableName)
	_, err = to.Raw(sql, edbInfoId).QueryRows(&dataList)
	if err != nil {
		return err
	}
	dataMap := make(map[string]string)
	for _, v := range dataList {
		dataMap[v.DataTime] = v.Value
	}
	edbInfoIdStr := strconv.Itoa(edbInfoId)

	addSql := ` INSERT INTO edb_data_lt(edb_info_id,edb_code,data_time,value,create_time,modify_time,status,data_timestamp) values `
	var isAdd bool

	for timestampInt, edbValue := range ltDataList {
		dataTime := time.Unix(timestampInt/1000, 0)

		//校验数据类型对不对
		valType := reflect.TypeOf(edbValue)
		if valType == nil {
			continue
		}
		if valType.String() != "float64" {
			continue
		}
		sValue := edbValue.(float64)

		eDate := dataTime.Format(utils.FormatDate)
		if err != nil {
			return err
		}

		saveValue := utils.SubFloatToString(sValue, 30)
		if existVal, ok := dataMap[eDate]; !ok {
			timestamp := dataTime.UnixNano() / 1e6
			timeStr := fmt.Sprintf("%d", timestamp)

			addSql += GetAddSql(edbInfoIdStr, edbCode, eDate, timeStr, saveValue)
			isAdd = true
		} else {
			if existVal != saveValue {
				sql := ` UPDATE %s SET value=?,modify_time=NOW() WHERE edb_info_id=? AND data_time=? `
				sql = fmt.Sprintf(sql, dataTableName)
				_, err = to.Raw(sql, sValue, edbInfoId, eDate).Exec()
				if err != nil {
					return err
				}
			}
		}
	}
	if isAdd {
		addSql = strings.TrimRight(addSql, ",")
		_, err = to.Raw(addSql).Exec()
		if err != nil {
			fmt.Println("RefreshAllEdbDataByLt add Err", err.Error())
			return
		}
	}
	return
}

type EdbDataFromLt struct {
	Close map[int64]interface{} `json:"CLOSE"`
}

// QueryEdbDataByLt 获取路透社数据
func QueryEdbDataByLt(edbCode, startDate, endDate string) (dataList map[int64]interface{}, err error) {
	dataList = make(map[int64]interface{})

	ltUrl := utils.Hz_Data_LT_Url + `edbInfo/ek?EdbCode=%s&StartDate=%s&EndDate=%s`
	ltUrl = fmt.Sprintf(ltUrl, edbCode, startDate, endDate)
	utils.FileLogData.Info("ltUrl:%s", ltUrl)
	body, err := http.Get(ltUrl)
	utils.FileLogData.Info("lt result:%s", string(body))

	if err != nil {
		return
	}
	//fmt.Println(string(body))
	item := new(EdbDataFromLt)
	err = json.Unmarshal(body, &item)

	if err != nil {
		return
	}
	dataList = item.Close
	return
}
