All Downloads are FREE. Search and download functionalities are using the official Maven repository.

shz.generator.module.AppendControllerData Maven / Gradle / Ivy

package shz.generator.module;

import shz.core.Help;
import shz.core.NullHelp;
import shz.generator.AppendData;
import shz.generator.Tgp;
import shz.jdbc.model.Table;
import shz.orm.entity.RecordEntity;

import java.util.LinkedList;
import java.util.List;
import java.util.Set;

public class AppendControllerData extends AppendData {
    @Override
    protected String comment(Table table) {
        String date = date();
        return "/**\n" +
                " * @author " + user(table) + "\n" +
                (NullHelp.isBlank(date) ? "" : " * @date " + date + "\n") +
                " * @description " + desc(table) + "控制类\n" +
                " */";
    }

    @Override
    protected List annotations(Tgp tgp, Set imports) {
        List annotations = new LinkedList<>();

        annotations.add("@RestController");
        annotations.add("@RequestMapping(\"" + requestMapping(tgp.table) + "\")");
        annotations.add("@Api(tags = \"" + apiTags(tgp.table) + "\")");
        annotations.add("@Permission(-1)");

        imports.add("import org.springframework.web.bind.annotation.RestController;");
        imports.add("import org.springframework.web.bind.annotation.RequestMapping;");
        imports.add("import io.swagger.annotations.Api;");
        imports.add("import shz.spring.api.Permission;");

        return annotations;
    }

    @Override
    protected String cls(Tgp tgp, Set imports) {
        String className = className(tgp.table);

        if (writeApi(tgp)) {
            imports.add("import " + tgp.apiGenInfo.packageName + "." + className + "Api;");

            return "public class " + className + "Controller implements " + className + "Api";
        }

        return "public class " + className + "Controller";
    }

    @Override
    protected List content(Tgp tgp, Set imports) {
        List content = new LinkedList<>();

        String className = className(tgp.table);
        String classFieldName = Help.pojo(className);
        String desc = desc(tgp.table);
        Class superEntity = superEntity(tgp.table);

        content.add("    @Autowired");
        content.add("    " + className + "Service " + classFieldName + "Service;\n");

        content.add("    @Permission(1)");
        content.add("    @ApiOperation(\"" + desc + "分页列表\")");
        content.add("    @PostMapping(\"page\")");
        content.add("    public Response> page(");
        content.add("            @RequestParam(value = \"pageNum\", defaultValue = \"1\") Integer pageNum,");
        content.add("            @RequestParam(value = \"pageSize\", defaultValue = \"10\") Integer pageSize,");
        content.add("            @RequestBody @Valid " + className + "Vo.Query vo) {");
        content.add("        PageInfo<" + className + "> page = " + classFieldName + "Service.selectPage(PageInfo.of(pageNum, pageSize), vo);");
        content.add("        return Response.ok(page.map(record -> ReflectionBean.copy(record, " + className + "Vo.Query.Vo.class)));");
        content.add("    }");

        if (superEntity == null || !RecordEntity.class.isAssignableFrom(superEntity)) {
            content.add("    @Permission(2)");
            content.add("    @ApiOperation(\"" + desc + "列表\")");
            content.add("    @PostMapping(\"list\")");
            content.add("    public Response> list(@RequestBody @Valid " + className + "Vo.Query vo) {");
            content.add("        List<" + className + "> list = " + classFieldName + "Service.selectList(vo);");
            content.add("        return Response.ok(ReflectionBean.copies(list, " + className + "Vo.Query.Vo.class));");
            content.add("    }\n");

            String type = primaryKeyType(tgp.table);
            String typeImport = getImport(type);
            if (typeImport != null) imports.add(typeImport);

            content.add("    @Permission(3)");
            content.add("    @ApiOperation(\"" + desc + "详情\")");
            content.add("    @GetMapping(\"detail/{id}\")");
            content.add("    public Response<" + className + "Vo.Detail> detail(@PathVariable(\"id\") " + type + " id) {");
            content.add("        return Response.ok(ReflectionBean.copy(" + classFieldName + "Service.selectById(id), " + className + "Vo.Detail.class));");
            content.add("    }\n");

            content.add("    @Permission(4)");
            content.add("    @ApiOperation(\"新增" + desc + "\")");
            content.add("    @PostMapping");
            content.add("    public Response insert(@RequestBody @Valid " + className + "Vo.Add vo) {");
            content.add("        " + classFieldName + "Service.insert(ReflectionBean.copy(vo, " + className + ".class));");
            content.add("        return Response.ok();");
            content.add("    }\n");

            content.add("    @Permission(5)");
            content.add("    @ApiOperation(\"更新" + desc + "\")");
            content.add("    @PutMapping");
            content.add("    public Response update(@RequestBody @Valid " + className + "Vo.Update vo) {");
            content.add("        " + classFieldName + "Service.updateById(ReflectionBean.copy(vo, " + className + ".class));");
            content.add("        return Response.ok();");
            content.add("    }\n");

            content.add("    @Permission(6)");
            content.add("    @ApiOperation(\"删除" + desc + "\")");
            content.add("    @DeleteMapping(\"{ids}\")");
            content.add("    public Response delete(@PathVariable(\"ids\") List<" + type + "> ids) {");
            content.add("        " + classFieldName + "Service.batchDeleteById(ids);");
            content.add("        return Response.ok();");
            content.add("    }");

            imports.add("import java.util.List;");
        }

        imports.add("import " + tgp.serviceGenInfo.packageName + "." + className + "Service;");
        imports.add("import org.springframework.beans.factory.annotation.Autowired;");

        imports.add("import shz.core.model.Response;");
        imports.add("import shz.core.model.PageInfo;");
        imports.add("import shz.spring.ReflectionBean;");
        imports.add("import javax.validation.Valid;");
        imports.add("import io.swagger.annotations.ApiOperation;");
        imports.add("import org.springframework.web.bind.annotation.*;");

        imports.add("import " + tgp.voGenInfo.packageName + "." + className + "Vo;");
        imports.add("import " + tgp.entityGenInfo.packageName + "." + className + ";");

        return content;
    }
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy