From 8f080783ffc910c8d93e71608c668ac6b5c5c03b Mon Sep 17 00:00:00 2001 From: makejava <1353036300@qq.com> Date: Mon, 16 Aug 2021 18:10:52 +0800 Subject: [PATCH] =?UTF-8?q?=E9=80=90=E6=AD=A5=E6=9B=BF=E6=8D=A2=E6=97=A7?= =?UTF-8?q?=E7=9A=84=E8=A1=A8=E9=85=8D=E7=BD=AE=E6=9C=8D=E5=8A=A1?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../com/sjhy/plugin/dto/ColumnInfoDTO.java | 11 ++ .../com/sjhy/plugin/dto/TableInfoDTO.java | 61 ++++++- .../sjhy/plugin/dto/TableInfoSettingsDTO.java | 40 ++++- .../service/TableInfoSettingsService.java | 9 + .../service/impl/CodeGenerateServiceImpl.java | 18 +- .../impl/TableInfoSettingsServiceImpl.java | 12 ++ .../com/sjhy/plugin/tool/DocCommentUtils.java | 37 ++++ .../com/sjhy/plugin/ui/SelectSavePath.java | 164 +++++++++--------- 8 files changed, 249 insertions(+), 103 deletions(-) create mode 100644 src/main/java/com/sjhy/plugin/tool/DocCommentUtils.java diff --git a/src/main/java/com/sjhy/plugin/dto/ColumnInfoDTO.java b/src/main/java/com/sjhy/plugin/dto/ColumnInfoDTO.java index 65c2de7..372d659 100644 --- a/src/main/java/com/sjhy/plugin/dto/ColumnInfoDTO.java +++ b/src/main/java/com/sjhy/plugin/dto/ColumnInfoDTO.java @@ -1,8 +1,10 @@ package com.sjhy.plugin.dto; import com.intellij.database.model.DasColumn; +import com.intellij.psi.PsiField; import com.sjhy.plugin.entity.TypeMapper; import com.sjhy.plugin.tool.CurrGroupUtils; +import com.sjhy.plugin.tool.DocCommentUtils; import com.sjhy.plugin.tool.NameUtils; import lombok.Data; import lombok.NoArgsConstructor; @@ -21,6 +23,15 @@ import java.util.regex.Pattern; @Data @NoArgsConstructor public class ColumnInfoDTO { + + public ColumnInfoDTO(PsiField field) { + this.name = field.getName(); + this.comment = DocCommentUtils.getComment(field.getDocComment()); + this.type = field.getType().getCanonicalText(); + this.custom = false; + this.ext = new HashMap<>(); + } + public ColumnInfoDTO(DasColumn column) { this.name = NameUtils.getInstance().getJavaName(column.getName()); this.comment = column.getComment(); diff --git a/src/main/java/com/sjhy/plugin/dto/TableInfoDTO.java b/src/main/java/com/sjhy/plugin/dto/TableInfoDTO.java index 56b89cb..87fb20e 100644 --- a/src/main/java/com/sjhy/plugin/dto/TableInfoDTO.java +++ b/src/main/java/com/sjhy/plugin/dto/TableInfoDTO.java @@ -3,12 +3,12 @@ package com.sjhy.plugin.dto; import com.intellij.database.model.DasColumn; import com.intellij.database.psi.DbTable; import com.intellij.database.util.DasUtil; +import com.intellij.psi.PsiClass; +import com.intellij.psi.PsiField; import com.intellij.util.containers.JBIterable; import com.sjhy.plugin.entity.ColumnInfo; import com.sjhy.plugin.entity.TableInfo; -import com.sjhy.plugin.tool.CollectionUtil; -import com.sjhy.plugin.tool.NameUtils; -import com.sjhy.plugin.tool.StringUtils; +import com.sjhy.plugin.tool.*; import lombok.Data; import lombok.NoArgsConstructor; @@ -28,11 +28,31 @@ import java.util.stream.Collectors; @Data @NoArgsConstructor public class TableInfoDTO { + public TableInfoDTO(TableInfoDTO dto, DbTable dbTable) { this(dbTable); merge(dto, this); } + public TableInfoDTO(TableInfoDTO dto, PsiClass psiClass) { + this(psiClass); + merge(dto, this); + } + + private TableInfoDTO(PsiClass psiClass) { + this.name = psiClass.getName(); + this.preName = ""; + this.comment = DocCommentUtils.getComment(psiClass.getDocComment()); + this.templateGroupName = ""; + this.savePackageName = ""; + this.savePath = ""; + this.saveModelName = ""; + this.fullColumn = new ArrayList<>(); + for (PsiField field : psiClass.getAllFields()) { + this.fullColumn.add(new ColumnInfoDTO(field)); + } + } + private TableInfoDTO(DbTable dbTable) { this.name = NameUtils.getInstance().getClassName(dbTable.getName()); this.preName = ""; @@ -129,6 +149,39 @@ public class TableInfoDTO { */ private String saveModelName; + public TableInfo toTableInfo(PsiClass psiClass) { + TableInfo tableInfo = new TableInfo(); + tableInfo.setPsiClassObj(psiClass); + tableInfo.setName(this.getName()); + tableInfo.setPreName(this.getPreName()); + tableInfo.setTemplateGroupName(this.getTemplateGroupName()); + tableInfo.setSavePackageName(this.getSavePackageName()); + tableInfo.setSavePath(this.getSavePath()); + tableInfo.setComment(this.getComment()); + tableInfo.setSaveModelName(this.getSaveModelName()); + tableInfo.setFullColumn(new ArrayList<>()); + tableInfo.setPkColumn(new ArrayList<>()); + tableInfo.setOtherColumn(new ArrayList<>()); + for (PsiField field : psiClass.getAllFields()) { + if (PsiClassGenerateUtils.isSkipField(field)) { + continue; + } + ColumnInfo columnInfo = new ColumnInfo(); + columnInfo.setName(field.getName()); + columnInfo.setShortType(field.getType().getPresentableText()); + columnInfo.setType(field.getType().getCanonicalText()); + columnInfo.setComment(DocCommentUtils.getComment(field.getDocComment())); + columnInfo.setCustom(false); + tableInfo.getFullColumn().add(columnInfo); + if (PsiClassGenerateUtils.isPkField(field)) { + tableInfo.getPkColumn().add(columnInfo); + } else { + tableInfo.getOtherColumn().add(columnInfo); + } + } + return tableInfo; + } + public TableInfo toTableInfo(DbTable dbTable) { TableInfo tableInfo = new TableInfo(); tableInfo.setObj(dbTable); @@ -169,7 +222,7 @@ public class TableInfoDTO { return tableInfo; } - public static TableInfoDTO parse(TableInfo tableInfo) { + public static TableInfoDTO valueOf(TableInfo tableInfo) { TableInfoDTO dto = new TableInfoDTO(); dto.setName(tableInfo.getName()); dto.setTemplateGroupName(tableInfo.getTemplateGroupName()); diff --git a/src/main/java/com/sjhy/plugin/dto/TableInfoSettingsDTO.java b/src/main/java/com/sjhy/plugin/dto/TableInfoSettingsDTO.java index 42193c9..05398f1 100644 --- a/src/main/java/com/sjhy/plugin/dto/TableInfoSettingsDTO.java +++ b/src/main/java/com/sjhy/plugin/dto/TableInfoSettingsDTO.java @@ -3,6 +3,7 @@ package com.sjhy.plugin.dto; import com.intellij.database.model.DasNamespace; import com.intellij.database.psi.DbElement; import com.intellij.database.psi.DbTable; +import com.intellij.psi.PsiClass; import com.sjhy.plugin.entity.TableInfo; import lombok.Data; @@ -47,16 +48,32 @@ public class TableInfoSettingsDTO { return builder.toString(); } + private String generateKey(PsiClass psiClass) { + return psiClass.getQualifiedName(); + } /** - * 读取表信息 + * 读表信息 * - * @param dbTable 原始表对象 - * @return 储存的表信息 + * @param psiClass psi类 + * @return {@link TableInfo} */ + @SuppressWarnings("Duplicates") + public TableInfo readTableInfo(PsiClass psiClass) { + String key = generateKey(psiClass); + TableInfoDTO dto = this.tableInfoMap.get(key); + dto = new TableInfoDTO(dto, psiClass); + this.tableInfoMap.put(key, dto); + return dto.toTableInfo(psiClass); + } + + /** + * 读表信息 + * + * @param dbTable 数据库表 + * @return {@link TableInfo} + */ + @SuppressWarnings("Duplicates") public TableInfo readTableInfo(DbTable dbTable) { - if (dbTable == null) { - return null; - } String key = generateKey(dbTable); TableInfoDTO dto = this.tableInfoMap.get(key); dto = new TableInfoDTO(dto, dbTable); @@ -74,11 +91,16 @@ public class TableInfoSettingsDTO { return; } DbTable dbTable = tableInfo.getObj(); - if (dbTable == null) { + PsiClass psiClass = tableInfo.getPsiClassObj(); + String key; + if (dbTable != null) { + key = generateKey(dbTable); + } else if (psiClass != null) { + key = generateKey(psiClass); + } else { return; } - String key = generateKey(dbTable); - this.tableInfoMap.put(key, TableInfoDTO.parse(tableInfo)); + this.tableInfoMap.put(key, TableInfoDTO.valueOf(tableInfo)); } /** diff --git a/src/main/java/com/sjhy/plugin/service/TableInfoSettingsService.java b/src/main/java/com/sjhy/plugin/service/TableInfoSettingsService.java index b09596d..7f1d80a 100644 --- a/src/main/java/com/sjhy/plugin/service/TableInfoSettingsService.java +++ b/src/main/java/com/sjhy/plugin/service/TableInfoSettingsService.java @@ -3,6 +3,7 @@ package com.sjhy.plugin.service; import com.intellij.database.psi.DbTable; import com.intellij.openapi.components.PersistentStateComponent; import com.intellij.openapi.components.ServiceManager; +import com.intellij.psi.PsiClass; import com.sjhy.plugin.dto.TableInfoSettingsDTO; import com.sjhy.plugin.entity.TableInfo; import com.sjhy.plugin.service.impl.TableInfoSettingsServiceImpl; @@ -31,6 +32,14 @@ public interface TableInfoSettingsService extends PersistentStateComponent templates, boolean unifiedConfig, boolean title, - boolean entityMode) { + boolean entityMode) { // 获取选中表信息 TableInfo selectedTableInfo; List tableInfoList; - if(!entityMode) { + if (!entityMode) { selectedTableInfo = tableInfoService.getTableInfoAndConfig(cacheDataUtils.getSelectDbTable()); tableInfoList = tableInfoService.getTableInfoAndConfig(cacheDataUtils.getDbTableList()); } else { @@ -77,7 +78,13 @@ public class CodeGenerateServiceImpl implements CodeGenerateService { } // 校验选中表的保存路径是否正确 if (StringUtils.isEmpty(selectedTableInfo.getSavePath())) { - Messages.showInfoMessage(selectedTableInfo.getObj().getName() + "表配置信息不正确,请尝试重新配置", GlobalDict.TITLE_INFO); + if (selectedTableInfo.getObj() != null) { + Messages.showInfoMessage(selectedTableInfo.getObj().getName() + "表配置信息不正确,请尝试重新配置", GlobalDict.TITLE_INFO); + } else if (selectedTableInfo.getPsiClassObj() != null) { + Messages.showInfoMessage(selectedTableInfo.getPsiClassObj().getName() + "类配置信息不正确,请尝试重新配置", GlobalDict.TITLE_INFO); + } else { + Messages.showInfoMessage("配置信息不正确,请尝试重新配置", GlobalDict.TITLE_INFO); + } return; } // 将未配置的表进行配置覆盖 @@ -87,6 +94,7 @@ public class CodeGenerateServiceImpl implements CodeGenerateService { tableInfo.setSaveModelName(finalSelectedTableInfo.getSaveModelName()); tableInfo.setSavePackageName(finalSelectedTableInfo.getSavePackageName()); tableInfo.setSavePath(finalSelectedTableInfo.getSavePath()); + tableInfo.setPreName(finalSelectedTableInfo.getPreName()); tableInfoService.save(tableInfo); } }); diff --git a/src/main/java/com/sjhy/plugin/service/impl/TableInfoSettingsServiceImpl.java b/src/main/java/com/sjhy/plugin/service/impl/TableInfoSettingsServiceImpl.java index d9c299c..34e030d 100644 --- a/src/main/java/com/sjhy/plugin/service/impl/TableInfoSettingsServiceImpl.java +++ b/src/main/java/com/sjhy/plugin/service/impl/TableInfoSettingsServiceImpl.java @@ -3,6 +3,7 @@ package com.sjhy.plugin.service.impl; import com.intellij.database.psi.DbTable; import com.intellij.openapi.components.State; import com.intellij.openapi.components.Storage; +import com.intellij.psi.PsiClass; import com.sjhy.plugin.dto.TableInfoSettingsDTO; import com.sjhy.plugin.entity.TableInfo; import com.sjhy.plugin.service.TableInfoSettingsService; @@ -43,6 +44,17 @@ public class TableInfoSettingsServiceImpl implements TableInfoSettingsService { return Objects.requireNonNull(getState()).readTableInfo(dbTable); } + /** + * 获取表信息 + * + * @param psiClass psi类 + * @return {@link TableInfo} + */ + @Override + public TableInfo getTableInfo(PsiClass psiClass) { + return Objects.requireNonNull(getState()).readTableInfo(psiClass); + } + /** * 保存表信息 * diff --git a/src/main/java/com/sjhy/plugin/tool/DocCommentUtils.java b/src/main/java/com/sjhy/plugin/tool/DocCommentUtils.java new file mode 100644 index 0000000..a677330 --- /dev/null +++ b/src/main/java/com/sjhy/plugin/tool/DocCommentUtils.java @@ -0,0 +1,37 @@ +package com.sjhy.plugin.tool; + +import com.intellij.psi.PsiElement; +import com.intellij.psi.javadoc.PsiDocComment; +import com.intellij.psi.javadoc.PsiDocToken; +import org.jetbrains.annotations.Nullable; + +import java.util.Arrays; + +/** + * 文档注释工具类 + * + * @author makejava + * @version 1.0.0 + * @date 2021/08/16 17:37 + */ +public class DocCommentUtils { + + /** + * 获取注释信息,获取第一条文本类型注释内容,不存在则返回null + * + * @param docComment 文档注释 + * @return 注释内容 + */ + public static String getComment(@Nullable PsiDocComment docComment) { + if (docComment == null) { + return null; + } + return Arrays.stream(docComment.getDescriptionElements()) + .filter(o -> o instanceof PsiDocToken) + .map(PsiElement::getText) + .findFirst() + .map(String::trim) + .orElse(null); + } + +} diff --git a/src/main/java/com/sjhy/plugin/ui/SelectSavePath.java b/src/main/java/com/sjhy/plugin/ui/SelectSavePath.java index 5679744..36390da 100644 --- a/src/main/java/com/sjhy/plugin/ui/SelectSavePath.java +++ b/src/main/java/com/sjhy/plugin/ui/SelectSavePath.java @@ -16,7 +16,7 @@ import com.sjhy.plugin.entity.TableInfo; import com.sjhy.plugin.entity.Template; import com.sjhy.plugin.service.CodeGenerateService; import com.sjhy.plugin.service.SettingsStorageService; -import com.sjhy.plugin.service.TableInfoService; +import com.sjhy.plugin.service.TableInfoSettingsService; import com.sjhy.plugin.tool.CacheDataUtils; import com.sjhy.plugin.tool.ModuleUtils; import com.sjhy.plugin.tool.ProjectUtils; @@ -89,7 +89,7 @@ public class SelectSavePath extends DialogWrapper { /** * 表信息服务 */ - private TableInfoService tableInfoService; + private TableInfoSettingsService tableInfoService; /** * 项目对象 */ @@ -125,8 +125,6 @@ public class SelectSavePath extends DialogWrapper { return this.contentPane; } - - /** * 构造方法 */ @@ -134,7 +132,7 @@ public class SelectSavePath extends DialogWrapper { super(project); this.entityMode = entityMode; this.project = project; - this.tableInfoService = TableInfoService.getInstance(project); + this.tableInfoService = TableInfoSettingsService.getInstance(); this.codeGenerateService = CodeGenerateService.getInstance(project); // 初始化module,存在资源路径的排前面 this.moduleList = new LinkedList<>(); @@ -151,84 +149,11 @@ public class SelectSavePath extends DialogWrapper { this.initEvent(); init(); setTitle(GlobalDict.TITLE_INFO); + //初始化路径 + refreshPath(); } private void initEvent() { - - } - - private void refreshData() { - - } - - @Override - protected void doOKAction() { - onOK(); - super.doOKAction(); - } - - /** - * 确认按钮回调事件 - */ - private void onOK() { - List