liusheng
2026-10-12 0c1f8c479065739f3c7a5126de8e667035d00b58
smartor/src/main/java/com/smartor/service/impl/PatMedOuthospServiceImpl.java
@@ -197,7 +197,48 @@
        patMedOuthosp.setUpdateTime(DateUtils.getNowDate());
        patMedOuthosp.setGuid(UUID.randomUUID().toString());
        log.info("[insert] 路由到分表: {}", targetTable);
        return patMedOuthospMapper.insertIntoTable(targetTable, patMedOuthosp);
        try {
            return patMedOuthospMapper.insertIntoTable(targetTable, patMedOuthosp);
        } catch (org.springframework.dao.DuplicateKeyException e) {
            // 加了 serialnum 唯一索引后,极端并发或重复采集会被数据库直接拦下,
            // 这属于"记录已存在",按正常跳过处理(返回0),不要当成入库失败写进错误表
            log.info("[insert] serialnum={} 已存在于表 {}(唯一索引拦截),跳过插入", patMedOuthosp.getSerialnum(), targetTable);
            return 0;
        }
    }
    /**
     * 批量查询已入库的 serialnum:按就诊日期定位分表,一次 IN 查询查出整页已存在的记录
     * <p>
     * 原来每条记录都要 countBySerialnum 一次,一页 500 条就是 500 次 SQL;
     * 门急诊按天全量采集时每天要这么跑 48 轮,这里合并成 1 次查询
     */
    @Override
    public Set<String> selectExistingSerialnums(String admitDateStr, Collection<String> serialnums) {
        Set<String> existing = new HashSet<>();
        if (CollectionUtils.isEmpty(serialnums)) {
            return existing;
        }
        Date admitDate = null;
        if (StringUtils.isNotBlank(admitDateStr)) {
            try {
                admitDate = new java.text.SimpleDateFormat("yyyy-MM-dd").parse(admitDateStr);
            } catch (java.text.ParseException e) {
                log.warn("[selectExistingSerialnums] 就诊日期解析失败:{},按主表查询", admitDateStr);
            }
        }
        // 与插入时的路由规则保持一致,保证查重查的就是要写入的那张表
        String targetTable = admitDate == null ? null : resolveTargetTable(admitDate);
        if (targetTable == null) {
            targetTable = "pat_med_outhosp";
        }
        List<String> existed = patMedOuthospMapper.selectExistingSerialnums(targetTable, serialnums);
        if (!CollectionUtils.isEmpty(existed)) {
            existing.addAll(existed);
        }
        log.info("[selectExistingSerialnums] 日期={},分表={},待查{}条,已存在{}条",
                admitDateStr, targetTable, serialnums.size(), existing.size());
        return existing;
    }
    /**