com.lq.cli.mybatis.CreateControllerTask Maven / Gradle / Ivy
package com.lq.cli.mybatis;
import com.lq.cli.comment.CreateTask;
import com.lq.cli.comment.StringUtil;
import com.lq.cli.comment.TaskArgs;
import com.lq.cli.jdbc.TableInfo;
import java.util.List;
public class CreateControllerTask extends CreateTask {
private List tableInfos;
public CreateControllerTask(TaskArgs taskArgs, List tableInfos) {
super(taskArgs);
this.tableInfos = tableInfos;
setPackageName("controller");
}
@Override
public Boolean call() throws Exception {
if (checkDir()){
for (TableInfo tableInfo : tableInfos) {
String controller = tableInfo2Controller(tableInfo);
createFile(tableInfo.getTransformTableInfo().getTableName()+"Controller.java",controller);
}
}
return true;
}
private String tableInfo2Controller(TableInfo tableInfo) {
TableInfo transformTableInfo = tableInfo.getTransformTableInfo();
StringBuilder sb = new StringBuilder();
sb.append("package ").append(taskArgs.packageName).append(".controller;\n\n")
.append("import ").append(taskArgs.packageName).append(".entity.").append(transformTableInfo.getTableName())
.append(";\nimport ").append(taskArgs.packageName).append(".util.CommentResponse")
.append(";\nimport ").append(taskArgs.packageName).append(".util.WebUtil")
.append(";\nimport ").append(taskArgs.packageName).append(".service.")
.append(transformTableInfo.getTableName())
.append("Service;\nimport org.springframework.validation.BindingResult;\nimport javax.validation.Valid;\nimport org.springframework.beans.factory.annotation.Autowired;\nimport org.springframework.web.bind.annotation.*;\n\n@RestController\n@RequestMapping(\"/")
.append(StringUtil.firstToLowerCase(transformTableInfo.getTableName()))
.append("\")\npublic class ")
.append(transformTableInfo.getTableName()).append("Controller {\n\n\t@Autowired\n\tprivate ")
.append(transformTableInfo.getTableName())
.append("Service ").append(StringUtil.firstToLowerCase(transformTableInfo.getTableName()))
.append("Service;\n").append("\n\t@PostMapping(value = \"insert")
.append(transformTableInfo.getTableName())
.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(StringUtil.firstToLowerCase(transformTableInfo.getTableName()))
.append("Service.insert")
.append(transformTableInfo.getTableName()).append("(")
.append(StringUtil.firstToLowerCase(transformTableInfo.getTableName())).append(") ? CommentResponse.success() : CommentResponse.fail();\n\t\t}\n\t}\n\n");
transformTableInfo.getFiledEntities().stream()
.filter(tableFiledEntity -> tableFiledEntity.getKey().equals("PRI"))
.findFirst()
.ifPresent(priKey -> sb.append("\t@GetMapping(\"/query")
.append(transformTableInfo.getTableName())
.append("By")
.append(StringUtil.firstToUpperCase(priKey.getName()))
.append("\")\n\tpublic CommentResponse")
.append(" query")
.append(transformTableInfo.getTableName())
.append("By")
.append(StringUtil.firstToUpperCase(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(\"/delete")
.append(transformTableInfo.getTableName())
.append("By")
.append(StringUtil.firstToUpperCase(priKey.getName()))
.append("\")\n\tpublic CommentResponse delete")
.append(transformTableInfo.getTableName())
.append("By")
.append(StringUtil.firstToUpperCase(priKey.getName()))
.append("(")
.append(priKey.getType())
.append(" ")
.append(priKey.getName())
.append("){\n\t\treturn ")
.append(StringUtil.firstToLowerCase(transformTableInfo.getTableName()))
.append("Service.delete")
.append(transformTableInfo.getTableName())
.append("By")
.append(StringUtil.firstToUpperCase(priKey.getName()))
.append("(")
.append(priKey.getName())
.append(")>0 ? CommentResponse.success() : CommentResponse.fail();\n\t}\n\n\n\t@PostMapping(value = \"update")
.append(transformTableInfo.getTableName())
.append("\", consumes = \"application/json\", produces = \"application/json\")\n\tpublic CommentResponse update")
.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(StringUtil.firstToLowerCase(transformTableInfo.getTableName()))
.append("Service.update")
.append(transformTableInfo.getTableName()).append("(")
.append(StringUtil.firstToLowerCase(transformTableInfo.getTableName())).append(")? CommentResponse.success() : CommentResponse.fail();\n\t\t}\n\t}\n"));
sb.append("\n}");
return sb.toString();
}
}