提交 588c4409 authored 作者: chenshiying's avatar chenshiying

[新增] 生成sql

上级 9ac3d957
package com.loit.common.insertSql;
import com.loit.common.insertSql.dto.ColumnInfoDTO;
import com.loit.common.insertSql.dto.LineInfoDTO;
import com.loit.common.utils.ListUtil;
import com.loit.common.utils.StringUtils;
import com.loit.common.utils.excel.ImportExcel;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.beanutils.BeanUtils;
import org.apache.http.entity.ContentType;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.mock.web.MockMultipartFile;
import org.springframework.web.multipart.MultipartFile;
import java.io.File;
import java.io.FileInputStream;
import java.util.ArrayList;
import java.util.LinkedHashMap;
import java.util.List;
import java.util.Map;
@Slf4j
public class GeneratorInsertSqlScript {
private static Logger logger = LoggerFactory.getLogger(GeneratorInsertSqlScript.class);
//TODO 1:事件,2:部件
public static final String CASE_TYPE = "2";
public static final String FILE_NAME = "loit_bc_process_config_node数据初始化.xlsx";
public static Map<String, String> columnNameMap = new LinkedHashMap<>();
public static Map<String, String> columnTypeMap = new LinkedHashMap<>();
public static int maxIndex = 100;
public static String tableName;
protected static String root_path = "F:\\9Git140\\loit-build-common\\loit-build-component\\loit-build-deploy-env\\src\\main\\resources";
public static void main(String[] args) {
try {
String filePathStr = "F:\\9Git140\\loit-build-common\\loit-build-component\\loit-build-deploy-env\\src\\main\\resources\\" + FILE_NAME;
File pdfFile = new File(filePathStr);
FileInputStream fileInputStream = new FileInputStream(pdfFile);
MultipartFile multipartFile = new MockMultipartFile(pdfFile.getName(), pdfFile.getName(),
ContentType.APPLICATION_OCTET_STREAM.toString(), fileInputStream);
ImportExcel ei = new ImportExcel(multipartFile, 0, 0);
List<LineInfoDTO> commandManualDTOList = ei.getDataList(LineInfoDTO.class);
if (ListUtil.isEmpty(commandManualDTOList)) {
return;
}
tableName = commandManualDTOList.get(0).getCol1();
LineInfoDTO columnLineInfo = commandManualDTOList.get(1);
for (int i = 0; i < 24; i++) {
int index = i + 1;
String colName = BeanUtils.getProperty(columnLineInfo, "col" + index);
columnNameMap.put(i + "", colName);
}
LineInfoDTO columnTypeInfo = commandManualDTOList.get(2);
for (int colIndex = 0; colIndex < 24; colIndex++) {
int index = colIndex + 1;
String colName = BeanUtils.getProperty(columnTypeInfo, "col" + index);
if (StringUtils.isNotEmpty(colName)) {
columnTypeMap.put(colIndex + "", colName);
}
}
for (int valueIndex = 0; valueIndex < commandManualDTOList.size(); valueIndex++) {
if (valueIndex <= 2) {
continue;
}
List<ColumnInfoDTO> columnInfoList = new ArrayList<>();
LineInfoDTO columnValueInfo = commandManualDTOList.get(valueIndex);
boolean isDataLine = true;
for (int colIndex = 0; colIndex < 24; colIndex++) {
int index = colIndex + 1;
String colValue = BeanUtils.getProperty(columnValueInfo, "col" + index);
if (index == 1 && StringUtils.isEmpty(colValue)) {
isDataLine = false;
break;
}
String columnName = columnNameMap.get(colIndex + "");
if (StringUtils.isEmpty(columnName)) {
continue;
}
ColumnInfoDTO columnInfoDTO = new ColumnInfoDTO();
columnInfoDTO.setColumnValue(colValue);
columnInfoDTO.setTableName(tableName);
columnInfoDTO.setColumnName(columnName);
columnInfoDTO.setColumnType(columnTypeMap.get(colIndex + ""));
columnInfoList.add(columnInfoDTO);
}
if (isDataLine) {
createInsertSql(columnInfoList);
}
}
} catch (Exception e) {
log.error(e.getMessage(), e);
}
}
public static void createInsertSql(List<ColumnInfoDTO> columnInfoList) {
StringBuffer sql = new StringBuffer("INSERT INTO `");
sql.append(tableName);
sql.append("` (");
sql.append(columNameStr(columnInfoList));
sql.append(") VALUES (");
sql.append(columnValueStr(columnInfoList));
sql.append(");");
System.out.println(sql.toString());
}
public static String columNameStr(List<ColumnInfoDTO> columnInfoList) {
StringBuilder resultBuilder = new StringBuilder();
for (ColumnInfoDTO columnInfoDTO : columnInfoList) {
resultBuilder.append("`" + columnInfoDTO.getColumnName() + "`").append(", ");
}
String result = resultBuilder.toString();
if (result.endsWith(", ")) {
result = result.substring(0, result.length() - 2);
}
return result;
}
public static String columnValueStr(List<ColumnInfoDTO> columnInfoList) {
StringBuilder resultBuilder = new StringBuilder();
for (ColumnInfoDTO columnInfoDTO : columnInfoList) {
String columnType = columnInfoDTO.getColumnType();
String columnValue = columnInfoDTO.getColumnValue();
if (StringUtils.isEmpty(columnValue)) {
resultBuilder.append("NULL").append(", ");
continue;
}
if (StringUtils.isEmpty(columnType)) {
resultBuilder.append("'" + columnInfoDTO.getColumnValue() + "'").append(", ");
} else {
resultBuilder.append(columnInfoDTO.getColumnType() + "'" + columnInfoDTO.getColumnValue() + "'").append(", ");
}
}
String result = resultBuilder.toString();
if (result.endsWith(", ")) {
result = result.substring(0, result.length() - 2);
}
return result;
}
}
package com.loit.common.insertSql.dto;
import io.swagger.annotations.ApiModel;
import io.swagger.annotations.ApiModelProperty;
import java.io.Serializable;
@ApiModel(value = "CoumnInfoDTO", description = "字段信息")
public class ColumnInfoDTO implements Serializable {
private static final long serialVersionUID = -6587921299183759035L;
@ApiModelProperty(value = "tableName")
private String tableName;
@ApiModelProperty(value = "columnName")
private String columnName;
@ApiModelProperty(value = "columnType")
private String columnType;
@ApiModelProperty(value = "columnValue")
private String columnValue;
public String getTableName() {
return tableName;
}
public void setTableName(String tableName) {
this.tableName = tableName;
}
public String getColumnName() {
return columnName;
}
public void setColumnName(String columnName) {
this.columnName = columnName;
}
public String getColumnType() {
return columnType;
}
public void setColumnType(String columnType) {
this.columnType = columnType;
}
public String getColumnValue() {
return columnValue;
}
public void setColumnValue(String columnValue) {
this.columnValue = columnValue;
}
}
package com.loit.common.insertSql.dto;
import com.loit.common.utils.excel.annotation.ExcelField;
import io.swagger.annotations.ApiModel;
import io.swagger.annotations.ApiModelProperty;
import java.io.Serializable;
@ApiModel(value = "LineInfoDTO", description = "行信息")
public class LineInfoDTO implements Serializable {
private static final long serialVersionUID = -6587921299183759035L;
@ApiModelProperty(value = "col1")
@ExcelField(title = "col1", sort = 1)
private String col1;
@ApiModelProperty(value = "col2")
@ExcelField(title = "col2", sort = 2)
private String col2;
@ApiModelProperty(value = "col3")
@ExcelField(title = "col3", sort = 3)
private String col3;
@ApiModelProperty(value = "col4")
@ExcelField(title = "col4", sort = 4)
private String col4;
@ApiModelProperty(value = "col5")
@ExcelField(title = "col5", sort = 5)
private String col5;
@ApiModelProperty(value = "col6")
@ExcelField(title = "col6", sort = 6)
private String col6;
@ApiModelProperty(value = "col7")
@ExcelField(title = "col7", sort = 7)
private String col7;
@ApiModelProperty(value = "col8")
@ExcelField(title = "col8", sort = 8)
private String col8;
@ApiModelProperty(value = "col9")
@ExcelField(title = "col9", sort = 8)
private String col9;
@ApiModelProperty(value = "col10")
@ExcelField(title = "col10", sort = 8)
private String col10;
@ApiModelProperty(value = "col11")
@ExcelField(title = "col11", sort = 8)
private String col11;
@ApiModelProperty(value = "col12")
@ExcelField(title = "col12", sort = 8)
private String col12;
@ApiModelProperty(value = "col13")
@ExcelField(title = "col13", sort = 8)
private String col13;
@ApiModelProperty(value = "col14")
@ExcelField(title = "col14", sort = 8)
private String col14;
@ApiModelProperty(value = "col15")
@ExcelField(title = "col15", sort = 8)
private String col15;
@ApiModelProperty(value = "col16")
@ExcelField(title = "col16", sort = 8)
private String col16;
@ApiModelProperty(value = "col17")
@ExcelField(title = "col17", sort = 8)
private String col17;
@ApiModelProperty(value = "col18")
@ExcelField(title = "col18", sort = 8)
private String col18;
@ApiModelProperty(value = "col19")
@ExcelField(title = "col19", sort = 8)
private String col19;
@ApiModelProperty(value = "col20")
@ExcelField(title = "col20", sort = 8)
private String col20;
@ApiModelProperty(value = "col21")
@ExcelField(title = "col21", sort = 8)
private String col21;
@ApiModelProperty(value = "col22")
@ExcelField(title = "col22", sort = 8)
private String col22;
@ApiModelProperty(value = "col23")
@ExcelField(title = "col23", sort = 8)
private String col23;
@ApiModelProperty(value = "col24")
@ExcelField(title = "col24", sort = 8)
private String col24;
public String getCol1() {
return col1;
}
public void setCol1(String col1) {
this.col1 = col1;
}
public String getCol2() {
return col2;
}
public void setCol2(String col2) {
this.col2 = col2;
}
public String getCol3() {
return col3;
}
public void setCol3(String col3) {
this.col3 = col3;
}
public String getCol4() {
return col4;
}
public void setCol4(String col4) {
this.col4 = col4;
}
public String getCol5() {
return col5;
}
public void setCol5(String col5) {
this.col5 = col5;
}
public String getCol6() {
return col6;
}
public void setCol6(String col6) {
this.col6 = col6;
}
public String getCol7() {
return col7;
}
public void setCol7(String col7) {
this.col7 = col7;
}
public String getCol8() {
return col8;
}
public void setCol8(String col8) {
this.col8 = col8;
}
public String getCol9() {
return col9;
}
public void setCol9(String col9) {
this.col9 = col9;
}
public String getCol10() {
return col10;
}
public void setCol10(String col10) {
this.col10 = col10;
}
public String getCol11() {
return col11;
}
public void setCol11(String col11) {
this.col11 = col11;
}
public String getCol12() {
return col12;
}
public void setCol12(String col12) {
this.col12 = col12;
}
public String getCol13() {
return col13;
}
public void setCol13(String col13) {
this.col13 = col13;
}
public String getCol14() {
return col14;
}
public void setCol14(String col14) {
this.col14 = col14;
}
public String getCol15() {
return col15;
}
public void setCol15(String col15) {
this.col15 = col15;
}
public String getCol16() {
return col16;
}
public void setCol16(String col16) {
this.col16 = col16;
}
public String getCol17() {
return col17;
}
public void setCol17(String col17) {
this.col17 = col17;
}
public String getCol18() {
return col18;
}
public void setCol18(String col18) {
this.col18 = col18;
}
public String getCol19() {
return col19;
}
public void setCol19(String col19) {
this.col19 = col19;
}
public String getCol20() {
return col20;
}
public void setCol20(String col20) {
this.col20 = col20;
}
public String getCol21() {
return col21;
}
public void setCol21(String col21) {
this.col21 = col21;
}
public String getCol22() {
return col22;
}
public void setCol22(String col22) {
this.col22 = col22;
}
public String getCol23() {
return col23;
}
public void setCol23(String col23) {
this.col23 = col23;
}
public String getCol24() {
return col24;
}
public void setCol24(String col24) {
this.col24 = col24;
}
public String getCol25() {
return col25;
}
public void setCol25(String col25) {
this.col25 = col25;
}
@ApiModelProperty(value = "col25")
@ExcelField(title = "col25", sort = 8)
private String col25;
}
Markdown 格式
0%
您添加了 0 到此讨论。请谨慎行事。
请先完成此评论的编辑!
注册 或者 后发表评论