UserStatusTransformer.php 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126
  1. <?php
  2. /**
  3. * Created by PhpStorm.
  4. * User: Mead
  5. * Date: 2019/8/5
  6. * Time: 2:44 PM
  7. */
  8. namespace App\Transformers;
  9. use App\Models\Coupon;
  10. use App\Models\CouponsUserBag;
  11. use App\Models\PunishmentOrder;
  12. use App\Models\Student;
  13. use App\Models\User;
  14. use App\Repositories\CouponRepository;
  15. use App\Repositories\OrderRepository;
  16. use App\Repositories\RentOrderRepository;
  17. use Carbon\Carbon;
  18. use League\Fractal\TransformerAbstract;
  19. class UserStatusTransformer extends TransformerAbstract
  20. {
  21. protected $orderRepository;
  22. protected $rentOrderRepository;
  23. protected $couponRepository;
  24. public function __construct(OrderRepository $orderRepository, RentOrderRepository $rentOrderRepository, CouponRepository $couponRepository)
  25. {
  26. $this->orderRepository = $orderRepository;
  27. $this->rentOrderRepository = $rentOrderRepository;
  28. $this->couponRepository = $couponRepository;
  29. }
  30. public function transform(User $user)
  31. {
  32. if (Carbon::today()->format('Y-m-d') == Carbon::parse($user->deposit_expire_time)->format('Y-m-d')) {
  33. $deposit_expire_time = '免押金资格' . Carbon::parse($user->deposit_expire_time)->diffForHumans(Carbon::now()) . '到期';
  34. } else {
  35. $deposit_expire_time = '免押金资格有效期至' . Carbon::parse($user->deposit_expire_time)->format('Y-m-d');
  36. }
  37. // $new_user_coupons = [];
  38. // if ($user->is_new_user_coupons == User::IS_NEW_USER_COUPONS_OK) {
  39. // $new_user_coupons = $this->couponRepository->getNewUserCoupons(request()->get('area_id', 0));
  40. // }
  41. return [
  42. 'is_card_certified' => $user->is_card_certified,
  43. 'is_deposit' => $user->is_deposit,
  44. 'is_coupon_deposit_free' => $user->is_coupon_deposit_free,
  45. 'deposit_type_name' => User::$depositTypeMaps[$user->deposit_type],
  46. 'deposit_type' => $user->deposit_type,
  47. 'deposit_expire_time' => $deposit_expire_time,
  48. 'is_match_ride_age' => $user->is_match_ride_age,
  49. 'is_bind_mobile' => $user->is_bind_mobile,
  50. 'status' => $user->status,
  51. 'is_register' => $user->is_register,
  52. 'is_ride_order' => $this->rideOrder($user),
  53. 'is_pay_order' => $this->noPayOrder($user),
  54. 'is_rent_order' => $this->rentOrder($user),
  55. 'is_pay_rent_order' => $this->rentPayOrder($user),
  56. 'is_punishment' => $this->checkPunishment($user->id),
  57. 'is_student' => $user->is_student,
  58. 'is_exist_auth_student' => $user->is_student ? true : $this->is_exist_auth_student($user->id),
  59. 'is_new_user_coupons' => $user->is_new_user_coupons,
  60. // 'new_user_coupons' => $new_user_coupons
  61. ];
  62. }
  63. /**
  64. * 检查用户是否有罚单
  65. * @param $user_id
  66. * @return mixed
  67. * Author: Mead
  68. */
  69. protected function checkPunishment($user_id)
  70. {
  71. return PunishmentOrder::where('user_id', $user_id)->where('pay_status', PunishmentOrder::PAY_STATUS_NO)->exists();
  72. }
  73. /**
  74. * 是否存在用户认证学生
  75. * @param $user_id
  76. * Author: Mead
  77. */
  78. protected function is_exist_auth_student($user_id)
  79. {
  80. $auth = Student::where('user_id', $user_id)->select(['auth_status', 'error_msg'])->first();
  81. if (!$auth) return false;
  82. if ((int)$auth->auth_status === Student::AUTH_STATUS_OK) {
  83. return true;
  84. }
  85. return $auth->toArray();
  86. }
  87. protected function rentOrder($user)
  88. {
  89. $no = $this->rentOrderRepository->byUserIdCheckIsExistRentOrderWithNo($user->id);
  90. if (is_null($no)) return false;
  91. return $no;
  92. }
  93. public function rentPayOrder($user)
  94. {
  95. $no = $this->rentOrderRepository->byUserIdCheckIsExistNoPayOrderWithNo($user->id);
  96. if (is_null($no)) return false;
  97. return $no;
  98. }
  99. protected function rideOrder($user)
  100. {
  101. $no = $this->orderRepository->byUserIdCheckIsExistRideOrderWithNo($user->id);
  102. if (is_null($no)) return false;
  103. return $no;
  104. }
  105. protected function noPayOrder($user)
  106. {
  107. $no = $this->orderRepository->byUserIdCheckIsExistNoPayOrderWithNo($user->id);
  108. if (is_null($no)) return false;
  109. return $no;
  110. }
  111. }