package backend import ( "context" "kpt-pasture/model" "net/http" "time" "gorm.io/gorm" "gitee.com/xuyiping_admin/pkg/xerr" pasturePb "gitee.com/xuyiping_admin/go_proto/proto/go/backend/cow" ) func (s *StoreEntry) DryMilkList(ctx context.Context, req *pasturePb.SearchEventRequest, pagination *pasturePb.PaginationModel) (*pasturePb.EventMilkResponse, error) { userModel, err := s.GetUserModel(ctx) if err != nil { return nil, xerr.WithStack(err) } dryMilkList := make([]*model.EventDryMilk, 0) var count int64 = 0 pref := s.DB.Model(new(model.EventDryMilk)). Where("pasture_id = ?", userModel.AppPasture.Id). Where("is_show = ?", pasturePb.IsShow_Ok) if req.EarNumber != "" { pref.Where("ear_number = ?", req.EarNumber) } if req.StartDayAt > 0 && req.EndDayAt > 0 && req.StartDayAt <= req.EndDayAt { pref.Where("reality_day BETWEEN ? AND ?", req.StartDayAt, req.EndDayAt) } if err = pref.Order("id desc"). Count(&count).Limit(int(pagination.PageSize)). Offset(int(pagination.PageOffset)). Find(&dryMilkList).Error; err != nil { return nil, xerr.WithStack(err) } return &pasturePb.EventMilkResponse{ Code: http.StatusOK, Msg: "ok", Data: &pasturePb.EventMilkData{ List: model.EventDryMilkSlice(dryMilkList).ToPB(), Total: int32(count), PageSize: pagination.PageSize, Page: pagination.Page, }, }, nil } func (s *StoreEntry) DryMilkBatch(ctx context.Context, req *pasturePb.EventMilkBatch) error { if len(req.Items) <= 0 { return nil } if len(req.Items) > 50 { return xerr.Custom("最多只能添加50条数据") } userModel, err := s.GetUserModel(ctx) if err != nil { return xerr.WithStack(err) } penMap := s.PenMap(ctx, userModel.AppPasture.Id) if err = s.DB.Transaction(func(tx *gorm.DB) error { for _, v := range req.Items { cowInfo, err := s.GetCowInfoByCowId(ctx, userModel.AppPasture.Id, int64(v.CowId)) if err != nil { return xerr.WithStack(err) } cowInfo.EventDryMilkUpdate(int64(v.DryMilkAt), penMap[v.PenId]) if err = tx.Select("milk_kind", "last_dry_milk_at", "pen_id", "pen_name"). Where("id = ?", cowInfo.Id). Updates(cowInfo).Error; err != nil { return xerr.WithStack(err) } eventDryMilk := &model.EventDryMilk{} if err = tx.Model(new(model.EventDryMilk)). Where("pasture_id = ?", userModel.AppPasture.Id). Where("cow_id = ?", cowInfo.Id). Where("is_show = ?", pasturePb.IsShow_No). First(eventDryMilk).Error; err != nil { return xerr.WithStack(err) } if eventDryMilk.Id <= 0 { return xerr.Customf("不存在该数据: %s", cowInfo.EarNumber) } operationUser, err := s.GetSystemUserById(ctx, int64(v.OperationId)) if err != nil { return xerr.Customf("获取操作人员信息失败") } eventDryMilk.EventDryMilkUpdate(cowInfo, int64(v.DryMilkAt), penMap[v.PenId], operationUser, userModel.SystemUser, v.Remarks) if err = tx.Select("lact", "reality_day", "plan_day", "is_show", "operation_id", "operation_name", "message_id", "message_name", "pen_id", "pen_name", "remarks"). Where("id = ?", eventDryMilk.Id). Updates(eventDryMilk).Error; err != nil { return xerr.WithStack(err) } // 记录日志 cowLog := s.SubmitEventLog(ctx, userModel.AppPasture.Id, cowInfo, pasturePb.EventType_Dry_Milk, eventDryMilk) if err = tx.Table(cowLog.TableName()).Create(cowLog).Error; err != nil { return xerr.WithStack(err) } } return nil }); err != nil { return xerr.WithStack(err) } return nil } func (s *StoreEntry) ForbiddenMatingBatch(ctx context.Context, req *pasturePb.EventForbiddenMatingBatch) error { userModel, err := s.GetUserModel(ctx) if err != nil { return xerr.WithStack(err) } if len(req.Items) > 50 { return xerr.Custom("最多只能添加50条数据") } items, err := s.ForbiddenMatingCheck(ctx, userModel, req.Items) if err != nil { return xerr.WithStack(err) } if len(items) <= 0 { return nil } eventForbiddenMatingList := make([]*model.EventForbiddenMating, 0) if err = s.DB.Transaction(func(tx *gorm.DB) error { for _, item := range items { cowLog := s.SubmitEventLog(ctx, userModel.AppPasture.Id, item.Cow, pasturePb.EventType_ForbiddenMating, item) eventForbiddenMating := model.NewEventForbiddenMating(userModel.AppPasture.Id, item.Cow, item.ForbiddenMatingAt, item.ForbiddenMatingReasonsKind, item.ForbiddenMatingReasonsName, item.Remarks, item.OperationUser, userModel.SystemUser) eventForbiddenMatingList = append(eventForbiddenMatingList, eventForbiddenMating) // 更新牛只信息 item.Cow.ForbiddenMatingUpdate(item.ForbiddenMatingAt) if err = tx.Model(new(model.Cow)). Select("is_forbidden_mating", "last_forbidden_mating_at", "breed_status"). Updates(item.Cow).Error; err != nil { return xerr.WithStack(err) } // 记录日志 if err = tx.Table(cowLog.TableName()). Create(cowLog).Error; err != nil { return xerr.WithStack(err) } } if err = tx.Model(new(model.EventForbiddenMating)). Create(eventForbiddenMatingList).Error; err != nil { return xerr.WithStack(err) } return nil }); err != nil { return xerr.WithStack(err) } return nil } func (s *StoreEntry) ForbiddenMatingList(ctx context.Context, req *pasturePb.SearchEventRequest, pagination *pasturePb.PaginationModel) (*pasturePb.EventForbiddenMatingResponse, error) { userModel, err := s.GetUserModel(ctx) if err != nil { return nil, xerr.WithStack(err) } eventForbiddenMatingList := make([]*model.EventForbiddenMating, 0) var count int64 = 0 pref := s.DB.Model(new(model.EventForbiddenMating)). Where("pasture_id = ?", userModel.AppPasture.Id). Where("is_show = ?", pasturePb.IsShow_Ok) if req.EarNumber != "" { pref.Where("ear_number = ?", req.EarNumber) } if req.StartDayAt > 0 && req.EndDayAt > 0 && req.StartDayAt <= req.EndDayAt { pref.Where("reality_day BETWEEN ? AND ?", req.StartDayAt, req.EndDayAt) } if err = pref.Order("id desc"). Count(&count).Limit(int(pagination.PageSize)). Offset(int(pagination.PageOffset)). Find(&eventForbiddenMatingList).Error; err != nil { return nil, xerr.WithStack(err) } return &pasturePb.EventForbiddenMatingResponse{ Code: http.StatusOK, Msg: "ok", Data: &pasturePb.EventForbiddenMatingData{ List: model.EventForbiddenMatingSlice(eventForbiddenMatingList).ToPB(), Total: int32(count), PageSize: pagination.PageSize, Page: pagination.Page, }, }, nil } func (s *StoreEntry) UnForbiddenMating(ctx context.Context, req *pasturePb.EventUnForbiddenMatingRequest) error { userModel, err := s.GetUserModel(ctx) if err != nil { return xerr.WithStack(err) } if len(req.EarNumbers) <= 0 { return xerr.Custom("请选择相关牛只") } if len(req.EarNumbers) > 50 { return xerr.Custom("最多只能添加50条数据") } eventForbiddenMatingList := make([]*model.EventForbiddenMating, 0) if err = s.DB.Model(new(model.EventForbiddenMating)). Where("pasture_id = ?", userModel.AppPasture.Id). Where("is_show = ?", pasturePb.IsShow_Ok). Where("ear_number IN (?)", req.EarNumbers). Find(&eventForbiddenMatingList).Error; err != nil { return xerr.WithStack(err) } if len(eventForbiddenMatingList) <= 0 { return nil } nowTime := time.Now().Unix() if err = s.DB.Transaction(func(tx *gorm.DB) error { for _, item := range eventForbiddenMatingList { cowInfo, err := s.GetCowInfoByCowId(ctx, userModel.AppPasture.Id, item.CowId) if err != nil { return xerr.WithStack(err) } // 牛只解配更新 cowInfo.UnForbiddenMatingUpdate() if err = tx.Model(new(model.Cow)). Select("is_forbidden_mating", "last_forbidden_mating_at", "breed_status"). Updates(cowInfo).Error; err != nil { return xerr.WithStack(err) } // 事件更新 item.UnForbiddenMatingUpdate(nowTime, userModel.SystemUser) if err = tx.Model(new(model.EventForbiddenMating)). Select("is_show", "un_forbidden_mating_at"). Where("id = ?", item.Id). Updates(item).Error; err != nil { return xerr.WithStack(err) } cowLog := s.SubmitEventLog(ctx, userModel.AppPasture.Id, cowInfo, pasturePb.EventType_UnForbiddenMating, item) if err = tx.Table(cowLog.TableName()).Create(cowLog).Error; err != nil { return xerr.WithStack(err) } } return nil }); err != nil { return xerr.WithStack(err) } return nil }