UserCoupon.php 7.6 KB

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