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

com.lq.jpa.task.CreateControllerTask Maven / Gradle / Ivy

The newest version!
package com.lq.jpa.task;

import com.lq.SpringBootCli;
import com.lq.entity.TableFiledEntity;
import com.lq.entity.TableInfo;
import com.lq.glob.task.BaseTask;
import com.lq.util.StringUtil;

import java.util.List;

public final class CreateControllerTask extends BaseTask {

    private List tableInfos;

    public CreateControllerTask(SpringBootCli springBootCli, List tableInfos) {
        super(springBootCli);
        this.tableInfos = tableInfos;
    }

    @Override
    public Boolean execute() throws Exception {
        if (checkDir()) {
            for (TableInfo tableInfo : tableInfos) {
                TableFiledEntity pri = tableInfo.getTransformTableInfo().getFiledEntities().stream()
                        .filter(tableFiledEntity -> tableFiledEntity.getKey().equals("PRI"))
                        .findFirst().orElse(null);
                if (pri != null) {
                    String controller = tableInfo2Controller(tableInfo);
                    createFile(tableInfo.getTransformTableInfo().getTableName() + "Controller.java", controller);
                }
            }
        }
        return true;
    }

    @Override
    protected String getPackageName() {
        return "controller";
    }

    private String tableInfo2Controller(TableInfo tableInfo) {
        TableInfo transformTableInfo = tableInfo.getTransformTableInfo();
        StringBuilder sb = new StringBuilder();
        sb.append("package ").append(springBootCli.getPackageName()).append(".controller;\n\n")
                .append("import ").append(springBootCli.getPackageName()).append(".entity.").append(transformTableInfo.getTableName())
                .append(";\nimport ").append(springBootCli.getPackageName()).append(".util.CommentResponse;\nimport org.springframework.data.domain.Page;\nimport java.util.List;\nimport ")
                .append(springBootCli.getPackageName()).append(".util.WebUtil")
                .append(";\nimport ").append(springBootCli.getPackageName()).append(".service.")
                .append(transformTableInfo.getTableName())
                .append("Service;\nimport org.springframework.validation.BindingResult;\nimport javax.validation.Valid;\nimport javax.annotation.Resource;\nimport org.springframework.web.bind.annotation.*;\n\n@RestController\n@RequestMapping(\"/")
                .append(StringUtil.firstToLowerCase(transformTableInfo.getTableName()))
                .append("s\")\npublic class ")
                .append(transformTableInfo.getTableName()).append("Controller {\n\n\t@Resource\n\tprivate ")
                .append(transformTableInfo.getTableName())
                .append("Service ").append(StringUtil.firstToLowerCase(transformTableInfo.getTableName()))
                .append("Service;\n");
        List filedEntities = transformTableInfo.getFiledEntities();
        filedEntities.stream()
                .filter(tableFiledEntity -> tableFiledEntity.getKey().equals("PRI"))
                .findFirst()
                .ifPresent(priKey -> {
                    sb.append("\n\t@PostMapping(")
                            .append("consumes = \"application/json\", produces = \"application/json\")\n\tpublic CommentResponse insert")
                            .append(transformTableInfo.getTableName())
                            .append("(@RequestBody @Valid ")
                            .append(transformTableInfo.getTableName())
                            .append(" ")
                            .append(StringUtil.firstToLowerCase(transformTableInfo.getTableName()))
                            .append(", BindingResult result){\n\t\tif (result.getErrorCount() > 0) {\n\t\t\treturn WebUtil.bindingResult(result);\n\t\t} else {\n\t\t\treturn ")
                            .append("CommentResponse.success(")
                            .append(StringUtil.firstToLowerCase(transformTableInfo.getTableName())).append("Service.insert")
                            .append(transformTableInfo.getTableName()).append("(").append(StringUtil.firstToLowerCase(transformTableInfo.getTableName())).append("));\n\t\t}\n\t}\n\n");

                    sb.append("\t@GetMapping\n\tpublic Page<").append(transformTableInfo.getTableName()).append("> queryAll")
                            .append(transformTableInfo.getTableName()).append("(");
                    for (int x = 1; x < filedEntities.size(); x++) {
                        TableFiledEntity tableFiledEntity = filedEntities.get(x);
                        if (tableFiledEntity.getType().equals("String") && tableFiledEntity.getFieldLimitSize() < springBootCli.getQueryFieldLimitLength() &&
                                tableFiledEntity.getFieldLimitSize() != null && tableFiledEntity.getFieldLimitSize() > 0) {
                            sb.append("@RequestParam(value = \"")
                                    .append(filedEntities.get(x).getName())
                                    .append("\", required = false) String ").append(filedEntities.get(x).getName()).append(",\n\t\t\t\t\t\t\t\t\t\t\t\t");
                        }
                    }
                    sb.append("@RequestParam(value = \"page\", required = false, defaultValue = \"1\") Integer page,\n\t\t\t\t\t\t\t\t\t\t\t\t@RequestParam(value = \"pageSize\", required = false, defaultValue = \"10\") Integer pageSize){\n\t\treturn ")
                            .append(StringUtil.firstToLowerCase(transformTableInfo.getTableName())).append("Service.queryAll").append(transformTableInfo.getTableName())
                            .append("(");
                    for (int x = 1; x < filedEntities.size(); x++) {
                        TableFiledEntity tableFiledEntity = filedEntities.get(x);
                        if (tableFiledEntity.getType().equals("String") && tableFiledEntity.getFieldLimitSize() < springBootCli.getQueryFieldLimitLength() &&
                                tableFiledEntity.getFieldLimitSize() != null && tableFiledEntity.getFieldLimitSize() > 0) {
                            sb.append(filedEntities.get(x).getName()).append(",");
                        }
                    }
                    sb.append("page, pageSize);\n\t}\n\n");
                    sb.append("\t@GetMapping(\"/{")
                            .append(priKey.getName())
                            .append("}")
                            .append("\")\n\tpublic CommentResponse")
                            .append(" query")
                            .append(transformTableInfo.getTableName())
                            .append("By")
                            .append(StringUtil.firstToUpperCase(priKey.getName())).append("(@PathVariable(\"")
                            .append(priKey.getName()).append("\")")
                            .append(priKey.getType())
                            .append(" ")
                            .append(priKey.getName())
                            .append("){\n\t\t")
                            .append("return CommentResponse.success(").append(StringUtil.firstToLowerCase(transformTableInfo.getTableName()))
                            .append("Service.query")
                            .append(transformTableInfo.getTableName())
                            .append("By")
                            .append(StringUtil.firstToUpperCase(priKey.getName()))
                            .append("(")
                            .append(priKey.getName())
                            .append("));\n\t}\n\n\t@DeleteMapping(\"/{")
                            .append(priKey.getName())
                            .append("}")
                            .append("\")\n\tpublic CommentResponse delete")
                            .append(transformTableInfo.getTableName())
                            .append("By")
                            .append(StringUtil.firstToUpperCase(priKey.getName()))
                            .append("(@PathVariable(\"")
                            .append(priKey.getName()).append("\")")
                            .append(priKey.getType())
                            .append(" ")
                            .append(priKey.getName())
                            .append("){\n\t\t")
                            .append(StringUtil.firstToLowerCase(transformTableInfo.getTableName()))
                            .append("Service.delete")
                            .append(transformTableInfo.getTableName())
                            .append("By")
                            .append(StringUtil.firstToUpperCase(priKey.getName()))
                            .append("(")
                            .append(priKey.getName())
                            .append(");\n\t\treturn CommentResponse.success();\n\t}\n\n\n\t@DeleteMapping\n\tpublic CommentResponse delete")
                            .append(transformTableInfo.getTableName())
                            .append("s(@RequestBody List<")
                            .append(transformTableInfo.getTableName())
                            .append("> ")
                            .append(StringUtil.firstToLowerCase(transformTableInfo.getTableName()))
                            .append("s){\n\t\t")
                            .append(StringUtil.firstToLowerCase(transformTableInfo.getTableName()))
                            .append("Service.delete")
                            .append(transformTableInfo.getTableName())
                            .append("s(")
                            .append(StringUtil.firstToLowerCase(transformTableInfo.getTableName()))
                            .append("s);\n\t\treturn CommentResponse.success();\n\t}\n\n\n\t@PutMapping(consumes = \"application/json\", produces = \"application/json\")\n\tpublic CommentResponse update")
                            .append(transformTableInfo.getTableName())
                            .append("(@RequestBody ")
                            .append(transformTableInfo.getTableName())
                            .append(" ")
                            .append(StringUtil.firstToLowerCase(transformTableInfo.getTableName()))
                            .append("){\n\t\tif(")
                            .append(StringUtil.firstToLowerCase(transformTableInfo.getTableName()))
                            .append(".get")
                            .append(StringUtil.firstToUpperCase(priKey.getName()))
                            .append("()==null){\n\t\t\treturn CommentResponse.fail(\"")
                            .append(priKey.getName()).append(" 字段不能为空\");\n\t\t}")
                            .append("\n\t\treturn CommentResponse.success(").
                             append(StringUtil.firstToLowerCase(transformTableInfo.getTableName()))
                            .append("Service.update")
                            .append(transformTableInfo.getTableName()).append("(")
                            .append(StringUtil.firstToLowerCase(transformTableInfo.getTableName()))
                            .append("));\n\t}\n");
                });
        sb.append("\n}");
        return sb.toString();
    }

}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy