Coupons.php 8.8 KB
<?php

namespace addons\shopro\model;

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

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

    // 表名,不含前缀
    protected $name = 'shopro_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)
    {
        $coupon = self::get($id);
        $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;
    }

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


    }

    public static function getCouponsDetail($id, $user_coupons_id = 0)
    {
        $user = User::info();
        $coupon = self::get($id);

        // 查询并返回用户的优惠券状态
        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($userCoupons->package_coupons_time > 0){
                        if ($userCoupons->package_coupons_time >= time()) {
                            $coupon->status_code = 'no_use';
                            $coupon->status_name = '未使用';
                        } else {
                            $coupon->status_code = 'expired';
                            $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::all([
                    'gettimestart' => ['elt', time()],
                    'gettimeend' => ['egt', time()]
                ]);
                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) {
                        if($u->package_coupons_time > 0){
                            // 开通会员后赠送的优惠券
                            if($u->package_coupons_time >= time()){
                                $coupon->user_coupons_id = $u->id;
                                $coupon->status_code = 'no_use';
                                $coupon->status_name = '未使用';
                                $couponsList[] = $coupon;
                            }
                        }else if($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->usetimestart <= time() && $coupon->usetimeend >= time()) {
                    $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){
                        if($u->package_coupons_time > 0){
                            // 开通会员后赠送的优惠券
                            if($u->package_coupons_time <= time()){
                                $coupon->user_coupons_id = $u->id;
                                $coupon->status_code = 'expired';
                                $coupon->status_name = '已过期';
                                $couponsList[] = $coupon;
                            }
                        }else if($coupon->usetimeend <= time()){
                            // 正常领取的优惠券
                            $coupon->user_coupons_id = $u->id;
                            $coupon->status_code = 'expired';
                            $coupon->status_name = '已过期';
                            $couponsList[] = $coupon;
                        }
                    }
                }
                break;
        }

        foreach ($couponsList as $v){
            if(!empty($v->user_coupons_id)){
                $user_coupons = UserCoupons::get($v->user_coupons_id);
                if($user_coupons && $user_coupons['package_coupons_time'] > 0){
                    $v->usetime = [
                        'start' => date('Y.m.d',$user_coupons['createtime']),
                        'end' => date('Y.m.d',$user_coupons['package_coupons_time']),
                    ];
                }
            }
        }

        return $couponsList;

    }

    public function getUsetimeAttr($value, $data)
    {
        if(is_array($value)){
            return $value;
        }
        $usetimeArray = explode(' - ', $value);
        $usetime['start'] = date('Y.m.d',strtotime($usetimeArray[0]));
        $usetime['end'] = date('Y.m.d',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');
    }



}