CouponManager.php 5.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209
  1. <?php
  2. namespace App\Managers;
  3. use App\Exceptions\AlertException;
  4. use App\Http\Controllers\Core\Auth;
  5. use App\Http\Controllers\Order;
  6. use App\Models\CouponModel;
  7. class CouponManager
  8. {
  9. /**
  10. * @var CouponModel
  11. */
  12. protected $coupon;
  13. public function __construct()
  14. {
  15. $this->coupon = new CouponModel();
  16. }
  17. /**
  18. * 使用券
  19. * @param $orderId
  20. * @param $couponId
  21. */
  22. public function use($orderId, $couponId)
  23. {
  24. $coupon = $this->coupon->find($couponId);
  25. $coupon->use($orderId, $couponId);
  26. }
  27. /**
  28. * 未支付返还优惠券
  29. * @param $couponId
  30. */
  31. public function cancelUse($couponId)
  32. {
  33. $coupon = $this->coupon->find($couponId);
  34. $coupon->order_id = 0;
  35. $coupon->used_at = 0;
  36. $coupon->save();
  37. }
  38. /**
  39. * 计算优惠券
  40. * @param int $goodsId
  41. * @param int $couponId
  42. * @return int
  43. * @throws AlertException
  44. */
  45. public function getSettlementAmount(int $goodsId, int $couponId)
  46. {
  47. $uid = Auth::auth();
  48. $coupon = $this->coupon->find($couponId);
  49. if (!$coupon->isOwner($uid)) {
  50. throw new AlertException("兑换码不正确哦");
  51. }
  52. if ($coupon->isUsed()) {
  53. throw new AlertException("卡券已使用");
  54. }
  55. if (!$coupon->isOpened()) {
  56. throw new AlertException("卡券未到使用时间");
  57. }
  58. if ($coupon->isOverdue()) {
  59. throw new AlertException("卡券过期啦");
  60. }
  61. if (!$coupon->isGoodsScope($goodsId)) {
  62. throw new AlertException("该商品/业务不可使用此优惠券");
  63. }
  64. if (!$coupon->isSatisfyPrice($goodsId)) {
  65. throw new AlertException("未满优惠券使用金额");
  66. }
  67. $goods = Order::GOODS[$goodsId];
  68. $total_amount = $goods['total_fee'];
  69. switch ($coupon->type) {
  70. case "无门槛券":
  71. case "72小时入场券":
  72. case "满减券":
  73. $coupon_amount = ($coupon->coupon_amount > $total_amount)
  74. ? $total_amount
  75. : $coupon->coupon_amount;
  76. break;
  77. case "折扣券":
  78. $coupon_amount = floor($goods['total_fee'] * (1 - $coupon->discount / 10) / 10) * 10;
  79. break;
  80. default:
  81. throw new AlertException("未定义的卡券");
  82. }
  83. $settlement_total_amount = (int)($total_amount - $coupon_amount);
  84. return $settlement_total_amount;
  85. }
  86. /**
  87. * 开通SuperVip送72小时入场券
  88. * @param $uid
  89. * @param int $number
  90. * @return bool
  91. */
  92. public function generateSuperVip72FreeCoupons($uid, $number = 1)
  93. {
  94. $batchId = "vip" . date("Ym");
  95. $attributes = [
  96. 'uid' => $uid,
  97. 'goods_scope' => 'pair',
  98. 'type' => '72小时入场券',
  99. 'name' => '72小时CP直升入场券',
  100. 'describe' => 'SVIP专属,直升全场最高匹配成功率',
  101. 'coupon_amount' => 990,
  102. 'min_amount' => 990,
  103. 'get_at' => time(),
  104. 'open_at' => time(),
  105. 'overdue_at' => now()->addWeek(1)->addHour(12)->getTimestamp(),
  106. 'batch_id' => $batchId,
  107. ];
  108. return $this->generateCoupons($attributes, $number);
  109. }
  110. /**
  111. * 签到送超级会员满减折扣券
  112. * @param $uid
  113. * @param int $number
  114. * @return bool
  115. */
  116. public function generateSignSuperVipCoupons($uid, $number = 1)
  117. {
  118. $batchId = "sign" . date("Ym");
  119. $attributes = [
  120. 'uid' => $uid,
  121. 'goods_scope' => 'pair',
  122. 'type' => '72小时入场券',
  123. 'name' => '72小时CP直升入场券',
  124. 'describe' => 'SVIP专属,直升全场最高匹配成功率',
  125. 'coupon_amount' => 990,
  126. 'min_amount' => 990,
  127. 'get_at' => time(),
  128. 'open_at' => time(),
  129. 'overdue_at' => now()->addWeek(1)->addHour(12)->getTimestamp(),
  130. 'batch_id' => $batchId,
  131. ];
  132. return $this->generateCoupons($attributes, $number);
  133. }
  134. /**
  135. * 签到送72小时抵扣券
  136. * @param $uid
  137. * @param int $number
  138. * @return bool
  139. */
  140. public function generateSign72DiscountCoupons($uid, $number = 1)
  141. {
  142. $batchId = "sign" . date("Ym");
  143. $attributes = [
  144. 'uid' => $uid,
  145. 'goods_scope' => 'pair',
  146. 'type' => '折扣券',
  147. 'name' => '72小时CP报名折扣券',
  148. 'describe' => '非免费入场时,可享受相应折扣',
  149. 'discount' => 6.8,
  150. 'min_amount' => 990,
  151. 'get_at' => time(),
  152. 'open_at' => time(),
  153. 'overdue_at' => now()->addMonth(1)->getTimestamp(),
  154. 'batch_id' => $batchId,
  155. ];
  156. return $this->generateCoupons($attributes, $number);
  157. }
  158. /**
  159. * 生成券
  160. * @param array $attributes
  161. * @param int $number
  162. * @return bool
  163. */
  164. public function generateCoupons(array $attributes, int $number = 1)
  165. {
  166. $data = [];
  167. while ($number) {
  168. $attributes['created_at'] = time();
  169. $attributes['updated_at'] = time();
  170. $attributes['code'] = \getChars(8);
  171. array_push($data, $attributes);
  172. $number--;
  173. }
  174. return $this->coupon->insert($data);
  175. }
  176. }