
namespace app\api\controller;

use app\api\controller\base\Base;

class {$humpName} extends Base
{
    private function getModel()
    {
        return new \app\common\model\{$humpName}();
    }

{if condition="array_key_exists('select',$crud)"}
    public function index()
    {
        $post = $this->request->param();
        $validate = new \think\Validate([
            ['id', 'number'],
            ['page', 'number'],
            ['pagenum', 'number|<=:1000']
        ]);
        if (!$validate->check($post)) {
            $this->json_error('提交失败：' . $validate->getError());
        }

        $where = [];
        if (isset($post['id'])) {
            $where['id'] = $post['id'];
        }

        if (isset($post['id'])) {
            $datalist = ($this->getModel())->where($where)->find();
        } else {
            $pagenum = $this->request->param('pagenum', 20, 'intval');
            $datalist = ($this->getModel())->where($where)->paginate($pagenum, true);
        }
        if (empty($datalist)) {
            $this->json_error("没有数据");
        }
        $this->json_success("查询成功", $datalist);
    }
{/if}

{if condition="array_key_exists('create',$crud)"}
    public function create()
    {
        $post = $this->request->param();
        //验证
        $validate = new \think\Validate([
        {volist name="$fieldsInfo" id="vo"}
            {php}
                $field_key = $vo['Field'];
                if(in_array($field_key,['id','create_time','update_time'])){
                    continue;
                }
                if(!empty($vo['Comment'])){
                    $comment = $vo['Comment'];
                    $field_key.= "|" . $comment;
                }
                $field_val = "";
                if($vo['Default'] === null){
                    $field_val.= "require";
                }
                if(startWith($vo["Type"],'int') || startWith($vo["Type"],'tinyint')){
                    $field_val .= empty($field_val) ? "number" : "|number";
                }
                if(startWith($vo["Type"],'varchar')){
                    $maxLen = str_replace(['varchar(',')'],['',''],$vo["Type"]);
                    $field_val .= empty($field_val) ? "max:".$maxLen : "|max:".$maxLen;
                }
            {/php}
            {notempty name="$field_val"}
            ['{$field_key}', '{$field_val}'],
            {/notempty}
         {/volist}
        ]);
        if (!$validate->check($post)) {
            $this->error('提交失败：' . $validate->getError());
        }

        $model = $this->getModel();
        $res = $model->allowField(true)->save($post);
        if ($res) {
            $this->json_success("创建成功", $model);
        } else {
            $this->json_error("创建失败");
        }
    }
{/if}

{if condition="array_key_exists('delete',$crud)"}
    public function delete()
    {
        $post = $this->request->param();
        $validate = new \think\Validate([
            ['id', 'require|number'],
        ]);
        if (!$validate->check($post)) {
            $this->json_error('提交失败：' . $validate->getError());
        }

        $item = ($this->getModel())->where(['id' => $post['id']])->find();
        if (!$item) {
            $this->json_error('找不到该id');
        }

        if (!$item->delete()) {
            $this->json_error('删除失败');
        }
        $this->json_success('删除成功');
    }
{/if}

{if condition="array_key_exists('update',$crud)"}
    public function update()
    {
        $post = $this->request->param();
        //验证
        $validate = new \think\Validate([
            ['id', 'require|number'],
        {volist name="$fieldsInfo" id="vo"}
            {php}
                $field_key = $vo['Field'];
                if(in_array($field_key,['id','create_time','update_time'])){
                    continue;
                }
                if($vo['Default'] === null){
                    continue;
                }
                if(!empty($vo['Comment'])){
                    $comment = $vo['Comment'];
                    $field_key.= "|" . $comment;
                }
                $field_val = "";
                if(startWith($vo["Type"],'int') || startWith($vo["Type"],'tinyint')){
                    $field_val .= empty($field_val) ? "number" : "|number";
                }
                if(startWith($vo["Type"],'varchar')){
                    $maxLen = str_replace(['varchar(',')'],['',''],$vo["Type"]);
                    $field_val .= empty($field_val) ? "max:".$maxLen : "|max:".$maxLen;
                }
            {/php}
            {notempty name="$field_val"}
            ['{$field_key}', '{$field_val}'],
            {/notempty}
         {/volist}
        ]);
        if (!$validate->check($post)) {
            $this->json_error('提交失败：' . $validate->getError());
        }

        $item = ($this->getModel())->where(['id' => $post['id']])->find();
        if (!$item) {
            $this->json_error('找不到该id');
        }

        if ($item->allowField(true)->save($post)) {
            $this->json_success("修改成功");
        } else {
            $this->json_error("修改失败");
        }
    }
{/if}

}