diff --git a/modules/export/src/main/resources/domains/CommonBoolean.xml b/modules/export/src/main/resources/domains/CommonBoolean.xml index 0a283bf..f541200 100644 --- a/modules/export/src/main/resources/domains/CommonBoolean.xml +++ b/modules/export/src/main/resources/domains/CommonBoolean.xml @@ -1,6 +1,6 @@ - + diff --git a/modules/export/src/main/resources/domains/ExportStrategy.xml b/modules/export/src/main/resources/domains/ExportStrategy.xml index 6120f70..a086401 100644 --- a/modules/export/src/main/resources/domains/ExportStrategy.xml +++ b/modules/export/src/main/resources/domains/ExportStrategy.xml @@ -5,7 +5,7 @@ - + diff --git a/modules/export/src/main/resources/domains/ExportTemplate.xml b/modules/export/src/main/resources/domains/ExportTemplate.xml index 932a784..2d2ad89 100644 --- a/modules/export/src/main/resources/domains/ExportTemplate.xml +++ b/modules/export/src/main/resources/domains/ExportTemplate.xml @@ -5,6 +5,6 @@ - + diff --git a/src/main/java/com/hypass/export/config/ExtendModule.java b/src/main/java/com/hypass/export/config/ExtendModule.java new file mode 100644 index 0000000..207f1ef --- /dev/null +++ b/src/main/java/com/hypass/export/config/ExtendModule.java @@ -0,0 +1,16 @@ +package com.hypass.export.config; + +import com.google.inject.Inject; +import com.hypaas.app.HypaasModule; +import com.hypass.export.service.IExportService; +import com.hypass.export.service.impl.ExportServiceImpl; +import org.springframework.context.ApplicationContext; + +public class ExtendModule extends HypaasModule { + @Inject private ApplicationContext context; + + @Override + protected void configure() { + bind(IExportService.class).to(ExportServiceImpl.class); + } +} diff --git a/src/main/java/com/hypass/export/controller/ExportController.java b/src/main/java/com/hypass/export/controller/ExportController.java new file mode 100644 index 0000000..c952610 --- /dev/null +++ b/src/main/java/com/hypass/export/controller/ExportController.java @@ -0,0 +1,119 @@ +package com.hypass.export.controller; + +import com.google.inject.Inject; +import com.google.inject.servlet.RequestScoped; +import com.hypaas.db.JpaSupport; +import com.hypaas.rpc.Response; +import com.hypass.export.model.CascadeModelFieldDTO; +import com.hypass.export.model.ExportStrategyDTO; +import com.hypass.export.model.ExportTemplateDTO; +import com.hypass.export.model.MetaModelDTO; +import com.hypass.export.service.IExportService; +import java.lang.reflect.InvocationTargetException; +import java.util.List; +import javax.ws.rs.*; +import javax.ws.rs.core.MediaType; + +/** + * @ClassName InventoryMgrController @Description TODO @Author Will @Date 2023/7/23 18:58 @Version + * 1.0 + */ +@RequestScoped +@Produces(MediaType.APPLICATION_JSON) +@Path("/export") +public class ExportController extends JpaSupport { + + @Inject private IExportService exportService; + + @GET + @Consumes(MediaType.APPLICATION_JSON) + @Path("/getCascadeModelFieldList") + public Response getCascadeModelFieldList( + @QueryParam("modelName") String modelName, @QueryParam("fieldName") String fieldName) { + Response response = new Response(); + List list = exportService.getCascadeModelFieldList(modelName, fieldName); + response.setData(list); + response.setStatus(Response.STATUS_SUCCESS); + return response; + } + + @POST + @Consumes(MediaType.APPLICATION_JSON) + @Path("/saveTemplate") + public Response saveTemplate(ExportTemplateDTO exportTemplateDTO) { + Response response = new Response(); + response.setStatus(Response.STATUS_SUCCESS); + Boolean result = false; + try { + result = exportService.saveTemplate(exportTemplateDTO); + } catch (InvocationTargetException e) { + result = false; + response.setStatus(Response.STATUS_FAILURE); + } catch (IllegalAccessException e) { + result = false; + response.setStatus(Response.STATUS_FAILURE); + } + response.setData(result); + return response; + } + + /** + * 查询模型列表 + * + * @return + */ + @GET + @Path("/getModelList") + public Response getModelList() { + List modelList = exportService.getModelList(); + Response response = new Response(); + response.setData(modelList); + response.setStatus(Response.STATUS_SUCCESS); + return response; + } + + @GET + @Consumes(MediaType.APPLICATION_JSON) + @Path("/preConfigExportStrategy") + public Response preConfigExportStrategy( + @QueryParam("modelName") String modelName, @QueryParam("templateId") String templateId) { + List exportStrategyDTOS = null; + Response response = new Response(); + exportStrategyDTOS = exportService.preConfigExportStrategy(modelName, templateId); + response.setData(exportStrategyDTOS); + response.setStatus(Response.STATUS_SUCCESS); + return response; + } + + @GET + @Consumes(MediaType.APPLICATION_JSON) + @Path("/getModelExportStrategy") + public Response getModelExportStrategy(@QueryParam("templateId") String templateId) { + ExportStrategyDTO modelExportStrategy = null; + Response response = new Response(); + try { + modelExportStrategy = exportService.getModelExportStrategy(templateId); + } catch (InvocationTargetException e) { + response.setData(null); + response.setStatus(Response.STATUS_FAILURE); + } catch (IllegalAccessException e) { + response.setData(null); + response.setStatus(Response.STATUS_FAILURE); + } + response.setData(modelExportStrategy); + response.setStatus(Response.STATUS_SUCCESS); + return response; + } + + @POST + @Consumes(MediaType.APPLICATION_JSON) + @Path("/saveTemplateStrategy") + public Response saveTemplateStrategy(ExportStrategyDTO templateStrategyDTO) { + Boolean result = null; + Response response = new Response(); + result = exportService.saveTemplateStrategy(templateStrategyDTO); + response.setData(result); + response.setStatus(Response.STATUS_SUCCESS); + return response; + } +} diff --git a/src/main/java/com/hypass/export/model/CascadeModelFieldDTO.java b/src/main/java/com/hypass/export/model/CascadeModelFieldDTO.java new file mode 100644 index 0000000..833e3a1 --- /dev/null +++ b/src/main/java/com/hypass/export/model/CascadeModelFieldDTO.java @@ -0,0 +1,25 @@ +package com.hypass.export.model; + +/** @ClassName MetaModelDTO @Description TODO @Author wyj @Date 2023/11/18 15:56 @Version 1.0 */ +public class CascadeModelFieldDTO { + + private String name; + + private String code; + + public String getName() { + return name; + } + + public void setName(String name) { + this.name = name; + } + + public String getCode() { + return code; + } + + public void setCode(String code) { + this.code = code; + } +} diff --git a/src/main/java/com/hypass/export/model/ExportStrategyDTO.java b/src/main/java/com/hypass/export/model/ExportStrategyDTO.java new file mode 100644 index 0000000..1a18371 --- /dev/null +++ b/src/main/java/com/hypass/export/model/ExportStrategyDTO.java @@ -0,0 +1,100 @@ +package com.hypass.export.model; + +import com.export.common.CommonBoolean; +import com.hypaas.auth.db.AuditableModel; +import javax.persistence.*; + +public class ExportStrategyDTO extends AuditableModel { + + private Long id; + + private String fieldName; + + private String fieldType; + + private String fieldAttr; + + private CommonBoolean isExport; + + private String outContent; + + private String exportFiledName; + + private String exportContent; + + private Integer templateId; + + public Integer getTemplateId() { + return templateId; + } + + public void setTemplateId(Integer templateId) { + this.templateId = templateId; + } + + @Override + public Long getId() { + return id; + } + + @Override + public void setId(Long id) { + this.id = id; + } + + public String getFieldName() { + return fieldName; + } + + public void setFieldName(String fieldName) { + this.fieldName = fieldName; + } + + public String getFieldType() { + return fieldType; + } + + public void setFieldType(String fieldType) { + this.fieldType = fieldType; + } + + public String getFieldAttr() { + return fieldAttr; + } + + public void setFieldAttr(String fieldAttr) { + this.fieldAttr = fieldAttr; + } + + public CommonBoolean getIsExport() { + return isExport; + } + + public void setIsExport(CommonBoolean isExport) { + this.isExport = isExport; + } + + public String getOutContent() { + return outContent; + } + + public void setOutContent(String outContent) { + this.outContent = outContent; + } + + public String getExportFiledName() { + return exportFiledName; + } + + public void setExportFiledName(String exportFiledName) { + this.exportFiledName = exportFiledName; + } + + public String getExportContent() { + return exportContent; + } + + public void setExportContent(String exportContent) { + this.exportContent = exportContent; + } +} diff --git a/src/main/java/com/hypass/export/model/ExportTemplateDTO.java b/src/main/java/com/hypass/export/model/ExportTemplateDTO.java new file mode 100644 index 0000000..3032a84 --- /dev/null +++ b/src/main/java/com/hypass/export/model/ExportTemplateDTO.java @@ -0,0 +1,61 @@ +package com.hypass.export.model; + +import com.export.common.CommonBoolean; +import com.hypaas.meta.db.MetaJsonModel; +import javax.persistence.*; + +/** + * @ClassName ExportTemplateDTO @Description TODO @Author wyj @Date 2023/11/24 10:19 @Version 1.0 + */ +public class ExportTemplateDTO { + + private Long id; + + private String code; + + private String name; + + private MetaJsonModel entityName; + + private CommonBoolean isDefaultTemplate; + + public Long getId() { + return id; + } + + public void setId(Long id) { + this.id = id; + } + + public String getCode() { + return code; + } + + public void setCode(String code) { + this.code = code; + } + + public String getName() { + return name; + } + + public void setName(String name) { + this.name = name; + } + + public MetaJsonModel getEntityName() { + return entityName; + } + + public void setEntityName(MetaJsonModel entityName) { + this.entityName = entityName; + } + + public CommonBoolean getIsDefaultTemplate() { + return isDefaultTemplate; + } + + public void setIsDefaultTemplate(CommonBoolean isDefaultTemplate) { + this.isDefaultTemplate = isDefaultTemplate; + } +} diff --git a/src/main/java/com/hypass/export/model/MetaModelDTO.java b/src/main/java/com/hypass/export/model/MetaModelDTO.java new file mode 100644 index 0000000..3dc4237 --- /dev/null +++ b/src/main/java/com/hypass/export/model/MetaModelDTO.java @@ -0,0 +1,55 @@ +package com.hypass.export.model; + +/** @ClassName MetaModelDTO @Description TODO @Author wyj @Date 2023/11/18 15:56 @Version 1.0 */ +public class MetaModelDTO { + + private Long id; + + private String name; + + private String packageName; + + private String tableName; + + private String fullName; + + public Long getId() { + return id; + } + + public void setId(Long id) { + this.id = id; + } + + public String getName() { + return name; + } + + public void setName(String name) { + this.name = name; + } + + public String getPackageName() { + return packageName; + } + + public void setPackageName(String packageName) { + this.packageName = packageName; + } + + public String getTableName() { + return tableName; + } + + public void setTableName(String tableName) { + this.tableName = tableName; + } + + public String getFullName() { + return fullName; + } + + public void setFullName(String fullName) { + this.fullName = fullName; + } +} diff --git a/src/main/java/com/hypass/export/service/IExportService.java b/src/main/java/com/hypass/export/service/IExportService.java new file mode 100644 index 0000000..d50da5a --- /dev/null +++ b/src/main/java/com/hypass/export/service/IExportService.java @@ -0,0 +1,25 @@ +package com.hypass.export.service; + +import com.hypass.export.model.CascadeModelFieldDTO; +import com.hypass.export.model.ExportStrategyDTO; +import com.hypass.export.model.ExportTemplateDTO; +import com.hypass.export.model.MetaModelDTO; +import java.lang.reflect.InvocationTargetException; +import java.util.List; + +/** 文件导出接口 */ +public interface IExportService { + List getModelList(); + + ExportStrategyDTO getModelExportStrategy(String templateId) + throws InvocationTargetException, IllegalAccessException; + + boolean saveTemplateStrategy(ExportStrategyDTO exportStrategyDTO); + + List getCascadeModelFieldList(String modelName, String fieldName); + + List preConfigExportStrategy(String modelName, String templateId); + + boolean saveTemplate(ExportTemplateDTO exportTemplateDTO) + throws InvocationTargetException, IllegalAccessException; +} diff --git a/src/main/java/com/hypass/export/service/impl/ExportServiceImpl.java b/src/main/java/com/hypass/export/service/impl/ExportServiceImpl.java new file mode 100644 index 0000000..b349e0a --- /dev/null +++ b/src/main/java/com/hypass/export/service/impl/ExportServiceImpl.java @@ -0,0 +1,151 @@ +package com.hypass.export.service.impl; + +import com.export.common.CommonBoolean; +import com.google.inject.Inject; +import com.hypaas.db.Query; +import com.hypaas.meta.db.MetaField; +import com.hypaas.meta.db.MetaModel; +import com.hypaas.meta.db.repo.MetaFieldRepository; +import com.hypaas.meta.db.repo.MetaModelRepository; +import com.hypass.export.ExportStrategy; +import com.hypass.export.ExportTemplate; +import com.hypass.export.model.CascadeModelFieldDTO; +import com.hypass.export.model.ExportStrategyDTO; +import com.hypass.export.model.ExportTemplateDTO; +import com.hypass.export.model.MetaModelDTO; +import com.hypass.export.repo.ExportStrategyRepository; +import com.hypass.export.repo.ExportTemplateRepository; +import com.hypass.export.service.IExportService; +import com.hypass.export.utils.HypassBeanUtils; +import java.lang.reflect.InvocationTargetException; +import java.util.ArrayList; +import java.util.List; +import org.apache.commons.collections.CollectionUtils; +import org.apache.commons.lang3.StringUtils; +import org.springframework.beans.BeanUtils; + +/** + * @ClassName ExportServiceImpl @Description TODO @Author wyj @Date 2023/11/18 16:02 @Version 1.0 + */ +public class ExportServiceImpl implements IExportService { + + @Inject private MetaModelRepository metaModelRepository; + + @Inject private ExportStrategyRepository exportStrategyRepository; + + @Inject private ExportTemplateRepository exportTemplateRepository; + + @Inject private MetaFieldRepository metaFieldRepository; + + @Override + public List getModelList() { + List fetch = metaModelRepository.all().fetch(); + if (CollectionUtils.isNotEmpty(fetch)) { + return HypassBeanUtils.copyProperties4List(fetch, MetaModelDTO.class); + } + return null; + } + + @Override + public ExportStrategyDTO getModelExportStrategy(String templateId) + throws InvocationTargetException, IllegalAccessException { + ExportTemplate exportTemplate = exportTemplateRepository.find(Long.valueOf(templateId)); + ExportStrategy exportStrategy = + Query.of(ExportStrategy.class) + .filter("self.templateInfo = :templateInfo") + .bind("templateInfo", exportTemplate) + .fetchOne(); + ExportStrategyDTO exportStrategyDTO = new ExportStrategyDTO(); + HypassBeanUtils.copyProperties(exportStrategy, exportStrategyDTO); + return exportStrategyDTO; + } + + @Override + public boolean saveTemplateStrategy(ExportStrategyDTO exportStrategyDTO) { + ExportStrategy exportStrategy = new ExportStrategy(); + BeanUtils.copyProperties(exportStrategyDTO, exportStrategy); + exportStrategyRepository.save(exportStrategy); + return true; + } + + @Override + public List getCascadeModelFieldList(String modelName, String fieldName) { + MetaModel metaModel = metaModelRepository.findByName(modelName); + MetaField metaField = + Query.of(MetaField.class) + .filter("self.metaModel = :metaModel and self.name = :name") + .bind("metaModel", metaModel) + .bind("name", fieldName) + .fetchOne(); + String typeName = metaField.getTypeName(); + MetaModel cascadeModel = metaModelRepository.findByName(typeName); + List fields = + Query.of(MetaField.class) + .filter("self.metaModel = :metaModel") + .bind("metaModel", cascadeModel) + .fetch(); + if (CollectionUtils.isNotEmpty(fields)) { + List list = new ArrayList<>(); + fields.forEach( + field -> { + CascadeModelFieldDTO cascadeModelDTO = new CascadeModelFieldDTO(); + cascadeModelDTO.setCode(field.getName()); + cascadeModelDTO.setName(field.getLabel()); + list.add(cascadeModelDTO); + }); + return list; + } + return null; + } + + @Override + public List preConfigExportStrategy(String modelName, String templateId) { + List exportStrategyDTOS = new ArrayList<>(); + ExportTemplate exportTemplate = exportTemplateRepository.find(Long.valueOf(templateId)); + if (exportTemplate == null) { + MetaModel metaModel = metaModelRepository.findByName(modelName); + List fields = + Query.of(MetaField.class) + .filter("self.metaModel = :metaModel") + .bind("metaModel", metaModel) + .fetch(); + if (CollectionUtils.isEmpty(fields)) { + return exportStrategyDTOS; + } + fields.forEach( + field -> { + ExportStrategyDTO exportStrategyDTO = new ExportStrategyDTO(); + exportStrategyDTO.setFieldName(field.getName()); + exportStrategyDTO.setFieldType(field.getTypeName()); + if (StringUtils.isEmpty(field.getRelationship())) { + exportStrategyDTO.setFieldAttr("普通"); + } else { + exportStrategyDTO.setFieldAttr(field.getRelationship()); + } + exportStrategyDTO.setIsExport(CommonBoolean.NOT); + }); + } else { + List list = + Query.of(ExportStrategy.class) + .filter("self.templateInfo = :templateInfo") + .bind("templateInfo", exportTemplate) + .fetch(); + + exportStrategyDTOS = HypassBeanUtils.copyProperties4List(list, ExportStrategyDTO.class); + } + return null; + } + + @Override + public boolean saveTemplate(ExportTemplateDTO exportTemplateDTO) + throws InvocationTargetException, IllegalAccessException { + ExportTemplate exportTemplate = new ExportTemplate(); + HypassBeanUtils.copyProperties(exportTemplateDTO, exportTemplate); + if (exportTemplateDTO.getIsDefaultTemplate() != null) { + exportTemplateRepository.save(exportTemplate); + } else { + exportTemplateRepository.merge(exportTemplate); + } + return true; + } +} diff --git a/src/main/java/com/hypass/export/utils/HypassBeanUtils.java b/src/main/java/com/hypass/export/utils/HypassBeanUtils.java new file mode 100644 index 0000000..4330443 --- /dev/null +++ b/src/main/java/com/hypass/export/utils/HypassBeanUtils.java @@ -0,0 +1,29 @@ +package com.hypass.export.utils; + +import cn.hutool.core.bean.BeanUtil; +import java.lang.reflect.InvocationTargetException; +import java.util.ArrayList; +import java.util.List; +import org.springframework.beans.BeanUtils; +import org.springframework.util.CollectionUtils; + +/** @ClassName HypassBeanUtils @Description TODO @Author wyj @Date 2023/2/8 18:26 @Version 1.0 */ +public class HypassBeanUtils { + + public static List copyProperties4List(List sources, Class destination) { + if (CollectionUtils.isEmpty(sources)) { + return new ArrayList<>(); + } + + List list = new ArrayList<>(); + for (Object source : sources) { + list.add(BeanUtil.copyProperties(source, destination)); + } + return list; + } + + public static void copyProperties(Object orig, Object dest) + throws InvocationTargetException, IllegalAccessException { + BeanUtils.copyProperties(orig, dest); + } +}