cow.go 1.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051
  1. package backend
  2. import (
  3. "context"
  4. "fmt"
  5. "kpt-pasture/model"
  6. "net/http"
  7. "strings"
  8. "gitee.com/xuyiping_admin/pkg/xerr"
  9. pasturePb "gitee.com/xuyiping_admin/go_proto/proto/go/backend/cow"
  10. )
  11. func (s *StoreEntry) CowList(ctx context.Context, req *pasturePb.SearchEventRequest, pagination *pasturePb.PaginationModel) (*pasturePb.SearchCowListResponse, error) {
  12. cowList := make([]*pasturePb.SearchCowList, 0)
  13. var count int64 = 0
  14. pref := s.DB.Table(fmt.Sprintf("%s as a", new(model.Cow).TableName())).
  15. 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,
  16. 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,
  17. e.name as cow_kind_name,f.name as pen_name,g.name as status_name`).
  18. Joins(fmt.Sprintf("JOIN %s AS b ON a.breed_status_id = b.id", new(model.ConfigBreedStatus).TableName())).
  19. Joins(fmt.Sprintf("JOIN %s AS d on a.type_id = d.id", new(model.ConfigCowType).TableName())).
  20. Joins(fmt.Sprintf("JOIN %s AS e ON a.kind_id = e.id", new(model.ConfigCowKind).TableName())).
  21. Joins(fmt.Sprintf("JOIN %s AS f on a.pen_id = f.id", new(model.Pen).TableName())).
  22. Joins(fmt.Sprintf("JOIN %s AS g on a.status = g.id", new(model.ConfigCowStatus).TableName()))
  23. if len(req.CowId) > 0 {
  24. cowIds := strings.Split(req.CowId, ",")
  25. pref.Where("a.id IN ?", cowIds)
  26. }
  27. if err := pref.Order("a.id desc").
  28. Count(&count).Limit(int(pagination.PageSize)).
  29. Offset(int(pagination.PageOffset)).
  30. Find(&cowList).Error; err != nil {
  31. return nil, xerr.WithStack(err)
  32. }
  33. return &pasturePb.SearchCowListResponse{
  34. Code: http.StatusOK,
  35. Message: "ok",
  36. Data: &pasturePb.SearchCowData{
  37. List: cowList,
  38. Total: int32(count),
  39. PageSize: pagination.PageSize,
  40. Page: pagination.Page,
  41. },
  42. }, nil
  43. }