UserCoupon.php 7.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206
  1. <?php
  2. // +----------------------------------------------------------------------
  3. // | 萤火商城系统 [ 致力于通过产品和服务,帮助商家高效化开拓市场 ]
  4. // +----------------------------------------------------------------------
  5. // | Copyright (c) 2017~2021 https://www.yiovo.com All rights reserved.
  6. // +----------------------------------------------------------------------
  7. // | Licensed 这不是一个自由软件,不允许对程序代码以任何形式任何目的的再发行
  8. // +----------------------------------------------------------------------
  9. // | Author: 萤火科技 <admin@yiovo.com>
  10. // +----------------------------------------------------------------------
  11. declare (strict_types=1);
  12. namespace app\api\model;
  13. use app\api\service\User as UserService;
  14. use app\api\model\Coupon as CouponModel;
  15. use app\common\model\UserCoupon as UserCouponModel;
  16. use app\common\enum\coupon\CouponType as CouponTypeEnum;
  17. use app\common\enum\coupon\ApplyRange as ApplyRangeEnum;
  18. use app\common\library\helper;
  19. use cores\exception\BaseException;
  20. /**
  21. * 用户优惠券模型
  22. * Class UserCoupon
  23. * @package app\api\model
  24. */
  25. class UserCoupon extends UserCouponModel
  26. {
  27. /**
  28. * 获取用户优惠券列表
  29. * @param int $userId
  30. * @param array $param
  31. * @return \think\Paginator
  32. * @throws \think\db\exception\DbException
  33. */
  34. public function getList(int $userId, array $param): \think\Paginator
  35. {
  36. $filter = $this->getFilter($param);
  37. return $this->where($filter)
  38. ->where('user_id', '=', $userId)
  39. ->paginate();
  40. }
  41. /**
  42. * 检索查询条件
  43. * @param array $param
  44. * @return array
  45. */
  46. private function getFilter(array $param = []): array
  47. {
  48. // 设置默认查询参数
  49. $params = $this->setQueryDefaultValue($param, [
  50. 'dataType' => 'all', // all:全部 isUsable:可用的 isExpire:已过期 isUse:已使用
  51. 'amount' => null, // 订单消费金额
  52. ]);
  53. // 检索列表类型
  54. $filter = [];
  55. // 可用的优惠券
  56. if ($params['dataType'] === 'isUsable') {
  57. $filter[] = ['is_use', '=', 0];
  58. $filter[] = ['is_expire', '=', 0];
  59. $filter[] = ['start_time', '<=', time()];
  60. $filter[] = ['end_time', '>', time()];
  61. }
  62. // 未使用的优惠券
  63. if ($params['dataType'] === 'isUnused') {
  64. $filter[] = ['is_use', '=', 0];
  65. $filter[] = ['is_expire', '=', 0];
  66. $filter[] = ['end_time', '>', time()];
  67. }
  68. // 已过期的优惠券
  69. if ($params['dataType'] === 'isExpire') {
  70. $filter[] = ['is_expire', '=', 1];
  71. }
  72. // 已使用的优惠券
  73. if ($params['dataType'] === 'isUse') {
  74. $filter[] = ['is_use', '=', 1];
  75. }
  76. // 订单消费金额
  77. $params['amount'] > 0 && $filter[] = ['min_price', '<=', $params['amount']];
  78. return $filter;
  79. }
  80. /**
  81. * 获取用户优惠券总数量(可用)
  82. * @param int $userId
  83. * @return int
  84. */
  85. public function getCount(int $userId): int
  86. {
  87. return $this->where('user_id', '=', $userId)
  88. ->where('is_use', '=', 0)
  89. ->where('is_expire', '=', 0)
  90. ->where('end_time', '>', time())
  91. ->count();
  92. }
  93. /**
  94. * 获取用户优惠券ID集
  95. * @param int $userId
  96. * @return array
  97. */
  98. public function getUserCouponIds(int $userId): array
  99. {
  100. return $this->where('user_id', '=', $userId)->column('coupon_id');
  101. }
  102. /**
  103. * 领取优惠券
  104. * @param int $couponId 优惠券ID
  105. * @return bool
  106. * @throws BaseException
  107. */
  108. public function receive(int $couponId): bool
  109. {
  110. // 当前用户ID
  111. $userId = UserService::getCurrentLoginUserId(true);
  112. // 获取优惠券信息
  113. $couponInfo = Coupon::detail($couponId);
  114. // 验证优惠券是否可领取
  115. if (!$this->checkReceive($userId, $couponInfo)) {
  116. return false;
  117. }
  118. // 添加领取记录
  119. return $this->add($userId, $couponInfo);
  120. }
  121. /**
  122. * 验证优惠券是否可领取
  123. * @param int $userId 当前用户ID
  124. * @param CouponModel $couponInfo 优惠券详情
  125. * @return bool
  126. */
  127. private function checkReceive(int $userId, CouponModel $couponInfo): bool
  128. {
  129. if (empty($couponInfo)) {
  130. $this->error = '当前优惠券不存在';
  131. return false;
  132. }
  133. // 验证优惠券状态是否可领取
  134. $model = new CouponModel;
  135. if (!$model->checkReceive($couponInfo)) {
  136. $this->error = $model->getError() ?: '优惠券状态不可领取';
  137. return false;
  138. }
  139. // 验证当前用户是否已领取
  140. if (static::checktUserCoupon($couponInfo['coupon_id'], $userId)) {
  141. $this->error = '当前用户已领取该优惠券';
  142. return false;
  143. }
  144. return true;
  145. }
  146. /**
  147. * 订单结算优惠券列表
  148. * @param int $userId 用户id
  149. * @param float $orderPayPrice 订单商品总金额
  150. * @return array
  151. * @throws \think\db\exception\DbException
  152. */
  153. public static function getUserCouponList(int $userId, float $orderPayPrice): array
  154. {
  155. // 获取用户可用的优惠券列表
  156. $list = (new static)->getList($userId, ['dataType' => 'isUsable', 'amount' => $orderPayPrice]);
  157. $data = $list->isEmpty() ? [] : $list->toArray()['data'];
  158. foreach ($data as &$item) {
  159. // 计算最大能折扣的金额
  160. if ($item['coupon_type'] == CouponTypeEnum::DISCOUNT) {
  161. $reducePrice = helper::bcmul($orderPayPrice, $item['discount'] / 10);
  162. $item['reduced_price'] = helper::bcsub($orderPayPrice, $reducePrice);
  163. } else {
  164. $item['reduced_price'] = $item['reduce_price'];
  165. }
  166. }
  167. // 根据折扣金额排序并返回
  168. return !empty($data) ? array_sort($data, 'reduced_price', true) : [];
  169. }
  170. /**
  171. * 判断当前优惠券是否满足订单使用条件
  172. * @param array $couponList
  173. * @param array $orderGoodsIds 订单商品ID集
  174. * @return array
  175. */
  176. public static function couponListApplyRange(array $couponList, array $orderGoodsIds): array
  177. {
  178. // 名词解释(is_apply):允许用于抵扣当前订单
  179. foreach ($couponList as &$item) {
  180. if ($item['apply_range'] == ApplyRangeEnum::ALL) {
  181. // 1. 全部商品
  182. $item['is_apply'] = true;
  183. } elseif ($item['apply_range'] == ApplyRangeEnum::SOME) {
  184. // 2. 指定商品, 判断订单商品是否存在可用
  185. $applyGoodsIds = array_intersect($item['apply_range_config']['applyGoodsIds'], $orderGoodsIds);
  186. $item['is_apply'] = !empty($applyGoodsIds);
  187. } elseif ($item['apply_range'] == ApplyRangeEnum::EXCLUDE) {
  188. // 2. 排除商品, 判断订单商品是否全部都在排除行列
  189. $excludedGoodsIds = array_intersect($item['apply_range_config']['excludedGoodsIds'], $orderGoodsIds);
  190. $item['is_apply'] = count($excludedGoodsIds) != count($orderGoodsIds);
  191. }
  192. !$item['is_apply'] && $item['not_apply_info'] = '该优惠券不支持当前商品';
  193. }
  194. return $couponList;
  195. }
  196. }