|
@@ -2,7 +2,6 @@ package backend
|
|
|
|
|
|
import (
|
|
|
"context"
|
|
|
- "encoding/json"
|
|
|
"fmt"
|
|
|
"kpt-pasture/model"
|
|
|
"kpt-pasture/util"
|
|
@@ -131,10 +130,10 @@ func (s *StoreEntry) CalvingCreate(ctx context.Context, req *pasturePb.EventCalv
|
|
|
return nil
|
|
|
}
|
|
|
|
|
|
-func (s *StoreEntry) PregnantCheckList(ctx context.Context, req *pasturePb.SearchEventRequest, pagination *pasturePb.PaginationModel) (*pasturePb.PregnantCheckEventResponse, error) {
|
|
|
- pregnantCheckList := make([]*model.EventPregnantCheck, 0)
|
|
|
+func (s *StoreEntry) EstrusList(ctx context.Context, req *pasturePb.SearchEventRequest, pagination *pasturePb.PaginationModel) (*pasturePb.EstrusEventResponse, error) {
|
|
|
+ estrusList := make([]*model.EventEstrus, 0)
|
|
|
var count int64 = 0
|
|
|
- pref := s.DB.Table(new(model.EventPregnantCheck).TableName())
|
|
|
+ pref := s.DB.Table(new(model.EventEstrus).TableName())
|
|
|
if len(req.CowId) > 0 {
|
|
|
cowIds := strings.Split(req.CowId, ",")
|
|
|
pref.Where("cow_id IN ?", cowIds)
|
|
@@ -143,17 +142,15 @@ func (s *StoreEntry) PregnantCheckList(ctx context.Context, req *pasturePb.Searc
|
|
|
if err := pref.Order("id desc").
|
|
|
Count(&count).Limit(int(pagination.PageSize)).
|
|
|
Offset(int(pagination.PageOffset)).
|
|
|
- Find(&pregnantCheckList).Error; err != nil {
|
|
|
+ Find(&estrusList).Error; err != nil {
|
|
|
return nil, xerr.WithStack(err)
|
|
|
}
|
|
|
|
|
|
- pregnantCheckResultMap := s.PregnantCheckResultMap()
|
|
|
- pregnantCheckMethodMap := s.PregnantCheckMethodMap()
|
|
|
- return &pasturePb.PregnantCheckEventResponse{
|
|
|
+ return &pasturePb.EstrusEventResponse{
|
|
|
Code: http.StatusOK,
|
|
|
Message: "ok",
|
|
|
- Data: &pasturePb.SearchPregnantCheckData{
|
|
|
- List: model.EventPregnantCheckSlice(pregnantCheckList).ToPB(pregnantCheckResultMap, pregnantCheckMethodMap),
|
|
|
+ Data: &pasturePb.SearchEstrusData{
|
|
|
+ List: model.EstrusSlice(estrusList).ToPB(),
|
|
|
Total: int32(count),
|
|
|
PageSize: pagination.PageSize,
|
|
|
Page: pagination.Page,
|
|
@@ -161,131 +158,115 @@ func (s *StoreEntry) PregnantCheckList(ctx context.Context, req *pasturePb.Searc
|
|
|
}, nil
|
|
|
}
|
|
|
|
|
|
-func (s *StoreEntry) PregnantCheckCreate(ctx context.Context, req *pasturePb.EventPregnantCheck) error {
|
|
|
- eventCheckModel, err := s.PregnantCheckCreateCheck(ctx, req)
|
|
|
+func (s *StoreEntry) EstrusCreate(ctx context.Context, req *pasturePb.EventEstrus) error {
|
|
|
+ if len(req.CowId) <= 0 {
|
|
|
+ return xerr.Custom("请选择相关牛只")
|
|
|
+ }
|
|
|
+ cowList, err := s.ParseCowIds(ctx, req.CowId)
|
|
|
if err != nil {
|
|
|
return xerr.WithStack(err)
|
|
|
}
|
|
|
+ estrusList := make([]*model.EventEstrus, 0)
|
|
|
+ currentUser, err := s.GetCurrentSystemUser(ctx)
|
|
|
+ if err != nil {
|
|
|
+ return xerr.Custom("获取当前登录用户失败")
|
|
|
+ }
|
|
|
|
|
|
- // 更新怀孕牛只
|
|
|
- updatePregnantCowIds := make([]int64, 0)
|
|
|
- // 更新流产牛只
|
|
|
- updateAbortCowIds := make([]int64, 0)
|
|
|
- // 更新空怀牛只
|
|
|
- updateEmptyCowIds := make([]int64, 0)
|
|
|
-
|
|
|
- cowIds := make([]int64, 0)
|
|
|
- for _, cow := range eventCheckModel.CowList {
|
|
|
- itemEventPregnantCheck := &model.EventPregnantCheck{}
|
|
|
- if err = s.DB.Model(new(model.EventPregnantCheck)).
|
|
|
- Where("lact = ?", cow.Lact).
|
|
|
- Where("cow_id = ?", cow.Id).
|
|
|
- Order("id desc").
|
|
|
- First(itemEventPregnantCheck).Error; err != nil {
|
|
|
+ for _, cow := range cowList {
|
|
|
+ operationUser, err := s.GetSystemUserById(ctx, int64(req.OperationId))
|
|
|
+ if err != nil {
|
|
|
return xerr.WithStack(err)
|
|
|
}
|
|
|
+ req.OperationName = operationUser.Name
|
|
|
+ estrusList = append(estrusList, model.NewEventEstrus(cow, currentUser, req))
|
|
|
+ }
|
|
|
|
|
|
- if req.PregnantCheckResult == pasturePb.PregnantCheckResult_Pregnant {
|
|
|
- updatePregnantCowIds = append(updatePregnantCowIds, cow.Id)
|
|
|
- }
|
|
|
+ if err = s.DB.Create(estrusList).Error; err != nil {
|
|
|
+ return xerr.WithStack(err)
|
|
|
+ }
|
|
|
+ return nil
|
|
|
+}
|
|
|
|
|
|
- if req.PregnantCheckResult == pasturePb.PregnantCheckResult_UnPregnant {
|
|
|
- if itemEventPregnantCheck.PregnantCheckName == model.PregnantCheckForFirst {
|
|
|
- updateAbortCowIds = append(updateAbortCowIds, cow.Id)
|
|
|
- }
|
|
|
- if itemEventPregnantCheck.PregnantCheckName == model.PregnantCheckForSecond {
|
|
|
- updateEmptyCowIds = append(updateEmptyCowIds, cow.Id)
|
|
|
- }
|
|
|
- }
|
|
|
- cowIds = append(cowIds, cow.Id)
|
|
|
+func (s *StoreEntry) SameTimeCreate(ctx context.Context, req *pasturePb.EventSameTime) error {
|
|
|
+ if _, err := s.GetSameTimeById(ctx, int64(req.SameTimeId)); err != nil {
|
|
|
+ return xerr.WithStack(err)
|
|
|
}
|
|
|
|
|
|
- if err = s.DB.Transaction(func(tx *gorm.DB) error {
|
|
|
- // 更新孕检事件表
|
|
|
- if err = tx.Model(new(model.EventPregnantCheck)).
|
|
|
- Where("id IN ?", cowIds).
|
|
|
- Updates(map[string]interface{}{
|
|
|
- "reality_day": req.PregnantCheckAt,
|
|
|
- "pregnant_check_result": req.PregnantCheckResult,
|
|
|
- "pregnant_check_method": req.PregnantCheckMethod,
|
|
|
- "operation_id": eventCheckModel.OperationUser.Id,
|
|
|
- "operation_name": eventCheckModel.OperationUser.Name,
|
|
|
- "message_id": eventCheckModel.CurrentUser.Id,
|
|
|
- "message_name": eventCheckModel.CurrentUser.Name,
|
|
|
- "remarks": req.Remarks,
|
|
|
- "is_pregnant": req.PregnantCheckResult,
|
|
|
- }).Error; err != nil {
|
|
|
+ drugs := &model.Drugs{}
|
|
|
+ var err error
|
|
|
+ if req.DrugsId > 0 {
|
|
|
+ if drugs, err = s.GetDrugsById(ctx, int64(req.DrugsId)); err != nil {
|
|
|
return xerr.WithStack(err)
|
|
|
}
|
|
|
- // 更新牛只信息和配种事件表怀孕状态
|
|
|
- if len(updatePregnantCowIds) > 0 {
|
|
|
- if err = tx.Model(&model.Cow{}).
|
|
|
- Where("cow_id IN ?", updatePregnantCowIds).
|
|
|
- Updates(map[string]interface{}{
|
|
|
- "breed_status": pasturePb.BreedStatus_Pregnant,
|
|
|
- "last_pregnant_check_at": req.PregnantCheckAt,
|
|
|
- "is_pregnant": pasturePb.IsShow_Ok,
|
|
|
- }).Error; err != nil {
|
|
|
- return xerr.WithStack(err)
|
|
|
- }
|
|
|
+ }
|
|
|
+ operationUser, err := s.GetSystemUserById(ctx, int64(req.OperationId))
|
|
|
+ if err != nil {
|
|
|
+ return xerr.WithStack(err)
|
|
|
+ }
|
|
|
|
|
|
- if err = tx.Model(&model.EventMating{}).Where("cow_id IN ?", updatePregnantCowIds).
|
|
|
- Group("cow_id").Select("MAX(id) as id").
|
|
|
- Updates(map[string]interface{}{
|
|
|
- "mating_result": pasturePb.MatingResult_Pregnant,
|
|
|
- "mating_result_at": req.PregnantCheckAt,
|
|
|
- }).Error; err != nil {
|
|
|
- return xerr.WithStack(err)
|
|
|
- }
|
|
|
+ if err = s.DB.Transaction(func(tx *gorm.DB) error {
|
|
|
+ if err = tx.Model(new(model.CowSameTime)).
|
|
|
+ Where("cow_id = ?", req.CowId).
|
|
|
+ Where("same_time_id = ?", req.SameTimeId).
|
|
|
+ Update("same_time_status", pasturePb.SameTimeStatus_In_Progress).Error; err != nil {
|
|
|
+ return xerr.WithStack(err)
|
|
|
}
|
|
|
- // 更新牛只信息和配种事件表流产状态
|
|
|
- if len(updateAbortCowIds) > 0 {
|
|
|
- if err = tx.Model(&model.Cow{}).Where("cow_id IN ?", updateAbortCowIds).
|
|
|
- Updates(map[string]interface{}{
|
|
|
- "breed_status": pasturePb.BreedStatus_Abort,
|
|
|
- "last_pregnant_check_at": req.PregnantCheckAt,
|
|
|
- "is_pregnant": pasturePb.IsShow_Ok,
|
|
|
- }).Error; err != nil {
|
|
|
- return xerr.WithStack(err)
|
|
|
- }
|
|
|
|
|
|
- if err = tx.Model(&model.EventMating{}).Where("cow_id IN ?", updateAbortCowIds).
|
|
|
- Group("cow_id").Select("MAX(id) as id").
|
|
|
- Updates(map[string]interface{}{
|
|
|
- "mating_result": pasturePb.MatingResult_Abort,
|
|
|
- "mating_result_at": req.PregnantCheckAt,
|
|
|
- }).Error; err != nil {
|
|
|
- return xerr.WithStack(err)
|
|
|
- }
|
|
|
+ if err = tx.Model(new(model.EventCowSameTime)).
|
|
|
+ Where("cow_id = ?", req.CowId).
|
|
|
+ Where("same_time_id = ?", req.SameTimeId).
|
|
|
+ Where("same_time_type = ?", req.SameTimeType).
|
|
|
+ Updates(map[string]interface{}{
|
|
|
+ "status": pasturePb.IsShow_Ok,
|
|
|
+ "drug_id": drugs.Id,
|
|
|
+ "unit": drugs.Unit,
|
|
|
+ "usage": req.Usage,
|
|
|
+ "remarks": req.Remarks,
|
|
|
+ "operation_id": operationUser.Id,
|
|
|
+ "operation_name": operationUser.Name,
|
|
|
+ }).Error; err != nil {
|
|
|
+ return xerr.WithStack(err)
|
|
|
}
|
|
|
- // 更新牛只信息和配种事件表空怀状态
|
|
|
- if len(updateEmptyCowIds) > 0 {
|
|
|
- if err = tx.Model(&model.Cow{}).Where("cow_id IN ?", updateEmptyCowIds).
|
|
|
- Updates(map[string]interface{}{
|
|
|
- "breed_status": pasturePb.BreedStatus_Empty,
|
|
|
- "last_pregnant_check_at": req.PregnantCheckAt,
|
|
|
- "is_pregnant": pasturePb.IsShow_Ok,
|
|
|
- }).Error; err != nil {
|
|
|
- return xerr.WithStack(err)
|
|
|
- }
|
|
|
|
|
|
- if err = tx.Model(&model.EventMating{}).Where("cow_id IN ?", updateEmptyCowIds).
|
|
|
- Group("cow_id").Select("MAX(id) as id").
|
|
|
- Updates(map[string]interface{}{
|
|
|
- "mating_result": pasturePb.MatingResult_Empty,
|
|
|
- "mating_result_at": req.PregnantCheckAt,
|
|
|
- }).Error; err != nil {
|
|
|
- return xerr.WithStack(err)
|
|
|
- }
|
|
|
- }
|
|
|
return nil
|
|
|
}); err != nil {
|
|
|
return xerr.WithStack(err)
|
|
|
}
|
|
|
-
|
|
|
return nil
|
|
|
}
|
|
|
|
|
|
+func (s *StoreEntry) SameTimeList(
|
|
|
+ ctx context.Context,
|
|
|
+ req *pasturePb.SearchEventRequest,
|
|
|
+ pagination *pasturePb.PaginationModel,
|
|
|
+) (*pasturePb.SearchSameTimeResponse, error) {
|
|
|
+ cowSameTimeList := make([]*model.EventCowSameTime, 0)
|
|
|
+ var count int64 = 0
|
|
|
+ pref := s.DB.Table(new(model.EventCowSameTime).TableName())
|
|
|
+ if req.CowId != "" {
|
|
|
+ cowIds := strings.Split(req.CowId, ",")
|
|
|
+ pref.Where("cow_id IN ?", cowIds)
|
|
|
+ }
|
|
|
+
|
|
|
+ if err := pref.Count(&count).
|
|
|
+ Limit(int(pagination.PageSize)).
|
|
|
+ Offset(int(pagination.PageOffset)).
|
|
|
+ Find(&cowSameTimeList).Error; err != nil {
|
|
|
+ return nil, xerr.WithStack(err)
|
|
|
+ }
|
|
|
+
|
|
|
+ return &pasturePb.SearchSameTimeResponse{
|
|
|
+ Code: http.StatusOK,
|
|
|
+ Message: "ok",
|
|
|
+ Data: &pasturePb.SameTimeData{
|
|
|
+ List: model.EventCowSameTimeSlice(cowSameTimeList).ToPB(),
|
|
|
+ Total: int32(count),
|
|
|
+ PageSize: pagination.PageSize,
|
|
|
+ Page: pagination.Page,
|
|
|
+ },
|
|
|
+ }, nil
|
|
|
+}
|
|
|
+
|
|
|
func (s *StoreEntry) MatingList(ctx context.Context, req *pasturePb.SearchEventRequest, pagination *pasturePb.PaginationModel) (*pasturePb.MatingEventResponse, error) {
|
|
|
matingList := make([]*model.EventMating, 0)
|
|
|
var count int64 = 0
|
|
@@ -329,6 +310,10 @@ func (s *StoreEntry) MatingCreate(ctx context.Context, req *pasturePb.EventMatin
|
|
|
return xerr.Custom("未找到冻精信息")
|
|
|
}
|
|
|
|
|
|
+ if frozenSemen.Quantity < req.FrozenSemenCount {
|
|
|
+ return xerr.Custom("冻精数量不足")
|
|
|
+ }
|
|
|
+
|
|
|
// 更新复配的牛只
|
|
|
matingReMatchIds := make([]int64, 0)
|
|
|
// 新建配种信息的牛只
|
|
@@ -338,7 +323,7 @@ func (s *StoreEntry) MatingCreate(ctx context.Context, req *pasturePb.EventMatin
|
|
|
// 所有牛只
|
|
|
cowIds := make([]int64, 0)
|
|
|
// 需要更新配次的牛只
|
|
|
- matingTimes := make([]*model.MatingTimes, 0)
|
|
|
+ matingTimes := make([]*MatingTimes, 0)
|
|
|
// 需要更新空怀的牛只
|
|
|
emptyCowIds := make([]int64, 0)
|
|
|
|
|
@@ -371,7 +356,7 @@ func (s *StoreEntry) MatingCreate(ctx context.Context, req *pasturePb.EventMatin
|
|
|
eventMatingHistory.MatingResult == pasturePb.MatingResult_Unknown {
|
|
|
matingReMatchIds = append(matingReMatchIds, eventMatingHistory.Id)
|
|
|
} else {
|
|
|
- matingTimes = append(matingTimes, &model.MatingTimes{
|
|
|
+ matingTimes = append(matingTimes, &MatingTimes{
|
|
|
Mt: cow.MatingTimes + 1,
|
|
|
CowId: cow.Id,
|
|
|
EventMatingId: eventMatingHistory.Id,
|
|
@@ -387,15 +372,14 @@ func (s *StoreEntry) MatingCreate(ctx context.Context, req *pasturePb.EventMatin
|
|
|
cowIds = append(cowIds, cow.Id)
|
|
|
}
|
|
|
|
|
|
- zaplog.Info("MatingCreate",
|
|
|
- zap.Any("cowIds", cowIds),
|
|
|
- zap.Any("updateMatingList", updateMatingList),
|
|
|
- zap.Any("newMatingList", newMatingList),
|
|
|
- zap.Any("matingReMatchIds", matingReMatchIds),
|
|
|
- zap.Any("req", req),
|
|
|
- )
|
|
|
-
|
|
|
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("配种信息有误")
|
|
|
}
|
|
|
|
|
@@ -509,244 +493,3 @@ func (s *StoreEntry) MatingCreate(ctx context.Context, req *pasturePb.EventMatin
|
|
|
|
|
|
return nil
|
|
|
}
|
|
|
-
|
|
|
-func (s *StoreEntry) EstrusList(ctx context.Context, req *pasturePb.SearchEventRequest, pagination *pasturePb.PaginationModel) (*pasturePb.EstrusEventResponse, error) {
|
|
|
- estrusList := make([]*model.EventEstrus, 0)
|
|
|
- var count int64 = 0
|
|
|
- pref := s.DB.Table(new(model.EventEstrus).TableName())
|
|
|
- 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(&estrusList).Error; err != nil {
|
|
|
- return nil, xerr.WithStack(err)
|
|
|
- }
|
|
|
-
|
|
|
- return &pasturePb.EstrusEventResponse{
|
|
|
- Code: http.StatusOK,
|
|
|
- Message: "ok",
|
|
|
- Data: &pasturePb.SearchEstrusData{
|
|
|
- List: model.EstrusSlice(estrusList).ToPB(),
|
|
|
- Total: int32(count),
|
|
|
- PageSize: pagination.PageSize,
|
|
|
- Page: pagination.Page,
|
|
|
- },
|
|
|
- }, nil
|
|
|
-}
|
|
|
-
|
|
|
-func (s *StoreEntry) EstrusCreate(ctx context.Context, req *pasturePb.EventEstrus) error {
|
|
|
- if len(req.CowId) <= 0 {
|
|
|
- return xerr.Custom("请选择相关牛只")
|
|
|
- }
|
|
|
- cowList, err := s.ParseCowIds(ctx, req.CowId)
|
|
|
- if err != nil {
|
|
|
- return xerr.WithStack(err)
|
|
|
- }
|
|
|
- estrusList := make([]*model.EventEstrus, 0)
|
|
|
- currentUser, err := s.GetCurrentSystemUser(ctx)
|
|
|
- if err != nil {
|
|
|
- return xerr.Custom("获取当前登录用户失败")
|
|
|
- }
|
|
|
-
|
|
|
- for _, cow := range cowList {
|
|
|
- operationUser, err := s.GetSystemUserById(ctx, int64(req.OperationId))
|
|
|
- if err != nil {
|
|
|
- return xerr.WithStack(err)
|
|
|
- }
|
|
|
- req.OperationName = operationUser.Name
|
|
|
- estrusList = append(estrusList, model.NewEventEstrus(cow, currentUser, req))
|
|
|
- }
|
|
|
-
|
|
|
- if err = s.DB.Create(estrusList).Error; err != nil {
|
|
|
- return xerr.WithStack(err)
|
|
|
- }
|
|
|
- return nil
|
|
|
-}
|
|
|
-
|
|
|
-func (s *StoreEntry) SameTimeCreate(ctx context.Context, req *pasturePb.EventSameTime) error {
|
|
|
- if _, err := s.GetSameTimeById(ctx, int64(req.SameTimeId)); err != nil {
|
|
|
- return xerr.WithStack(err)
|
|
|
- }
|
|
|
-
|
|
|
- drugs := &model.Drugs{}
|
|
|
- var err error
|
|
|
- if req.DrugsId > 0 {
|
|
|
- if drugs, err = s.GetDrugsById(ctx, int64(req.DrugsId)); err != nil {
|
|
|
- return xerr.WithStack(err)
|
|
|
- }
|
|
|
- }
|
|
|
- operationUser, err := s.GetSystemUserById(ctx, int64(req.OperationId))
|
|
|
- if err != nil {
|
|
|
- return xerr.WithStack(err)
|
|
|
- }
|
|
|
-
|
|
|
- if err = s.DB.Transaction(func(tx *gorm.DB) error {
|
|
|
- if err = tx.Model(new(model.CowSameTime)).
|
|
|
- Where("cow_id = ?", req.CowId).
|
|
|
- Where("same_time_id = ?", req.SameTimeId).
|
|
|
- Update("same_time_status", pasturePb.SameTimeStatus_In_Progress).Error; err != nil {
|
|
|
- return xerr.WithStack(err)
|
|
|
- }
|
|
|
-
|
|
|
- if err = tx.Model(new(model.EventCowSameTime)).
|
|
|
- Where("cow_id = ?", req.CowId).
|
|
|
- Where("same_time_id = ?", req.SameTimeId).
|
|
|
- Where("same_time_type = ?", req.SameTimeType).
|
|
|
- Updates(map[string]interface{}{
|
|
|
- "status": pasturePb.IsShow_Ok,
|
|
|
- "drug_id": drugs.Id,
|
|
|
- "unit": drugs.Unit,
|
|
|
- "usage": req.Usage,
|
|
|
- "remarks": req.Remarks,
|
|
|
- "operation_id": operationUser.Id,
|
|
|
- "operation_name": operationUser.Name,
|
|
|
- }).Error; err != nil {
|
|
|
- return xerr.WithStack(err)
|
|
|
- }
|
|
|
-
|
|
|
- return nil
|
|
|
- }); err != nil {
|
|
|
- return xerr.WithStack(err)
|
|
|
- }
|
|
|
- return nil
|
|
|
-}
|
|
|
-
|
|
|
-func (s *StoreEntry) SameTimeList(
|
|
|
- ctx context.Context,
|
|
|
- req *pasturePb.SearchEventRequest,
|
|
|
- pagination *pasturePb.PaginationModel,
|
|
|
-) (*pasturePb.SearchSameTimeResponse, error) {
|
|
|
- cowSameTimeList := make([]*model.EventCowSameTime, 0)
|
|
|
- var count int64 = 0
|
|
|
- pref := s.DB.Table(new(model.EventCowSameTime).TableName())
|
|
|
- if req.CowId != "" {
|
|
|
- cowIds := strings.Split(req.CowId, ",")
|
|
|
- pref.Where("cow_id IN ?", cowIds)
|
|
|
- }
|
|
|
-
|
|
|
- if err := pref.Count(&count).
|
|
|
- Limit(int(pagination.PageSize)).
|
|
|
- Offset(int(pagination.PageOffset)).
|
|
|
- Find(&cowSameTimeList).Error; err != nil {
|
|
|
- return nil, xerr.WithStack(err)
|
|
|
- }
|
|
|
-
|
|
|
- return &pasturePb.SearchSameTimeResponse{
|
|
|
- Code: http.StatusOK,
|
|
|
- Message: "ok",
|
|
|
- Data: &pasturePb.SameTimeData{
|
|
|
- List: model.EventCowSameTimeSlice(cowSameTimeList).ToPB(),
|
|
|
- Total: int32(count),
|
|
|
- PageSize: pagination.PageSize,
|
|
|
- Page: pagination.Page,
|
|
|
- },
|
|
|
- }, nil
|
|
|
-
|
|
|
-}
|
|
|
-
|
|
|
-func (s *StoreEntry) AbortionCreate(ctx context.Context, req *pasturePb.EventAbortionRequest) error {
|
|
|
- cow, err := s.GetCowInfoByCowId(ctx, int64(req.CowId))
|
|
|
- if err != nil {
|
|
|
- return xerr.WithStack(err)
|
|
|
- }
|
|
|
-
|
|
|
- systemUser, err := s.GetSystemUserById(ctx, int64(req.OperationId))
|
|
|
- if err != nil {
|
|
|
- return xerr.WithStack(err)
|
|
|
- }
|
|
|
-
|
|
|
- req.OperationName = systemUser.Name
|
|
|
- abortionReasonsMap := s.AbortionReasonsMap()
|
|
|
- newEventAbortion := model.NewEventAbortion(cow, req, abortionReasonsMap)
|
|
|
-
|
|
|
- lastCowMating := &model.EventMating{}
|
|
|
- if err = s.DB.Model(new(model.EventMating)).Where("cow_id = ?", cow.Id).
|
|
|
- Where("mating_result = ?", pasturePb.MatingResult_Pregnant).
|
|
|
- Order("id desc").First(lastCowMating).Error; err != nil {
|
|
|
- return xerr.WithStack(err)
|
|
|
- }
|
|
|
-
|
|
|
- if err = s.DB.Transaction(func(tx *gorm.DB) error {
|
|
|
- // 创建牛只流产事件数据
|
|
|
- if err = tx.Create(newEventAbortion).Error; err != nil {
|
|
|
- return xerr.WithStack(err)
|
|
|
- }
|
|
|
- // 更新牛只状态
|
|
|
- if err = tx.Model(new(model.Cow)).Where("id = ?", req.CowId).
|
|
|
- Updates(map[string]interface{}{
|
|
|
- "is_pregnant": pasturePb.IsShow_No,
|
|
|
- "last_abortion_at": req.AbortionAt,
|
|
|
- "breed_status": pasturePb.BreedStatus_Abort,
|
|
|
- }).Error; err != nil {
|
|
|
- return xerr.WithStack(err)
|
|
|
- }
|
|
|
-
|
|
|
- // 更新最近一次配种结果为流产
|
|
|
- if err = tx.Model(new(model.EventMating)).
|
|
|
- Where("id = ?", lastCowMating.Id).
|
|
|
- Updates(map[string]interface{}{
|
|
|
- "mating_result": pasturePb.MatingResult_Abort,
|
|
|
- "mating_result_at": req.AbortionAt,
|
|
|
- }).Error; err != nil {
|
|
|
- }
|
|
|
- return nil
|
|
|
- }); err != nil {
|
|
|
- return xerr.WithStack(err)
|
|
|
- }
|
|
|
-
|
|
|
- return nil
|
|
|
-}
|
|
|
-
|
|
|
-func (s *StoreEntry) AbortionCreateSlice(ctx context.Context, req *pasturePb.EventAbortionSlice) error {
|
|
|
- if len(req.Item) <= 0 {
|
|
|
- return xerr.WithStack(xerr.New("流产数据不能为空"))
|
|
|
- }
|
|
|
- errors := make(map[int32]error)
|
|
|
- for _, v := range req.Item {
|
|
|
- if err := s.AbortionCreate(ctx, v); err != nil {
|
|
|
- errors[v.CowId] = err
|
|
|
- }
|
|
|
- }
|
|
|
-
|
|
|
- if len(errors) > 0 {
|
|
|
- b, _ := json.Marshal(errors)
|
|
|
- return xerr.WithStack(xerr.Customf("部分流产数据创建失败: %s", string(b)))
|
|
|
- }
|
|
|
- return nil
|
|
|
-}
|
|
|
-
|
|
|
-func (s *StoreEntry) AbortionList(
|
|
|
- ctx context.Context,
|
|
|
- req *pasturePb.SearchEventRequest,
|
|
|
- pagination *pasturePb.PaginationModel,
|
|
|
-) (*pasturePb.EventAbortionResponse, error) {
|
|
|
- abortionList := make([]*model.EventAbortion, 0)
|
|
|
- var count int64 = 0
|
|
|
- pref := s.DB.Model(new(model.EventAbortion))
|
|
|
- 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(&abortionList).Error; err != nil {
|
|
|
- return nil, xerr.WithStack(err)
|
|
|
- }
|
|
|
-
|
|
|
- return &pasturePb.EventAbortionResponse{
|
|
|
- Code: http.StatusOK,
|
|
|
- Message: "ok",
|
|
|
- Data: &pasturePb.EventAbortionData{
|
|
|
- List: model.AbortionSlice(abortionList).ToPB(),
|
|
|
- Total: int32(count),
|
|
|
- PageSize: pagination.PageSize,
|
|
|
- Page: pagination.Page,
|
|
|
- },
|
|
|
- }, nil
|
|
|
-}
|