Prechádzať zdrojové kódy

cow: 牛只详情接口

Yi 10 mesiacov pred
rodič
commit
4e9be99ad5

+ 1 - 1
go.mod

@@ -3,7 +3,7 @@ module kpt-pasture
 go 1.17
 
 require (
-	gitee.com/xuyiping_admin/go_proto v0.0.0-20240508080006-64977b5c321d
+	gitee.com/xuyiping_admin/go_proto v0.0.0-20240510075137-e44046757a95
 	gitee.com/xuyiping_admin/pkg v0.0.0-20231218082641-aac597b8a015
 	github.com/dgrijalva/jwt-go v3.2.0+incompatible
 	github.com/getsentry/sentry-go v0.23.0

+ 10 - 0
go.sum

@@ -50,6 +50,16 @@ gitee.com/xuyiping_admin/go_proto v0.0.0-20240508070519-7040a3cce5cb h1:9pcF+F4S
 gitee.com/xuyiping_admin/go_proto v0.0.0-20240508070519-7040a3cce5cb/go.mod h1:x47UOU+lOkZnrtAENAsOGd7mZ5I8D2JRkMKMqLLRlVw=
 gitee.com/xuyiping_admin/go_proto v0.0.0-20240508080006-64977b5c321d h1:4mvRqpIybdhodr8Idd/brpBl65U7EXBWPhFstH3nM2I=
 gitee.com/xuyiping_admin/go_proto v0.0.0-20240508080006-64977b5c321d/go.mod h1:x47UOU+lOkZnrtAENAsOGd7mZ5I8D2JRkMKMqLLRlVw=
+gitee.com/xuyiping_admin/go_proto v0.0.0-20240509085711-c8276ecbc7ce h1:dqyKcy6tEWAt8NTY2EfkQ7VMygqlRcaobR03tj64Xyo=
+gitee.com/xuyiping_admin/go_proto v0.0.0-20240509085711-c8276ecbc7ce/go.mod h1:x47UOU+lOkZnrtAENAsOGd7mZ5I8D2JRkMKMqLLRlVw=
+gitee.com/xuyiping_admin/go_proto v0.0.0-20240509090708-1d93c51763e0 h1:MHyu/pHpYajBlpXX6YDhgU4qCt7aJMzPjsxlRpos5ys=
+gitee.com/xuyiping_admin/go_proto v0.0.0-20240509090708-1d93c51763e0/go.mod h1:x47UOU+lOkZnrtAENAsOGd7mZ5I8D2JRkMKMqLLRlVw=
+gitee.com/xuyiping_admin/go_proto v0.0.0-20240509092618-b272821a426b h1:M8NazQv/F580VSNUp+cJ/A+BEzTxArfu5Ycg05q8QXE=
+gitee.com/xuyiping_admin/go_proto v0.0.0-20240509092618-b272821a426b/go.mod h1:x47UOU+lOkZnrtAENAsOGd7mZ5I8D2JRkMKMqLLRlVw=
+gitee.com/xuyiping_admin/go_proto v0.0.0-20240509093937-40d0124cdcc6 h1:TMyoyrhi4FV6Rih+smHyDrWzaAwYWfEvoI1fzDtkjKE=
+gitee.com/xuyiping_admin/go_proto v0.0.0-20240509093937-40d0124cdcc6/go.mod h1:x47UOU+lOkZnrtAENAsOGd7mZ5I8D2JRkMKMqLLRlVw=
+gitee.com/xuyiping_admin/go_proto v0.0.0-20240510075137-e44046757a95 h1:TgTRGVSWZK/csFc7g5IM0A8gq6xBqBrj12/QYpGT/bI=
+gitee.com/xuyiping_admin/go_proto v0.0.0-20240510075137-e44046757a95/go.mod h1:x47UOU+lOkZnrtAENAsOGd7mZ5I8D2JRkMKMqLLRlVw=
 gitee.com/xuyiping_admin/pkg v0.0.0-20231218082641-aac597b8a015 h1:dfb5dRd57L2HKjdwLT93UFmPYFPOmEl56gtZmqcNnaE=
 gitee.com/xuyiping_admin/pkg v0.0.0-20231218082641-aac597b8a015/go.mod h1:Fk4GYI/v0IK3XFrm1Gn+VkgCz5Y7mfswD5hsTJYOG6A=
 github.com/BurntSushi/toml v0.3.1/go.mod h1:xHWCNGjB5oqiDr8zfno3MHue2Ht5sIBksp03qcyfWMU=

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

@@ -0,0 +1,32 @@
+package cow
+
+import (
+	"kpt-pasture/http/middleware"
+	"net/http"
+
+	pasturePb "gitee.com/xuyiping_admin/go_proto/proto/go/backend/cow"
+	"gitee.com/xuyiping_admin/pkg/apierr"
+	"gitee.com/xuyiping_admin/pkg/ginutil"
+	"github.com/gin-gonic/gin"
+)
+
+func List(c *gin.Context) {
+	var req pasturePb.SearchEventRequest
+	if err := ginutil.BindProto(c, &req); err != nil {
+		apierr.AbortBadRequest(c, http.StatusBadRequest, err)
+		return
+	}
+
+	pagination := &pasturePb.PaginationModel{
+		Page:       int32(c.GetInt(middleware.Page)),
+		PageSize:   int32(c.GetInt(middleware.PageSize)),
+		PageOffset: int32(c.GetInt(middleware.PageOffset)),
+	}
+
+	res, err := middleware.Dependency(c).StoreEventHub.OpsService.CowList(c, &req, pagination)
+	if err != nil {
+		apierr.ClassifiedAbort(c, err)
+		return
+	}
+	ginutil.JSONResp(c, res)
+}

+ 3 - 3
http/middleware/pagination.go

@@ -8,9 +8,9 @@ import (
 )
 
 const (
-	Page       = "page"
-	PageSize   = "page_size"
-	PageOffset = "page_offset"
+	Page       = "Currentpage"
+	PageSize   = "Pagesize"
+	PageOffset = "Pageoffset"
 )
 
 // Pagination sets page, pageSize and pageOffset to *gin.Context

+ 18 - 0
http/route/cow.go

@@ -0,0 +1,18 @@
+package route
+
+import (
+	"kpt-pasture/http/handler/cow"
+
+	"github.com/gin-gonic/gin"
+)
+
+func CowAPI(opts ...func(engine *gin.Engine)) func(s *gin.Engine) {
+	return func(s *gin.Engine) {
+		for _, opt := range opts {
+			opt(s)
+		}
+		// CowAPI API 组  牛只信息
+		eventRoute := authRouteGroup(s, "/api/v1/cow/")
+		eventRoute.POST("/list", cow.List)
+	}
+}

+ 1 - 0
http/route/route.go

@@ -10,6 +10,7 @@ func HTTPServerRoute(opts ...func(engine *gin.Engine)) func(s *gin.Engine) {
 		PastureManageAPI(opts...),
 		ConfigAPI(opts...),
 		EventAPI(opts...),
+		CowAPI(opts...),
 	}
 
 	return func(s *gin.Engine) {

+ 51 - 0
module/backend/cow.go

@@ -0,0 +1,51 @@
+package backend
+
+import (
+	"context"
+	"fmt"
+	"kpt-pasture/model"
+	"net/http"
+	"strings"
+
+	"gitee.com/xuyiping_admin/pkg/xerr"
+
+	pasturePb "gitee.com/xuyiping_admin/go_proto/proto/go/backend/cow"
+)
+
+func (s *StoreEntry) CowList(ctx context.Context, req *pasturePb.SearchEventRequest, pagination *pasturePb.PaginationModel) (*pasturePb.SearchCowListResponse, error) {
+	cowList := make([]*pasturePb.SearchCowList, 0)
+	var count int64 = 0
+	pref := s.DB.Table(fmt.Sprintf("%s as a", new(model.Cow).TableName())).
+		Select(`a.id as cow_id,a.sex,a.neck_ring_number,a.ear_number,a.pen_id,a.lact,a.type_id as cow_type_id,a.breed_status_id,
+			a.status as status_id,a.kind_id as cow_kind_id,a.birth_at,b.name as breed_status_name,d.name as cow_type_name,
+			e.name as cow_kind_name,f.name as pen_name,g.name as status_name`).
+		Joins(fmt.Sprintf("JOIN %s AS b ON a.breed_status_id = b.id", new(model.ConfigBreedStatus).TableName())).
+		Joins(fmt.Sprintf("JOIN %s AS d on a.type_id = d.id", new(model.ConfigCowType).TableName())).
+		Joins(fmt.Sprintf("JOIN %s AS e ON a.kind_id = e.id", new(model.ConfigCowKind).TableName())).
+		Joins(fmt.Sprintf("JOIN %s AS f on a.pen_id = f.id", new(model.Pen).TableName())).
+		Joins(fmt.Sprintf("JOIN %s AS g on a.status = g.id", new(model.ConfigCowStatus).TableName()))
+
+	if len(req.CowId) > 0 {
+		cowIds := strings.Split(req.CowId, ",")
+		pref.Where("a.id IN ?", cowIds)
+	}
+
+	if err := pref.Order("a.id desc").
+		Count(&count).Limit(int(pagination.PageSize)).
+		Offset(int(pagination.PageOffset)).
+		Find(&cowList).Error; err != nil {
+		return nil, xerr.WithStack(err)
+	}
+
+	return &pasturePb.SearchCowListResponse{
+		Code:    http.StatusOK,
+		Message: "ok",
+		Data: &pasturePb.SearchCowData{
+			List:     cowList,
+			Total:    int32(count),
+			PageSize: pagination.PageSize,
+			Page:     pagination.Page,
+		},
+	}, nil
+
+}

+ 8 - 5
module/backend/event.go

@@ -5,6 +5,7 @@ import (
 	"fmt"
 	"kpt-pasture/model"
 	"net/http"
+	"strings"
 
 	pasturePb "gitee.com/xuyiping_admin/go_proto/proto/go/backend/cow"
 	"gitee.com/xuyiping_admin/pkg/xerr"
@@ -25,8 +26,9 @@ func (s *StoreEntry) EnterList(ctx context.Context, req *pasturePb.SearchEventRe
 		Joins(fmt.Sprintf("JOIN %s AS e ON a.cow_kind_id = e.id", new(model.ConfigCowKind).TableName())).
 		Joins(fmt.Sprintf("JOIN %s AS f on a.pen_id = f.id", new(model.Pen).TableName())).
 		Joins(fmt.Sprintf("JOIN %s AS g on a.operation_id = g.id", new(model.SystemUser).TableName()))
-	if req.CowId > 0 {
-		pref.Where("a.cow_id = ?", req.CowId)
+	if len(req.CowId) > 0 {
+		cowIds := strings.Split(req.CowId, ",")
+		pref.Where("a.cow_id IN ?", cowIds)
 	}
 
 	if err := pref.Order("a.id desc").
@@ -78,9 +80,10 @@ func (s *StoreEntry) GroupTransferList(ctx context.Context, req *pasturePb.Searc
 		Joins(fmt.Sprintf("JOIN %s AS c on a.transfer_out_pen_id = c.id", new(model.Pen).TableName())).
 		Joins(fmt.Sprintf("JOIN %s AS d on a.transfer_reason_id = d.id", new(model.ConfigTransferPenReason).TableName())).
 		Joins(fmt.Sprintf("JOIN %s AS e ON a.staff_member_id = e.id", new(model.SystemUser).TableName())).
-		Joins(fmt.Sprintf("JOIN %s AS f ON a.cow_id = b.id", new(model.Cow).TableName()))
-	if req.CowId > 0 {
-		pref.Where("a.cow_id = ?", req.CowId)
+		Joins(fmt.Sprintf("JOIN %s AS f ON a.cow_id = f.id", new(model.Cow).TableName()))
+	if len(req.CowId) > 0 {
+		cowIds := strings.Split(req.CowId, ",")
+		pref.Where("a.cow_id IN ?", cowIds)
 	}
 
 	if err := pref.Order("a.id desc").Group("a.id").

+ 6 - 0
module/backend/interface.go

@@ -44,6 +44,7 @@ type KptService interface {
 	PastureManageService // 牧场管理相关
 	ConfigDataService    // 配置数据相关
 	EventService         // 事件相关
+	CowService           // 牛相关
 }
 
 //go:generate mockgen -destination mock/SystemService.go -package kptservicemock kpt-pasture/module/backend SystemService
@@ -126,3 +127,8 @@ type EventService interface {
 	GroupTransferList(ctx context.Context, req *pasturePb.SearchEventRequest, pagination *pasturePb.PaginationModel) (*pasturePb.SearchTransferGroupEventResponse, error)
 	CreateGroupTransfer(ctx context.Context, req *pasturePb.TransferGroupEventRequest) error
 }
+
+//go:generate mockgen -destination mock/CowService.go -package kptservicemock kpt-pasture/module/backend CowService
+type CowService interface {
+	CowList(ctx context.Context, req *pasturePb.SearchEventRequest, pagination *pasturePb.PaginationModel) (*pasturePb.SearchCowListResponse, error)
+}

+ 5 - 5
module/backend/sql.go

@@ -36,7 +36,7 @@ func (s *StoreEntry) GetCurrentSystemUser(ctx context.Context) (*model.SystemUse
 		Where("is_show = ? and is_delete = ?", pasturePb.IsShow_Ok, pasturePb.IsShow_Ok).
 		First(systemUser).Error; err != nil {
 		if errors.Is(err, gorm.ErrRecordNotFound) {
-			return nil, xerr.Custom("用户数据不存在")
+			return nil, xerr.Custom("当前登录用户数据不存在")
 		}
 		return nil, xerr.WithStack(err)
 	}
@@ -49,7 +49,7 @@ func (s *StoreEntry) GetSystemUserInfo(ctx context.Context, userId int64) (*mode
 	}
 	if err := s.DB.First(systemUser).Error; err != nil {
 		if errors.Is(err, gorm.ErrRecordNotFound) {
-			return nil, xerr.Custom("该数据不存在")
+			return nil, xerr.Customf("该系统用户数据不存在: %d", userId)
 		}
 		return nil, xerr.WithStack(err)
 	}
@@ -62,7 +62,7 @@ func (s *StoreEntry) GetPenInfo(ctx context.Context, penId int64) (*model.Pen, e
 	}
 	if err := s.DB.First(penData).Error; err != nil {
 		if errors.Is(err, gorm.ErrRecordNotFound) {
-			return nil, xerr.Custom("该数据不存在")
+			return nil, xerr.Customf("该栏舍数据不存在: %d", penId)
 		}
 		return nil, xerr.WithStack(err)
 	}
@@ -73,7 +73,7 @@ func (s *StoreEntry) GetCowInfo(ctx context.Context, cowId int64) (*model.Cow, e
 	cowData := &model.Cow{Id: cowId}
 	if err := s.DB.First(cowData).Error; err != nil {
 		if errors.Is(err, gorm.ErrRecordNotFound) {
-			return nil, xerr.Custom("该数据不存在")
+			return nil, xerr.Customf("该牛只数据不存在: %d", cowId)
 		}
 		return nil, xerr.WithStack(err)
 	}
@@ -86,7 +86,7 @@ func (s *StoreEntry) GetTransferReasonInfo(ctx context.Context, reasonId int64)
 	}
 	if err := s.DB.First(configTransferPenReasonData).Error; err != nil {
 		if errors.Is(err, gorm.ErrRecordNotFound) {
-			return nil, xerr.Custom("该数据不存在")
+			return nil, xerr.Customf("该转群原因数据不存在: %d", reasonId)
 		}
 		return nil, xerr.WithStack(err)
 	}