|
@@ -6,7 +6,6 @@ import (
|
|
|
"kpt-pasture/model"
|
|
|
"kpt-pasture/util"
|
|
|
"net/http"
|
|
|
- "strconv"
|
|
|
"strings"
|
|
|
"time"
|
|
|
|
|
@@ -360,59 +359,79 @@ func (s *StoreEntry) EstrusList(ctx context.Context, req *pasturePb.EstrusItemsR
|
|
|
}, nil
|
|
|
}
|
|
|
|
|
|
-func (s *StoreEntry) EstrusBatchMating(ctx context.Context, req *pasturePb.EventEstrus) (err error) {
|
|
|
+func (s *StoreEntry) EstrusBatchMating(ctx context.Context, req *pasturePb.EventNaturalEstrusBatch) (err error) {
|
|
|
userModel, err := s.GetUserModel(ctx)
|
|
|
if err != nil {
|
|
|
return xerr.WithStack(err)
|
|
|
}
|
|
|
- operationUser, err := s.GetSystemUserById(ctx, int64(req.OperationId))
|
|
|
- if err != nil {
|
|
|
- return xerr.Customf("该用户不存在: %d", req.OperationId)
|
|
|
- }
|
|
|
- req.OperationName = operationUser.Name
|
|
|
|
|
|
- eventEstrusIds := make([]string, 0)
|
|
|
+ eventEstrusList := make([]*model.EventEstrus, 0)
|
|
|
eventMatingList := make([]*model.EventMating, 0)
|
|
|
- cowIds := make([]int32, 0)
|
|
|
- for _, cowId := range req.CowIds {
|
|
|
- cowInfo := GetCowInfoByCowId(s.DB, int64(cowId))
|
|
|
- if cowInfo == nil {
|
|
|
+ unMatingReasonsMap := s.UnMatingReasonsMap()
|
|
|
+ for _, item := range req.Items {
|
|
|
+ cowInfo, err := s.GetCowInfoByCowId(ctx, userModel.AppPasture.Id, int64(item.CowId))
|
|
|
+ if err != nil {
|
|
|
return xerr.Custom("牛只信息不存在")
|
|
|
}
|
|
|
|
|
|
- newEventMating := model.NewEventMating(userModel.AppPasture.Id, cowInfo, time.Now().Unix(), pasturePb.ExposeEstrusType_Neck_Ring)
|
|
|
- eventEstrus, ok, err := s.FindEventEstrusByCowId(ctx, userModel.AppPasture.Id, int64(cowId))
|
|
|
+ if cowInfo.Sex != pasturePb.Genders_Female {
|
|
|
+ return xerr.Custom("该牛只不是母牛")
|
|
|
+ }
|
|
|
+
|
|
|
+ existsEventEstrus, isExists, err := s.FindEventEstrusByCowId(ctx, userModel.AppPasture.Id, int64(item.CowId))
|
|
|
if err != nil {
|
|
|
return xerr.WithStack(err)
|
|
|
}
|
|
|
|
|
|
- if !ok {
|
|
|
+ // 如果存在,并且是脖环揭发则跳过
|
|
|
+ if isExists && existsEventEstrus.ExposeEstrusType == pasturePb.ExposeEstrusType_Neck_Ring {
|
|
|
+ zaplog.Info("EventNaturalEstrusBatch", zap.Any("existsEventEstrus", existsEventEstrus), zap.Any("item", item))
|
|
|
continue
|
|
|
}
|
|
|
|
|
|
- newEventMating.EventEstrusId = eventEstrus.Id
|
|
|
- eventEstrusIds = append(eventEstrusIds, strconv.FormatInt(eventEstrus.Id, 10))
|
|
|
- eventMatingList = append(eventMatingList, newEventMating)
|
|
|
- cowIds = append(cowIds, cowId)
|
|
|
- }
|
|
|
- if len(eventMatingList) <= 0 {
|
|
|
- return nil
|
|
|
+ operationUser, _ := s.GetSystemUserById(ctx, userModel.SystemUser.Id)
|
|
|
+ newEventEstrus := model.NewEventEstrus(
|
|
|
+ userModel.AppPasture.Id,
|
|
|
+ cowInfo,
|
|
|
+ pasturePb.ExposeEstrusType_Natural_Estrus,
|
|
|
+ pasturePb.IsShow_Ok,
|
|
|
+ operationUser,
|
|
|
+ userModel.SystemUser,
|
|
|
+ )
|
|
|
+ if item.IsMating == pasturePb.IsShow_Ok {
|
|
|
+ newEventEstrus.IsMating = pasturePb.IsShow_Ok
|
|
|
+ newEventMating := model.NewEventMating(userModel.AppPasture.Id, cowInfo, time.Now().Unix(), pasturePb.ExposeEstrusType_Natural_Estrus)
|
|
|
+ eventMatingList = append(eventMatingList, newEventMating)
|
|
|
+ } else {
|
|
|
+ newEventEstrus.IsMating = pasturePb.IsShow_No
|
|
|
+ newEventEstrus.UnMatingReasonsKind = item.UnMatingReasonsKind
|
|
|
+ newEventEstrus.UnMatingReasonsName = unMatingReasonsMap[item.UnMatingReasonsKind]
|
|
|
+ }
|
|
|
+ eventEstrusList = append(eventEstrusList, newEventEstrus)
|
|
|
}
|
|
|
|
|
|
+ // 记录事件日志
|
|
|
+ defer func() {
|
|
|
+ if err == nil {
|
|
|
+ if len(eventEstrusList) > 0 {
|
|
|
+ for _, v := range eventEstrusList {
|
|
|
+ cow, _ := s.GetCowInfoByCowId(ctx, userModel.AppPasture.Id, v.CowId)
|
|
|
+ cowLogs := s.SubmitEventLog(ctx, userModel.AppPasture.Id, cow, pasturePb.EventType_Estrus, v.ExposeEstrusType, v)
|
|
|
+ s.DB.Table(cowLogs.TableName()).Create(cowLogs)
|
|
|
+ }
|
|
|
+ }
|
|
|
+ }
|
|
|
+ }()
|
|
|
+
|
|
|
if err = s.DB.Transaction(func(tx *gorm.DB) error {
|
|
|
- if len(eventEstrusIds) > 0 {
|
|
|
- if err = tx.Model(new(model.EventEstrus)).
|
|
|
- Where("id IN ?", eventEstrusIds).
|
|
|
- Updates(map[string]interface{}{
|
|
|
- "is_show": pasturePb.IsShow_No,
|
|
|
- "check_result": pasturePb.CheckResult_Correct,
|
|
|
- }).Error; err != nil {
|
|
|
+ if len(eventMatingList) > 0 {
|
|
|
+ if err = tx.Model(new(model.EventMating)).Create(eventMatingList).Error; err != nil {
|
|
|
return xerr.WithStack(err)
|
|
|
}
|
|
|
}
|
|
|
|
|
|
- if len(eventMatingList) > 0 {
|
|
|
- if err = tx.Create(eventMatingList).Error; err != nil {
|
|
|
+ if len(eventEstrusList) > 0 {
|
|
|
+ if err = tx.Model(new(model.EventEstrus)).Create(eventEstrusList).Error; err != nil {
|
|
|
return xerr.WithStack(err)
|
|
|
}
|
|
|
}
|
|
@@ -420,21 +439,6 @@ func (s *StoreEntry) EstrusBatchMating(ctx context.Context, req *pasturePb.Event
|
|
|
}); err != nil {
|
|
|
return xerr.WithStack(err)
|
|
|
}
|
|
|
-
|
|
|
- // 配种信息
|
|
|
- eventMating := &pasturePb.EventMating{
|
|
|
- CowIds: cowIds,
|
|
|
- FrozenSemenNumber: req.BullNumber,
|
|
|
- FrozenSemenCount: req.Quantity,
|
|
|
- OperationId: req.OperationId,
|
|
|
- Remarks: req.Remarks,
|
|
|
- MatingAt: req.MatingAt,
|
|
|
- ExposeEstrusType: pasturePb.ExposeEstrusType_Neck_Ring,
|
|
|
- }
|
|
|
- if err = s.MatingCreate(ctx, eventMating); err != nil {
|
|
|
- zaplog.Error("EstrusBatchMating", zap.Any("MatingCreate", err), zap.Any("eventMating", eventMating))
|
|
|
- return xerr.WithStack(err)
|
|
|
- }
|
|
|
return nil
|
|
|
}
|
|
|
|