Преглед на файлове

feeding: 饲喂模块更新

Yi преди 2 дни
родител
ревизия
bbe1875bef
променени са 6 файла, в които са добавени 42 реда и са изтрити 99 реда
  1. 1 1
      http/route/feeding_api.go
  2. 2 2
      model/app_pasture_list.go
  3. 12 0
      model/feeding.go
  4. 20 89
      module/backend/feeding.go
  5. 6 6
      module/backend/interface.go
  6. 1 1
      service/httpclient/http.go

+ 1 - 1
http/route/feeding.go → http/route/feeding_api.go

@@ -13,7 +13,7 @@ func FeedingAPI(opts ...func(engine *gin.Engine)) func(s *gin.Engine) {
 		}
 		feedingRoute := authRouteGroup(s, "/api/v1/feeding/")
 
-		feedingRoute.GET("tmrdata", feeding.GetFeedingHomepage)
+		feedingRoute.GET("tmr/data", feeding.GetFeedingHomepage)
 		feedingRoute.GET("management", feeding.GetFeedingManagement)
 	}
 }

+ 2 - 2
model/app_pasture_list.go

@@ -21,14 +21,14 @@ type AppPastureList struct {
 	PlanScale            string                         `json:"planScale"`
 	AppId                string                         `json:"appId"`
 	Status               int32                          `json:"status"`
+	FeedPastureId        int64                          `json:"feedPastureId"`
+	FeedPastureUrl       string                         `json:"feedPastureUrl"`
 	IsShow               pasturePb.IsShow_Kind          `json:"isShow"`
 	ProductionModel      int32                          `json:"productionModel"`
 	Remarks              string                         `json:"remarks"`
 	CreatedName          string                         `json:"createdName"`
 	CreatedAt            int64                          `json:"createdAt"`
 	UpdatedAt            string                         `json:"updatedAt"`
-	PastureId            int64                          `json:"pastureId"`
-	PastureUrl           string                         `json:"pastureUrl"`
 }
 
 func (a *AppPastureList) TableName() string {

+ 12 - 0
model/feeding.go

@@ -1,5 +1,17 @@
 package model
 
+import "kpt-pasture/service/httpclient"
+
+var FeedingHeaders = []*httpclient.Header{
+	{
+		Key:   "Content-Type",
+		Value: "application/json",
+	}, {
+		Key:   "Accept",
+		Value: "application/json",
+	},
+}
+
 type FeedingHomepageResponse struct {
 	Code int32                  `json:"code"`
 	Msg  string                 `json:"msg"`

+ 20 - 89
module/backend/feeding.go

@@ -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
 }

+ 6 - 6
module/backend/interface.go

@@ -60,8 +60,8 @@ type KptService interface {
 	MilkHallService      // 奶厅数据相关
 	UploadService        // 上传文件相关
 	WarningService       // 预警相关
-	TestService          // 测试相关
 	FeedingService       // 饲喂相关
+	TestService          // 测试相关
 }
 
 //go:generate mockgen -destination mock/SystemService.go -package kptservicemock kpt-pasture/module/backend SystemService
@@ -367,6 +367,11 @@ type UploadService interface {
 	ImportExcel2(ctx context.Context, data [][]string, excelHeader []string) error
 }
 
+type FeedingService interface {
+	GetFeedingHomepage(ctx context.Context, req *pasturePb.FeedingHomepageRequest) (*pasturePb.FeedingHomepageResponse, error)
+	GetFeedingManagement(ctx context.Context, req *pasturePb.FeedingManagementRequest) (*pasturePb.FeedingManagementResponse, error)
+}
+
 type TestService interface {
 	CowNeckRingNumberBound(ctx context.Context, pagination *pasturePb.PaginationModel) error
 	CowNeckRingNumberBound2(ctx context.Context, pagination *pasturePb.PaginationModel) error
@@ -378,8 +383,3 @@ type TestService interface {
 	CalvingAge(ctx context.Context) error
 	SystemMenuInit(ctx context.Context) error
 }
-
-type FeedingService interface {
-	GetFeedingHomepage(ctx context.Context, req *pasturePb.FeedingHomepageRequest) (*pasturePb.FeedingHomepageResponse, error)
-	GetFeedingManagement(ctx context.Context, req *pasturePb.FeedingManagementRequest) (*pasturePb.FeedingManagementResponse, error)
-}

+ 1 - 1
service/httpclient/http.go

@@ -21,7 +21,7 @@ type Service struct {
 func NewClientService() *Service {
 	return &Service{
 		authClient: &http.Client{
-			Timeout: time.Duration(5) * time.Second,
+			Timeout: time.Duration(60) * time.Second,
 		},
 	}
 }