CardRidingUserBagsRepository.php 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133
  1. <?php
  2. namespace App\Repositories;
  3. use App\Models\CardRidingOrder;
  4. use App\Models\CardRidingUserBags;
  5. use Carbon\Carbon;
  6. use Illuminate\Support\Facades\Log;
  7. class CardRidingUserBagsRepository extends BaseRepository
  8. {
  9. public $userRepository;
  10. public $cardRidingRepository;
  11. public $cardRidingOrderRepository;
  12. public function __construct(CardRidingUserBags $cardRidingUserBags,UserRepository $userRepository,CardRidingRepository $cardRidingRepository,CardRidingOrderRepository $cardRidingOrderRepository)
  13. {
  14. $this->model = $cardRidingUserBags;
  15. $this->userRepository = $userRepository;
  16. $this->cardRidingRepository = $cardRidingRepository;
  17. $this->cardRidingOrderRepository = $cardRidingOrderRepository;
  18. }
  19. /**
  20. * 判断用户是否存在有效骑行卡 有的话返回该卡 没有的话 返回[]
  21. *
  22. * @param $user_id integer
  23. *
  24. * @return mixed
  25. *
  26. * User:Fx
  27. * */
  28. public function isExist($user_id,$order_start_time=''){
  29. $cardRidingUser = $this->model->query()
  30. ->where('user_id',$user_id)
  31. ->where('status',CardRidingUserBags::STATUS_OK)
  32. ->first();
  33. if(empty($cardRidingUser)){
  34. // 不存在
  35. return [];
  36. }
  37. $t1 = Carbon::make($cardRidingUser->expiration_time); // 骑行卡过期时间
  38. $t2 = Carbon::now(); // 对比过期时间
  39. if(!empty($order_start_time)){
  40. $t2 = Carbon::make($order_start_time);
  41. }
  42. if($t1->lt($t2)){
  43. // 已过期
  44. $cardRidingUser->status = CardRidingUserBags::STATUS_NO;
  45. $cardRidingUser->save();
  46. return [];
  47. }
  48. if($cardRidingUser->is_limit_times == CardRidingUserBags::LIMIT_TIMES_YES){
  49. // 限次卡 判断是否次数归0
  50. if($cardRidingUser->can_ridding_times <= 0){
  51. // 次数已经归0 更新卡失效
  52. $cardRidingUser->status = CardRidingUserBags::STATUS_NO;
  53. $cardRidingUser->save();
  54. return [];
  55. }
  56. }
  57. // 存在
  58. return $cardRidingUser;
  59. }
  60. /**
  61. * 更新用户骑行卡失效
  62. *
  63. * @param $id integer
  64. * @param $user_id integer
  65. *
  66. * @return bool
  67. * */
  68. public function updateStatusNo($id){
  69. try{
  70. $this->model->query()->where('id',$id)->update(['status'=>CardRidingUserBags::STATUS_NO]);
  71. return true;
  72. }catch (\Exception $exception){
  73. Log::error($exception->getMessage());
  74. return false;
  75. }
  76. }
  77. /**
  78. * 邀请新用户送卡 inviteNewRewardCard
  79. *
  80. * @param $card_id
  81. * @param $user_id
  82. * @return void
  83. * @author Fx
  84. *
  85. */
  86. public function inviteNewRewardCard($card_id,$user_id){
  87. $users = $this->userRepository->byIdGetModel($user_id);
  88. $cardRiding = $this->cardRidingRepository->byIdGetModel($card_id);
  89. $cardRidingOrdersData = [
  90. 'no' => CardRidingOrder::makeNo(),
  91. 'area_id' => $users->register_area_id,
  92. 'user_id' => $users->id,
  93. 'riding_card_id' => $card_id,
  94. 'money' => 0,
  95. 'discount' => 1,
  96. 'pay_money' => 0,
  97. 'pay_type' => CardRidingOrder::PAY_TYPE_INVITE_NEW_USER_GIVE,
  98. 'pay_status' => CardRidingOrder::PAY_STATUS_OK,
  99. 'pay_time' => Carbon::now(),
  100. ];
  101. $cardRidingOrder = $this->cardRidingOrderRepository->model::firstOrCreate($cardRidingOrdersData);
  102. $expiration_time = Carbon::now()->addDays($cardRiding->effective_days)->format('Y-m-d H:i:s');
  103. $data = [
  104. 'user_id' => $users->id,
  105. 'riding_card_id' => $card_id,
  106. 'card_riding_order_id' => $cardRidingOrder->id,
  107. 'take_effect_time' => Carbon::now(),
  108. 'deduction_money' => $cardRiding->deduction_money,
  109. 'expiration_time' => $expiration_time,
  110. 'is_limit_times' => $cardRiding->is_limit_times,
  111. 'can_ridding_times' => $cardRiding->times,
  112. 'day_can_ridding_times' => $cardRiding->day_can_ridding_times,
  113. 'status' => CardRidingUserBags::STATUS_OK
  114. ];
  115. // Log::info($data);
  116. return $this->model::firstOrCreate(['card_riding_order_id' => $cardRidingOrder->id], $data);
  117. }
  118. }