CouponUserBagsRepository.php 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146
  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. $update = $this->model->where('user_id', $user_id)->where('status', Coupon::STATUS_OK)->get();
  32. $this->updateCouponsStatus($update);
  33. if ($order_type == $this->model::ORDER_TYPE_NORMAL) {
  34. $order_type_arr = [
  35. $this->model::ORDER_TYPE_NORMAL,
  36. $this->model::ORDER_TYPE_ALL
  37. ];
  38. } elseif ($order_type == $this->model::ORDER_TYPE_RENT) {
  39. $order_type_arr = [
  40. $this->model::ORDER_TYPE_RENT,
  41. $this->model::ORDER_TYPE_ALL
  42. ];
  43. } elseif ($order_type == $this->model::ORDER_TYPE_ALL) {
  44. $order_type_arr = [
  45. $this->model::ORDER_TYPE_RENT,
  46. $this->model::ORDER_TYPE_NORMAL,
  47. $this->model::ORDER_TYPE_ALL
  48. ];
  49. }
  50. return $this->model->query()
  51. ->where('user_id', $user_id)
  52. ->where('status', $status)
  53. ->whereIn('order_type', $order_type_arr)
  54. ->where('created_at', '>', Carbon::now()->subMonths(3))
  55. ->get();
  56. }
  57. public function getCouponByUserIdAndID($user_id, $id)
  58. {
  59. return $this->model->query()
  60. ->where('id', $id)
  61. ->where('user_id', $user_id)
  62. ->where('status', CouponsUserBag::STATUS_OK)
  63. ->first();
  64. }
  65. /**
  66. * updateCouponsStatus 更新优惠券失效
  67. *
  68. * @param CouponsUserBag $coupon
  69. * @return void
  70. * @author Fx
  71. *
  72. */
  73. public function updateCouponsStatus($coupon)
  74. {
  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. }
  84. } else {
  85. $overdue_time = Carbon::parse($v->created_at)->addDays($v->valid_days);
  86. $overdue_time_diff = Carbon::now()->diffInSeconds($overdue_time, false); // 大等于0有效
  87. if ($overdue_time_diff < 0) {
  88. $v->status = Coupon::STATUS_NO;
  89. }
  90. }
  91. $v->save();
  92. }
  93. }
  94. }
  95. /**
  96. * 邀请新用户赠送优惠券 inviteNewRewardCoupon
  97. *
  98. * @param $coupon_id
  99. * @param $user_id
  100. * @return void
  101. * @author Fx
  102. *
  103. */
  104. public function inviteNewRewardCoupon($coupon_id, $user_id, $coupon_num)
  105. {
  106. $users = $this->userRepository->byIdGetModel($user_id);
  107. $coupon = $this->couponRepository->byIdGetModel($coupon_id);
  108. $data = [
  109. 'coupon_id' => $coupon_id,
  110. 'type' => CouponsUserBag::TYPE_INVITE_NEW_GIVE,
  111. 'user_id' => $users->id,
  112. 'coupons_data' => $coupon->toArray(),
  113. 'status' => CouponsUserBag::STATUS_OK,
  114. 'valid_type' => $coupon->valid_type,
  115. 'valid_start_time' => $coupon->valid_start_time,
  116. 'valid_end_time' => $coupon->valid_end_time,
  117. 'valid_days' => $coupon->valid_days,
  118. 'grant_start_at' => $coupon->grant_start_at,
  119. 'grant_end_at' => $coupon->grant_end_at,
  120. 'give_admin_id' => 0,
  121. 'order_type' => $coupon->order_type
  122. ];
  123. for ($coupon_num; $coupon_num > 0; $coupon_num--) {
  124. $this->model->create($data);
  125. $coupon->increment('take_count');
  126. }
  127. }
  128. }