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

use app\repository\user\UserLevelRepository;
use Core\service\BaseCoreService;

/**
 * 用户等级服务
 * @package app\service\domain\user
 * Class CoreUserLevelService
 * @package app\service\domain\user
 */
class CoreUserLevelService extends BaseCoreService
{
    /**
     * 获取所有用户等级
     * @return array
     */
    public function listAll(): array
    {
        return $this->levelRepository()->all();
    }

    /**
     * 获取所有启用的用户等级选项
     * @return array
     */
    public function listEnabledOptions(): array
    {
        $rows = $this->levelRepository()->allEnabled();
        $data = [];
        foreach ($rows as $row) {
            $data[] = [
                'id' => (int)($row['id'] ?? 0),
                'name' => (string)($row['name'] ?? ''),
                'code' => (string)($row['code'] ?? ''),
                'description' => (string)($row['description'] ?? ''),
            ];
        }
        return $data;
    }

    /**
     * 保存用户等级
     * @param array $payload 数据数组
     * @return array
     */
    public function save(array $payload): array
    {
        $id = is_numeric($payload['id'] ?? null) ? (int)$payload['id'] : 0;
        $name = trim((string)($payload['name'] ?? ''));
        $code = trim((string)($payload['code'] ?? ''));
        $description = trim((string)($payload['description'] ?? ''));
        $sort = (int)($payload['sort'] ?? 0);
        $status = (int)($payload['status'] ?? 1) ? 1 : 0;

        if ($name === '' || $code === '') {
            return ['code' => 1, 'msg' => ''];
        }
        if ($this->levelRepository()->existsByCode($code, $id)) {
            return ['code' => 1, 'msg' => ''];
        }

        $level = $id > 0 ? $this->levelRepository()->findById($id) : null;
        if ($id > 0 && !$level) {
            return ['code' => 1, 'msg' => ''];
        }
        if (!$level) {
            $level = new \app\model\UserLevel();
        }

        $level->name = $name;
        $level->code = $code;
        $level->description = $description;
        $level->sort = $sort;
        $level->status = $status;
        $level->save();

        return ['code' => 0, 'msg' => ''];
    }

    /**
     * 删除用户等级
     * @param int $id 用户等级ID
     * @return array
     */
    public function delete(int $id): array
    {
        $level = $this->levelRepository()->findById($id);
        if (!$level) {
            return ['code' => 1, 'msg' => ''];
        }
        if ($this->levelRepository()->hasUsers($id)) {
            return ['code' => 1, 'msg' => ''];
        }
        $level->delete();
        return ['code' => 0, 'msg' => ''];
    }

    /**
     * 获取用户等级仓库
     * @return UserLevelRepository
     */
    private function levelRepository(): UserLevelRepository
    {
        return $this->app->make(UserLevelRepository::class);
    }
}
