| | |
| | | import org.springblade.core.tool.utils.Func; |
| | | import org.springblade.core.tool.utils.StringPool; |
| | | import org.springframework.beans.factory.annotation.Autowired; |
| | | import org.springframework.beans.factory.annotation.Value; |
| | | import org.springframework.cache.Cache; |
| | | import org.springframework.stereotype.Service; |
| | | import org.springframework.transaction.annotation.Transactional; |
| | |
| | | import java.time.ZoneId; |
| | | import java.time.ZonedDateTime; |
| | | import java.util.*; |
| | | import java.util.concurrent.ConcurrentHashMap; |
| | | import java.util.concurrent.*; |
| | | import java.util.stream.Collectors; |
| | | |
| | | import static com.vci.ubcs.code.constant.FrameWorkLangCodeConstant.DATA_OID_NOT_EXIST; |
| | |
| | | @Service |
| | | public class MdmEngineServiceImpl implements MdmEngineService { |
| | | |
| | | /** |
| | | * 单次sql的最多导入数量 |
| | | */ |
| | | @Value("${import.maxNum}") |
| | | private Integer MAX_IMPORT_NUM; |
| | | |
| | | /** |
| | | * 模板的服务 |
| | |
| | | throw new VciBaseException("类型转换错误:" + e.toString()); |
| | | } |
| | | }); |
| | | return commonsMapper.insertByBaseModel(listR.getData().get(0).getTableName(), maps.get(0), maps); |
| | | try { |
| | | bactchExecuteInsert(listR.getData().get(0).getTableName(),maps); |
| | | }catch (Exception e){ |
| | | throw new ServiceException("分批执行insert语句报错:"+e.getMessage()); |
| | | } |
| | | return maps.size(); |
| | | } |
| | | |
| | | /** |
| | | * 分批执行insert语句 |
| | | * @param tableName |
| | | * @param maps |
| | | * @throws ServiceException |
| | | */ |
| | | @Transactional(rollbackFor = Exception.class) |
| | | void bactchExecuteInsert(String tableName, List<Map<String, String>> maps) throws ServiceException{ |
| | | ExecutorService executor = Executors.newFixedThreadPool(10); // 创建一个固定大小的线程池 |
| | | List<Map<String, String>> threadSafeMaps = new CopyOnWriteArrayList<>(maps); |
| | | for (int i = 0; i < threadSafeMaps.size(); i += MAX_IMPORT_NUM) { |
| | | final int startIndex = i; |
| | | final int endIndex = Math.min(i + MAX_IMPORT_NUM, maps.size()); |
| | | |
| | | executor.execute(() -> { |
| | | List<Map<String, String>> subList = threadSafeMaps.subList(startIndex, endIndex); |
| | | // 调用插入数据库的方法 |
| | | commonsMapper.insertByBaseModel(tableName, threadSafeMaps.get(0), subList); |
| | | }); |
| | | } |
| | | // 关闭线程池 |
| | | executor.shutdown(); |
| | | try { |
| | | // 等待所有任务执行完成 |
| | | executor.awaitTermination(Long.MAX_VALUE, TimeUnit.NANOSECONDS); |
| | | } catch (InterruptedException e) { |
| | | // 处理异常 |
| | | throw new ServiceException("多线程方式执行批量插入时产生错误:"+e.getMessage()); |
| | | } |
| | | } |
| | | |
| | | /** |
| | | * 传入业务类型以及ID查询业务表数据是否重复 |
| | | * |
| | | * @param btmType 业务类型 |