diff --git a/jsowell-admin/src/main/java/com/jsowell/service/PileService.java b/jsowell-admin/src/main/java/com/jsowell/service/PileService.java index cfe3bc64e..62927ebe5 100644 --- a/jsowell-admin/src/main/java/com/jsowell/service/PileService.java +++ b/jsowell-admin/src/main/java/com/jsowell/service/PileService.java @@ -115,9 +115,9 @@ public class PileService { // 批量生成sn号 List snList = null; if (StringUtils.equals(SoftwareProtocolEnum.YOU_DIAN.getValue(), softwareProtocol)) { - snList = snUtils.generateSN(dto.getNum()); + snList = snUtils.generateEBikeSN(dto.getNum()); } else { - snList = snUtils.generateSN(dto.getNum()); + snList = snUtils.generateEVPileSN(dto.getNum()); } // diff --git a/jsowell-admin/src/test/java/SpringBootTestController.java b/jsowell-admin/src/test/java/SpringBootTestController.java index 2b49fa1b8..52b0222cd 100644 --- a/jsowell-admin/src/test/java/SpringBootTestController.java +++ b/jsowell-admin/src/test/java/SpringBootTestController.java @@ -261,6 +261,15 @@ public class SpringBootTestController { @Autowired private ZDLService zdlService; + @Autowired + private PileSnGenerateService pileSnGenerateService; + + @Test + public void getEBikePileSnTest() { + List strings = pileSnGenerateService.generateEBikeSN(8); + System.out.println(strings); + } + @Test public void getListByMemberIdAndOrderStatusTest() { String memberId = "12345678"; @@ -3507,7 +3516,7 @@ public class SpringBootTestController { // 生成100个 stopWatch.start("生成100个sn号"); - List list2 = snUtils.generateSN(1); + List list2 = snUtils.generateEVPileSN(1); stopWatch.stop(); System.out.println(list2); diff --git a/jsowell-pile/src/main/java/com/jsowell/pile/service/PileSnGenerateService.java b/jsowell-pile/src/main/java/com/jsowell/pile/service/PileSnGenerateService.java index 18f06648f..6906bbc48 100644 --- a/jsowell-pile/src/main/java/com/jsowell/pile/service/PileSnGenerateService.java +++ b/jsowell-pile/src/main/java/com/jsowell/pile/service/PileSnGenerateService.java @@ -2,9 +2,14 @@ package com.jsowell.pile.service; import com.google.common.collect.Lists; import com.jsowell.common.constant.CacheConstants; +import com.jsowell.common.constant.Constants; +import com.jsowell.common.core.domain.entity.SysDictData; import com.jsowell.common.core.redis.RedisCache; +import com.jsowell.common.util.DictUtils; import com.jsowell.common.util.StringUtils; import com.jsowell.pile.domain.PileBasicInfo; +import com.jsowell.system.service.SysDictDataService; +import org.apache.commons.collections4.CollectionUtils; import org.slf4j.Logger; import org.slf4j.LoggerFactory; import org.springframework.beans.factory.annotation.Autowired; @@ -27,8 +32,18 @@ public class PileSnGenerateService { @Autowired private PileBasicInfoService pileBasicInfoService; + @Autowired + private SysDictDataService dictDataService; + private final String prefix = "88"; + // 桩号生成字典类型 + private String pile_sn_generate_type = "pile_sn_generate"; + + private String EV_PILE_SN_LABEL = "云快充桩号"; + + private String EBIKE_PILE_SN_LABEL = "友电桩号"; + /** * 获取递增的序列号 汽车桩 * 充电桩编号定义为14位: 固定位88 + 年份23 + 10位自增数字不足补0 @@ -50,7 +65,6 @@ public class PileSnGenerateService { // String pileSnNum = StringUtils.substring(pileSn, 4, pileSn.length()); // 再将该值存入数据库 redisCache.setCacheObject(key, pileSnNum.intValue()); - } Long increNum = redisCache.increment(key, 1); // 年份 @@ -63,13 +77,14 @@ public class PileSnGenerateService { return increResult; } + /** * 批量生成sn号 * * @param size * @return */ - public List generateSN(int size) { + public List generateEVPileSN(int size) { List resultList = Lists.newArrayList(); if (size <= 0) { return resultList; @@ -80,6 +95,11 @@ public class PileSnGenerateService { return resultList; } + /** + * 生成电单车桩号 + * @param size 生成数量 + * @return + */ public List generateEBikeSN(int size) { List resultList = Lists.newArrayList(); if (size <= 0) { @@ -98,31 +118,48 @@ public class PileSnGenerateService { * @return */ public synchronized String getEBikePileSn(String prefix) { - String key = CacheConstants.PILE_SN_GENERATE_KEY + prefix; String increResult = null; - try { - // 取缓存 - Integer pileNum = redisCache.getCacheObject(key); - if (pileNum == null) { - // 缓存中没有,从数据库中取 - PileBasicInfo pileInfo = pileBasicInfoService.getMaxNumPileInfo(); - String pileSn = pileInfo.getSn(); - // 将前四位截取,并将后面转为long - Long pileSnNum = Long.parseLong(StringUtils.substring(pileSn, 4, pileSn.length())); - // String pileSnNum = StringUtils.substring(pileSn, 4, pileSn.length()); - // 再将该值存入数据库 - redisCache.setCacheObject(key, pileSnNum.intValue()); - } - Long increNum = redisCache.increment(key, 1); - // 年份 - int year = LocalDate.now().getYear() - 2000; - //不足补位 - increResult = prefix + year + String.format("%1$010d", increNum); - } catch (Exception e) { - logger.error("获取序列号失败", e); + // 年份 + int year = LocalDate.now().getYear() - 2000; + + // 取字典中保存的值 + String dictValue = DictUtils.getDictValue(pile_sn_generate_type, EBIKE_PILE_SN_LABEL + year); + if (StringUtils.isNotBlank(dictValue)) { + // 将前四位截取,并将后面转为long + Long pileSnNum = Long.parseLong(dictValue); + pileSnNum += 1; + // 再将该值存入数据库 + increResult = prefix + year + String.format("%1$04d", pileSnNum); + save2Dict(year, pileSnNum); + } else { + // 没有值, 从1开始, 并保存到字典中 + Long pileSnNum = 1L; + increResult = prefix + year + String.format("%1$04d", pileSnNum); + save2Dict(year, pileSnNum); } return increResult; } + + private void save2Dict(int year, long pileSnNum) { + SysDictData queryData = new SysDictData(); + queryData.setDictType(pile_sn_generate_type); + queryData.setDictLabel(EBIKE_PILE_SN_LABEL + year); + List sysDictData = dictDataService.selectDictDataList(queryData); + if (CollectionUtils.isNotEmpty(sysDictData)) { + SysDictData dictData = sysDictData.get(0); + dictData.setDictValue(pileSnNum + ""); + dictDataService.updateDictData(dictData); + } else { + SysDictData dictData = new SysDictData(); + dictData.setDictType(pile_sn_generate_type); + dictData.setDictLabel(EBIKE_PILE_SN_LABEL + year); + dictData.setDictValue(pileSnNum + ""); + dictData.setListClass(Constants.DEFAULT); + dictData.setCreateBy(Constants.SYSTEM); + dictDataService.insertDictData(dictData); + } + } + }