123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687 |
- <?php
- // +----------------------------------------------------------------------
- // | 萤火商城系统 [ 致力于通过产品和服务,帮助商家高效化开拓市场 ]
- // +----------------------------------------------------------------------
- // | Copyright (c) 2017~2021 https://www.yiovo.com All rights reserved.
- // +----------------------------------------------------------------------
- // | Licensed 这不是一个自由软件,不允许对程序代码以任何形式任何目的的再发行
- // +----------------------------------------------------------------------
- // | Author: 萤火科技 <admin@yiovo.com>
- // +----------------------------------------------------------------------
- declare (strict_types=1);
- namespace app\index\model;
- use app\index\model\UserCoupon;
- use app\index\service\User as UserService;
- use app\common\model\Coupon as CouponModel;
- use cores\exception\BaseException;
- /**
- * 优惠券模型
- * Class Coupon
- * @package app\api\model
- */
- class Coupon extends CouponModel
- {
- /**
- * 隐藏字段
- * @var array
- */
- protected $hidden = [
- 'receive_num',
- 'is_delete',
- 'create_time',
- 'update_time',
- ];
- /**
- * 获取优惠券列表
- * @param int|null $limit 获取的数量
- * @param bool $onlyReceive 只显示可领取
- * @return mixed
- * @throws BaseException
- * @throws \think\db\exception\DataNotFoundException
- * @throws \think\db\exception\DbException
- * @throws \think\db\exception\ModelNotFoundException
- */
- public function getList(int $limit = null, bool $onlyReceive = false)
- {
- // 查询构造器
- $query = $this->getNewQuery();
- // 只显示可领取(未过期,未发完)的优惠券
- if ($onlyReceive) {
- $query->where('IF ( `total_num` > - 1, `receive_num` < `total_num`, 1 = 1 )')
- ->where('IF ( `expire_type` = 20, (`end_time` + 86400) >= ' . time() . ', 1 = 1 )');
- }
- // 查询数量
- $limit > 0 && $query->limit($limit);
- // 优惠券列表
- $couponList = $query->where('status', '=', 1)
- ->where('is_delete', '=', 0)
- ->order(['sort', 'create_time' => 'desc'])
- ->select();
- // 获取用户已领取的优惠券
- return $this->setIsReceive($couponList);
- }
- /**
- * 获取用户已领取的优惠券
- * @param mixed $couponList
- * @return mixed
- * @throws BaseException
- */
- private function setIsReceive($couponList)
- {
- // 获取用户已领取的优惠券
- $userInfo = UserService::getCurrentLoginUser();
- if ($userInfo !== false) {
- $UserCouponModel = new UserCoupon;
- $userCouponIds = $UserCouponModel->getUserCouponIds($userInfo['user_id']);
- foreach ($couponList as $key => $item) {
- $couponList[$key]['is_receive'] = in_array($item['coupon_id'], $userCouponIds);
- }
- }
- return $couponList;
- }
- }
|