123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184 |
- 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
- UserModel *model.UserModel
- 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
- UserModel *model.UserModel
- LastMating *model.EventMating
- }
- // MatingTimes 更新配次
- type MatingTimes struct {
- Mt int32
- CowId int64
- EventMatingId int64
- }
- func (s *StoreEntry) EnterCheck(ctx context.Context, req *pasturePb.EventEnterRequest) error {
- var count int64
- if err := s.DB.Model(new(model.Cow)).Where("ear_number = ?", req.EarNumber).Count(&count).Error; err != nil {
- return xerr.WithStack(err)
- }
- if count > 0 {
- return xerr.Custom("该牛只已存在")
- }
- return nil
- }
- func (s *StoreEntry) MatingCreateCheck(ctx context.Context, req *pasturePb.EventMating) (*EventCheckBatchModel, error) {
- if len(req.CowIds) <= 0 {
- return nil, xerr.Custom("请选择相关牛只")
- }
- userModel, err := s.GetUserModel(ctx)
- if err != nil {
- return nil, xerr.WithStack(err)
- }
- 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, userModel.AppPasture.Id, cowIds)
- if err != nil {
- return nil, xerr.WithStack(err)
- }
- operationUser, err := s.GetSystemUserById(ctx, 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,
- UserModel: userModel,
- OperationUser: operationUser,
- }, nil
- }
- func (s *StoreEntry) PregnantCheckDataCheck(ctx context.Context, req *pasturePb.EventPregnantCheckBatch) ([]*PregnantCheckBatchModel, error) {
- if len(req.Item) <= 0 {
- return nil, xerr.Custom("请选择相关牛只")
- }
- userModel, err := s.GetUserModel(ctx)
- if err != nil {
- return nil, xerr.WithStack(err)
- }
- pregnantCheckBatchModelList := make([]*PregnantCheckBatchModel, 0)
- for _, pregnantCheckData := range req.Item {
- operationUser, err := s.GetSystemUserById(ctx, 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, userModel.AppPasture.Id, int64(pregnantCheckData.CowId))
- if err != nil {
- return nil, xerr.WithStack(err)
- }
- // 过滤掉没有配种状态的牛只
- if cow.BreedStatus != pasturePb.BreedStatus_Breeding {
- return nil, xerr.Customf("牛只: %d 未参加配种,不能进行孕检", cow.Id)
- }
- 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, userModel.AppPasture.Id, 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,
- UserModel: userModel,
- LastMating: lastEventMating,
- })
- }
- return pregnantCheckBatchModelList, nil
- }
|