Coupon.php 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112
  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\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. 'receive_num',
  29. 'is_delete',
  30. 'create_time',
  31. 'update_time',
  32. ];
  33. /**
  34. * 获取优惠券列表
  35. * @param int|null $limit 获取的数量
  36. * @param bool $onlyReceive 只显示可领取
  37. * @return mixed
  38. * @throws BaseException
  39. * @throws \think\db\exception\DataNotFoundException
  40. * @throws \think\db\exception\DbException
  41. * @throws \think\db\exception\ModelNotFoundException
  42. */
  43. public function getList(int $limit = null, bool $onlyReceive = false)
  44. {
  45. // 查询构造器
  46. $query = $this->getNewQuery();
  47. // 只显示可领取(未过期,未发完)的优惠券
  48. if ($onlyReceive) {
  49. $query->where('IF ( `total_num` > - 1, `receive_num` < `total_num`, 1 = 1 )')
  50. ->where('IF ( `expire_type` = 20, (`end_time` + 86400) >= ' . time() . ', 1 = 1 )');
  51. }
  52. // 查询数量
  53. $limit > 0 && $query->limit($limit);
  54. // 优惠券列表
  55. $couponList = $query->where('status', '=', 1)
  56. ->where('is_delete', '=', 0)
  57. ->order(['sort', 'create_time' => 'desc'])
  58. ->select();
  59. // 获取用户已领取的优惠券
  60. return $this->setIsReceive($couponList);
  61. }
  62. /**
  63. * 获取用户已领取的优惠券
  64. * @param mixed $couponList
  65. * @return mixed
  66. * @throws BaseException
  67. */
  68. private function setIsReceive($couponList)
  69. {
  70. // 获取用户已领取的优惠券
  71. $userInfo = UserService::getCurrentLoginUser();
  72. if ($userInfo !== false) {
  73. $UserCouponModel = new UserCoupon;
  74. $userCouponIds = $UserCouponModel->getUserCouponIds($userInfo['user_id']);
  75. foreach ($couponList as $key => $item) {
  76. $couponList[$key]['is_receive'] = in_array($item['coupon_id'], $userCouponIds);
  77. }
  78. }
  79. return $couponList;
  80. }
  81. /**
  82. * 验证优惠券是否可领取
  83. * @return bool
  84. */
  85. public function checkReceive(): bool
  86. {
  87. if ($this['total_num'] > -1 && $this['receive_num'] >= $this['total_num']) {
  88. $this->error = '优惠券已发完';
  89. return false;
  90. }
  91. if ($this['expire_type'] == 20 && ($this->getData('end_time') + 86400) < time()) {
  92. $this->error = '优惠券已过期';
  93. return false;
  94. }
  95. return true;
  96. }
  97. /**
  98. * 累计已领取数量
  99. * @return mixed
  100. */
  101. public function setIncReceiveNum()
  102. {
  103. return $this->setInc($this['coupon_id'], 'receive_num', 1);
  104. }
  105. }