Browse Source

event: 体况评分事件

Yi 10 months ago
parent
commit
5d8088f44e
7 changed files with 75 additions and 1 deletions
  1. 1 1
      go.mod
  2. 2 0
      go.sum
  3. 29 0
      http/handler/event/event.go
  4. 1 0
      http/route/event_api.go
  5. 17 0
      model/event_body_score.go
  6. 24 0
      module/backend/event.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-20240511081455-b5ea1fc3fb2c
+	gitee.com/xuyiping_admin/go_proto v0.0.0-20240513085831-3da9e28378d7
 	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

+ 2 - 0
go.sum

@@ -66,6 +66,8 @@ gitee.com/xuyiping_admin/go_proto v0.0.0-20240511063932-87bb586873ec h1:xFue7pGV
 gitee.com/xuyiping_admin/go_proto v0.0.0-20240511063932-87bb586873ec/go.mod h1:x47UOU+lOkZnrtAENAsOGd7mZ5I8D2JRkMKMqLLRlVw=
 gitee.com/xuyiping_admin/go_proto v0.0.0-20240511081455-b5ea1fc3fb2c h1:W/2ntkrh7zdntmfSKlnOv1Zt+Yu+/pjngE79Y8BLm3o=
 gitee.com/xuyiping_admin/go_proto v0.0.0-20240511081455-b5ea1fc3fb2c/go.mod h1:x47UOU+lOkZnrtAENAsOGd7mZ5I8D2JRkMKMqLLRlVw=
+gitee.com/xuyiping_admin/go_proto v0.0.0-20240513085831-3da9e28378d7 h1:pFs2rZAvWu0jxaUm9vuHNRW4FFEgaNRbP1Be4OoFUHM=
+gitee.com/xuyiping_admin/go_proto v0.0.0-20240513085831-3da9e28378d7/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=

+ 29 - 0
http/handler/event/event.go

@@ -142,3 +142,32 @@ func BodyScoreEventList(c *gin.Context) {
 	}
 	ginutil.JSONResp(c, res)
 }
+
+func BodyScoreEventCreate(c *gin.Context) {
+	var req pasturePb.BodyScoreEventRequest
+	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),
+		valid.Field(&req.ScoreAt, valid.Required),
+		valid.Field(&req.Score, valid.Required),
+		valid.Field(&req.StaffMemberId, valid.Required),
+	); err != nil {
+		apierr.AbortBadRequest(c, http.StatusBadRequest, err)
+		return
+	}
+
+	if err := middleware.BackendOperation(c).OpsService.CreateBodyScore(c, &req); err != nil {
+		apierr.ClassifiedAbort(c, err)
+		return
+	}
+
+	ginutil.JSONResp(c, &operationPb.CommonOK{
+		Code: http.StatusOK,
+		Msg:  "ok",
+		Data: &operationPb.Success{Success: true},
+	})
+}

+ 1 - 0
http/route/event_api.go

@@ -20,5 +20,6 @@ func EventAPI(opts ...func(engine *gin.Engine)) func(s *gin.Engine) {
 		eventRoute.POST("/group/transfer/create", event.GroupTransferEventCreate)
 
 		eventRoute.POST("/body/score/list", event.BodyScoreEventList)
+		eventRoute.POST("/body/score/create", event.BodyScoreEventCreate)
 	}
 }

+ 17 - 0
model/event_body_score.go

@@ -1,8 +1,11 @@
 package model
 
+import pasturePb "gitee.com/xuyiping_admin/go_proto/proto/go/backend/cow"
+
 type EventBodyScore struct {
 	Id            int64  `json:"id"`
 	CowId         int64  `json:"cow_id"`
+	EarNumber     string `json:"ear_number"`
 	Score         int32  `json:"score"`
 	Lact          int32  `json:"lact"`
 	DayAge        int32  `json:"day_age"`
@@ -17,3 +20,17 @@ type EventBodyScore struct {
 func (e *EventBodyScore) TableName() string {
 	return "event_body_score"
 }
+
+func NewEventBodyScore(cow *Cow, operationId int64, req *pasturePb.BodyScoreEventRequest) *EventBodyScore {
+	return &EventBodyScore{
+		CowId:         cow.Id,
+		EarNumber:     cow.EarNumber,
+		Score:         req.Score,
+		Lact:          cow.Lact,
+		DayAge:        cow.GetDayAge(),
+		ScoreAt:       int64(req.ScoreAt),
+		Remarks:       req.Remarks,
+		StaffMemberId: int64(req.StaffMemberId),
+		OperationId:   operationId,
+	}
+}

+ 24 - 0
module/backend/event.go

@@ -5,6 +5,7 @@ import (
 	"fmt"
 	"kpt-pasture/model"
 	"net/http"
+	"strconv"
 	"strings"
 
 	pasturePb "gitee.com/xuyiping_admin/go_proto/proto/go/backend/cow"
@@ -209,3 +210,26 @@ func (s *StoreEntry) BodyScoreList(ctx context.Context, req *pasturePb.SearchEve
 		},
 	}, nil
 }
+
+func (s *StoreEntry) CreateBodyScore(ctx context.Context, req *pasturePb.BodyScoreEventRequest) error {
+	if len(req.CowId) <= 0 {
+		return xerr.Custom("请选择相关牛只")
+	}
+	currentSystemUser, _ := s.GetCurrentSystemUser(ctx)
+
+	cowList := strings.Split(req.CowId, ",")
+	bodyScourEvent := make([]*model.EventBodyScore, 0)
+	for _, cowIdStr := range cowList {
+		cowId, _ := strconv.ParseInt(cowIdStr, 10, 64)
+		cow, err := s.GetCowInfo(ctx, cowId)
+		if err != nil {
+			return xerr.WithStack(err)
+		}
+		bodyScourEvent = append(bodyScourEvent, model.NewEventBodyScore(cow, currentSystemUser.Id, req))
+	}
+	if len(bodyScourEvent) <= 0 {
+		return nil
+	}
+
+	return s.DB.Create(bodyScourEvent).Error
+}

+ 1 - 0
module/backend/interface.go

@@ -126,6 +126,7 @@ type EventService interface {
 	GroupTransferList(ctx context.Context, req *pasturePb.SearchEventRequest, pagination *pasturePb.PaginationModel) (*pasturePb.SearchTransferGroupEventResponse, error)
 	CreateGroupTransfer(ctx context.Context, req *pasturePb.TransferGroupEventRequest) error
 	BodyScoreList(ctx context.Context, req *pasturePb.SearchEventRequest, pagination *pasturePb.PaginationModel) (*pasturePb.SearchBodyScoreEventResponse, error)
+	CreateBodyScore(ctx context.Context, req *pasturePb.BodyScoreEventRequest) error
 }
 
 //go:generate mockgen -destination mock/CowService.go -package kptservicemock kpt-pasture/module/backend CowService