package data import ( "encoding/json" "eta_gn/eta_task/models" "eta_gn/eta_task/models/data_manage" "eta_gn/eta_task/utils" "fmt" "io/ioutil" "net/http" "strings" ) // 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: // if subSource == 0 { // urlStr = "ths/refresh" // } else { // urlStr = "ths/ds/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_NATIONAL_STATISTICS: // urlStr = "national_statistics/refresh" //case utils.DATA_SOURCE_FUBAO: // urlStr = "fubao/refresh" //case utils.DATA_SOURCE_GFEX: // urlStr = "gz/refresh" //default: // urlStr = data_manage.GetEdbSourceRefreshMethodBySourceId(source) // 没有对应的从edb_source中取 //} urlStr = data_manage.GetEdbSourceRefreshMethodBySourceId(source) // 没有对应的从edb_source中取 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 } // 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), "application/json") if err != nil { return } err = json.Unmarshal(result, &resp) if err != nil { return } return resp, nil } // SetEdbSourceStat 指标终端更新情况定时汇总 func SetEdbSourceStat() (resp *models.BaseResponse, err error) { postUrl := utils.EDB_LIB_URL + "edb_stat/source_update" result, err := HttpPost(postUrl, "", "application/json") if err != nil { return } err = json.Unmarshal(result, &resp) if err != nil { return } return resp, nil } func HttpPost(url, postData 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("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) //fmt.Println("HttpPost:" + string(b)) return b, err }