<?php

namespace app\common\model\User;

use app\BasePivot;
use app\common\model\Role\Role;
use think\model\concern\SoftDelete;
use app\common\arw\adjfut\src\Model\Pivot as ModelPivot;

class UserRole extends BasePivot
{
    // use SoftDelete;
    // 删除字段
    protected $deleteTime = 'user_role_delete_time';
    // 设置主键名
    protected $pk = 'user_role_guid';
    // 设置废弃字段
    protected $disuse = [];
    // 设置字段信息
    protected $schema = [
        'user_role_id'          => 'int',
        'user_guid'      => 'string',
        'role_guid'      => 'string',
        'user_role_status'      => 'int',
        'user_role_create_time' => 'datetime',
        'user_role_create_user_guid' => 'string',
        'user_role_update_time' => 'datetime',
        'user_role_update_user_guid' => 'string',
        'user_role_delete_time' => 'datetime',
        'user_role_delete_user_guid' => 'string',
        'user_role_guid' => 'string',
    ];
    // 开启自动写入时间戳字段
    protected $autoWriteTimestamp = 'datetime';
    // 创建时间
    protected $createTime = 'user_role_create_time';
    // 修改时间
    protected $updateTime = 'user_role_update_time';
    /**
     * 状态 启用
     */
    const STATUS_ENABLE = 1;
    /**
     * 状态 禁用
     */
    const STATUS_DISABLE = 2;
    // 状态查询范围
    public function scopeStatus($query, $status = self::STATUS_ENABLE)
    {
        $query->where('user_role_status', $status);
    }
    /**
     * 中间关联
     *
     * @return ModelPivot
     * @date 2022-12-30
     * @example
     * @author admin
     * @since 1.0.0
     */
    public static function pivot(): ModelPivot
    {
        return new ModelPivot(
            self::class,
            User::class,
            Role::class,
            true
        );
    }
    /**
     * 绑定用户角色
     *
     * @param $users
     * @param $roles
     * @param array $extra
     * @return void
     * @date 2022-12-29
     * @example
     * @author admin
     * @since 1.0.0
     */
    public static function bindUserRole($users, $roles, array $extra = []): void
    {
        self::pivot()->bind($users, $roles, $extra);
    }
    /**
     * 解绑用户角色
     *
     * @param $users
     * @param $roles
     * @return void
     * @date 2022-12-30
     * @example
     * @author admin
     * @since 1.0.0
     */
    public static function unbindUserRole($users, $roles): void
    {
        self::pivot()->unbind($users, $roles);
    }
    /**
     * 绑定用户角色
     *
     * @param $users
     * @param $roles
     * @param array $extra
     * @return void
     * @date 2022-12-29
     * @example
     * @author admin
     * @since 1.0.0
     */
    public static function rebindUserRole($users, $roles, array $extra = []): void
    {
        self::pivot()->rebind($users, $roles, $extra);
    }

    /**
     * 新增前
     *
     * @date 2022-02-22
     * @example
     * @author admin
     * @since 1.0.0
     */
    public static function onBeforeInsert(self $model): void
    {
        $model->user_role_status = self::STATUS_ENABLE;
        $model->completeCreateField();
    }

    /**
     * 更新前
     *
     * @date 2022-02-28
     * @example
     * @author admin
     * @since 1.0.0
     */
    public static function onBeforeUpdate(self $model): void
    {
        $model->completeUpdateField();
    }

    /**
     * 删除前
     *
     * @date 2022-02-28
     * @example
     * @author admin
     * @since 1.0.0
     */
    public static function onBeforeDelete(self $model): void
    {
        $model->completeDeleteField();
    }
}
