Coupon.php 3.0 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889
  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\common\model\Coupon as CouponModel;
  15. use cores\exception\BaseException;
  16. /**
  17. * 优惠券模型
  18. * Class Coupon
  19. * @package app\api\model
  20. */
  21. class Coupon extends CouponModel
  22. {
  23. /**
  24. * 隐藏字段
  25. * @var array
  26. */
  27. protected $hidden = [
  28. 'total_num',
  29. 'receive_num',
  30. 'status',
  31. 'is_delete',
  32. 'store_id',
  33. 'create_time',
  34. 'update_time',
  35. ];
  36. /**
  37. * 获取优惠券列表
  38. * @param int|null $limit 获取的数量
  39. * @param bool $onlyReceive 只显示可领取
  40. * @return array|\think\Collection
  41. * @throws BaseException
  42. * @throws \think\db\exception\DataNotFoundException
  43. * @throws \think\db\exception\DbException
  44. * @throws \think\db\exception\ModelNotFoundException
  45. */
  46. public function getList(int $limit = null, bool $onlyReceive = false)
  47. {
  48. // 查询构造器
  49. $query = $this->getNewQuery();
  50. // 只显示可领取(未过期,未发完)的优惠券
  51. if ($onlyReceive) {
  52. $query->where('IF ( `total_num` > - 1, `receive_num` < `total_num`, 1 = 1 )')
  53. ->where('IF ( `expire_type` = 20, (`end_time` + 86400) >= ' . time() . ', 1 = 1 )');
  54. }
  55. // 查询数量
  56. $limit > 0 && $query->limit($limit);
  57. // 优惠券列表
  58. $couponList = $query->where('status', '=', 1)
  59. ->where('is_delete', '=', 0)
  60. ->order(['sort', 'create_time' => 'desc'])
  61. ->select();
  62. // 获取用户已领取的优惠券
  63. return $this->setIsReceive($couponList);
  64. }
  65. /**
  66. * 获取用户已领取的优惠券
  67. * @param mixed $couponList
  68. * @return \think\Collection
  69. * @throws BaseException
  70. */
  71. private function setIsReceive($couponList)
  72. {
  73. // 获取用户已领取的优惠券
  74. $userInfo = UserService::getCurrentLoginUser();
  75. if ($userInfo !== false) {
  76. $UserCouponModel = new UserCoupon;
  77. $userCouponIds = $UserCouponModel->getUserCouponIds($userInfo['user_id']);
  78. foreach ($couponList as $key => $item) {
  79. $couponList[$key]['is_receive'] = in_array($item['coupon_id'], $userCouponIds);
  80. }
  81. }
  82. return $couponList;
  83. }
  84. }