package xiaoqu.home.open.utils;

import java.io.IOException;
import java.util.List;


/*********
 * 自动生成Input和outPut
 * 
 * @author wh 
 */
public class AutoWriteInputAndVo {

	public static void main(String[] args) throws IOException {
		
		/**bean所在的数据库索引 1:xiaoqu_comples  2:xiaoqu_intellectual ***/
		int dbindex=2;
		boolean insertFlag =true;
		boolean importFlag =false;
		/**需要生成的bean集合**/
		String[] beanNames = new String[] {
			"WorkGroupToPrincipal"
//				,"Toilet"
//				,"WarehouseOut",
//			"WarehouseIn","WarehouseOutDetails"
//			"WarehouseInDetails","MallOrderDetails","MallOrderLogistics"
//				"HardwareOrder","CheckcodeOrder","ToiletAirquality"
		};
		
		//me.iiv.iivframework.beans.b0   bean所在的包路径
		String packagepath ="xiaoqu.home.open.model.db"+dbindex;
		//bean所在的相对路径
		String beanSrc = "../xiaoqu-intellectual-public/src/main/java/xiaoqu/home/open/model/db"+dbindex+"/";
		//input对象输出目录
		String beanDir1 = "./src/main/java/xiaoqu/home/open/controller/input/";
		//output输出目录
		String beanDir2 = "./src/main/java/xiaoqu/home/open/controller/output/";
		//output输出目录
		String beanDir3 = "./src/main/java/xiaoqu/home/open/controller/insert/";
		//output输出目录
		String beanDir4 = "./src/main/java/xiaoqu/home/open/controller/export/";

		for (String beanName : beanNames) {
			// bean重新生成
			String srcBeanPath = beanSrc+beanName+".java";
			String beanPath1 = beanDir1 + beanName + "PageInput.java";
			StringUtil.writerFile(beanPath1, CreatePageInput(srcBeanPath,beanName , "PageInput"));//pageInput
			beanPath1 = beanDir3 + beanName + "InsertInput.java";
			if (insertFlag) {
				StringUtil.writerFile(beanPath1, CreateInput(srcBeanPath,beanName , "InsertInput"));//InsertInput
			}
			String beanPath2 = beanDir2 + beanName + "Output.java";
			StringUtil.writerFile(beanPath2, CreateOutput(packagepath,beanName , "Output"));//OutPut
			//导出
			String exportPath = beanDir4 + beanName + "Export.java";
			if (importFlag) {
				StringUtil.writerFile(exportPath, CreateExport(srcBeanPath,beanName , "Export"));//InsertInput
			}
		}

	}
	private static String CreateExport(String beanPath,String bean,String fileName) {
		// 读取源文件
		List<String> beanContent = FileUtil.ReadFileByNameToArray(beanPath, "utf-8");
		StringBuilder _StringBuilder = new StringBuilder();
		for (String string : beanContent) {
			//@ApiModel(value="Agent对象", description="代理商表")
			if (string.contains("@ApiModel(")) {
				string="@ApiModel(value=\""+bean+"导出对象\")";
				StringUtil.addRow(_StringBuilder, string);
				continue;
			}
			if (string.contains(" @Override")) {
				continue;
			}
			if (string.contains("*")) {
				continue;
			}
			if (string.contains("serialVersionUID")) {
				continue;
			}
			if (string.contains("com.baomidou.mybatisplus.annotation.IdType")) {
				continue;
			}
			if (string.contains("com.baomidou.mybatisplus.annotation.TableId")) {
				continue;
			}
			if (string.contains("com.baomidou.mybatisplus.annotation.TableField")) {
				continue;
			}
			if (string.contains("com.baomidou.mybatisplus.extension.activerecord.Model")) {
				continue;
			}
			if (string.contains("com.baomidou.mybatisplus.annotation.TableName")) {
				continue;
			}
			//package
			if (string.contains("package xiaoqu.home.open.model")) {
				string="package xiaoqu.home.open.controller.export;";
				StringUtil.addRow(_StringBuilder, string);
				StringUtil.addRow(_StringBuilder, "import xiaoqu.home.open.utils.poi.annotation.Excel;");
				StringUtil.addRow(_StringBuilder, "import xiaoqu.home.open.utils.poi.annotation.Excel.ColumnType;");
				continue;
			}
			if (string.contains("@TableName")|| string.contains("@TableField")) {
				continue;
			}
			if (string.contains(" @TableId(value")) {
				string="	@Excel(name = \"ID\", cellType = ColumnType.NUMERIC)";
				StringUtil.addRow(_StringBuilder, string);
				continue;
			}
			if (string.contains("时间")||string.contains("日期")) {
				string="    @Excel(name = \"操作日期\", width = 30, dateFormat = \"yyyy-MM-dd HH:mm:ss\")";
				StringUtil.addRow(_StringBuilder, string);
				continue;
			}
			if (string.contains("数据状态")) {
				string="    @Excel(name = \"数据状态\", readConverterExp = \"0=正常,1=删除 \")";
				StringUtil.addRow(_StringBuilder, string);
				continue;
			}
			if (string.contains("@ApiModelProperty(value")) {
				string=string.replace("@ApiModelProperty(value ", "@Excel(name");
				StringUtil.addRow(_StringBuilder, string);
				continue;
			}
			if (string.contains("public class")) {
				string="public class "+bean+fileName+" {";
				StringUtil.addRow(_StringBuilder, string);
			} else {
				StringUtil.addRow(_StringBuilder, string);
			}
		}

		return _StringBuilder.toString();
	}

