<?php
declare(strict_types=1);
namespace App\Controller;

use App\JsonRpc\PublicRpcServiceInterface;
use Hyperf\Di\Annotation\Inject;
use Hyperf\HttpServer\Annotation\AutoController;
use App\Tools\Result;
use App\Tools\CommonService;
use Hyperf\Validation\Contract\ValidatorFactoryInterface;

/**
 * Class PublicController
 * @package App\Controller
 */
#[AutoController]
class PublicController extends AbstractController
{
    #[Inject]
    protected ValidatorFactoryInterface $validationFactory;
    /**
     * @var PublicRpcServiceInterface
     */
    #[Inject]
    private $publicServiceClient;

    /**
     * 地区
     * @return array
     */
    public function getDistrictList()
    {
        $pid = $this->request->input("pid",0);
        $data = [
            'pid'=>$pid
        ];
        $result = $this->publicServiceClient->getDistrictList($data);
        return $result ? Result::success($result['data']) : Result::error($result['message']);
    }

    /**
     * 获取用户等级
     * @return array
     */
    public function getUserLevelList()
    {
        $requireData = $this->request->all();
        $result = $this->publicServiceClient->getUserLevelList($requireData);
        return $result ? Result::success($result['data']) : Result::error($result['message']);
    }

    /**
     * 上传图片
     * @return array
     */
    public function uploadFile()
    {
        $urlN = new CommonService();
        $file = $this->request->file('file');
        try {
            if ($file == null) {
               return  Result::error("未找到上传文件");
            }
            $data = CommonService::uploadFile($file, ['png','jpg','png','jpeg','gif','xls', 'xlsx', 'pdf', 'xls', 'xlsx', 'doc', 'docx', 'ppt', 'zip', 'pptx', 'mp4', 'flv'], 'image');
            $data['imgUrl'] = $urlN->imgUrl($data['src']);
            return  Result::success($data, '上传成功');
        } catch (\Exception $e) {
            return Result::error($e->getMessage());

        }
    }
    /**
     * 添加用户等级
     * @return array
     */
    public function addUserLevel()
    {
        $requireData = $this->request->all();
        $validator = $this->validationFactory->make(
            $requireData,
            [
                'name' => 'required',
            ],
            [
                'name.required' => '等级名不能为空',
            ]
        );
        if ($validator->fails()){
            $errorMessage = $validator->errors()->first();
            return Result::error($errorMessage);
        }
        $result = $this->publicServiceClient->addUserLevel($requireData);
        return $result ? Result::success($result['data']) : Result::error($result['message']);
    }
    /**
     * 删除用户等级
     * @return array
     */
    public function delUserLevel()
    {
        $requireData = $this->request->all();
        $validator = $this->validationFactory->make(
            $requireData,
            [
                'id' => 'required'
            ],
            [
                'id.required' => 'ID不能为空'
            ]
        );
        if ($validator->fails()){
            $errorMessage = $validator->errors()->first();
            return Result::error($errorMessage);
        }
        $result = $this->publicServiceClient->delUserLevel($requireData);
        return $result ? Result::success($result['data']) : Result::error($result['message']);
    }
    /**
     * 更新用户等级
     * @return array
     */
    public function updateUserLevel()
    {
        $requireData = $this->request->all();
        $validator = $this->validationFactory->make(
            $requireData,
            [
                'name' => 'required',
                'id' => 'required'
            ],
            [
                'id.required' => 'ID不能为空',
                'name.required' => '名称不能为空'
            ]
        );
        if ($validator->fails()){
            $errorMessage = $validator->errors()->first();
            return Result::error($errorMessage);
        }
        $result = $this->publicServiceClient->updateUserLevel($requireData);
        return $result ? Result::success($result['data']) : Result::error($result['message']);
    }
    /**
     * 城市列表
     * @return array
     */
    public function getCityList()
    {
        $requireData = $this->request->all();
        var_dump($requireData);
        $result = $this->publicServiceClient->getDistrictList($requireData);
        return $result ? Result::success($result['data']) : Result::error($result['message']);
    }


}