模版下载接口
This commit is contained in:
parent
f95d8040ed
commit
386b367b42
|
|
@ -170,7 +170,7 @@ public class EpcServiceImpl implements EpcService {
|
||||||
//1.2公司核心人员文件
|
//1.2公司核心人员文件
|
||||||
data.getComCoreList().forEach(item -> {
|
data.getComCoreList().forEach(item -> {
|
||||||
//获取附件信息
|
//获取附件信息
|
||||||
List<TbFileSourceVo> comCoreList = stateGridMapper.getFileSourceList(item.getId(),TableType.TB_KEY_PEOPLE.getCode());
|
List<TbFileSourceVo> comCoreList = stateGridMapper.getFileSourceList(Long.parseLong(item.getUserId()),TableType.TB_KEY_PEOPLE.getCode());
|
||||||
personFileGroup(item, comCoreList);
|
personFileGroup(item, comCoreList);
|
||||||
//获取人员业绩
|
//获取人员业绩
|
||||||
TbCompanyPerfVo tbCompanyPerfVo = new TbCompanyPerfVo();
|
TbCompanyPerfVo tbCompanyPerfVo = new TbCompanyPerfVo();
|
||||||
|
|
@ -186,14 +186,14 @@ public class EpcServiceImpl implements EpcService {
|
||||||
//1.3公司其他人员文件
|
//1.3公司其他人员文件
|
||||||
data.getComOtherList().forEach(item -> {
|
data.getComOtherList().forEach(item -> {
|
||||||
//获取附件信息
|
//获取附件信息
|
||||||
List<TbFileSourceVo> comOtherList = stateGridMapper.getFileSourceList(item.getId(),TableType.TB_OTHER_PEOPLE.getCode());
|
List<TbFileSourceVo> comOtherList = stateGridMapper.getFileSourceList(item.getUserId(),TableType.TB_OTHER_PEOPLE.getCode());
|
||||||
personFileGroup(item, comOtherList);
|
personFileGroup(item, comOtherList);
|
||||||
});
|
});
|
||||||
|
|
||||||
//2.2拟派人员材料
|
//2.2拟派人员材料
|
||||||
data.getSubPersonList().forEach(item -> {
|
data.getSubPersonList().forEach(item -> {
|
||||||
//获取附件信息
|
//获取附件信息
|
||||||
List<TbFileSourceVo> comOtherList = stateGridMapper.getFileSourceList(item.getId(),TableType.TB_SUB_PEOPLE.getCode());
|
List<TbFileSourceVo> comOtherList = stateGridMapper.getFileSourceList(item.getUserId(),TableType.TB_SUB_PEOPLE.getCode());
|
||||||
personFileGroup(item, comOtherList);
|
personFileGroup(item, comOtherList);
|
||||||
});
|
});
|
||||||
//将数据转换为模版数据
|
//将数据转换为模版数据
|
||||||
|
|
@ -299,7 +299,7 @@ public class EpcServiceImpl implements EpcService {
|
||||||
List<Map<String, Object>> infoList = new ArrayList<>();
|
List<Map<String, Object>> infoList = new ArrayList<>();
|
||||||
if (item.getPeoplePerfList() != null && !item.getPeoplePerfList().isEmpty()) {
|
if (item.getPeoplePerfList() != null && !item.getPeoplePerfList().isEmpty()) {
|
||||||
for (int j = 0; j < item.getPeoplePerfList().size(); j++) {
|
for (int j = 0; j < item.getPeoplePerfList().size(); j++) {
|
||||||
TbCompanyPerfVo tbCompanyPerfVo = item.getPeoplePerfList().get(i);
|
TbCompanyPerfVo tbCompanyPerfVo = item.getPeoplePerfList().get(j);
|
||||||
Map<String, Object> expMap = new HashMap<>();
|
Map<String, Object> expMap = new HashMap<>();
|
||||||
expMap.put("time", tbCompanyPerfVo.getStartTime()+"至"+tbCompanyPerfVo.getEndTime());
|
expMap.put("time", tbCompanyPerfVo.getStartTime()+"至"+tbCompanyPerfVo.getEndTime());
|
||||||
expMap.put("xm", tbCompanyPerfVo.getProName());
|
expMap.put("xm", tbCompanyPerfVo.getProName());
|
||||||
|
|
|
||||||
|
|
@ -173,7 +173,7 @@ public class SouthServiceImpl implements SouthService {
|
||||||
//1.2公司核心人员文件
|
//1.2公司核心人员文件
|
||||||
data.getComCoreList().forEach(item -> {
|
data.getComCoreList().forEach(item -> {
|
||||||
//获取附件信息
|
//获取附件信息
|
||||||
List<TbFileSourceVo> comCoreList = stateGridMapper.getFileSourceList(item.getId(), TableType.TB_KEY_PEOPLE.getCode());
|
List<TbFileSourceVo> comCoreList = stateGridMapper.getFileSourceList(Long.parseLong(item.getUserId()), TableType.TB_KEY_PEOPLE.getCode());
|
||||||
personFileGroup(item, comCoreList);
|
personFileGroup(item, comCoreList);
|
||||||
//获取人员业绩
|
//获取人员业绩
|
||||||
TbCompanyPerfVo tbCompanyPerfVo = new TbCompanyPerfVo();
|
TbCompanyPerfVo tbCompanyPerfVo = new TbCompanyPerfVo();
|
||||||
|
|
@ -189,14 +189,14 @@ public class SouthServiceImpl implements SouthService {
|
||||||
//1.3公司其他人员文件
|
//1.3公司其他人员文件
|
||||||
data.getComOtherList().forEach(item -> {
|
data.getComOtherList().forEach(item -> {
|
||||||
//获取附件信息
|
//获取附件信息
|
||||||
List<TbFileSourceVo> comOtherList = stateGridMapper.getFileSourceList(item.getId(),TableType.TB_OTHER_PEOPLE.getCode());
|
List<TbFileSourceVo> comOtherList = stateGridMapper.getFileSourceList(item.getUserId(),TableType.TB_OTHER_PEOPLE.getCode());
|
||||||
personFileGroup(item, comOtherList);
|
personFileGroup(item, comOtherList);
|
||||||
});
|
});
|
||||||
|
|
||||||
//2.2拟派人员材料
|
//2.2拟派人员材料
|
||||||
data.getSubPersonList().forEach(item -> {
|
data.getSubPersonList().forEach(item -> {
|
||||||
//获取附件信息
|
//获取附件信息
|
||||||
List<TbFileSourceVo> comOtherList = stateGridMapper.getFileSourceList(item.getId(),TableType.TB_SUB_PEOPLE.getCode());
|
List<TbFileSourceVo> comOtherList = stateGridMapper.getFileSourceList(item.getUserId(),TableType.TB_SUB_PEOPLE.getCode());
|
||||||
personFileGroup(item, comOtherList);
|
personFileGroup(item, comOtherList);
|
||||||
});
|
});
|
||||||
epcServiceImpl.dealWithWordData(request, response, data, "NW_DOC.ftl");
|
epcServiceImpl.dealWithWordData(request, response, data, "NW_DOC.ftl");
|
||||||
|
|
|
||||||
|
|
@ -9,6 +9,7 @@ import com.bonus.tool.mapper.StateGridMapper;
|
||||||
import com.bonus.tool.mapper.TbCompanyPerfMapper;
|
import com.bonus.tool.mapper.TbCompanyPerfMapper;
|
||||||
import com.bonus.tool.service.StateGridService;
|
import com.bonus.tool.service.StateGridService;
|
||||||
import com.bonus.tool.template.util.FreeMarkerUtil;
|
import com.bonus.tool.template.util.FreeMarkerUtil;
|
||||||
|
import com.bonus.tool.template.util.SnowflakeIdWorker;
|
||||||
import com.bonus.tool.template.util.WordUtils;
|
import com.bonus.tool.template.util.WordUtils;
|
||||||
import lombok.extern.slf4j.Slf4j;
|
import lombok.extern.slf4j.Slf4j;
|
||||||
import org.springframework.beans.factory.SmartInitializingSingleton;
|
import org.springframework.beans.factory.SmartInitializingSingleton;
|
||||||
|
|
@ -35,6 +36,8 @@ public class StateGridServiceImpl implements StateGridService {
|
||||||
@Resource
|
@Resource
|
||||||
private TbCompanyPerfMapper tbCompanyPerfMapper;
|
private TbCompanyPerfMapper tbCompanyPerfMapper;
|
||||||
|
|
||||||
|
SnowflakeIdWorker idWorker = new SnowflakeIdWorker(1, 1);
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* 国网模版列表查询
|
* 国网模版列表查询
|
||||||
*
|
*
|
||||||
|
|
@ -216,7 +219,7 @@ public class StateGridServiceImpl implements StateGridService {
|
||||||
//1.2公司核心人员文件
|
//1.2公司核心人员文件
|
||||||
data.getComCoreList().forEach(item -> {
|
data.getComCoreList().forEach(item -> {
|
||||||
//获取附件信息
|
//获取附件信息
|
||||||
List<TbFileSourceVo> comCoreList = getFileSourceList(item.getId(),TableType.TB_KEY_PEOPLE.getCode());
|
List<TbFileSourceVo> comCoreList = getFileSourceList(Long.parseLong(item.getUserId()),TableType.TB_KEY_PEOPLE.getCode());
|
||||||
personFileGroup(item, comCoreList);
|
personFileGroup(item, comCoreList);
|
||||||
//获取人员业绩
|
//获取人员业绩
|
||||||
TbCompanyPerfVo tbCompanyPerfVo = new TbCompanyPerfVo();
|
TbCompanyPerfVo tbCompanyPerfVo = new TbCompanyPerfVo();
|
||||||
|
|
@ -234,7 +237,7 @@ public class StateGridServiceImpl implements StateGridService {
|
||||||
//1.3公司其他人员文件
|
//1.3公司其他人员文件
|
||||||
data.getComOtherList().forEach(item -> {
|
data.getComOtherList().forEach(item -> {
|
||||||
//获取附件信息
|
//获取附件信息
|
||||||
List<TbFileSourceVo> comOtherList = getFileSourceList(item.getId(),TableType.TB_OTHER_PEOPLE.getCode());
|
List<TbFileSourceVo> comOtherList = getFileSourceList(item.getUserId(),TableType.TB_OTHER_PEOPLE.getCode());
|
||||||
personFileGroup(item, comOtherList);
|
personFileGroup(item, comOtherList);
|
||||||
});
|
});
|
||||||
|
|
||||||
|
|
@ -253,7 +256,7 @@ public class StateGridServiceImpl implements StateGridService {
|
||||||
//2.2拟派人员材料
|
//2.2拟派人员材料
|
||||||
item.getSubPersonList().forEach(personItem -> {
|
item.getSubPersonList().forEach(personItem -> {
|
||||||
//获取附件信息
|
//获取附件信息
|
||||||
List<TbFileSourceVo> comOtherList = getFileSourceList(personItem.getId(),TableType.TB_SUB_PEOPLE.getCode());
|
List<TbFileSourceVo> comOtherList = getFileSourceList(personItem.getUserId(),TableType.TB_SUB_PEOPLE.getCode());
|
||||||
personFileGroup(personItem, comOtherList);
|
personFileGroup(personItem, comOtherList);
|
||||||
});
|
});
|
||||||
|
|
||||||
|
|
@ -338,10 +341,10 @@ public class StateGridServiceImpl implements StateGridService {
|
||||||
List<Map<String, Object>> zmImgList = new ArrayList<>();
|
List<Map<String, Object>> zmImgList = new ArrayList<>();
|
||||||
// 处理分包商业绩
|
// 处理分包商业绩
|
||||||
if (item.getSubPerfList() != null && !item.getSubPerfList().isEmpty()) {
|
if (item.getSubPerfList() != null && !item.getSubPerfList().isEmpty()) {
|
||||||
int perfIndex = 1;
|
int perfIndex = 0;
|
||||||
for (SubPerformanceBean perfItem : item.getSubPerfList()) {
|
for (SubPerformanceBean perfItem : item.getSubPerfList()) {
|
||||||
Map<String, Object> perfMap = new HashMap<>();
|
Map<String, Object> perfMap = new HashMap<>();
|
||||||
perfMap.put("i", perfIndex++);
|
perfMap.put("i", ++perfIndex);
|
||||||
perfMap.put("proName", perfItem.getProName() != null ? perfItem.getProName() : "");
|
perfMap.put("proName", perfItem.getProName() != null ? perfItem.getProName() : "");
|
||||||
perfMap.put("unit", perfItem.getConsUnit() != null ? perfItem.getConsUnit() : "");
|
perfMap.put("unit", perfItem.getConsUnit() != null ? perfItem.getConsUnit() : "");
|
||||||
perfMap.put("money", perfItem.getMoney() != null ? perfItem.getMoney().toString() : "");
|
perfMap.put("money", perfItem.getMoney() != null ? perfItem.getMoney().toString() : "");
|
||||||
|
|
@ -358,20 +361,20 @@ public class StateGridServiceImpl implements StateGridService {
|
||||||
yjList.add(perfMap);
|
yjList.add(perfMap);
|
||||||
// 处理业绩证明材料图片
|
// 处理业绩证明材料图片
|
||||||
if (perfItem.getSubPerfFileList() != null && !perfItem.getSubPerfFileList().isEmpty()) {
|
if (perfItem.getSubPerfFileList() != null && !perfItem.getSubPerfFileList().isEmpty()) {
|
||||||
Map<String, Object> zmMap = new HashMap<>();
|
Map<String, Object> map2 = new HashMap<>();
|
||||||
zmMap.put("proName", perfIndex + "、" + perfItem.getProName());
|
map2.put("proName", perfIndex + "、" + perfItem.getProName());
|
||||||
List<Map<String, Object>> imgList = new ArrayList<>();
|
List<Map<String, Object>> imgList = new ArrayList<>();
|
||||||
int imgIndex = 1;
|
|
||||||
for (TbFileSourceVo file : perfItem.getSubPerfFileList()) {
|
for (TbFileSourceVo file : perfItem.getSubPerfFileList()) {
|
||||||
Map<String, Object> imgMap = new HashMap<>();
|
Map<String, Object> map3 = new HashMap<>();
|
||||||
imgMap.put("index", 100000 * perfIndex + imgIndex++);
|
map3.put("index", idWorker.nextId());
|
||||||
imgMap.put("width", "481.15");
|
map3.put("width", "481.15");
|
||||||
imgMap.put("height", "634.5");
|
map3.put("height", "634.5");
|
||||||
imgMap.put("base64Url", file.getFilePath() != null ? FreeMarkerUtil.getImageBase(filePath+file.getFilePath()) : "");
|
System.err.println("处理业绩证明材料图片:"+filePath+file.getFilePath());
|
||||||
imgList.add(imgMap);
|
map3.put("base64Url", file.getFilePath() != null ? FreeMarkerUtil.getImageBase(filePath+file.getFilePath()) : "");
|
||||||
|
imgList.add(map3);
|
||||||
}
|
}
|
||||||
zmMap.put("imgList", imgList);
|
map2.put("imgList", imgList);
|
||||||
zmImgList.add(zmMap);
|
zmImgList.add(map2);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
@ -389,6 +392,7 @@ public class StateGridServiceImpl implements StateGridService {
|
||||||
}
|
}
|
||||||
// 处理分包商拟派人员
|
// 处理分包商拟派人员
|
||||||
if (item.getSubPersonList() != null && !item.getSubPersonList().isEmpty()) {
|
if (item.getSubPersonList() != null && !item.getSubPersonList().isEmpty()) {
|
||||||
|
//先处理人员
|
||||||
int personIndex = 1;
|
int personIndex = 1;
|
||||||
for (SubOtherPeopleBean personItem : item.getSubPersonList()) {
|
for (SubOtherPeopleBean personItem : item.getSubPersonList()) {
|
||||||
Map<String, Object> personMap = new HashMap<>();
|
Map<String, Object> personMap = new HashMap<>();
|
||||||
|
|
@ -443,6 +447,49 @@ public class StateGridServiceImpl implements StateGridService {
|
||||||
personImgList.add(personImgMap);
|
personImgList.add(personImgMap);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
//再循环图片
|
||||||
|
for (SubOtherPeopleBean personItem : item.getSubPersonList()) {
|
||||||
|
// 处理拟派人员证件图片
|
||||||
|
Map<String, Object> personImgMap = new HashMap<>();
|
||||||
|
personImgMap.put("userType", personItem.getPostName() != null ? personItem.getPostName() : "");
|
||||||
|
List<Map<String, Object>> imgList = new ArrayList<>();
|
||||||
|
Map<String, Object> imgMap = new HashMap<>();
|
||||||
|
// 处理身份证
|
||||||
|
if (personItem.getIdCardFileList() != null && !personItem.getIdCardFileList().isEmpty()) {
|
||||||
|
for (TbFileSourceVo file : personItem.getIdCardFileList()) {
|
||||||
|
imgMap.put("index", idWorker.nextId());
|
||||||
|
imgMap.put("width", "375.35");
|
||||||
|
imgMap.put("height", "207.85");
|
||||||
|
imgMap.put("base64Url", file.getFilePath() != null ? FreeMarkerUtil.getImageBase(filePath+file.getFilePath()) : "");
|
||||||
|
imgList.add(imgMap);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
// 处理资格证书
|
||||||
|
if (personItem.getDiplomaFileList() != null && !personItem.getDiplomaFileList().isEmpty()) {
|
||||||
|
for (TbFileSourceVo file : personItem.getDiplomaFileList()) {
|
||||||
|
imgMap.put("index", idWorker.nextId());
|
||||||
|
imgMap.put("width", "481.15");
|
||||||
|
imgMap.put("height", "634.5");
|
||||||
|
imgMap.put("base64Url", file.getFilePath() != null ? FreeMarkerUtil.getImageBase(filePath+file.getFilePath()) : "");
|
||||||
|
imgList.add(imgMap);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
// 处理其他资质
|
||||||
|
if (personItem.getOtherFileList() != null && !personItem.getOtherFileList().isEmpty()) {
|
||||||
|
for (TbFileSourceVo file : personItem.getOtherFileList()) {
|
||||||
|
imgMap.put("index", idWorker.nextId());
|
||||||
|
imgMap.put("width", "481.15");
|
||||||
|
imgMap.put("height", "634.5");
|
||||||
|
imgMap.put("base64Url", file.getFilePath() != null ? FreeMarkerUtil.getImageBase(filePath+file.getFilePath()) : "");
|
||||||
|
imgList.add(imgMap);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
if (!imgList.isEmpty()) {
|
||||||
|
personImgMap.put("imgList", imgList);
|
||||||
|
personImgList.add(personImgMap);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
subMap.put("yjList", yjList);
|
subMap.put("yjList", yjList);
|
||||||
subMap.put("zzList", zzList);
|
subMap.put("zzList", zzList);
|
||||||
|
|
@ -457,7 +504,6 @@ public class StateGridServiceImpl implements StateGridService {
|
||||||
List<Map<String, Object>> otherUserList = new ArrayList<>();
|
List<Map<String, Object>> otherUserList = new ArrayList<>();
|
||||||
List<Map<String, Object>> userSettingList = new ArrayList<>();
|
List<Map<String, Object>> userSettingList = new ArrayList<>();
|
||||||
List<Map<String, Object>> personImgList = new ArrayList<>();
|
List<Map<String, Object>> personImgList = new ArrayList<>();
|
||||||
|
|
||||||
if (tbData.getComOtherList() != null && !tbData.getComOtherList().isEmpty()) {
|
if (tbData.getComOtherList() != null && !tbData.getComOtherList().isEmpty()) {
|
||||||
int otherIndex = 1;
|
int otherIndex = 1;
|
||||||
for (ComOtherPersonBean item : tbData.getComOtherList()) {
|
for (ComOtherPersonBean item : tbData.getComOtherList()) {
|
||||||
|
|
@ -473,15 +519,15 @@ public class StateGridServiceImpl implements StateGridService {
|
||||||
otherMap.put("bh", "");
|
otherMap.put("bh", "");
|
||||||
// 处理证书列表
|
// 处理证书列表
|
||||||
List<Map<String, Object>> zsList = new ArrayList<>();
|
List<Map<String, Object>> zsList = new ArrayList<>();
|
||||||
// if (item.getDiploma() != null || item.getDiplomaNum() != null || item.getMajor() != null || item.getLevel() != null) {
|
if (item.getDiploma() != null || item.getDiplomaNum() != null || item.getMajor() != null || item.getLevel() != null) {
|
||||||
// Map<String, Object> zsMap = new HashMap<>();
|
Map<String, Object> zsMap = new HashMap<>();
|
||||||
// zsMap.put("zs", item.getDiploma() != null ? item.getDiploma() : "");
|
zsMap.put("zs", item.getDiploma() != null ? item.getDiploma() : "");
|
||||||
// zsMap.put("jb", item.getLevel() != null ? item.getLevel() : "");
|
zsMap.put("jb", item.getLevel() != null ? item.getLevel() : "");
|
||||||
// zsMap.put("zh", item.getDiplomaNum() != null ? item.getDiplomaNum() : "");
|
zsMap.put("zh", item.getDiplomaNum() != null ? item.getDiplomaNum() : "");
|
||||||
// zsMap.put("zy", item.getMajor() != null ? item.getMajor() : "");
|
zsMap.put("zy", item.getMajor() != null ? item.getMajor() : "");
|
||||||
// zsMap.put("bh", "");
|
zsMap.put("bh", "");
|
||||||
// zsList.add(zsMap);
|
zsList.add(zsMap);
|
||||||
// }
|
}
|
||||||
otherMap.put("zsList", zsList);
|
otherMap.put("zsList", zsList);
|
||||||
otherUserList.add(otherMap);
|
otherUserList.add(otherMap);
|
||||||
// 处理人员证件图片
|
// 处理人员证件图片
|
||||||
|
|
|
||||||
|
|
@ -0,0 +1,114 @@
|
||||||
|
package com.bonus.tool.template.util;
|
||||||
|
|
||||||
|
public class SnowflakeIdWorker {
|
||||||
|
|
||||||
|
// 开始时间截 (UTC 2025-01-01)
|
||||||
|
private final long twepoch = 1704268800000L;
|
||||||
|
|
||||||
|
// 机器id所占的位数
|
||||||
|
private final long workerIdBits = 5L;
|
||||||
|
|
||||||
|
// 数据标识id所占的位数
|
||||||
|
private final long datacenterIdBits = 5L;
|
||||||
|
|
||||||
|
// 支持的最大机器id,结果是31 (这个值不包含id为0的情况)
|
||||||
|
private final long maxWorkerId = -1L ^ (-1L << workerIdBits);
|
||||||
|
|
||||||
|
// 支持的最大数据标识id,结果是31
|
||||||
|
private final long maxDatacenterId = -1L ^ (-1L << datacenterIdBits);
|
||||||
|
|
||||||
|
// 序列在id中占的位数
|
||||||
|
private final long sequenceBits = 12L;
|
||||||
|
|
||||||
|
// 机器ID向左移12位
|
||||||
|
private final long workerIdShift = sequenceBits;
|
||||||
|
|
||||||
|
// 数据标识id向左移17位(12+5)
|
||||||
|
private final long datacenterIdShift = sequenceBits + workerIdBits;
|
||||||
|
|
||||||
|
// 时间截向左移22位(12+5+5)
|
||||||
|
private final long timestampLeftShift = sequenceBits + workerIdBits + datacenterIdBits;
|
||||||
|
|
||||||
|
// 生成序列的掩码,这里为4095 (0b111111111111=4095)
|
||||||
|
private final long sequenceMask = -1L ^ (-1L << sequenceBits);
|
||||||
|
|
||||||
|
// 工作机器ID(0~31)
|
||||||
|
private long workerId;
|
||||||
|
|
||||||
|
// 数据中心ID(0~31)
|
||||||
|
private long datacenterId;
|
||||||
|
|
||||||
|
// 毫秒内序列(0~4095)
|
||||||
|
private long sequence = 0L;
|
||||||
|
|
||||||
|
// 上次生成ID的时间截
|
||||||
|
private long lastTimestamp = -1L;
|
||||||
|
|
||||||
|
// 构造函数
|
||||||
|
public SnowflakeIdWorker(long workerId, long datacenterId) {
|
||||||
|
if (workerId > maxWorkerId || workerId < 0) {
|
||||||
|
throw new IllegalArgumentException(String.format("worker Id can't be greater than %d or less than 0", maxWorkerId));
|
||||||
|
}
|
||||||
|
if (datacenterId > maxDatacenterId || datacenterId < 0) {
|
||||||
|
throw new IllegalArgumentException(String.format("datacenter Id can't be greater than %d or less than 0", maxDatacenterId));
|
||||||
|
}
|
||||||
|
this.workerId = workerId;
|
||||||
|
this.datacenterId = datacenterId;
|
||||||
|
}
|
||||||
|
|
||||||
|
// 获得下一个ID (该方法是线程安全的)
|
||||||
|
public synchronized long nextId() {
|
||||||
|
long timestamp = timeGen();
|
||||||
|
|
||||||
|
// 如果当前时间小于上一次ID生成的时间戳,说明系统时钟回退过这个时候应当抛出异常
|
||||||
|
if (timestamp < lastTimestamp) {
|
||||||
|
throw new RuntimeException(String.format("Clock moved backwards. Refusing to generate id for %d milliseconds", lastTimestamp - timestamp));
|
||||||
|
}
|
||||||
|
|
||||||
|
// 如果是同一时间生成的,则进行毫秒内序列
|
||||||
|
if (lastTimestamp == timestamp) {
|
||||||
|
sequence = (sequence + 1) & sequenceMask;
|
||||||
|
// 毫秒内序列溢出
|
||||||
|
if (sequence == 0) {
|
||||||
|
// 阻塞到下一个毫秒,获得新的时间戳
|
||||||
|
timestamp = tilNextMillis(lastTimestamp);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
// 时间戳改变,毫秒内序列重置
|
||||||
|
else {
|
||||||
|
sequence = 0L;
|
||||||
|
}
|
||||||
|
|
||||||
|
// 上次生成ID的时间截
|
||||||
|
lastTimestamp = timestamp;
|
||||||
|
|
||||||
|
// 移位并通过或运算拼到一起组成64位的ID
|
||||||
|
return ((timestamp - twepoch) << timestampLeftShift)
|
||||||
|
| (datacenterId << datacenterIdShift)
|
||||||
|
| (workerId << workerIdShift)
|
||||||
|
| sequence;
|
||||||
|
}
|
||||||
|
|
||||||
|
// 阻塞到下一个毫秒,直到获得新的时间戳
|
||||||
|
protected long tilNextMillis(long lastTimestamp) {
|
||||||
|
long timestamp = timeGen();
|
||||||
|
while (timestamp <= lastTimestamp) {
|
||||||
|
timestamp = timeGen();
|
||||||
|
}
|
||||||
|
return timestamp;
|
||||||
|
}
|
||||||
|
|
||||||
|
// 返回以毫秒为单位的当前时间
|
||||||
|
protected long timeGen() {
|
||||||
|
return System.currentTimeMillis();
|
||||||
|
}
|
||||||
|
|
||||||
|
public static void main(String[] args) {
|
||||||
|
SnowflakeIdWorker idWorker = new SnowflakeIdWorker(1, 1);
|
||||||
|
for (int i = 0; i < 10; i++) {
|
||||||
|
long id = idWorker.nextId();
|
||||||
|
System.out.println(Long.toBinaryString(id));
|
||||||
|
System.out.println(id);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
Loading…
Reference in New Issue