瀏覽代碼

estrus: 发情数据批量提交

Yi 5 月之前
父節點
當前提交
ebc4cf8c94

+ 1 - 1
go.mod

@@ -3,7 +3,7 @@ module kpt-pasture
 go 1.17
 
 require (
-	gitee.com/xuyiping_admin/go_proto v0.0.0-20241219080808-6a83c815206e
+	gitee.com/xuyiping_admin/go_proto v0.0.0-20241223072631-d6d7cc8fef31
 	gitee.com/xuyiping_admin/pkg v0.0.0-20241108060137-caea58c59f5b
 	github.com/dgrijalva/jwt-go v3.2.0+incompatible
 	github.com/eclipse/paho.mqtt.golang v1.4.3

+ 10 - 0
go.sum

@@ -68,6 +68,16 @@ gitee.com/xuyiping_admin/go_proto v0.0.0-20241219074512-9ad3e0b600fb h1:2Acmom7X
 gitee.com/xuyiping_admin/go_proto v0.0.0-20241219074512-9ad3e0b600fb/go.mod h1:BKrFW6YLDectlQcQk3FYKBeXvjEiodAKJ5rq7O/QiPE=
 gitee.com/xuyiping_admin/go_proto v0.0.0-20241219080808-6a83c815206e h1:lSDWnqRTfBokXgiH6bGdnDwFFfewIp1fbgpkA8iadDY=
 gitee.com/xuyiping_admin/go_proto v0.0.0-20241219080808-6a83c815206e/go.mod h1:BKrFW6YLDectlQcQk3FYKBeXvjEiodAKJ5rq7O/QiPE=
+gitee.com/xuyiping_admin/go_proto v0.0.0-20241223023708-63ff08408266 h1:oLUtWC1Vme8B4znOYDM/0ZZBVsG5eMtFdy6KfnScLgo=
+gitee.com/xuyiping_admin/go_proto v0.0.0-20241223023708-63ff08408266/go.mod h1:BKrFW6YLDectlQcQk3FYKBeXvjEiodAKJ5rq7O/QiPE=
+gitee.com/xuyiping_admin/go_proto v0.0.0-20241223031557-299a2d417bba h1:so/AiSPsYkfQnlJgnmkjuQxm8Qhb2TEiRKCrfCNaI7o=
+gitee.com/xuyiping_admin/go_proto v0.0.0-20241223031557-299a2d417bba/go.mod h1:BKrFW6YLDectlQcQk3FYKBeXvjEiodAKJ5rq7O/QiPE=
+gitee.com/xuyiping_admin/go_proto v0.0.0-20241223035802-00146a704807 h1:zNVU/0dFGnqMW4iw1CiESC7efp7KHo3+HnHFdsAyO28=
+gitee.com/xuyiping_admin/go_proto v0.0.0-20241223035802-00146a704807/go.mod h1:BKrFW6YLDectlQcQk3FYKBeXvjEiodAKJ5rq7O/QiPE=
+gitee.com/xuyiping_admin/go_proto v0.0.0-20241223063242-cbd9ca4ef527 h1:bwLz4q7u5nxs7PGarS/zl6hgK5E7hcOMKSBfibEKdM4=
+gitee.com/xuyiping_admin/go_proto v0.0.0-20241223063242-cbd9ca4ef527/go.mod h1:BKrFW6YLDectlQcQk3FYKBeXvjEiodAKJ5rq7O/QiPE=
+gitee.com/xuyiping_admin/go_proto v0.0.0-20241223072631-d6d7cc8fef31 h1:KSfnFVKSlAyC7A6jCWAJkvAGxq/IVPsezl5ss3CqPMw=
+gitee.com/xuyiping_admin/go_proto v0.0.0-20241223072631-d6d7cc8fef31/go.mod h1:BKrFW6YLDectlQcQk3FYKBeXvjEiodAKJ5rq7O/QiPE=
 gitee.com/xuyiping_admin/pkg v0.0.0-20241108060137-caea58c59f5b h1:w05MxH7yqveRlaRbxHhbif5YjPrJFodRPfOjYhXn7Zk=
 gitee.com/xuyiping_admin/pkg v0.0.0-20241108060137-caea58c59f5b/go.mod h1:8tF25X6pE9WkFCczlNAC0K2mrjwKvhhp02I7o0HtDxY=
 github.com/BurntSushi/toml v0.3.1/go.mod h1:xHWCNGjB5oqiDr8zfno3MHue2Ht5sIBksp03qcyfWMU=

+ 5 - 37
http/handler/event/event_breed.go

@@ -180,7 +180,7 @@ func MatingCreate(c *gin.Context) {
 }
 
 func EstrusEventList(c *gin.Context) {
-	var req pasturePb.SearchEventRequest
+	var req pasturePb.EstrusItemsRequest
 	if err := ginutil.BindProto(c, &req); err != nil {
 		apierr.AbortBadRequest(c, http.StatusBadRequest, err)
 		return
@@ -200,7 +200,7 @@ func EstrusEventList(c *gin.Context) {
 	ginutil.JSONResp(c, res)
 }
 
-func EstrusCreate(c *gin.Context) {
+func EstrusBatch(c *gin.Context) {
 	var req pasturePb.EventEstrus
 	if err := ginutil.BindProto(c, &req); err != nil {
 		apierr.AbortBadRequest(c, http.StatusBadRequest, err)
@@ -208,42 +208,10 @@ func EstrusCreate(c *gin.Context) {
 	}
 
 	if err := valid.ValidateStruct(&req,
-		valid.Field(&req.CowId, valid.Required),
+		valid.Field(&req.CowIds, valid.Required),
+		valid.Field(&req.BullNumber, valid.Required),
 		valid.Field(&req.OperationId, 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},
-	})
-}
-
-func EstrusBatch(c *gin.Context) {
-	var req pasturePb.EventEstrusBatch
-	if err := ginutil.BindProto(c, &req); err != nil {
-		apierr.AbortBadRequest(c, http.StatusBadRequest, err)
-		return
-	}
-
-	if err := valid.ValidateStruct(&req,
-		valid.Field(&req.Item, valid.Required, valid.Each(valid.By(func(value interface{}) error {
-			item := value.(pasturePb.EventEstrus)
-			return valid.ValidateStruct(&item, valid.Field(&item.CowId, valid.Required),
-				valid.Field(&item.OperationId, valid.Required),
-				valid.Field(&item.IsMathing, valid.Required),
-				valid.Field(&item.UnMatingReasons, valid.Required),
-			)
-		}))),
+		valid.Field(&req.Consumption, valid.Required),
 	); err != nil {
 		apierr.AbortBadRequest(c, http.StatusBadRequest, err)
 		return

+ 22 - 0
http/handler/event/event_health.go

@@ -198,3 +198,25 @@ func CowDiseaseCurable(c *gin.Context) {
 		Data: &operationPb.Success{Success: true},
 	})
 }
+
+func EstrusList(c *gin.Context) {
+	var req pasturePb.EstrusItemsRequest
+	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.BackendOperation(c).OpsService.EstrusList(c, &req, pagination)
+	if err != nil {
+		apierr.ClassifiedAbort(c, err)
+		return
+	}
+
+	ginutil.JSONResp(c, res)
+}

+ 0 - 2
http/route/event_api.go

@@ -38,8 +38,6 @@ 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)
 		eventRoute.POST("/estrus/batch", event.EstrusBatch)
 		// 同期
 		eventRoute.POST("/same/time/list", event.SameTimeList)

+ 1 - 1
http/route/work_api.go

@@ -31,6 +31,6 @@ func WorkOrderAPI(opts ...func(engine *gin.Engine)) func(s *gin.Engine) {
 		workRoute.POST("/mating/items", work.MatingCowList)
 		workRoute.POST("/calving/items", work.CalvingList)
 		workRoute.POST("/disease/items", event.CowDiseaseList)
-
+		workRoute.POST("/estrus/items", event.EstrusList)
 	}
 }

+ 1 - 0
model/cow.go

@@ -16,6 +16,7 @@ type Cow struct {
 	EarNumber           string                         `json:"earNumber"`
 	EarOldNumber        string                         `json:"earOldNumber"`
 	PenId               int32                          `json:"penId"`
+	PenName             string                         `json:"penName"`
 	Lact                int32                          `json:"lact"`
 	DayAge              int32                          `json:"dayAge"`
 	CalvingAge          int64                          `json:"calvingAge"`

+ 52 - 59
model/event_estrus.go

@@ -1,37 +1,33 @@
 package model
 
 import (
-	"time"
+	"fmt"
+	"kpt-pasture/store/kptstore"
 
 	pasturePb "gitee.com/xuyiping_admin/go_proto/proto/go/backend/cow"
 )
 
 type EventEstrus struct {
-	Id                  int64                           `json:"id"`
-	CowId               int64                           `json:"cowId"`
-	DayAge              int32                           `json:"dayAge"`
-	Lact                int32                           `json:"lact"`
-	CalvingAge          int32                           `json:"calvingAge"`
-	ExposeEstrusType    pasturePb.ExposeEstrusType_Kind `json:"exposeEstrusType"`
-	FilterHigh          int32                           `json:"filter_high"`
-	EstrusDate          string                          `json:"estrusDate"`
-	ActiveDate          string                          `json:"activeDate"`
-	LastEstrusDate      string                          `json:"lastEstrusDate"`
-	Level               pasturePb.EstrusLevel_Kind      `json:"level"`
-	IsPeak              pasturePb.IsShow_Kind           `json:"isPeak"`
-	PerTwentyFourHigh   int32                           `json:"perTwentyFourHigh"`
-	Result              pasturePb.EstrusResult_Kind     `json:"result"`
-	UnMatingReasons     pasturePb.UnMatingReasons_Kind  `json:"unMatingReasons"`
-	UnMatingReasonsName string                          `json:"unMatingReasonsName"`
-	IsMating            pasturePb.IsShow_Kind           `json:"isMating"`
-	Remarks             string                          `json:"remarks"`
-	IsShow              pasturePb.IsShow_Kind           `json:"isShow"`
-	OperationId         int64                           `json:"operationId"`
-	OperationName       string                          `json:"operationName"`
-	MessageId           int64                           `json:"messageId"`
-	MessageName         string                          `json:"messageName"`
-	CreatedAt           int64                           `json:"createdAt"`
-	UpdatedAt           int64                           `json:"updatedAt"`
+	Id                int64                           `json:"id"`
+	CowId             int64                           `json:"cowId"`
+	Lact              int32                           `json:"lact"`
+	ExposeEstrusType  pasturePb.ExposeEstrusType_Kind `json:"exposeEstrusType"`
+	FilterHigh        int32                           `json:"filter_high"`
+	EstrusDate        string                          `json:"estrusDate"`
+	ActiveDate        string                          `json:"activeDate"`
+	LastEstrusDate    string                          `json:"lastEstrusDate"`
+	Level             pasturePb.EstrusLevel_Kind      `json:"level"`
+	IsPeak            pasturePb.IsShow_Kind           `json:"isPeak"`
+	PerTwentyFourHigh int32                           `json:"perTwentyFourHigh"`
+	Result            pasturePb.EstrusResult_Kind     `json:"result"`
+	Remarks           string                          `json:"remarks"`
+	IsShow            pasturePb.IsShow_Kind           `json:"isShow"`
+	OperationId       int64                           `json:"operationId"`
+	OperationName     string                          `json:"operationName"`
+	MessageId         int64                           `json:"messageId"`
+	MessageName       string                          `json:"messageName"`
+	CreatedAt         int64                           `json:"createdAt"`
+	UpdatedAt         int64                           `json:"updatedAt"`
 }
 
 func (e *EventEstrus) TableName() string {
@@ -41,50 +37,47 @@ func (e *EventEstrus) TableName() string {
 func NewEventEstrus(
 	exposeEstrusType pasturePb.ExposeEstrusType_Kind,
 	cow *Cow,
-	currentUser *SystemUser,
-	operation *SystemUser,
-	req *pasturePb.EventEstrus,
+	estrusDate, remarks string,
 ) *EventEstrus {
 	return &EventEstrus{
 		CowId:            cow.Id,
-		DayAge:           cow.GetDayAge(),
 		Lact:             cow.Lact,
-		CalvingAge:       int32(cow.CalvingAge),
-		EstrusDate:       time.Unix(int64(req.EstrusAt), 0).Format(LayoutTime),
-		Remarks:          req.Remarks,
-		IsMating:         req.IsMathing,
-		UnMatingReasons:  req.UnMatingReasons,
-		MessageId:        currentUser.Id,
-		MessageName:      currentUser.Name,
-		OperationId:      operation.Id,
-		OperationName:    operation.Name,
 		ExposeEstrusType: exposeEstrusType,
+		EstrusDate:       estrusDate,
+		Remarks:          remarks,
 	}
 }
 
 type EstrusSlice []*EventEstrus
 
-func (e EstrusSlice) ToPB() []*pasturePb.SearchEstrusList {
-	res := make([]*pasturePb.SearchEstrusList, len(e))
+func (e EstrusSlice) ToPB(
+	dB *kptstore.DB,
+	getCowInfo func(dB *kptstore.DB, cowId int64) *Cow,
+	getCowLastEvent func(DB *kptstore.DB, cowId int64, eventCategoryId pasturePb.EventCategory_Kind) *EventCowLog,
+) []*pasturePb.EstrusItems {
+	res := make([]*pasturePb.EstrusItems, len(e))
 	for i, v := range e {
-		res[i] = &pasturePb.SearchEstrusList{
-			Id:     int32(v.Id),
-			CowId:  int32(v.CowId),
-			DayAge: v.DayAge,
-			Lact:   v.Lact,
-			//EstrusAt:            int32(v.EstrusAt),
-			Level:               v.Level,
-			Result:              v.Result,
-			IsMathing:           v.IsMating,
-			UnMatingReasonsName: v.UnMatingReasonsName,
-			CalvingAge:          v.CalvingAge,
-			MessengerId:         int32(v.MessageId),
-			MessengerName:       v.MessageName,
-			Remarks:             v.Remarks,
-			OperationId:         int32(v.OperationId),
-			OperationName:       v.OperationName,
-			CreatedAt:           int32(v.CreatedAt),
-			UpdatedAt:           int32(v.UpdatedAt),
+		cowInfo := getCowInfo(dB, v.CowId)
+		lastEventLog := getCowLastEvent(dB, v.CowId, pasturePb.EventCategory_Breed)
+		planDay, optimumMatingTime := "", ""
+		lastBreedEvnetDetails := ""
+		if lastEventLog != nil {
+			lastBreedEvnetDetails = fmt.Sprintf("%s %s", lastEventLog.EventTypeName, lastEventLog.EventDescription)
+		}
+		res[i] = &pasturePb.EstrusItems{
+			Id:                     int32(v.Id),
+			CowId:                  int32(v.CowId),
+			DayAge:                 cowInfo.DayAge,
+			Lact:                   cowInfo.Lact,
+			Level:                  v.Level,
+			PenName:                cowInfo.PenName,
+			Status:                 v.IsShow,
+			CalvingAge:             int32(cowInfo.CalvingAge),
+			PlanDay:                planDay,
+			MatingTimes:            cowInfo.MatingTimes,
+			OptimumMatingStartTime: optimumMatingTime,
+			OptimumMatingEndTime:   optimumMatingTime,
+			LastBreedEvnetDetails:  lastBreedEvnetDetails,
 		}
 	}
 	return res

+ 1 - 0
model/event_mating.go

@@ -30,6 +30,7 @@ type EventMating struct {
 	MessageId         int64                           `json:"messageId"`
 	MessageName       string                          `json:"messageName"`
 	Remarks           string                          `json:"remarks"`
+	EventEstrusId     int64                           `json:"eventEstrusId"`
 	CreatedAt         int64                           `json:"createdAt"`
 	UpdatedAt         int64                           `json:"updatedAt"`
 }

+ 61 - 42
module/backend/event_breed.go

@@ -6,6 +6,7 @@ import (
 	"kpt-pasture/model"
 	"kpt-pasture/util"
 	"net/http"
+	"strconv"
 	"strings"
 	"time"
 
@@ -133,16 +134,25 @@ func (s *StoreEntry) CalvingCreate(ctx context.Context, req *pasturePb.EventCalv
 	return nil
 }
 
-func (s *StoreEntry) EstrusList(ctx context.Context, req *pasturePb.SearchEventRequest, pagination *pasturePb.PaginationModel) (*pasturePb.EstrusEventResponse, error) {
+func (s *StoreEntry) EstrusList(ctx context.Context, req *pasturePb.EstrusItemsRequest, pagination *pasturePb.PaginationModel) (*pasturePb.EstrusItemsResponse, error) {
 	estrusList := make([]*model.EventEstrus, 0)
 	var count int64 = 0
 	pref := s.DB.Table(new(model.EventEstrus).TableName()).
 		Where("is_show = ?", pasturePb.IsShow_Ok)
-	if len(req.CowId) > 0 {
-		cowIds := strings.Split(req.CowId, ",")
+	if len(req.CowIds) > 0 {
+		cowIds := strings.Split(util.ArrayInt32ToStrings(req.CowIds, ","), ",")
 		pref.Where("cow_id IN ?", cowIds)
 	}
 
+	if req.Level > 0 {
+		pref.Where("level = ?", req.Level)
+	}
+
+	if len(req.PenIds) > 0 {
+		penIds := strings.Split(util.ArrayInt32ToStrings(req.PenIds, ","), ",")
+		pref.Where("pen_id IN ?", penIds)
+	}
+
 	if err := pref.Order("id desc").
 		Count(&count).Limit(int(pagination.PageSize)).
 		Offset(int(pagination.PageOffset)).
@@ -150,11 +160,13 @@ func (s *StoreEntry) EstrusList(ctx context.Context, req *pasturePb.SearchEventR
 		return nil, xerr.WithStack(err)
 	}
 
-	return &pasturePb.EstrusEventResponse{
+	getCowInfoByCowId := GetCowInfoByCowId
+	getCowLastEvent := GetCowLastEvent
+	return &pasturePb.EstrusItemsResponse{
 		Code:    http.StatusOK,
 		Message: "ok",
-		Data: &pasturePb.SearchEstrusData{
-			List:     model.EstrusSlice(estrusList).ToPB(),
+		Data: &pasturePb.EstrusItemsData{
+			List:     model.EstrusSlice(estrusList).ToPB(s.DB, getCowInfoByCowId, getCowLastEvent),
 			Total:    int32(count),
 			PageSize: pagination.PageSize,
 			Page:     pagination.Page,
@@ -162,53 +174,60 @@ func (s *StoreEntry) EstrusList(ctx context.Context, req *pasturePb.SearchEventR
 	}, nil
 }
 
-func (s *StoreEntry) EstrusCreate(ctx context.Context, req *pasturePb.EventEstrus) error {
-	estrusInfo, err := s.EventEstrusCheck(ctx, req)
+func (s *StoreEntry) EstrusBatch(ctx context.Context, req *pasturePb.EventEstrus) error {
+	eventMatingList := make([]*model.EventMating, 0)
+	currentUser, err := s.GetCurrentSystemUser(ctx)
 	if err != nil {
-		return xerr.Custom("获取当前登录用户失败")
+		return xerr.Custom("当前用户信息错误")
+	}
+	operationUser, err := s.GetSystemUserById(ctx, int64(req.OperationId))
+	if err != nil {
+		return xerr.Customf("该用户不存在: %d", req.OperationId)
 	}
-	if err = s.DB.Transaction(func(tx *gorm.DB) error {
 
-		// 发情信息
-		if err = tx.Create(estrusInfo).Error; err != nil {
-			return xerr.WithStack(err)
+	eventEstrusIds := make([]string, 0)
+	for _, cowId := range req.CowIds {
+		cowInfo := GetCowInfoByCowId(s.DB, int64(cowId))
+		if cowInfo == nil {
+			return xerr.Custom("牛只信息不存在")
 		}
 
-		// 配种信息
-		if estrusInfo.IsMating == pasturePb.IsShow_Ok {
-			cow, _ := s.GetCowInfoByCowId(ctx, int64(req.CowId))
-			planAt, _ := time.Parse(model.LayoutTime, estrusInfo.EstrusDate)
-			newEventMating := model.NewEventMating(cow, planAt.Unix(), estrusInfo.ExposeEstrusType)
-			if err = tx.Create(newEventMating).Error; err != nil {
-				return xerr.WithStack(err)
-			}
+		newEventMating := model.NewEventMating(cowInfo, time.Now().Unix(), pasturePb.ExposeEstrusType_Neck_Ring)
+		eventEstrus, ok := s.EventEstrusIsExist(ctx, int64(cowId))
+		if ok {
+			newEventMating.EventEstrusId = eventEstrus.Id
+			newEventMating.MatingTimes = cowInfo.MatingTimes + 1
+			newEventMating.Status = pasturePb.IsShow_Ok
+			newEventMating.RealityDay = int64(req.MatingAt)
+			newEventMating.OperationId = operationUser.Id
+			newEventMating.OperationName = operationUser.Name
+			newEventMating.FrozenSemenNumber = req.BullNumber
+			newEventMating.Remarks = req.Remarks
+			newEventMating.MessageId = currentUser.Id
+			newEventMating.MessageName = currentUser.Name
+			eventEstrusIds = append(eventEstrusIds, strconv.FormatInt(eventEstrus.Id, 10))
 		}
-		return nil
-	}); err != nil {
-		return xerr.WithStack(err)
+		eventMatingList = append(eventMatingList, newEventMating)
 	}
-	return nil
-}
 
-func (s *StoreEntry) EstrusBatch(ctx context.Context, req *pasturePb.EventEstrusBatch) error {
-	estrusList := make([]*model.EventEstrus, 0)
-	eventMatingList := make([]*model.EventMating, 0)
-	for _, v := range req.Item {
-		estrusInfo, err := s.EventEstrusCheck(ctx, v)
-		if err != nil {
-			return xerr.Custom("获取当前登录用户失败")
-		}
-		estrusList = append(estrusList, estrusInfo)
-		cow, _ := s.GetCowInfoByCowId(ctx, int64(v.CowId))
-		planAt, _ := time.Parse(model.LayoutTime, estrusInfo.EstrusDate)
-		eventMatingList = append(eventMatingList, model.NewEventMating(cow, planAt.Unix(), estrusInfo.ExposeEstrusType))
+	if len(eventMatingList) <= 0 {
+		return nil
 	}
-	if err := s.DB.Transaction(func(tx *gorm.DB) error {
-		if err := tx.Create(estrusList).Error; err != nil {
-			return xerr.WithStack(err)
+
+	if err = s.DB.Transaction(func(tx *gorm.DB) error {
+		if len(eventEstrusIds) > 0 {
+			if err = tx.Model(new(model.EventEstrus)).
+				Where("id IN ?", eventEstrusIds).
+				Updates(map[string]interface{}{
+					"is_show": pasturePb.IsShow_No,
+					"result":  pasturePb.EstrusResult_Correct,
+				}).Error; err != nil {
+				return xerr.WithStack(err)
+			}
 		}
+
 		if len(eventMatingList) > 0 {
-			if err := tx.Create(eventMatingList).Error; err != nil {
+			if err = tx.Create(eventMatingList).Error; err != nil {
 				return xerr.WithStack(err)
 			}
 		}

+ 0 - 0
module/backend/event_breed_abortion.go → module/backend/event_breed_more.go


+ 0 - 22
module/backend/event_check.go

@@ -117,25 +117,3 @@ func (s *StoreEntry) PregnantCheckDataCheck(ctx context.Context, req *pasturePb.
 
 	return pregnantCheckBatchModelList, nil
 }
-
-func (s *StoreEntry) EventEstrusCheck(ctx context.Context, req *pasturePb.EventEstrus) (*model.EventEstrus, error) {
-	if req.CowId <= 0 {
-		return nil, xerr.Custom("请选择相关牛只")
-	}
-
-	cow, err := s.GetCowInfoByCowId(ctx, int64(req.CowId))
-	if err != nil {
-		return nil, xerr.WithStack(err)
-	}
-
-	currentUser, err := s.GetCurrentSystemUser(ctx)
-	if err != nil {
-		return nil, xerr.Custom("获取当前登录用户失败")
-	}
-
-	operationUser, err := s.GetSystemUserById(ctx, int64(req.OperationId))
-	if err != nil {
-		return nil, xerr.WithStack(err)
-	}
-	return model.NewEventEstrus(pasturePb.ExposeEstrusType_Natural_Estrus, cow, currentUser, operationUser, req), nil
-}

+ 3 - 4
module/backend/interface.go

@@ -167,10 +167,8 @@ type EventService interface {
 	// MatingList 配种
 	MatingList(ctx context.Context, req *pasturePb.SearchEventRequest, pagination *pasturePb.PaginationModel) (*pasturePb.MatingEventResponse, error)
 	MatingCreate(ctx context.Context, req *pasturePb.EventMating) error
-	// EstrusList 发情
-	EstrusList(ctx context.Context, req *pasturePb.SearchEventRequest, pagination *pasturePb.PaginationModel) (*pasturePb.EstrusEventResponse, error)
-	EstrusCreate(ctx context.Context, req *pasturePb.EventEstrus) error
-	EstrusBatch(ctx context.Context, req *pasturePb.EventEstrusBatch) error
+	// EstrusBatch 发情
+	EstrusBatch(ctx context.Context, req *pasturePb.EventEstrus) error
 	// AbortionList 流产
 	AbortionList(ctx context.Context, req *pasturePb.SearchEventRequest, pagination *pasturePb.PaginationModel) (*pasturePb.EventAbortionResponse, error)
 	AbortionCreate(ctx context.Context, req *pasturePb.EventAbortionRequest) error
@@ -267,4 +265,5 @@ type WorkService interface {
 	MatingCowList(ctx context.Context, req *pasturePb.ItemsRequest, pagination *pasturePb.PaginationModel) (*pasturePb.MatingItemsResponse, error)
 	CalvingCowList(ctx context.Context, req *pasturePb.ItemsRequest, pagination *pasturePb.PaginationModel) (*pasturePb.CalvingItemsResponse, error)
 	CowDiseaseList(ctx context.Context, req *pasturePb.SearchEventCowTreatmentRequest, pagination *pasturePb.PaginationModel) (*pasturePb.EventCowDiseaseResponse, error)
+	EstrusList(ctx context.Context, req *pasturePb.EstrusItemsRequest, pagination *pasturePb.PaginationModel) (*pasturePb.EstrusItemsResponse, error)
 }

+ 66 - 0
module/backend/sql.go

@@ -301,6 +301,22 @@ func (s *StoreEntry) GetLastEventMating(ctx context.Context, cowId int64) (*mode
 	return res, nil
 }
 
+func (s *StoreEntry) EventEstrusIsExist(ctx context.Context, cowId int64) (*model.EventEstrus, bool) {
+	newEventEstrus := &model.EventEstrus{}
+	if err := s.DB.Model(new(model.EventEstrus)).
+		Where("cow_id = ?", cowId).
+		Where("is_show = ? ", pasturePb.IsShow_Ok).
+		First(newEventEstrus).Error; err != nil {
+		if errors.Is(err, gorm.ErrRecordNotFound) {
+			return nil, false
+		}
+	}
+	if newEventEstrus.Id <= 0 {
+		return nil, false
+	}
+	return newEventEstrus, true
+}
+
 func (s *StoreEntry) GetOutboundById(ctx context.Context, id int64) (*model.Outbound, error) {
 	res := &model.Outbound{}
 	if err := s.DB.Model(new(model.Outbound)).
@@ -380,6 +396,32 @@ func (s *StoreEntry) NeckRingIsExist(ctx context.Context, number string) (*model
 	return neckRing, true
 }
 
+func (s *StoreEntry) GetEventLogList(
+	cowId int64, lact int32,
+	eventCategoryId pasturePb.EventCategory_Kind,
+	paginationModel *pasturePb.PaginationModel,
+) ([]*model.EventCowLog, error) {
+	eventLogList := make([]*model.EventCowLog, 0)
+	newEventCowLog := &model.EventCowLog{CowId: cowId}
+	pref := s.DB.Table(newEventCowLog.TableName()).
+		Where("cow_id = ?", cowId)
+	if lact >= 0 {
+		pref.Where("lact = ?", lact)
+	}
+
+	if eventCategoryId > 0 {
+		pref.Where("event_category_id = ?", eventCategoryId)
+	}
+
+	if err := pref.Order("id desc").
+		Offset(int(paginationModel.PageOffset)).
+		Limit(int(paginationModel.PageSize)).
+		Find(&eventLogList).Error; err != nil {
+		return nil, xerr.WithStack(err)
+	}
+	return eventLogList, nil
+}
+
 func GetCowPenInfoByCowId(DB *kptstore.DB, cowId int64) *model.Pen {
 	penData := &model.Pen{}
 	if err := DB.Model(new(model.Cow)).
@@ -393,3 +435,27 @@ func GetCowPenInfoByCowId(DB *kptstore.DB, cowId int64) *model.Pen {
 	}
 	return penData
 }
+
+func GetCowInfoByCowId(DB *kptstore.DB, cowId int64) *model.Cow {
+	cowData := &model.Cow{}
+	if err := DB.Model(new(model.Cow)).
+		Where("id = ?", cowId).
+		First(cowData).Error; err != nil {
+		return nil
+	}
+	return cowData
+}
+
+func GetCowLastEvent(DB *kptstore.DB, cowId int64, eventCategoryId pasturePb.EventCategory_Kind) *model.EventCowLog {
+	newEventCowLog := &model.EventCowLog{CowId: cowId}
+	pref := DB.Table(newEventCowLog.TableName()).
+		Where("cow_id = ?", cowId)
+	if eventCategoryId > 0 {
+		pref.Where("event_category_id = ?", eventCategoryId)
+	}
+
+	if err := pref.Order("id desc").First(newEventCowLog); err != nil {
+		return nil
+	}
+	return newEventCowLog
+}

+ 0 - 2
module/crontab/estrus_warning.go

@@ -161,9 +161,7 @@ func (e *Entry) CowEstrusWarning(ctx context.Context, xToday *XToday) error {
 
 			eventEstrusList = append(eventEstrusList, &model.EventEstrus{
 				CowId:             v.CowId,
-				DayAge:            cowInfo.DayAge,
 				Lact:              cowInfo.Lact,
-				CalvingAge:        int32(cowInfo.CalvingAge),
 				ExposeEstrusType:  pasturePb.ExposeEstrusType_Natural_Estrus,
 				FilterHigh:        v.FilterHigh,
 				EstrusDate:        v.ActiveTime,

+ 12 - 0
util/util.go

@@ -5,6 +5,7 @@ import (
 	"math"
 	"math/rand"
 	"regexp"
+	"strings"
 	"time"
 
 	"gitee.com/xuyiping_admin/pkg/xerr"
@@ -367,3 +368,14 @@ func GetNeckRingActiveTimer(frameId int32) (dateTime string, hours int) {
 	dateTime = nowTime.AddDate(0, 0, day).Format(Layout)
 	return
 }
+
+func ArrayInt32ToStrings(cowIds []int32, cutset string) string {
+	cows := ""
+	if len(cowIds) <= 0 {
+		return cows
+	}
+	for _, v := range cowIds {
+		cows += fmt.Sprintf("%d,", v)
+	}
+	return strings.TrimRight(cows, cutset)
+}