package services

import (
	"encoding/json"
	"fmt"
	"github.com/rdlucklib/rdluck_tools/http"
	"hongze/hongze_edb_lib/utils"
	"net/url"
)

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"`
}

// GetEdbDataFromPb 获取Pb数据
func GetEdbDataFromPb(edbCode, startDate, endDate string) (item *EdbDataFromPb, err error) {
	edbCode = url.QueryEscape(edbCode)
	bpUrl := utils.Hz_Pb_Data_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)
	if err != nil {
		utils.FileLog.Info("GetEdbDataByPb Err:" + err.Error())
		return
	}
	utils.FileLog.Info("GetEdbDataByPb result:" + string(body))
	item = new(EdbDataFromPb)
	err = json.Unmarshal(body, &item)
	if err != nil {
		utils.FileLog.Info("GetEdbDataByPb Unmarshal Err:" + err.Error())
		return
	}
	edbCode, _ = url.QueryUnescape(edbCode)
	return
}

// GetEdbDataFromPbFinance 获取Pb财务数据
func GetEdbDataFromPbFinance(companyCode, edbCode, startDate, endDate string) (item *EdbDataFromPb, err error) {
	companyCode = url.QueryEscape(companyCode)
	edbCode = url.QueryEscape(edbCode)
	bpUrl := utils.Hz_Pb_Data_Url + `edbInfo/pb/finance?CompanyCode=%s&EdbCode=%s&StartDate=%s&EndDate=%s`
	bpUrl = fmt.Sprintf(bpUrl, companyCode, edbCode, startDate, endDate)
	utils.FileLog.Info("bpUrl:" + bpUrl)
	body, err := http.Get(bpUrl)
	if err != nil {
		utils.FileLog.Info("GetEdbDataByPb Err:" + err.Error())
		return
	}
	utils.FileLog.Info("GetEdbDataByPb result:" + string(body))
	item = new(EdbDataFromPb)
	err = json.Unmarshal(body, &item)
	if err != nil {
		utils.FileLog.Info("GetEdbDataByPb Unmarshal Err:" + err.Error())
		return
	}
	edbCode, _ = url.QueryUnescape(edbCode)
	return
}
