base_supply_analysis_lib.go 2.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596
  1. package supply_analysis
  2. import (
  3. "encoding/json"
  4. "eta/eta_mobile/models"
  5. "eta/eta_mobile/utils"
  6. "io/ioutil"
  7. "net/http"
  8. "strings"
  9. )
  10. type CalculateEdbDataResponse struct {
  11. Ret int
  12. Msg string
  13. ErrMsg string
  14. ErrCode string
  15. Data interface{}
  16. Success bool `description:"true 执行成功,false 执行失败"`
  17. IsSendEmail bool `json:"-" description:"true 发送邮件,false 不发送邮件"`
  18. IsAddLog bool `json:"-" description:"true 新增操作日志,false 不新增操作日志" `
  19. }
  20. // Calculate 分析品种
  21. func Calculate(paramStr string) (resp *CalculateEdbDataResponse, err error) {
  22. _, resultByte, err := postAddEdbData(paramStr, "stock_plant/calculate")
  23. err = json.Unmarshal(resultByte, &resp)
  24. if err != nil {
  25. return
  26. }
  27. return
  28. }
  29. // postAddEdbData 新增指标数据
  30. func postAddEdbData(paramStr string, urlStr string) (resp *models.BaseResponse, result []byte, err error) {
  31. postUrl := utils.EDB_LIB_URL + urlStr
  32. result, err = HttpPost(postUrl, paramStr, "application/json")
  33. if err != nil {
  34. return
  35. }
  36. err = json.Unmarshal(result, &resp)
  37. if err != nil {
  38. return
  39. }
  40. return
  41. }
  42. // RefreshEdbData 刷新指标数据
  43. func RefreshEdbData(futureGoodEdbInfoId int, futureGoodEdbCode, startDate string) (resp *models.BaseResponse, err error) {
  44. param := make(map[string]interface{})
  45. param["FutureGoodEdbCode"] = futureGoodEdbCode
  46. param["FutureGoodEdbInfoId"] = futureGoodEdbInfoId
  47. param["StartDate"] = startDate
  48. urlStr := `future_good/refresh`
  49. resp, err = postRefreshEdbData(param, urlStr)
  50. return
  51. }
  52. // postRefreshEdbData 刷新指标数据
  53. func postRefreshEdbData(param map[string]interface{}, urlStr string) (resp *models.BaseResponse, err error) {
  54. postUrl := utils.EDB_LIB_URL + urlStr
  55. postData, err := json.Marshal(param)
  56. if err != nil {
  57. return
  58. }
  59. result, err := HttpPost(postUrl, string(postData), "application/json")
  60. if err != nil {
  61. return
  62. }
  63. utils.FileLog.Info("postRefreshEdbData:" + postUrl + ";" + string(postData) + ";result:" + string(result))
  64. err = json.Unmarshal(result, &resp)
  65. if err != nil {
  66. return
  67. }
  68. return resp, nil
  69. }
  70. func HttpPost(url, postData string, params ...string) ([]byte, error) {
  71. body := ioutil.NopCloser(strings.NewReader(postData))
  72. client := &http.Client{}
  73. req, err := http.NewRequest("POST", url, body)
  74. if err != nil {
  75. return nil, err
  76. }
  77. contentType := "application/x-www-form-urlencoded;charset=utf-8"
  78. if len(params) > 0 && params[0] != "" {
  79. contentType = params[0]
  80. }
  81. req.Header.Set("Content-Type", contentType)
  82. req.Header.Set("authorization", utils.MD5(utils.APP_EDB_LIB_NAME_EN+utils.EDB_LIB_Md5_KEY))
  83. resp, err := client.Do(req)
  84. defer resp.Body.Close()
  85. b, err := ioutil.ReadAll(resp.Body)
  86. utils.FileLog.Debug("HttpPost:" + string(b))
  87. return b, err
  88. }