123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281 |
- package backend
- import (
- "context"
- "fmt"
- "kpt-pasture/model"
- "time"
- "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"
- )
- // EventMatingCheckBatchModel 批量配种
- type EventMatingCheckBatchModel struct {
- CowList []*model.Cow
- FrozenSemen *model.FrozenSemen
- 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
- LastMating *model.EventMating
- }
- // MatingTimes 更新配次
- type MatingTimes struct {
- Mt int32
- CowId int64
- EventMatingId int64
- }
- type AbortionCheckBatchModel struct {
- EventAbortion *model.EventAbortion
- Cow *model.Cow
- OperationUser *model.SystemUser
- }
- 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, pastureId int64, req *pasturePb.EventMating) (*EventMatingCheckBatchModel, error) {
- if len(req.CowIds) <= 0 {
- 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, pastureId, 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 &EventMatingCheckBatchModel{
- CowList: cowList,
- FrozenSemen: frozenSemen,
- OperationUser: operationUser,
- }, nil
- }
- func (s *StoreEntry) PregnantCheckDataCheck(ctx context.Context, pastureId int64, req *pasturePb.EventPregnantCheckBatch) ([]*PregnantCheckBatchModel, error) {
- if len(req.Items) <= 0 {
- return nil, xerr.Custom("请选择相关牛只数据")
- }
- pregnantCheckBatchModelList := make([]*PregnantCheckBatchModel, 0)
- cowInfo := &model.Cow{}
- var err error
- for _, item := range req.Items {
- cowInfo, err = s.GetCowInfoByCowId(ctx, pastureId, int64(item.CowId))
- if err != nil {
- return nil, xerr.WithStack(err)
- }
- if cowInfo.Sex != pasturePb.Genders_Female {
- return nil, xerr.Customf("牛只: %d,不是母牛", cowInfo.Id)
- }
- operationUser, err := s.GetSystemUserById(ctx, int64(item.OperationId))
- if err != nil {
- zaplog.Error("PregnantCheckDataCheck", zap.Any("id", item.OperationId), zap.Any("error", err.Error()))
- return nil, xerr.Customf("获取操作人员信息失败")
- }
- // 过滤掉没有配种状态的牛只
- if cowInfo.BreedStatus != pasturePb.BreedStatus_Breeding && cowInfo.BreedStatus != pasturePb.BreedStatus_Pregnant {
- return nil, xerr.Customf("牛只: %s 未参加配种,不能进行孕检", cowInfo.EarNumber)
- }
- if cowInfo.LastMatingAt <= 0 {
- return nil, xerr.Customf("牛只: %s,最近一次配种数据异常", cowInfo.EarNumber)
- }
- itemEventPregnantCheck, err := s.FindEventPregnantCheckIsExIstByCowId(ctx, cowInfo)
- if err != nil {
- return nil, xerr.WithStack(err)
- }
- if itemEventPregnantCheck.Id <= 0 {
- return nil, xerr.Customf("未发现该牛只: %s 孕检数据", cowInfo.EarNumber)
- }
- lastEventMating, err := s.FindLastEventMatingByCowId(ctx, pastureId, cowInfo.Id)
- if err != nil {
- return nil, xerr.WithStack(err)
- }
- if lastEventMating == nil || lastEventMating.Status == pasturePb.IsShow_No {
- return nil, xerr.Customf("未发现该牛只: %s 配种数据", cowInfo.EarNumber)
- }
- pregnantCheckBatchModelList = append(pregnantCheckBatchModelList, &PregnantCheckBatchModel{
- Cow: cowInfo,
- OperationUser: operationUser,
- PregnantCheckAt: item.PregnantCheckAt,
- PregnantCheckMethod: item.PregnantCheckMethod,
- PregnantCheckResult: item.PregnantCheckResult,
- Remarks: item.Remarks,
- EventPregnancyCheck: itemEventPregnantCheck,
- LastMating: lastEventMating,
- })
- }
- return pregnantCheckBatchModelList, nil
- }
- func (s *StoreEntry) EstrusCheckDataCheck(ctx context.Context, userModel *model.UserModel, items []*pasturePb.EventNaturalEstrusItems) ([]*model.EventEstrus, []*model.EventMating, error) {
- eventEstrusList := make([]*model.EventEstrus, 0)
- eventMatingList := make([]*model.EventMating, 0)
- unMatingReasonsMap := s.UnMatingReasonsMap()
- for _, item := range items {
- cowInfo, err := s.GetCowInfoByCowId(ctx, userModel.AppPasture.Id, int64(item.CowId))
- if err != nil {
- return nil, nil, xerr.Custom("牛只信息不存在")
- }
- if cowInfo.Sex != pasturePb.Genders_Female {
- return nil, nil, xerr.Custom("该牛只不是母牛")
- }
- existsEventEstrus, isExists, err := s.FindEventEstrusByCowId(ctx, userModel.AppPasture.Id, int64(item.CowId))
- if err != nil {
- return nil, nil, xerr.WithStack(err)
- }
- // 如果存在,并且是脖环揭发则跳过
- if isExists {
- if existsEventEstrus.ExposeEstrusType == pasturePb.ExposeEstrusType_Neck_Ring {
- zaplog.Info("EventNaturalEstrusBatch", zap.Any("existsEventEstrus", existsEventEstrus), zap.Any("item", item))
- continue
- }
- realityDay := time.Unix(existsEventEstrus.RealityDay, 0).Format(model.LayoutDate2)
- planDay := time.Unix(int64(item.EstrusAt), 0).Format(model.LayoutDate2)
- if realityDay == planDay {
- return nil, nil, xerr.Customf("该牛只:%d,今天已经提交过发情数据", item.CowId)
- }
- }
- operationUser, _ := s.GetSystemUserById(ctx, int64(item.OperationId))
- item.OperationName = operationUser.Name
- newEventEstrus := model.NewEventEstrus(userModel.AppPasture.Id, cowInfo, pasturePb.ExposeEstrusType_Natural_Estrus,
- pasturePb.IsShow_Ok, item.IsMating, item.EstrusAt, operationUser, userModel.SystemUser)
- fmt.Println("item.IsMating", item.IsMating, item.UnMatingReasonsKind)
- if item.IsMating == pasturePb.IsShow_Ok {
- newEventMating := model.NewEventMating(userModel.AppPasture.Id, cowInfo, time.Now().Unix(), pasturePb.ExposeEstrusType_Natural_Estrus)
- eventMatingList = append(eventMatingList, newEventMating)
- } else {
- newEventEstrus.UnMatingReasonsKind = item.UnMatingReasonsKind
- newEventEstrus.UnMatingReasonsName = unMatingReasonsMap[item.UnMatingReasonsKind]
- }
- newEventEstrus.Remarks = item.Remarks
- eventEstrusList = append(eventEstrusList, newEventEstrus)
- }
- return eventEstrusList, eventMatingList, nil
- }
- func (s *StoreEntry) AbortionEventDataCheck(ctx context.Context, userModel *model.UserModel, items []*pasturePb.EventAbortionItem) ([]*AbortionCheckBatchModel, error) {
- abortionCheckBatchModelList := make([]*AbortionCheckBatchModel, 0)
- for _, item := range items {
- cow, err := s.GetCowInfoByCowId(ctx, userModel.AppPasture.Id, int64(item.CowId))
- if err != nil {
- return nil, xerr.WithStack(err)
- }
- if cow.Sex != pasturePb.Genders_Female {
- return nil, xerr.Customf("牛只: %s,不是母牛", cow.EarNumber)
- }
- if cow.IsPregnant != pasturePb.IsShow_Ok {
- return nil, xerr.Customf("牛只: %s,不是怀孕状态", cow.EarNumber)
- }
- if cow.BreedStatus != pasturePb.BreedStatus_Pregnant {
- return nil, xerr.Customf("牛只: %s,不是怀孕状态", cow.EarNumber)
- }
- operationUser, err := s.GetSystemUserById(ctx, int64(item.OperationId))
- if err != nil {
- return nil, xerr.WithStack(err)
- }
- item.OperationName = operationUser.Name
- item.AbortionReasonsName = s.AbortionReasonsMap()[item.AbortionReasons]
- newEventAbortion := model.NewEventAbortion(userModel.AppPasture.Id, cow, item, pasturePb.IsShow_No)
- abortionCheckBatchModelList = append(abortionCheckBatchModelList, &AbortionCheckBatchModel{
- EventAbortion: newEventAbortion,
- Cow: cow,
- OperationUser: operationUser,
- })
- }
- return abortionCheckBatchModelList, nil
- }
|