Coupons.php 7.0 KB
<?php

namespace addons\groupon\model;

use think\Model;
use addons\groupon\exception\Exception;
use traits\model\SoftDelete;

/**
 * 优惠券模型
 */
class Coupons extends Model
{
    use SoftDelete;

    // 表名,不含前缀
    protected $name = 'groupon_coupons';
    // 自动写入时间戳字段
    protected $autoWriteTimestamp = 'int';
    // 定义时间戳字段名
    protected $createTime = 'createtime';
    protected $updateTime = 'updatetime';
    protected $deleteTime = 'deletetime';

    protected $hidden = ['createtime', 'updatetime', 'deletetime'];


    // 追加属性
    protected $append = [

    ];
    const COUPONS_CENTER = 0; // 领券中心
    const COUPONS_CAN_USE = 1; // 可使用
    const COUPONS_USED = 2; // 已使用
    const COUPONS_EXPIRED = 3; // 已失效

    public static function getCoupon($id)
    {
        $result = \think\Db::transaction(function () use ($id) {
            $coupon = self::canGet()->where('id', $id)->lock(true)->find();    // 新人券,或者普通券
            $user = User::info();
            if (!$coupon) {
                throw new Exception('未找到优惠券');
            }

            if ($coupon['gettimestart'] > time() || $coupon['gettimeend'] < time()) {
                throw new Exception('优惠券领取已结束');
            }

            $getList = UserCoupons::all([
                'user_id' => $user->id,
                'coupons_id' => $id
            ]);
            if (count($getList) >= $coupon->limit) {
                throw new Exception('您已经领取过');
            }

            if ($coupon->stock <= 0) {
                throw new Exception('优惠券已经被领完了');
            }
            $coupon->stock -= 1;
            $coupon->save();

            $result = UserCoupons::create([
                'user_id' => $user->id,
                'coupons_id' => $id,
            ]);
            return $result;
        });

        return $result;
    }

    public static function getCouponsListByIds($ids)
    {
        $couponsIdsArray = explode(',', $ids);
        $where = [
            'id' => ['in', $couponsIdsArray]
        ];
        $coupons = self::where($where)->select();
        return $coupons;


    }

    public static function getCouponsDetail($id, $user_coupons_id = 0)
    {
        $user = User::info();
        $coupon = self::where('id', $id)->find();

        // 查询并返回用户的优惠券状态
        if ($coupon && $user_coupons_id && $user) {
            $userCoupons = UserCoupons::where('id', $user_coupons_id)
                ->where('coupons_id', $coupon->id)
                ->where('user_id', $user->id)->find();

            if ($userCoupons) {
                $coupon->user_coupons_id = $userCoupons->id;

                if ($userCoupons->usetime) {
                    $coupon->status_code = 'used';
                    $coupon->status_name = '已使用';
                } else {
                    if ($coupon->usetimestart <= time() && $coupon->usetimeend >= time()) {
                        $coupon->status_code = 'no_use';
                        $coupon->status_name = '未使用';
                    } else if ($coupon->usetimeend <= time()) {
                        $coupon->status_code = 'expired';
                        $coupon->status_name = '已过期';
                    } else {
                        // 未到使用日期
                        $coupon->status_code = 'no_can_use';
                        $coupon->status_name = '不可使用';
                    }
                }
            }
        }

        return $coupon;
    }

    public static function getGoodsByCoupons($id)
    {
        $goodsIds = self::where('id', $id)->value('goods_ids');
        return Goods::getGoodsListByIds($goodsIds);


    }

    public static function getCouponsList($type)
    {
        $user = User::info();
        $couponsList = [];
        switch ($type) {
            case self::COUPONS_CENTER:
                $couponsList = self::canGet()->where([
                    'gettimestart' => ['elt', time()],
                    'gettimeend' => ['egt', time()]
                ])->select();
                break;
            case self::COUPONS_CAN_USE:
                $userCoupons = UserCoupons::where(['user_id' => $user->id, 'usetime' => null])->select();
                foreach ($userCoupons as $u) {
                    $coupon = self::get($u->coupons_id);
                    if ($coupon && $coupon->usetimestart <= time() && $coupon->usetimeend >= time()) {
                        $coupon->user_coupons_id = $u->id;
                        $coupon->status_code = 'no_use';
                        $coupon->status_name = '未使用';
                        $couponsList[] = $coupon;
                    }
                }

                break;
            case self::COUPONS_USED:
                $userCoupons = UserCoupons::where('user_id', $user->id)->where('usetime', 'not null')->select();
                foreach ($userCoupons as $u) {
                    $coupon = self::get($u->coupons_id);
                    if ($coupon) {
                        $coupon->user_coupons_id = $u->id;
                        $coupon->status_code = 'used';
                        $coupon->status_name = '已使用';
                        $couponsList[] = $coupon;
                    }
                }
                break;
            case self::COUPONS_EXPIRED:
                $userCoupons = UserCoupons::where(['user_id' => $user->id, 'usetime' => null])->select();
                foreach ($userCoupons as $u) {
                    $coupon = self::get($u->coupons_id);
                    if ($coupon && $coupon->usetimeend <= time()) {
                        $coupon->user_coupons_id = $u->id;
                        $coupon->status_code = 'expired';
                        $coupon->status_name = '已过期';
                        $couponsList[] = $coupon;
                    }
                }
                break;
        }

        return $couponsList;

    }

    public function scopeCanGet($query)
    {
        return $query->where('coupon_type', 'in', ['normal']);
    }

    public function scopeNormal($query)
    {
        return $query->where('coupon_type', 'normal');
    }


    public function scopeExclusive($query)
    {
        return $query->where('coupon_type', 'exclusive');
    }

    public function getUsetimeAttr($value, $data)
    {
        $usetimeArray = explode(' - ', $value);
        $usetime['start'] = strtotime($usetimeArray[0]);
        $usetime['end'] = strtotime($usetimeArray[1]);
        return $usetime;
    }

    public function getGettimeAttr($value, $data)
    {
        $gettimeArray = explode(' - ', $value);
        $gettime['start'] = strtotime($gettimeArray[0]);
        $gettime['end'] = strtotime($gettimeArray[1]);
        return $gettime;
    }

    //定义关联方法
    public function userCoupons(){
        //hasMany('租客表名','租客表外键','宿舍主键',['模型别名定义']);
        return $this->hasMany('userCoupons','coupons_id','id');
    }



}