Browse Source

id生成器 24位

dev_shibei_match
jianjun 4 years ago
parent
commit
151ece9e6a
  1. 21
      epmet-commons/epmet-commons-tools/src/main/java/com/epmet/commons/tools/utils/UniqueIdGenerator.java
  2. 26
      epmet-module/data-statistical/data-statistical-server/src/main/java/com/epmet/model/IndexExcelDataListener.java
  3. 6
      epmet-module/data-statistical/data-statistical-server/src/main/java/com/epmet/service/evaluationindex/screen/impl/IndexGroupServiceImpl.java

21
epmet-commons/epmet-commons-tools/src/main/java/com/epmet/commons/tools/utils/UniqueIdGenerator.java

@ -11,6 +11,8 @@ import java.util.concurrent.atomic.AtomicLong;
* 唯一ID生成器
*/
public class UniqueIdGenerator {
public final static String FORMAT_DMDHS = "yyyyMMddHHmmss";
private static UniqueValue uniqueValue = new UniqueValue();
private static String middle;
@ -25,7 +27,7 @@ public class UniqueIdGenerator {
private static class UniqueValue {
private AtomicLong uniqueValue = new AtomicLong(0L);
private volatile String currentTime = DateUtils.format(new Date(), DateUtils.DATE_TIME_NO_SPLIT);
private volatile String currentTime = DateUtils.format(new Date(), FORMAT_DMDHS);
private volatile String lastTime = currentTime;
/**
@ -35,7 +37,7 @@ public class UniqueIdGenerator {
* @return
*/
public String getCurrentTime() {
currentTime = DateUtils.format(new Date(), DateUtils.DATE_TIME_NO_SPLIT);
currentTime = DateUtils.format(new Date(), FORMAT_DMDHS);
if (!currentTime.equals(lastTime)) {
lastTime = currentTime;
Random random = new Random();
@ -51,31 +53,32 @@ public class UniqueIdGenerator {
/**
* 生成一个24位唯一ID
* 15位时间+2位中间值防止多服务冲突+2个线程code+5位秒级递增值
* 14位时间+2位中间值防止多服务冲突+3个线程code+5位秒级递增值
*
* @return
*/
public static String generate() {
public static String generate24() {
StringBuilder builder = new StringBuilder(32);
builder.append(uniqueValue.getCurrentTime())
.append(middle)
.append(getUniqueThreadCode())
.append(getUniqueThreadCode(3))
.append(uniqueValue.getCurrentValue());
return builder.toString();
}
public static String getUniqueThreadCode() {
String threadCode = StringUtils.left(String.valueOf(Thread.currentThread().hashCode()), 2);
return StringUtils.leftPad(threadCode, 2, "0");
public static String getUniqueThreadCode(Integer length) {
String threadCode = StringUtils.left(String.valueOf(Thread.currentThread().hashCode()), length);
return StringUtils.leftPad(threadCode, length, "0");
}
public static void main(String[] args) throws InterruptedException {
System.out.println(UniqueIdGenerator.uniqueValue.currentTime);
System.out.println(UniqueIdGenerator.middle);
System.out.println(UniqueIdGenerator.getUniqueThreadCode());
System.out.println(UniqueIdGenerator.getUniqueThreadCode(2));
System.out.println(uniqueValue.getCurrentValue());
System.out.println(UniqueIdGenerator.generate24());
}
}

26
epmet-module/data-statistical/data-statistical-server/src/main/java/com/epmet/model/IndexExcelDataListener.java

@ -157,7 +157,7 @@ public class IndexExcelDataListener extends AnalysisEventListener<IndexModel> {
group1 = new IndexGroupTemplateEntity();
group1.setIndexId(indexDictEntity.getId());
group1.setParentIndexGroupId("0");
group1.setId(UniqueIdGenerator.generate());
group1.setId(UniqueIdGenerator.generate24());
group1.setIndexCode(Pinyin4jUtil.getSpellPinYin(indexDictEntity.getIndexName(), true, 4));
group1.setAllIndexCodePath(index.getAllIndexCodePath(0));
indexGroupMap.put(index.getLevel1Index(), group1);
@ -173,7 +173,7 @@ public class IndexExcelDataListener extends AnalysisEventListener<IndexModel> {
group2 = new IndexGroupTemplateEntity();
group2.setIndexId(indexDictEntity.getId());
group2.setParentIndexGroupId(group1.getId());
group2.setId(UniqueIdGenerator.generate());
group2.setId(UniqueIdGenerator.generate24());
group2.setIndexCode(Pinyin4jUtil.getSpellPinYin(indexDictEntity.getIndexName(), false, 4));
group2.setAllIndexCodePath(index.getAllIndexCodePath(3));
indexGroupMap.put(level4IndexDetailKey, group2);
@ -184,7 +184,7 @@ public class IndexExcelDataListener extends AnalysisEventListener<IndexModel> {
templateEntity.setIndexGroupId(group1.getId());
templateEntity.setIndexId(indexDictEntity.getId());
templateEntity.setAllParentIndexCode(allIndexCodeSb.toString());
templateEntity.setId(UniqueIdGenerator.generate());
templateEntity.setId(UniqueIdGenerator.generate24());
templateEntity.setCorrelation(index.getCorrelation());
templateEntity.setAllIndexCodePath(index.getAllIndexCodePath(3));
buildIndexGroupDetail(templateEntity, indexDictEntity, index, 2);
@ -202,7 +202,7 @@ public class IndexExcelDataListener extends AnalysisEventListener<IndexModel> {
templateEntity.setIndexGroupId(group2.getId());
templateEntity.setIndexId(indexDictEntity.getId());
templateEntity.setAllParentIndexCode(allIndexCodeSb.toString());
templateEntity.setId(UniqueIdGenerator.generate());
templateEntity.setId(UniqueIdGenerator.generate24());
templateEntity.setCorrelation(index.getCorrelation());
templateEntity.setAllIndexCodePath(index.getAllIndexCodePath(4));
buildIndexGroupDetail(templateEntity, indexDictEntity, index, 5);
@ -217,7 +217,7 @@ public class IndexExcelDataListener extends AnalysisEventListener<IndexModel> {
group1 = new IndexGroupTemplateEntity();
group1.setIndexId(indexDictEntity.getId());
group1.setParentIndexGroupId("0");
group1.setId(UniqueIdGenerator.generate());
group1.setId(UniqueIdGenerator.generate24());
group1.setIndexCode(Pinyin4jUtil.getSpellPinYin(indexDictEntity.getIndexName(), false, 4));
group1.setAllIndexCodePath(index.getAllIndexCodePath(0));
indexGroupMap.put(index.getLevel1Index(), group1);
@ -232,7 +232,7 @@ public class IndexExcelDataListener extends AnalysisEventListener<IndexModel> {
group2 = new IndexGroupTemplateEntity();
group2.setIndexId(indexDictEntity.getId());
group2.setParentIndexGroupId(group1.getId());
group2.setId(UniqueIdGenerator.generate());
group2.setId(UniqueIdGenerator.generate24());
group2.setIndexCode(Pinyin4jUtil.getSpellPinYin(indexDictEntity.getIndexName(), false, 4));
group2.setAllIndexCodePath(index.getAllIndexCodePath(1));
indexGroupMap.put(level2IndexGroupKey, group2);
@ -243,7 +243,7 @@ public class IndexExcelDataListener extends AnalysisEventListener<IndexModel> {
templateEntity.setIndexGroupId(group2.getId());
templateEntity.setIndexId(indexDictEntity.getId());
templateEntity.setAllParentIndexCode(allIndexCodeSb.toString());
templateEntity.setId(UniqueIdGenerator.generate());
templateEntity.setId(UniqueIdGenerator.generate24());
templateEntity.setCorrelation(index.getCorrelation());
templateEntity.setAllIndexCodePath(index.getAllIndexCodePath(1));
buildIndexGroupDetail(templateEntity, indexDictEntity, index, 2);
@ -260,7 +260,7 @@ public class IndexExcelDataListener extends AnalysisEventListener<IndexModel> {
templateEntity.setIndexGroupId(group2.getId());
templateEntity.setIndexId(indexDictEntity.getId());
templateEntity.setAllParentIndexCode(allIndexCodeSb.toString());
templateEntity.setId(UniqueIdGenerator.generate());
templateEntity.setId(UniqueIdGenerator.generate24());
templateEntity.setCorrelation(index.getCorrelation());
templateEntity.setAllIndexCodePath(index.getAllIndexCodePath(4));
buildIndexGroupDetail(templateEntity, indexDictEntity, index, 5);
@ -289,7 +289,7 @@ public class IndexExcelDataListener extends AnalysisEventListener<IndexModel> {
private void buildIndexDicEntity(IndexModel data, IndexDictEntity entity, IndexDictEntity entity2, IndexDictEntity entity3, IndexDictEntity entity4, IndexDictEntity entity5) {
if (!indexDicMap.containsKey(data.getLevel1Index())) {
entity.setId(UniqueIdGenerator.generate());
entity.setId(UniqueIdGenerator.generate24());
entity.setIndexName(data.getLevel1Index());
entity.setCorrelation(data.getCorrelation());
entity.setLevel("1");
@ -297,7 +297,7 @@ public class IndexExcelDataListener extends AnalysisEventListener<IndexModel> {
indexDicMap.put(data.getLevel1Index(), entity);
}
if (!indexDicMap.containsKey(data.getLevel2Index())) {
entity2.setId(UniqueIdGenerator.generate());
entity2.setId(UniqueIdGenerator.generate24());
entity2.setIndexName(data.getLevel2Index());
entity2.setCorrelation(data.getCorrelation());
entity2.setLevel("2");
@ -305,7 +305,7 @@ public class IndexExcelDataListener extends AnalysisEventListener<IndexModel> {
indexDicMap.put(data.getLevel2Index(), entity2);
}
if (!indexDicMap.containsKey(data.getLevel3Index())) {
entity3.setId(UniqueIdGenerator.generate());
entity3.setId(UniqueIdGenerator.generate24());
entity3.setIndexName(data.getLevel3Index());
entity3.setCorrelation(data.getCorrelation());
entity3.setLevel("3");
@ -313,7 +313,7 @@ public class IndexExcelDataListener extends AnalysisEventListener<IndexModel> {
indexDicMap.put(data.getLevel3Index(), entity3);
}
if (!indexDicMap.containsKey(data.getLevel4Index())) {
entity4.setId(UniqueIdGenerator.generate());
entity4.setId(UniqueIdGenerator.generate24());
entity4.setIndexName(data.getLevel4Index());
entity4.setCorrelation(data.getCorrelation());
entity4.setLevel("4");
@ -321,7 +321,7 @@ public class IndexExcelDataListener extends AnalysisEventListener<IndexModel> {
indexDicMap.put(data.getLevel4Index(), entity4);
}
if (!indexDicMap.containsKey(data.getLevel5Index())) {
entity5.setId(UniqueIdGenerator.generate());
entity5.setId(UniqueIdGenerator.generate24());
entity5.setIndexName(data.getLevel5Index());
entity5.setCorrelation(data.getCorrelation());
entity5.setLevel("5");

6
epmet-module/data-statistical/data-statistical-server/src/main/java/com/epmet/service/evaluationindex/screen/impl/IndexGroupServiceImpl.java

@ -80,7 +80,7 @@ private IndexGroupTemplateDao indexGroupTemplateDao;
List<IndexGroupEntity> insertGroupList = new ArrayList<>();
groupMap.forEach((oldGroupTempId, o) -> {
IndexGroupEntity newGroupEntity = ConvertUtils.sourceToTarget(o, IndexGroupEntity.class);
String newGroupId = UniqueIdGenerator.generate();
String newGroupId = UniqueIdGenerator.generate24();
newGroupEntity.setId(newGroupId);
newGroupEntity.setCustomerId(customerId);
groupNewIdMap.put(oldGroupTempId, newGroupId);
@ -99,7 +99,7 @@ private IndexGroupTemplateDao indexGroupTemplateDao;
IndexGroupDetailEntity entity = ConvertUtils.sourceToTarget(groupDetailTemp, IndexGroupDetailEntity.class);
entity.setId(UniqueIdGenerator.generate());
entity.setId(UniqueIdGenerator.generate24());
String newGroupId = groupNewIdMap.get(oldIndexGroupId);
if (StringUtils.isBlank(newGroupId)){
log.error("新旧id映射关系有误");
@ -134,4 +134,4 @@ private IndexGroupTemplateDao indexGroupTemplateDao;
});*/
return true;
}
}
}

Loading…
Cancel
Save