Browse Source

event: 发情事件

ping 10 months ago
parent
commit
4143cb0a28
7 changed files with 180 additions and 2 deletions
  1. 1 1
      go.mod
  2. 4 0
      go.sum
  3. 50 1
      http/handler/event/event.go
  4. 3 0
      http/route/event_api.go
  5. 68 0
      model/event_estrus.go
  6. 52 0
      module/backend/event.go
  7. 2 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-20240528055802-b5a257a61f02
+	gitee.com/xuyiping_admin/go_proto v0.0.0-20240528132259-854b1b5724a7
 	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

+ 4 - 0
go.sum

@@ -90,6 +90,10 @@ gitee.com/xuyiping_admin/go_proto v0.0.0-20240528053721-d0592ac1e139 h1:qPsLiTnA
 gitee.com/xuyiping_admin/go_proto v0.0.0-20240528053721-d0592ac1e139/go.mod h1:BKrFW6YLDectlQcQk3FYKBeXvjEiodAKJ5rq7O/QiPE=
 gitee.com/xuyiping_admin/go_proto v0.0.0-20240528055802-b5a257a61f02 h1:SzB3bZJ/qb/aKDUqRkufiJr9d26CeTygLW+SeCZ97Js=
 gitee.com/xuyiping_admin/go_proto v0.0.0-20240528055802-b5a257a61f02/go.mod h1:BKrFW6YLDectlQcQk3FYKBeXvjEiodAKJ5rq7O/QiPE=
+gitee.com/xuyiping_admin/go_proto v0.0.0-20240528124602-b05c9ca54524 h1:XpGCGOdny8QO50Jtm4uNe+srse/eZ4a+9gu9qNfe+v4=
+gitee.com/xuyiping_admin/go_proto v0.0.0-20240528124602-b05c9ca54524/go.mod h1:BKrFW6YLDectlQcQk3FYKBeXvjEiodAKJ5rq7O/QiPE=
+gitee.com/xuyiping_admin/go_proto v0.0.0-20240528132259-854b1b5724a7 h1:ewjC7qfTvPWB/jv5mSAp8k/uveh85LRjdmDemRJX8Lk=
+gitee.com/xuyiping_admin/go_proto v0.0.0-20240528132259-854b1b5724a7/go.mod h1:BKrFW6YLDectlQcQk3FYKBeXvjEiodAKJ5rq7O/QiPE=
 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=

+ 50 - 1
http/handler/event/event.go

@@ -315,7 +315,7 @@ func MatingCreate(c *gin.Context) {
 		valid.Field(&req.CowId, valid.Required),
 		valid.Field(&req.StaffMemberId, valid.Required),
 		valid.Field(&req.ExposeEstrusType, valid.Required),
-		valid.Field(&req.StaffMemberId, valid.Required),
+		valid.Field(&req.FrozenSemenNumber, valid.Required),
 		valid.Field(&req.MatingAt, valid.Required),
 	); err != nil {
 		apierr.AbortBadRequest(c, http.StatusBadRequest, err)
@@ -333,3 +333,52 @@ func MatingCreate(c *gin.Context) {
 		Data: &operationPb.Success{Success: true},
 	})
 }
+
+func EstrusEventList(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.EstrusList(c, &req, pagination)
+	if err != nil {
+		apierr.ClassifiedAbort(c, err)
+		return
+	}
+	ginutil.JSONResp(c, res)
+}
+
+func EstrusCreate(c *gin.Context) {
+	var req pasturePb.EstrusEventRequest
+	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.StaffMemberId, valid.Required),
+		valid.Field(&req.EstrusAt, valid.Required),
+	); err != nil {
+		apierr.AbortBadRequest(c, http.StatusBadRequest, err)
+		return
+	}
+
+	if err := middleware.BackendOperation(c).OpsService.EstrusCreate(c, &req); err != nil {
+		apierr.ClassifiedAbort(c, err)
+		return
+	}
+
+	ginutil.JSONResp(c, &operationPb.CommonOK{
+		Code: http.StatusOK,
+		Msg:  "ok",
+		Data: &operationPb.Success{Success: true},
+	})
+}

+ 3 - 0
http/route/event_api.go

@@ -30,5 +30,8 @@ func EventAPI(opts ...func(engine *gin.Engine)) func(s *gin.Engine) {
 
 		eventRoute.POST("/mating/list", event.MatingEventList)
 		eventRoute.POST("/mating/create", event.MatingCreate)
+
+		eventRoute.POST("/estrus/list", event.EstrusEventList)
+		eventRoute.POST("/estrus/create", event.EstrusCreate)
 	}
 }

+ 68 - 0
model/event_estrus.go

