|
@@ -2,7 +2,9 @@ package backend
|
|
|
|
|
|
import (
|
|
|
"context"
|
|
|
+ "fmt"
|
|
|
"kpt-pasture/model"
|
|
|
+ "time"
|
|
|
|
|
|
"go.uber.org/zap"
|
|
|
|
|
@@ -12,11 +14,10 @@ import (
|
|
|
"gitee.com/xuyiping_admin/pkg/xerr"
|
|
|
)
|
|
|
|
|
|
-// EventCheckBatchModel 批量事件
|
|
|
-type EventCheckBatchModel struct {
|
|
|
+// EventMatingCheckBatchModel 批量配种
|
|
|
+type EventMatingCheckBatchModel struct {
|
|
|
CowList []*model.Cow
|
|
|
FrozenSemen *model.FrozenSemen
|
|
|
- UserModel *model.UserModel
|
|
|
OperationUser *model.SystemUser
|
|
|
}
|
|
|
|
|
@@ -29,7 +30,6 @@ type PregnantCheckBatchModel struct {
|
|
|
PregnantCheckResult pasturePb.PregnantCheckResult_Kind // 孕检结果
|
|
|
PregnantCheckMethod pasturePb.PregnantCheckMethod_Kind // 孕检方式
|
|
|
Remarks string
|
|
|
- UserModel *model.UserModel
|
|
|
LastMating *model.EventMating
|
|
|
}
|
|
|
|
|
@@ -51,16 +51,11 @@ func (s *StoreEntry) EnterCheck(ctx context.Context, req *pasturePb.EventEnterRe
|
|
|
return nil
|
|
|
}
|
|
|
|
|
|
-func (s *StoreEntry) MatingCreateCheck(ctx context.Context, req *pasturePb.EventMating) (*EventCheckBatchModel, error) {
|
|
|
+func (s *StoreEntry) MatingCreateCheck(ctx context.Context, pastureId int64, req *pasturePb.EventMating) (*EventMatingCheckBatchModel, 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))
|
|
@@ -70,7 +65,7 @@ func (s *StoreEntry) MatingCreateCheck(ctx context.Context, req *pasturePb.Event
|
|
|
return nil, xerr.Custom("最多只能选择50只牛只")
|
|
|
}
|
|
|
|
|
|
- cowList, err := s.GetCowInfoByCowIds(ctx, userModel.AppPasture.Id, cowIds)
|
|
|
+ cowList, err := s.GetCowInfoByCowIds(ctx, pastureId, cowIds)
|
|
|
if err != nil {
|
|
|
return nil, xerr.WithStack(err)
|
|
|
}
|
|
@@ -116,69 +111,128 @@ func (s *StoreEntry) MatingCreateCheck(ctx context.Context, req *pasturePb.Event
|
|
|
}
|
|
|
}
|
|
|
|
|
|
- return &EventCheckBatchModel{
|
|
|
+ return &EventMatingCheckBatchModel{
|
|
|
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)
|
|
|
+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)
|
|
|
- for _, pregnantCheckData := range req.Item {
|
|
|
- operationUser, err := s.GetSystemUserById(ctx, int64(pregnantCheckData.OperationId))
|
|
|
+ cowInfo := &model.Cow{}
|
|
|
+ var err error
|
|
|
+ for _, item := range req.Items {
|
|
|
+ cowInfo, err = s.GetCowInfoByCowId(ctx, pastureId, int64(item.CowId))
|
|
|
if err != nil {
|
|
|
- zaplog.Error("PregnantCheckDataCheck", zap.Any("id", pregnantCheckData.OperationId), zap.Any("error", err.Error()))
|
|
|
- return nil, xerr.Customf("获取操作人员信息失败")
|
|
|
+ return nil, xerr.WithStack(err)
|
|
|
+ }
|
|
|
+
|
|
|
+ if cowInfo.Sex != pasturePb.Genders_Female {
|
|
|
+ return nil, xerr.Customf("牛只: %d,不是母牛", cowInfo.Id)
|
|
|
}
|
|
|
- pregnantCheckData.OperationName = operationUser.Name
|
|
|
- cow, err := s.GetCowInfoByCowId(ctx, userModel.AppPasture.Id, int64(pregnantCheckData.CowId))
|
|
|
+
|
|
|
+ operationUser, err := s.GetSystemUserById(ctx, int64(item.OperationId))
|
|
|
if err != nil {
|
|
|
- return nil, xerr.WithStack(err)
|
|
|
+ zaplog.Error("PregnantCheckDataCheck", zap.Any("id", item.OperationId), zap.Any("error", err.Error()))
|
|
|
+ return nil, xerr.Customf("获取操作人员信息失败")
|
|
|
}
|
|
|
|
|
|
// 过滤掉没有配种状态的牛只
|
|
|
- if cow.BreedStatus != pasturePb.BreedStatus_Breeding {
|
|
|
- return nil, xerr.Customf("牛只: %d 未参加配种,不能进行孕检", cow.Id)
|
|
|
+ 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, ok, err := s.FindEventPregnantCheckIsExIstByCowId(ctx, cow)
|
|
|
+ itemEventPregnantCheck, err := s.FindEventPregnantCheckIsExIstByCowId(ctx, cowInfo)
|
|
|
if err != nil {
|
|
|
- zaplog.Error("GetEventPregnantCheckIsExIstByCowId", zap.Any("err", err), zap.Any("cow", cow))
|
|
|
- return nil, xerr.Customf("该牛只孕检事件数据异常, cowId: %d", pregnantCheckData.CowId)
|
|
|
+ return nil, xerr.WithStack(err)
|
|
|
}
|
|
|
|
|
|
- if itemEventPregnantCheck.Id <= 0 || !ok {
|
|
|
- return nil, xerr.Customf("该牛只孕检事件数据异常, cowId: %d", pregnantCheckData.CowId)
|
|
|
+ if itemEventPregnantCheck.Id <= 0 {
|
|
|
+ return nil, xerr.Customf("未发现该牛只: %s 孕检数据", cowInfo.EarNumber)
|
|
|
}
|
|
|
|
|
|
- lastEventMating, ok, err := s.FindLastEventMatingByCowId(ctx, userModel.AppPasture.Id, cow.Id)
|
|
|
- if err != nil || !ok {
|
|
|
- return nil, xerr.Customf("该牛只配种事件数据异常, cowId: %d", pregnantCheckData.CowId)
|
|
|
+ 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: cow,
|
|
|
+ Cow: cowInfo,
|
|
|
OperationUser: operationUser,
|
|
|
- PregnantCheckAt: pregnantCheckData.PregnantCheckAt,
|
|
|
- PregnantCheckMethod: pregnantCheckData.PregnantCheckMethod,
|
|
|
- PregnantCheckResult: pregnantCheckData.PregnantCheckResult,
|
|
|
- Remarks: pregnantCheckData.Remarks,
|
|
|
+ PregnantCheckAt: item.PregnantCheckAt,
|
|
|
+ PregnantCheckMethod: item.PregnantCheckMethod,
|
|
|
+ PregnantCheckResult: item.PregnantCheckResult,
|
|
|
+ Remarks: item.Remarks,
|
|
|
EventPregnancyCheck: itemEventPregnantCheck,
|
|
|
- UserModel: userModel,
|
|
|
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
|
|
|
+}
|