Browse Source

event: 体况评分

Yi 10 months ago
parent
commit
59128086db

+ 1 - 1
go.mod

@@ -3,7 +3,7 @@ module kpt-pasture
 go 1.17
 
 require (
-	gitee.com/xuyiping_admin/go_proto v0.0.0-20240510075137-e44046757a95
+	gitee.com/xuyiping_admin/go_proto v0.0.0-20240511081455-b5ea1fc3fb2c
 	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

@@ -60,6 +60,12 @@ gitee.com/xuyiping_admin/go_proto v0.0.0-20240509093937-40d0124cdcc6 h1:TMyoyrhi
 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/go_proto v0.0.0-20240511061932-cc92d4da4504 h1:TH+jY6anSWvB8SUv0tR8X70bToA84ehgTWJ6A9X6vx0=
+gitee.com/xuyiping_admin/go_proto v0.0.0-20240511061932-cc92d4da4504/go.mod h1:x47UOU+lOkZnrtAENAsOGd7mZ5I8D2JRkMKMqLLRlVw=
+gitee.com/xuyiping_admin/go_proto v0.0.0-20240511063932-87bb586873ec h1:xFue7pGVKPAPOyus0RUpoY09hwfeXFW+ThcDIQN1NWc=
+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/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=

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

@@ -121,3 +121,24 @@ func GroupTransferEventCreate(c *gin.Context) {
 		Data: &operationPb.Success{Success: true},
 	})
 }
+
+func BodyScoreEventList(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.BodyScoreList(c, &req, pagination)
+	if err != nil {
+		apierr.ClassifiedAbort(c, err)
+		return
+	}
+	ginutil.JSONResp(c, res)
+}

+ 2 - 0
http/route/event_api.go

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

+ 10 - 1
model/cow.go

@@ -1,6 +1,11 @@
 package model
 
-import pasturePb "gitee.com/xuyiping_admin/go_proto/proto/go/backend/cow"
+import (
+	"math"
+	"time"
+
+	pasturePb "gitee.com/xuyiping_admin/go_proto/proto/go/backend/cow"
+)
 
 type Cow struct {
 	Id                  int64                  `json:"id"`
@@ -69,3 +74,7 @@ func NewCow(req *pasturePb.SearchEnterData) *Cow {
 		LastPregnantCheckAt: int64(req.PregnancyCheckAt),
 	}
 }
+
+func (c *Cow) GetDayAge() int32 {
+	return int32(math.Floor(float64(time.Now().Unix()-c.BirthAt) / 86400))
+}

+ 19 - 0
model/event_body_score.go

@@ -0,0 +1,19 @@
+package model
+
+type EventBodyScore struct {
+	Id            int64  `json:"id"`
+	CowId         int64  `json:"cow_id"`
+	Score         int32  `json:"score"`
+	Lact          int32  `json:"lact"`
+	DayAge        int32  `json:"day_age"`
+	ScoreAt       int64  `json:"score_at"`
+	Remarks       string `json:"remarks"`
+	StaffMemberId int64  `json:"staff_member_id"`
+	OperationId   int64  `json:"operation_id"`
+	CreatedAt     int64  `json:"created_at"`
+	UpdatedAt     int64  `json:"updated_at"`
+}
+
+func (e *EventBodyScore) TableName() string {
+	return "event_body_score"
+}

+ 7 - 0
model/event_enter.go

@@ -1,6 +1,9 @@
 package model
 
 import (
+	"math"
+	"time"
+
 	pasturePb "gitee.com/xuyiping_admin/go_proto/proto/go/backend/cow"
 )
 
@@ -15,6 +18,7 @@ type EventEnter struct {
 	CowTypeId        int32                  `json:"cow_type_id"`
 	BreedStatusId    int32                  `json:"breed_status_id"`
 	Lact             int32                  `json:"lact"`
+	DayAge           int32                  `json:"day_age"`
 	PenId            int32                  `json:"pen_id"`
 	CowKindId        int32                  `json:"cow_kind_id"`
 	Status           int32                  `json:"status"`
@@ -30,6 +34,7 @@ type EventEnter struct {
 	Weight           int64                  `json:"weight"`
 	Price            int64                  `json:"price"`
 	OperationId      int64                  `json:"operation_id"`
+	StaffMemberId    int64                  `json:"staff_member_id"`
 	CreatedAt        int64                  `json:"created_at"`
 	UpdatedAt        int64                  `json:"updated_at"`
 }
@@ -47,6 +52,7 @@ func NewEventEnter(cowId, operationId int64, req *pasturePb.SearchEnterData) *Ev
 		CowTypeId:        req.CowTypeId,
 		BreedStatusId:    req.BreedStatusId,
 		Lact:             req.Lact,
+		DayAge:           int32(math.Floor(float64(int32(time.Now().Unix())-req.BirthAt) / 86400)),
 		PenId:            req.PenId,
 		CowKindId:        req.CowKindId,
 		Status:           1,
@@ -61,6 +67,7 @@ func NewEventEnter(cowId, operationId int64, req *pasturePb.SearchEnterData) *Ev
 		Remarks:          req.Remarks,
 		Weight:           int64(req.Weight * 100),
 		Price:            int64(req.Price * 100),
+		StaffMemberId:    int64(req.StaffMemberId),
 		OperationId:      operationId,
 	}
 }

+ 1 - 0
model/event_transfer_group.go

