Commit 0d4f439a by yangll

优化定位唯一键生成的雪花算法

git-svn-id: svn://192.168.3.233/frms/code/car-service/trunk/car-service@32477 aeb6102e-e053-11e5-9fec-f9ebc45caf4b
1 parent eb4513bf
...@@ -30,8 +30,8 @@ import com.glsx.ordercenter.strategy.DeviceVerifyStrategy; ...@@ -30,8 +30,8 @@ import com.glsx.ordercenter.strategy.DeviceVerifyStrategy;
import com.glsx.ordercenter.strategy.OrderSyncStrategy; import com.glsx.ordercenter.strategy.OrderSyncStrategy;
import com.glsx.ordercenter.thread.OrderStatusNotifyTask; import com.glsx.ordercenter.thread.OrderStatusNotifyTask;
import com.glsx.ordercenter.utils.OrderUtil; import com.glsx.ordercenter.utils.OrderUtil;
import com.glsx.ordercenter.utils.SnowflakeWorker;
import com.glsx.ordercenter.utils.SpringContextUtils; import com.glsx.ordercenter.utils.SpringContextUtils;
import com.glsx.plat.common.utils.SnowFlake;
import com.google.common.collect.Lists; import com.google.common.collect.Lists;
import org.apache.commons.collections.CollectionUtils; import org.apache.commons.collections.CollectionUtils;
import org.slf4j.Logger; import org.slf4j.Logger;
...@@ -183,9 +183,6 @@ public class OrderServiceImpl implements IOrderService { ...@@ -183,9 +183,6 @@ public class OrderServiceImpl implements IOrderService {
@Autowired @Autowired
private SysSubOrderMapper subOrderMapper; private SysSubOrderMapper subOrderMapper;
@Autowired
private SnowflakeWorker snowflakeWorker;
@Override @Override
public ResultObject deleteByPrimaryKey(Integer id) { public ResultObject deleteByPrimaryKey(Integer id) {
SysOrder record = orderMapper.selectByPrimaryKey(id); SysOrder record = orderMapper.selectByPrimaryKey(id);
...@@ -2206,7 +2203,7 @@ public class OrderServiceImpl implements IOrderService { ...@@ -2206,7 +2203,7 @@ public class OrderServiceImpl implements IOrderService {
public String getOrderSn(int orderType) { public String getOrderSn(int orderType) {
StringBuffer buffer = new StringBuffer(); StringBuffer buffer = new StringBuffer();
buffer.append(OrderTypeEnum.getOrderNoPrefix(orderType)); buffer.append(OrderTypeEnum.getOrderNoPrefix(orderType));
String snowId = String.valueOf(snowflakeWorker.nextId()); String snowId = String.valueOf(SnowFlake.nextId());
int len = snowId.length(); int len = snowId.length();
if (len == 16) { //长度16直接返回 if (len == 16) { //长度16直接返回
return buffer.append(snowId).toString(); return buffer.append(snowId).toString();
......
package com.glsx.ordercenter;
import com.glsx.ordercenter.constants.OrderTypeEnum;
import com.glsx.plat.common.utils.SnowFlake;
/**
* @author Yanglinlong
* @date 2022/8/11 10:42
*/
public class SnowFlakeTest {
public static void main(String[] args) {
StringBuffer buffer = new StringBuffer();
buffer.append(OrderTypeEnum.getOrderNoPrefix(1));
String snowId = String.valueOf(SnowFlake.nextId());
System.out.println("snowId = " + snowId);
int len = snowId.length();
if (len == 16) { //长度16直接返回
System.out.println("=== 1 ===");
System.out.println(buffer.append(snowId).toString());
}
if (len > 16) { //长度大于16,截取
buffer.append(snowId, len - 16, len);
System.out.println("=== 2 ===");
System.out.println(buffer.toString());
}
//长度不足的,前面补0
int count = 16 - len;
for (int i = 0; i < count; i++) {
buffer.append("0");
}
System.out.println("=== 3 ===");
System.out.println(buffer.append(snowId).toString());
}
}
Markdown is supported
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!