UserCoupon.php 9.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250
  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\common\model\UserCoupon as UserCouponModel;
  15. use app\common\enum\coupon\CouponType as CouponTypeEnum;
  16. use app\common\enum\coupon\ExpireType as ExpireTypeEnum;
  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. // 当前用户信息
  111. $userInfo = UserService::getCurrentLoginUser(true);
  112. // 获取优惠券信息
  113. $coupon = Coupon::detail($couponId);
  114. // 验证优惠券是否可领取
  115. if (!$this->checkReceive($userInfo, $coupon)) {
  116. return false;
  117. }
  118. // 添加领取记录
  119. return $this->add($userInfo, $coupon);
  120. }
  121. /**
  122. * 添加领取记录
  123. * @param $user
  124. * @param Coupon $coupon
  125. * @return bool
  126. */
  127. private function add($user, Coupon $coupon): bool
  128. {
  129. // 计算有效期
  130. if ($coupon['expire_type'] == ExpireTypeEnum::RECEIVE) {
  131. $startTime = time();
  132. $endTime = $startTime + ($coupon['expire_day'] * 86400);
  133. } else {
  134. $startTime = $coupon->getData('start_time');
  135. $endTime = $coupon->getData('end_time');
  136. }
  137. // 整理领取记录
  138. $data = [
  139. 'coupon_id' => $coupon['coupon_id'],
  140. 'name' => $coupon['name'],
  141. 'coupon_type' => $coupon['coupon_type'],
  142. 'reduce_price' => $coupon['reduce_price'],
  143. 'discount' => $coupon['discount'],
  144. 'min_price' => $coupon['min_price'],
  145. 'expire_type' => $coupon['expire_type'],
  146. 'expire_day' => $coupon['expire_day'],
  147. 'start_time' => $startTime,
  148. 'end_time' => $endTime,
  149. 'apply_range' => $coupon['apply_range'],
  150. 'apply_range_config' => $coupon['apply_range_config'],
  151. 'user_id' => $user['user_id'],
  152. 'store_id' => self::$storeId
  153. ];
  154. // 事务处理
  155. return $this->transaction(function () use ($data, $coupon) {
  156. // 添加领取记录
  157. $status = $this->save($data);
  158. if ($status) {
  159. // 更新优惠券领取数量
  160. $coupon->setIncReceiveNum();
  161. }
  162. return $status;
  163. });
  164. }
  165. /**
  166. * 验证优惠券是否可领取
  167. * @param mixed $userInfo 当前登录用户信息
  168. * @param Coupon $coupon 优惠券详情
  169. * @return bool
  170. */
  171. private function checkReceive($userInfo, Coupon $coupon): bool
  172. {
  173. if (empty($coupon)) {
  174. $this->error = '优惠券不存在';
  175. return false;
  176. }
  177. if (!$coupon->checkReceive()) {
  178. $this->error = $coupon->getError();
  179. return false;
  180. }
  181. // 验证是否已领取
  182. $userCouponIds = $this->getUserCouponIds($userInfo['user_id']);
  183. if (in_array($coupon['coupon_id'], $userCouponIds)) {
  184. $this->error = '该优惠券已领取';
  185. return false;
  186. }
  187. return true;
  188. }
  189. /**
  190. * 订单结算优惠券列表
  191. * @param int $userId 用户id
  192. * @param float $orderPayPrice 订单商品总金额
  193. * @return array
  194. * @throws \think\db\exception\DbException
  195. */
  196. public static function getUserCouponList(int $userId, float $orderPayPrice): array
  197. {
  198. // 获取用户可用的优惠券列表
  199. $list = (new static)->getList($userId, ['dataType' => 'isUsable', 'amount' => $orderPayPrice]);
  200. $data = $list->isEmpty() ? [] : $list->toArray()['data'];
  201. foreach ($data as &$item) {
  202. // 计算最大能折扣的金额
  203. if ($item['coupon_type'] == CouponTypeEnum::DISCOUNT) {
  204. $reducePrice = helper::bcmul($orderPayPrice, $item['discount'] / 10);
  205. $item['reduced_price'] = helper::bcsub($orderPayPrice, $reducePrice);
  206. } else {
  207. $item['reduced_price'] = $item['reduce_price'];
  208. }
  209. }
  210. // 根据折扣金额排序并返回
  211. return !empty($data) ? array_sort($data, 'reduced_price', true) : [];
  212. }
  213. /**
  214. * 判断当前优惠券是否满足订单使用条件
  215. * @param array $couponList
  216. * @param array $orderGoodsIds 订单商品ID集
  217. * @return array
  218. */
  219. public static function couponListApplyRange(array $couponList, array $orderGoodsIds): array
  220. {
  221. // 名词解释(is_apply):允许用于抵扣当前订单
  222. foreach ($couponList as &$item) {
  223. if ($item['apply_range'] == ApplyRangeEnum::ALL) {
  224. // 1. 全部商品
  225. $item['is_apply'] = true;
  226. } elseif ($item['apply_range'] == ApplyRangeEnum::SOME) {
  227. // 2. 指定商品, 判断订单商品是否存在可用
  228. $applyGoodsIds = array_intersect($item['apply_range_config']['applyGoodsIds'], $orderGoodsIds);
  229. $item['is_apply'] = !empty($applyGoodsIds);
  230. } elseif ($item['apply_range'] == ApplyRangeEnum::EXCLUDE) {
  231. // 2. 排除商品, 判断订单商品是否全部都在排除行列
  232. $excludedGoodsIds = array_intersect($item['apply_range_config']['excludedGoodsIds'], $orderGoodsIds);
  233. $item['is_apply'] = count($excludedGoodsIds) != count($orderGoodsIds);
  234. }
  235. !$item['is_apply'] && $item['not_apply_info'] = '该优惠券不支持当前商品';
  236. }
  237. return $couponList;
  238. }
  239. }