代码备份
parent
93cf1636f3
commit
ace1830bbb
@ -0,0 +1,97 @@
|
|||||||
|
package com.glxp.api.util;
|
||||||
|
|
||||||
|
|
||||||
|
import cn.hutool.core.collection.CollUtil;
|
||||||
|
import cn.hutool.core.util.StrUtil;
|
||||||
|
import com.glxp.api.entity.basic.UdiEntity;
|
||||||
|
import com.glxp.api.res.inout.VailCodeResultResponse;
|
||||||
|
import com.glxp.api.util.udi.FilterUdiUtils;
|
||||||
|
|
||||||
|
import java.util.*;
|
||||||
|
import java.util.stream.Collectors;
|
||||||
|
|
||||||
|
public class VailBatchCodeUtils {
|
||||||
|
|
||||||
|
public static List<VailCodeResultResponse> batchVailCode(String codes) {
|
||||||
|
List<String> codeList = List.of(codes.split(";"));
|
||||||
|
if (CollUtil.isEmpty(codeList)) return null;
|
||||||
|
codeList = codeList.stream().distinct().collect(Collectors.toList());
|
||||||
|
List<VailCodeResultResponse> vailCodeResultResponses = new ArrayList<>();
|
||||||
|
List<String> prefixes = Arrays.asList("01", "11", "17", "10", "21");
|
||||||
|
if (hasMoreThanTwoPrefixes(codeList, prefixes.toArray(new String[0]))) {
|
||||||
|
//判断是否多段扫码
|
||||||
|
String mutiCode = filterAndConcatenate(codeList, prefixes);
|
||||||
|
UdiEntity temp = FilterUdiUtils.getUdi(mutiCode);
|
||||||
|
if (temp != null) {
|
||||||
|
codeList.clear();
|
||||||
|
codeList.add(mutiCode);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
for (String code : codeList) {
|
||||||
|
VailCodeResultResponse vailCodeResultResponse = new VailCodeResultResponse();
|
||||||
|
UdiEntity udiEntity = FilterUdiUtils.getUdi(code);
|
||||||
|
vailCodeResultResponse.setCode(code);
|
||||||
|
if (udiEntity != null) {
|
||||||
|
vailCodeResultResponse.setStatus(1);
|
||||||
|
vailCodeResultResponse.setErrMsg("解析正确");
|
||||||
|
} else {
|
||||||
|
vailCodeResultResponse.setStatus(2);
|
||||||
|
vailCodeResultResponse.setErrMsg("追溯码格式错误");
|
||||||
|
}
|
||||||
|
vailCodeResultResponses.add(vailCodeResultResponse);
|
||||||
|
}
|
||||||
|
return vailCodeResultResponses;
|
||||||
|
}
|
||||||
|
|
||||||
|
public static boolean hasMoreThanTwoPrefixes(List<String> codes, String[] prefixes) {
|
||||||
|
int uniquePrefixCount = 0;
|
||||||
|
boolean[] prefixFound = new boolean[prefixes.length];
|
||||||
|
for (String code : codes) {
|
||||||
|
for (int i = 0; i < prefixes.length; i++) {
|
||||||
|
if (code.startsWith(prefixes[i]) && !prefixFound[i]) {
|
||||||
|
prefixFound[i] = true;
|
||||||
|
uniquePrefixCount++;
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return uniquePrefixCount > 1;
|
||||||
|
}
|
||||||
|
|
||||||
|
public static void main(String[] args) {
|
||||||
|
String code = "MA.156.M0.100442.0WET6505.S1394.M230624.L230601.DS22306241.E250624.V250623.C5";
|
||||||
|
// String code = "0116922943800657112406011724063010JY999\u001D210003;0118822943800657112406011724063010JY999\u001D210003";
|
||||||
|
// String code = "0116922943800657;1124060117240630;10JY999\u001D210003;www.baidu.com;32329382;0118822943800657112406011724063010JY999\u001D210003";
|
||||||
|
|
||||||
|
List<VailCodeResultResponse> vailCodeResultResponses = batchVailCode(code);
|
||||||
|
for (VailCodeResultResponse vailCodeResultResponse : vailCodeResultResponses) {
|
||||||
|
System.out.println(vailCodeResultResponse.getCode() + ":" + vailCodeResultResponse.getStatus() + ":" + vailCodeResultResponse.getErrMsg());
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
public static String filterAndConcatenate(List<String> codeList, List<String> prefixes) {
|
||||||
|
Set<String> matchedPrefixes = new HashSet<>();
|
||||||
|
StringBuilder concatenatedResult = new StringBuilder();
|
||||||
|
for (String prefix : prefixes) {
|
||||||
|
for (String code : codeList) {
|
||||||
|
if (code.length() >= 2 && code.startsWith(prefix)) {
|
||||||
|
if (!matchedPrefixes.contains(prefix)) {
|
||||||
|
matchedPrefixes.add(prefix);
|
||||||
|
concatenatedResult.append(code);
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
UdiEntity udiEntity = FilterUdiUtils.getUdi(concatenatedResult.toString());
|
||||||
|
if (udiEntity != null && StrUtil.isNotEmpty(udiEntity.getBatchNo())) {
|
||||||
|
if (concatenatedResult.toString().endsWith("10" + udiEntity.getBatchNo())) {
|
||||||
|
concatenatedResult.append("\u001D");
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return concatenatedResult.toString();
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
Loading…
Reference in New Issue