package backend import ( "context" "kpt-pasture/model" "net/http" "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 }