CouponUserBagsRepository.php 5.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165
  1. <?php
  2. /**
  3. *
  4. *
  5. * @category xxx
  6. * @package PSR
  7. * @subpackage Documentation\API
  8. * @author xxx <xxx@xxx.com>
  9. * @license GPL https://xxx.com
  10. * @link https://xxx.com
  11. * @ctime: 2020/4/7 9:40
  12. */
  13. namespace App\Repositories;
  14. use App\Models\Coupon;
  15. use App\Models\CouponsUserBag;
  16. use Carbon\Carbon;
  17. use Illuminate\Support\Facades\Log;
  18. class CouponUserBagsRepository extends BaseRepository
  19. {
  20. public $couponRepository;
  21. public $userRepository;
  22. public function __construct(CouponsUserBag $couponsUserBag, CouponRepository $couponRepository, UserRepository $userRepository)
  23. {
  24. $this->model = $couponsUserBag;
  25. $this->couponRepository = $couponRepository;
  26. $this->userRepository = $userRepository;
  27. }
  28. public function getCouponsByUserIdAndStatus($user_id, $status, $order_type = 3)
  29. {
  30. // 查询前先更新一下失效的
  31. $this->updateCouponsStatus($user_id);
  32. if ($order_type == $this->model::ORDER_TYPE_NORMAL) {
  33. $order_type_arr = [
  34. $this->model::ORDER_TYPE_NORMAL,
  35. $this->model::ORDER_TYPE_ALL
  36. ];
  37. } elseif ($order_type == $this->model::ORDER_TYPE_RENT) {
  38. $order_type_arr = [
  39. $this->model::ORDER_TYPE_RENT,
  40. $this->model::ORDER_TYPE_ALL
  41. ];
  42. } elseif ($order_type == $this->model::ORDER_TYPE_ALL) {
  43. $order_type_arr = [
  44. $this->model::ORDER_TYPE_RENT,
  45. $this->model::ORDER_TYPE_NORMAL,
  46. $this->model::ORDER_TYPE_ALL
  47. ];
  48. }
  49. return $this->model->query()
  50. ->where('user_id', $user_id)
  51. ->where('status', $status)
  52. ->whereIn('order_type', $order_type_arr)
  53. ->where('created_at', '>', Carbon::now()->subMonths(3))
  54. ->get();
  55. }
  56. public function getCouponByUserIdAndID($user_id, $id)
  57. {
  58. return $this->model->query()
  59. ->where('id', $id)
  60. ->where('user_id', $user_id)
  61. ->where('status', CouponsUserBag::STATUS_OK)
  62. ->first();
  63. }
  64. /**
  65. * updateCouponsStatus 更新优惠券失效
  66. *
  67. * @param CouponsUserBag $coupon
  68. * @return void
  69. * @author Fx
  70. *
  71. */
  72. public function updateCouponsStatus($user_id)
  73. {
  74. $this->model->query()->where('user_id', $user_id)->where('status', CouponsUserBag::STATUS_OK)->where('valid_end_time', '<', Carbon::now()->toDateTimeString())->update(['status' => CouponsUserBag::STATUS_NO]);
  75. // if (count($coupon) !== 0) {
  76. // foreach ($coupon as $v) {
  77. // if ($v->valid_type == Coupon::VALID_TYPE_ABSOLUTELY) {
  78. // // 绝对时效
  79. // $valid_start_time_diff = Carbon::now()->diffInSeconds(Carbon::make($v->valid_start_time), false); //小于等于0有效 大于0无效
  80. // $valid_end_time_diff = Carbon::now()->diffInSeconds(Carbon::make($v->valid_end_time), false); //大于等于0有效 小于0无效
  81. // if ($valid_start_time_diff > 0 || $valid_end_time_diff < 0) {
  82. // $v->status = Coupon::STATUS_NO;
  83. // $v->save();
  84. // }
  85. //
  86. // }
  87. // if ($v->valid_type == Coupon::VALID_TYPE_RELATIVE) {
  88. // //相对失效
  89. // $overdue_time = Carbon::parse($v->valid_end_time);
  90. // $overdue_time_diff = Carbon::now()->diffInSeconds($overdue_time, false); // 大等于0有效
  91. // if ($overdue_time_diff < 0) {
  92. // $v->status = Coupon::STATUS_NO;
  93. // $v->save();
  94. // }
  95. // }
  96. // }
  97. // }
  98. }
  99. /**
  100. * 邀请新用户赠送优惠券 inviteNewRewardCoupon
  101. *
  102. * @param $coupon_id
  103. * @param $user_id
  104. * @return void
  105. * @author Fx
  106. *
  107. */
  108. public function inviteNewRewardCoupon($coupon_id, $user_id, $coupon_num)
  109. {
  110. $users = $this->userRepository->byIdGetModel($user_id);
  111. $coupon = $this->couponRepository->byIdGetModel($coupon_id);
  112. $data = [
  113. 'coupon_id' => $coupon_id,
  114. 'type' => CouponsUserBag::TYPE_INVITE_NEW_GIVE,
  115. 'user_id' => $users->id,
  116. 'coupons_data' => $coupon->toArray(),
  117. 'status' => CouponsUserBag::STATUS_OK,
  118. 'valid_type' => $coupon->valid_type,
  119. 'valid_start_time' => $coupon->valid_start_time,
  120. 'valid_end_time' => $coupon->valid_end_time,
  121. 'valid_days' => $coupon->valid_days,
  122. 'grant_start_at' => $coupon->grant_start_at,
  123. 'grant_end_at' => $coupon->grant_end_at,
  124. 'give_admin_id' => 0,
  125. 'order_type' => $coupon->order_type,
  126. 'merchant_id' => merchant_id()
  127. ];
  128. for ($coupon_num; $coupon_num > 0; $coupon_num--) {
  129. $this->model->create($data);
  130. $coupon->increment('take_count');
  131. }
  132. }
  133. /**
  134. * 根据用户获取用户免押金卡
  135. * @param $user_id
  136. * Author: Mead
  137. */
  138. public function isExistsByUserIdGetDepositFree($user_id)
  139. {
  140. return $this->model->query()->where('user_id', $user_id)->where('type', Coupon::TYPE_DEPOSIT_FREE)->where('status', CouponsUserBag::STATUS_OK)->exists();
  141. }
  142. }