8 天以前 d640da3dac5b5f811284ab9a7c386da1e7ab6739
src/main/java/com/ruoyi/common/utils/OrderUtils.java
@@ -2,10 +2,6 @@
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.mapper.BaseMapper;
import com.baomidou.mybatisplus.core.toolkit.CollectionUtils;
import com.ruoyi.common.utils.uuid.UUID;
import org.apache.poi.ss.formula.functions.T;
import org.springframework.stereotype.Component;
import java.time.LocalDate;
import java.time.LocalDateTime;
@@ -13,7 +9,6 @@
import java.time.ZoneId;
import java.time.format.DateTimeFormatter;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
@@ -25,90 +20,88 @@
    /**
     * List<Integer> 转换为 Long[] 数组
     * @param ids
     * @return
     * @param ids ids
     * @return Long[]
     */
    public static Long[] listIntegerToLongArray(List<Integer> ids) {
        return ids.stream()
                // 处理null值:如果元素为null,转换为0L(可根据业务调整,比如抛异常)
                .map(id -> id != null ? id.longValue() : -1L)
                // 将Stream<Long>转换为Long[]数组
                .toArray(Long[]::new);
    }
    /**
     * 判断目标id是否在逗号分隔的字符串中
     * @param targetId
     * @param str
     * @return
     * @param targetId targetId
     * @param str source
     * @return boolean
     */
    public boolean isStaffIdExist(Object targetId,String str) {
        // 空值校验,避免空指针
        if (str == null || str.trim().isEmpty() || targetId == null) {
            return false;
        }
        // 按逗号分割成数组
        String[] idArray = str.split(",");
        // 遍历数组判断是否包含目标id
        for (String id : idArray) {
            // 去除空格(防止字符串中有多余空格,如"1, 121")
            String cleanId = id.trim();
            // 转换为数字并比较
            try {
                if (cleanId.equals(String.valueOf(targetId))) {
                    return true;
                }
            } catch (NumberFormatException e) {
                // 若存在非数字ID,直接返回false
                return false;
            }
        }
        return false;
    }
    /**
     * 查询当天(基于createTime字段)的记录数量
     * @param mapper 实体类对应的BaseMapper
     * @param <T> 实体类泛型
     * @return 当天记录数量
     * code 表编码字段
     * 查询当天基于 create_time 的最新编号,并生成下一个编号
     * @param mapper mapper
     * @param preFix 编号前缀
     * @param code 编号字段
     * @param <T> 实体类型
     * @return 订单编号
     */
    public static <T> String countTodayByCreateTime(BaseMapper<T> mapper,String preFix,String code) {
        // 1. 当天时间范围
        LocalDateTime todayStart = LocalDateTime.of(LocalDate.now(), LocalTime.MIN);
        LocalDateTime todayEnd = LocalDateTime.of(LocalDate.now(), LocalTime.MAX);
        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;
        Date startDate = Date.from(todayStart.atZone(ZoneId.systemDefault()).toInstant());
        Date endDate = Date.from(todayEnd.atZone(ZoneId.systemDefault()).toInstant());
        // 2. 日期字符串 yyyyMMdd
        String dateStr = LocalDate.now().format(DateTimeFormatter.BASIC_ISO_DATE);
        // 4. 查询今天最后一条编号(按时间倒序,取最新一条)
        QueryWrapper<T> wrapper = new QueryWrapper<>();
        wrapper.ge("create_time", startDate)
                .lt("create_time", endDate)
                .orderByDesc("create_time")
        wrapper.select(code)
                .ge("create_time", todayStart)
                .lt("create_time", tomorrowStart)
                .likeRight(code, codePrefix)
                .orderByDesc(code)
                .last("LIMIT 1");
        // 这里必须用 selectMaps 或 selectMap,不要用 selectOne,避免实体类问题
        Map<String, Object> result = (Map<String, Object>) mapper.selectOne(wrapper);
        long nextSeq = 1;
        if (result != null && result.get(code) != null) {
            String fullCode = result.get(code).toString(); // 例如:ABC20250122005
            // ✅ 关键:截取最后 3 位序号
            String seqStr = fullCode.substring(fullCode.length() - 3);
            nextSeq = Long.parseLong(seqStr) + 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;
            }
        }
        // 5. 生成编号
        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字段)的记录数量
@@ -117,29 +110,23 @@
     * @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);   // 小于当天结束(避免毫秒精度问题)
        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));
    }
}