package services

import (
	"encoding/json"
	"errors"
	"eta/eta_index_lib/models"
	"eta/eta_index_lib/utils"
	"fmt"
	"github.com/rdlucklib/rdluck_tools/http"
	"net/url"
)

// GetEdbDataFromPb 获取Pb数据
func GetEdbDataFromPb(edbCode, startDate, endDate string) (item *models.EdbDataFromPb, err error) {
	if utils.Hz_Pb_Data_Url == `` {
		err = errors.New("彭博接口未配置")
		return
	}
	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(models.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 *models.EdbDataFromPb, err error) {
	if utils.Hz_Pb_Data_Url == `` {
		err = errors.New("彭博接口未配置")
		return
	}
	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(models.EdbDataFromPb)
	err = json.Unmarshal(body, &item)
	if err != nil {
		utils.FileLog.Info("GetEdbDataByPb Unmarshal Err:" + err.Error())
		return
	}
	edbCode, _ = url.QueryUnescape(edbCode)
	return
}
