liding
2026-04-22 c1babcfa8a9cd01dbeb8527a6a95cbcc431957c7
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
package com.ruoyi.common.utils;
 
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.mapper.BaseMapper;
 
import java.time.LocalDate;
import java.time.LocalDateTime;
import java.time.LocalTime;
import java.time.ZoneId;
import java.time.format.DateTimeFormatter;
import java.util.Date;
import java.util.List;
import java.util.Map;
 
/**
 * @author :yys
 * @date : 2025/9/15 15:31
 */
public class OrderUtils {
 
    /**
     * List<Integer> 转换为 Long[] 数组
     * @param ids ids
     * @return Long[]
     */
    public static Long[] listIntegerToLongArray(List<Integer> ids) {
        return ids.stream()
                .map(id -> id != null ? id.longValue() : -1L)
                .toArray(Long[]::new);
    }
 
    /**
     * 判断目标id是否在逗号分隔的字符串中
     * @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(",");
        for (String id : idArray) {
            String cleanId = id.trim();
            try {
                if (cleanId.equals(String.valueOf(targetId))) {
                    return true;
                }
            } catch (NumberFormatException e) {
                return false;
            }
        }
        return false;
    }
 
    /**
     * 查询当天基于 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) {
        LocalDateTime todayStart = LocalDateTime.of(
                LocalDateTime.now().toLocalDate(),
                LocalTime.MIN
        );
        LocalDateTime todayEnd = LocalDateTime.of(
                LocalDateTime.now().toLocalDate(),
                LocalTime.MAX
        );
 
        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);
        return preFix + LocalDate.now().format(DateTimeFormatter.ISO_LOCAL_DATE).replaceAll("-", "") + String.format("%03d", (aLong + 1));
    }
}