UserCoupon.php 6.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190
  1. <?php
  2. namespace app\api\model;
  3. use app\common\library\helper;
  4. use app\common\model\UserCoupon as UserCouponModel;
  5. /**
  6. * 用户优惠券模型
  7. * Class UserCoupon
  8. * @package app\api\model
  9. */
  10. class UserCoupon extends UserCouponModel
  11. {
  12. /**
  13. * 获取用户优惠券列表
  14. * @param $user_id
  15. * @param bool $is_use 是否已使用
  16. * @param bool $is_expire 是否已过期
  17. * @return false|\PDOStatement|string|\think\Collection
  18. * @throws \think\db\exception\DataNotFoundException
  19. * @throws \think\db\exception\ModelNotFoundException
  20. * @throws \think\exception\DbException
  21. */
  22. public function getList($user_id, $is_use = false, $is_expire = false)
  23. {
  24. return $this->where('user_id', '=', $user_id)
  25. ->where('is_use', '=', $is_use ? 1 : 0)
  26. ->where('is_expire', '=', $is_expire ? 1 : 0)
  27. ->select();
  28. }
  29. /**
  30. * 获取用户优惠券总数量(可用)
  31. * @param $user_id
  32. * @return int|string
  33. * @throws \think\Exception
  34. */
  35. public function getCount($user_id)
  36. {
  37. return $this->where('user_id', '=', $user_id)
  38. ->where('is_use', '=', 0)
  39. ->where('is_expire', '=', 0)
  40. ->count();
  41. }
  42. /**
  43. * 获取用户优惠券ID集
  44. * @param $user_id
  45. * @return array
  46. */
  47. public function getUserCouponIds($user_id)
  48. {
  49. return $this->where('user_id', '=', $user_id)->column('coupon_id');
  50. }
  51. /**
  52. * 领取优惠券
  53. * @param $user
  54. * @param $coupon_id
  55. * @return bool|false|int
  56. * @throws \think\exception\DbException
  57. */
  58. public function receive($user, $coupon_id)
  59. {
  60. // 获取优惠券信息
  61. $coupon = Coupon::detail($coupon_id);
  62. // 验证优惠券是否可领取
  63. if (!$this->checkReceive($user, $coupon)) {
  64. return false;
  65. }
  66. // 添加领取记录
  67. return $this->add($user, $coupon);
  68. }
  69. /**
  70. * 添加领取记录
  71. * @param $user
  72. * @param Coupon $coupon
  73. * @return bool
  74. */
  75. private function add($user, $coupon)
  76. {
  77. // 计算有效期
  78. if ($coupon['expire_type'] == 10) {
  79. $start_time = time();
  80. $end_time = $start_time + ($coupon['expire_day'] * 86400);
  81. } else {
  82. $start_time = $coupon['start_time']['value'];
  83. $end_time = $coupon['end_time']['value'];
  84. }
  85. // 整理领取记录
  86. $data = [
  87. 'coupon_id' => $coupon['coupon_id'],
  88. 'name' => $coupon['name'],
  89. 'color' => $coupon['color']['value'],
  90. 'coupon_type' => $coupon['coupon_type']['value'],
  91. 'reduce_price' => $coupon['reduce_price'],
  92. 'discount' => $coupon->getData('discount'),
  93. 'min_price' => $coupon['min_price'],
  94. 'expire_type' => $coupon['expire_type'],
  95. 'expire_day' => $coupon['expire_day'],
  96. 'start_time' => $start_time,
  97. 'end_time' => $end_time,
  98. 'apply_range' => $coupon['apply_range'],
  99. 'user_id' => $user['user_id'],
  100. 'wxapp_id' => self::$wxapp_id
  101. ];
  102. return $this->transaction(function () use ($data, $coupon) {
  103. // 添加领取记录
  104. $status = $this->save($data);
  105. if ($status) {
  106. // 更新优惠券领取数量
  107. $coupon->setIncReceiveNum();
  108. }
  109. return $status;
  110. });
  111. }
  112. /**
  113. * 验证优惠券是否可领取
  114. * @param $user
  115. * @param Coupon $coupon
  116. * @return bool
  117. */
  118. private function checkReceive($user, $coupon)
  119. {
  120. if (!$coupon) {
  121. $this->error = '优惠券不存在';
  122. return false;
  123. }
  124. if (!$coupon->checkReceive()) {
  125. $this->error = $coupon->getError();
  126. return false;
  127. }
  128. // 验证是否已领取
  129. $userCouponIds = $this->getUserCouponIds($user['user_id']);
  130. if (in_array($coupon['coupon_id'], $userCouponIds)) {
  131. $this->error = '该优惠券已领取';
  132. return false;
  133. }
  134. return true;
  135. }
  136. /**
  137. * 订单结算优惠券列表
  138. * @param int $user_id 用户id
  139. * @param double $orderPayPrice 订单商品总金额
  140. * @return mixed
  141. * @throws \think\db\exception\DataNotFoundException
  142. * @throws \think\db\exception\ModelNotFoundException
  143. * @throws \think\exception\DbException
  144. */
  145. public static function getUserCouponList($user_id, $orderPayPrice)
  146. {
  147. // todo: 新增筛选条件: 最低消费金额
  148. // 获取用户可用的优惠券列表
  149. $list = (new self)->getList($user_id);
  150. $data = [];
  151. foreach ($list as $coupon) {
  152. // 最低消费金额
  153. if ($orderPayPrice < $coupon['min_price']) continue;
  154. // 有效期范围内
  155. if ($coupon['start_time']['value'] > time()) continue;
  156. $key = $coupon['user_coupon_id'];
  157. $data[$key] = [
  158. 'user_coupon_id' => $coupon['user_coupon_id'],
  159. 'name' => $coupon['name'],
  160. 'color' => $coupon['color'],
  161. 'coupon_type' => $coupon['coupon_type'],
  162. 'reduce_price' => $coupon['reduce_price'],
  163. 'discount' => $coupon['discount'],
  164. 'min_price' => $coupon['min_price'],
  165. 'expire_type' => $coupon['expire_type'],
  166. 'start_time' => $coupon['start_time'],
  167. 'end_time' => $coupon['end_time'],
  168. ];
  169. // 计算打折金额
  170. if ($coupon['coupon_type']['value'] == 20) {
  171. // $reduce_price = $orderPayPrice * ($coupon['discount'] / 10);
  172. $reducePrice = helper::bcmul($orderPayPrice, $coupon['discount'] / 10);
  173. $data[$key]['reduced_price'] = bcsub($orderPayPrice, $reducePrice, 2);
  174. } else
  175. $data[$key]['reduced_price'] = $coupon['reduce_price'];
  176. }
  177. // 根据折扣金额排序并返回
  178. return array_sort($data, 'reduced_price', true);
  179. }
  180. }