<?php
// +----------------------------------------------------------------------
// | onezan-admin 快速开发的管理平台
// +----------------------------------------------------------------------
// | 官方网址：https://www.onezan.cn
// +----------------------------------------------------------------------
// | onezan团队 版权所有 开源版本可自由商用
// +----------------------------------------------------------------------
// | Author: 543885307@qq.com
// +----------------------------------------------------------------------
namespace app\service\admin\user;

use app\service\admin\BaseAdminService;
use app\service\domain\user\CoreUserLevelService;
use think\Request;


/**
 * User Level Service
 * @package app\service\admin\user
 */
class UserLevelService extends BaseAdminService
{

    /**
     * 用户等级列表
     * @return array
     */
    public function list(): array
    {
        return ['code' => 0, 'data' => $this->core()->listAll()];
    }


    /**
     * 用户等级选项
     * @return array
     */
    public function options(): array
    {
        return ['code' => 0, 'data' => $this->core()->listEnabledOptions()];
    }


    /**
     * 新增或更新用户等级
     * @param Request $request 请求对象
     * @return array
     */
    public function save(Request $request): array
    {
        return $this->core()->save([
            'id' => $request->post('id'),
            'name' => $request->post('name', ''),
            'code' => $request->post('code', ''),
            'description' => $request->post('description', ''),
            'sort' => $request->post('sort', 0),
            'status' => $request->post('status', 1),
        ]);
    }


    /**
     * 删除用户等级
     * @param Request $request 请求对象
     * @return array
     */
    public function delete(Request $request): array
    {
        $id = $request->post('id');
        if (!is_numeric($id)) {
            return ['code' => 1, 'msg' => ''];
        }
        return $this->core()->delete((int)$id);
    }

    /**
     * 获取用户等级核心服务实例
     * @return CoreUserLevelService
     */
    private function core(): CoreUserLevelService
    {
        return $this->app->make(CoreUserLevelService::class);
    }
}