123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584 |
- package data
- import (
- "encoding/json"
- "eta/eta_api/models"
- "eta/eta_api/models/data_manage"
- "eta/eta_api/models/data_manage/response"
- "eta/eta_api/utils"
- "fmt"
- "io/ioutil"
- "net/http"
- "strings"
- )
- // AddEdbData 新增指标数据
- func AddEdbData(source int, edbCode, frequency string) (resp *models.BaseResponse, err error) {
- param := make(map[string]interface{})
- param["EdbCode"] = edbCode
- param["Source"] = source
- param["Frequency"] = frequency
- urlStr := ``
- switch source {
- case utils.DATA_SOURCE_THS:
- urlStr = "ths/add"
- case utils.DATA_SOURCE_WIND:
- urlStr = "wind/add"
- case utils.DATA_SOURCE_PB:
- urlStr = "pb/add"
- case utils.DATA_SOURCE_PB_FINANCE:
- urlStr = "pb_finance/add"
- case utils.DATA_SOURCE_MANUAL:
- urlStr = "manual/add"
- case utils.DATA_SOURCE_LZ:
- urlStr = "lz/add"
- case utils.DATA_SOURCE_YS:
- urlStr = "smm/add"
- case utils.DATA_SOURCE_GL:
- urlStr = "mysteel/add"
- case utils.DATA_SOURCE_ZZ:
- urlStr = "zz/add"
- case utils.DATA_SOURCE_DL:
- urlStr = "dl/add"
- case utils.DATA_SOURCE_SH:
- urlStr = "sh/add"
- case utils.DATA_SOURCE_CFFEX:
- urlStr = "cffex/add"
- case utils.DATA_SOURCE_SHFE:
- urlStr = "shfe/add"
- case utils.DATA_SOURCE_GIE:
- urlStr = "gie/add"
- case utils.DATA_SOURCE_LT:
- urlStr = "lt/add"
- case utils.DATA_SOURCE_COAL:
- urlStr = "coal/add"
- case utils.DATA_SOURCE_GOOGLE_TRAVEL:
- urlStr = "google_travel/add"
- case utils.DATA_SOURCE_MYSTEEL_CHEMICAL:
- urlStr = "mysteel_chemical/add"
- case utils.DATA_SOURCE_EIA_STEO:
- urlStr = "eia_steo/add"
- case utils.DATA_SOURCE_COM_TRADE:
- urlStr = "com_trade/add"
- case utils.DATA_SOURCE_SCI:
- urlStr = "sci/add"
- case utils.DATA_SOURCE_BAIINFO:
- urlStr = "baiinfo/add"
- case utils.DATA_SOURCE_NATIONAL_STATISTICS:
- urlStr = "national_statistics/add"
- case utils.DATA_SOURCE_FUBAO:
- urlStr = "fubao/add"
- case utils.DATA_SOURCE_GFEX:
- urlStr = "gz/add"
- case utils.DATA_SOURCE_ICPI:
- urlStr = "icpi/add"
- case utils.DATA_SOURCE_SCI99:
- urlStr = "sci99/add"
- case utils.DATA_SOURCE_SCI_HQ:
- urlStr = "sci_hq/add"
- case utils.DATA_SOURCE_LY:
- urlStr = "ly/add"
- default:
- edbSource := data_manage.EdbSourceIdMap[source]
- if edbSource != nil {
- urlStr = edbSource.EdbAddMethod
- }
- }
- if urlStr == "" {
- err = fmt.Errorf("未实现该指标的刷新接口,请联系管理员")
- return
- }
- resp, err = postRefreshEdbData(param, urlStr)
- return
- }
- // AddEdbData 新增指标数据
- func AddEdbDataWindWsd(source int, stockCode, edbCode string) (resp *models.BaseResponse, err error) {
- param := make(map[string]interface{})
- param["EdbCode"] = edbCode
- param["StockCode"] = stockCode
- param["Source"] = source
- urlStr := `wind/wsd/add`
- if urlStr == "" {
- err = fmt.Errorf("未实现该指标的刷新接口,请联系管理员")
- return
- }
- resp, err = postRefreshEdbData(param, urlStr)
- return
- }
- // AddEdbDataThsDs 新增指标数据
- func AddEdbDataThsDs(source int, stockCode, edbCode, extraPars string) (resp *models.BaseResponse, err error) {
- param := make(map[string]interface{})
- param["EdbCode"] = edbCode
- param["StockCode"] = stockCode
- param["Source"] = source
- param["ExtraPars"] = extraPars
- urlStr := `ths/ds/add`
- resp, err = postRefreshEdbData(param, urlStr)
- return
- }
- type AddPredictEdbDataResponse struct {
- Ret int
- Msg string
- ErrMsg string
- ErrCode string
- Data data_manage.AddEdbInfoResp
- Success bool `description:"true 执行成功,false 执行失败"`
- IsSendEmail bool `json:"-" description:"true 发送邮件,false 不发送邮件"`
- IsAddLog bool `json:"-" description:"true 新增操作日志,false 不新增操作日志" `
- }
- // EdbCalculateSaveMultiResponse 批量计算返回值
- type EdbCalculateSaveMultiResponse struct {
- Ret int
- Msg string
- ErrMsg string
- ErrCode string
- Data data_manage.BatchEdbInfoCalculateBatchSaveResp
- Success bool `description:"true 执行成功,false 执行失败"`
- IsSendEmail bool `json:"-" description:"true 发送邮件,false 不发送邮件"`
- IsAddLog bool `json:"-" description:"true 新增操作日志,false 不新增操作日志" `
- }
- // SaveBasePredictEdbData 新增/编辑预测指标运算
- func SaveBasePredictEdbData(edbInfoCalculateBatchSaveReqStr, lang string) (resp *AddPredictEdbDataResponse, err error) {
- _, resultByte, err := postAddEdbData(edbInfoCalculateBatchSaveReqStr, "predict/save", lang)
- err = json.Unmarshal(resultByte, &resp)
- if err != nil {
- return
- }
- return
- }
- type PredictRuleCalculateByNineRespResponse struct {
- Ret int
- Msg string
- ErrMsg string
- ErrCode string
- Data response.PredictRuleCalculateByNineResp
- Success bool `description:"true 执行成功,false 执行失败"`
- IsSendEmail bool `json:"-" description:"true 发送邮件,false 不发送邮件"`
- IsAddLog bool `json:"-" description:"true 新增操作日志,false 不新增操作日志" `
- }
- // PredictCalculateByNinePreview 预测指标动态环差演算
- func PredictCalculateByNinePreview(RuleConfigReqStr, lang string) (resp *PredictRuleCalculateByNineRespResponse, err error) {
- _, resultByte, err := postAddEdbData(RuleConfigReqStr, "predict/calculate_by_nine/preview", lang)
- err = json.Unmarshal(resultByte, &resp)
- if err != nil {
- return
- }
- return
- }
- // SavePredictEdbData 新增/编辑预测指标运算
- func SavePredictEdbData(edbInfoCalculateBatchSaveReqStr, lang string) (resp *AddPredictEdbDataResponse, err error) {
- _, resultByte, err := postAddEdbData(edbInfoCalculateBatchSaveReqStr, "predict_calculate/save", lang)
- err = json.Unmarshal(resultByte, &resp)
- if err != nil {
- return
- }
- return
- }
- // BatchSavePredictEdbData 新增/编辑 预测指标运算(同比、同差)
- func BatchSavePredictEdbData(edbInfoCalculateBatchSaveReqStr, lang string) (resp *AddPredictEdbDataResponse, err error) {
- _, resultByte, err := postAddEdbData(edbInfoCalculateBatchSaveReqStr, "predict_calculate/batch/save", lang)
- if err != nil {
- return
- }
- err = json.Unmarshal(resultByte, &resp)
- if err != nil {
- return
- }
- return
- }
- // RefreshEdbData 刷新指标数据
- func RefreshEdbData(edbInfoId, source, subSource int, edbCode, startDate string) (resp *models.BaseResponse, err error) {
- param := make(map[string]interface{})
- param["EdbCode"] = edbCode
- param["EdbInfoId"] = edbInfoId
- param["StartDate"] = startDate
- param["Source"] = source
- urlStr := ``
- switch source {
- case utils.DATA_SOURCE_THS:
- switch subSource {
- case utils.DATA_SUB_SOURCE_DATE:
- urlStr = "ths/ds/refresh"
- case utils.DATA_SUB_SOURCE_HIGH_FREQUENCY:
- urlStr = "ths/hf/edb/refresh"
- default:
- urlStr = "ths/refresh"
- }
- case utils.DATA_SOURCE_WIND:
- if subSource == 0 {
- urlStr = "wind/refresh"
- } else {
- urlStr = "wind/wsd/refresh"
- }
- case utils.DATA_SOURCE_PB:
- urlStr = "pb/refresh"
- case utils.DATA_SOURCE_PB_FINANCE:
- urlStr = "pb_finance/refresh"
- case utils.DATA_SOURCE_MANUAL:
- urlStr = "manual/refresh"
- case utils.DATA_SOURCE_LZ:
- urlStr = "lz/refresh"
- case utils.DATA_SOURCE_YS:
- urlStr = "smm/refresh"
- case utils.DATA_SOURCE_GL:
- urlStr = "mysteel/refresh"
- case utils.DATA_SOURCE_ZZ:
- urlStr = "zz/refresh"
- case utils.DATA_SOURCE_DL:
- urlStr = "dl/refresh"
- case utils.DATA_SOURCE_SH:
- urlStr = "sh/refresh"
- case utils.DATA_SOURCE_CFFEX:
- urlStr = "cffex/refresh"
- case utils.DATA_SOURCE_SHFE:
- urlStr = "shfe/refresh"
- case utils.DATA_SOURCE_GIE:
- urlStr = "gie/refresh"
- case utils.DATA_SOURCE_LT:
- urlStr = "lt/refresh"
- case utils.DATA_SOURCE_COAL:
- urlStr = "coal/refresh"
- case utils.DATA_SOURCE_GOOGLE_TRAVEL:
- urlStr = "google_travel/refresh"
- case utils.DATA_SOURCE_MYSTEEL_CHEMICAL:
- urlStr = "mysteel_chemical/refresh"
- case utils.DATA_SOURCE_EIA_STEO:
- urlStr = "eia_steo/refresh"
- case utils.DATA_SOURCE_PREDICT:
- urlStr = "predict/refresh"
- case utils.DATA_SOURCE_COM_TRADE:
- urlStr = "com_trade/refresh"
- case utils.DATA_SOURCE_SCI:
- urlStr = "sci/refresh"
- case utils.DATA_SOURCE_BAIINFO:
- urlStr = "baiinfo/refresh"
- case utils.DATA_SOURCE_STOCK_PLANT:
- urlStr = "stock_plant/refresh"
- case utils.DATA_SOURCE_NATIONAL_STATISTICS:
- urlStr = "national_statistics/refresh"
- case utils.DATA_SOURCE_FUBAO:
- urlStr = "fubao/refresh"
- case utils.DATA_SOURCE_GFEX:
- urlStr = "gz/refresh"
- case utils.DATA_SOURCE_ICPI:
- urlStr = "icpi/refresh"
- case utils.DATA_SOURCE_SCI99:
- urlStr = "sci99/refresh"
- case utils.DATA_SOURCE_SCI_HQ:
- urlStr = "sci_hq/refresh"
- case utils.DATA_SOURCE_LY:
- urlStr = "ly/refresh"
- case utils.DATA_SOURCE_TRADE_ANALYSIS:
- urlStr = "trade_analysis/edb/refresh"
- default:
- edbSource := data_manage.EdbSourceIdMap[source]
- if edbSource != nil {
- urlStr = edbSource.EdbRefreshMethod
- }
- }
- if urlStr == "" {
- err = fmt.Errorf(fmt.Sprint("source:", source, ";未实现该指标的刷新接口,请联系管理员"))
- return
- }
- resp, err = postRefreshEdbData(param, urlStr)
- return
- }
- // RefreshEdbCalculateData 刷新普通计算指标数据请求
- func RefreshEdbCalculateData(edbInfoId int, edbCode, startDate string) (resp *models.BaseResponse, err error) {
- param := make(map[string]interface{})
- param["EdbCode"] = edbCode
- param["EdbInfoId"] = edbInfoId
- param["StartDate"] = startDate
- resp, err = postRefreshEdbData(param, "calculate/refresh")
- return
- }
- // RefreshPredictEdbCalculateData 刷新 预测计算指标 数据请求
- func RefreshPredictEdbCalculateData(edbInfoId int, edbCode, startDate string) (resp *models.BaseResponse, err error) {
- param := make(map[string]interface{})
- param["EdbCode"] = edbCode
- param["EdbInfoId"] = edbInfoId
- param["StartDate"] = startDate
- resp, err = postRefreshEdbData(param, "predict_calculate/refresh")
- return
- }
- // AddEdbCalculateData 新增 累计值转月-同比值-同差等计算新增
- func AddEdbCalculateData(edbInfoCalculateBatchSaveReqStr, lang string) (resp *AddPredictEdbDataResponse, err error) {
- _, resultByte, err := postAddEdbData(edbInfoCalculateBatchSaveReqStr, "calculate/add", lang)
- err = json.Unmarshal(resultByte, &resp)
- if err != nil {
- return
- }
- return
- }
- // EditEdbCalculateData 编辑 累计值转月-同比值-同差等计算新增
- func EditEdbCalculateData(edbInfoCalculateBatchSaveReqStr, lang string) (resp *AddPredictEdbDataResponse, err error) {
- _, resultByte, err := postAddEdbData(edbInfoCalculateBatchSaveReqStr, "calculate/edit", lang)
- err = json.Unmarshal(resultByte, &resp)
- if err != nil {
- return
- }
- return
- }
- // BatchSaveEdbCalculateData 新增 累计值转月-同比值-同差等计算新增
- func BatchSaveEdbCalculateData(edbInfoCalculateBatchSaveReqStr, lang string) (resp *AddPredictEdbDataResponse, err error) {
- _, resultByte, err := postAddEdbData(edbInfoCalculateBatchSaveReqStr, "calculate/batch/save", lang)
- err = json.Unmarshal(resultByte, &resp)
- if err != nil {
- return
- }
- return
- }
- // BatchEditEdbCalculateData 编辑 累计值转月-同比值-同差等计算新增
- func BatchEditEdbCalculateData(edbInfoCalculateBatchSaveReqStr, lang string) (resp *AddPredictEdbDataResponse, err error) {
- _, resultByte, err := postAddEdbData(edbInfoCalculateBatchSaveReqStr, "calculate/batch/edit", lang)
- err = json.Unmarshal(resultByte, &resp)
- if err != nil {
- return
- }
- return
- }
- // BatchSaveEdbCalculateMultiData 批量新增 累计值转月-同比值-同差等计算新增
- func BatchSaveEdbCalculateMultiData(edbInfoCalculateBatchSaveReqStr, lang string) (resp *EdbCalculateSaveMultiResponse, err error) {
- _, resultByte, err := postAddEdbData(edbInfoCalculateBatchSaveReqStr, "calculate/batch/save/multi", lang)
- err = json.Unmarshal(resultByte, &resp)
- if err != nil {
- return
- }
- return
- }
- // BatchEditEdbCalculateMultiData 批量编辑 累计值转月-同比值-同差等计算新增
- func BatchEditEdbCalculateMultiData(edbInfoCalculateBatchSaveReqStr, lang string) (resp *EdbCalculateSaveMultiResponse, err error) {
- _, resultByte, err := postAddEdbData(edbInfoCalculateBatchSaveReqStr, "calculate/batch/edit/multi", lang)
- err = json.Unmarshal(resultByte, &resp)
- if err != nil {
- return
- }
- return
- }
- //// EditEdbCalculateData 修改计算指标数据请求
- //func EditEdbCalculateData(edbInfoCalculateBatchSaveReqStr string) (resp *models.BaseResponse, err error) {
- // resp, _, err = postAddEdbData(edbInfoCalculateBatchSaveReqStr, "batch/save")
- // return
- //}
- // ExecPythonCode 执行python代码
- func ExecPythonCode(pythonCode string) (resp *models.BaseResponse, err error) {
- param := make(map[string]interface{})
- param["PythonCode"] = pythonCode
- urlStr := `/python/exec`
- resp, err = postRefreshEdbData(param, urlStr)
- return
- }
- // AddPythonEdbData 新增python指标
- func AddPythonEdbData(param, lang string) (resp *models.BaseResponse, err error) {
- urlStr := ``
- urlStr = "python/add"
- resp, _, err = postAddEdbData(param, urlStr, lang)
- return
- }
- // EditPythonEdbData 编辑python指标
- func EditPythonEdbData(param, lang string) (resp *models.BaseResponse, err error) {
- urlStr := ``
- urlStr = "python/edit"
- resp, _, err = postAddEdbData(param, urlStr, lang)
- return
- }
- // SaveAdjustEdbInfo 保存数据调整指标
- func SaveAdjustEdbInfo(param, lang string) (resp *models.BaseResponse, err error) {
- urlStr := ``
- urlStr = "calculate/adjust/save"
- resp, _, err = postAddEdbData(param, urlStr, lang)
- return
- }
- // ResetCustomAnalysisData 重置自定义表格的数据
- func ResetCustomAnalysisData(reqStr, lang string) (resp *AddPredictEdbDataResponse, err error) {
- _, resultByte, err := postAddEdbData(reqStr, "calculate/custom_analysis/reset", lang)
- err = json.Unmarshal(resultByte, &resp)
- if err != nil {
- return
- }
- return
- }
- // CalculateComputeCorrelationResp 拟合残差计算相关性的值返回
- type CalculateComputeCorrelationResp struct {
- Ret int
- Msg string
- ErrMsg string
- ErrCode string
- Data string
- Success bool `description:"true 执行成功,false 执行失败"`
- IsSendEmail bool `json:"-" description:"true 发送邮件,false 不发送邮件"`
- IsAddLog bool `json:"-" description:"true 新增操作日志,false 不新增操作日志" `
- }
- // CalculateComputeCorrelation 拟合残差计算相关性的值
- func CalculateComputeCorrelation(edbInfoCalculateBatchSaveReqStr, lang string) (resp *CalculateComputeCorrelationResp, err error) {
- _, resultByte, err := postAddEdbData(edbInfoCalculateBatchSaveReqStr, "calculate/compute_correlation", lang)
- err = json.Unmarshal(resultByte, &resp)
- if err != nil {
- return
- }
- return
- }
- // PredictCalculateComputeCorrelation 拟合残差计算相关性的值(预测指标)
- func PredictCalculateComputeCorrelation(edbInfoCalculateBatchSaveReqStr, lang string) (resp *CalculateComputeCorrelationResp, err error) {
- _, resultByte, err := postAddEdbData(edbInfoCalculateBatchSaveReqStr, "predict_calculate/compute_correlation", lang)
- err = json.Unmarshal(resultByte, &resp)
- if err != nil {
- return
- }
- return
- }
- // BaseCalculateResp 拟合残差计算相关性的值返回
- type BaseCalculateResp struct {
- Ret int
- Msg string
- ErrMsg string
- ErrCode string
- Data BaseCalculateDataResp
- Success bool `description:"true 执行成功,false 执行失败"`
- IsSendEmail bool `json:"-" description:"true 发送邮件,false 不发送邮件"`
- IsAddLog bool `json:"-" description:"true 新增操作日志,false 不新增操作日志" `
- }
- // BaseCalculateDataResp
- // @Description: 基础计算的返回结果
- type BaseCalculateDataResp struct {
- DataMap map[string]float64
- DateList []string
- }
- // BaseCalculate 基础计算
- func BaseCalculate(param, lang string) (resp *BaseCalculateResp, err error) {
- urlStr := "calculate/base"
- _, resultByte, err := postAddEdbData(param, urlStr, lang)
- err = json.Unmarshal(resultByte, &resp)
- if err != nil {
- return
- }
- return
- }
- // AddBaseEdbInfo 新增基础指标
- func AddBaseEdbInfo(addBaseEdbInfoReqStr string, source, subSource int, lang string) (resp *AddPredictEdbDataResponse, err error) {
- urlStr := ``
- switch source {
- case utils.DATA_SOURCE_BUSINESS:
- urlStr = "business_index/add"
- case utils.DATA_SOURCE_THS:
- if subSource == utils.DATA_SUB_SOURCE_HIGH_FREQUENCY {
- urlStr = "ths/hf/edb/add"
- }
- default:
- edbSource := data_manage.EdbSourceIdMap[source]
- if edbSource != nil {
- urlStr = edbSource.EdbRefreshMethod
- }
- }
- if urlStr == "" {
- err = fmt.Errorf(fmt.Sprint("source:", source, ";未实现该指标的刷新接口,请联系管理员"))
- return
- }
- _, resultByte, err := postAddEdbData(addBaseEdbInfoReqStr, urlStr, lang)
- if err != nil {
- return
- }
- err = json.Unmarshal(resultByte, &resp)
- return
- }
- // postRefreshEdbData 刷新指标数据
- func postRefreshEdbData(param map[string]interface{}, urlStr string) (resp *models.BaseResponse, err error) {
- postUrl := utils.EDB_LIB_URL + urlStr
- postData, err := json.Marshal(param)
- if err != nil {
- return
- }
- result, err := HttpPost(postUrl, string(postData), utils.ZhLangVersion, "application/json")
- if err != nil {
- return
- }
- utils.FileLog.Info("postRefreshEdbData:" + postUrl + ";" + string(postData) + ";result:" + string(result))
- err = json.Unmarshal(result, &resp)
- if err != nil {
- return
- }
- return resp, nil
- }
- // postAddEdbData 新增指标数据
- func postAddEdbData(paramStr string, urlStr, lang string) (resp *models.BaseResponse, result []byte, err error) {
- postUrl := utils.EDB_LIB_URL + urlStr
- result, err = HttpPost(postUrl, paramStr, lang, "application/json")
- if err != nil {
- return
- }
- err = json.Unmarshal(result, &resp)
- if err != nil {
- return
- }
- return
- }
- func HttpPost(url, postData, lang string, params ...string) ([]byte, error) {
- body := ioutil.NopCloser(strings.NewReader(postData))
- client := &http.Client{}
- req, err := http.NewRequest("POST", url, body)
- if err != nil {
- return nil, err
- }
- contentType := "application/x-www-form-urlencoded;charset=utf-8"
- if len(params) > 0 && params[0] != "" {
- contentType = params[0]
- }
- req.Header.Set("Content-Type", contentType)
- req.Header.Set("Lang", lang)
- req.Header.Set("authorization", utils.MD5(utils.APP_EDB_LIB_NAME_EN+utils.EDB_LIB_Md5_KEY))
- resp, err := client.Do(req)
- if err != nil {
- return nil, err
- }
- defer resp.Body.Close()
- b, err := ioutil.ReadAll(resp.Body)
- utils.FileLog.Debug("HttpPost:" + string(b))
- return b, err
- }
- // BaseStepCalculate 基础运算-多步骤
- func BaseStepCalculate(param, lang string) (resp *BaseCalculateResp, err error) {
- urlStr := "calculate/base/step_calculate"
- _, resultByte, err := postAddEdbData(param, urlStr, lang)
- err = json.Unmarshal(resultByte, &resp)
- if err != nil {
- return
- }
- return
- }
|