|
@@ -2,7 +2,6 @@ package backend
|
|
|
|
|
|
import (
|
|
|
"context"
|
|
|
- "errors"
|
|
|
"fmt"
|
|
|
"kpt-pasture/model"
|
|
|
"kpt-pasture/util"
|
|
@@ -399,256 +398,3 @@ func (s *StoreEntry) EstrusBatchMating(ctx context.Context, req *pasturePb.Event
|
|
|
}
|
|
|
return nil
|
|
|
}
|
|
|
-
|
|
|
-func (s *StoreEntry) MatingList(ctx context.Context, req *pasturePb.SearchEventRequest, pagination *pasturePb.PaginationModel) (*pasturePb.EventMatingResponse, error) {
|
|
|
- userModel, err := s.GetUserModel(ctx)
|
|
|
- if err != nil {
|
|
|
- return nil, xerr.WithStack(err)
|
|
|
- }
|
|
|
-
|
|
|
- matingList := make([]*model.EventMating, 0)
|
|
|
- var count int64 = 0
|
|
|
- pref := s.DB.Model(new(model.EventMating)).
|
|
|
- Where("mating_result > ?", pasturePb.MatingResult_Invalid).
|
|
|
- Where("status = ?", pasturePb.IsShow_Ok).
|
|
|
- Where("pasture_id = ?", userModel.AppPasture.Id)
|
|
|
- if len(req.CowId) > 0 {
|
|
|
- cowIds := strings.Split(req.CowId, ",")
|
|
|
- pref.Where("cow_id IN ?", cowIds)
|
|
|
- }
|
|
|
-
|
|
|
- if err = pref.Order("id desc").
|
|
|
- Count(&count).Limit(int(pagination.PageSize)).
|
|
|
- Offset(int(pagination.PageOffset)).
|
|
|
- Find(&matingList).Error; err != nil {
|
|
|
- return nil, xerr.WithStack(err)
|
|
|
- }
|
|
|
-
|
|
|
- exposeEstrusTypeMap := s.ExposeEstrusTypeMap()
|
|
|
- return &pasturePb.EventMatingResponse{
|
|
|
- Code: http.StatusOK,
|
|
|
- Msg: "ok",
|
|
|
- Data: &pasturePb.SearchMatingData{
|
|
|
- List: model.EventMatingSlice(matingList).ToPB(exposeEstrusTypeMap),
|
|
|
- Total: int32(count),
|
|
|
- PageSize: pagination.PageSize,
|
|
|
- Page: pagination.Page,
|
|
|
- },
|
|
|
- }, nil
|
|
|
-}
|
|
|
-
|
|
|
-// MatingCreate 牛只配种
|
|
|
-func (s *StoreEntry) MatingCreate(ctx context.Context, req *pasturePb.EventMating) (err error) {
|
|
|
- userModel, err := s.GetUserModel(ctx)
|
|
|
- if err != nil {
|
|
|
- return xerr.WithStack(err)
|
|
|
- }
|
|
|
-
|
|
|
- eventMatingCheckModel, err := s.MatingCreateCheck(ctx, userModel.AppPasture.Id, req)
|
|
|
- if err != nil {
|
|
|
- return xerr.WithStack(err)
|
|
|
- }
|
|
|
-
|
|
|
- req.OperationName = eventMatingCheckModel.OperationUser.Name
|
|
|
- if err = s.DB.Transaction(func(tx *gorm.DB) error {
|
|
|
- for _, cow := range eventMatingCheckModel.CowList {
|
|
|
- // 获取牛只最近一次配种信息
|
|
|
- lastEventMating, err := s.FindLastEventMatingByCowId(ctx, userModel.AppPasture.Id, cow.Id)
|
|
|
- if err != nil {
|
|
|
- // 1. 没有配种信息,(第一次参加配种的牛只,并且没有参与同期的牛只 )
|
|
|
- if errors.Is(err, gorm.ErrRecordNotFound) {
|
|
|
- newMating := model.NewEventMatingNaturalEstrus(userModel.AppPasture.Id, cow, req, userModel.SystemUser)
|
|
|
- if err = tx.Create(newMating).Error; err != nil {
|
|
|
- return xerr.WithStack(err)
|
|
|
- }
|
|
|
- if err = s.MatingCowUpdate(ctx, userModel.AppPasture.Id, cow, req, false); err != nil {
|
|
|
- return xerr.WithStack(err)
|
|
|
- }
|
|
|
- continue
|
|
|
- } else {
|
|
|
- return xerr.WithStack(err)
|
|
|
- }
|
|
|
- }
|
|
|
-
|
|
|
- // 2. 所有有配种数据的牛只
|
|
|
- if lastEventMating == nil || lastEventMating.Id <= 0 || lastEventMating.Status != pasturePb.IsShow_No {
|
|
|
- zaplog.Error("MatingCreate", zap.Any("cow", cow), zap.Any("UserModel", userModel))
|
|
|
- return xerr.Customf("牛只配种数据异常: %d", cow.EarNumber)
|
|
|
- }
|
|
|
-
|
|
|
- // 2.1 复配 => 本胎次配次不加1
|
|
|
- isReMating := lastEventMating.IsReMating(cow, int64(req.MatingAt))
|
|
|
- if isReMating {
|
|
|
- lastEventMating.EventReMatingUpdate(int64(req.MatingAt))
|
|
|
- if err = tx.Model(lastEventMating).
|
|
|
- Select("mating_result", "mating_result_at", "status").
|
|
|
- Where("id = ?", lastEventMating.Id).
|
|
|
- Updates(lastEventMating).Error; err != nil {
|
|
|
- }
|
|
|
- }
|
|
|
-
|
|
|
- // 2.2. 同期初配
|
|
|
- IsMatingUpdate := lastEventMating.IsMatingUpdate()
|
|
|
- if IsMatingUpdate {
|
|
|
- lastEventMating.EventUpdate(int64(req.MatingAt), req.FrozenSemenNumber, isReMating, eventMatingCheckModel.OperationUser, userModel.SystemUser)
|
|
|
- if err = tx.Model(lastEventMating).
|
|
|
- Select("mating_at", "status", "reality_day", "frozen_semen_number", "operation_id", "operation_name", "message_id", "message_name").
|
|
|
- Where("id = ?", lastEventMating.Id).
|
|
|
- Updates(lastEventMating).Error; err != nil {
|
|
|
- return xerr.WithStack(err)
|
|
|
- }
|
|
|
- }
|
|
|
-
|
|
|
- // 2.3. 提交的配种数据中,定位出空怀的牛只,
|
|
|
- isEmptyMating := lastEventMating.IsEmptyMating(cow, int64(req.MatingAt))
|
|
|
- if isEmptyMating {
|
|
|
- // 把上次配种结果信息更新成空怀
|
|
|
- lastEventMating.EventMatingResultUpdate(pasturePb.MatingResult_Empty, int64(req.MatingAt))
|
|
|
- if err = tx.Model(lastEventMating).
|
|
|
- Select("mating_result", "mating_result_at").
|
|
|
- Where("id = ?", lastEventMating.Id).
|
|
|
- Updates(lastEventMating).Error; err != nil {
|
|
|
- return xerr.WithStack(err)
|
|
|
- }
|
|
|
- // 先创建一条新的配种数据
|
|
|
- newMating := model.NewEventMating(userModel.AppPasture.Id, cow, int64(req.MatingAt), req.ExposeEstrusType)
|
|
|
- newMating.EventUpdate(int64(req.MatingAt), req.FrozenSemenNumber, isReMating, eventMatingCheckModel.OperationUser, userModel.SystemUser)
|
|
|
- if err = tx.Model(newMating).Create(newMating).Error; err != nil {
|
|
|
- return xerr.WithStack(err)
|
|
|
- }
|
|
|
-
|
|
|
- // 更新牛只配种结果日志
|
|
|
- if err = s.UpdateMatingResultEventCowLogByCowId(ctx, cow.Id, s.MatingResultMap()[pasturePb.MatingResult_Empty]); err != nil {
|
|
|
- zaplog.Error("MatingCreate", zap.Any("UpdateEventCowLogByCowId", err), zap.Any("cow", cow))
|
|
|
- }
|
|
|
- }
|
|
|
-
|
|
|
- // 牛只基本中配种信息更新
|
|
|
- if err = s.MatingCowUpdate(ctx, userModel.AppPasture.Id, cow, req, isReMating); err != nil {
|
|
|
- return xerr.WithStack(err)
|
|
|
- }
|
|
|
- }
|
|
|
-
|
|
|
- // 创建冻精使用记录日志
|
|
|
- itemFrozenSemenLog := model.NewEventFrozenSemenLog(eventMatingCheckModel.FrozenSemen.PastureId, req)
|
|
|
- if err = tx.Create(itemFrozenSemenLog).Error; err != nil {
|
|
|
- return xerr.WithStack(err)
|
|
|
- }
|
|
|
-
|
|
|
- // 减去精液的数量
|
|
|
- eventMatingCheckModel.FrozenSemen.EventQuantityUpdate(req.FrozenSemenCount)
|
|
|
- if err = tx.Model(eventMatingCheckModel.FrozenSemen).
|
|
|
- Select("quantity").
|
|
|
- Where("id = ?", eventMatingCheckModel.FrozenSemen.Id).
|
|
|
- Updates(eventMatingCheckModel.FrozenSemen).Error; err != nil {
|
|
|
- return xerr.WithStack(err)
|
|
|
- }
|
|
|
- return nil
|
|
|
- }); err != nil {
|
|
|
- return xerr.WithStack(err)
|
|
|
- }
|
|
|
-
|
|
|
- return nil
|
|
|
-}
|
|
|
-
|
|
|
-func (s *StoreEntry) MatingCowUpdate(ctx context.Context, pastureId int64, cow *model.Cow, req *pasturePb.EventMating, isReMating bool) error {
|
|
|
- // 牛只基本中配种信息更新
|
|
|
- cow.EventMatingUpdate(int64(req.MatingAt), req.FrozenSemenNumber, isReMating)
|
|
|
- if err := s.DB.Model(cow).
|
|
|
- Select("last_mating_at", "mating_times", "last_bull_number", "first_mating_at", "is_pregnant", "breed_status").
|
|
|
- Where("id = ?", cow.Id).
|
|
|
- Updates(cow).Error; err != nil {
|
|
|
- return xerr.WithStack(err)
|
|
|
- }
|
|
|
- // 记录日志
|
|
|
- cowLogs := s.SubmitEventLog(ctx, pastureId, cow, pasturePb.EventType_Mating, req.ExposeEstrusType, req)
|
|
|
- if err := s.DB.Table(cowLogs.TableName()).Create(cowLogs).Error; err != nil {
|
|
|
- return xerr.WithStack(err)
|
|
|
- }
|
|
|
- return nil
|
|
|
-}
|
|
|
-
|
|
|
-func (s *StoreEntry) WeaningBatch(ctx context.Context, req *pasturePb.EventWeaningBatchRequest) (err error) {
|
|
|
- userModel, err := s.GetUserModel(ctx)
|
|
|
- if err != nil {
|
|
|
- return xerr.WithStack(err)
|
|
|
- }
|
|
|
-
|
|
|
- if len(req.Item) <= 0 {
|
|
|
- return nil
|
|
|
- }
|
|
|
- cowIds := make([]int64, 0)
|
|
|
- cowWeightMap := make(map[int64]float64)
|
|
|
- for _, item := range req.Item {
|
|
|
- cowIds = append(cowIds, int64(item.CowId))
|
|
|
- cowWeightMap[int64(item.CowId)] = float64(item.Weight)
|
|
|
- }
|
|
|
-
|
|
|
- eventWeaningList := make([]*model.EventWeaning, 0)
|
|
|
- if err = s.DB.Model(new(model.EventWeaning)).
|
|
|
- Where("cow_id IN ?", cowIds).
|
|
|
- Where("status = ?", pasturePb.IsShow_No).
|
|
|
- Find(&eventWeaningList).Error; err != nil {
|
|
|
- return xerr.WithStack(err)
|
|
|
- }
|
|
|
-
|
|
|
- operation, err := s.GetSystemUserById(ctx, int64(req.OperationId))
|
|
|
- if err != nil {
|
|
|
- return xerr.WithStack(err)
|
|
|
- }
|
|
|
-
|
|
|
- defer func() {
|
|
|
- if err != nil {
|
|
|
- for _, cowId := range cowIds {
|
|
|
- cow, _ := s.GetCowInfoByCowId(ctx, userModel.AppPasture.Id, cowId)
|
|
|
- cowLogs := s.SubmitEventLog(ctx, userModel.AppPasture.Id, cow, pasturePb.EventType_Weaning, pasturePb.ExposeEstrusType_Invalid, req)
|
|
|
- s.DB.Table(cowLogs.TableName()).Create(cowLogs)
|
|
|
- }
|
|
|
- }
|
|
|
- }()
|
|
|
-
|
|
|
- if err = s.DB.Transaction(func(tx *gorm.DB) error {
|
|
|
- cowInfo := &model.Cow{}
|
|
|
- for _, eventWeaning := range eventWeaningList {
|
|
|
- eventWeaning.EventUpdate(int64(req.WeaningAt), int32(cowWeightMap[cowInfo.Id]*1000), req.Remarks, req.PenId, operation, userModel.SystemUser)
|
|
|
- if err = tx.Model(new(model.EventWeaning)).
|
|
|
- Select("status", "reality_day", "operation_id", "operation_name", "message_id", "message_name", "remarks", "after_pen_id").
|
|
|
- Where("id = ?", eventWeaning.Id).
|
|
|
- Updates(eventWeaning).Error; err != nil {
|
|
|
- return xerr.WithStack(err)
|
|
|
- }
|
|
|
- cowInfo, err = s.GetCowInfoByCowId(ctx, userModel.AppPasture.Id, eventWeaning.CowId)
|
|
|
- if err != nil {
|
|
|
- return xerr.WithStack(err)
|
|
|
- }
|
|
|
- cowInfo.EventWeaningUpdate(int64(req.WeaningAt), req.PenId, int64(cowWeightMap[cowInfo.Id]*1000))
|
|
|
- if err = tx.Model(new(model.Cow)).
|
|
|
- Select("pen_id", "current_weight", "weaning_at", "last_weight_at").
|
|
|
- Where("id = ?", cowInfo.Id).
|
|
|
- Updates(cowInfo).Error; err != nil {
|
|
|
- return xerr.WithStack(err)
|
|
|
- }
|
|
|
- // 创建牛只的体重记录
|
|
|
- newEventWeight := model.NewEventWeight(
|
|
|
- userModel.AppPasture.Id,
|
|
|
- cowInfo,
|
|
|
- userModel.SystemUser,
|
|
|
- &pasturePb.EventWeight{
|
|
|
- WeightAt: req.WeaningAt,
|
|
|
- OperationId: req.OperationId,
|
|
|
- OperationName: req.OperationName,
|
|
|
- Remarks: req.Remarks,
|
|
|
- Weight: float32(cowWeightMap[cowInfo.Id]),
|
|
|
- Height: 0,
|
|
|
- })
|
|
|
- if err = tx.Create(newEventWeight).Error; err != nil {
|
|
|
- return xerr.WithStack(err)
|
|
|
- }
|
|
|
- }
|
|
|
-
|
|
|
- return nil
|
|
|
- }); err != nil {
|
|
|
- return xerr.WithStack(err)
|
|
|
- }
|
|
|
- return nil
|
|
|
-}
|