This commit is contained in:
parent
2e46b796cb
commit
0e4a1e2b73
@ -19,8 +19,10 @@ import org.springframework.validation.annotation.Validated;
|
|||||||
import org.springframework.web.bind.annotation.*;
|
import org.springframework.web.bind.annotation.*;
|
||||||
import tech.riemann.ims.entity.material.Material;
|
import tech.riemann.ims.entity.material.Material;
|
||||||
import tech.riemann.ims.service.material.IMaterialService;
|
import tech.riemann.ims.service.material.IMaterialService;
|
||||||
|
import tech.riemann.ims.utils.BarcodeUtil;
|
||||||
|
|
||||||
import javax.annotation.Nonnull;
|
import javax.annotation.Nonnull;
|
||||||
|
import java.util.ArrayList;
|
||||||
import java.util.List;
|
import java.util.List;
|
||||||
import java.util.concurrent.ExecutionException;
|
import java.util.concurrent.ExecutionException;
|
||||||
import java.util.concurrent.TimeUnit;
|
import java.util.concurrent.TimeUnit;
|
||||||
@ -127,4 +129,21 @@ public class MaterialController {
|
|||||||
throw BizException.create("删除物料失败!");
|
throw BizException.create("删除物料失败!");
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// 批量生成条形码
|
||||||
|
@PostMapping("material/{id}/generate-barcodes")
|
||||||
|
@Operation(summary = "批量生成条形码")
|
||||||
|
public List<String> generateBarcodes(@Parameter(description = "物料id", required = true) @PathVariable Long id,
|
||||||
|
@Parameter(description = "条形码数量", required = true) @RequestParam(name = "count") int count) {
|
||||||
|
List<String> res = new ArrayList<>();
|
||||||
|
Material byId = materialService.getById(id);
|
||||||
|
if (byId != null && count > 0) {
|
||||||
|
String code = byId.getCode();
|
||||||
|
while (count-- > 0) {
|
||||||
|
res.add(code + BarcodeUtil.generateBarcode());
|
||||||
|
}
|
||||||
|
}
|
||||||
|
log.info("生成条形码成功,条形码列表:{}", res);
|
||||||
|
return res;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
60
src/main/java/tech/riemann/ims/utils/BarcodeUtil.java
Normal file
60
src/main/java/tech/riemann/ims/utils/BarcodeUtil.java
Normal file
@ -0,0 +1,60 @@
|
|||||||
|
package tech.riemann.ims.utils;
|
||||||
|
|
||||||
|
import lombok.experimental.UtilityClass;
|
||||||
|
|
||||||
|
import java.text.SimpleDateFormat;
|
||||||
|
import java.util.Collections;
|
||||||
|
import java.util.Date;
|
||||||
|
import java.util.HashSet;
|
||||||
|
import java.util.Set;
|
||||||
|
import java.util.concurrent.ConcurrentHashMap;
|
||||||
|
import java.util.concurrent.ThreadLocalRandom;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @author mayong
|
||||||
|
* @since 2024/12/12 20:39
|
||||||
|
*/
|
||||||
|
@UtilityClass
|
||||||
|
public class BarcodeUtil {
|
||||||
|
|
||||||
|
// 用于存储每天已经生成的条码的最后三位
|
||||||
|
private static final ConcurrentHashMap<String, Set<Integer>> dailySequences = new ConcurrentHashMap<>();
|
||||||
|
|
||||||
|
// 日期格式化为年份后两位和一年中的某一天
|
||||||
|
private static final SimpleDateFormat sdf = new SimpleDateFormat("yyDDD");
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 生成一个7位数字条码,前4位表示年份后两位和一年中的某一天,后3位随机且当天不重复
|
||||||
|
*/
|
||||||
|
public static synchronized String generateBarcode() {
|
||||||
|
// 获取当前日期部分(简化为年份后两位和一年中的第几天)
|
||||||
|
String datePart = sdf.format(new Date());
|
||||||
|
|
||||||
|
// 如果当天的序列号集合不存在,则初始化一个新的集合
|
||||||
|
dailySequences.putIfAbsent(datePart, Collections.synchronizedSet(new HashSet<>()));
|
||||||
|
|
||||||
|
// 获取当天的序列号集合
|
||||||
|
Set<Integer> sequencesToday = dailySequences.get(datePart);
|
||||||
|
|
||||||
|
// 生成随机且不重复的三位数
|
||||||
|
int sequence;
|
||||||
|
do {
|
||||||
|
sequence = ThreadLocalRandom.current().nextInt(0, 9999);
|
||||||
|
} while (sequencesToday.contains(sequence));
|
||||||
|
|
||||||
|
// 将新生成的序列号添加到当天的集合中
|
||||||
|
sequencesToday.add(sequence);
|
||||||
|
|
||||||
|
// 格式化序列号为3位字符串,不足前面补0
|
||||||
|
String sequencePart = String.format("%04d", sequence);
|
||||||
|
|
||||||
|
return datePart + sequencePart;
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 模拟新的一天开始,重置当天的序列号集合
|
||||||
|
*/
|
||||||
|
public static void resetDailySequence() {
|
||||||
|
dailySequences.clear();
|
||||||
|
}
|
||||||
|
}
|
Loading…
x
Reference in New Issue
Block a user