@@ -0,0 +1,68 @@
+package model
+
+import pasturePb "gitee.com/xuyiping_admin/go_proto/proto/go/backend/cow"
+
+type EventEstrus struct {
+	Id            int64  `json:"id"`
+	CowId         int64  `json:"cow_id"`
+	DayAge        int32  `json:"day_age"`
+	Lact          int8   `json:"lact"`
+	LactationDays int32  `json:"lactation_days"`
+	EstrusAt      int64  `json:"estrus_at"`
+	Remarks       string `json:"remarks"`
+	StallNumberId int64  `json:"stall_number_id"`
+	OperationId   int64  `json:"operation_id"`
+	CreatedAt     int64  `json:"created_at"`
+	UpdatedAt     int64  `json:"updated_at"`
+}
+
+func (e *EventEstrus) TableName() string {
+	return "event_estrus"
+}
+
+func NewEventEstrus(cow *Cow, currentUser *SystemUser, req *pasturePb.EstrusEventRequest) *EventEstrus {
+	return &EventEstrus{
+		CowId:         cow.Id,
+		DayAge:        cow.GetDayAge(),
+		Lact:          int8(cow.Lact),
+		LactationDays: cow.GetLactationDays(),
+		EstrusAt:      int64(req.EstrusAt),
+		Remarks:       req.Remarks,
+		StallNumberId: int64(req.StaffMemberId),
+		OperationId:   currentUser.Id,
+	}
+}
+
+type EstrusSlice []*EventEstrus
+
+func (e EstrusSlice) ToPB(userList []*SystemUser) []*pasturePb.SearchEstrusList {
+	res := make([]*pasturePb.SearchEstrusList, len(e))
+	for i, v := range e {
+		staffMemberName, operationName := "", ""
+		for _, u := range userList {
+			if u.Id == v.StallNumberId {
+				staffMemberName = u.Name
+			}
+			if u.Id == v.OperationId {
+				operationName = u.Name
+			}
+		}
+
+		res[i] = &pasturePb.SearchEstrusList{
+			Id:              int32(v.Id),
+			CowId:           int32(v.CowId),
+			DayAge:          v.DayAge,
+			Lact:            int32(v.Lact),
+			EstrusAt:        int32(v.EstrusAt),
+			LactationDays:   v.LactationDays,
+			StaffMemberId:   int32(v.StallNumberId),
+			StaffMemberName: staffMemberName,
+			Remarks:         v.Remarks,
+			OperationId:     int32(v.OperationId),
+			OperationName:   operationName,
+			CreatedAt:       int32(v.CreatedAt),
+			UpdatedAt:       int32(v.UpdatedAt),
+		}
+	}
+	return res
+}

+ 52 - 0
module/backend/event.go

@@ -422,3 +422,55 @@ func (s *StoreEntry) MatingCreate(ctx context.Context, req *pasturePb.MatingEven
 	}
 	return nil
 }
+
+func (s *StoreEntry) EstrusList(ctx context.Context, req *pasturePb.SearchEventRequest, pagination *pasturePb.PaginationModel) (*pasturePb.EstrusEventResponse, error) {
+	estrusList := make([]*model.EventEstrus, 0)
+	var count int64 = 0
+	pref := s.DB.Table(new(model.EventEstrus).TableName())
+	if len(req.CowId) > 0 {
+		cowIds := strings.Split(req.CowId, ",")
+		pref.Where("cow_id IN ?", cowIds)
+	}
+
+	if err := pref.Order("id desc").
+		Count(&count).Limit(int(pagination.PageSize)).
+		Offset(int(pagination.PageOffset)).
+		Find(&estrusList).Error; err != nil {
+		return nil, xerr.WithStack(err)
+	}
+
+	systemUserList, _ := s.SystemUserList(ctx)
+	return &pasturePb.EstrusEventResponse{
+		Code:    http.StatusOK,
+		Message: "ok",
+		Data: &pasturePb.SearchEstrusData{
+			List:     model.EstrusSlice(estrusList).ToPB(systemUserList),
+			Total:    int32(count),
+			PageSize: pagination.PageSize,
+			Page:     pagination.Page,
+		},
+	}, nil
+}
+
+func (s *StoreEntry) EstrusCreate(ctx context.Context, req *pasturePb.EstrusEventRequest) error {
+	if len(req.CowId) <= 0 {
+		return xerr.Custom("请选择相关牛只")
+	}
+	cowList := strings.Split(req.CowId, ",")
+	estruList := make([]*model.EventEstrus, 0)
+	currentUser, _ := s.GetCurrentSystemUser(ctx)
+
+	for _, v := range cowList {
+		cowId, _ := strconv.ParseInt(v, 10, 64)
+		cow, err := s.GetCowInfo(ctx, cowId)
+		if err != nil {
+			return xerr.WithStack(err)
+		}
+		estruList = append(estruList, model.NewEventEstrus(cow, currentUser, req))
+	}
+
+	if err := s.DB.Create(estruList).Error; err != nil {
+		return xerr.WithStack(err)
+	}
+	return nil
+}

+ 2 - 0
module/backend/interface.go

@@ -135,6 +135,8 @@ type EventService interface {
 	PregnantCheckCreate(ctx context.Context, req *pasturePb.PregnantCheckEventRequest) error
 	MatingList(ctx context.Context, req *pasturePb.SearchEventRequest, pagination *pasturePb.PaginationModel) (*pasturePb.MatingEventResponse, error)
 	MatingCreate(ctx context.Context, req *pasturePb.MatingEventRequest) error
+	EstrusList(ctx context.Context, req *pasturePb.SearchEventRequest, pagination *pasturePb.PaginationModel) (*pasturePb.EstrusEventResponse, error)
+	EstrusCreate(ctx context.Context, req *pasturePb.EstrusEventRequest) error
 }
 
 //go:generate mockgen -destination mock/CowService.go -package kptservicemock kpt-pasture/module/backend CowService