package backend import ( "context" "kpt-pasture/model" "go.uber.org/zap" "gitee.com/xuyiping_admin/pkg/logger/zaplog" pasturePb "gitee.com/xuyiping_admin/go_proto/proto/go/backend/cow" "gitee.com/xuyiping_admin/pkg/xerr" ) // EventCheckBatchModel 批量事件 type EventCheckBatchModel struct { CowList []*model.Cow FrozenSemen *model.FrozenSemen CurrentUser *model.SystemUser OperationUser *model.SystemUser } // PregnantCheckBatchModel 批量孕检 type PregnantCheckBatchModel struct { EventPregnancyCheck *model.EventPregnantCheck Cow *model.Cow OperationUser *model.SystemUser PregnantCheckAt int32 // 孕检日期 PregnantCheckResult pasturePb.PregnantCheckResult_Kind // 孕检结果 PregnantCheckMethod pasturePb.PregnantCheckMethod_Kind // 孕检方式 Remarks string CurrentUser *model.SystemUser LastMating *model.EventMating } // MatingTimes 更新配次 type MatingTimes struct { Mt int32 CowId int64 EventMatingId int64 } func (s *StoreEntry) MatingCreateCheck(ctx context.Context, req *pasturePb.EventMating) (*EventCheckBatchModel, error) { if len(req.CowIds) <= 0 { return nil, xerr.Custom("请选择相关牛只") } currentUser, err := s.GetCurrentSystemUser(ctx) if err != nil { return nil, xerr.Custom("当前用户信息错误,请退出重新登录") } cowIds := make([]int64, 0) for _, v := range req.CowIds { cowIds = append(cowIds, int64(v)) } if len(cowIds) > 20 { return nil, xerr.Custom("最多只能选择50只牛只") } cowList, err := s.GetCowInfoByCowIds(ctx, currentUser.PastureId, cowIds) if err != nil { return nil, xerr.WithStack(err) } operationUser, err := s.GetSystemUserById(ctx, currentUser.PastureId, int64(req.OperationId)) if err != nil { return nil, xerr.WithStack(err) } frozenSemen := &model.FrozenSemen{} if err = s.DB.Where("bull_id = ?", req.FrozenSemenNumber). First(frozenSemen).Error; err != nil { return nil, xerr.Custom("未找到冻精信息") } if frozenSemen.Quantity < req.FrozenSemenCount { return nil, xerr.Custom("冻精数量不足") } for _, cow := range cowList { if cow.Sex != pasturePb.Genders_Female { return nil, xerr.Customf("牛只: %d,不是母牛", cow.Id) } if int64(req.MatingAt) < cow.LastMatingAt { return nil, xerr.Customf("牛只: %d,最近一次配种时间: %d,不能小于本次配种时间: %d", cow.Id, cow.LastMatingAt, req.MatingAt) } if int64(req.MatingAt) < cow.LastPregnantCheckAt { return nil, xerr.Customf("牛只: %d,最近一次孕检时间: %d,不能小于本次配种时间: %d", cow.Id, cow.LastPregnantCheckAt, req.MatingAt) } if int64(req.MatingAt) < cow.LastAbortionAt { return nil, xerr.Customf("牛只: %d,最近一次流产时间: %d,不能小于本次配种时间: %d", cow.Id, cow.LastAbortionAt, req.MatingAt) } if int64(req.MatingAt) < cow.BirthAt { return nil, xerr.Customf("牛只: %d,出生时间: %d,不能小于本次配种时间: %d", cow.Id, cow.BirthAt, req.MatingAt) } if cow.BreedStatus == pasturePb.BreedStatus_Pregnant || cow.BreedStatus == pasturePb.BreedStatus_No_Mating { return nil, xerr.Customf("牛只: %d,当前状态为: %s,不能进行配种", cow.Id, cow.BreedStatus.String()) } } return &EventCheckBatchModel{ CowList: cowList, FrozenSemen: frozenSemen, CurrentUser: currentUser, OperationUser: operationUser, }, nil } func (s *StoreEntry) PregnantCheckDataCheck(ctx context.Context, req *pasturePb.EventPregnantCheckBatch) ([]*PregnantCheckBatchModel, error) { if len(req.Item) <= 0 { return nil, xerr.Custom("请选择相关牛只") } currentUser, err := s.GetCurrentSystemUser(ctx) if err != nil { return nil, xerr.Custom("当前用户信息错误,请退出重新登录") } pregnantCheckBatchModelList := make([]*PregnantCheckBatchModel, 0) for _, pregnantCheckData := range req.Item { operationUser, err := s.GetSystemUserById(ctx, currentUser.PastureId, int64(pregnantCheckData.OperationId)) if err != nil { zaplog.Error("PregnantCheckDataCheck", zap.Any("id", pregnantCheckData.OperationId), zap.Any("error", err.Error())) return nil, xerr.Customf("获取操作人员信息失败") } pregnantCheckData.OperationName = operationUser.Name cow, err := s.GetCowInfoByCowId(ctx, currentUser.PastureId, int64(pregnantCheckData.CowId)) if err != nil { return nil, xerr.WithStack(err) } // 过滤掉没有配种状态的牛只 if cow.BreedStatus != pasturePb.BreedStatus_Breeding { return nil, xerr.Customf("牛只: %d,当前状态为: %s,不能进行孕检", cow.Id, cow.BreedStatus.String()) } itemEventPregnantCheck, ok, err := s.FindEventPregnantCheckIsExIstByCowId(ctx, cow) if err != nil { zaplog.Error("GetEventPregnantCheckIsExIstByCowId", zap.Any("err", err), zap.Any("cow", cow)) return nil, xerr.Customf("该牛只孕检事件数据异常, cowId: %d", pregnantCheckData.CowId) } if itemEventPregnantCheck.Id <= 0 || !ok { return nil, xerr.Customf("该牛只孕检事件数据异常, cowId: %d", pregnantCheckData.CowId) } lastEventMating, ok, err := s.FindLastEventMatingByCowId(ctx, currentUser.PastureId, cow.Id) if err != nil || !ok { return nil, xerr.Customf("该牛只配种事件数据异常, cowId: %d", pregnantCheckData.CowId) } pregnantCheckBatchModelList = append(pregnantCheckBatchModelList, &PregnantCheckBatchModel{ Cow: cow, OperationUser: operationUser, PregnantCheckAt: pregnantCheckData.PregnantCheckAt, PregnantCheckMethod: pregnantCheckData.PregnantCheckMethod, PregnantCheckResult: pregnantCheckData.PregnantCheckResult, Remarks: pregnantCheckData.Remarks, EventPregnancyCheck: itemEventPregnantCheck, CurrentUser: currentUser, LastMating: lastEventMating, }) } return pregnantCheckBatchModelList, nil }