소스 검색

event: 配种和孕检优化

Yi 7 달 전
부모
커밋
14a31121b4
7개의 변경된 파일130개의 추가작업 그리고 36개의 파일을 삭제
  1. 1 1
      go.mod
  2. 6 0
      go.sum
  3. 1 0
      model/cow.go
  4. 7 2
      model/event_pregnant_check.go
  5. 18 26
      module/backend/config_data.go
  6. 3 2
      module/backend/event_base.go
  7. 94 5
      module/backend/event_breed.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-20240911090051-338ee53b7bdd
+	gitee.com/xuyiping_admin/go_proto v0.0.0-20240912024219-df4ae8e00ef3
 	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

+ 6 - 0
go.sum

@@ -60,6 +60,12 @@ gitee.com/xuyiping_admin/go_proto v0.0.0-20240911082204-134766e09b9c h1:qeS8jqn/
 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/go_proto v0.0.0-20240912012226-2a41e99f794f h1:F2JObMAfVnq+8RJAtEhID6UaeF3qpO4mgWZsyQA+6PA=
+gitee.com/xuyiping_admin/go_proto v0.0.0-20240912012226-2a41e99f794f/go.mod h1:BKrFW6YLDectlQcQk3FYKBeXvjEiodAKJ5rq7O/QiPE=
+gitee.com/xuyiping_admin/go_proto v0.0.0-20240912013108-b140d281ec27 h1:wzQovv4fvjiVP7xTY1DrEEw3De8LtBDe/AOWrkYt+e4=
+gitee.com/xuyiping_admin/go_proto v0.0.0-20240912013108-b140d281ec27/go.mod h1:BKrFW6YLDectlQcQk3FYKBeXvjEiodAKJ5rq7O/QiPE=
+gitee.com/xuyiping_admin/go_proto v0.0.0-20240912024219-df4ae8e00ef3 h1:wYX+YcTbo+81TrtewBCWExxctetMvjN1sabiq3oPI6k=
+gitee.com/xuyiping_admin/go_proto v0.0.0-20240912024219-df4ae8e00ef3/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=

+ 1 - 0
model/cow.go

@@ -143,6 +143,7 @@ type BarCowStruct struct {
 	TypeId pasturePb.CowType_Kind `json:"type_id"`
 }
 
