| | |
| | | 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.PlatformTransactionManager; |
| | | import org.springframework.transaction.TransactionStatus; |
| | | import org.springframework.transaction.annotation.Transactional; |
| | | import org.springframework.transaction.support.DefaultTransactionDefinition; |
| | | import org.springframework.transaction.support.TransactionTemplate; |
| | | import org.springframework.util.CollectionUtils; |
| | | |
| | | import javax.annotation.Resource; |
| | |
| | | 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 { |
| | | |
| | | /** |
| | | * 多线程方式批量执行开启线程池的总数 |
| | | */ |
| | | @Value("${batchadd.thread_num:10}") |
| | | private Integer THREAD_NUM; |
| | | |
| | | /** |
| | | * 单次sql的最多导入数量 |
| | | */ |
| | | @Value("${batchadd.single_maxnum:200}") |
| | | private Integer MAX_IMPORT_NUM; |
| | | |
| | | /** |
| | | * 是否开启多线程方式导入历史数据 |
| | | */ |
| | | @Value("${batchadd.is_thread_import:false}") |
| | | private boolean IS_THREAD_IMPORT; |
| | | |
| | | /** |
| | | * 模板的服务 |
| | |
| | | @Autowired |
| | | private FormulaServiceImpl formulaService; |
| | | |
| | | |
| | | /** |
| | | * 对omd中提供的feign接口进行调用,以及处理相关逻辑 |
| | | */ |
| | | @Autowired |
| | | ICodeReferBtmTypeService codeReferBtmTypeService; |
| | | |
| | | |
| | | /** |
| | | * 相似项查询规则 |
| | |
| | | * @param orderDTO 申请的信息,需要包含属性的内容和码段相关的内容 |
| | | * @return 返回编码的内容 |
| | | */ |
| | | private String addSaveCode(CodeOrderDTO orderDTO,boolean authUser) throws Exception { |
| | | private String addSaveCode(CodeOrderDTO orderDTO, boolean authUser) throws Exception { |
| | | VciBaseUtil.alertNotNull(orderDTO, "编码申请相关的属性和码段的内容都为空", orderDTO.getCodeClassifyOid(), "主题库分类的主键", |
| | | orderDTO.getTemplateOid(), "模板的主键", orderDTO.getCodeRuleOid(), "编码规则的主键"); |
| | | CodeClassifyFullInfoBO classifyFullInfo = classifyService.getClassifyFullInfo(orderDTO.getCodeClassifyOid()); |
| | |
| | | logger.error("设置默认的属性的值错误", e); |
| | | } |
| | | } |
| | | |
| | | |
| | | /** |
| | | * 初始化业务类型 |
| | |
| | | cbo.setName(orderDTO.getName()); |
| | | //数据的时候,编码是不变的 |
| | | cbo.setCreateTime(cbo.getCreateTime()); |
| | | cbo.setCreator(cbo.getCreator()); |
| | | cbo.setLastModifyTime(cbo.getLastModifyTime()); |
| | | cbo.setTenantId(AuthUtil.getTenantId()); |
| | | oldCbo.setLastV("0"); |
| | |
| | | }).collect(Collectors.toSet()); |
| | | //将bean转为map,mybatis统一处理 |
| | | List<Map<String, String>> maps = new ArrayList<>(); |
| | | |
| | | baseModels.stream().forEach(model -> { |
| | | try { |
| | | maps.add(VciBaseUtil.convertBean2Map(model,existFild)); |
| | |
| | | throw new VciBaseException("类型转换错误:" + e.toString()); |
| | | } |
| | | }); |
| | | return commonsMapper.insertByBaseModel(listR.getData().get(0).getTableName(), maps.get(0), maps); |
| | | // 是否开启多线程执行插入语句 |
| | | if(IS_THREAD_IMPORT){ |
| | | try { |
| | | threadBactchExecuteInsert(listR.getData().get(0).getTableName(),maps); |
| | | }catch (Exception e){ |
| | | throw new ServiceException("分批执行insert语句报错:"+e.getMessage()); |
| | | } |
| | | }else { |
| | | bacthExcecuteInsert(listR.getData().get(0).getTableName(),maps); |
| | | } |
| | | |
| | | return maps.size(); |
| | | } |
| | | |
| | | /** |
| | | * 多线程方式分批执行insert语句 |
| | | * @param tableName |
| | | * @param maps |
| | | * @throws ServiceException |
| | | */ |
| | | private void threadBactchExecuteInsert(String tableName, List<Map<String, String>> maps) throws ServiceException{ |
| | | ExecutorService executor = Executors.newFixedThreadPool(THREAD_NUM); // 创建一个固定大小的线程池 |
| | | 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, threadSafeMaps.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()); |
| | | } |
| | | } |
| | | |
| | | /** |
| | | * 单线程方式分批执行 |
| | | * @param tableName |
| | | * @param maps |
| | | */ |
| | | private void bacthExcecuteInsert(String tableName, List<Map<String, String>> maps){ |
| | | for (int i = 0; i < maps.size(); i += MAX_IMPORT_NUM) { |
| | | final int startIndex = i; |
| | | final int endIndex = Math.min(i + MAX_IMPORT_NUM, maps.size()); |
| | | List<Map<String, String>> subList = maps.subList(startIndex, endIndex); |
| | | // 调用插入数据库的方法 |
| | | commonsMapper.insertByBaseModel(tableName, maps.get(0), subList); |
| | | } |
| | | } |
| | | |
| | | /** |
| | | * 传入业务类型以及ID查询业务表数据是否重复 |
| | |
| | | toBo.setLastV(String.valueOf(1)); |
| | | // 数据升版不需要重新设置创建人,只对最后修改人做更新就可以了 |
| | | //toBo.setCreator(String.valueOf(AuthUtil.getUser().getUserId())); |
| | | toBo.setCreateTime(new Date()); |
| | | toBo.setCreateTime(fromBo.getCreateTime()); |
| | | // toBo.setLastModifier(String.valueOf(AuthUtil.getUser().getUserId())); |
| | | toBo.setLastModifier(String.valueOf(AuthUtil.getUser().getAccount())); |
| | | toBo.setLastModifyTime(new Date()); |