提交 9ce3c14b 编写于 作者: ibizdev's avatar ibizdev

Mosher 发布系统代码 [后台服务,演示应用]

上级 cf35628b
package cn.ibizlab.core.sample.mapping;
import cn.ibizlab.core.sample.domain.IBIZBOOK;
import org.mapstruct.*;
import java.util.List;
@Mapper(componentModel = "spring", uses = {})
public interface IBIZBOOKDataImport {
@Mappings({
@Mapping(target = "ibizbookid", source = "ibizbookid"),
@Mapping(target = "type", source = "type"),
@Mapping(target = "press", source = "press"),
@Mapping(target = "author", source = "author"),
@Mapping(target = "price", source = "price"),
@Mapping(target = "ibizbookname", source = "ibizbookname"),
})
@BeanMapping(ignoreByDefault = true)
IBIZBOOK toDomain(IBIZBOOK entity);
List<IBIZBOOK> toDomain(List<IBIZBOOK> entities);
}
......@@ -8,10 +8,10 @@ import java.util.List;
public interface IBIZOrderImport {
@Mappings({
@Mapping(target = "ibizorderid", source = "ibizorderid"),
@Mapping(target = "ibizordername", source = "ibizordername"),
@Mapping(target = "ordertype", source = "ordertype"),
@Mapping(target = "ordertime", source = "ordertime"),
@Mapping(target = "orderuid", source = "orderuid"),
@Mapping(target = "ibizordername", source = "ibizordername"),
@Mapping(target = "ordertype", source = "ordertype"),
})
@BeanMapping(ignoreByDefault = true)
IBIZOrder toDomain(IBIZOrder entity);
......
......@@ -37,6 +37,10 @@ public interface IIBIZBOOKService extends IService<IBIZBOOK> {
boolean save(IBIZBOOK et);
void saveBatch(List<IBIZBOOK> list);
Page<IBIZBOOK> searchDefault(IBIZBOOKSearchContext context);
JSONObject importData(List<IBIZBOOK> entities, int batchSize, boolean isIgnoreError);
@Async("asyncExecutor")
void asyncImportData(List<IBIZBOOK> entities, int batchSize, boolean isIgnoreError);
/**
*自定义查询SQL
* @param sql select * from table where id =#{et.param}
......
......@@ -213,6 +213,174 @@ public class IBIZBOOKServiceImpl extends ServiceImpl<IBIZBOOKMapper, IBIZBOOK> i
}
}
@Autowired
@Lazy
IIBIZBOOKService proxyService;
@Value("${ibiz.syncImportLimit:1000}")
private int syncImportLimit;
/**
* 上传数据检查
* @param entities
* @param isIgnoreError
* @return
*/
private JSONObject testImportData(List<IBIZBOOK> entities,boolean isIgnoreError) {
JSONObject rs=new JSONObject();
Set ids=new HashSet<>();
List<String> errorMsgs = new ArrayList<>();
List<Integer> errorLines = new ArrayList<>();
List<IBIZBOOK> duplicateKeys=new ArrayList<>();
String keyField= DEFieldCacheMap.getDEKeyField(IBIZBOOK.class);
if(ObjectUtils.isEmpty(keyField)){
errorLines.add(1);
rs.put("rst", 1);
rs.put("msg", "数据导入失败,未能获取到实体[IBIZBOOK]的主键属性");
rs.put("errorLines", errorLines);
return rs;
}
//主键重复性判断.外键约束判断(上传数据自身的检查/数据库的检查)
for(int i=0;i<entities.size();i++) {
IBIZBOOK entity = entities.get(i);
Object id = entity.getIbizbookid();
if(ObjectUtils.isEmpty(id)) {
id = entity.getDefaultKey(true);
if(ObjectUtils.isEmpty(id)){
Integer lineNum = i + 1;
errorLines.add(lineNum);
errorMsgs.add("第" + lineNum + "行:无法获取当前数据主键。");
continue;
}
else{
entity.setIbizbookid((String) 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(duplicateKeys.size()>0){
for(IBIZBOOK 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 isIgnoreError
* @return
*/
@Override
@Transactional
public JSONObject importData(List<IBIZBOOK> entities, int batchSize ,boolean isIgnoreError) {
if(entities.size()>syncImportLimit){
proxyService.asyncImportData(entities,batchSize,isIgnoreError);
JSONObject rs=new JSONObject();
rs.put("rst", 0);
rs.put("msg",String.format("当前导入数据已超过同步导入数量上限[%s],系统正在进行异步导入,请稍后!",syncImportLimit));
rs.put("data",entities);
return rs;
}
else{
return syncImportData(entities,batchSize,isIgnoreError);
}
}
@Override
@Transactional
public void asyncImportData(List<IBIZBOOK> entities, int batchSize ,boolean isIgnoreError){
executeImportData(entities,batchSize,isIgnoreError);
}
@Transactional
public JSONObject syncImportData(List<IBIZBOOK> entities, int batchSize ,boolean isIgnoreError){
return executeImportData(entities,batchSize,isIgnoreError);
}
@Transactional
public JSONObject executeImportData(List<IBIZBOOK> entities, int batchSize ,boolean isIgnoreError) {
JSONObject rs=testImportData(entities,isIgnoreError);
if(rs.getInteger("rst")==1 && !isIgnoreError) {
return rs;
}
List<IBIZBOOK> tempDEList=new ArrayList<>();
Set tempIds=new HashSet<>();
for(int i=0;i<entities.size();i++) {
IBIZBOOK entity = entities.get(i);
tempDEList.add(entity);
Object id=entity.getIbizbookid();
if(!ObjectUtils.isEmpty(id)) {
tempIds.add(id);
}
if(tempDEList.size()>=batchSize || (tempDEList.size()<batchSize && i==entities.size()-1)){
commit(tempDEList,tempIds);
tempDEList.clear();
tempIds.clear();
}
}
rs.put("rst", 0);
rs.put("data",entities);
return rs;
}
/**
* 批量提交
* @param entities 数据
* @param ids 要提交数据的id
*/
@Transactional
public void commit(List<IBIZBOOK> entities, Set ids){
List<IBIZBOOK> _create=new ArrayList<>();
List<IBIZBOOK> _update=new ArrayList<>();
Set oldIds=new HashSet<>();
if(ids.size()>0){
List<IBIZBOOK> oldEntities=this.listByIds(ids);
for(IBIZBOOK entity:oldEntities){
oldIds.add(entity.getIbizbookid());
}
}
for(IBIZBOOK entity:entities){
Object id=entity.getIbizbookid();
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);
}
}
......
......@@ -28,7 +28,7 @@
<!--输出实体[IBIZBOOK]数据结构 -->
<changeSet author="a_LAB01_df847bdfd" id="tab-ibizbook-82-2">
<changeSet author="a_LAB01_df847bdfd" id="tab-ibizbook-86-2">
<createTable tableName="T_IBIZBOOK">
<column name="CREATEMAN" remarks="" type="VARCHAR(60)">
</column>
......
......@@ -159,5 +159,23 @@ public class IBIZBOOKResource {
}
@Autowired
cn.ibizlab.core.sample.mapping.IBIZBOOKDataImport dataimportImpMapping;
@RequestMapping(method = RequestMethod.POST, value = "/ibizbooks/import")
public ResponseEntity<JSONObject> importData(@RequestParam(value = "config") String config , @RequestBody List<IBIZBOOK> 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{
if("DataImport".equals(config)){
rs=ibizbookService.importData(dataimportImpMapping.toDomain(dtos),1000,false);
}
return ResponseEntity.status(HttpStatus.OK).body(rs);
}
}
}
Markdown 格式
0% or
您添加了 0 到此讨论。请谨慎行事。
先完成此消息的编辑!
想要评论请 注册