cow.go 1.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687
  1. package backend
  2. import (
  3. "context"
  4. "kpt-pasture/model"
  5. "net/http"
  6. "strings"
  7. "gitee.com/xuyiping_admin/pkg/xerr"
  8. pasturePb "gitee.com/xuyiping_admin/go_proto/proto/go/backend/cow"
  9. )
  10. func (s *StoreEntry) CowList(ctx context.Context, req *pasturePb.SearchEventRequest, pagination *pasturePb.PaginationModel) (*pasturePb.SearchCowListResponse, error) {
  11. cowList := make([]*model.Cow, 0)
  12. var count int64 = 0
  13. pref := s.DB.Model(new(model.Cow))
  14. if len(req.CowId) > 0 {
  15. cowIds := strings.Split(req.CowId, ",")
  16. pref.Where("id IN ?", cowIds)
  17. }
  18. if req.Id > 0 {
  19. pref.Where("id = ?", req.Id)
  20. }
  21. if req.PenId > 0 {
  22. pref.Where("pen_id = ?", req.PenId)
  23. }
  24. if req.CowType > 0 {
  25. pref.Where("cow_type = ?", req.CowType)
  26. }
  27. if req.BreedStatus > 0 {
  28. pref.Where("breed_status = ?", req.BreedStatus)
  29. }
  30. if req.CowKind > 0 {
  31. pref.Where("cow_kind = ?", req.CowKind)
  32. }
  33. if req.Sex > 0 {
  34. pref.Where("sex = ?", req.Sex)
  35. }
  36. if req.Lact > 0 {
  37. pref.Where("lact = ?", req.Lact)
  38. }
  39. if req.CowSource > 0 {
  40. pref.Where("source_id = ?", req.CowSource)
  41. }
  42. if req.EarNumber != "" {
  43. pref.Where("ear_number = ?", req.EarNumber)
  44. }
  45. if err := pref.Order("id desc").
  46. Count(&count).
  47. Limit(int(pagination.PageSize)).
  48. Offset(int(pagination.PageOffset)).
  49. Find(&cowList).Error; err != nil {
  50. return nil, xerr.WithStack(err)
  51. }
  52. penMap := s.PenMap(ctx)
  53. cowTypeMap := s.CowTypeMap()
  54. breedStatusMap := s.CowBreedStatusMap()
  55. cowKindMap := s.CowKindMap()
  56. cowSourceMap := s.CowSourceMap()
  57. admissionStatusMap := s.AdmissionStatusMap()
  58. healthStatusMap := s.HealthStatusMap()
  59. return &pasturePb.SearchCowListResponse{
  60. Code: http.StatusOK,
  61. Message: "ok",
  62. Data: &pasturePb.SearchCowData{
  63. List: model.CowSlice(cowList).ToPB(penMap, cowTypeMap, breedStatusMap,
  64. cowKindMap, cowSourceMap, admissionStatusMap, healthStatusMap),
  65. Total: int32(count),
  66. PageSize: pagination.PageSize,
  67. Page: pagination.Page,
  68. },
  69. }, nil
  70. }