2024-09-03 09:36:27 +08:00
|
|
|
|
package com.jsowell.pile.service;
|
2023-03-04 16:29:55 +08:00
|
|
|
|
|
|
|
|
|
|
import com.google.common.collect.Lists;
|
|
|
|
|
|
import com.jsowell.common.constant.CacheConstants;
|
2024-09-21 16:26:05 +08:00
|
|
|
|
import com.jsowell.common.constant.Constants;
|
|
|
|
|
|
import com.jsowell.common.core.domain.entity.SysDictData;
|
2023-03-04 16:29:55 +08:00
|
|
|
|
import com.jsowell.common.core.redis.RedisCache;
|
2024-09-21 16:26:05 +08:00
|
|
|
|
import com.jsowell.common.util.DictUtils;
|
2023-08-26 09:46:46 +08:00
|
|
|
|
import com.jsowell.common.util.StringUtils;
|
|
|
|
|
|
import com.jsowell.pile.domain.PileBasicInfo;
|
2024-09-21 16:26:05 +08:00
|
|
|
|
import com.jsowell.system.service.SysDictDataService;
|
|
|
|
|
|
import org.apache.commons.collections4.CollectionUtils;
|
2023-03-04 16:29:55 +08:00
|
|
|
|
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
|
2024-09-03 09:36:27 +08:00
|
|
|
|
public class PileSnGenerateService {
|
2023-03-04 16:29:55 +08:00
|
|
|
|
|
2024-09-21 14:54:58 +08:00
|
|
|
|
private Logger logger = LoggerFactory.getLogger(this.getClass());
|
2023-03-04 16:29:55 +08:00
|
|
|
|
|
|
|
|
|
|
@Autowired
|
2023-08-26 09:46:46 +08:00
|
|
|
|
private RedisCache redisCache;
|
|
|
|
|
|
|
|
|
|
|
|
@Autowired
|
2024-01-06 15:20:28 +08:00
|
|
|
|
private PileBasicInfoService pileBasicInfoService;
|
2023-03-04 16:29:55 +08:00
|
|
|
|
|
2024-09-21 16:26:05 +08:00
|
|
|
|
@Autowired
|
|
|
|
|
|
private SysDictDataService dictDataService;
|
|
|
|
|
|
|
2024-09-21 14:54:58 +08:00
|
|
|
|
private final String prefix = "88";
|
2023-03-04 16:29:55 +08:00
|
|
|
|
|
2024-09-21 16:26:05 +08:00
|
|
|
|
// 桩号生成字典类型
|
|
|
|
|
|
private String pile_sn_generate_type = "pile_sn_generate";
|
|
|
|
|
|
|
|
|
|
|
|
private String EV_PILE_SN_LABEL = "云快充桩号";
|
|
|
|
|
|
|
|
|
|
|
|
private String EBIKE_PILE_SN_LABEL = "友电桩号";
|
|
|
|
|
|
|
2023-03-04 16:29:55 +08:00
|
|
|
|
/**
|
2024-09-21 14:54:58 +08:00
|
|
|
|
* 获取递增的序列号 汽车桩
|
2023-03-04 16:29:55 +08:00
|
|
|
|
* 充电桩编号定义为14位: 固定位88 + 年份23 + 10位自增数字不足补0
|
|
|
|
|
|
* @param prefix 生成序列号的前缀
|
|
|
|
|
|
* @return
|
|
|
|
|
|
*/
|
2024-09-21 14:54:58 +08:00
|
|
|
|
public synchronized String getEVPileSn(String prefix) {
|
2023-03-04 16:29:55 +08:00
|
|
|
|
String key = CacheConstants.PILE_SN_GENERATE_KEY + prefix;
|
|
|
|
|
|
String increResult = null;
|
|
|
|
|
|
try {
|
2023-08-26 15:47:10 +08:00
|
|
|
|
// 取缓存
|
|
|
|
|
|
Integer pileNum = redisCache.getCacheObject(key);
|
|
|
|
|
|
if (pileNum == null) {
|
2023-08-26 09:46:46 +08:00
|
|
|
|
// 缓存中没有,从数据库中取
|
|
|
|
|
|
PileBasicInfo pileInfo = pileBasicInfoService.getMaxNumPileInfo();
|
|
|
|
|
|
String pileSn = pileInfo.getSn();
|
2024-09-25 17:07:17 +08:00
|
|
|
|
// 将前四位截取,并将后面转为int
|
|
|
|
|
|
pileNum = Integer.parseInt(StringUtils.substring(pileSn, 4, pileSn.length()));
|
2023-08-26 09:46:46 +08:00
|
|
|
|
// 再将该值存入数据库
|
2024-09-25 17:07:17 +08:00
|
|
|
|
redisCache.setCacheObject(key, pileNum);
|
2023-08-26 09:46:46 +08:00
|
|
|
|
}
|
2023-03-04 16:29:55 +08:00
|
|
|
|
Long increNum = redisCache.increment(key, 1);
|
|
|
|
|
|
// 年份
|
|
|
|
|
|
int year = LocalDate.now().getYear() - 2000;
|
|
|
|
|
|
//不足补位
|
|
|
|
|
|
increResult = prefix + year + String.format("%1$010d", increNum);
|
2024-09-25 17:07:17 +08:00
|
|
|
|
|
|
|
|
|
|
// 保存到字典中
|
|
|
|
|
|
savePileSn2Dict(pileNum);
|
2023-03-04 16:29:55 +08:00
|
|
|
|
} catch (Exception e) {
|
|
|
|
|
|
logger.error("获取序列号失败", e);
|
|
|
|
|
|
}
|
|
|
|
|
|
return increResult;
|
|
|
|
|
|
}
|
|
|
|
|
|
|
2024-09-25 17:07:17 +08:00
|
|
|
|
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(EBIKE_PILE_SN_LABEL);
|
|
|
|
|
|
dictData.setDictValue(pileSnNum + "");
|
|
|
|
|
|
dictData.setListClass(Constants.DEFAULT);
|
|
|
|
|
|
dictData.setCreateBy(Constants.SYSTEM);
|
|
|
|
|
|
dictDataService.insertDictData(dictData);
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|
2024-09-21 16:26:05 +08:00
|
|
|
|
|
2023-03-04 16:29:55 +08:00
|
|
|
|
/**
|
|
|
|
|
|
* 批量生成sn号
|
|
|
|
|
|
*
|
|
|
|
|
|
* @param size
|
|
|
|
|
|
* @return
|
|
|
|
|
|
*/
|
2024-09-21 16:26:05 +08:00
|
|
|
|
public List<String> generateEVPileSN(int size) {
|
2023-03-04 16:29:55 +08:00
|
|
|
|
List<String> resultList = Lists.newArrayList();
|
|
|
|
|
|
if (size <= 0) {
|
|
|
|
|
|
return resultList;
|
|
|
|
|
|
}
|
|
|
|
|
|
for (int i = 0; i < size; i++) {
|
2024-09-21 14:54:58 +08:00
|
|
|
|
resultList.add(getEVPileSn(prefix));
|
|
|
|
|
|
}
|
|
|
|
|
|
return resultList;
|
|
|
|
|
|
}
|
|
|
|
|
|
|
2024-09-21 16:26:05 +08:00
|
|
|
|
/**
|
|
|
|
|
|
* 生成电单车桩号
|
|
|
|
|
|
* @param size 生成数量
|
|
|
|
|
|
* @return
|
|
|
|
|
|
*/
|
2024-09-21 14:54:58 +08:00
|
|
|
|
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));
|
2023-03-04 16:29:55 +08:00
|
|
|
|
}
|
|
|
|
|
|
return resultList;
|
|
|
|
|
|
}
|
|
|
|
|
|
|
2024-09-21 14:54:58 +08:00
|
|
|
|
/**
|
|
|
|
|
|
* 获取递增的序列号 汽车桩
|
|
|
|
|
|
* 充电桩编号定义为14位: 固定位88 + 年份23 + 10位自增数字不足补0
|
|
|
|
|
|
* @param prefix 生成序列号的前缀
|
|
|
|
|
|
* @return
|
|
|
|
|
|
*/
|
|
|
|
|
|
public synchronized String getEBikePileSn(String prefix) {
|
|
|
|
|
|
String increResult = null;
|
|
|
|
|
|
|
2024-09-21 16:26:05 +08:00
|
|
|
|
// 年份
|
|
|
|
|
|
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);
|
2024-09-25 17:07:17 +08:00
|
|
|
|
saveEBikeSn2Dict(year, pileSnNum);
|
2024-09-21 16:26:05 +08:00
|
|
|
|
} else {
|
|
|
|
|
|
// 没有值, 从1开始, 并保存到字典中
|
|
|
|
|
|
Long pileSnNum = 1L;
|
|
|
|
|
|
increResult = prefix + year + String.format("%1$04d", pileSnNum);
|
2024-09-25 17:07:17 +08:00
|
|
|
|
saveEBikeSn2Dict(year, pileSnNum);
|
2024-09-21 14:54:58 +08:00
|
|
|
|
}
|
|
|
|
|
|
return increResult;
|
|
|
|
|
|
}
|
|
|
|
|
|
|
2024-09-21 16:26:05 +08:00
|
|
|
|
|
2024-09-25 17:07:17 +08:00
|
|
|
|
private void saveEBikeSn2Dict(int year, long pileSnNum) {
|
2024-09-21 16:26:05 +08:00
|
|
|
|
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);
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
|
2023-03-04 16:29:55 +08:00
|
|
|
|
}
|