<?php

namespace App\Models;

use Illuminate\Foundation\Auth\User as Authenticatable;
use Illuminate\Database\Eloquent\Factories\HasFactory;
use Illuminate\Notifications\Notifiable;
use Illuminate\Support\Facades\DB;
use Tymon\JWTAuth\Contracts\JWTSubject;

/**
 * This is the model class for table "uc_employee_t".
 *
 * @property $id bigint
 * @property $username varchar
 * @property $nickname varchar
 * @property $password varchar
 * @property $company_id int
 * @property $group_id int
 * @property $phone bigint
 * @property $monthly_working_days int
 * @property $entry_date date
 * @property $official_date date
 * @property $resign_date date
 * @property $probation_salary decimal
 * @property $official_salary decimal
 * @property $is_outer tinyint
 * @property $status tinyint
 * @property $rules varchar
 * @property $created_at timestamp
 * @property $updated_at timestamp
 */
class EmployeeT extends Authenticatable implements JWTSubject
{
    use HasFactory, Notifiable;

    protected $table = 'uc_employee_t';

    public $attrs = [
        'username' => ['name' => 'username', 'title' => '员工工号', 'type' => 'varchar', 'is_must' => '1'],
        'nickname' => ['name' => 'nickname', 'title' => '员工姓名', 'type' => 'varchar', 'is_must' => '1'],
        'password' => ['name' => 'password', 'title' => 'password', 'type' => 'varchar', 'is_must' => '1'],
        'company_id' => ['name' => 'company_id', 'title' => '公司ID', 'type' => 'int', 'is_must' => '0'],
        'group_id' => ['name' => 'group_id', 'title' => '岗位ID', 'type' => 'int', 'is_must' => '0'],
        'phone' => ['name' => 'phone', 'title' => '联系电话', 'type' => 'bigint', 'is_must' => '0'],
        'monthly_working_days' => ['name' => 'monthly_working_days', 'title' => '月工作天数', 'type' => 'int', 'is_must' => '0'],
        'entry_date' => ['name' => 'entry_date', 'title' => '入职日期', 'type' => 'date', 'is_must' => '0'],
        'official_date' => ['name' => 'official_date', 'title' => '转正日期', 'type' => 'date', 'is_must' => '0'],
        'resign_date' => ['name' => 'resign_date', 'title' => '离职日期', 'type' => 'date', 'is_must' => '0'],
        'probation_salary' => ['name' => 'probation_salary', 'title' => '试用工资', 'type' => 'decimal', 'is_must' => '0'],
        'official_salary' => ['name' => 'official_salary', 'title' => '转正工资', 'type' => 'decimal', 'is_must' => '0'],
        'is_outer' => ['name' => 'is_outer', 'title' => '外网登录', 'type' => 'tinyint', 'is_must' => '0'],
        'status' => ['name' => 'status', 'title' => '员工状态', 'type' => 'tinyint', 'is_must' => '0'],
        'rules' => ['name' => 'rules', 'title' => '规则id,多个规则 , 隔开', 'type' => 'varchar', 'is_must' => '0'],
    ];

    /**
     * @inheritDoc
     */
    public function getJWTIdentifier()
    {
        // TODO: Implement getJWTIdentifier() method.
        return $this->getKey();
    }

    /**
     * @inheritDoc
     */
    public function getJWTCustomClaims()
    {
        // TODO: Implement getJWTCustomClaims() method.
        return ['auth_list' => $this->getAuthList()];
    }

    public function getAuthList()
    {
        $config = config('auth.conf');
        $user_auth_ids = DB::table($config['auth_group'])
            ->where('id', $this->group_id)
            ->value('rules');
        $ids = array_unique(explode(',', $user_auth_ids . ',' . $this->rules));
        if (empty($ids)) {
            return array();
        }

        //读取用户组所有权限规则
        $rules = DB::table($config['auth_rule'])
            ->whereIn('id',$ids)
            ->where('status',1)->select(['condition','name','module','method'])->get();

        //循环规则,判断结果。
        $authList = array();   //
        foreach ($rules as $rule) {
            if (!empty($rule->condition)) { //根据condition进行验证
                $command = preg_replace('/\{(\w*?)\}/', '$user[\'\\1\']', $rule->condition);
                $condition = false;
                @(eval('$condition=(' . $command . ');'));
                if ($condition) {
                    $authList[] = strtolower($rule->module .'_'. $rule->method .'_'. $rule->name);
                }
            } else {
                //只要存在就记录
                $authList[] = strtolower($rule->module .'_'. $rule->method .'_'. $rule->name);
            }
        }
        return array_unique($authList);
    }
}