| | |
| | | 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; |
| | |
| | | |
| | | |
| | | /** |
| | | * 查询当天(基于createTime字段)的记录数量 |
| | | * @param mapper 实体类对应的BaseMapper |
| | | * @param <T> 实体类泛型 |
| | | * @return 当天记录数量 |
| | | * 生成当天批次号:前缀 + 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) { |
| | | // 获取当天开始时间(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 |
| | | ); |
| | | public static <T> String countTodayByCreateTime(BaseMapper<T> mapper, String preFix, String batchField) { |
| | | String dateStr = LocalDate.now().format(DateTimeFormatter.ofPattern("yyyyMMdd")); |
| | | String prefix = preFix + dateStr; |
| | | |
| | | // 转换为Date类型(如果实体类中createTime是LocalDateTime可直接使用) |
| | | Date startDate = Date.from(todayStart.atZone(ZoneId.systemDefault()).toInstant()); |
| | | Date endDate = Date.from(todayEnd.atZone(ZoneId.systemDefault()).toInstant()); |
| | | LocalDateTime todayStart = LocalDateTime.of(LocalDate.now(), LocalTime.MIN); |
| | | LocalDateTime todayEnd = LocalDateTime.of(LocalDate.now(), LocalTime.MAX); |
| | | |
| | | // 构建查询条件 |
| | | QueryWrapper<T> queryWrapper = new QueryWrapper<>(); |
| | | queryWrapper.ge("create_time", startDate) // 大于等于当天开始 |
| | | .lt("create_time", endDate); // 小于当天结束(避免毫秒精度问题) |
| | | 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); |
| | | |
| | | // 执行查询 |
| | | 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(); |
| | | 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"); |
| | | } |
| | | |
| | | |