添加 机型 分销
This commit is contained in:
@@ -0,0 +1,153 @@
|
||||
package com.ruoyi.bas.BasProductCode.util;
|
||||
|
||||
import java.util.ArrayList;
|
||||
import java.util.List;
|
||||
|
||||
public class ProcodeUtils {
|
||||
// 定义密钥,与原始逻辑保持一致
|
||||
private static final String KEY = "0";
|
||||
|
||||
/**
|
||||
* 按照指定规则对输入字符串进行编码
|
||||
* @param text 输入字符串,格式为"订单号@型号@机型@分销@序列号"
|
||||
* @return 编码后的字符串
|
||||
*/
|
||||
public static String encode(String text) {
|
||||
// 按@分割字符串
|
||||
String[] parts = text.split("@");
|
||||
// 验证输入格式是否正确
|
||||
if (parts.length < 5) {
|
||||
throw new IllegalArgumentException("输入格式不正确,应为\"订单号@型号@机型@分销@序列号\"");
|
||||
}
|
||||
|
||||
// 构建中间字符串
|
||||
String text1 = "@" + parts[1] + "@" + parts[2] + "@" + parts[3] + "@";
|
||||
String str = KEY + text1;
|
||||
|
||||
// 处理每个字符为4位十六进制
|
||||
StringBuilder hexBuilder = new StringBuilder();
|
||||
for (int i = 0; i < str.length(); i++) {
|
||||
char c = str.charAt(i);
|
||||
// 获取Unicode编码并转换为4位十六进制
|
||||
hexBuilder.append(String.format("%04X", (int) c));
|
||||
}
|
||||
|
||||
// 反转字符串
|
||||
String reversedHex = new StringBuilder(hexBuilder.toString()).reverse().toString();
|
||||
|
||||
// 替换操作
|
||||
String replaced = reversedHex
|
||||
.replace("100", "Q")
|
||||
.replace("200", "R")
|
||||
.replace("300", "S")
|
||||
.replace("400", "T")
|
||||
.replace("500", "U")
|
||||
.replace("600", "V")
|
||||
.replace("700", "W")
|
||||
.replace("800", "X")
|
||||
.replace("900", "Y")
|
||||
.replace("00", "Z");
|
||||
|
||||
// 构建最终结果并反转
|
||||
String result = parts[0] + "$" + replaced + "$" + parts[4];
|
||||
return new StringBuilder(result).reverse().toString();
|
||||
}
|
||||
|
||||
/**
|
||||
* 解析编码后的字符串,还原原始信息
|
||||
* @param encodedStr 编码后的字符串
|
||||
* @return 原始信息数组,格式为[订单号, 型号, 机型, 分销, 序列号]
|
||||
*/
|
||||
public static String[] decode(String encodedStr) {
|
||||
// 步骤1:整体反转字符串
|
||||
String reversed = new StringBuilder(encodedStr).reverse().toString();
|
||||
|
||||
// 步骤2:按$分割为三部分
|
||||
String[] parts = reversed.split("\\$");
|
||||
if (parts.length != 3) {
|
||||
throw new IllegalArgumentException("编码字符串格式不正确");
|
||||
}
|
||||
|
||||
String orderNo = parts[0]; // 订单号
|
||||
String encodedPart = parts[1]; // 编码的中间部分
|
||||
String serialNo = parts[2]; // 序列号
|
||||
|
||||
// 步骤3:逆向替换操作,恢复十六进制片段
|
||||
String replaced = encodedPart
|
||||
.replace("Q", "100")
|
||||
.replace("R", "200")
|
||||
.replace("S", "300")
|
||||
.replace("T", "400")
|
||||
.replace("U", "500")
|
||||
.replace("V", "600")
|
||||
.replace("W", "700")
|
||||
.replace("X", "800")
|
||||
.replace("Y", "900")
|
||||
.replace("Z", "00");
|
||||
|
||||
// 步骤4:再次反转,恢复十六进制顺序
|
||||
String hexStr = new StringBuilder(replaced).reverse().toString();
|
||||
|
||||
// 步骤5:按4位分割,转换为字符
|
||||
List<Character> chars = new ArrayList<>();
|
||||
for (int i = 0; i < hexStr.length(); i += 4) {
|
||||
int endIndex = Math.min(i + 4, hexStr.length());
|
||||
String hexSegment = hexStr.substring(i, endIndex);
|
||||
|
||||
// 不足4位的补0
|
||||
if (hexSegment.length() < 4) {
|
||||
hexSegment = String.format("%-" + 4 + "s", hexSegment).replace(' ', '0');
|
||||
}
|
||||
|
||||
// 十六进制转字符
|
||||
try {
|
||||
int code = Integer.parseInt(hexSegment, 16);
|
||||
chars.add((char) code);
|
||||
} catch (NumberFormatException e) {
|
||||
throw new RuntimeException("解析十六进制时出错: " + hexSegment, e);
|
||||
}
|
||||
}
|
||||
|
||||
// 拼接字符得到原始中间字符串
|
||||
StringBuilder middleStr = new StringBuilder();
|
||||
for (char c : chars) {
|
||||
middleStr.append(c);
|
||||
}
|
||||
|
||||
// 步骤6:提取型号、机型、分销信息(去掉key和前后的@)
|
||||
if (middleStr.length() < 1) {
|
||||
throw new RuntimeException("解析中间信息失败,中间字符串为空");
|
||||
}
|
||||
String text1 = middleStr.substring(1); // 去掉key(第一个字符)
|
||||
String[] infoParts = text1.split("@");
|
||||
|
||||
if (infoParts.length < 4) {
|
||||
throw new RuntimeException("解析中间信息失败,格式不正确");
|
||||
}
|
||||
|
||||
String model = infoParts[1]; // 型号
|
||||
String machineType = infoParts[2]; // 机型
|
||||
String distribution = infoParts[3]; // 分销
|
||||
|
||||
return new String[]{orderNo, model, machineType, distribution, serialNo};
|
||||
}
|
||||
|
||||
// 测试方法
|
||||
public static void main(String[] args) {
|
||||
// 测试编码
|
||||
String original = "123456789@M-221F@12@13@25318S7000F03";
|
||||
System.out.println("原始字符串: " + original);
|
||||
|
||||
String encoded = encode(original);
|
||||
System.out.println("编码结果: " + encoded);
|
||||
|
||||
// 测试解码
|
||||
String[] decoded = decode("30F0007S81352$S0T0TDRDS2S2S1T6T0S1S2T0S1S3T0$654321");
|
||||
System.out.println("解码结果:");
|
||||
System.out.println("订单号: " + decoded[0]);
|
||||
System.out.println("型号: " + decoded[1]);
|
||||
System.out.println("机型: " + decoded[2]);
|
||||
System.out.println("分销: " + decoded[3]);
|
||||
System.out.println("序列号: " + decoded[4]);
|
||||
}
|
||||
}
|
||||
@@ -147,10 +147,13 @@ public class MydoJobDetail extends BaseEntity
|
||||
@Excel(name = "毛重")
|
||||
private String spare8;
|
||||
/** 备用字段4 */
|
||||
@Excel(name = "分销")
|
||||
private String spare9;
|
||||
/** 备用字段4 */
|
||||
@Excel(name = "规格")
|
||||
private String spec;
|
||||
@Excel(name = "机型")
|
||||
private String unit;
|
||||
|
||||
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user