base_supply_analysis_lib.go 2.7 KB

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