This commit is contained in:
2023-08-26 09:48:06 +08:00
7 changed files with 55 additions and 5 deletions

View File

@@ -157,4 +157,10 @@ public interface PileBasicInfoMapper {
* 批量修改充电桩运营商
*/
void updatePileMerchantBatch(@Param("pileIdList") List<Long> pileIdList, @Param("newMerchantId") String newMerchantId);
/**
* 获取最新一条桩的数据
* @return
*/
PileBasicInfo getMaxNumPileInfo();
}

View File

@@ -177,4 +177,9 @@ public interface IPileBasicInfoService {
* @return
*/
List<GroundLockInfoVO> getGroundLockInfo(String stationId);
/**
* 获取最新建一条桩的信息
*/
PileBasicInfo getMaxNumPileInfo();
}

View File

@@ -788,4 +788,13 @@ public class PileBasicInfoServiceImpl implements IPileBasicInfoService {
}
return resultList;
}
/**
* 获取最新一条桩的数据
* @return
*/
@Override
public PileBasicInfo getMaxNumPileInfo() {
return pileBasicInfoMapper.getMaxNumPileInfo();
}
}

View File

@@ -0,0 +1,100 @@
package com.jsowell.pile.util;
import com.google.common.collect.Lists;
import com.jsowell.common.constant.CacheConstants;
import com.jsowell.common.core.redis.RedisCache;
import com.jsowell.common.util.StringUtils;
import com.jsowell.pile.domain.PileBasicInfo;
import com.jsowell.pile.service.IPileBasicInfoService;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import java.time.LocalDate;
import java.util.List;
/**
* 生成sn号Util
*/
@Component
public class SnUtils {
private static Logger logger = LoggerFactory.getLogger(SnUtils.class);
@Autowired
private RedisCache redisCache;
@Autowired
private IPileBasicInfoService pileBasicInfoService;
private String prefix = "88";
/**
* 获取递增的序列号
* 充电桩编号定义为14位: 固定位88 + 年份23 + 10位自增数字不足补0
* @param prefix 生成序列号的前缀
* @return
*/
private String getPileSn(String prefix) {
//序列号前缀加特定标识,如系统模块名之类的 防止重复
String key = CacheConstants.PILE_SN_GENERATE_KEY + prefix;
String increResult = null;
try {
String pileNum = redisCache.getCacheObject(key);
if (StringUtils.isBlank(pileNum)) {
// 缓存中没有,从数据库中取
PileBasicInfo pileInfo = pileBasicInfoService.getMaxNumPileInfo();
String pileSn = pileInfo.getSn();
// 将前四位截取并将后面转为long
long pileSnNum = Long.parseLong(StringUtils.substring(pileSn, 4, pileSn.length()));
// 再将该值存入数据库
redisCache.setCacheObject(key, pileSnNum);
}
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);
}
return increResult;
}
public static void main(String[] args) {
String pileSn = "88230000002060";
String substring = StringUtils.substring(pileSn, 4, pileSn.length());
long l = Long.parseLong(substring);
System.out.println(substring);
System.out.println(l);
}
/**
* 生成sn号
*
* @return
*/
public List<String> generateSN() {
return generateSN(1);
}
/**
* 批量生成sn号
*
* @param size
* @return
*/
public List<String> generateSN(int size) {
List<String> resultList = Lists.newArrayList();
if (size <= 0) {
return resultList;
}
for (int i = 0; i < size; i++) {
resultList.add(getPileSn(prefix));
}
return resultList;
}
}