package data

import (
	"encoding/json"
	"fmt"
	"github.com/rdlucklib/rdluck_tools/http"
	"hongze/hz_crm_api/models/data_manage"
	"hongze/hz_crm_api/utils"
	"net/url"
	"strings"
	"time"
)

type EdbDataFromPb struct {
	Date   map[string]int64   `json:"date"`
	Ticker map[string]string  `json:"ticker"`
	Field  map[string]string  `json:"field"`
	Value  map[string]float64 `json:"value"`
}

func GetEdbDataByPb(edbCode, startDate, endDate string) (searchItem *data_manage.EdbInfoSearch, err error) {
	defer func() {
		if err != nil {
			utils.FileLog.Info("GetEdbDataByPb Err:%s", err.Error())
		}
	}()
	edbCode = url.QueryEscape(edbCode)
	bpUrl := utils.Hz_Data_PB_Url + `edbInfo/pb?EdbCode=%s&StartDate=%s&EndDate=%s`
	bpUrl = fmt.Sprintf(bpUrl, edbCode, startDate, endDate)
	utils.FileLog.Info("bpUrl:%s", bpUrl)
	body, err := http.Get(bpUrl)
	if err != nil {
		utils.FileLog.Info("GetEdbDataByPb Err:%s", err.Error())
		return
	}
	utils.FileLog.Info("GetEdbDataByPb result:%s", string(body))
	item := new(EdbDataFromPb)
	err = json.Unmarshal(body, &item)
	if err != nil {
		utils.FileLog.Info("GetEdbDataByPb Unmarshal Err:%s", err.Error())
		return
	}
	edbCode, _ = url.QueryUnescape(edbCode)
	searchItem = new(data_manage.EdbInfoSearch)
	searchItem.EdbCode = edbCode
	if len(item.Date) > 0 {
		var isAdd bool
		dateMap := item.Date
		addSql := ` INSERT INTO edb_data_pb(edb_info_id,edb_code,data_time,value,create_time,modify_time,status,ticker,field,data_timestamp) values `
		nowStr := time.Now().Format(utils.FormatDateTime)
		for k, v := range dateMap {
			timeStr := fmt.Sprintf("%d", v)
			v = v / 1000
			t := time.Unix(v, 0)
			dateTime := t.Format(utils.FormatDate)

			val := item.Value[k]
			field := item.Field[k]
			ticker := item.Ticker[k]

			if field == "PX_LAST" {
				addSql += "("
				addSql += "0," + "'" + edbCode + "'" + "," + "'" + dateTime + "'" + "," + utils.SubFloatToString(val, 20) + "," + "'" + nowStr + "'" +
					"," + "'" + nowStr + "'" + "," + "0" + "," + "'" + ticker + "'" + "," + "'" + field + "'" + "," + "'" + timeStr + "'"
				addSql += "),"
				isAdd = true
			}
		}
		if isAdd {
			addSql = strings.TrimRight(addSql, ",")
			err = data_manage.AddEdbDataPbBySql(addSql)
			if err != nil {
				utils.FileLogData.Info("GetEdbDataByPb AddEdbDataPbBySql Err:%s", err.Error())
				return searchItem, err
			}
		}

		size := utils.EDB_DATA_LIMIT
		dataList, err := data_manage.GetEdbDataPbByCode(edbCode, size)
		if err != nil {
			fmt.Println("GetEdbDataWindByCode Err", err.Error())
			return searchItem, err
		}
		minDate, maxDate, err := data_manage.GetEdbDataPbMaxOrMinDate(edbCode)
		if err != nil {
			return searchItem, err
		}
		searchItem.DataList = dataList
		searchItem.StartDate = minDate
		searchItem.EndDate = maxDate
	}

	if searchItem.DataList == nil {
		searchItem.DataList = make([]*data_manage.EdbInfoSearchData, 0)
	}

	return
}

func RefreshEdbDataByPb(edbInfoId int, edbCode, startDate, endDate string) (err error) {
	refreshEdbCode := edbCode
	edbCode = url.QueryEscape(edbCode)
	bpUrl := utils.Hz_Data_PB_Url + `edbInfo/pb?EdbCode=%s&StartDate=%s&EndDate=%s`
	bpUrl = fmt.Sprintf(bpUrl, edbCode, startDate, endDate)
	utils.FileLog.Info("bpUrl+" + bpUrl)
	body, err := http.Get(bpUrl)
	fmt.Println("RefreshEdbDataByPb body:")
	fmt.Println(string(body))
	utils.FileLog.Info("RefreshEdbDataByPb:" + string(body))

	if err != nil {
		return
	}
	item := new(EdbDataFromPb)
	err = json.Unmarshal(body, &item)
	if err != nil {
		return
	}
	if len(item.Date) > 0 {
		dateMap := item.Date
		pbList := make([]*data_manage.EdbDataPb, 0)
		for k, v := range dateMap {
			timestamp := v
			v = v / 1000
			t := time.Unix(v, 0)
			dateTime := t.Format(utils.FormatDate)

			val := item.Value[k]
			field := item.Field[k]
			ticker := item.Ticker[k]
			if field == "PX_LAST" {
				//判断数据是否已经存在
				count, err := data_manage.GetEdbDataPbByCodeAndDate(refreshEdbCode, dateTime)
				if err != nil && err.Error() != utils.ErrNoRow() {
					return err
				}
				fmt.Println("field:", field)
				if count <= 0 {
					item := new(data_manage.EdbDataPb)
					item.EdbCode, _ = url.QueryUnescape(edbCode)
					item.EdbInfoId = edbInfoId
					item.CreateTime = time.Now()
					item.ModifyTime = time.Now()
					item.DataTime = dateTime
					item.Value = val
					item.Status = 1
					item.Field = field
					item.Ticker = ticker
					item.DataTimestamp = timestamp
					pbList = append(pbList, item)
				} else {
					err = data_manage.ModifyEdbDataPb(edbInfoId, dateTime, val)
					if err != nil {
						return err
					}
				}
			}
		}

		if len(pbList) > 0 {
			err = data_manage.AddEdbDataPb(pbList)
			if err != nil {
				fmt.Println("AddEdbDataPb Err", err.Error())
				return
			}
		}
	}
	return
}
