UserStatusTransformer.php 3.6 KB

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