浏览代码

event: 称重事件

Yi 10 月之前
父节点
当前提交
dd38283ae4
共有 8 个文件被更改,包括 155 次插入3 次删除
  1. 1 1
      go.mod
  2. 6 0
      go.sum
  3. 50 0
      http/handler/event/event.go
  4. 4 0
      http/route/event_api.go
  5. 38 0
      model/cow_weight.go
  6. 54 0
      module/backend/event.go
  7. 2 0
      module/backend/interface.go
  8. 0 2
      module/backend/pasture.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-20240604100629-0c14a75c8db9
+	gitee.com/xuyiping_admin/go_proto v0.0.0-20240605030356-5e185b3955e4
 	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

+ 6 - 0
go.sum

@@ -126,6 +126,12 @@ gitee.com/xuyiping_admin/go_proto v0.0.0-20240604090708-739d4174a0e4 h1:KIzkTpmj
 gitee.com/xuyiping_admin/go_proto v0.0.0-20240604090708-739d4174a0e4/go.mod h1:BKrFW6YLDectlQcQk3FYKBeXvjEiodAKJ5rq7O/QiPE=
 gitee.com/xuyiping_admin/go_proto v0.0.0-20240604100629-0c14a75c8db9 h1:DnST6fvYcKzIBaOkivc2/WqlvYQcwK/nrRs2a4HDT/8=
 gitee.com/xuyiping_admin/go_proto v0.0.0-20240604100629-0c14a75c8db9/go.mod h1:BKrFW6YLDectlQcQk3FYKBeXvjEiodAKJ5rq7O/QiPE=
+gitee.com/xuyiping_admin/go_proto v0.0.0-20240605024919-cd3343e3d510 h1:Xj9s2wQ7svli+WdYS68avDbDyN/l6Zoy/KJIJvk6/H4=
+gitee.com/xuyiping_admin/go_proto v0.0.0-20240605024919-cd3343e3d510/go.mod h1:BKrFW6YLDectlQcQk3FYKBeXvjEiodAKJ5rq7O/QiPE=
+gitee.com/xuyiping_admin/go_proto v0.0.0-20240605025134-92750dd51e8d h1:DJP0jJoLAdXiCrGmaWEq3nofE6+QbODqqhpIuUxTQRM=
+gitee.com/xuyiping_admin/go_proto v0.0.0-20240605025134-92750dd51e8d/go.mod h1:BKrFW6YLDectlQcQk3FYKBeXvjEiodAKJ5rq7O/QiPE=
+gitee.com/xuyiping_admin/go_proto v0.0.0-20240605030356-5e185b3955e4 h1:0hQ8qWbSBph/2jgB2RUUSbuXGu1NARJHVwRwIM1cV68=
+gitee.com/xuyiping_admin/go_proto v0.0.0-20240605030356-5e185b3955e4/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 - 0
http/handler/event/event.go

@@ -434,3 +434,53 @@ func FrozenSemeCreate(c *gin.Context) {
 		Data: &operationPb.Success{Success: true},
 	})
 }
+
+func WeightEventList(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.WeightList(c, &req, pagination)
+	if err != nil {
+		apierr.ClassifiedAbort(c, err)
+		return
+	}
+	ginutil.JSONResp(c, res)
+}
+
+func WeightEventCreate(c *gin.Context) {
+	var req pasturePb.WeightEventRequest
+	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.WeightAt, valid.Required),
+		valid.Field(&req.Weight, valid.Required),
+		valid.Field(&req.StaffMemberId, valid.Required),
+	); err != nil {
+		apierr.AbortBadRequest(c, http.StatusBadRequest, err)
+		return
+	}
+
+	if err := middleware.BackendOperation(c).OpsService.CreateWeight(c, &req); err != nil {
+		apierr.ClassifiedAbort(c, err)
+		return
+	}
+
+	ginutil.JSONResp(c, &operationPb.CommonOK{
+		Code: http.StatusOK,
+		Msg:  "ok",
+		Data: &operationPb.Success{Success: true},
+	})
+}

+ 4 - 0
http/route/event_api.go

@@ -37,5 +37,9 @@ func EventAPI(opts ...func(engine *gin.Engine)) func(s *gin.Engine) {
 		// 冻精
 		eventRoute.POST("/frozen/seme/list", event.FrozenSemeList)
 		eventRoute.POST("/frozen/seme/create", event.FrozenSemeCreate)
+
+		// 称重
+		eventRoute.POST("/weight/list", event.WeightEventList)
+		eventRoute.POST("/weight/create", event.WeightEventCreate)
 	}
 }

+ 38 - 0
model/cow_weight.go

