|
@@ -1,104 +1,36 @@
|
|
|
package backend
|
|
|
|
|
|
import (
|
|
|
- "bytes"
|
|
|
"context"
|
|
|
"encoding/json"
|
|
|
"fmt"
|
|
|
- "io/ioutil"
|
|
|
"kpt-pasture/model"
|
|
|
- "net/http"
|
|
|
- "time"
|
|
|
|
|
|
feedingPb "gitee.com/xuyiping_admin/go_proto/proto/go/backend/cow"
|
|
|
- "gitee.com/xuyiping_admin/pkg/logger/zaplog"
|
|
|
"gitee.com/xuyiping_admin/pkg/xerr"
|
|
|
- "go.uber.org/zap"
|
|
|
)
|
|
|
|
|
|
-type FeedingHttp struct {
|
|
|
- authClient *http.Client
|
|
|
-}
|
|
|
-
|
|
|
-func NewFeedingService() *FeedingHttp {
|
|
|
- return &FeedingHttp{
|
|
|
- authClient: &http.Client{
|
|
|
- Timeout: time.Duration(60) * time.Second,
|
|
|
- },
|
|
|
- }
|
|
|
-}
|
|
|
-
|
|
|
-func (c *FeedingHttp) doRequest(req *http.Request, response interface{}) ([]byte, error) {
|
|
|
- resp, err := http.DefaultClient.Do(req)
|
|
|
- if err != nil {
|
|
|
- zaplog.Error("ClientService", zap.Any("authClient.Do", err))
|
|
|
- return nil, xerr.WithStack(err)
|
|
|
- }
|
|
|
- b, err := ioutil.ReadAll(resp.Body)
|
|
|
+func (s *StoreEntry) GetFeedingHomepage(ctx context.Context, req *feedingPb.FeedingHomepageRequest) (*feedingPb.FeedingHomepageResponse, error) {
|
|
|
+ userModel, err := s.GetUserModel(ctx)
|
|
|
if err != nil {
|
|
|
- zaplog.Error("ClientService", zap.Any("ioutil.ReadAll", err))
|
|
|
return nil, xerr.WithStack(err)
|
|
|
}
|
|
|
- if resp.StatusCode != http.StatusOK {
|
|
|
- if len(b) > 0 {
|
|
|
- return nil, xerr.Customf("err:%v,body:%s", err, string(b))
|
|
|
- } else {
|
|
|
- return nil, xerr.Customf("err:%v", err)
|
|
|
- }
|
|
|
- }
|
|
|
- fmt.Println(string(b))
|
|
|
- if err = json.Unmarshal(b, response); err != nil {
|
|
|
- return nil, xerr.WithStack(err)
|
|
|
+ pasture := userModel.AppPasture
|
|
|
+ if pasture.FeedPastureId == 0 {
|
|
|
+ return nil, xerr.Customf("饲喂数据未配置")
|
|
|
}
|
|
|
- return b, nil
|
|
|
-}
|
|
|
|
|
|
-func (c *FeedingHttp) DoGet(url string, response interface{}) ([]byte, error) {
|
|
|
- req, err := http.NewRequest(http.MethodGet, url, nil)
|
|
|
- if err != nil {
|
|
|
- zaplog.Error("ClientService", zap.Any("DoGet", err))
|
|
|
- return nil, err
|
|
|
- }
|
|
|
- req.Header.Add("Accept", "application/json")
|
|
|
- req.Header.Add("Content-Type", "application/json")
|
|
|
- return c.doRequest(req, response)
|
|
|
-}
|
|
|
-
|
|
|
-func (c *FeedingHttp) DoPost(url string, body interface{}, response *interface{}) ([]byte, error) {
|
|
|
- b, err := json.Marshal(body)
|
|
|
- if err != nil {
|
|
|
- zaplog.Error("ClientService", zap.Any("DoPost-Marshal", err))
|
|
|
- return nil, xerr.WithStack(err)
|
|
|
- }
|
|
|
- req, err := http.NewRequest(http.MethodPost, url, bytes.NewReader(b))
|
|
|
+ url := fmt.Sprintf("%d/feeding/tmrdata", pasture.FeedPastureId)
|
|
|
+ response := &model.FeedingHomepageResponse{Data: []*model.FeedingHomepageData{}}
|
|
|
+ result, err := s.HttpClient.DoGet(url, model.FeedingHeaders)
|
|
|
if err != nil {
|
|
|
- zaplog.Error("ClientService", zap.Any("NewRequest", err))
|
|
|
return nil, xerr.WithStack(err)
|
|
|
}
|
|
|
- req.Header.Add("Accept", "application/json")
|
|
|
- req.Header.Add("Content-Type", "application/json")
|
|
|
- return c.doRequest(req, response)
|
|
|
-}
|
|
|
|
|
|
-func (s *StoreEntry) GetFeedingHomepage(ctx context.Context, req *feedingPb.FeedingHomepageRequest) (*feedingPb.FeedingHomepageResponse, error) {
|
|
|
- var pasture model.AppPastureList
|
|
|
- if err := s.DB.Debug().Table(new(model.AppPastureList).TableName()).Where("farm_id = ?", req.FarmId).First(&pasture).Error; err != nil {
|
|
|
+ if err = json.Unmarshal(result, response); err != nil {
|
|
|
return nil, xerr.WithStack(err)
|
|
|
}
|
|
|
- if pasture.PastureId == 0 {
|
|
|
- return nil, xerr.Customf("牧场不存在")
|
|
|
- }
|
|
|
-
|
|
|
- url := fmt.Sprintf("%s/feeding/tmrdata", pasture.PastureUrl)
|
|
|
- fmt.Println(url)
|
|
|
- client := NewFeedingService()
|
|
|
|
|
|
- response := &model.FeedingHomepageResponse{Data: []*model.FeedingHomepageData{}}
|
|
|
- _, err := client.DoGet(url, response)
|
|
|
- if err != nil {
|
|
|
- return nil, xerr.WithStack(err)
|
|
|
- }
|
|
|
- fmt.Println(response)
|
|
|
res := &feedingPb.FeedingHomepageResponse{
|
|
|
Code: response.Code,
|
|
|
Msg: response.Msg,
|
|
@@ -123,29 +55,29 @@ func (s *StoreEntry) GetFeedingHomepage(ctx context.Context, req *feedingPb.Feed
|
|
|
YesRateSLR: v.YesRateSLR,
|
|
|
}
|
|
|
}
|
|
|
-
|
|
|
return res, nil
|
|
|
}
|
|
|
|
|
|
func (s *StoreEntry) GetFeedingManagement(ctx context.Context, req *feedingPb.FeedingManagementRequest) (*feedingPb.FeedingManagementResponse, error) {
|
|
|
- var pasture model.AppPastureList
|
|
|
- if err := s.DB.Debug().Table(new(model.AppPastureList).TableName()).Where("farm_id = ?", req.FarmId).First(&pasture).Error; err != nil {
|
|
|
+ userModel, err := s.GetUserModel(ctx)
|
|
|
+ if err != nil {
|
|
|
return nil, xerr.WithStack(err)
|
|
|
}
|
|
|
- if pasture.PastureId == 0 {
|
|
|
- return nil, xerr.Customf("牧场不存在")
|
|
|
+ pasture := userModel.AppPasture
|
|
|
+ if pasture.FeedPastureId == 0 {
|
|
|
+ return nil, xerr.Customf("饲喂数据未配置")
|
|
|
}
|
|
|
|
|
|
- url := fmt.Sprintf("%s/feeding/management?typea=%s&startdate=%s&enddate=%s", pasture.PastureUrl, req.Typea, req.Startdate, req.Enddate)
|
|
|
- fmt.Println(url)
|
|
|
- client := NewFeedingService()
|
|
|
-
|
|
|
+ url := fmt.Sprintf("%d/feeding/management?typea=%s&startdate=%s&enddate=%s", pasture.FeedPastureId, req.Typea, req.Startdate, req.Enddate)
|
|
|
response := &model.FeedingManagementResponse{Data: []*model.FeedingManagementData{}}
|
|
|
- _, err := client.DoGet(url, response)
|
|
|
+ result, err := s.HttpClient.DoGet(url, model.FeedingHeaders)
|
|
|
if err != nil {
|
|
|
return nil, xerr.WithStack(err)
|
|
|
}
|
|
|
- fmt.Println(response)
|
|
|
+
|
|
|
+ if err = json.Unmarshal(result, response); err != nil {
|
|
|
+ return nil, xerr.WithStack(err)
|
|
|
+ }
|
|
|
res := &feedingPb.FeedingManagementResponse{
|
|
|
Code: response.Code,
|
|
|
Msg: response.Msg,
|
|
@@ -162,6 +94,5 @@ func (s *StoreEntry) GetFeedingManagement(ctx context.Context, req *feedingPb.Fe
|
|
|
Typea: v.TypeA,
|
|
|
})
|
|
|
}
|
|
|
-
|
|
|
return res, nil
|
|
|
}
|