@@ -5,6 +5,7 @@ type EventTransferGroup struct {
 	CowId            int64  `json:"cow_id"`
 	TransferInPenId  int64  `json:"transfer_in_pen_id"`
 	TransferOutPenId int64  `json:"transfer_out_pen_id"`
+	DayAge           int32  `json:"day_age"`
 	Lact             int32  `json:"lact"`
 	TransferDate     string `json:"transfer_date"`
 	TransferReasonId int64  `json:"transfer_reason_id"`

+ 47 - 6
module/backend/event.go

@@ -12,20 +12,29 @@ import (
 	"gorm.io/gorm"
 )
 
+func (s *StoreEntry) EventTemplate(ctx context.Context, req *pasturePb.SearchEventRequest) {
+
+}
+
+func (s *StoreEntry) CreateEventTemplate(ctx context.Context, req *pasturePb.SearchEventRequest) *pasturePb.EventData {
+	return nil
+}
+
 func (s *StoreEntry) EnterList(ctx context.Context, req *pasturePb.SearchEventRequest, pagination *pasturePb.PaginationModel) (*pasturePb.SearchEnterEventResponse, error) {
 	eventEnterList := make([]*pasturePb.SearchEnterData, 0)
 	var count int64 = 0
 	pref := s.DB.Table(fmt.Sprintf("%s as a", new(model.EventEnter).TableName())).
-		Select(`a.id,a.birth_at,a.weaning_at,a.dry_milk_at,a.cow_source_id,a.remarks,a.mating_at,a.status,
-			a.lact,a.breed_status_id,a.mother_id,a.cow_kind_id,a.cow_id,a.ear_number,a.sex,a.created_at,
-			a.weight / 100 as weight,a.price / 100 as price,b.name as breed_status_name,
-			c.name as cow_source_name,d.name as cow_type_name,e.name as cow_kind_name,f.name as pen_name,g.name as operation_name`).
+		Select(`a.id,a.birth_at,a.weaning_at,a.dry_milk_at,a.cow_source_id,a.remarks,a.mating_at,a.status,a.lact,
+		a.breed_status_id,a.mother_id,a.cow_kind_id,a.cow_id,a.ear_number,a.sex,a.created_at,a.updated_at,a.enter_at,
+		a.weight / 100 as weight,a.price / 100 as price,b.name as breed_status_name,c.name as cow_source_name,d.name as cow_type_name,
+		e.name as cow_kind_name,f.name as pen_name,g.name as operation_name,h.name as staff_member_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 c on a.cow_source_id = c.id", new(model.ConfigCowSource).TableName())).
 		Joins(fmt.Sprintf("JOIN %s AS d on a.cow_type_id = d.id", new(model.ConfigCowType).TableName())).
 		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()))
+		Joins(fmt.Sprintf("JOIN %s AS g on a.operation_id = g.id", new(model.SystemUser).TableName())).
+		Joins(fmt.Sprintf("JOIN %s AS h on a.staff_member_id = h.id", new(model.SystemUser).TableName()))
 	if len(req.CowId) > 0 {
 		cowIds := strings.Split(req.CowId, ",")
 		pref.Where("a.cow_id IN ?", cowIds)
@@ -61,7 +70,6 @@ func (s *StoreEntry) CreateEnter(ctx context.Context, req *pasturePb.SearchEnter
 		if err := tx.Create(model.NewEventEnter(newCowData.Id, operationUser.Id, req)).Error; err != nil {
 			return xerr.WithStack(err)
 		}
-
 		return nil
 	}); err != nil {
 		return xerr.WithStack(err)
@@ -137,6 +145,7 @@ func (s *StoreEntry) CreateGroupTransfer(ctx context.Context, req *pasturePb.Tra
 			TransferInPenId:  int64(v.TransferInPenId),
 			TransferOutPenId: pen.Id,
 			Lact:             cow.Lact,
+			DayAge:           cow.GetDayAge(),
 			TransferDate:     v.TransferDate,
 			TransferReasonId: transferReasonData.Id,
 			Remarks:          v.Remarks,
@@ -168,3 +177,35 @@ func (s *StoreEntry) CreateGroupTransfer(ctx context.Context, req *pasturePb.Tra
 
 	return nil
 }
+
+func (s *StoreEntry) BodyScoreList(ctx context.Context, req *pasturePb.SearchEventRequest, pagination *pasturePb.PaginationModel) (*pasturePb.SearchBodyScoreEventResponse, error) {
+	bodyScoreList := make([]*pasturePb.SearchBodyScoreList, 0)
+	var count int64 = 0
+	pref := s.DB.Table(fmt.Sprintf("%s as a", new(model.EventBodyScore).TableName())).
+		Select(`a.id,a.cow_id,a.ear_number,a.score as body_score,a.lact,a.day_age,a.score_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(&bodyScoreList).Error; err != nil {
+		return nil, xerr.WithStack(err)
+	}
+
+	return &pasturePb.SearchBodyScoreEventResponse{
+		Code:    http.StatusOK,
+		Message: "ok",
+		Data: &pasturePb.SearchBodyScoreData{
+			List:     bodyScoreList,
+			Total:    int32(count),
+			PageSize: pagination.PageSize,
+			Page:     pagination.Page,
+		},
+	}, nil
+}

+ 1 - 1
module/backend/interface.go

@@ -123,9 +123,9 @@ type ConfigDataService interface {
 type EventService interface {
 	EnterList(ctx context.Context, req *pasturePb.SearchEventRequest, pagination *pasturePb.PaginationModel) (*pasturePb.SearchEnterEventResponse, error)
 	CreateEnter(ctx context.Context, req *pasturePb.SearchEnterData) error
-
 	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)
 }
 
 //go:generate mockgen -destination mock/CowService.go -package kptservicemock kpt-pasture/module/backend CowService