2 天以前 c477506a6d672f71c6353608f3cf3424d8026790
src/main/java/com/ruoyi/common/utils/OrderUtils.java
@@ -99,4 +99,88 @@
        // 拼接订单编号 preFix + 时间(yyyyMMdd) + 订单数量(001)
        return preFix + LocalDate.now().format(DateTimeFormatter.ISO_LOCAL_DATE).replaceAll("-", "") + String.format("%03d", (aLong + 1)) + "-" + new Date().getTime();
    }
    /**
     * 查询当天基于 create_time 的最新编号,并生成下一个编号
     * @param mapper mapper
     * @param preFix 编号前缀
     * @param code 编号字段
     * @param <T> 实体类型
     * @return 订单编号
     */
    public static <T> String countTodayByCreateTime(BaseMapper<T> mapper,String preFix,String code) {
        LocalDate today = LocalDate.now();
        LocalDateTime todayStart = today.atStartOfDay();
        LocalDateTime tomorrowStart = today.plusDays(1).atStartOfDay();
        String dateStr = today.format(DateTimeFormatter.BASIC_ISO_DATE);
        String codePrefix = preFix + dateStr;
        QueryWrapper<T> wrapper = new QueryWrapper<>();
        wrapper.select(code)
                .ge("create_time", todayStart)
                .lt("create_time", tomorrowStart)
                .likeRight(code, codePrefix)
                .orderByDesc(code)
                .last("LIMIT 1");
        long nextSeq = 1;
        List<Map<String, Object>> records = mapper.selectMaps(wrapper);
        if (!records.isEmpty()) {
            Object lastCode = records.get(0).get(code);
            if (lastCode != null) {
                nextSeq = extractSequence(lastCode.toString(), codePrefix) + 1;
            }
        }
        return preFix + dateStr + String.format("%03d", nextSeq);
    }
    private static long extractSequence(String fullCode, String codePrefix) {
        if (!fullCode.startsWith(codePrefix)) {
            return 0;
        }
        String seqStr = fullCode.substring(codePrefix.length()).trim();
        if (seqStr.isEmpty()) {
            return 0;
        }
        try {
            return Long.parseLong(seqStr);
        } catch (NumberFormatException e) {
            return 0;
        }
    }
    /**
     * 查询当天(基于createTime字段)的记录数量
     * @param mapper 实体类对应的BaseMapper
     * @param <T> 实体类泛型
     * @return 当天记录数量
     */
    public static <T> String countAfterServiceTodayByCreateTime(BaseMapper<T> mapper,String preFix) {
        // 获取当天开始时间(00:00:00)
        LocalDateTime todayStart = LocalDateTime.of(
                LocalDateTime.now().toLocalDate(),
                LocalTime.MIN
        );
        // 获取当天结束时间(23:59:59.999)
        LocalDateTime todayEnd = LocalDateTime.of(
                LocalDateTime.now().toLocalDate(),
                LocalTime.MAX
        );
        // 转换为Date类型(如果实体类中createTime是LocalDateTime可直接使用)
        Date startDate = Date.from(todayStart.atZone(ZoneId.systemDefault()).toInstant());
        Date endDate = Date.from(todayEnd.atZone(ZoneId.systemDefault()).toInstant());
        // 构建查询条件
        QueryWrapper<T> queryWrapper = new QueryWrapper<>();
        queryWrapper.ge("create_time", startDate)  // 大于等于当天开始
                .lt("create_time", endDate);   // 小于当天结束(避免毫秒精度问题)
        // 执行查询
        Long aLong = mapper.selectCount(queryWrapper);
        // 拼接订单编号 preFix + 时间(yyyyMMdd) + 订单数量(001)
        return preFix + LocalDate.now().format(DateTimeFormatter.ISO_LOCAL_DATE).replaceAll("-", "") + String.format("%03d", (aLong + 1));
    }
}