提交 fdd3455d 编写于 作者: zhouweidong's avatar zhouweidong

实体数据导入

上级 ea5cc5e9
<#ibiztemplate>
TARGET=PSDEDATAIMP
</#ibiztemplate>
package ${pub.getPKGCodeName()}.core.${de.getPSSystemModule().codeName?lower_case}.mapping;
import ${pub.getPKGCodeName()}.core.${de.getPSSystemModule().codeName?lower_case}.domain.${de.codeName};
import org.mapstruct.*;
import java.util.List;
@Mapper(componentModel = "spring", uses = {})
public interface ${de.codeName}${item.codeName} {
<#if item.getPSDEDataImportItems?? && item.getPSDEDataImportItems()??>
@Mappings({
<#list de.getPSDEDataImportItems as importItem>
<#if importItem.getPSDEField()??>
<#assign deFieldName = srfcaseformat(importItem.getPSDEField().getCodeName(),'l_u2lC') >
@Mapping(target = "${deFieldName}",source = "${deFieldName}"),
</#if>
</#list>
})
@BeanMapping(ignoreByDefault = true)
</#if>
${de.codeName} toDomain(${de.codeName} entity);
List<${de.codeName}> toDomain(List<${de.codeName}> entities);
}
......@@ -7,6 +7,14 @@ TARGET=PSDATAENTITY
<#if de.getPSDEFieldByPDT('ORGID',true)?? || de.getPSDEFieldByPDT('ORGSECTORID',true)?? || de.getPSDEFieldByPDT('CREATEMAN',true)?? >
<#assign hasDEPrefield=true>
</#if>
<#assign hasDEImport=false>
<#comment>实体数据导入</#comment>
<#if de.getAllPSDEDataImports?? && de.getAllPSDEDataImports()??>
<#list de.getAllPSDEDataImports() as deImport>
<#assign hasDEImport=true>
<#break>
</#list>
</#if>
<#if de.getStorageMode()==1 || de.getStorageMode()==2 ||de.getStorageMode()==4||de.getStorageMode()==0>
package ${pub.getPKGCodeName()}.core.${item.getPSSystemModule().getCodeName()?lower_case}.service;
......@@ -194,6 +202,10 @@ public interface I${item.codeName}Service{
</#if>
</#list>
</#if>
<#comment>实体数据导入</#comment>
<#if hasDEImport>
JSONObject importData(List<${de.codeName}> entities,int batchSize,boolean isIgnoreError);
</#if>
</#macro>
<#comment>输出测试行为</#comment>
......
......@@ -58,6 +58,14 @@ TARGET=PSDATAENTITY
</#if>
</#list>
</#if>
<#assign hasDEImport=false>
<#comment>实体数据导入</#comment>
<#if de.getAllPSDEDataImports?? && de.getAllPSDEDataImports()??>
<#list de.getAllPSDEDataImports() as deImport>
<#assign hasDEImport=true>
<#break>
</#list>
</#if>
package ${pub.getPKGCodeName()}.core.${item.getPSSystemModule().getCodeName()?lower_case}.service.impl;
import java.io.Serializable;
......@@ -580,6 +588,7 @@ ${deaction.getRender().code}
</#list>
</#if>
<#if nested>
<@autowiredProxyService/>
@Override
public void saveBy${srfcaseformat(MajorField.codeName,'l_u2lC')?cap_first}(${srfjavatype(MajorField.stdDataType)} ${MajorkeyField.codeName?lower_case},List<${item.getCodeName()}> list) {
if(list==null)
......@@ -602,11 +611,11 @@ ${deaction.getRender().code}
_create.add(sub);
}
if(_update.size()>0)
this.updateBatch(_update);
proxyService.updateBatch(_update);
if(_create.size()>0)
this.createBatch(_create);
proxyService.createBatch(_create);
if(delIds.size()>0)
this.removeBatch(delIds);
proxyService.removeBatch(delIds);
}
</#if>
......@@ -865,6 +874,8 @@ ${deaction.getRender().code}
}
</#if>
<#comment>实体数据导入</#comment>
<@deImportData/>
}
<#comment>NOSQL存储</#comment>
......@@ -1258,6 +1269,7 @@ ${deaction.getRender().code}
</#list>
</#if>
<#if nested>
<@autowiredProxyService/>
@Override
public void saveBy${srfcaseformat(MajorField.codeName,'l_u2lC')?cap_first}(${srfjavatype(MajorField.stdDataType)} ${MajorkeyField.codeName?lower_case},List<${item.getCodeName()}> list) {
if(list==null)
......@@ -1280,11 +1292,11 @@ ${deaction.getRender().code}
_create.add(sub);
}
if(_update.size()>0)
this.updateBatch(_update);
proxyService.updateBatch(_update);
if(_create.size()>0)
this.createBatch(_create);
proxyService.createBatch(_create);
if(delIds.size()>0)
this.removeBatch(delIds);
proxyService.removeBatch(delIds);
}
</#if>
......@@ -1348,6 +1360,8 @@ ${deaction.getRender().code}
}
</#if>
<#comment>实体数据导入</#comment>
<@deImportData/>
}
......@@ -1783,6 +1797,7 @@ ${deaction.getRender().code}
</#list>
</#if>
<#if nested>
<@autowiredProxyService/>
@Override
public void saveBy${srfcaseformat(MajorField.codeName,'l_u2lC')?cap_first}(${srfjavatype(MajorField.stdDataType)} ${MajorkeyField.codeName?lower_case},List<${item.getCodeName()}> list) {
if(list==null)
......@@ -1805,11 +1820,11 @@ ${deaction.getRender().code}
_create.add(sub);
}
if(_update.size()>0)
this.updateBatch(_update);
proxyService.updateBatch(_update);
if(_create.size()>0)
this.createBatch(_create);
proxyService.createBatch(_create);
if(delIds.size()>0)
this.removeBatch(delIds);
proxyService.removeBatch(delIds);
}
</#if>
......@@ -1849,6 +1864,9 @@ ${deaction.getRender().code}
</#list>
</#if>
<#comment>实体数据导入</#comment>
<@deImportData/>
}
<#comment>无存储</#comment>
......@@ -2089,6 +2107,7 @@ ${deaction.getRender().code}
</#list>
</#if>
<#if nested>
<@autowiredProxyService/>
@Override
public void saveBy${srfcaseformat(MajorField.codeName,'l_u2lC')?cap_first}(${srfjavatype(MajorField.stdDataType)} ${MajorkeyField.codeName?lower_case},List<${item.getCodeName()}> list) {
if(list==null)
......@@ -2111,11 +2130,11 @@ ${deaction.getRender().code}
_create.add(sub);
}
if(_update.size()>0)
this.updateBatch(_update);
proxyService.updateBatch(_update);
if(_create.size()>0)
this.createBatch(_create);
proxyService.createBatch(_create);
if(delIds.size()>0)
this.removeBatch(delIds);
proxyService.removeBatch(delIds);
}
</#if>
......@@ -2149,6 +2168,8 @@ ${deaction.getRender().code}
</#list>
</#if>
<#comment>实体数据导入</#comment>
<@deImportData/>
}
</#if>
......@@ -2405,4 +2426,174 @@ ${deaction.getRender().code}
</#if>
</#if>
</#macro>
<#comment>注入代理行为</#comment>
<#macro autowiredProxyService>
<#if !P.exists(de.codeName,'proxyService')>
@Autowired
@Lazy
I${de.codeName}Service proxyService;
</#if>
</#macro>
<#comment>实体数据导入</#comment>
<#macro deImportData>
<#if hasDEImport>
<@autowiredProxyService/>
/**
* 上传数据检查
* @param entities
* @param mode
* @return
*/
private JSONObject testImportData(List<${de.codeName}> entities,boolean isIgnoreError) {
JSONObject rs=new JSONObject();
Set ids=new HashSet<>();//主键
List<String> errorMsgs = new ArrayList<>();
List<Integer> errorLines = new ArrayList<>();
List<${de.codeName}> duplicateKeys=new ArrayList<>();
String keyField= DEFieldCacheMap.getDEKeyField(${de.codeName}.class);
if(ObjectUtils.isEmpty(keyField)){
errorLines.add(1);
rs.put("rst", 1);
rs.put("msg", "数据导入失败,未能获取到实体[${de.codeName}]的主键属性");
rs.put("errorLines", errorLines);
return rs;
}
//主键重复性判断.外键约束判断(上传数据自身的检查/数据库的检查)
for(int i=0;i<entities.size();i++) {
${de.codeName} entity = entities.get(i);
Object id = entity.get(keyField);
if(ObjectUtils.isEmpty(id)) {
id = entity.getDefaultKey(true);
entity.set(keyField, id);
}
if(!ids.contains(id)){
ids.add(id);
}
else{
Integer lineNum = i + 1;
errorLines.add(lineNum);
errorMsgs.add("第" + lineNum + "行:导入数据之间存在重复数据。");
if(isIgnoreError){
duplicateKeys.add(entity);
continue;
}
else{
break;
}
}
<#if de.getMajorPSDER1Ns?? && de.getMajorPSDER1Ns()??>
//外键约束检查
<#list de.getMinorPSDERs() as MinorPSDER>
<#if MinorPSDER?? && MinorPSDER.getDERType()=='DER1N' && (MinorPSDER.getMajorPSDataEntity().getStorageMode()==1||MinorPSDER.getMajorPSDataEntity().getStorageMode()==2||MinorPSDER.getMajorPSDataEntity().getStorageMode()==4)>
<#if MinorPSDER.getMajorPSDataEntity?? && MinorPSDER.getMajorPSDataEntity()?? && MinorPSDER.getCodeName?? && MinorPSDER.getCodeName()??>
<#assign MajorKeyFieldCodeName =srfcaseformat(MinorPSDER.getPSPickupDEField().codeName,'l_u2lC')?cap_first>
<#assign MajorEntityCodeName=MinorPSDER.getMajorPSDataEntity().codeName>
<#assign MajorEntityService=srfcaseformat(MinorPSDER.getMajorPSDataEntity().getCodeName(),'l_u2lC')+"Service">
//实体关系[${MinorPSDER.name}]
if(!ObjectUtils.isEmpty(et.get${MajorKeyFieldCodeName}())){
${MajorEntityCodeName} fkEntity=${MajorEntityService}.getById(et.get${MajorKeyFieldCodeName}());
if(ObjectUtils.isEmpty(fkEntity)){
Integer lineNum = i + 1;
errorLines.add(lineNum);
errorMsgs.add(String.format("第" + lineNum + "行:[%s]父数据有误。",et.get${MajorKeyFieldCodeName}()));
if(isIgnoreError){
et.set${MajorKeyFieldCodeName}(null);
continue;
}
else{
break;
}
}
}
</#if>
</#if>
</#list>
</#if>
}
if(duplicateKeys.size()>0){
for(${de.codeName} duplicateKey:duplicateKeys){
entities.remove(duplicateKey);
}
}
if (errorMsgs.size() > 0) {
rs.put("rst", 1);
rs.put("msg", String.join("<br>", errorMsgs));
rs.put("errorLines", errorLines);
return rs;
}
rs.put("rst", 0);
return rs;
}
/**
* 实体数据导入
* @param entities
* @param batchSize
* @param mode
* @return
*/
@Override
@Transactional
public JSONObject importData(List<${de.codeName}> entities, int batchSize ,boolean isIgnoreError) {
JSONObject rs=testImportData(entities,isIgnoreError);
if(rs.getInteger("rst")==1 && !isIgnoreError)
return rs;
String keyField= DEFieldCacheMap.getDEKeyField(${de.codeName}.class);
List<${de.codeName}> tempDEList=new ArrayList<>();
Set tempIds=new HashSet<>();//主键
for(int i=0;i<entities.size();i++) {
${de.codeName} entity = entities.get(i);
tempDEList.add(entity);
tempIds.add(entity.get(keyField));
//分批次提交
if(tempDEList.size()>=batchSize || (tempDEList.size()<batchSize && i==entities.size()-1)){
commit(tempDEList,tempIds);
tempDEList.clear();
tempIds.clear();
}
}
rs.put("rst", 0);
return rs;
}
/**
* 批量提交
* @param entities 数据
* @param ids 要提交数据的id
*/
@Transactional
public void commit(List<${de.codeName}> entities, Set ids){
String keyField= DEFieldCacheMap.getDEKeyField(${de.codeName}.class);
List<${de.codeName}> _create=new ArrayList<>();
List<${de.codeName}> _update=new ArrayList<>();
Set oldIds=new HashSet<>();
if(ids.size()>0){
List<${de.codeName}> oldEntities=this.listByIds(ids);
for(${de.codeName} entity:oldEntities){
oldIds.add(entity.get(keyField));
}
}
for(${de.codeName} entity:entities){
Object id=entity.get(keyField);
if(oldIds.contains(id))
_update.add(entity);
else
_create.add(entity);
}
if(_update.size()>0)
proxyService.updateBatch(_update);
if(_create.size()>0)
proxyService.createBatch(_create);
}
</#if>
</#macro>
</#if>
......@@ -22,6 +22,14 @@ TARGET=PSDESERVICEAPI
<#if de.getPSDEFieldByPDT('ORGID',true)?? || de.getPSDEFieldByPDT('ORGSECTORID',true)?? || de.getPSDEFieldByPDT('CREATEMAN',true)?? >
<#assign hasDEPrefield=true>
</#if>
<#assign hasDEImport=false>
<#comment>实体数据导入</#comment>
<#if de.getAllPSDEDataImports?? && de.getAllPSDEDataImports()??>
<#list de.getAllPSDEDataImports() as deImport>
<#assign hasDEImport=true>
<#break>
</#list>
</#if>
package ${pubPkgCodeName}.${itemSysApiCodeNameLC}.rest;
import java.sql.Timestamp;
......@@ -614,7 +622,37 @@ public class ${itemCodeName}Resource {
</#if>
</#list>
</#if>
<#-- 关系接口 end -->
<#comment>实体数据导入</#comment>
<#if hasDEImport>
<#list de.getAllPSDEDataImports() as deImport>
@Autowired
${pub.getPKGCodeName()}.core.${de.getPSSystemModule().codeName?lower_case}.mapping.${deCodeName}${deImport.codeName} ${deImport.codeName?lower_case}ImpMapping;
</#list>
@RequestMapping(method = RequestMethod.POST, value = "/${srfpluralize(itemCodeNameLC)}/import")
public ResponseEntity<JSONObject> importData(@RequestParam(value = "name") String config , @RequestBody List<${deCodeName}> dtos){
JSONObject rs=new JSONObject();
if(dtos.size()==0){
rs.put("rst", 1);
rs.put("msg", "未传入内容");
return ResponseEntity.status(HttpStatus.NO_CONTENT).body(rs);
}
else{
<#list de.getAllPSDEDataImports() as deImport>
<#if deImport_index==0>
if(config.equals("${deImport.codeName}")){
rs=${deCodeNameLC}Service.importData(${deImport.codeName?lower_case}ImpMapping.toDomain(dtos),${deImport.getBatchSize()},${deImport.isIgnoreError()?c});
}
<#else>
else if(config.equals("${deImport.codeName}")){
rs=${deCodeNameLC}Service.importData(${deImport.codeName?lower_case}ImpMapping.toDomain(dtos),${deImport.getBatchSize()},${deImport.isIgnoreError()?c});
}
</#if>
</#list>
return ResponseEntity.status(HttpStatus.OK).body(rs);
}
}
</#if>
}
</#if>
</#if>
......
......@@ -98,9 +98,9 @@ public class DEFieldDefaultValueAspect
for(Object item:(List)obj) {
if(item instanceof EntityBase) {
if(deFields==null) {
deFields = DEFieldCacheMap.getDEFields(obj.getClass());
deFields = DEFieldCacheMap.getDEFields(item.getClass());
curUser = AuthenticationUser.getAuthenticationUser();
keyField=DEFieldCacheMap.getDEKeyField(obj.getClass());
keyField=DEFieldCacheMap.getDEKeyField(item.getClass());
if(StringUtils.isEmpty(keyField))
return true;
}
......
Markdown 格式
0% or
您添加了 0 到此讨论。请谨慎行事。
先完成此消息的编辑!
想要评论请 注册