	private static String CreatePageInput(String beanPath,String bean,String fileName) {
		// 读取源文件
		System.out.println(beanPath);
		List<String> beanContent = FileUtil.ReadFileByNameToArray(beanPath, "utf-8");
		StringBuilder _StringBuilder = new StringBuilder();
		for (String string : beanContent) {
			//@ApiModel(value="Agent对象", description="代理商表")
			if (string.contains("@ApiModel(")) {
				string="@ApiModel(value=\""+bean+fileName+"分页输入对象\")";
				StringUtil.addRow(_StringBuilder, string);
				continue;
			}
			if (string.contains(" @TableId(value")) {
				string="	@ApiModelProperty(value = \"唯一ID,修改的时候必须传递\")";
				StringUtil.addRow(_StringBuilder, string);
				continue;
			}
			
			if (string.contains(" @Override")) {
				continue;
			}
			if (string.contains("*")) {
				continue;
			}
			if (string.contains("serialVersionUID")) {
				continue;
			}
			if (string.contains("com.baomidou.mybatisplus.annotation.IdType")) {
				continue;
			}
			if (string.contains("com.baomidou.mybatisplus.annotation.TableId")) {
				continue;
			}
			if (string.contains("com.baomidou.mybatisplus.annotation.TableField")) {
				continue;
			}
			if (string.contains("com.baomidou.mybatisplus.extension.activerecord.Model")) {
				continue;
			}
			if (string.contains("com.baomidou.mybatisplus.annotation.TableName")) {
				continue;
			}
			//package
			if (string.contains("package xiaoqu.home.open.model")) {
				string="package xiaoqu.home.open.controller.input;";
				StringUtil.addRow(_StringBuilder, string);
				continue;
			}
			if (string.contains("@TableName")|| string.contains("@TableField")) {
				continue;
			}
			if (string.contains("public class")) {
				string="public class "+bean+fileName+" extends PageInput {";
				StringUtil.addRow(_StringBuilder, string);
			} else {
				StringUtil.addRow(_StringBuilder, string);
			}
		}
		
		return _StringBuilder.toString();
	}
	private static String CreateInput(String beanPath,String bean,String fileName) {
		// 读取源文件
		List<String> beanContent = FileUtil.ReadFileByNameToArray(beanPath, "utf-8");
		StringBuilder _StringBuilder = new StringBuilder();
		for (String string : beanContent) {
			//@ApiModel(value="Agent对象", description="代理商表")
			if (string.contains("@ApiModel(")) {
				string="@ApiModel(value=\""+bean+"添加输入对象\")";
				StringUtil.addRow(_StringBuilder, string);
				continue;
			}
			if (string.contains(" @Override")) {
				continue;
			}
			if (string.contains("*")) {
				continue;
			}
			if (string.contains("serialVersionUID")) {
				continue;
			}
			if (string.contains("com.baomidou.mybatisplus.annotation.IdType")) {
				continue;
			}
			if (string.contains("com.baomidou.mybatisplus.annotation.TableId")) {
				continue;
			}
			if (string.contains("com.baomidou.mybatisplus.annotation.TableField")) {
				continue;
			}
			if (string.contains("com.baomidou.mybatisplus.extension.activerecord.Model")) {
				continue;
			}
			if (string.contains("com.baomidou.mybatisplus.annotation.TableName")) {
				continue;
			}
			//package
			if (string.contains("package xiaoqu.home.open.model")) {
				string="package xiaoqu.home.open.controller.insert;";
				StringUtil.addRow(_StringBuilder, string);
				continue;
			}
			if (string.contains("@TableName")|| string.contains("@TableField")) {
				continue;
			}
			if (string.contains(" @TableId(value")) {
				string="	@ApiModelProperty(value = \"唯一ID,修改的时候必须传递\")";
				StringUtil.addRow(_StringBuilder, string);
				continue;
			}
			if (string.contains("public class")) {
				string="public class "+bean+fileName+" {";
				StringUtil.addRow(_StringBuilder, string);
			} else {
				StringUtil.addRow(_StringBuilder, string);
			}
		}

		return _StringBuilder.toString();
	}
	private static String CreateOutput(String importPacke,String bean,String fileName) {
		StringBuilder _StringBuilder = new StringBuilder();
		StringUtil.addRow(_StringBuilder, "package xiaoqu.home.open.controller.output;");
		StringUtil.addRow(_StringBuilder, "");
		StringUtil.addRow(_StringBuilder, "import "+importPacke+"."+bean+";");
		StringUtil.addRow(_StringBuilder, "import io.swagger.annotations.ApiModel;");
		StringUtil.addRow(_StringBuilder, "import io.swagger.annotations.ApiModelProperty;");
		StringUtil.addRow(_StringBuilder, "");
		StringUtil.addRow(_StringBuilder, "/**");
		StringUtil.addRow(_StringBuilder, " * ");
		StringUtil.addRow(_StringBuilder, " * @author wh");
		StringUtil.addRow(_StringBuilder, " * @version 1.0");
		StringUtil.addRow(_StringBuilder, " * @desc autoCreateoutput");
		StringUtil.addRow(_StringBuilder, " */");
		String string="@ApiModel(value=\""+bean+fileName+"分页输入对象\")";
		StringUtil.addRow(_StringBuilder, string);
		StringUtil.addRow(_StringBuilder, "public class "+bean+fileName+"  extends "+bean+" {");
		StringUtil.addRow(_StringBuilder, "");
		StringUtil.addRow(_StringBuilder, "	private static final long serialVersionUID=1L;");
		StringUtil.addRow(_StringBuilder, "");
		StringUtil.addRow(_StringBuilder, "}");
		return _StringBuilder.toString();
	}

}
