package com.shmy.codeUtil;
import org.apache.commons.lang.StringUtils;
import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;
/**
* 类描述
* @Author: zhanghj
* @Date: 2019/8/29 16:10
* @Version: 1.0
*/
public class GenCodeUtil {
/**
* 从表结构中去生成javabean
* @param author
* @param table
* @param beanName
* @param packagePath
* @return
*/
private static String genJavaBeanFromTableStructure(String author, TableModel table,String beanName, String packagePath){
StringBuffer sb = new StringBuffer();
if(StringUtils.isNotEmpty(packagePath)){
sb.append("package "+packagePath+";\n");
}
for(String imp : table.getImports()){
sb.append("import "+imp+";\n");
}
sb.append("\n");
sb.append("/**\n *@author "+author+"\n **/\n");
List<ColumnModel> columnModelList = table.getColumns();
try {
sb.append("public class "+toFirstCharUpCase(beanName)+" {\r\n");
for (ColumnModel columnModel : columnModelList) {
if(StringUtils.isNotBlank(columnModel.getRemarks())){
sb.append(" //"+columnModel.getRemarks()+" \r\n");
}
sb.append(" private "+columnModel.getFieldType()+" "+columnModel.getFieldName()+";\r\n");
}
sb.append("\r\n");
//get set
for (ColumnModel columnModel : columnModelList) {
sb.append(
"\tpublic "+columnModel.getColumnClassName()+" get"+toFirstCharUpCase((String) columnModel.getFieldName())+"() {\r\n" +
"\t\treturn "+columnModel.getFieldName()+";\r\n" +
"\t}\r\n" +
"\r\n" +
"\tpublic void set"+toFirstCharUpCase((String) columnModel.getFieldName())+"("+columnModel.getColumnClassName()+" "+columnModel.getFieldName()+") {\r\n" +
"\t\tthis."+columnModel.getFieldName()+" = "+columnModel.getFieldName()+";\r\n" +
"\t}\r\n\r\n");
}
sb.append("}\r\n");
} catch (Exception e) {
e.printStackTrace();
}
return sb.toString();
}
/**
* 从表结构中去生成查询实体类
* @param author
* @param table
* @param beanName
* @param extendsBasePackage
* @param packagePath
* @return
*/
private static String genQueryModelFromTableStructure(String author, TableModel table,String beanName, String extendsBasePackage, String packagePath){
StringBuffer sb = new StringBuffer();
if(StringUtils.isNotEmpty(packagePath)){
sb.append("package "+packagePath+";\n\n");
}
sb.append("import "+extendsBasePackage+".entity.QueryBase;\n\n");
sb.append("/**\n *@author "+author+"\n **/\n");
try {
sb.append("public class "+toFirstCharUpCase(beanName)+" extends QueryBase {\r\n");
List<ColumnModel> columns = getQueryFields(table);
for (ColumnModel columnModel : columns) {
if(StringUtils.isNotBlank(columnModel.getRemarks())){
sb.append(" //"+columnModel.getRemarks()+" \r\n");
}
String qFieldType = getQueryModelFieldType(columnModel.getFieldType());
sb.append(" private "+qFieldType+" "+columnModel.getFieldName()+";\r\n");
}
sb.append("\r\n");
//get set
for (ColumnModel columnModel : columns) {
String qFieldType = getQueryModelFieldType(columnModel.getFieldType());
sb.append(
"\tpublic "+qFieldType+" get"+toFirstCharUpCase((String) columnModel.getFieldName())+"() {\r\n" +
"\t\treturn "+columnModel.getFieldName()+";\r\n" +
"\t}\r\n" +
"\r\n" +
"\tpublic void set"+toFirstCharUpCase((String) columnModel.getFieldName())+"("+qFieldType+" "+columnModel.getFieldName()+") {\r\n" +
"\t\tthis."+columnModel.getFieldName()+" = "+columnModel.getFieldName()+";\r\n" +
"\t}\r\n\r\n");
}
sb.append("}\r\n");
} catch (Exception e) {
e.printStackTrace();
}
return sb.toString();
}
/**
* 生成Dao
* */
private static String genDao(String author, String packagePath, String beanName, String queryModelName,String extendsBasePackage){
StringBuffer sb = new StringBuffer();
if(StringUtils.isNotEmpty(packagePath)){
sb.append("package "+packagePath+";\n\n");
}
String businessPackage = packagePath.substring(0, packagePath.lastIndexOf("."));
String basePackage = businessPackage.substring(0, businessPackage.lastIndexOf("."));
sb.append("import "+extendsBasePackage+".dao.GenericDao;\n\n");
sb.append("import "+businessPackage+".entity."+beanName+";\n");
sb.append("import "+businessPackage+".entity."+queryModelName+";\n\n");
sb.append("/**\n *@author "+author+"\n **/\n");
sb.append("public interface "+beanName+"Dao extends GenericDao<"+beanName+", "+queryModelName+"> {\r\n");
sb.append("\n\t\n}");
return sb.toString();
}
/**
* 生成Service
* */
private static String genService(String author, String packagePath, String beanName, String queryModelName,String extendsBasePackage){
StringBuffer sb = new StringBuffer();
if(StringUtils.isNotEmpty(packagePath)){
sb.append("package "+packagePath+";\n\n");
}
String businessPackage = packagePath.substring(0, packagePath.lastIndexOf("."));
String basePackage = businessPackage.substring(0, businessPackage.lastIndexOf("."));
sb.append("import org.springframework.beans.factory.annotation.Autowired;\n");
sb.append("import org.springframework.stereotype.Service;\n\n");
sb.append("import org.springframework.transaction.annotation.Transactional;\n\n");
sb.append("import "+extendsBasePackage+".service.GenericService;\n");
sb.append("import "+basePackage+".common.base.dao.GenericDao;\n\n");
sb.append("import "+businessPackage+".entity."+beanName+";\n");
sb.append("import "+businessPackage+".entity."+queryModelName+";\n");
sb.append("import "+businessPackage+".dao."+beanName+"Dao;\n\n");
sb.append("/**\n *@author "+author+"\n **/\n");
sb.append("@Service(\""+toFirstCharLowerCase(beanName)+"Service\")\n");
sb.append("@Transactional(rollbackFor={IllegalArgumentException.class})\n");
sb.append("public class "+beanName+"Service extends GenericService<"+beanName+", "+queryModelName+"> {\r\n");
sb.append("\t@Autowired\n");
sb.append("\t@SuppressWarnings(\"SpringJavaAutowiringInspection\")\n");
sb.append("\tprivate "+beanName+"Dao "+toFirstCharLowerCase(beanName)+"Dao;\n");
sb.append("\t@Override\n\tprotected GenericDao<"+beanName+", "+queryModelName+"> getDao() {\n");
sb.append("\t\treturn "+toFirstCharLowerCase(beanName)+"Dao;\n");
sb.append("\t}\n");
sb.append("}");
return sb.toString();
}
/**
* 生成controller
* */
private static String genController(String author, String packagePath, String beanName, String queryModelName,String extendsBasePackage){
StringBuffer sb = new StringBuffer();
if(StringUtils.isNotEmpty(packagePath)){
sb.append("package "+packagePath+";\n\n");
}
String businessPackage = packagePath.substring(0, packagePath.lastIndexOf("."));
Strin