gongchunyi
2026-06-28 86379aae223a16fed59d46f915ade96ef987b8d2
src/main/java/com/ruoyi/common/utils/OrderUtils.java
@@ -6,6 +6,7 @@
import org.apache.poi.ss.formula.functions.T;
import org.springframework.stereotype.Component;
import java.lang.reflect.Field;
import java.time.LocalDate;
import java.time.LocalDateTime;
import java.time.LocalTime;
@@ -13,6 +14,7 @@
import java.time.format.DateTimeFormatter;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
/**
@@ -21,6 +23,106 @@
 */
public class OrderUtils {
    /**
     * List<Integer> 转换为 Long[] 数组
     * @param ids
     * @return
     */
    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
     */
    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;
    }
    /**
     * 生成当天批次号:前缀 + yyyyMMdd + 连号序号(当天已有同类批次中最大序号+1)
     *
     * <p>规则:查出当天所有 batch 字段以 prefix 开头的记录,取后缀最大数字+1。
     * 格式示例:RK20260627-0001</p>
     *
     * @param mapper   实体类对应的 BaseMapper
     * @param preFix   前缀,如 "RK" / "CK"
     * @param batchField 数据库字段名,如 "inbound_batches" / "outbound_batches"
     * @param <T>      实体类泛型
     * @return 批次号,如 "RK20260627-0001"
     */
    public static <T> String countTodayByCreateTime(BaseMapper<T> mapper, String preFix, String batchField) {
        String dateStr = LocalDate.now().format(DateTimeFormatter.ofPattern("yyyyMMdd"));
        String prefix = preFix + dateStr;
        LocalDateTime todayStart = LocalDateTime.of(LocalDate.now(), LocalTime.MIN);
        LocalDateTime todayEnd = LocalDateTime.of(LocalDate.now(), LocalTime.MAX);
        QueryWrapper<T> qw = new QueryWrapper<>();
        qw.ge("create_time", Date.from(todayStart.atZone(ZoneId.systemDefault()).toInstant()))
                .lt("create_time", Date.from(todayEnd.atZone(ZoneId.systemDefault()).toInstant()))
                .likeRight(batchField, prefix);
        List<T> list = mapper.selectList(qw);
        int maxSeq = 0;
        for (T record : list) {
            try {
                Field f = record.getClass().getDeclaredField(
                        batchField.equals("inbound_batches") ? "inboundBatches" : "outboundBatches");
                f.setAccessible(true);
                String batch = (String) f.get(record);
                if (batch != null && batch.startsWith(prefix + "-")) {
                    String suffix = batch.substring((prefix + "-").length());
                    // 只匹配纯数字序号(1-99999),排除时间戳格式
                    if (suffix.matches("\\d{1,5}")) {
                        int n = Integer.parseInt(suffix);
                        if (n > maxSeq) maxSeq = n;
                    }
                }
            } catch (Exception ignored) {
            }
        }
        return prefix + "-" + String.format("%04d", maxSeq + 1);
    }
    /**
     * 兼容旧签名——默认 batchField 为 "inbound_batches"
     */
    public static <T> String countTodayByCreateTime(BaseMapper<T> mapper, String preFix) {
        return countTodayByCreateTime(mapper, preFix, "inbound_batches");
    }
    /**
     * 查询当天(基于createTime字段)的记录数量
@@ -28,7 +130,7 @@
     * @param <T> 实体类泛型
     * @return 当天记录数量
     */
    public static <T> String countTodayByCreateTime(BaseMapper<T> mapper,String preFix) {
    public static <T> String countAfterServiceTodayByCreateTime(BaseMapper<T> mapper,String preFix) {
        // 获取当天开始时间(00:00:00)
        LocalDateTime todayStart = LocalDateTime.of(
                LocalDateTime.now().toLocalDate(),
@@ -52,6 +154,6 @@
        // 执行查询
        Long aLong = mapper.selectCount(queryWrapper);
        // 拼接订单编号 preFix + 时间(yyyyMMdd) + 订单数量(001)
        return preFix + LocalDate.now().format(DateTimeFormatter.ISO_LOCAL_DATE).replaceAll("-", "") + String.format("%03d", (aLong + 1)) + "-" + new Date().getTime();
        return preFix + LocalDate.now().format(DateTimeFormatter.ISO_LOCAL_DATE).replaceAll("-", "") + String.format("%03d", (aLong + 1));
    }
}