浏览代码

event: 更新时间发生牛只实际日龄

Yi 3 周之前
父节点
当前提交
36ee0ff84e

+ 8 - 0
model/cow.go

@@ -251,6 +251,14 @@ func (c *Cow) GetDayAge() int32 {
 	return int32(math.Floor(float64(time.Now().Local().Unix()-c.BirthAt) / 86400))
 }
 
+// GetEventDayAge 获取事件发生的日龄
+func (c *Cow) GetEventDayAge(eventAt int64) int32 {
+	if c.BirthAt <= 0 {
+		return 0
+	}
+	return int32(math.Floor(float64(eventAt-c.BirthAt) / 86400))
+}
+
 // GetCalvingAge 产后天数
 func (c *Cow) GetCalvingAge() int32 {
 	if c.LastCalvingAt <= 0 {

+ 1 - 1
model/event_abortion.go

@@ -41,7 +41,7 @@ func NewEventAbortion(pastureId int64, cow *Cow, item *pasturePb.EventAbortionIt
 		Lact:                cow.Lact,
 		CowType:             cow.CowType,
 		PregnantAge:         cow.GetDaysPregnant(),
-		DayAge:              cow.DayAge,
+		DayAge:              cow.GetEventDayAge(int64(item.AbortionAt)),
 		AbortionAt:          int64(item.AbortionAt),
 		IsAfterbirth:        item.IsAfterbirth,
 		IsLact:              item.IsLact,

+ 1 - 1
model/event_calving.go

@@ -45,7 +45,7 @@ func (e *EventCalving) EventUpdate(operationUser, currentUser *SystemUser, req *
 		e.OperationName = operationUser.Name
 	}
 	e.RealityDay = int64(req.CalvingAt)
-	e.DayAge = cow.GetDayAge()
+	e.DayAge = cow.GetEventDayAge(int64(req.CalvingAt))
 	e.PregnancyAge = cow.GetDaysPregnant()
 	e.CalvingLevel = req.CalvingLevel
 	e.BullNumber = cow.LastBullNumber

+ 1 - 1
model/event_death.go

@@ -37,7 +37,7 @@ func NewEventDeath(pastureId int64, cow *Cow, req *pasturePb.EventDeath, current
 		CowId:                cow.Id,
 		EarNumber:            cow.EarNumber,
 		Lact:                 cow.Lact,
-		DayAge:               cow.GetDayAge(),
+		DayAge:               cow.GetEventDayAge(int64(req.DeathAt)),
 		DeathAt:              int64(req.DeathAt),
 		DeathReasonKind:      req.DeathReasonKind,
 		DeathReasonName:      req.DeathReasonName,

+ 2 - 0
model/event_dry_milk.go

@@ -11,6 +11,7 @@ type EventDryMilk struct {
 	PastureId     int64                 `json:"pastureId"`
 	CowId         int64                 `json:"cowId"`
 	EarNumber     string                `json:"earNumber"`
+	DayAge        int32                 `json:"dayAge"`
 	Lact          int32                 `json:"lact"`
 	PenId         int32                 `json:"penId"`
 	PenName       string                `json:"penName"`
@@ -42,6 +43,7 @@ func (e *EventDryMilk) EventDryMilkUpdate(cow *Cow, dryMilkAt int64, pen *Pen, o
 	e.MessageName = message.Name
 	e.PenId = pen.Id
 	e.PenName = pen.Name
+	e.DayAge = cow.GetEventDayAge(dryMilkAt)
 }
 
 func NewEventDryMilk(pastureId int64, cow *Cow, startDay, endDay string) *EventDryMilk {

+ 2 - 0
model/event_forbidden_mating.go

@@ -9,6 +9,7 @@ type EventForbiddenMating struct {
 	PastureId                  int64                                 `json:"pastureId"`
 	CowId                      int64                                 `json:"cowId"`
 	EarNumber                  string                                `json:"earNumber"`
+	DayAge                     int32                                 `json:"dayAge"`
 	ForbiddenMatingAt          int64                                 `json:"forbiddenMatingAt"`
 	ForbiddenMatingReasonsKind pasturePb.ForbiddenMatingReasons_Kind `json:"forbiddenMatingReasonsKind"`
 	ForbiddenMatingReasonsName string                                `json:"forbiddenMatingReasonsName"`
@@ -42,6 +43,7 @@ func NewEventForbiddenMating(pastureId int64, cow *Cow, forbiddenMatingAt int64,
 		PastureId:                  pastureId,
 		CowId:                      cow.Id,
 		EarNumber:                  cow.EarNumber,
+		DayAge:                     cow.GetEventDayAge(forbiddenMatingAt),
 		ForbiddenMatingAt:          forbiddenMatingAt,
 		ForbiddenMatingReasonsKind: forbiddenMatingReasonsKind,
 		ForbiddenMatingReasonsName: forbiddenMatingReasonsName,

+ 1 - 1
model/event_immunization_plan.go

@@ -50,7 +50,7 @@ func (e *EventImmunizationPlan) EventUpdate(immunizationAt int64, cowInfo *Cow,
 	e.RealityDay = immunizationAt
 	e.Remarks = remark
 	e.Lact = cowInfo.Lact
-	e.DayAge = cowInfo.DayAge
+	e.DayAge = cowInfo.GetEventDayAge(immunizationAt)
 	e.EarNumber = cowInfo.EarNumber
 	e.Status = pasturePb.IsShow_Ok
 	e.DrugsId = drugs.Id

+ 2 - 1
model/event_mating.go

@@ -42,7 +42,7 @@ func (e *EventMating) TableName() string {
 	return "event_mating"
 }
 
-func (e *EventMating) EventUpdate(matingAt int64, bullNumber string, isReMating bool, operationUser, currentUser *SystemUser) {
+func (e *EventMating) EventUpdate(cowInfo *Cow, matingAt int64, bullNumber string, isReMating bool, operationUser, currentUser *SystemUser) {
 	e.MatingResult = pasturePb.MatingResult_Unknown
 	e.Status = pasturePb.IsShow_Ok
 	e.RealityDay = matingAt
@@ -54,6 +54,7 @@ func (e *EventMating) EventUpdate(matingAt int64, bullNumber string, isReMating
 	if !isReMating {
 		e.MatingTimes += 1
 	}
+	e.DayAge = cowInfo.GetEventDayAge(matingAt)
 }
 
 // EventReMatingUpdate 复配更新

+ 3 - 1
model/event_pregnant_check.go

@@ -45,6 +45,7 @@ func (e *EventPregnantCheck) EventUpdate(
 	pregnantCheckMethod pasturePb.PregnantCheckMethod_Kind,
 	operationUser, currentUser *SystemUser,
 	remarks string,
+	cowInfo *Cow,
 ) {
 	e.RealityDay = pregnantCheckAt
 	e.PregnantCheckResult = pregnantCheckResult
@@ -55,6 +56,7 @@ func (e *EventPregnantCheck) EventUpdate(
 	e.MessageName = currentUser.Name
 	e.Remarks = remarks
 	e.Status = pasturePb.IsShow_Ok
+	e.DayAge = cowInfo.GetEventDayAge(pregnantCheckAt)
 }
 
 func NewEventPregnantCheck(pastureId int64, cow *Cow, penMap map[int32]*Pen, pregnantCheckName, startDay, endDay string) *EventPregnantCheck {
@@ -69,7 +71,7 @@ func NewEventPregnantCheck(pastureId int64, cow *Cow, penMap map[int32]*Pen, pre
 		CowType:           cow.CowType,
 		PenId:             cow.PenId,
 		PenName:           penName,
-		DayAge:            cow.GetDayAge(),
+		DayAge:            0,
 		Lact:              int8(cow.Lact),
 		PlanDay:           util.TimeParseLocalUnix(startDay),
 		EndDay:            util.TimeParseLocalEndUnix(endDay),

+ 5 - 3
model/event_sale_cow.go

@@ -8,6 +8,7 @@ type EventSaleCow struct {
 	SaleId       int64                      `json:"saleId"`
 	CowId        int64                      `json:"cowId"`
 	EarNumber    string                     `json:"earNumber"`
+	DayAge       int32                      `json:"dayAge"`
 	Lact         int32                      `json:"lact"`
 	PregnancyAge int32                      `json:"pregnancyAge"`
 	BreedStatus  pasturePb.BreedStatus_Kind `json:"breedStatus"`
@@ -22,7 +23,7 @@ func (e *EventSaleCow) TableName() string {
 	return "event_sale_cow"
 }
 
-func NewEventSaleCow(pastureId int64, saleId int64, cowInfo *Cow) *EventSaleCow {
+func NewEventSaleCow(pastureId int64, saleId, saleAt int64, cowInfo *Cow) *EventSaleCow {
 	return &EventSaleCow{
 		PastureId:    pastureId,
 		SaleId:       saleId,
@@ -33,13 +34,14 @@ func NewEventSaleCow(pastureId int64, saleId int64, cowInfo *Cow) *EventSaleCow
 		BreedStatus:  cowInfo.BreedStatus,
 		LactationAge: cowInfo.LactationAge,
 		AdmissionAge: cowInfo.AdmissionAge,
+		DayAge:       cowInfo.GetEventDayAge(saleAt),
 	}
 }
 
-func NewEventSaleCowList(pastureId int64, saleId int64, cowList []*Cow) []*EventSaleCow {
+func NewEventSaleCowList(pastureId int64, eventSale *EventSale, cowList []*Cow) []*EventSaleCow {
 	res := make([]*EventSaleCow, 0)
 	for _, cow := range cowList {
-		res = append(res, NewEventSaleCow(pastureId, saleId, cow))
+		res = append(res, NewEventSaleCow(pastureId, eventSale.Id, eventSale.SaleAt, cow))
 	}
 	return res
 }

+ 7 - 2
model/event_transfer_group.go

@@ -1,6 +1,10 @@
 package model
 
-import pasturePb "gitee.com/xuyiping_admin/go_proto/proto/go/backend/cow"
+import (
+	"kpt-pasture/util"
+
+	pasturePb "gitee.com/xuyiping_admin/go_proto/proto/go/backend/cow"
+)
 
 type EventTransferGroup struct {
 	Id                 int64  `json:"id"`
@@ -26,13 +30,14 @@ func (e *EventTransferGroup) TableName() string {
 	return "event_transfer_group"
 }
 func NewEventTransferGroup(pastureId int64, cow *Cow, req *pasturePb.TransferGroupEventData, transferPenMap map[int32]string, currentUser *SystemUser, operationUser *SystemUser) *EventTransferGroup {
+	eventAt := util.DateTimeParseLocalUnix(req.TransferDate)
 	return &EventTransferGroup{
 		PastureId:          pastureId,
 		CowId:              cow.Id,
 		PenInId:            req.TransferInPenId,
 		PenOutId:           cow.PenId,
 		Lact:               cow.Lact,
-		DayAge:             cow.GetDayAge(),
+		DayAge:             cow.GetEventDayAge(eventAt),
 		TransferDate:       req.TransferDate,
 		TransferReasonId:   int64(req.TransferReasonId),
 		TransferReasonName: transferPenMap[req.TransferReasonId],

+ 3 - 1
model/event_weaning.go

@@ -12,6 +12,7 @@ type EventWeaning struct {
 	PastureId     int64                 `json:"pastureId"`
 	CowId         int64                 `json:"cowId"`
 	EarNumber     string                `json:"earNumber"`
+	DayAge        int32                 `json:"dayAge"`
 	MotherId      int64                 `json:"motherId"`
 	BullNumber    string                `json:"bullNumber"`
 	PlanDay       int64                 `json:"planDay"`
@@ -34,7 +35,7 @@ func (e *EventWeaning) TableName() string {
 	return "event_weaning"
 }
 
-func (e *EventWeaning) EventUpdate(weaningAt int64, weight int32, remarks string, afterPenId int32, operationUser, currentUser *SystemUser) {
+func (e *EventWeaning) EventUpdate(cowInfo *Cow, weaningAt int64, weight int32, remarks string, afterPenId int32, operationUser, currentUser *SystemUser) {
 	e.Status = pasturePb.IsShow_Ok
 	e.RealityDay = weaningAt
 	e.OperationId = int32(operationUser.Id)
@@ -44,6 +45,7 @@ func (e *EventWeaning) EventUpdate(weaningAt int64, weight int32, remarks string
 	e.Remarks = remarks
 	e.AfterPenId = afterPenId
 	e.Weight = weight
+	e.DayAge = cowInfo.GetEventDayAge(weaningAt)
 }
 
 func NewEventWeaning(pastureId int64, cowInfo *Cow, startDay, endDay string) *EventWeaning {

+ 1 - 1
model/event_weight.go

@@ -40,7 +40,7 @@ func NewEventWeight(pastureId int64, cow *Cow, currentUser *SystemUser, item *pa
 		Weight:        int32(item.Weight * 1000),
 		Height:        item.Height,
 		Lact:          cow.Lact,
-		DayAge:        cow.GetDayAge(),
+		DayAge:        cow.GetEventDayAge(int64(item.WeightAt)),
 		WeightAt:      int64(item.WeightAt),
 		Remarks:       item.Remarks,
 		MessageId:     currentUser.Id,

+ 6 - 0
module/backend/analysis_more.go

@@ -366,6 +366,9 @@ func getIQR3(dataList []*pasturePb.CowBehaviorData, getField func(*pasturePb.Cow
 		return 0
 	}
 	index := (n + 1) * 3 / 4
+	if index >= n {
+		return 0
+	}
 
 	return int32(values[index])
 }
@@ -381,5 +384,8 @@ func getIQR1(dataList []*pasturePb.CowBehaviorData, getField func(*pasturePb.Cow
 		return 0
 	}
 	index := (n + 1) / 4
+	if index >= n {
+		return 0
+	}
 	return int32(values[index])
 }

+ 1 - 1
module/backend/event_base_more.go

@@ -235,7 +235,7 @@ func (s *StoreEntry) CowSaleCreate(ctx context.Context, req *pasturePb.EventCowS
 			CowList:          cowList,
 			SalesType:        req.SalesType,
 			EventSaleCarList: model.NewEventSaleCarList(userModel.AppPasture.Id, newEventSale.Id, newEventSale.SaleAt, req.SaleVehicleItems),
-			EventSaleCowList: model.NewEventSaleCowList(userModel.AppPasture.Id, newEventSale.Id, cowList),
+			EventSaleCowList: model.NewEventSaleCowList(userModel.AppPasture.Id, newEventSale, cowList),
 			NeckRingList:     neckRingList,
 			EventCowLog:      eventCowLogList,
 			SaleAt:           int64(req.SaleAt),

+ 18 - 7
module/backend/event_breed_more.go

@@ -114,8 +114,15 @@ func (s *StoreEntry) PregnantCheckCreateBatch(ctx context.Context, req *pastureP
 			}
 
 			// 更新孕检事件表
-			item.EventPregnancyCheck.EventUpdate(int64(item.PregnantCheckAt), item.PregnantCheckResult, item.PregnantCheckMethod,
-				item.OperationUser, userModel.SystemUser, item.Remarks)
+			item.EventPregnancyCheck.EventUpdate(
+				int64(item.PregnantCheckAt),
+				item.PregnantCheckResult,
+				item.PregnantCheckMethod,
+				item.OperationUser,
+				userModel.SystemUser,
+				item.Remarks,
+				item.Cow,
+			)
 			if err = tx.Model(new(model.EventPregnantCheck)).
 				Select("reality_day", "pregnant_check_result", "pregnant_check_method", "operation_id", "operation_name", "message_id", "message_name", "remarks", "status").
 				Where("id = ?", item.EventPregnancyCheck.Id).
@@ -388,11 +395,12 @@ func (s *StoreEntry) NeckRingEstrusToMating(ctx context.Context, userModel *mode
 	}
 	// 新增配种事件数据
 	newMating := model.NewEventMating(userModel.AppPasture.Id, item.Cow, item.MatingAt, pasturePb.ExposeEstrusType_Neck_Ring)
-	newMating.EventUpdate(item.MatingAt, item.FrozenSemen.BullId, false, item.OperationUser, userModel.SystemUser)
+	newMating.EventUpdate(item.Cow, item.MatingAt, item.FrozenSemen.BullId, false, item.OperationUser, userModel.SystemUser)
 	if err := s.MatingCowUpdate(ctx, userModel.AppPasture.Id, item, false); err != nil {
 		return xerr.WithStack(err)
 	}
-	if err := tx.Model(newMating).Create(newMating).Error; err != nil {
+	if err := tx.Model(new(model.EventMating)).
+		Create(newMating).Error; err != nil {
 		return xerr.WithStack(err)
 	}
 	return nil
@@ -438,7 +446,7 @@ func (s *StoreEntry) NaturalEstrusToMating(ctx context.Context, userModel *model
 	// 2.2.  同期初配
 	IsMatingUpdate := lastEventMating.IsMatingUpdate()
 	if IsMatingUpdate {
-		lastEventMating.EventUpdate(item.MatingAt, item.FrozenSemen.BullId, isReMating, item.OperationUser, userModel.SystemUser)
+		lastEventMating.EventUpdate(item.Cow, item.MatingAt, item.FrozenSemen.BullId, isReMating, item.OperationUser, userModel.SystemUser)
 		if err = tx.Model(lastEventMating).
 			Select("mating_at", "status", "reality_day", "frozen_semen_number", "operation_id", "operation_name", "message_id", "message_name").
 			Where("id = ?", lastEventMating.Id).
@@ -460,7 +468,7 @@ func (s *StoreEntry) NaturalEstrusToMating(ctx context.Context, userModel *model
 		}
 		// 先创建一条新的配种数据
 		newMating := model.NewEventMating(userModel.AppPasture.Id, item.Cow, item.MatingAt, pasturePb.ExposeEstrusType_Natural_Estrus)
-		newMating.EventUpdate(item.MatingAt, item.FrozenSemen.BullId, isReMating, item.OperationUser, userModel.SystemUser)
+		newMating.EventUpdate(item.Cow, item.MatingAt, item.FrozenSemen.BullId, isReMating, item.OperationUser, userModel.SystemUser)
 		if err = tx.Model(newMating).Create(newMating).Error; err != nil {
 			return xerr.WithStack(err)
 		}
@@ -528,7 +536,10 @@ func (s *StoreEntry) WeaningBatch(ctx context.Context, req *pasturePb.EventWeani
 				return xerr.WithStack(err)
 			}
 
-			eventWeaning.EventUpdate(int64(item.WeaningAt), int32(item.Weight*1000), item.Remarks, item.PenId, operation, userModel.SystemUser)
+			eventWeaning.EventUpdate(
+				cowInfo, int64(item.WeaningAt), int32(item.Weight*1000),
+				item.Remarks, item.PenId, operation, userModel.SystemUser,
+			)
 			if err = tx.Model(new(model.EventWeaning)).
 				Select("status", "reality_day", "operation_id", "operation_name", "message_id", "message_name", "remarks", "after_pen_id").
 				Where("id = ?", eventWeaning.Id).

+ 7 - 0
util/util.go

@@ -119,6 +119,13 @@ func ConvertParseLocalUnix(timeParse string) (int64, error) {
 	return theTime.Unix(), nil
 }
 
+// DateTimeParseLocalUnix
+// eg 2025-10-13 15:04:05 => 1676998245
+func DateTimeParseLocalUnix(DayTime string) int64 {
+	theTime, _ := TimeParseLocal(LayoutTime, DayTime)
+	return theTime.Unix()
+}
+
 // GetMonthRemainDay 获取当前月还剩几天
 func GetMonthRemainDay() int {
 	now := time.Now().Local()