From ac8e8998235f247365280d1a1632115c43b10037 Mon Sep 17 00:00:00 2001 From: ludc Date: 星期五, 17 十一月 2023 02:24:30 +0800 Subject: [PATCH] Merge remote-tracking branch 'origin/master' --- Source/UBCS/ubcs-service/ubcs-code/src/main/java/com/vci/ubcs/code/service/impl/MdmEngineServiceImpl.java | 121 +++++++++++++++++++++++++++++++++++----- 1 files changed, 105 insertions(+), 16 deletions(-) diff --git a/Source/UBCS/ubcs-service/ubcs-code/src/main/java/com/vci/ubcs/code/service/impl/MdmEngineServiceImpl.java b/Source/UBCS/ubcs-service/ubcs-code/src/main/java/com/vci/ubcs/code/service/impl/MdmEngineServiceImpl.java index 0d7fb7a..1a85609 100644 --- a/Source/UBCS/ubcs-service/ubcs-code/src/main/java/com/vci/ubcs/code/service/impl/MdmEngineServiceImpl.java +++ b/Source/UBCS/ubcs-service/ubcs-code/src/main/java/com/vci/ubcs/code/service/impl/MdmEngineServiceImpl.java @@ -66,9 +66,14 @@ 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; @@ -81,7 +86,7 @@ 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; @@ -97,6 +102,23 @@ @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; /** * 妯℃澘鐨勬湇鍔� @@ -147,13 +169,11 @@ @Autowired private FormulaServiceImpl formulaService; - /** * 瀵筼md涓彁渚涚殑feign鎺ュ彛杩涜璋冪敤锛屼互鍙婂鐞嗙浉鍏抽�昏緫 */ @Autowired ICodeReferBtmTypeService codeReferBtmTypeService; - /** * 鐩镐技椤规煡璇㈣鍒� @@ -411,7 +431,7 @@ * @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()); @@ -681,14 +701,16 @@ //娌℃湁闄愬埗鍒嗙被锛屼絾鏄竴涓ā鏉垮彧鍙兘鍦ㄤ竴涓笟鍔$被鍨嬮噷闈紝鎵�浠ョ洿鎺ユ煡璇㈣繖涓笟鍔$被鍨嬪嵆鍙� if (!CollectionUtils.isEmpty(conditionMap)) { - conditionMap.put("CODETEMPLATEOID","'" + orderDTO.getTemplateOid() + "'"); + // TODO: 鍚屼竴涓簱鍒ら噸涓嶉渶瑕佸尯鍒嗗垎绫籵id + // conditionMap.put("CODETEMPLATEOID","'" + orderDTO.getTemplateOid() + "'"); // final String[] sql = {"select count(*) from " + VciBaseUtil.getTableName(classifyFullInfo.getTopClassifyVO().getBtmtypeid()) + " t where 1 = 1 "}; R<List<BtmTypeVO>> listR = btmTypeClient.selectByIdCollection(Collections.singletonList(classifyFullInfo.getTopClassifyVO().getBtmTypeId())); // String referTable = VciBaseUtil.getTableName(referVO.getReferType()); if (!listR.isSuccess() || listR.getData().size() == 0) { throw new VciBaseException("浼犲叆涓氬姟绫诲瀷鏈煡璇㈠埌鐩稿簲琛ㄥ崟锛岃妫�鏌ワ紒"); } - final String[] sql = {"select count(*) from " + listR.getData().get(0).getTableName() + " t where 1 = 1 "}; + //final String[] sql = {"select count(*) from " + listR.getData().get(0).getTableName() + " t where 1 = 1 "}; + final String[] sql = {"select id from " + listR.getData().get(0).getTableName() + " t where 1 = 1 "}; conditionMap.forEach((key, value) -> { if(StringUtils.isBlank(value)||value.equals(QueryOptionConstant.ISNULL)) { sql[0] += " and " + key + " is null"; @@ -703,11 +725,16 @@ sql[0] += " and oid != '" + orderDTO.getCopyFromVersion() + "'"; } sql[0] += " and lastR = '1' and lastV = '1' "; + String isParticipateCheckOids = classifyService.selectLeafByParentClassifyOid(classifyFullInfo.getTopClassifyVO().getOid(), classifyFullInfo.getCurrentClassifyVO().getOid()); + if(Func.isNotEmpty(isParticipateCheckOids)){ + sql[0] += " and codeclsfid not in(" + isParticipateCheckOids + ")"; + } // if (boService.queryCountBySql(sql[0], new HashMap<>()) > 0) { - if (Integer.parseInt(commonsMapper.selectById(sql[0]).get(0)) > 0) { + List<String> repeatData = commonsMapper.selectList(sql[0]); + if (!repeatData.isEmpty()) { String ruleInfoMsg = keyRuleVO == null ? "" : "鏌ヨ瑙勫垯锛氬幓闄ょ┖鏍�--{0},蹇界暐澶у皬鍐�--{1},蹇界暐鍏ㄥ崐瑙�--{2},蹇界暐鍏ㄩ儴绌烘牸--{3}"; String[] objs = new String[]{trim ? "鏄�" : "鍚�", ignoreCase ? "鏄�" : "鍚�", ignoreWidth ? "鏄�" : "鍚�", trimAll ? "鏄�" : "鍚�"}; - throw new VciBaseException("鏍规嵁鎮ㄥ~鍐欑殑鍏抽敭灞炴�х殑鍐呭锛岀粨鍚堝叧閿睘鎬ф煡璇㈣鍒欙紝鍙戠幇杩欎釜鏁版嵁宸茬粡鍦ㄧ郴缁熶腑瀛樺湪浜嗐�傝淇!銆�" + ruleInfoMsg, objs); + throw new VciBaseException("鏍规嵁鎮ㄥ~鍐欑殑鍏抽敭灞炴�х殑鍐呭锛岀粨鍚堝叧閿睘鎬ф煡璇㈣鍒欙紝鍙戠幇璇ユ暟鎹凡缁忎笌绯荤粺涓紪鍙蜂负"+repeatData.stream().collect(Collectors.joining(","))+"鐨勬暟鎹噸澶嶃�傝淇!銆�" + ruleInfoMsg, objs); } } } @@ -956,10 +983,10 @@ // cbo.setLcStatus("Editing"); //// cbo.setLcStatus(lifeCycleVO.getStartStatus()); // } else { - cbo.setLcStatus(CodeDefaultLC.EDITING.getValue()); + cbo.setLcStatus(CodeDefaultLC.RELEASED.getValue()); // } } else { - cbo.setLcStatus(CodeDefaultLC.EDITING.getValue()); + cbo.setLcStatus(CodeDefaultLC.RELEASED.getValue()); } } @@ -974,7 +1001,6 @@ logger.error("璁剧疆榛樿鐨勫睘鎬х殑鍊奸敊璇�", e); } } - /** * 鍒濆鍖栦笟鍔$被鍨� @@ -1774,7 +1800,6 @@ } } - /** * 鏍规嵁妯℃澘灞炴�х敓鎴愮浉搴旂殑sql淇℃伅 * @@ -1940,6 +1965,8 @@ String sqlHasPage = pageHelper.getLimit() > 0 ? ("select * from (select A.*,rownum RN from (" + sql + whereSubfixForPage) : sql; String sqlCount = "select count(1) from " + tableName + SPACE + "t" + SPACE + joinTableList.values().stream().collect(Collectors.joining(SPACE)) + (StringUtils.isBlank(whereSql) ? "" : " where ") + whereSql; + String sqlId = "select ID from " + tableName + SPACE + "t" + SPACE + joinTableList.values().stream().collect(Collectors.joining(SPACE)) + + (StringUtils.isBlank(whereSql) ? "" : " where ") + whereSql; CodeTemplateAttrSqlBO sqlBO = new CodeTemplateAttrSqlBO(); sqlBO.setTableName(tableName); sqlBO.setJoinTable(joinTableList); @@ -1948,9 +1975,9 @@ sqlBO.setSqlHasPage(sqlHasPage); sqlBO.setSqlCount(sqlCount); sqlBO.setSqlUnPage(sql); + sqlBO.setSqlId(sqlId); return sqlBO; } - /** * 缁勫悎鏌ヨ鏉′欢鐨剆ql @@ -2754,6 +2781,7 @@ cbo.setName(orderDTO.getName()); //鏁版嵁鐨勬椂鍊欙紝缂栫爜鏄笉鍙樼殑 cbo.setCreateTime(cbo.getCreateTime()); + cbo.setCreator(cbo.getCreator()); cbo.setLastModifyTime(cbo.getLastModifyTime()); cbo.setTenantId(AuthUtil.getTenantId()); oldCbo.setLastV("0"); @@ -3356,7 +3384,6 @@ return getTableDefineByTemplateVO(templateService.getObjectHasAttrByOid(templateOid)); } - /** * 浣跨敤妯℃澘鏄剧ず瀵硅薄杞崲涓鸿〃鏍肩殑淇℃伅锛堝寘鍚墿灞曠殑鎸夐挳锛� * @@ -3541,6 +3568,7 @@ }).collect(Collectors.toSet()); //灏哹ean杞负map,mybatis缁熶竴澶勭悊 List<Map<String, String>> maps = new ArrayList<>(); + baseModels.stream().forEach(model -> { try { maps.add(VciBaseUtil.convertBean2Map(model,existFild)); @@ -3548,8 +3576,69 @@ 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(); } + + /** + * 澶氱嚎绋嬫柟寮忓垎鎵规墽琛宨nsert璇彞 + * @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()); + try { + executor.execute(() -> { + List<Map<String, String>> subList = threadSafeMaps.subList(startIndex, endIndex); + // 璋冪敤鎻掑叆鏁版嵁搴撶殑鏂规硶 + commonsMapper.insertByBaseModel(tableName, threadSafeMaps.get(0), subList); + }); + }catch (Throwable e){ + throw new ServiceException(e.getMessage()); + } + } + + // 鍏抽棴绾跨▼姹� + 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鏌ヨ涓氬姟琛ㄦ暟鎹槸鍚﹂噸澶� @@ -4110,7 +4199,7 @@ 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()); -- Gitblit v1.9.3