cow.go 2.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293
  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. currentUser, err := s.GetCurrentSystemUser(ctx)
  12. if err != nil {
  13. return nil, xerr.Custom("当前用户信息错误,请退出重新登录")
  14. }
  15. cowList := make([]*model.Cow, 0)
  16. var count int64 = 0
  17. pref := s.DB.Model(new(model.Cow)).
  18. Where("pasture_id = ?", currentUser.PastureId)
  19. if len(req.CowId) > 0 {
  20. cowIds := strings.Split(req.CowId, ",")
  21. pref.Where("id IN ?", cowIds)
  22. }
  23. if req.Id > 0 {
  24. pref.Where("id = ?", req.Id)
  25. }
  26. if req.PenId > 0 {
  27. pref.Where("pen_id = ?", req.PenId)
  28. }
  29. if req.CowType > 0 {
  30. pref.Where("cow_type = ?", req.CowType)
  31. }
  32. if req.BreedStatus > 0 {
  33. pref.Where("breed_status = ?", req.BreedStatus)
  34. }
  35. if req.CowKind > 0 {
  36. pref.Where("cow_kind = ?", req.CowKind)
  37. }
  38. if req.Sex > 0 {
  39. pref.Where("sex = ?", req.Sex)
  40. }
  41. if req.Lact > 0 {
  42. pref.Where("lact = ?", req.Lact)
  43. }
  44. if req.CowSource > 0 {
  45. pref.Where("source_id = ?", req.CowSource)
  46. }
  47. if req.EarNumber != "" {
  48. pref.Where("ear_number = ?", req.EarNumber)
  49. }
  50. if err = pref.Order("id desc").
  51. Count(&count).
  52. Limit(int(pagination.PageSize)).
  53. Offset(int(pagination.PageOffset)).
  54. Find(&cowList).Error; err != nil {
  55. return nil, xerr.WithStack(err)
  56. }
  57. penMap := s.PenMap(ctx, currentUser.PastureId)
  58. cowTypeMap := s.CowTypeMap()
  59. breedStatusMap := s.CowBreedStatusMap()
  60. cowKindMap := s.CowKindMap()
  61. cowSourceMap := s.CowSourceMap()
  62. admissionStatusMap := s.AdmissionStatusMap()
  63. healthStatusMap := s.HealthStatusMap()
  64. return &pasturePb.SearchCowListResponse{
  65. Code: http.StatusOK,
  66. Message: "ok",
  67. Data: &pasturePb.SearchCowData{
  68. List: model.CowSlice(cowList).ToPB(penMap, cowTypeMap, breedStatusMap,
  69. cowKindMap, cowSourceMap, admissionStatusMap, healthStatusMap),
  70. Total: int32(count),
  71. PageSize: pagination.PageSize,
  72. Page: pagination.Page,
  73. },
  74. }, nil
  75. }