@@ -0,0 +1,38 @@
+package model
+
+import pasturePb "gitee.com/xuyiping_admin/go_proto/proto/go/backend/cow"
+
+type EventWeight struct {
+	ID            int64  `json:"id"`
+	CowId         int64  `json:"cow_id"`
+	EarNumber     string `json:"ear_number"`
+	DayAge        int32  `json:"day_age"`
+	LactationDay  int64  `json:"lactation_day"`
+	PenId         int32  `json:"pen_id"`
+	Lact          int32  `json:"lact"`
+	Weight        int32  `json:"weight"`
+	WeightAt      int64  `json:"weight_at"`
+	Remarks       string `json:"remarks"`
+	OperationId   int32  `json:"operation_id"`
+	StaffMemberId int32  `json:"staff_member_id"`
+	CreatedAt     int64  `json:"created_at"`
+	UpdatedAt     int64  `json:"updated_at"`
+}
+
+func (c *EventWeight) TableName() string {
+	return "event_weight"
+}
+
+func NewEventWeight(cow *Cow, operationId int64, req *pasturePb.WeightEventRequest) *EventWeight {
+	return &EventWeight{
+		CowId:         cow.Id,
+		EarNumber:     cow.EarNumber,
+		Weight:        int32(req.Weight * 100),
+		Lact:          cow.Lact,
+		DayAge:        cow.GetDayAge(),
+		WeightAt:      int64(req.WeightAt),
+		Remarks:       req.Remarks,
+		StaffMemberId: req.StaffMemberId,
+		OperationId:   int32(operationId),
+	}
+}

+ 54 - 0
module/backend/event.go

@@ -541,3 +541,57 @@ func (s *StoreEntry) FrozenSemenCreate(ctx context.Context, req *pasturePb.Searc
 	}
 	return nil
 }
+
+func (s *StoreEntry) WeightList(ctx context.Context, req *pasturePb.SearchEventRequest, pagination *pasturePb.PaginationModel) (*pasturePb.SearchWeightEventResponse, error) {
+	weightList := make([]*pasturePb.SearchWeightList, 0)
+	var count int64 = 0
+	pref := s.DB.Table(fmt.Sprintf("%s as a", new(model.EventWeight).TableName())).
+		Select(`a.id,a.cow_id,a.ear_number,a.weight / 100 as weight,a.lact,a.day_age,a.weight_at,a.remarks,a.created_at,
+		a.updated_at,a.staff_member_id,a.operation_id,b.name as staff_member_name,c.name as operation_name`).
+		Joins(fmt.Sprintf("JOIN %s AS b on a.staff_member_id = b.id", new(model.SystemUser).TableName())).
+		Joins(fmt.Sprintf("JOIN %s AS c on a.operation_id = c.id", new(model.SystemUser).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").
+		Count(&count).Limit(int(pagination.PageSize)).
+		Offset(int(pagination.PageOffset)).
+		Find(&weightList).Error; err != nil {
+		return nil, xerr.WithStack(err)
+	}
+
+	return &pasturePb.SearchWeightEventResponse{
+		Code:    http.StatusOK,
+		Message: "ok",
+		Data: &pasturePb.SearchWeightData{
+			List:     weightList,
+			Total:    int32(count),
+			PageSize: pagination.PageSize,
+			Page:     pagination.Page,
+		},
+	}, nil
+}
+
+func (s *StoreEntry) CreateWeight(ctx context.Context, req *pasturePb.WeightEventRequest) error {
+	if len(req.CowId) <= 0 {
+		return xerr.Custom("请选择相关牛只")
+	}
+	currentSystemUser, _ := s.GetCurrentSystemUser(ctx)
+	cowList := strings.Split(req.CowId, ",")
+	weightEvent := make([]*model.EventWeight, 0)
+	for _, cowIdStr := range cowList {
+		cowId, _ := strconv.ParseInt(cowIdStr, 10, 64)
+		cow, err := s.GetCowInfo(ctx, cowId)
+		if err != nil {
+			return xerr.WithStack(err)
+		}
+		weightEvent = append(weightEvent, model.NewEventWeight(cow, currentSystemUser.Id, req))
+	}
+	if len(weightEvent) <= 0 {
+		return nil
+	}
+
+	return s.DB.Create(weightEvent).Error
+}

+ 2 - 0
module/backend/interface.go

@@ -144,6 +144,8 @@ type EventService interface {
 	EstrusCreate(ctx context.Context, req *pasturePb.EstrusEventRequest) error
 	FrozenSemenList(ctx context.Context, req *pasturePb.FrozenSemenRequest, pagination *pasturePb.PaginationModel) (*pasturePb.FrozenSemenResponse, error)
 	FrozenSemenCreate(ctx context.Context, req *pasturePb.SearchFrozenSemenList) error
+	WeightList(ctx context.Context, req *pasturePb.SearchEventRequest, pagination *pasturePb.PaginationModel) (*pasturePb.SearchWeightEventResponse, error)
+	CreateWeight(ctx context.Context, req *pasturePb.WeightEventRequest) error
 }
 
 //go:generate mockgen -destination mock/CowService.go -package kptservicemock kpt-pasture/module/backend CowService

+ 0 - 2
module/backend/pasture.go

@@ -415,12 +415,10 @@ func (s *StoreEntry) CreateOrUpdateSemeTime(ctx context.Context, req *pasturePb.
 	if err := s.DB.Transaction(func(tx *gorm.DB) error {
 		semeTime := model.NewEventSemeTime(currentUser, req)
 		semeTimeFlow := model.NewEventSemeTimeFlow(semeTime.Id, req)
-
 		if req.Form.Id > 0 {
 			if err := tx.Model(new(model.EventSemeTime)).Where("id = ?", req.Form.Id).Updates(semeTime).Error; err != nil {
 				return xerr.WithStack(err)
 			}
-
 			if err := tx.Model(new(model.EventSemeTimeFlow)).Where("seme_time_id = ?", req.Form.Id).Updates(semeTimeFlow).Error; err != nil {
 				return xerr.WithStack(err)
 			}