|
@@ -111,8 +111,10 @@ func (s *StoreEntry) CalvingCreate(ctx context.Context, req *pasturePb.EventCalv
|
|
|
}
|
|
|
if err = s.DB.Transaction(func(tx *gorm.DB) error {
|
|
|
// 更新产犊事件表
|
|
|
- newEventCalving.Update(operationUser, req, cow)
|
|
|
+ newEventCalving.EventUpdate(operationUser, req, cow)
|
|
|
if err = tx.Model(new(model.EventCalving)).
|
|
|
+ Select("operation_id", "operation_name", "reality_day", "day_age", "pregnancy_age", "bull_number",
|
|
|
+ "calving_level", "child_number", "status", "is_inducing_childbirth", "pen_id", "dystocia_reason", "remarks").
|
|
|
Where("id = ?", newEventCalving.Id).
|
|
|
Updates(newEventCalving).Error; err != nil {
|
|
|
return xerr.WithStack(err)
|
|
@@ -139,7 +141,7 @@ func (s *StoreEntry) CalvingCreate(ctx context.Context, req *pasturePb.EventCalv
|
|
|
// 更新母牛信息
|
|
|
cow.EventCalvingUpdate(int64(req.CalvingAt))
|
|
|
if err = tx.Model(cow).
|
|
|
- Select("calving_age,mating_times,pregnancy_age,lact,breed_status,is_pregnant,last_calving_at").
|
|
|
+ Select("calving_age", "mating_times", "pregnancy_age", "lact", "breed_status", "is_pregnant", "last_calving_at").
|
|
|
Where("id = ?", cow.Id).
|
|
|
Updates(cow).Error; err != nil {
|
|
|
return xerr.WithStack(err)
|
|
@@ -151,126 +153,6 @@ func (s *StoreEntry) CalvingCreate(ctx context.Context, req *pasturePb.EventCalv
|
|
|
return nil
|
|
|
}
|
|
|
|
|
|
-func (s *StoreEntry) EstrusList(ctx context.Context, req *pasturePb.EstrusItemsRequest, pagination *pasturePb.PaginationModel) (*pasturePb.EstrusItemsResponse, error) {
|
|
|
- currentUser, err := s.GetCurrentSystemUser(ctx)
|
|
|
- if err != nil {
|
|
|
- return nil, xerr.Custom("用户信息错误,请退出重新登录")
|
|
|
- }
|
|
|
-
|
|
|
- estrusList := make([]*model.EventEstrus, 0)
|
|
|
- var count int64 = 0
|
|
|
- pref := s.DB.Table(new(model.EventEstrus).TableName()).
|
|
|
- Where("is_show = ?", pasturePb.IsShow_Ok).
|
|
|
- Where("pasture_id = ?", currentUser.PastureId)
|
|
|
- if len(req.CowIds) > 0 {
|
|
|
- cowIds := strings.Split(util.ArrayInt32ToStrings(req.CowIds, ","), ",")
|
|
|
- pref.Where("cow_id IN ?", cowIds)
|
|
|
- }
|
|
|
-
|
|
|
- if req.Level > 0 {
|
|
|
- pref.Where("level = ?", req.Level)
|
|
|
- }
|
|
|
-
|
|
|
- if len(req.PenIds) > 0 {
|
|
|
- penIds := strings.Split(util.ArrayInt32ToStrings(req.PenIds, ","), ",")
|
|
|
- pref.Where("pen_id IN ?", penIds)
|
|
|
- }
|
|
|
-
|
|
|
- if err = pref.Order("id desc").
|
|
|
- Count(&count).Limit(int(pagination.PageSize)).
|
|
|
- Offset(int(pagination.PageOffset)).
|
|
|
- Find(&estrusList).Error; err != nil {
|
|
|
- return nil, xerr.WithStack(err)
|
|
|
- }
|
|
|
-
|
|
|
- getCowInfoByCowId := GetCowInfoByCowId
|
|
|
- getCowLastEvent := GetCowLastEvent
|
|
|
- return &pasturePb.EstrusItemsResponse{
|
|
|
- Code: http.StatusOK,
|
|
|
- Message: "ok",
|
|
|
- Data: &pasturePb.EstrusItemsData{
|
|
|
- List: model.EstrusSlice(estrusList).ToPB(s.DB, getCowInfoByCowId, getCowLastEvent),
|
|
|
- Total: int32(count),
|
|
|
- PageSize: pagination.PageSize,
|
|
|
- Page: pagination.Page,
|
|
|
- },
|
|
|
- }, nil
|
|
|
-}
|
|
|
-
|
|
|
-func (s *StoreEntry) EstrusBatch(ctx context.Context, req *pasturePb.EventEstrus) (err error) {
|
|
|
- currentUser, err := s.GetCurrentSystemUser(ctx)
|
|
|
- if err != nil {
|
|
|
- return xerr.Custom("当前用户信息错误,请退出重新登录")
|
|
|
- }
|
|
|
- operationUser, err := s.GetSystemUserById(ctx, currentUser.PastureId, int64(req.OperationId))
|
|
|
- if err != nil {
|
|
|
- return xerr.Customf("该用户不存在: %d", req.OperationId)
|
|
|
- }
|
|
|
- req.OperationName = operationUser.Name
|
|
|
-
|
|
|
- eventEstrusIds := make([]string, 0)
|
|
|
- eventMatingList := make([]*model.EventMating, 0)
|
|
|
- for _, cowId := range req.CowIds {
|
|
|
- cowInfo := GetCowInfoByCowId(s.DB, int64(cowId))
|
|
|
- if cowInfo == nil {
|
|
|
- return xerr.Custom("牛只信息不存在")
|
|
|
- }
|
|
|
-
|
|
|
- newEventMating := model.NewEventMating(currentUser.PastureId, cowInfo, time.Now().Unix(), pasturePb.ExposeEstrusType_Neck_Ring)
|
|
|
- eventEstrus, ok := s.EventEstrusIsExist(ctx, currentUser.PastureId, int64(cowId))
|
|
|
- if ok {
|
|
|
- newEventMating.EventEstrusId = eventEstrus.Id
|
|
|
- newEventMating.MatingTimes = cowInfo.MatingTimes + 1
|
|
|
- newEventMating.Status = pasturePb.IsShow_Ok
|
|
|
- newEventMating.RealityDay = int64(req.MatingAt)
|
|
|
- newEventMating.OperationId = operationUser.Id
|
|
|
- newEventMating.OperationName = operationUser.Name
|
|
|
- newEventMating.FrozenSemenNumber = req.BullNumber
|
|
|
- newEventMating.Remarks = req.Remarks
|
|
|
- newEventMating.MessageId = currentUser.Id
|
|
|
- newEventMating.MessageName = currentUser.Name
|
|
|
- eventEstrusIds = append(eventEstrusIds, strconv.FormatInt(eventEstrus.Id, 10))
|
|
|
- }
|
|
|
- eventMatingList = append(eventMatingList, newEventMating)
|
|
|
- }
|
|
|
- if len(eventMatingList) <= 0 {
|
|
|
- return nil
|
|
|
- }
|
|
|
- defer func() {
|
|
|
- // 记录牛只事件日志
|
|
|
- if err == nil {
|
|
|
- for _, v := range eventMatingList {
|
|
|
- cow, _ := s.GetCowInfoByCowId(ctx, currentUser.PastureId, v.CowId)
|
|
|
- cowLogs := s.SubmitEventLog(ctx, currentUser, cow, pasturePb.EventType_Mating, pasturePb.ExposeEstrusType_Neck_Ring, req)
|
|
|
- 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,
|
|
|
- "result": pasturePb.EstrusResult_Correct,
|
|
|
- }).Error; err != nil {
|
|
|
- return xerr.WithStack(err)
|
|
|
- }
|
|
|
- }
|
|
|
-
|
|
|
- if len(eventMatingList) > 0 {
|
|
|
- if err = tx.Create(eventMatingList).Error; err != nil {
|
|
|
- return xerr.WithStack(err)
|
|
|
- }
|
|
|
- }
|
|
|
- return nil
|
|
|
- }); err != nil {
|
|
|
- return xerr.WithStack(err)
|
|
|
- }
|
|
|
- return nil
|
|
|
-}
|
|
|
-
|
|
|
func (s *StoreEntry) SameTimeCreate(ctx context.Context, req *pasturePb.EventSameTime) (err error) {
|
|
|
currentUser, err := s.GetCurrentSystemUser(ctx)
|
|
|
if err != nil {
|
|
@@ -294,7 +176,7 @@ func (s *StoreEntry) SameTimeCreate(ctx context.Context, req *pasturePb.EventSam
|
|
|
return xerr.Customf("该药品不存在: %d", req.DrugsId)
|
|
|
}
|
|
|
req.DrugsName = drugs.Name
|
|
|
- eventCowSameTime.Update(drugs, req.Usage, req.Remarks, operationUser, currentUser)
|
|
|
+ eventCowSameTime.EventUpdate(drugs, req.Usage, req.Remarks, operationUser, currentUser)
|
|
|
|
|
|
defer func() {
|
|
|
if err == nil {
|
|
@@ -306,7 +188,7 @@ func (s *StoreEntry) SameTimeCreate(ctx context.Context, req *pasturePb.EventSam
|
|
|
}()
|
|
|
|
|
|
if err = s.DB.Model(new(model.EventCowSameTime)).
|
|
|
- Select("status,drugs_id,unit,usage,remarks,operation_id,operation_name").
|
|
|
+ Select("status", "drugs_id", "unit", "usage", "remarks", "operation_id", "operation_name").
|
|
|
Where("id = ?", eventCowSameTime.Id).
|
|
|
Updates(eventCowSameTime).Error; err != nil {
|
|
|
return xerr.WithStack(err)
|
|
@@ -375,9 +257,9 @@ func (s *StoreEntry) SameTimeBatch(ctx context.Context, req *pasturePb.EventSame
|
|
|
if err = s.DB.Transaction(func(tx *gorm.DB) error {
|
|
|
for _, v := range eventCowSameTimeList {
|
|
|
// 更新SameTime
|
|
|
- v.Update(drugs, req.Usage, req.Remarks, currentUser, operationUser)
|
|
|
+ v.EventUpdate(drugs, req.Usage, req.Remarks, currentUser, operationUser)
|
|
|
if err = tx.Model(new(model.EventCowSameTime)).
|
|
|
- Select("status,drugs_id,unit,usage,remarks,operation_id,operation_name").
|
|
|
+ Select("status", "drugs_id", "unit", "usage", "remarks", "operation_id", "operation_name").
|
|
|
Where("id = ?", v.Id).
|
|
|
Updates(v).Error; err != nil {
|
|
|
return xerr.WithStack(err)
|
|
@@ -428,36 +310,45 @@ func (s *StoreEntry) SameTimeList(
|
|
|
}, nil
|
|
|
}
|
|
|
|
|
|
-func (s *StoreEntry) MatingList(ctx context.Context, req *pasturePb.SearchEventRequest, pagination *pasturePb.PaginationModel) (*pasturePb.MatingEventResponse, error) {
|
|
|
+func (s *StoreEntry) EstrusList(ctx context.Context, req *pasturePb.EstrusItemsRequest, pagination *pasturePb.PaginationModel) (*pasturePb.EstrusItemsResponse, error) {
|
|
|
currentUser, err := s.GetCurrentSystemUser(ctx)
|
|
|
if err != nil {
|
|
|
- return nil, xerr.Custom("当前用户信息错误,请退出重新登录")
|
|
|
+ return nil, xerr.Custom("用户信息错误,请退出重新登录")
|
|
|
}
|
|
|
|
|
|
- matingList := make([]*model.EventMating, 0)
|
|
|
+ estrusList := make([]*model.EventEstrus, 0)
|
|
|
var count int64 = 0
|
|
|
- pref := s.DB.Model(new(model.EventMating)).
|
|
|
- Where("mating_result > ?", pasturePb.MatingResult_Invalid).
|
|
|
- Where("status = ?", pasturePb.IsShow_No).
|
|
|
+ pref := s.DB.Table(new(model.EventEstrus).TableName()).
|
|
|
+ Where("is_show = ?", pasturePb.IsShow_Ok).
|
|
|
Where("pasture_id = ?", currentUser.PastureId)
|
|
|
- if len(req.CowId) > 0 {
|
|
|
- cowIds := strings.Split(req.CowId, ",")
|
|
|
+ if len(req.CowIds) > 0 {
|
|
|
+ cowIds := strings.Split(util.ArrayInt32ToStrings(req.CowIds, ","), ",")
|
|
|
pref.Where("cow_id IN ?", cowIds)
|
|
|
}
|
|
|
|
|
|
+ if req.Level > 0 {
|
|
|
+ pref.Where("level = ?", req.Level)
|
|
|
+ }
|
|
|
+
|
|
|
+ if len(req.PenIds) > 0 {
|
|
|
+ penIds := strings.Split(util.ArrayInt32ToStrings(req.PenIds, ","), ",")
|
|
|
+ pref.Where("pen_id IN ?", penIds)
|
|
|
+ }
|
|
|
+
|
|
|
if err = pref.Order("id desc").
|
|
|
Count(&count).Limit(int(pagination.PageSize)).
|
|
|
Offset(int(pagination.PageOffset)).
|
|
|
- Find(&matingList).Error; err != nil {
|
|
|
+ Find(&estrusList).Error; err != nil {
|
|
|
return nil, xerr.WithStack(err)
|
|
|
}
|
|
|
|
|
|
- exposeEstrusTypeMap := s.ExposeEstrusTypeMap()
|
|
|
- return &pasturePb.MatingEventResponse{
|
|
|
+ getCowInfoByCowId := GetCowInfoByCowId
|
|
|
+ getCowLastEvent := GetCowLastEvent
|
|
|
+ return &pasturePb.EstrusItemsResponse{
|
|
|
Code: http.StatusOK,
|
|
|
Message: "ok",
|
|
|
- Data: &pasturePb.SearchMatingData{
|
|
|
- List: model.EventMatingSlice(matingList).ToPB(exposeEstrusTypeMap),
|
|
|
+ Data: &pasturePb.EstrusItemsData{
|
|
|
+ List: model.EstrusSlice(estrusList).ToPB(s.DB, getCowInfoByCowId, getCowLastEvent),
|
|
|
Total: int32(count),
|
|
|
PageSize: pagination.PageSize,
|
|
|
Page: pagination.Page,
|
|
@@ -465,190 +356,218 @@ func (s *StoreEntry) MatingList(ctx context.Context, req *pasturePb.SearchEventR
|
|
|
}, nil
|
|
|
}
|
|
|
|
|
|
-// MatingCreate 牛只配种
|
|
|
-func (s *StoreEntry) MatingCreate(ctx context.Context, req *pasturePb.EventMating) (err error) {
|
|
|
- eventCheckModel, err := s.MatingCreateCheck(ctx, req)
|
|
|
+func (s *StoreEntry) EstrusBatchMating(ctx context.Context, req *pasturePb.EventEstrus) (err error) {
|
|
|
+ currentUser, err := s.GetCurrentSystemUser(ctx)
|
|
|
if err != nil {
|
|
|
- return xerr.WithStack(err)
|
|
|
- }
|
|
|
-
|
|
|
- frozenSemen := &model.FrozenSemen{}
|
|
|
- if err = s.DB.Where("bull_id = ?", req.FrozenSemenNumber).
|
|
|
- First(frozenSemen).Error; err != nil {
|
|
|
- return xerr.Custom("未找到冻精信息")
|
|
|
+ return xerr.Custom("当前用户信息错误,请退出重新登录")
|
|
|
}
|
|
|
-
|
|
|
- if frozenSemen.Quantity < req.FrozenSemenCount {
|
|
|
- return xerr.Custom("冻精数量不足")
|
|
|
+ operationUser, err := s.GetSystemUserById(ctx, currentUser.PastureId, int64(req.OperationId))
|
|
|
+ if err != nil {
|
|
|
+ return xerr.Customf("该用户不存在: %d", req.OperationId)
|
|
|
}
|
|
|
+ req.OperationName = operationUser.Name
|
|
|
|
|
|
- // 更新复配的牛只
|
|
|
- matingReMatchIds := make([]int64, 0)
|
|
|
- // 新建配种信息的牛只
|
|
|
- newMatingList := make([]*model.EventMating, 0)
|
|
|
- // 更新配种信息牛只
|
|
|
- updateMatingList := make([]int64, 0)
|
|
|
- // 所有牛只
|
|
|
- cowIds := make([]int64, 0)
|
|
|
- // 需要更新配次的牛只
|
|
|
- matingTimes := make([]*MatingTimes, 0)
|
|
|
- // 需要更新空怀的牛只
|
|
|
- emptyCowIds := make([]int64, 0)
|
|
|
-
|
|
|
- for _, cow := range eventCheckModel.CowList {
|
|
|
- // 1. 第一次配种,创建配种信息(自然发情牛只,未经过同期的牛只)
|
|
|
- if cow.LastMatingAt <= 0 {
|
|
|
- newMatingList = append(newMatingList, model.NewEventMating2(cow, req, eventCheckModel.CurrentUser))
|
|
|
- continue
|
|
|
+ eventEstrusIds := make([]string, 0)
|
|
|
+ eventMatingList := make([]*model.EventMating, 0)
|
|
|
+ cowIds := make([]int32, 0)
|
|
|
+ for _, cowId := range req.CowIds {
|
|
|
+ cowInfo := GetCowInfoByCowId(s.DB, int64(cowId))
|
|
|
+ if cowInfo == nil {
|
|
|
+ return xerr.Custom("牛只信息不存在")
|
|
|
}
|
|
|
|
|
|
- eventMatingHistory, err := s.GetEventMatingIsExIstByCowId(ctx, cow)
|
|
|
+ newEventMating := model.NewEventMating(currentUser.PastureId, cowInfo, time.Now().Unix(), pasturePb.ExposeEstrusType_Neck_Ring)
|
|
|
+ eventEstrus, ok, err := s.FindEventEstrusByCowId(ctx, currentUser.PastureId, int64(cowId))
|
|
|
if err != nil {
|
|
|
return xerr.WithStack(err)
|
|
|
}
|
|
|
|
|
|
- // 2. 同期更新配种信息 牛号&& 胎次 && 未配 && 配种结果未知 ==> 同期初配
|
|
|
- if eventMatingHistory.Status == pasturePb.IsShow_No &&
|
|
|
- eventMatingHistory.MatingResult == pasturePb.MatingResult_Unknown {
|
|
|
- updateMatingList = append(updateMatingList, eventMatingHistory.Id)
|
|
|
+ if !ok {
|
|
|
continue
|
|
|
}
|
|
|
|
|
|
- lastMatingAt := time.Unix(cow.LastMatingAt, 0)
|
|
|
- currentMatingAt := time.Unix(int64(req.MatingAt), 0)
|
|
|
- daysBetween := util.DaysBetween(currentMatingAt.Unix(), lastMatingAt.Unix())
|
|
|
-
|
|
|
- // 3. 如何两次配种时间为连续两天之内&&有过一次配种记录,则更新为复配状态 牛号&& 胎次 && 已配 && 配种结果未知 ==> 复配
|
|
|
- if (currentMatingAt.Format(model.LayoutDate2) == lastMatingAt.Format(model.LayoutDate2) || daysBetween == 1) &&
|
|
|
- eventMatingHistory.Status == pasturePb.IsShow_Ok &&
|
|
|
- eventMatingHistory.MatingResult == pasturePb.MatingResult_Unknown {
|
|
|
- matingReMatchIds = append(matingReMatchIds, eventMatingHistory.Id)
|
|
|
- } else {
|
|
|
- matingTimes = append(matingTimes, &MatingTimes{
|
|
|
- Mt: cow.MatingTimes + 1,
|
|
|
- CowId: cow.Id,
|
|
|
- EventMatingId: eventMatingHistory.Id,
|
|
|
- })
|
|
|
- }
|
|
|
-
|
|
|
- // 4. 提交的配种数据中,如何定位出空怀的牛只,
|
|
|
- if (eventMatingHistory.MatingResult == pasturePb.MatingResult_Unknown || eventMatingHistory.MatingResult == pasturePb.MatingResult_ReMatch) &&
|
|
|
- daysBetween >= 2 {
|
|
|
- emptyCowIds = append(emptyCowIds, eventMatingHistory.Id)
|
|
|
- newMatingList = append(newMatingList, model.NewEventMating2(cow, req, eventCheckModel.CurrentUser))
|
|
|
- }
|
|
|
- cowIds = append(cowIds, cow.Id)
|
|
|
+ newEventMating.EventEstrusId = eventEstrus.Id
|
|
|
+ eventEstrusIds = append(eventEstrusIds, strconv.FormatInt(eventEstrus.Id, 10))
|
|
|
+ eventMatingList = append(eventMatingList, newEventMating)
|
|
|
+ cowIds = append(cowIds, cowId)
|
|
|
}
|
|
|
-
|
|
|
- if len(cowIds) != len(updateMatingList)+len(matingReMatchIds)+len(newMatingList) {
|
|
|
- zaplog.Error("MatingCreate",
|
|
|
- zap.Any("cowIds", cowIds),
|
|
|
- zap.Any("updateMatingList", updateMatingList),
|
|
|
- zap.Any("newMatingList", newMatingList),
|
|
|
- zap.Any("matingReMatchIds", matingReMatchIds),
|
|
|
- zap.Any("req", req),
|
|
|
- )
|
|
|
- return xerr.Custom("配种信息有误")
|
|
|
+ if len(eventMatingList) <= 0 {
|
|
|
+ return nil
|
|
|
}
|
|
|
- defer func() {
|
|
|
- if err != nil {
|
|
|
- // 记录事件日志
|
|
|
- for _, cowId := range cowIds {
|
|
|
- cow, _ := s.GetCowInfoByCowId(ctx, eventCheckModel.CurrentUser.PastureId, cowId)
|
|
|
- cowLogs := s.SubmitEventLog(ctx, eventCheckModel.CurrentUser, cow, pasturePb.EventType_Mating, req.ExposeEstrusType, req)
|
|
|
- s.DB.Table(cowLogs.TableName()).Create(cowLogs)
|
|
|
- }
|
|
|
- }
|
|
|
- }()
|
|
|
|
|
|
- itemFrozenSemenLog := model.NewEventFrozenSemenLog(req)
|
|
|
if err = s.DB.Transaction(func(tx *gorm.DB) error {
|
|
|
- // 更新配种事件数据(初配)
|
|
|
- if len(updateMatingList) > 0 {
|
|
|
- if err = tx.Model(new(model.EventMating)).
|
|
|
- Where("id IN ?", updateMatingList).
|
|
|
- Where("status = ?", pasturePb.IsShow_No).
|
|
|
- Updates(
|
|
|
- model.EventMatingUpdateMap(int64(req.MatingAt), req.FrozenSemenNumber, eventCheckModel.OperationUser, eventCheckModel.CurrentUser),
|
|
|
- ).Error; err != nil {
|
|
|
+ if len(eventEstrusIds) > 0 {
|
|
|
+ if err = tx.Model(new(model.EventEstrus)).
|
|
|
+ Where("id IN ?", eventEstrusIds).
|
|
|
+ Updates(map[string]interface{}{
|
|
|
+ "is_show": pasturePb.IsShow_No,
|
|
|
+ "result": pasturePb.EstrusResult_Correct,
|
|
|
+ }).Error; err != nil {
|
|
|
return xerr.WithStack(err)
|
|
|
}
|
|
|
}
|
|
|
|
|
|
- // 更新已配种的牛只为复配状态
|
|
|
- if len(matingReMatchIds) > 0 {
|
|
|
- if err = tx.Model(new(model.EventMating)).
|
|
|
- Where("id IN ?", matingReMatchIds).
|
|
|
- Where("mating_result = ?", pasturePb.MatingResult_Unknown).
|
|
|
- Where("status = ?", pasturePb.IsShow_Ok).
|
|
|
- Update("mating_result", pasturePb.MatingResult_ReMatch).
|
|
|
- Error; err != nil {
|
|
|
+ if len(eventMatingList) > 0 {
|
|
|
+ if err = tx.Create(eventMatingList).Error; err != nil {
|
|
|
return xerr.WithStack(err)
|
|
|
}
|
|
|
}
|
|
|
+ return nil
|
|
|
+ }); err != nil {
|
|
|
+ return xerr.WithStack(err)
|
|
|
+ }
|
|
|
+
|
|
|
+ // 配种信息
|
|
|
+ eventMating := &pasturePb.EventMating{
|
|
|
+ CowIds: cowIds,
|
|
|
+ FrozenSemenNumber: req.BullNumber,
|
|
|
+ FrozenSemenCount: req.Consumption,
|
|
|
+ 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
|
|
|
+}
|
|
|
|
|
|
- // 更新上次已配种的牛只为空怀
|
|
|
- if len(emptyCowIds) > 0 {
|
|
|
- if err = tx.Model(new(model.EventMating)).
|
|
|
- Where("id IN ?", emptyCowIds).
|
|
|
- Update("mating_result", pasturePb.BreedStatus_Empty).
|
|
|
- Error; err != nil {
|
|
|
+func (s *StoreEntry) MatingList(ctx context.Context, req *pasturePb.SearchEventRequest, pagination *pasturePb.PaginationModel) (*pasturePb.MatingEventResponse, error) {
|
|
|
+ currentUser, err := s.GetCurrentSystemUser(ctx)
|
|
|
+ if err != nil {
|
|
|
+ return nil, xerr.Custom("当前用户信息错误,请退出重新登录")
|
|
|
+ }
|
|
|
+
|
|
|
+ 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_No).
|
|
|
+ Where("pasture_id = ?", currentUser.PastureId)
|
|
|
+ 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.MatingEventResponse{
|
|
|
+ Code: http.StatusOK,
|
|
|
+ Message: "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) {
|
|
|
+ eventCheckModel, err := s.MatingCreateCheck(ctx, req)
|
|
|
+ if err != nil {
|
|
|
+ return xerr.WithStack(err)
|
|
|
+ }
|
|
|
+
|
|
|
+ if err = s.DB.Transaction(func(tx *gorm.DB) error {
|
|
|
+ for _, cow := range eventCheckModel.CowList {
|
|
|
+ lastEventMating, ok, err := s.FindLastEventMatingByCowId(ctx, eventCheckModel.CurrentUser.PastureId, cow.Id)
|
|
|
+ if err != nil {
|
|
|
return xerr.WithStack(err)
|
|
|
}
|
|
|
- }
|
|
|
|
|
|
- // 创建配种事件数据
|
|
|
- if len(newMatingList) > 0 {
|
|
|
- if err = tx.Create(newMatingList).Error; err != nil {
|
|
|
+ var (
|
|
|
+ newMating *model.EventMating
|
|
|
+ isReMating = false
|
|
|
+ isEmptyMating = false
|
|
|
+ )
|
|
|
+
|
|
|
+ // 1. 自然发情牛只,未经过同期的牛只和未佩戴脖环的牛只
|
|
|
+ if !ok {
|
|
|
+ newMating = model.NewEventMatingNaturalEstrus(cow, req, eventCheckModel.CurrentUser)
|
|
|
+ if err = tx.Create(newMating).Error; err != nil {
|
|
|
+ return xerr.WithStack(err)
|
|
|
+ }
|
|
|
+ } else {
|
|
|
+ // 2. 所有有配种数据的牛只
|
|
|
+ if lastEventMating == nil || lastEventMating.Id <= 0 {
|
|
|
+ zaplog.Error("MatingCreate", zap.Any("cow", cow), zap.Any("CurrentUser", eventCheckModel.CurrentUser))
|
|
|
+ continue
|
|
|
+ }
|
|
|
+
|
|
|
+ // 复配 => 本胎次配次不加1
|
|
|
+ isReMating = lastEventMating.IsReMating(cow, int64(req.MatingAt))
|
|
|
+ // 空怀
|
|
|
+ isEmptyMating = lastEventMating.IsEmptyMating(cow, int64(req.MatingAt))
|
|
|
+
|
|
|
+ // 2。1. 提交的配种数据中,定位出空怀的牛只,
|
|
|
+ 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(eventCheckModel.CurrentUser.PastureId, cow, int64(req.MatingAt), req.ExposeEstrusType)
|
|
|
+ newMating.EventUpdate(int64(req.MatingAt), req.FrozenSemenNumber, isReMating, eventCheckModel.OperationUser, eventCheckModel.CurrentUser)
|
|
|
+ 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))
|
|
|
+ }
|
|
|
+ }
|
|
|
+ // 2.2. 同期初配
|
|
|
+ if !isEmptyMating && !isReMating {
|
|
|
+ lastEventMating.EventUpdate(int64(req.MatingAt), req.FrozenSemenNumber, isReMating, eventCheckModel.OperationUser, eventCheckModel.CurrentUser)
|
|
|
+ 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)
|
|
|
+ }
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+ // 牛只基本中配种信息更新
|
|
|
+ cow.EventMatingUpdate(int64(req.MatingAt), req.FrozenSemenNumber, isReMating)
|
|
|
+ if err = tx.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, eventCheckModel.CurrentUser, cow, pasturePb.EventType_Mating, req.ExposeEstrusType, req)
|
|
|
+ tx.Table(cowLogs.TableName()).Create(cowLogs)
|
|
|
}
|
|
|
|
|
|
// 创建冻精使用记录日志
|
|
|
+ itemFrozenSemenLog := model.NewEventFrozenSemenLog(req)
|
|
|
if err = tx.Create(itemFrozenSemenLog).Error; err != nil {
|
|
|
return xerr.WithStack(err)
|
|
|
}
|
|
|
|
|
|
- // 更新牛只的繁殖状态为配种
|
|
|
- if err = tx.Model(new(model.Cow)).
|
|
|
- Where("id IN ?", cowIds).
|
|
|
- Where("admission_status = ?", pasturePb.AdmissionStatus_Admission).
|
|
|
- Updates(map[string]interface{}{
|
|
|
- "breed_status": pasturePb.BreedStatus_Breeding,
|
|
|
- "last_bull_number": req.FrozenSemenNumber,
|
|
|
- "last_mating_at": int64(req.MatingAt),
|
|
|
- }).Error; err != nil {
|
|
|
- return xerr.WithStack(err)
|
|
|
- }
|
|
|
-
|
|
|
// 减去精液的数量
|
|
|
- if err = tx.Model(frozenSemen).
|
|
|
- Where("id = ?", frozenSemen.Id).
|
|
|
- Where("quantity > 0").
|
|
|
- UpdateColumn("quantity",
|
|
|
- gorm.Expr("quantity - ?", req.FrozenSemenCount),
|
|
|
- ).Error; err != nil {
|
|
|
+ eventCheckModel.FrozenSemen.EventQuantityUpdate(req.FrozenSemenCount)
|
|
|
+ if err = tx.Model(eventCheckModel.FrozenSemen).
|
|
|
+ Select("quantity").
|
|
|
+ Where("id = ?", eventCheckModel.FrozenSemen.Id).
|
|
|
+ Updates(eventCheckModel.FrozenSemen).Error; err != nil {
|
|
|
return xerr.WithStack(err)
|
|
|
}
|
|
|
-
|
|
|
- // 更新配次
|
|
|
- if len(matingTimes) > 0 {
|
|
|
- for _, mt := range matingTimes {
|
|
|
- if err = tx.Model(new(model.EventMating)).
|
|
|
- Where("id = ?", mt.EventMatingId).
|
|
|
- Update("mating_times", mt.Mt).Error; err != nil {
|
|
|
- return xerr.WithStack(err)
|
|
|
- }
|
|
|
-
|
|
|
- if err = tx.Model(new(model.Cow)).
|
|
|
- Where("id = ?", mt.CowId).
|
|
|
- Where("admission_status = ?", pasturePb.AdmissionStatus_Admission).
|
|
|
- Where("breed_status = ?", pasturePb.BreedStatus_Breeding).
|
|
|
- Update("mating_times", mt.Mt).Error; err != nil {
|
|
|
- return xerr.WithStack(err)
|
|
|
- }
|
|
|
- }
|
|
|
- }
|
|
|
return nil
|
|
|
}); err != nil {
|
|
|
return xerr.WithStack(err)
|
|
@@ -701,7 +620,7 @@ func (s *StoreEntry) WeaningBatch(ctx context.Context, req *pasturePb.EventWeani
|
|
|
for _, v := range eventWeaningList {
|
|
|
v.EventUpdate(int64(req.WeaningAt), req.Remarks, req.PenId, operation, currentUser)
|
|
|
if err = tx.Model(new(model.EventWeaning)).
|
|
|
- Select("status,reality_day,operation_id,operation_name,message_id,message_name,remarks,after_pen_id").
|
|
|
+ Select("status", "reality_day", "operation_id", "operation_name", "message_id", "message_name", "remarks", "after_pen_id").
|
|
|
Where("id = ?", v.Id).
|
|
|
Updates(v).Error; err != nil {
|
|
|
return xerr.WithStack(err)
|
|
@@ -712,7 +631,7 @@ func (s *StoreEntry) WeaningBatch(ctx context.Context, req *pasturePb.EventWeani
|
|
|
}
|
|
|
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").
|
|
|
+ Select("pen_id", "current_weight", "weaning_at", "last_weight_at").
|
|
|
Where("id = ?", v.CowId).
|
|
|
Updates(cowInfo).Error; err != nil {
|
|
|
return xerr.WithStack(err)
|