|
@@ -0,0 +1,177 @@
|
|
|
+package com.rongwei.bscommon.sys.service.impl;
|
|
|
+
|
|
|
+import com.lowagie.text.*;
|
|
|
+import com.lowagie.text.Font;
|
|
|
+import com.lowagie.text.rtf.RtfWriter2;
|
|
|
+import com.rongwei.bscommon.sys.dao.DmTableMapper;
|
|
|
+import com.rongwei.bscommon.sys.service.DmTableService;
|
|
|
+import com.rongwei.bsentity.vo.DmTableField;
|
|
|
+import com.rongwei.bsentity.vo.DmTableInfo;
|
|
|
+import org.springframework.beans.factory.annotation.Autowired;
|
|
|
+import org.springframework.stereotype.Service;
|
|
|
+
|
|
|
+import java.awt.*;
|
|
|
+import java.io.File;
|
|
|
+import java.io.FileOutputStream;
|
|
|
+import java.io.IOException;
|
|
|
+import java.util.List;
|
|
|
+
|
|
|
+@Service()
|
|
|
+public class DmTableServiceImpl implements DmTableService {
|
|
|
+ @Autowired
|
|
|
+ private DmTableMapper dmTableMapper;
|
|
|
+
|
|
|
+ private final static String SCHEMA_NAME = "INCONTROL";
|
|
|
+ private final static String ROOT_PATH = "/Users/zhuang/Downloads";
|
|
|
+ private final static String FILE_NAME = "数据库设计文档.doc";
|
|
|
+ private static final String[] PREFIXES = {
|
|
|
+ "SYS_", "VT", "WF_", "APIDOC_", "CMS_", "FORM_", "HIBERNATE_", "MT_", "RL_", "T_"
|
|
|
+ };
|
|
|
+ @Override
|
|
|
+ public void tableInfo2Word() {
|
|
|
+ List<DmTableInfo> tables = dmTableMapper.findAllTables(SCHEMA_NAME);
|
|
|
+ Document document = new Document(PageSize.A4);
|
|
|
+ try {
|
|
|
+
|
|
|
+ // 创建文件
|
|
|
+ File file = createFile();
|
|
|
+
|
|
|
+ // 写入文件信息
|
|
|
+ RtfWriter2.getInstance(document, new FileOutputStream(file));
|
|
|
+ document.open();
|
|
|
+
|
|
|
+ // 文档标题
|
|
|
+ setDocTitle(document);
|
|
|
+
|
|
|
+ int i = 0;
|
|
|
+ for (DmTableInfo tableInfo : tables) {
|
|
|
+ Boolean a = false;
|
|
|
+ for (String prefix : PREFIXES) {
|
|
|
+ if (tableInfo.getTableName().startsWith(prefix)) {
|
|
|
+ a = true;
|
|
|
+ }
|
|
|
+ }
|
|
|
+ if (!a) {
|
|
|
+ // 标题
|
|
|
+ setTableTitle(document, tableInfo, ++i);
|
|
|
+
|
|
|
+ // 创建表格
|
|
|
+ Table table = getTable();
|
|
|
+ // 设置表格头部
|
|
|
+ setTableHeader(table);
|
|
|
+ // 表格的主体
|
|
|
+ setTableBody(tableInfo.getTableName(), table);
|
|
|
+ // 生成表格
|
|
|
+ document.add(table);
|
|
|
+
|
|
|
+ // 换行
|
|
|
+ document.add(new Paragraph(""));
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+ document.close();
|
|
|
+ } catch (Exception e) {
|
|
|
+ System.out.println("生成数据库设计文档失败!:"+ e);
|
|
|
+ }
|
|
|
+ }
|
|
|
+ private void setDocTitle(Document document) throws DocumentException {
|
|
|
+ Font font = new Font(Font.NORMAL, 24, Font.NORMAL, new Color(0, 0, 0));
|
|
|
+ Paragraph paragraph = new Paragraph("数据库设计文档", font);
|
|
|
+ paragraph.setAlignment(1);
|
|
|
+ document.add(paragraph);
|
|
|
+ }
|
|
|
+
|
|
|
+ private File createFile() throws IOException {
|
|
|
+ File dir = new File(ROOT_PATH);
|
|
|
+ boolean result = dir.mkdirs();
|
|
|
+ //log.info("创建根目录结果:{}", result);
|
|
|
+ System.out.println("创建根目录结果:{}"+result);
|
|
|
+ // 创建文件
|
|
|
+ File file = new File(ROOT_PATH + File.separator + FILE_NAME);
|
|
|
+ if (file.exists() && file.isFile()) {
|
|
|
+ boolean deleteFlag = file.delete();
|
|
|
+ //log.info("删除旧doc文件结果:{}", deleteFlag);
|
|
|
+ System.out.println("删除旧doc文件结果:{}"+deleteFlag);
|
|
|
+ }
|
|
|
+ result = file.createNewFile();
|
|
|
+ //log.info("创建文件结果:{}", result);
|
|
|
+ System.out.println("创建文件结果:{}"+result);
|
|
|
+ return file;
|
|
|
+ }
|
|
|
+
|
|
|
+ private void setTableTitle(Document document, DmTableInfo tableInfo, int num) throws DocumentException {
|
|
|
+ String tableComment = tableInfo.getTableComment();
|
|
|
+ tableComment = tableComment == null ? "" : "(" + tableComment + ")";
|
|
|
+ String tableTitle = num + ". 表名称: " + tableInfo.getTableName() + tableComment;
|
|
|
+ document.add(new Paragraph(tableTitle));
|
|
|
+ }
|
|
|
+
|
|
|
+ private Table getTable() throws DocumentException {
|
|
|
+ Table table = new Table(5);
|
|
|
+ // 表格宽度占比
|
|
|
+ table.setWidth(90.0f);
|
|
|
+
|
|
|
+ // 每列宽度
|
|
|
+ int[] widths = {5, 30, 20, 10, 30};
|
|
|
+ table.setWidths(widths);
|
|
|
+
|
|
|
+ table.setBorderWidth(1);
|
|
|
+ table.setPadding(0);
|
|
|
+ table.setSpacing(0);
|
|
|
+ return table;
|
|
|
+ }
|
|
|
+
|
|
|
+ private void setTableHeader(Table table) {
|
|
|
+ //添加表头的元素,并设置表头背景的颜色
|
|
|
+ Color color = new Color(176, 196, 222);
|
|
|
+
|
|
|
+ Cell cell = new Cell("编号");
|
|
|
+ addTableHeader(table, cell, color);
|
|
|
+ cell = new Cell("字段名");
|
|
|
+ addTableHeader(table, cell, color);
|
|
|
+ cell = new Cell("类型");
|
|
|
+ addTableHeader(table, cell, color);
|
|
|
+ cell = new Cell("非空");
|
|
|
+ addTableHeader(table, cell, color);
|
|
|
+ cell = new Cell("注释");
|
|
|
+ addTableHeader(table, cell, color);
|
|
|
+
|
|
|
+ // 表头结束
|
|
|
+ table.endHeaders();
|
|
|
+ }
|
|
|
+
|
|
|
+ private void setTableBody(String tableName, Table table) {
|
|
|
+ List<DmTableField> fields = dmTableMapper.findFieldsByTableName(SCHEMA_NAME, tableName);
|
|
|
+ for (int i = 0; i < fields.size(); i++) {
|
|
|
+ addContent(table, (i + 1) + "");
|
|
|
+ addContent(table, fields.get(i).getColumnName());
|
|
|
+
|
|
|
+ String type = fields.get(i).getDataType();
|
|
|
+ if (!"CLOB".equals(type)) {
|
|
|
+ type += "(" + fields.get(i).getDataLength() + ")";
|
|
|
+ }
|
|
|
+ addContent(table, type);
|
|
|
+
|
|
|
+ addContent(table, "Y".equals(fields.get(i).getNullable()) ? "否" : "是");
|
|
|
+ addContent(table, fields.get(i).getComments());
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+ /**
|
|
|
+ * 添加表头到表格
|
|
|
+ */
|
|
|
+ private void addTableHeader(Table table, Cell cell, Color color) {
|
|
|
+ cell.setHorizontalAlignment(Element.ALIGN_CENTER);
|
|
|
+ cell.setBackgroundColor(color);
|
|
|
+ table.addCell(cell);
|
|
|
+ }
|
|
|
+
|
|
|
+ /**
|
|
|
+ * 添加内容到表格
|
|
|
+ */
|
|
|
+ private void addContent(Table table, String content) {
|
|
|
+ Cell cell = new Cell(content);
|
|
|
+ cell.setHorizontalAlignment(Element.ALIGN_CENTER);
|
|
|
+ table.addCell(cell);
|
|
|
+ }
|
|
|
+}
|