<?php
// +----------------------------------------------------------------------
// | saiadmin [ saiadmin快速开发框架 ]
// +----------------------------------------------------------------------
// | Author: your name
// +----------------------------------------------------------------------
namespace {{namespace_start}}controller{{namespace_end}};

use plugin\saiadmin\basic\BaseController;
use {{namespace_start}}logic{{namespace_end}}\{{class_name}}Logic;
use {{namespace_start}}validate{{namespace_end}}\{{class_name}}Validate;
use plugin\saiadmin\service\Permission;
use support\Request;
use support\Response;

/**
 * {{menu_name}}控制器
 */
class {{class_name}}Controller extends BaseController
{
    /**
     * 构造函数
     */
    public function __construct()
    {
        $this->logic = new {{class_name}}Logic();
        $this->validate = new {{class_name}}Validate;
        parent::__construct();
    }

    /**
     * 数据列表
     * @param Request $request
     * @return Response
     */
    #[Permission('{{menu_name}}列表', '{{namespace}}:{{package_name}}:{{business_name}}:index')]
    public function index(Request $request): Response
    {
        $where = $request->more([
{% for column in columns %}
{% if column.is_query == '2' %}
            ['{{column.column_name}}', ''],
{% endif %}
{% endfor %}
        ]);
{% if tpl_category == 'single' %}
        $query = $this->logic->search($where);
{% if options.relations != null %}
        $query->with([
{% for item in options.relations %}
            '{{item.name}}',
{% endfor %}
        ]);
{% endif %}
        $data = $this->logic->getList($query);
{% endif %}
{% if tpl_category == 'tree' %}
        $data = $this->logic->tree($where);
{% endif %}
        return $this->success($data);
    }

    /**
     * 读取数据
     * @param Request $request
     * @return Response
     */
    #[Permission('{{menu_name}}读取', '{{namespace}}:{{package_name}}:{{business_name}}:read')]
    public function read(Request $request): Response
    {
        $id = $request->input('id', '');
        $model = $this->logic->read($id);
        if ($model) {
            $data = is_array($model) ? $model : $model->toArray();
            return $this->success($data);
        } else {
            return $this->fail('未查找到信息');
        }
    }

    /**
     * 保存数据
     * @param Request $request
     * @return Response
     */
    #[Permission('{{menu_name}}添加', '{{namespace}}:{{package_name}}:{{business_name}}:save')]
    public function save(Request $request): Response
    {
        $data = $request->post();
        $this->validate('save', $data);
        $result = $this->logic->add($data);
        if ($result) {
            return $this->success('添加成功');
        } else {
            return $this->fail('添加失败');
        }
    }

    /**
     * 更新数据
     * @param Request $request
     * @return Response
     */
    #[Permission('{{menu_name}}修改', '{{namespace}}:{{package_name}}:{{business_name}}:update')]
    public function update(Request $request): Response
    {
        $data = $request->post();
        $this->validate('update', $data);
        $result = $this->logic->edit($data['id'], $data);
        if ($result) {
            return $this->success('修改成功');
        } else {
            return $this->fail('修改失败');
        }
    }

    /**
     * 删除数据
     * @param Request $request
     * @return Response
     */
    #[Permission('{{menu_name}}删除', '{{namespace}}:{{package_name}}:{{business_name}}:destroy')]
    public function destroy(Request $request): Response
    {
        $ids = $request->post('ids', '');
        if (empty($ids)) {
            return $this->fail('请选择要删除的数据');
        }
        $result = $this->logic->destroy($ids);
        if ($result) {
            return $this->success('删除成功');
        } else {
            return $this->fail('删除失败');
        }
    }

}
