package data import ( "encoding/json" "fmt" "hongze/hz_crm_api/models" "hongze/hz_crm_api/models/data_manage" "hongze/hz_crm_api/utils" "io/ioutil" "net/http" "strings" ) // AddEdbData 新增指标数据 func AddEdbData(source int, edbCode string) (resp *models.BaseResponse, err error) { param := make(map[string]interface{}) param["EdbCode"] = edbCode 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" default: } if urlStr == "" { err = fmt.Errorf("未实现该指标的刷新接口,请联系管理员") return } 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 不新增操作日志" ` } // SaveBasePredictEdbData 新增/编辑预测指标运算 func SaveBasePredictEdbData(edbInfoCalculateBatchSaveReqStr string) (resp *AddPredictEdbDataResponse, err error) { _, resultByte, err := postAddEdbData(edbInfoCalculateBatchSaveReqStr, "predict/save") err = json.Unmarshal(resultByte, &resp) if err != nil { return } return } // SavePredictEdbData 新增/编辑预测指标运算 func SavePredictEdbData(edbInfoCalculateBatchSaveReqStr string) (resp *AddPredictEdbDataResponse, err error) { _, resultByte, err := postAddEdbData(edbInfoCalculateBatchSaveReqStr, "predict_calculate/save") err = json.Unmarshal(resultByte, &resp) if err != nil { return } return } // BatchSavePredictEdbData 新增/编辑 预测指标运算(同比、同差) func BatchSavePredictEdbData(edbInfoCalculateBatchSaveReqStr string) (resp *AddPredictEdbDataResponse, err error) { _, resultByte, err := postAddEdbData(edbInfoCalculateBatchSaveReqStr, "predict_calculate/batch/save") if err != nil { return } err = json.Unmarshal(resultByte, &resp) if err != nil { return } return } // RefreshEdbData 刷新指标数据 func RefreshEdbData(edbInfoId, source int, edbCode, startDate string) (resp *models.BaseResponse, err error) { param := make(map[string]interface{}) param["EdbCode"] = edbCode param["EdbInfoId"] = edbInfoId param["StartDate"] = startDate urlStr := `` switch source { case utils.DATA_SOURCE_THS: urlStr = "ths/refresh" case utils.DATA_SOURCE_WIND: urlStr = "wind/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" } 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 string) (resp *AddPredictEdbDataResponse, err error) { _, resultByte, err := postAddEdbData(edbInfoCalculateBatchSaveReqStr, "calculate/add") err = json.Unmarshal(resultByte, &resp) if err != nil { return } return } // EditEdbCalculateData 编辑 累计值转月-同比值-同差等计算新增 func EditEdbCalculateData(edbInfoCalculateBatchSaveReqStr string) (resp *AddPredictEdbDataResponse, err error) { _, resultByte, err := postAddEdbData(edbInfoCalculateBatchSaveReqStr, "calculate/edit") err = json.Unmarshal(resultByte, &resp) if err != nil { return } return } // BatchSaveEdbCalculateData 新增 累计值转月-同比值-同差等计算新增 func BatchSaveEdbCalculateData(edbInfoCalculateBatchSaveReqStr string) (resp *AddPredictEdbDataResponse, err error) { _, resultByte, err := postAddEdbData(edbInfoCalculateBatchSaveReqStr, "calculate/batch/save") err = json.Unmarshal(resultByte, &resp) if err != nil { return } return } // BatchEditEdbCalculateData 编辑 累计值转月-同比值-同差等计算新增 func BatchEditEdbCalculateData(edbInfoCalculateBatchSaveReqStr string) (resp *AddPredictEdbDataResponse, err error) { _, resultByte, err := postAddEdbData(edbInfoCalculateBatchSaveReqStr, "calculate/batch/edit") 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 string) (resp *models.BaseResponse, err error) { urlStr := `` urlStr = "python/add" resp, _, err = postAddEdbData(param, urlStr) return } // EditPythonEdbData 编辑python指标 func EditPythonEdbData(param string) (resp *models.BaseResponse, err error) { urlStr := `` urlStr = "python/edit" resp, _, err = postAddEdbData(param, urlStr) return } // SaveAdjustEdbInfo 保存数据调整指标 func SaveAdjustEdbInfo(param string) (resp *models.BaseResponse, err error) { urlStr := `` urlStr = "calculate/adjust/save" resp, _, err = postAddEdbData(param, urlStr) 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 } 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 string) (resp *models.BaseResponse, result []byte, err error) { postUrl := utils.EDB_LIB_URL + urlStr result, err = HttpPost(postUrl, paramStr, "application/json") if err != nil { return } err = json.Unmarshal(result, &resp) if err != nil { return } return } 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) defer resp.Body.Close() b, err := ioutil.ReadAll(resp.Body) fmt.Println("HttpPost:" + string(b)) return b, err }