Browse Source

cow: 牛只查询接口

Yi 1 month ago
parent
commit
e8856f538d
7 changed files with 65 additions and 2 deletions
  1. 1 1
      go.mod
  2. 4 0
      go.sum
  3. 22 0
      http/handler/cow/cow.go
  4. 0 0
      http/middleware/auth.go
  5. 1 0
      http/route/cow_api.go
  6. 36 1
      module/backend/cow.go
  7. 1 0
      module/backend/interface.go

+ 1 - 1
go.mod

@@ -3,7 +3,7 @@ module kpt-pasture
 go 1.17
 
 require (
-	gitee.com/xuyiping_admin/go_proto v0.0.0-20250205095115-901ea79b7e1f
+	gitee.com/xuyiping_admin/go_proto v0.0.0-20250206022501-ea32be0345bd
 	gitee.com/xuyiping_admin/pkg v0.0.0-20241108060137-caea58c59f5b
 	github.com/dgrijalva/jwt-go v3.2.0+incompatible
 	github.com/eclipse/paho.mqtt.golang v1.4.3

+ 4 - 0
go.sum

@@ -138,6 +138,10 @@ gitee.com/xuyiping_admin/go_proto v0.0.0-20250124085242-ce995d9353fb h1:NYaR6Kru
 gitee.com/xuyiping_admin/go_proto v0.0.0-20250124085242-ce995d9353fb/go.mod h1:BKrFW6YLDectlQcQk3FYKBeXvjEiodAKJ5rq7O/QiPE=
 gitee.com/xuyiping_admin/go_proto v0.0.0-20250205095115-901ea79b7e1f h1:sVJ/ZSR4r1h2bO0HEf7IVZppBjJ/UzEqCu1hptptgrM=
 gitee.com/xuyiping_admin/go_proto v0.0.0-20250205095115-901ea79b7e1f/go.mod h1:BKrFW6YLDectlQcQk3FYKBeXvjEiodAKJ5rq7O/QiPE=
+gitee.com/xuyiping_admin/go_proto v0.0.0-20250206021619-2b0fc78035bb h1:I0QqA8hsBc4WZQ7jl6WsiIEPwFxLvQmJv7XtLJhzlBM=
+gitee.com/xuyiping_admin/go_proto v0.0.0-20250206021619-2b0fc78035bb/go.mod h1:BKrFW6YLDectlQcQk3FYKBeXvjEiodAKJ5rq7O/QiPE=
+gitee.com/xuyiping_admin/go_proto v0.0.0-20250206022501-ea32be0345bd h1:wLHGWLUXJG4Uh0BXyD0VDeq6XnGWqVxxfZxUZHv294I=
+gitee.com/xuyiping_admin/go_proto v0.0.0-20250206022501-ea32be0345bd/go.mod h1:BKrFW6YLDectlQcQk3FYKBeXvjEiodAKJ5rq7O/QiPE=
 gitee.com/xuyiping_admin/pkg v0.0.0-20241108060137-caea58c59f5b h1:w05MxH7yqveRlaRbxHhbif5YjPrJFodRPfOjYhXn7Zk=
 gitee.com/xuyiping_admin/pkg v0.0.0-20241108060137-caea58c59f5b/go.mod h1:8tF25X6pE9WkFCczlNAC0K2mrjwKvhhp02I7o0HtDxY=
 github.com/BurntSushi/toml v0.3.1/go.mod h1:xHWCNGjB5oqiDr8zfno3MHue2Ht5sIBksp03qcyfWMU=

+ 22 - 0
http/handler/cow/cow.go

@@ -12,6 +12,28 @@ import (
 	"github.com/gin-gonic/gin"
 )
 
+func Detail(c *gin.Context) {
+	var req pasturePb.SearchEventRequest
+	if err := ginutil.BindProto(c, &req); err != nil {
+		apierr.AbortBadRequest(c, http.StatusBadRequest, err)
+		return
+	}
+
+	if err := valid.ValidateStruct(&req,
+		valid.Field(&req.CowId, valid.Required),
+	); err != nil {
+		apierr.AbortBadRequest(c, http.StatusBadRequest, err)
+		return
+	}
+
+	res, err := middleware.Dependency(c).StoreEventHub.OpsService.Detail(c, &req)
+	if err != nil {
+		apierr.ClassifiedAbort(c, err)
+		return
+	}
+	ginutil.JSONResp(c, res)
+}
+
 func List(c *gin.Context) {
 	var req pasturePb.SearchEventRequest
 	if err := ginutil.BindProto(c, &req); err != nil {

+ 0 - 0
http/middleware/sso.go → http/middleware/auth.go


+ 1 - 0
http/route/cow_api.go

@@ -13,6 +13,7 @@ func CowAPI(opts ...func(engine *gin.Engine)) func(s *gin.Engine) {
 		}
 		// CowAPI API 组  牛只信息
 		cowRoute := authRouteGroup(s, "/api/v1/cow/")
+		cowRoute.POST("/detail", cow.Detail)
 		cowRoute.POST("/list", cow.List)
 		cowRoute.POST("/event/list", cow.EventList)
 		cowRoute.POST("/behavior/curve", cow.BehaviorCurve)

+ 36 - 1
module/backend/cow.go

@@ -17,6 +17,42 @@ import (
 	pasturePb "gitee.com/xuyiping_admin/go_proto/proto/go/backend/cow"
 )
 
+func (s *StoreEntry) Detail(ctx context.Context, req *pasturePb.SearchEventRequest) (*pasturePb.CowInfoResponse, error) {
+	currentUser, err := s.GetCurrentSystemUser(ctx)
+	if err != nil {
+		return nil, xerr.Custom("当前用户信息错误,请退出重新登录")
+	}
+
+	cowInfo := make([]*model.Cow, 0)
+	pref := s.DB.Model(new(model.Cow)).Where("pasture_id = ?", currentUser.PastureId).Where("id = ?", req.CowId)
+	if req.NeckRingNumber != "" {
+		pref.Where("neck_ring_number = ?", req.NeckRingNumber)
+	}
+
+	if req.EarNumber != "" {
+		pref.Where("ear_number = ?", req.EarNumber)
+	}
+
+	if err = pref.Order("id desc").
+		Find(&cowInfo).Error; err != nil {
+		return nil, xerr.WithStack(err)
+	}
+
+	penMap := s.PenMap(ctx, currentUser.PastureId)
+	cowTypeMap := s.CowTypeMap()
+	breedStatusMap := s.CowBreedStatusMap()
+	cowKindMap := s.CowKindMap()
+	cowSourceMap := s.CowSourceMap()
+	admissionStatusMap := s.AdmissionStatusMap()
+	healthStatusMap := s.HealthStatusMap()
+
+	return &pasturePb.CowInfoResponse{
+		Code: http.StatusOK,
+		Msg:  "ok",
+		Data: model.CowSlice(cowInfo).ToPB(penMap, cowTypeMap, breedStatusMap, cowKindMap, cowSourceMap, admissionStatusMap, healthStatusMap)[0],
+	}, nil
+}
+
 func (s *StoreEntry) List(ctx context.Context, req *pasturePb.SearchEventRequest, pagination *pasturePb.PaginationModel) (*pasturePb.SearchCowListResponse, error) {
 	currentUser, err := s.GetCurrentSystemUser(ctx)
 	if err != nil {
@@ -97,7 +133,6 @@ func (s *StoreEntry) List(ctx context.Context, req *pasturePb.SearchEventRequest
 			Page:     pagination.Page,
 		},
 	}, nil
-
 }
 
 func (s *StoreEntry) EventList(ctx context.Context, req *pasturePb.SearchCowEventListRequest, pagination *pasturePb.PaginationModel) (*pasturePb.CowEventListResponse, error) {

+ 1 - 0
module/backend/interface.go

@@ -209,6 +209,7 @@ type EventService interface {
 
 //go:generate mockgen -destination mock/CowService.go -package kptservicemock kpt-pasture/module/backend CowService
 type CowService interface {
+	Detail(ctx context.Context, req *pasturePb.SearchEventRequest) (*pasturePb.CowInfoResponse, error)
 	List(ctx context.Context, req *pasturePb.SearchEventRequest, pagination *pasturePb.PaginationModel) (*pasturePb.SearchCowListResponse, error)
 	EventList(ctx context.Context, req *pasturePb.SearchCowEventListRequest, pagination *pasturePb.PaginationModel) (*pasturePb.CowEventListResponse, error)
 	BehaviorCurve(ctx context.Context, req *pasturePb.CowBehaviorCurveRequest) (*model.CowBehaviorCurveResponse, error)