package backend import ( "context" "encoding/json" "fmt" "kpt-pasture/model" "net/http" "strings" pasturePb "gitee.com/xuyiping_admin/go_proto/proto/go/backend/cow" "gitee.com/xuyiping_admin/pkg/xerr" "gorm.io/gorm" ) func (s *StoreEntry) PregnantCheckList(ctx context.Context, req *pasturePb.SearchEventRequest, pagination *pasturePb.PaginationModel) (*pasturePb.PregnantCheckEventResponse, error) { currentUser, err := s.GetCurrentSystemUser(ctx) if err != nil { return nil, xerr.Custom("当前用户信息错误,请退出重新登录") } pregnantCheckList := make([]*model.EventPregnantCheck, 0) var count int64 = 0 pref := s.DB.Table(new(model.EventPregnantCheck).TableName()). 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(&pregnantCheckList).Error; err != nil { return nil, xerr.WithStack(err) } pregnantCheckResultMap := s.PregnantCheckResultMap() pregnantCheckMethodMap := s.PregnantCheckMethodMap() return &pasturePb.PregnantCheckEventResponse{ Code: http.StatusOK, Message: "ok", Data: &pasturePb.SearchPregnantCheckData{ List: model.EventPregnantCheckSlice(pregnantCheckList).ToPB(pregnantCheckResultMap, pregnantCheckMethodMap), Total: int32(count), PageSize: pagination.PageSize, Page: pagination.Page, }, }, nil } func (s *StoreEntry) PregnantCheckCreateBatch(ctx context.Context, req *pasturePb.EventPregnantCheckBatch) (err error) { currentUser, err := s.GetCurrentSystemUser(ctx) if err != nil { return xerr.Custom("当前用户信息错误,请退出重新登录") } pregnantCheckBatchModelList, err := s.PregnantCheckDataCheck(ctx, req) if err != nil { return xerr.WithStack(err) } cowIds := make([]int64, 0) // 更新孕检牛只 newPregnantCheckBatchModelList := make([]*PregnantCheckBatchModel, 0) for _, item := range pregnantCheckBatchModelList { newPregnantCheckBatchModelList = append(newPregnantCheckBatchModelList, item) cowIds = append(cowIds, item.Cow.Id) } defer func() { if err == nil { // 提交事件日志 for _, cowId := range cowIds { cow, _ := s.GetCowInfoByCowId(ctx, currentUser.PastureId, cowId) cowLogs := s.SubmitEventLog(ctx, currentUser, cow, pasturePb.EventType_Pregnancy_Check, pasturePb.ExposeEstrusType_Invalid, req) s.DB.Table(cowLogs.TableName()).Create(cowLogs) } } }() if err = s.DB.Transaction(func(tx *gorm.DB) error { for _, item := range newPregnantCheckBatchModelList { breedStatus := pasturePb.BreedStatus_Pregnant isPregnant := pasturePb.IsShow_Ok if item.PregnantCheckResult == pasturePb.PregnantCheckResult_UnPregnant { breedStatus = pasturePb.BreedStatus_Empty isPregnant = pasturePb.IsShow_No } // 更新上一次配种结果数据,如何是复检无胎则更新为流产 if item.EventPregnancyCheck.PregnantCheckName == model.PregnantCheckForSecond { breedStatus = pasturePb.BreedStatus_Abort } item.Cow.EventPregnantCheckUpdate(breedStatus, int64(item.PregnantCheckAt), isPregnant) // 更新牛只基本信息 if err = tx.Model(&model.Cow{}). Select("breed_status,last_pregnant_check_at,is_pregnant"). Where("cow_id = ?", item.Cow.Id). Updates(item.Cow).Error; err != nil { return xerr.WithStack(err) } if err = tx.Model(new(model.EventMating)). Where("id = (?)", fmt.Sprintf(` SELECT id FROM event_mating where cow_id = %d AND status = %d ORDER BY id DESC LIMIT 1 `, item.Cow.Id, pasturePb.IsShow_Ok)). Updates(map[string]interface{}{ "mating_result": breedStatus, "mating_result_at": item.PregnantCheckAt, }).Error; err != nil { return xerr.WithStack(err) } // 更新事件表 item.EventPregnancyCheck.EventUpdate(int64(item.PregnantCheckAt), item.PregnantCheckResult, item.PregnantCheckMethod, item.OperationUser, currentUser, item.Remarks) // 更新孕检事件表 if err = tx.Model(new(model.EventPregnantCheck)). Select("reality_day,pregnant_check_result,pregnant_check_method,operation_id,operation_name,message_id,message_name,remarks,status"). Where("id = ?", item.EventPregnancyCheck.Id). Updates(item.EventPregnancyCheck).Error; err != nil { return xerr.WithStack(err) } } return nil }); err != nil { return xerr.WithStack(err) } return nil } func (s *StoreEntry) AbortionCreate(ctx context.Context, req *pasturePb.EventAbortionRequest) error { currentUser, err := s.GetCurrentSystemUser(ctx) if err != nil { return xerr.Custom("当前用户信息错误,请退出重新登录") } cow, err := s.GetCowInfoByCowId(ctx, currentUser.PastureId, int64(req.CowId)) if err != nil { return xerr.WithStack(err) } systemUser, err := s.GetSystemUserById(ctx, currentUser.PastureId, int64(req.OperationId)) if err != nil { return xerr.WithStack(err) } req.OperationName = systemUser.Name abortionReasonsMap := s.AbortionReasonsMap() newEventAbortion := model.NewEventAbortion(currentUser.PastureId, 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) } // 更新最近一次配种结果为流产 lastCowMating.EventAbortionUpdate(int64(req.AbortionAt)) // 更新流产数据 cow.EventAbortionUpdate(int64(req.AbortionAt)) defer func() { if err != nil { cowLogs := s.SubmitEventLog(ctx, currentUser, cow, pasturePb.EventType_Abort, pasturePb.ExposeEstrusType_Invalid, req) s.DB.Table(cowLogs.TableName()).Create(cowLogs) } }() 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)). Select("is_pregnant,last_abortion_at,breed_status"). Where("id = ?", req.CowId). Updates(cow).Error; err != nil { return xerr.WithStack(err) } // 更新最近一次配种结果为流产 if err = tx.Model(new(model.EventMating)). Select("mating_result,mating_result_at"). Where("id = ?", lastCowMating.Id). Updates(lastCowMating).Error; err != nil { } return nil }); err != nil { return xerr.WithStack(err) } return nil } func (s *StoreEntry) AbortionCreateBatch(ctx context.Context, req *pasturePb.EventAbortionBatch) 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) { currentUser, err := s.GetCurrentSystemUser(ctx) if err != nil { return nil, xerr.Custom("当前用户信息错误,请退出重新登录") } abortionList := make([]*model.EventAbortion, 0) var count int64 = 0 pref := s.DB.Model(new(model.EventAbortion)). 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(&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 }