<?php

namespace app\common\model\Menu;

use app\BaseModel;
use app\exception\ErrorMsg;
use think\Exception;
use think\facade\App;
use think\facade\Config;
use think\model\concern\SoftDelete;
use think\model\relation\BelongsToMany;
use think\model\relation\HasMany;
use think\model\relation\HasOne;

class MenuApi extends BaseModel
{
    use SoftDelete;
    // 删除字段
    protected $deleteTime = 'menu_api_delete_time';
    // 设置主键名
    protected $pk = 'menu_api_guid';
    // 设置废弃字段
    protected $disuse = [];
    // 设置字段信息
    protected $schema = [
        'menu_api_id'          => 'int',
        'menu_api_guid' => 'string',
        'menu_guid'          => 'string',
        'menu_api_url'        => 'string',
        'menu_api_status'      => 'int',
        'menu_api_create_time' => 'datetime',
        'menu_api_create_user_guid' => 'string',
        'menu_api_update_time' => 'datetime',
        'menu_api_update_user_guid' => 'string',
        'menu_api_delete_time' => 'datetime',
        'menu_api_delete_user_guid' => 'string',
    ];
    // 开启自动写入时间戳字段
    protected $autoWriteTimestamp = 'datetime';
    // 创建时间
    protected $createTime = 'menu_api_create_time';
    // 修改时间
    protected $updateTime = 'menu_api_update_time';
    /**
     * 状态 启用
     */
    const STATUS_ENABLE = 1;
    /**
     * 状态 禁用
     */
    const STATUS_DISABLE = 2;
    // 状态查询范围
    public function scopeStatus($query, $status = self::STATUS_ENABLE)
    {
        $query->where('menu_api_status', $status);
    }

    /**
     * 新增前
     *
     * @date 2022-02-22
     * @example
     * @author admin
     * @since 1.0.0
     */
    public static function onBeforeInsert(self $model): void
    {
        $model->menu_api_status = self::STATUS_ENABLE;
        $menu_api_urls = explode('/', $model->menu_api_url);
        if (count($menu_api_urls) == 3) {
            $app = isset($menu_api_urls[0]) ? $menu_api_urls[0] : '';
            $controller = isset($menu_api_urls[1]) ? $menu_api_urls[1] : '';
            $action = isset($menu_api_urls[2]) ? $menu_api_urls[2] : '';
        } else {
            $controller = isset($menu_api_urls[0]) ? $menu_api_urls[0] : '';
            $action = isset($menu_api_urls[1]) ? $menu_api_urls[1] : '';
        }
        if (!$controller) {
            throwErrorMsg("缺少controller", 1);
        }
        if (!$action) {
            throwErrorMsg("缺少action", 1);
        }
        $controller = str_replace('.', '\\', $controller);
        if ($app) {
            $class = "\\app\\$app\\controller\\$controller";
        } else {
            $class = "\\app\\controller\\$controller";
        }
        if (!class_exists($class)) {
            throwErrorMsg("controller不存在 $controller", 1);
        }
        if (!method_exists($class, $action)) {
            throwErrorMsg("action不存在 $action", 1);
        }
        $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();
    }

    /**
     * 获取状态
     *
     * @param mixed $value
     * @param array $data
     * @date 2022-02-22
     * @example
     * @author admin
     * @since 1.0.0
     */
    public function getMenuApiStatusTextAttr(): string
    {
        return [
            self::STATUS_ENABLE => '启用',
            self::STATUS_DISABLE => '停用'
        ][$this->menu_api_status];
    }
}
