mirror of
https://codeup.aliyun.com/67c68d4e484ca2f0a13ac3c1/ydc/jsowell-charger-web.git
synced 2026-04-22 03:55:17 +08:00
187 lines
5.5 KiB
Java
187 lines
5.5 KiB
Java
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;
|
||
import org.springframework.stereotype.Component;
|
||
|
||
import java.time.LocalDate;
|
||
import java.util.List;
|
||
|
||
/**
|
||
* 生成sn号Util
|
||
*/
|
||
@Component
|
||
public class PileSnGenerateService {
|
||
|
||
private Logger logger = LoggerFactory.getLogger(this.getClass());
|
||
|
||
@Autowired
|
||
private RedisCache redisCache;
|
||
|
||
@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
|
||
* @param prefix 生成序列号的前缀
|
||
* @return
|
||
*/
|
||
public synchronized String getEVPileSn(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();
|
||
// 将前四位截取,并将后面转为int
|
||
pileNum = Integer.parseInt(StringUtils.substring(pileSn, 4, pileSn.length()));
|
||
// 再将该值存入数据库
|
||
redisCache.setCacheObject(key, pileNum);
|
||
}
|
||
Long increNum = redisCache.increment(key, 1);
|
||
// 年份
|
||
int year = LocalDate.now().getYear() - 2000;
|
||
//不足补位
|
||
increResult = prefix + year + String.format("%1$010d", increNum);
|
||
|
||
// 保存到字典中
|
||
savePileSn2Dict(pileNum);
|
||
} catch (Exception e) {
|
||
logger.error("获取序列号失败", e);
|
||
}
|
||
return increResult;
|
||
}
|
||
|
||
private void savePileSn2Dict(long pileSnNum) {
|
||
SysDictData queryData = new SysDictData();
|
||
queryData.setDictType(pile_sn_generate_type);
|
||
queryData.setDictLabel(EV_PILE_SN_LABEL);
|
||
List<SysDictData> 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(EV_PILE_SN_LABEL);
|
||
dictData.setDictValue(pileSnNum + "");
|
||
dictData.setListClass(Constants.DEFAULT);
|
||
dictData.setCreateBy(Constants.SYSTEM);
|
||
dictDataService.insertDictData(dictData);
|
||
}
|
||
}
|
||
|
||
/**
|
||
* 批量生成sn号
|
||
*
|
||
* @param size
|
||
* @return
|
||
*/
|
||
public List<String> generateEVPileSN(int size) {
|
||
List<String> resultList = Lists.newArrayList();
|
||
if (size <= 0) {
|
||
return resultList;
|
||
}
|
||
for (int i = 0; i < size; i++) {
|
||
resultList.add(getEVPileSn(prefix));
|
||
}
|
||
return resultList;
|
||
}
|
||
|
||
/**
|
||
* 生成电单车桩号
|
||
* @param size 生成数量
|
||
* @return
|
||
*/
|
||
public List<String> generateEBikeSN(int size) {
|
||
List<String> resultList = Lists.newArrayList();
|
||
if (size <= 0) {
|
||
return resultList;
|
||
}
|
||
for (int i = 0; i < size; i++) {
|
||
resultList.add(getEBikePileSn(prefix));
|
||
}
|
||
return resultList;
|
||
}
|
||
|
||
/**
|
||
* 获取递增的序列号 汽车桩
|
||
* 充电桩编号定义为14位: 固定位88 + 年份23 + 10位自增数字不足补0
|
||
* @param prefix 生成序列号的前缀
|
||
* @return
|
||
*/
|
||
public synchronized String getEBikePileSn(String prefix) {
|
||
String increResult = null;
|
||
|
||
// 年份
|
||
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);
|
||
saveEBikeSn2Dict(year, pileSnNum);
|
||
} else {
|
||
// 没有值, 从1开始, 并保存到字典中
|
||
Long pileSnNum = 1L;
|
||
increResult = prefix + year + String.format("%1$04d", pileSnNum);
|
||
saveEBikeSn2Dict(year, pileSnNum);
|
||
}
|
||
return increResult;
|
||
}
|
||
|
||
|
||
private void saveEBikeSn2Dict(int year, long pileSnNum) {
|
||
SysDictData queryData = new SysDictData();
|
||
queryData.setDictType(pile_sn_generate_type);
|
||
queryData.setDictLabel(EBIKE_PILE_SN_LABEL + year);
|
||
List<SysDictData> 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);
|
||
}
|
||
}
|
||
|
||
}
|