<?php

namespace Modules\User\Models;

use App\Models\Model;
use App\Traits\HasStatus;
use GeneaLabs\LaravelModelCaching\Traits\Cachable;
use Illuminate\Support\Facades\Artisan;
use Illuminate\Support\Facades\Cache;
use Modules\User\Models\Traits\CertificationTrait;

class UserCertificationConfig extends Model
{
    use HasStatus,
        Cachable,
        CertificationTrait;

    const  IDCARD    = 1;
    const  PHONECARD = 2;

    const TYPE = [
        self::IDCARD    => '姓名，身份证验证',
        self::PHONECARD => '姓名，手机号，身份证号验证',
    ];

    public static function loading()
    {
        $config = Cache::rememberForever('userCertification', function () {
            $conf = self::shown()
                ->first();
            Artisan::call('queue:restart');

            return $conf ? $conf->toArray() : '';
        });
        config(['userCertification' => $config]);
    }

    protected static function boot()
    {
        parent::boot();

        self::saved(function ($model) {
            if ($model->status == 1) {
                Cache::forget('userCertification');
            }
        });
    }
}