package data

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

type EdbDataFromThs struct {
	DataVol   int64       `json:"dataVol"`
	Errmsg    string      `json:"errmsg"`
	Errorcode int64       `json:"errorcode"`
	Perf      interface{} `json:"perf"`
	Tables    []struct {
		ID    []string  `json:"id"`
		Time  []string  `json:"time"`
		Value []float64 `json:"value"`
	} `json:"tables"`
}

func GetEdbDataByThs(edbCode, startDate, endDate string) (searchItem *data_manage.EdbInfoSearch, err error) {
	thsUrl := utils.Hz_Server_Data_Url + `edbInfo/ths?EdbCode=%s&StartDate=%s&EndDate=%s`
	thsUrl = fmt.Sprintf(thsUrl, edbCode, startDate, endDate)
	utils.FileLogData.Info("thsUrl:%s", thsUrl)
	body, err := http.Get(thsUrl)
	utils.FileLogData.Info("ths result:%s", string(body))

	if err != nil {
		return
	}
	item := new(EdbDataFromThs)
	err = json.Unmarshal(body, &item)
	if err != nil {
		return
	}
	if item.Errorcode != 0 {
		err = errors.New(string(body))
		return
	}
	searchItem = new(data_manage.EdbInfoSearch)
	searchItem.EdbCode = edbCode
	if len(item.Tables) > 0 {
		table := item.Tables[0]
		dataLen := len(table.Time)

		var isAdd bool
		addSql := ` INSERT INTO edb_data_ths(edb_info_id,edb_code,data_time,value,create_time,modify_time,status,data_timestamp) values `
		nowStr := time.Now().Format(utils.FormatDateTime)
		dataList := make([]*data_manage.EdbInfoSearchData, 0)
		for i := 0; i < dataLen; i++ {
			eDate := table.Time[i]
			sValue := table.Value[i]
			dataTime, err := time.Parse(utils.FormatDate, eDate)
			if err != nil {
				return nil, err
			}
			timestamp := dataTime.UnixNano() / 1e6
			timeStr := fmt.Sprintf("%d", timestamp)
			addSql += "("
			addSql += "0," + "'" + edbCode + "'" + "," + "'" + eDate + "'" + "," + utils.SubFloatToString(sValue, 20) + "," + "'" + nowStr + "'" +
				"," + "'" + nowStr + "'" + "," + "0" + "," + "'" + timeStr + "'"
			addSql += "),"
			isAdd = true
		}
		if isAdd {
			addSql = strings.TrimRight(addSql, ",")
			err = data_manage.AddEdbDataThsBySql(addSql)
			if err != nil {
				utils.FileLogData.Info("AddEdbDataThsBySql Err:%s", err.Error())
				return
			}
		}

		size := utils.EDB_DATA_LIMIT
		dataList, err := data_manage.GetEdbDataThsByCode(edbCode, size)
		if err != nil {
			utils.FileLogData.Info("GetEdbDataThsByCode Err:%s", err.Error())
			return searchItem, err
		}
		minDate, maxDate, err := data_manage.GetEdbDataThsMaxOrMinDate(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 RefreshEdbDataByThs(edbInfoId int, edbCode, startDate, endDate string) (err error) {
	thsUrl := utils.Hz_Server_Data_Url + `edbInfo/ths?EdbCode=%s&StartDate=%s&EndDate=%s`
	thsUrl = fmt.Sprintf(thsUrl, edbCode, startDate, endDate)
	utils.FileLog.Info("thsUrl:%s", thsUrl)
	body, err := http.Get(thsUrl)
	fmt.Println("GetEdbDataByThs body:")
	fmt.Println(string(body))

	if err != nil {
		return
	}
	item := new(EdbDataFromThs)
	err = json.Unmarshal(body, &item)
	if err != nil {
		return
	}
	if item.Errorcode != 0 {
		err = errors.New(string(body))
		return
	}
	if len(item.Tables) > 0 {
		table := item.Tables[0]
		dataLen := len(table.Time)
		addSql := ` INSERT INTO edb_data_ths(edb_info_id,edb_code,data_time,value,create_time,modify_time,status,data_timestamp) values `
		nowStr := time.Now().Format(utils.FormatDateTime)
		var isAdd bool
		for i := 0; i < dataLen; i++ {
			eDate := table.Time[i]
			sValue := table.Value[i]

			count, err := data_manage.GetEdbDataThsByCodeAndDate(edbCode, eDate)
			if err != nil && err.Error() != utils.ErrNoRow() {
				return err
			}
			if count <= 0 {
				dataTime, err := time.Parse(utils.FormatDate, eDate)
				if err != nil {

				}
				timestamp := dataTime.UnixNano() / 1e6
				timeStr := fmt.Sprintf("%d", timestamp)

				addSql += "("
				addSql += strconv.Itoa(edbInfoId) + "," + "'" + edbCode + "'" + "," + "'" + eDate + "'" + "," + utils.SubFloatToString(sValue, 30) + "," + "'" + nowStr + "'" +
					"," + "'" + nowStr + "'" + "," + "1"
				addSql += "," + "'" + timeStr + "'"
				addSql += "),"
				isAdd = true
			} else {
				err = data_manage.ModifyEdbDataThs(int64(edbInfoId), eDate, sValue)
				if err != nil {
					return err
				}
			}
		}

		if isAdd {
			addSql = strings.TrimRight(addSql, ",")
			err = data_manage.AddEdbDataThsBySql(addSql)
			if err != nil {
				fmt.Println("AddEdbDataThs Err", err.Error())
				return
			}
		}
	}
	return
}