+// BarCowStructSlice 首页牛群结构
 type BarCowStructSlice []*BarCowStruct
 
 func (b BarCowStructSlice) ToPB(cowTypeMap map[pasturePb.CowType_Kind]string, count int32) []*pasturePb.BarCowStruct {

+ 7 - 2
model/event_pregnant_check.go

@@ -21,13 +21,18 @@ func (e *EventPregnantCheck) TableName() string {
 	return "event_pregnant_check"
 }
 
-func NewEventPregnantCheck(cow *Cow, systemUser *SystemUser, req *pasturePb.EventPregnantCheck) *EventPregnantCheck {
+func NewEventPregnantCheck(
+	cow *Cow,
+	systemUser *SystemUser,
+	req *pasturePb.EventPregnantCheck,
+	pregnantCheckResult pasturePb.PregnantCheckResult_Kind,
+) *EventPregnantCheck {
 	return &EventPregnantCheck{
 		CowId:               cow.Id,
 		DayAge:              cow.GetDayAge(),
 		Lact:                int8(cow.Lact),
 		PregnantCheckAt:     int64(req.PregnantCheckAt),
-		PregnantCheckResult: req.PregnantCheckResult,
+		PregnantCheckResult: pregnantCheckResult,
 		PregnantCheckMethod: req.PregnantCheckMethod,
 		StaffMemberId:       int64(req.StaffMemberId),
 		OperationId:         systemUser.Id,

+ 18 - 26
module/backend/config_data.go

@@ -79,7 +79,7 @@ func (s *StoreEntry) BreedStatusEnumList() []*pasturePb.ConfigOptionsList {
 		Label:    "流产",
 		Disabled: true,
 	}, &pasturePb.ConfigOptionsList{
-		Value:    int32(pasturePb.BreedStatus_No_Pregnant),
+		Value:    int32(pasturePb.BreedStatus_No_Mating),
 		Label:    "禁配",
 		Disabled: true,
 	})
@@ -89,28 +89,28 @@ func (s *StoreEntry) BreedStatusEnumList() []*pasturePb.ConfigOptionsList {
 func (s *StoreEntry) CowKindEnumList() []*pasturePb.ConfigOptionsList {
 	cowKindList := make([]*pasturePb.ConfigOptionsList, 0)
 	cowKindList = append(cowKindList, &pasturePb.ConfigOptionsList{
-		Value:    int32(pasturePb.CowKind_HST),
-		Label:    "荷斯坦",
+		Value:    int32(pasturePb.CowKind_XMTEN),
+		Label:    "西门塔尔牛",
 		Disabled: true,
 	}, &pasturePb.ConfigOptionsList{
-		Value:    int32(pasturePb.CowKind_JSN),
-		Label:    "娟姗牛",
+		Value:    int32(pasturePb.CowKind_AGSN),
+		Label:    "安格斯牛",
 		Disabled: true,
 	}, &pasturePb.ConfigOptionsList{
-		Value:    int32(pasturePb.CowKind_SHN),
-		Label:    "三河牛",
+		Value:    int32(pasturePb.CowKind_XNLN),
+		Label:    "夏洛莱牛",
 		Disabled: true,
 	}, &pasturePb.ConfigOptionsList{
-		Value:    int32(pasturePb.CowKind_XJHN),
-		Label:    "新疆褐牛",
+		Value:    int32(pasturePb.CowKind_LMZN),
+		Label:    "利木赞牛",
 		Disabled: true,
 	}, &pasturePb.ConfigOptionsList{
-		Value:    int32(pasturePb.CowKind_MN),
-		Label:    "牛",
+		Value:    int32(pasturePb.CowKind_HFTN),
+		Label:    "海福特牛",
 		Disabled: true,
 	}, &pasturePb.ConfigOptionsList{
-		Value:    int32(pasturePb.CowKind_XMTEN),
-		Label:    "西门塔尔牛",
+		Value:    int32(pasturePb.CowKind_HN),
+		Label:    "牛",
 		Disabled: true,
 	})
 	return cowKindList
@@ -141,7 +141,7 @@ func (s *StoreEntry) CowTypeEnumList() []*pasturePb.ConfigOptionsList {
 		Disabled: true,
 	}, &pasturePb.ConfigOptionsList{
 		Value:    int32(pasturePb.CowType_Weaned_Calf),
-		Label:    "断奶犊牛",
+		Label:    "育成牛",
 		Disabled: true,
 	}, &pasturePb.ConfigOptionsList{
 		Value:    int32(pasturePb.CowType_Youth_Calf),
@@ -361,20 +361,12 @@ func (s *StoreEntry) DystociaReasonEnumList() []*pasturePb.ConfigOptionsList {
 func (s *StoreEntry) PregnantCheckResultEnumList() []*pasturePb.ConfigOptionsList {
 	configOptions := make([]*pasturePb.ConfigOptionsList, 0)
 	configOptions = append(configOptions, &pasturePb.ConfigOptionsList{
-		Value:    int32(pasturePb.PregnantCheckResult_InCheck_UnPregnant),
-		Label:    "初检未孕",
-		Disabled: true,
-	}, &pasturePb.ConfigOptionsList{
-		Value:    int32(pasturePb.PregnantCheckResult_InCheck_Pregnant),
-		Label:    "初检已孕",
-		Disabled: true,
-	}, &pasturePb.ConfigOptionsList{
-		Value:    int32(pasturePb.PregnantCheckResult_Recheck_UnPregnant),
-		Label:    "复检未孕",
+		Value:    int32(pasturePb.PregnantCheckResult_Pregnant),
+		Label:    "有胎",
 		Disabled: true,
 	}, &pasturePb.ConfigOptionsList{
-		Value:    int32(pasturePb.PregnantCheckResult_Recheck_Pregnant),
-		Label:    "初检已孕",
+		Value:    int32(pasturePb.PregnantCheckResult_UnPregnant),
+		Label:    "无胎",
 		Disabled: true,
 	})
 	return configOptions

+ 3 - 2
module/backend/event_base.go

@@ -25,9 +25,9 @@ func (s *StoreEntry) ParseCowIds(ctx context.Context, cowId string) ([]*model.Co
 	if len(cowId) == 0 {
 		return nil, xerr.Custom("cow id is required")
 	}
-
+	cowIdStr := strings.Split(cowId, ",")
 	var cowIds = make([]*model.Cow, 0)
-	for _, v := range strings.Split(cowId, ",") {
+	for _, v := range cowIdStr {
 		cowId, err := strconv.ParseInt(v, 10, 64)
 		cow, err := s.GetCowInfoByCowId(ctx, cowId)
 		if err != nil {
@@ -35,6 +35,7 @@ func (s *StoreEntry) ParseCowIds(ctx context.Context, cowId string) ([]*model.Co
 		}
 		cowIds = append(cowIds, cow)
 	}
+
 	return cowIds, nil
 }
 

+ 94 - 5
module/backend/event_breed.go

@@ -41,7 +41,9 @@ func (s *StoreEntry) CalvingList(ctx context.Context, req *pasturePb.SearchEvent
 		calvingIds = append(calvingIds, v.Id)
 	}
 	calvingCalfList := make([]*model.CalvingCalf, 0)
-	if err := s.DB.Model(new(model.CalvingCalf)).Where("calving_id IN ?", calvingIds).Find(&calvingCalfList).Error; err != nil {
+	if err := s.DB.Model(new(model.CalvingCalf)).
+		Where("calving_id IN ?", calvingIds).
+		Find(&calvingCalfList).Error; err != nil {
 		return nil, xerr.WithStack(err)
 	}
 
@@ -157,13 +159,90 @@ func (s *StoreEntry) PregnantCheckCreate(ctx context.Context, req *pasturePb.Eve
 	eventPregnantCheckList := make([]*model.EventPregnantCheck, 0)
 	currentUser, _ := s.GetCurrentSystemUser(ctx)
 
+	// 更新怀孕牛只
+	updatePregnantCowIds := make([]int64, 0)
+	// 更新流产牛只
+	updateAbortCowIds := make([]int64, 0)
+	// 更新空怀牛只
+	updateEmptyCowIds := make([]int64, 0)
 	for _, cow := range cowList {
-		eventPregnantCheckList = append(eventPregnantCheckList, model.NewEventPregnantCheck(cow, currentUser, req))
+		// 过滤掉没有配种状态的牛只 todo 是否需要返回前端提示用户
+		if cow.BreedStatus != pasturePb.BreedStatus_Breeding {
+			continue
+		}
+		var count int64 = 0
+		if err = s.DB.Model(new(model.EventPregnantCheck)).
+			Where("lact = ?", cow.Lact).
+			Where("cow_id = ?", cow.Id).
+			Order("id desc").Limit(1).Count(&count).Error; err != nil {
+			return xerr.WithStack(err)
+		}
+
+		pregnantCheckResult := req.PregnantCheckResult
+		if req.PregnantCheckResult == pasturePb.PregnantCheckResult_Pregnant {
+			if count > 0 {
+				pregnantCheckResult = pasturePb.PregnantCheckResult_Recheck_Pregnant
+			} else {
+				pregnantCheckResult = pasturePb.PregnantCheckResult_InCheck_Pregnant
+			}
+			updatePregnantCowIds = append(updatePregnantCowIds, cow.Id)
+		}
+
+		if req.PregnantCheckResult == pasturePb.PregnantCheckResult_UnPregnant {
+			if count > 0 {
+				pregnantCheckResult = pasturePb.PregnantCheckResult_Recheck_UnPregnant
+				updateAbortCowIds = append(updateAbortCowIds, cow.Id)
+			} else {
+				pregnantCheckResult = pasturePb.PregnantCheckResult_InCheck_UnPregnant
+				updateEmptyCowIds = append(updateEmptyCowIds, cow.Id)
+			}
+		}
+		eventPregnantCheckList = append(eventPregnantCheckList, model.NewEventPregnantCheck(cow, currentUser, req, pregnantCheckResult))
 	}
 
-	if err := s.DB.Create(eventPregnantCheckList).Error; err != nil {
+	if err = s.DB.Transaction(func(tx *gorm.DB) error {
+		if err = tx.Create(eventPregnantCheckList).Error; err != nil {
+			return xerr.WithStack(err)
+		}
+
+		// 抽象公共方法来更新牛只状态
+		err = s.updateCowStatus(tx, updatePregnantCowIds, pasturePb.BreedStatus_Pregnant, pasturePb.MatingResult_Pregnant)
+		if err != nil {
+			return err
+		}
+
+		err = s.updateCowStatus(tx, updateAbortCowIds, pasturePb.BreedStatus_Abort, pasturePb.MatingResult_Abort)
+		if err != nil {
+			return err
+		}
+
+		err = s.updateCowStatus(tx, updateEmptyCowIds, pasturePb.BreedStatus_Empty, pasturePb.MatingResult_Empty)
+		if err != nil {
+			return err
+		}
+
+		return nil
+	}); err != nil {
 		return xerr.WithStack(err)
 	}
+
+	return nil
+}
+
+// 抽象公共方法来更新牛只状态
+func (s *StoreEntry) updateCowStatus(tx *gorm.DB, cowIds []int64, breedStatus pasturePb.BreedStatus_Kind, matingResult pasturePb.MatingResult_Kind) error {
+	if len(cowIds) > 0 {
+		if err := tx.Model(&model.Cow{}).Where("cow_id IN ?", cowIds).
+			Updates(map[string]interface{}{"breed_status": breedStatus}).Error; err != nil {
+			return xerr.WithStack(err)
+		}
+
+		if err := tx.Model(&model.EventMating{}).Where("cow_id IN ?", cowIds).
+			Group("cow_id").Select("MAX(id) as id").
+			Updates(map[string]interface{}{"mating_result": matingResult}).Error; err != nil {
+			return xerr.WithStack(err)
+		}
+	}
 	return nil
 }
 
@@ -216,13 +295,15 @@ func (s *StoreEntry) MatingCreate(ctx context.Context, req *pasturePb.EventMatin
 	sameTimeCowIds := make([]int64, 0)
 	matingInsertList := make([]*model.EventMating, 0)
 	matingUpdateIds := make([]int64, 0)
+	cowIds := 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 {
+			Where("cow_id = ?", cow.Id).
+			Order("id desc").
+			First(itemEventMating).Count(&count).Error; err != nil {
 			return xerr.WithStack(err)
 		}
 
@@ -250,6 +331,8 @@ func (s *StoreEntry) MatingCreate(ctx context.Context, req *pasturePb.EventMatin
 		if sameTimeCow.Id > 0 {
 			sameTimeCowIds = append(sameTimeCowIds, sameTimeCow.Id)
 		}
+
+		cowIds = append(cowIds, cow.Id)
 	}
 
 	if err = s.DB.Transaction(func(tx *gorm.DB) error {
@@ -295,6 +378,12 @@ func (s *StoreEntry) MatingCreate(ctx context.Context, req *pasturePb.EventMatin
 			return xerr.WithStack(err)
 		}
 
+		if err = tx.Table(new(model.Cow).TableName()).
+			Where("id IN ?", cowIds).
+			UpdateColumn("breed_status", pasturePb.BreedStatus_Breeding).Error; err != nil {
+			return xerr.WithStack(err)
+		}
+
 		return nil
 	}); err != nil {
 		return xerr.WithStack(err)