Procházet zdrojové kódy

event: mating 配种事件更新

Yi před 6 měsíci
rodič
revize
3f82e5aeab

+ 2 - 1
README.md

@@ -32,4 +32,5 @@ lint:
 - make generate
 
 todo列表:
-- module/crontab/crontab.go 中119行[Limit(100)] 待优化,case为产后日期类型待测试
+- [x] module/crontab/crontab.go 中119行[Limit(100)] 待优化,case为产后日期类型待测试
+- [ ] 后台添加配种数据时候,不知道该牛只是同期还是自然发情还是人工揭发?

+ 1 - 1
go.mod

@@ -3,7 +3,7 @@ module kpt-pasture
 go 1.17
 
 require (
-	gitee.com/xuyiping_admin/go_proto v0.0.0-20240910085752-595a1baf8c90
+	gitee.com/xuyiping_admin/go_proto v0.0.0-20240911090051-338ee53b7bdd
 	gitee.com/xuyiping_admin/pkg v0.0.0-20231218082641-aac597b8a015
 	github.com/dgrijalva/jwt-go v3.2.0+incompatible
 	github.com/eko/gocache v1.1.0

+ 4 - 0
go.sum

@@ -56,6 +56,10 @@ gitee.com/xuyiping_admin/go_proto v0.0.0-20240910084821-7a933688a3b5 h1:eLdcgCs/
 gitee.com/xuyiping_admin/go_proto v0.0.0-20240910084821-7a933688a3b5/go.mod h1:BKrFW6YLDectlQcQk3FYKBeXvjEiodAKJ5rq7O/QiPE=
 gitee.com/xuyiping_admin/go_proto v0.0.0-20240910085752-595a1baf8c90 h1:MoEtVVhW9LMk1FgiYYubNixK1H8hCNdkLMKdKjwETQE=
 gitee.com/xuyiping_admin/go_proto v0.0.0-20240910085752-595a1baf8c90/go.mod h1:BKrFW6YLDectlQcQk3FYKBeXvjEiodAKJ5rq7O/QiPE=
+gitee.com/xuyiping_admin/go_proto v0.0.0-20240911082204-134766e09b9c h1:qeS8jqn/lrs6GfXoQkykbDooCYaWI5Gud3xsG8HfNV8=
+gitee.com/xuyiping_admin/go_proto v0.0.0-20240911082204-134766e09b9c/go.mod h1:BKrFW6YLDectlQcQk3FYKBeXvjEiodAKJ5rq7O/QiPE=
+gitee.com/xuyiping_admin/go_proto v0.0.0-20240911090051-338ee53b7bdd h1:sMOix7olqu2kp/AhXZMD4HpW3OtKQ2pCii0k3HscWwI=
+gitee.com/xuyiping_admin/go_proto v0.0.0-20240911090051-338ee53b7bdd/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=

+ 2 - 0
model/cow.go

@@ -30,6 +30,7 @@ type Cow struct {
 	MotherId            int64                      `json:"mother_id"`
 	IsRemove            pasturePb.IsShow_Kind      `json:"is_remove"`
 	IsPregnant          pasturePb.IsShow_Kind      `json:"is_pregnant"`
+	IsHealth            pasturePb.IsShow_Kind      `json:"is_health"`
 	WeaningAt           int64                      `json:"weaning_at"`
 	CalvingAt           int64                      `json:"calving_at"`
 	BirthAt             int64                      `json:"birth_at"`
@@ -110,6 +111,7 @@ func NewCow(req *pasturePb.EventEnterData) *Cow {
 		FatherId:            int64(req.FatherId),
 		MotherId:            int64(req.MotherId),
 		IsRemove:            pasturePb.IsShow_Ok,
+		IsHealth:            pasturePb.IsShow_Ok,
 		IsPregnant:          isPregnant,
 		WeaningAt:           int64(req.WeaningAt),
 		BirthAt:             int64(req.BirthAt),

+ 14 - 13
model/event_mating.go

@@ -4,19 +4,20 @@ import pasturePb "gitee.com/xuyiping_admin/go_proto/proto/go/backend/cow"
 
 type EventMating struct {
 	Id                int64                           `json:"id"`
-	CowId             int64                           `json:"cow_id"`
-	DayAge            int64                           `json:"day_age"`
+	CowId             int64                           `json:"cowId"`
+	DayAge            int64                           `json:"dayAge"`
 	Lact              int8                            `json:"lact"`
-	LactationDays     int32                           `json:"lactation_days"`
-	ExposeEstrusType  pasturePb.ExposeEstrusType_Kind `json:"expose_estrus_type"`
-	MatingAt          int64                           `json:"mating_at"`
-	BullId            string                          `json:"bull_id"`
-	FrozenSemenNumber string                          `json:"frozen_semen_number"`
-	StallNumberId     int64                           `json:"stall_number_id"`
-	OperationId       int64                           `json:"operation_id"`
+	LactationDays     int32                           `json:"lactationDays"`
+	MatingNumber      int64                           `json:"matingNumber"`
+	MatingResult      pasturePb.MatingResult_Kind     `json:"matingResult"`
+	ExposeEstrusType  pasturePb.ExposeEstrusType_Kind `json:"exposeEstrusType"`
+	MatingAt          int64                           `json:"matingAt"`
+	FrozenSemenNumber string                          `json:"frozenSemenNumber"`
+	StallNumberId     int64                           `json:"stallNumberId"`
+	OperationId       int64                           `json:"operationId"`
 	Remarks           string                          `json:"remarks"`
-	CreatedAt         int64                           `json:"created_at"`
-	UpdatedAt         int64                           `json:"updated_at"`
+	CreatedAt         int64                           `json:"createdAt"`
+	UpdatedAt         int64                           `json:"updatedAt"`
 }
 
 func (e *EventMating) TableName() string {
@@ -29,9 +30,10 @@ func NewEventMating(cow *Cow, currentUser *SystemUser, req *pasturePb.EventMatin
 		DayAge:            int64(cow.GetDayAge()),
 		Lact:              int8(cow.Lact),
 		LactationDays:     cow.GetLactationDays(),
+		MatingNumber:      1,
+		MatingResult:      pasturePb.MatingResult_Unknown,
 		ExposeEstrusType:  req.ExposeEstrusType,
 		MatingAt:          int64(req.MatingAt),
-		BullId:            req.BullId,
 		FrozenSemenNumber: req.FrozenSemenNumber,
 		StallNumberId:     int64(req.StaffMemberId),
 		OperationId:       currentUser.Id,
@@ -61,7 +63,6 @@ func (e EventMatingSlice) ToPB(user []*SystemUser, exposeEstrusTypeMap map[pastu
 			ExposeEstrusType:     v.ExposeEstrusType,
 			ExposeEstrusTypeName: exposeEstrusTypeMap[v.ExposeEstrusType],
 			MatingAt:             int32(v.MatingAt),
-			BullId:               v.BullId,
 			FrozenSemenNumber:    v.FrozenSemenNumber,
 			LactationDays:        v.LactationDays,
 			StaffMemberId:        int32(v.StallNumberId),

+ 7 - 7
model/event_frozen_semen.go → model/frozen_semen.go

@@ -2,7 +2,7 @@ package model
 
 import pasturePb "gitee.com/xuyiping_admin/go_proto/proto/go/backend/cow"
 
-type EventFrozenSemen struct {
+type FrozenSemen struct {
 	Id              int64                          `json:"id"`
 	ParentId        int64                          `json:"parent_id"`
 	Producer        string                         `json:"producer"`
@@ -18,8 +18,8 @@ type EventFrozenSemen struct {
 	UpdatedAt       int64                          `json:"updated_at"`
 }
 
-func NewEventFrozenSemen(req *pasturePb.SearchFrozenSemenList, currentUser *SystemUser) *EventFrozenSemen {
-	return &EventFrozenSemen{
+func NewFrozenSemen(req *pasturePb.SearchFrozenSemenList, currentUser *SystemUser) *FrozenSemen {
+	return &FrozenSemen{
 		Producer:        req.Producer,
 		BullId:          req.BullId,
 		KindId:          req.CowKind,
@@ -32,13 +32,13 @@ func NewEventFrozenSemen(req *pasturePb.SearchFrozenSemenList, currentUser *Syst
 	}
 }
 
-func (e *EventFrozenSemen) TableName() string {
-	return "event_frozen_semen"
+func (e *FrozenSemen) TableName() string {
+	return "frozen_semen"
 }
 
-type EventFrozenSemenSlice []*EventFrozenSemen
+type FrozenSemenSlice []*FrozenSemen
 
-func (e EventFrozenSemenSlice) ToPB(
+func (e FrozenSemenSlice) ToPB(
 	frozenSemenTypeMap map[pasturePb.FrozenSemenType_Kind]string,
 	unitMap map[pasturePb.Unit_Kind]string) []*pasturePb.SearchFrozenSemenList {
 	res := make([]*pasturePb.SearchFrozenSemenList, len(e))

+ 7 - 5
model/event_frozen_semen_log.go → model/frozen_semen_log.go

@@ -1,22 +1,24 @@
 package model
 
-type EventFrozenSemenLog struct {
+type FrozenSemenLog struct {
 	Id            int64  `json:"id"`
 	BullId        string `json:"bull_id"`
 	CowId         int64  `json:"cow_id"`
+	Quantity      int32  `json:"quantity"`
 	StallNumberId int64  `json:"stall_number_id"`
 	CreatedAt     int64  `json:"created_at"`
 	UpdatedAt     int64  `json:"updated_at"`
 }
 
-func (e *EventFrozenSemenLog) TableName() string {
-	return "event_frozen_semen_log"
+func (e *FrozenSemenLog) TableName() string {
+	return "frozen_semen_log"
 }
 
-func NewEventFrozenSemenLog(bullId string, cow *Cow, stallNumberId int64) *EventFrozenSemenLog {
-	return &EventFrozenSemenLog{
+func NewEventFrozenSemenLog(bullId string, cow *Cow, stallNumberId int64) *FrozenSemenLog {
+	return &FrozenSemenLog{
 		BullId:        bullId,
 		CowId:         cow.Id,
 		StallNumberId: stallNumberId,
+		Quantity:      1,
 	}
 }

+ 4 - 0
model/same_time_cow.go

@@ -19,6 +19,10 @@ type SameTimeCow struct {
 	UpdatedAt        int64                         `json:"updatedAt"`
 }
 
+func (s *SameTimeCow) TableName() string {
+	return "same_time_cow"
+}
+
 func NewSameTimeCow(cowId, sameTimeId int64) *SameTimeCow {
 	return &SameTimeCow{
 		CowId:      cowId,

+ 2 - 14
module/backend/config_data.go

@@ -573,7 +573,7 @@ func (s *StoreEntry) FrozenSemenTypeEnumList() []*pasturePb.ConfigOptionsList {
 }
 
 func (s *StoreEntry) BullNumberEnumList() []*pasturePb.BullOptionsList {
-	frozenSemenList := make([]*model.EventFrozenSemen, 0)
+	frozenSemenList := make([]*model.FrozenSemen, 0)
 	bullNumberList := make([]*pasturePb.BullOptionsList, 0)
 	if err := s.DB.Where("quantity > 0").Group("bull_id").Find(&frozenSemenList).Error; err != nil {
 		zaplog.Error("BullNumberEnumList", zap.Any("Find", err))
@@ -731,21 +731,9 @@ func CalendarTypeEnumList() []*pasturePb.ConfigOptionsList {
 		Label:    "RnGH",
 		Disabled: true,
 	}, &pasturePb.ConfigOptionsList{
-		Value:    int32(pasturePb.CalendarType_First_Pregnancy_Check),
+		Value:    int32(pasturePb.CalendarType_Pregnancy_Check),
 		Label:    "孕检-初检",
 		Disabled: true,
-	}, &pasturePb.ConfigOptionsList{
-		Value:    int32(pasturePb.CalendarType_Second_Pregnancy_Check),
-		Label:    "孕检-二检",
-		Disabled: true,
-	}, &pasturePb.ConfigOptionsList{
-		Value:    int32(pasturePb.CalendarType_Third_Pregnancy_Check),
-		Label:    "孕检-三检",
-		Disabled: true,
-	}, &pasturePb.ConfigOptionsList{
-		Value:    int32(pasturePb.CalendarType_Fourth_Pregnancy_Check),
-		Label:    "孕检-四检",
-		Disabled: true,
 	}, &pasturePb.ConfigOptionsList{
 		Value:    int32(pasturePb.CalendarType_WorkOrder),
 		Label:    "工单",

+ 73 - 19
module/backend/event_breed.go

@@ -205,27 +205,69 @@ func (s *StoreEntry) MatingCreate(ctx context.Context, req *pasturePb.EventMatin
 	if err != nil {
 		return xerr.WithStack(err)
 	}
-	matingList := make([]*model.EventMating, 0)
-	eventFrozenSemenLogList := make([]*model.EventFrozenSemenLog, 0)
-	currentUser, _ := s.GetCurrentSystemUser(ctx)
 
-	frozenSemen := &model.EventFrozenSemen{}
+	eventFrozenSemenLogList := make([]*model.FrozenSemenLog, 0)
+	currentUser, _ := s.GetCurrentSystemUser(ctx)
+	frozenSemen := &model.FrozenSemen{}
 	if err = s.DB.Where("bull_id = ?", req.BullId).First(frozenSemen).Error; err != nil {
 		return xerr.WithStack(err)
 	}
 
-	if err = s.DB.Transaction(func(tx *gorm.DB) error {
-		if err = tx.Create(matingList).Error; err != nil {
+	sameTimeCowIds := make([]int64, 0)
+	matingInsertList := make([]*model.EventMating, 0)
+	matingUpdateIds := make([]int64, 0)
+	nowTime := time.Now()
+	for _, cow := range cowList {
+		var count int64 = 0
+		itemEventMating := &model.EventMating{}
+		if err = s.DB.Where("lact = ?", cow.Lact).
+			Where("mating_at <= ").
+			Where("cow_id = ?", cow.Id).First(itemEventMating).Count(&count).Error; err != nil {
 			return xerr.WithStack(err)
 		}
 
-		for _, cow := range cowList {
-			matingList = append(matingList, model.NewEventMating(cow, currentUser, req))
-			eventFrozenSemenLogList = append(eventFrozenSemenLogList, model.NewEventFrozenSemenLog(req.BullId, cow, int64(req.StaffMemberId)))
-			if err = tx.Model(&model.SameTimeCow{}).Where("lact = ?", cow.Lact).Where("cow_id = ?", cow.Id).Updates(map[string]interface{}{
-				"status": pasturePb.IsShow_No,
-				"end_at": time.Now().Unix(),
-			}).Error; err != nil {
+		// 判断当前输精时间距离上次输精时间是否超过2天,如果超过则更新为复配状态
+		itemBeforeTwoDays := nowTime.Sub(time.Unix(itemEventMating.MatingAt, 0)).Hours()
+		if count > 0 && itemBeforeTwoDays > 48 {
+			matingUpdateIds = append(matingUpdateIds, itemEventMating.Id)
+		} else {
+			matingInsertList = append(matingInsertList, model.NewEventMating(cow, currentUser, req))
+		}
+
+		eventFrozenSemenLogList = append(
+			eventFrozenSemenLogList,
+			model.NewEventFrozenSemenLog(req.BullId, cow, int64(req.StaffMemberId)),
+		)
+		sameTimeCow := &model.SameTimeCow{}
+		if err = s.DB.Where("lact = ?", cow.Lact).
+			Where("cow_id = ?", cow.Id).
+			Where("status = ?", pasturePb.SameTimeStatus_In_Progress).
+			First(sameTimeCow).Error; err != nil {
+			if !errors.Is(err, gorm.ErrRecordNotFound) {
+				return xerr.WithStack(err)
+			}
+		}
+		if sameTimeCow.Id > 0 {
+			sameTimeCowIds = append(sameTimeCowIds, sameTimeCow.Id)
+		}
+	}
+
+	if err = s.DB.Transaction(func(tx *gorm.DB) error {
+
+		// 创建配种事件数据
+		if len(matingInsertList) > 0 {
+			if err = tx.Create(matingInsertList).Error; err != nil {
+				return xerr.WithStack(err)
+			}
+		}
+
+		// 更新已配种的牛只为复配状态
+		if len(matingUpdateIds) > 0 {
+			if err = tx.Model(new(model.EventMating)).
+				Where("id IN ?", matingUpdateIds).
+				Where("mating_result = ?", pasturePb.MatingResult_Unknown).
+				Update("mating_result", pasturePb.MatingResult_ReMatch).
+				Error; err != nil {
 				return xerr.WithStack(err)
 			}
 		}
@@ -235,9 +277,21 @@ func (s *StoreEntry) MatingCreate(ctx context.Context, req *pasturePb.EventMatin
 			return xerr.WithStack(err)
 		}
 
-		if err = tx.Table(new(model.EventFrozenSemen).TableName()).
+		// 如果有同期牛只,则修改为已结束状态
+		if len(sameTimeCowIds) > 0 {
+			if err = tx.Table(new(model.SameTimeCow).TableName()).
+				Where("id IN ?", sameTimeCowIds).
+				UpdateColumn("status", pasturePb.SameTimeStatus_End).Error; err != nil {
+			}
+		}
+
+		// 减去精液的数量
+		if err = tx.Table(new(model.FrozenSemen).TableName()).
 			Where("bull_id = ?", req.BullId).
-			Where("quantity > 0").UpdateColumn("quantity", gorm.Expr("quantity - ?", len(matingList))).Error; err != nil {
+			Where("quantity > 0").
+			UpdateColumn("quantity",
+				gorm.Expr("quantity - ?", len(matingUpdateIds)+len(matingInsertList)),
+			).Error; err != nil {
 			return xerr.WithStack(err)
 		}
 
@@ -300,9 +354,9 @@ func (s *StoreEntry) EstrusCreate(ctx context.Context, req *pasturePb.EventEstru
 }
 
 func (s *StoreEntry) FrozenSemenList(ctx context.Context, req *pasturePb.FrozenSemenRequest, pagination *pasturePb.PaginationModel) (*pasturePb.FrozenSemenResponse, error) {
-	frozenSemenList := make([]*model.EventFrozenSemen, 0)
+	frozenSemenList := make([]*model.FrozenSemen, 0)
 	var count int64 = 0
-	pref := s.DB.Table(new(model.EventFrozenSemen).TableName())
+	pref := s.DB.Table(new(model.FrozenSemen).TableName())
 	if req.BullId != "" {
 		pref.Where("bull_id = ?", req.BullId)
 	}
@@ -325,7 +379,7 @@ func (s *StoreEntry) FrozenSemenList(ctx context.Context, req *pasturePb.FrozenS
 		Code:    http.StatusOK,
 		Message: "ok",
 		Data: &pasturePb.SearchFrozenSemenData{
-			List:     model.EventFrozenSemenSlice(frozenSemenList).ToPB(frozenSemenTypeMap, unitMap),
+			List:     model.FrozenSemenSlice(frozenSemenList).ToPB(frozenSemenTypeMap, unitMap),
 			Total:    int32(count),
 			PageSize: pagination.PageSize,
 			Page:     pagination.Page,
@@ -336,7 +390,7 @@ func (s *StoreEntry) FrozenSemenList(ctx context.Context, req *pasturePb.FrozenS
 func (s *StoreEntry) FrozenSemenCreate(ctx context.Context, req *pasturePb.SearchFrozenSemenList) error {
 	currentUser, _ := s.GetCurrentSystemUser(ctx)
 	req.CowKindName = s.CowKindMap()[req.CowKind]
-	newFrozenSemen := model.NewEventFrozenSemen(req, currentUser)
+	newFrozenSemen := model.NewFrozenSemen(req, currentUser)
 	if err := s.DB.Create(newFrozenSemen).Error; err != nil {
 		return xerr.WithStack(err)
 	}