<?php

namespace app\common\model\coupon;
use app\common\model\BaseModel;
use app\common\model\dict\DictData;
use think\facade\Cache;

/**
 * @author 林海涛
 * @date 2024/7/18 下午3:42
 */

class UserCoupon extends BaseModel
{

    protected $name = 'user_coupon';

    public function couponCategory()
    {
        return $this->hasMany(CouponCategory::class, 'coupon_id', 'goods_category_id');
    }

    public function couponRules()
    {
        return $this->hasOne(CouponRules::class,'code','code');
    }
    public function getVoucherStatusTextAttr($value,$data):string
    {
        $default = Cache::get('VOUCHER_STATUS');
        if (!$default) {
            $status = DictData::whereIn('type_value', 'voucher_status')->column('name','value');
            Cache::set('VOUCHER_STATUS', json_encode($status,true),5);
        } else {
            $status = json_decode($default,true);
        }
        return $status[$data['voucher_status']] ?? '';
    }

    public function getDiscountRatioTextAttr($value,$data)
    {
        return !empty($data['discount_ratio'])?$data['discount_ratio']*10:'';
    }

}