Coupon.php 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129
  1. <?php
  2. // +----------------------------------------------------------------------
  3. // | 萤火商城系统 [ 致力于通过产品和服务,帮助商家高效化开拓市场 ]
  4. // +----------------------------------------------------------------------
  5. // | Copyright (c) 2017~2025 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\Coupon as CouponModel;
  15. use app\common\enum\coupon\ApplyRange as ApplyRangeEnum;
  16. use cores\exception\BaseException;
  17. /**
  18. * 优惠券模型
  19. * Class Coupon
  20. * @package app\api\model
  21. */
  22. class Coupon extends CouponModel
  23. {
  24. /**
  25. * 隐藏字段
  26. * @var array
  27. */
  28. protected $hidden = [
  29. 'total_num',
  30. 'receive_num',
  31. 'status',
  32. 'is_delete',
  33. 'store_id',
  34. 'create_time',
  35. 'update_time',
  36. ];
  37. /**
  38. * 获取优惠券列表
  39. * @param int|null $limit 获取的数量
  40. * @param bool $onlyReceive 只获取可领取的优惠券
  41. * @param int|null $goodsId 只获取指定商品ID可用的优惠券
  42. * @param array|null $couponIds 指定优惠券ID集
  43. * @return array|\think\Collection
  44. * @throws BaseException
  45. * @throws \think\db\exception\DataNotFoundException
  46. * @throws \think\db\exception\DbException
  47. * @throws \think\db\exception\ModelNotFoundException
  48. */
  49. public function getList(int $limit = null, bool $onlyReceive = false, ?int $goodsId = null, ?array $couponIds = [])
  50. {
  51. // 查询构造器
  52. $query = $this->getNewQuery();
  53. // 只显示可领取(未过期,未发完)的优惠券
  54. if ($onlyReceive) {
  55. $query->where('IF ( `total_num` > - 1, `receive_num` < `total_num`, 1 = 1 )')
  56. ->where('IF ( `expire_type` = 20, (`end_time` + 86400) >= ' . time() . ', 1 = 1 )');
  57. }
  58. // 指定优惠券ID集
  59. if (!empty($couponIds)) {
  60. $query->where('coupon_id', 'in', $couponIds);
  61. $query->orderRaw('field(coupon_id, ' . implode(',', $couponIds) . ')');
  62. } else {
  63. $limit > 0 && $query->limit($limit);
  64. $query->order(['sort', 'create_time' => 'desc']);
  65. }
  66. // 优惠券列表
  67. $list = $query->where('status', '=', 1)
  68. ->where('is_delete', '=', 0)
  69. ->select();
  70. // 获取用户已领取的优惠券
  71. $list = $this->setIsReceive($list);
  72. // 筛选指定商品ID可用的优惠券
  73. return $this->screenByGoodsId($list, $goodsId);
  74. }
  75. /**
  76. * 筛选指定商品ID可用的优惠券
  77. * @param iterable $couponList
  78. * @param int|null $goodsId
  79. * @return array|iterable
  80. */
  81. private function screenByGoodsId(iterable $couponList, ?int $goodsId = null)
  82. {
  83. if (empty($goodsId)) {
  84. return $couponList;
  85. }
  86. $list = [];
  87. foreach ($couponList as $item) {
  88. // 优惠券指定商品可用
  89. if ($item['apply_range'] == ApplyRangeEnum::SOME
  90. && $goodsId > 0
  91. && !\in_array($goodsId, $item['apply_range_config']['goodsIds'])) {
  92. continue;
  93. }
  94. // 优惠券指定商品不可用
  95. if ($item['apply_range'] == ApplyRangeEnum::EXCLUDE
  96. && $goodsId > 0
  97. && \in_array($goodsId, $item['apply_range_config']['goodsIds'])) {
  98. continue;
  99. }
  100. $list[] = $item;
  101. }
  102. return $list;
  103. }
  104. /**
  105. * 获取用户已领取的优惠券
  106. * @param mixed $couponList
  107. * @return \think\Collection
  108. * @throws BaseException
  109. */
  110. private function setIsReceive($couponList)
  111. {
  112. // 获取用户已领取的优惠券
  113. $userInfo = UserService::getCurrentLoginUser(false);
  114. if ($userInfo !== false) {
  115. $UserCouponModel = new UserCoupon;
  116. $userCouponIds = $UserCouponModel->getUserCouponIds($userInfo['user_id']);
  117. foreach ($couponList as $key => $item) {
  118. $couponList[$key]['is_receive'] = in_array($item['coupon_id'], $userCouponIds);
  119. }
  120. }
  121. return $couponList;
  122. }
  123. }