package backend import ( "context" "kpt-pasture/model" "net/http" "strings" "gitee.com/xuyiping_admin/pkg/logger/zaplog" "go.uber.org/zap" "gitee.com/xuyiping_admin/pkg/xerr" pasturePb "gitee.com/xuyiping_admin/go_proto/proto/go/backend/cow" ) func (s *StoreEntry) List(ctx context.Context, req *pasturePb.SearchEventRequest, pagination *pasturePb.PaginationModel) (*pasturePb.SearchCowListResponse, error) { currentUser, err := s.GetCurrentSystemUser(ctx) if err != nil { return nil, xerr.Custom("当前用户信息错误,请退出重新登录") } cowList := make([]*model.Cow, 0) var count int64 = 0 pref := s.DB.Model(new(model.Cow)). Where("pasture_id = ?", currentUser.PastureId) if len(req.CowId) > 0 { cowIds := strings.Split(req.CowId, ",") pref.Where("id IN ?", cowIds) } if req.Id > 0 { pref.Where("id = ?", req.Id) } if req.PenId > 0 { pref.Where("pen_id = ?", req.PenId) } if req.CowType > 0 { pref.Where("cow_type = ?", req.CowType) } if req.BreedStatus > 0 { pref.Where("breed_status = ?", req.BreedStatus) } if req.CowKind > 0 { pref.Where("cow_kind = ?", req.CowKind) } if req.Sex > 0 { pref.Where("sex = ?", req.Sex) } if req.Lact > 0 { pref.Where("lact = ?", req.Lact) } if req.CowSource > 0 { pref.Where("source_id = ?", req.CowSource) } if req.EarNumber != "" { pref.Where("ear_number = ?", req.EarNumber) } if err = pref.Order("id desc"). Count(&count). Limit(int(pagination.PageSize)). Offset(int(pagination.PageOffset)). Find(&cowList).Error; err != nil { return nil, xerr.WithStack(err) } penMap := s.PenMap(ctx, currentUser.PastureId) cowTypeMap := s.CowTypeMap() breedStatusMap := s.CowBreedStatusMap() cowKindMap := s.CowKindMap() cowSourceMap := s.CowSourceMap() admissionStatusMap := s.AdmissionStatusMap() healthStatusMap := s.HealthStatusMap() return &pasturePb.SearchCowListResponse{ Code: http.StatusOK, Message: "ok", Data: &pasturePb.SearchCowData{ List: model.CowSlice(cowList).ToPB(penMap, cowTypeMap, breedStatusMap, cowKindMap, cowSourceMap, admissionStatusMap, healthStatusMap), Total: int32(count), PageSize: pagination.PageSize, Page: pagination.Page, }, }, nil } func (s *StoreEntry) EventList(ctx context.Context, req *pasturePb.SearchCowEventListRequest, pagination *pasturePb.PaginationModel) (*pasturePb.CowEventListResponse, error) { currentUser, err := s.GetCurrentSystemUser(ctx) if err != nil { return nil, xerr.Custom("当前用户信息错误,请退出重新登录") } cow := &model.Cow{} if err = s.DB.Model(cow). Where("pasture_id = ?", currentUser.PastureId). Where("id = ?", req.CowId).First(cow).Error; err != nil { zaplog.Error("EventList", zap.Any("req", req), zap.Any("err", err), zap.Any("currentUser", currentUser)) return nil, xerr.Customf("该牛不存在: %d", req.CowId) } eventCowLogList := make([]*model.EventCowLog, 0) eventCowLog := &model.EventCowLog{CowId: cow.Id} pref := s.DB.Table(eventCowLog.TableName()). Where("cow_id = ?", req.CowId) if req.Lact >= 0 { pref.Where("lact = ?", req.Lact) } if req.EventCategoryKind > 0 { pref.Where("event_category_id = ?", req.EventCategoryKind) } if err = pref.Order("id desc"). Limit(int(pagination.PageSize)). Offset(int(pagination.PageOffset)). Find(&eventCowLogList).Error; err != nil { zaplog.Error("EventList", zap.Any("req", req), zap.Any("err", err), zap.Any("currentUser", currentUser)) return nil, xerr.WithStack(err) } eventCategoryMap := s.EventCategoryMap() return &pasturePb.CowEventListResponse{ Code: http.StatusOK, Message: "ok", Data: &pasturePb.CowEventData{ List: model.EventCowLogSlice(eventCowLogList).ToPB(eventCategoryMap), Total: int32(len(eventCowLogList)), PageSize: pagination.PageSize, Page: pagination.Page, }, }, nil }