<?php

namespace app\controller\admin\system;

use app\BaseController;
use app\entity\SysDictionary;
use app\service\CurdService;
use think\response\Json;

class DictionaryController extends BaseController
{
    public function lists(): Json
    {
        $lists = CurdService::getList($this->request, new SysDictionary(), ['sort_number' => 'desc']);

        return $this->writeSuccess('success', $lists);
    }

    public function update()
    {
        $data = $this->request->put([
            'dictId' => 0,
            'dictCode' => null,
            'dictName' => null,
            'sortNumber' => 100,
            'comments' => ''
        ]);
        SysDictionary::findOrFail($data['dictId'])->save([
            'dict_code' => $data['dictCode'],
            'dict_name' => $data['dictName'],
            'sort_number' => $data['sortNumber'],
            'comments' => $data['comments']
        ]);
        return $this->writeSuccess('修改成功');
    }

    public function remove(SysDictionary $model)
    {
        $model->delete();

        return $this->writeSuccess('删除成功');
    }

    public function add()
    {
        $data = $this->request->post([
            'dictCode' => null,
            'dictName' => null,
            'sortNumber' => 100,
            'comments' => ''
        ]);
        $sysDictionary = new SysDictionary();
        $sysDictionary->save([
            'dict_code' => $data['dictCode'],
            'dict_name' => $data['dictName'],
            'sort_number' => $data['sortNumber'],
            'comments' => $data['comments']
        ]);
        return $this->writeSuccess('添加成功');
    }
}