package backend import ( "context" "fmt" "kpt-pasture/model" "net/http" "strings" "gitee.com/xuyiping_admin/pkg/xerr" pasturePb "gitee.com/xuyiping_admin/go_proto/proto/go/backend/cow" ) func (s *StoreEntry) CowList(ctx context.Context, req *pasturePb.SearchEventRequest, pagination *pasturePb.PaginationModel) (*pasturePb.SearchCowListResponse, error) { cowList := make([]*pasturePb.SearchCowList, 0) var count int64 = 0 pref := s.DB.Table(fmt.Sprintf("%s as a", new(model.Cow).TableName())). Select(`a.id as cow_id,a.sex,a.neck_ring_number,a.ear_number,a.pen_id,a.lact,a.type_id as cow_type_id,a.breed_status_id, a.status as status_id,a.kind_id as cow_kind_id,a.birth_at,b.name as breed_status_name,d.name as cow_type_name, e.name as cow_kind_name,f.name as pen_name,g.name as status_name`). Joins(fmt.Sprintf("JOIN %s AS b ON a.breed_status_id = b.id", new(model.ConfigBreedStatus).TableName())). Joins(fmt.Sprintf("JOIN %s AS d on a.type_id = d.id", new(model.ConfigCowType).TableName())). Joins(fmt.Sprintf("JOIN %s AS e ON a.kind_id = e.id", new(model.ConfigCowKind).TableName())). Joins(fmt.Sprintf("JOIN %s AS f on a.pen_id = f.id", new(model.Pen).TableName())). Joins(fmt.Sprintf("JOIN %s AS g on a.status = g.id", new(model.ConfigCowStatus).TableName())) if len(req.CowId) > 0 { cowIds := strings.Split(req.CowId, ",") pref.Where("a.id IN ?", cowIds) } if err := pref.Order("a.id desc"). Count(&count).Limit(int(pagination.PageSize)). Offset(int(pagination.PageOffset)). Find(&cowList).Error; err != nil { return nil, xerr.WithStack(err) } return &pasturePb.SearchCowListResponse{ Code: http.StatusOK, Message: "ok", Data: &pasturePb.SearchCowData{ List: cowList, Total: int32(count), PageSize: pagination.PageSize, Page: pagination.Page, }, }, nil }