<?php

namespace app\common\model\Role;

use app\BasePivot;
use app\common\model\Menu\Menu;
use think\db\Query;
use think\facade\Db;
use think\model\Collection;
use think\model\concern\SoftDelete;
use think\model\Pivot;
use think\model\relation\HasMany;
use app\common\arw\adjfut\src\Model\Pivot as ModelPivot;

class RoleMenu extends BasePivot
{
    use SoftDelete;
    // 删除字段
    protected $deleteTime = 'role_menu_delete_time';
    // 设置主键名
    protected $pk = 'role_menu_guid';
    // 设置废弃字段
    protected $disuse = [];
    // 设置字段信息
    protected $schema = [
        'role_menu_id' => 'int',
        'menu_guid' => 'string',
        'role_guid' => 'string',
        'role_menu_status' => 'int',
        'role_menu_create_time' => 'datetime',
        'role_menu_create_user_guid' => 'string',
        'role_menu_update_time' => 'datetime',
        'role_menu_update_user_guid' => 'string',
        'role_menu_delete_time' => 'datetime',
        'role_menu_delete_user_guid' => 'string',
        'role_menu_guid' => 'string',
    ];
    // 开启自动写入时间戳字段
    protected $autoWriteTimestamp = 'datetime';
    // 创建时间
    protected $createTime = 'role_menu_create_time';
    // 修改时间
    protected $updateTime = 'role_menu_update_time';
    /**
     * 状态 启用
     */
    const STATUS_ENABLE = 1;
    /**
     * 状态 禁用
     */
    const STATUS_DISABLE = 2;
    // 状态查询范围
    public function scopeStatus($query, $status = self::STATUS_ENABLE)
    {
        $query->where('role_menu_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,
            Role::class,
            Menu::class,
            true
        );
    }
    /**
     * 绑定角色菜单
     *
     * @param $roles
     * @param $menus
     * @param array $extra
     * @return void
     * @date 2022-12-29
     * @example
     * @author admin
     * @since 1.0.0
     */
    public static function bindRoleMenu($roles, $menus, array $extra = []): void
    {
        self::pivot()->bind($roles, $menus, $extra);
    }
    /**
     * 解绑角色菜单
     *
     * @param $roles
     * @param $menus
     * @return void
     * @date 2022-12-30
     * @example
     * @author admin
     * @since 1.0.0
     */
    public static function unbindRoleMenu($roles, $menus): void
    {
        self::pivot()->unbind($roles, $menus);
    }
    /**
     * 绑定角色菜单
     *
     * @param $roles
     * @param $menus
     * @param array $extra
     * @return void
     * @date 2022-12-29
     * @example
     * @author admin
     * @since 1.0.0
     */
    public static function rebindRoleMenu($roles, $menus, array $extra = []): void
    {
        self::pivot()->rebind($roles, $menus, $extra);
    }

    /**
     * 新增前
     *
     * @date 2022-02-22
     * @example
     * @author admin
     * @since 1.0.0
     */
    public static function onBeforeInsert(self $model): void
    {
        $model->role_menu_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();
    }
}
