fix QueryStationsInfo

This commit is contained in:
ZZ 2021-11-16 15:04:26 +08:00
parent 591be029bd
commit 356bdd7084
3 changed files with 122 additions and 209 deletions

View File

@ -1,14 +1,15 @@
package com.xhpc.evcs.api;
import com.xhpc.common.api.ChargingStationPushStatusService;
import com.xhpc.common.api.dto.ChargingStationDto;
import com.xhpc.common.data.redis.CacheRateModel;
import com.xhpc.evcs.domain.XhpcChargingPile;
import com.xhpc.evcs.domain.XhpcChargingStation;
import com.xhpc.evcs.domain.XhpcInternetUser;
import com.xhpc.evcs.domain.XhpcStationInternetBlacklist;
import com.xhpc.evcs.dto.*;
import com.xhpc.evcs.jpa.XhpcChargingPileRepository;
import com.xhpc.evcs.jpa.XhpcChargingStationRepository;
import com.xhpc.evcs.jpa.XhpcInternetUserRepository;
import com.xhpc.evcs.jpa.XhpcStationInternetBlacklistRepository;
import com.xhpc.evcs.utils.JSONUtil;
import lombok.extern.slf4j.Slf4j;
@ -20,6 +21,7 @@ import org.springframework.web.bind.annotation.RestController;
import java.math.BigDecimal;
import java.math.RoundingMode;
import java.time.Instant;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;
@ -32,7 +34,7 @@ import static com.xhpc.common.data.redis.StaticBeanUtil.REDIS;
public class QueryStationsInfoController {
@Autowired
private ChargingStationPushStatusService chargingStationPushStatusService;
private XhpcInternetUserRepository xhpcInternetUserRepository;
@Autowired
private XhpcChargingPileRepository xhpcChargingPileRepository;
@Autowired
@ -43,10 +45,8 @@ public class QueryStationsInfoController {
@PostMapping("/v1/query_stations_info")
public CommonResponse queryStationsInfo(@RequestBody(required = false) CommonRequest<PageRequest> commonRequest) throws Exception {
// 电站状态更新逻辑,没必要做全推
// chargingStationPushStatusService.editStationStatus();
// chargingStationPushStatusService.saveStationStatus();
CommonResponse resp = new CommonResponse();
//当前页数
PageRequest pageRequest = commonRequest.transformDataType(PageRequest.class);
Integer pageNo = pageRequest.getPageNo();
@ -54,215 +54,128 @@ public class QueryStationsInfoController {
response.setPageNo(pageNo);
//页码总数
String operatorId = commonRequest.getOperatorId();
List<XhpcStationInternetBlacklist> xhpcStationInternetBlacklist = xhpcStationInternetBlacklistRepo.findByInternetUserId(operatorId);
List<String> stationKeys = new ArrayList<>(REDIS.keys("station:*"));
List<String> validStationKeys = new ArrayList<>();
for (String stationKey : stationKeys) {
boolean isValid = true;
for (XhpcStationInternetBlacklist xhpcStationInternetBlack : xhpcStationInternetBlacklist) {
if (stationKey.substring(8).equals(xhpcStationInternetBlack.getChargingStationId().toString())) {
isValid = false;
}
}
ChargingStationDto chargingStationDto = REDIS.getCacheObject(stationKey);
if (chargingStationDto.getPiles() != null) {
if (isValid) {
validStationKeys.add(stationKey);
}
}
}
if (validStationKeys.size() > 0) {
Integer pageSize = pageRequest.getPageSize();
int totalPage = validStationKeys.size() % pageSize == 0 ? validStationKeys.size() / pageSize : (validStationKeys.size() / pageSize) + 1;
response.setPageCount(totalPage);
}
//总记录条数
response.setItemSize(validStationKeys.size());
//充电站信息列表
List<StationInfo> stationInfos = new ArrayList<>();
//计算每页开始索引
int startIndex = -1;
int endIndex = -1;
if (pageNo == 1 && validStationKeys.size() <= 10) {
startIndex = 0;
endIndex = validStationKeys.size() - 1;
final List<XhpcInternetUser> xhpcInternetUserList =
xhpcInternetUserRepository.findByCooperationStartTimeBeforeAndCooperationEndTimeAfterAndOperatorIdEvcsLike(Instant.now(), Instant.now(), operatorId);
if (xhpcInternetUserList.isEmpty()) {
resp.setRet("1");
resp.setMsg("Not valid internet user/OperatorID");
resp.setData(JSONUtil.toJSONString(response));
} else {
Integer pageSize = pageRequest.getPageSize();
startIndex = (pageNo - 1) * pageSize;
endIndex = startIndex + (pageSize - 1);
}
for (int i = startIndex; i <= endIndex; i++) {
String stationKey = null;
try {
stationKey = validStationKeys.get(i);
} catch (Exception e) {
continue;
}
ChargingStationDto chargingStationDto = REDIS.getCacheObject(stationKey);
StationInfo stationInfo = new StationInfo();
//站点状态
stationInfo.setStationStatus(50);
//充电站Id
String stationId = stationKey.replace("station:", "");
stationInfo.setStationId(stationId);
Double lat = chargingStationDto.getLat();
if (lat == null) {
XhpcChargingStation stationExample = new XhpcChargingStation();
stationExample.setId(Long.parseLong(stationId));
Example<XhpcChargingStation> example = Example.of(stationExample);
XhpcChargingStation xhpcChargingStation =
xhpcChargingStationRepository.findOne(example).orElse(null);
if (xhpcChargingStation != null) {
//纬度
chargingStationDto.setLat(new BigDecimal(xhpcChargingStation.getLatitude()).setScale(6,
RoundingMode.HALF_UP).doubleValue());
//经度
chargingStationDto.setLng(new BigDecimal(xhpcChargingStation.getLongitude()).setScale(6,
RoundingMode.HALF_UP).doubleValue());
//站点类型
chargingStationDto.setType(xhpcChargingStation.getType());
//运营商id
String operatorIdEvcs = xhpcChargingStation.getOperatorIdEvcs();
chargingStationDto.setOperatorId(operatorIdEvcs == null ? "MA6DFCTD5"
: operatorIdEvcs.length() == 9 ? operatorId : operatorIdEvcs.substring(9, 19));
//详细地址
chargingStationDto.setAddress(xhpcChargingStation.getAddress());
//充电站省直辖区编码
chargingStationDto.setAreaCode(xhpcChargingStation.getAreaCode().toString());
//充电站名称
chargingStationDto.setName(xhpcChargingStation.getName());
//服务电话
chargingStationDto.setServiceTel(xhpcChargingStation.getServiceTel());
//车位数量
chargingStationDto.setParkNums(xhpcChargingStation.getParkNums());
REDIS.setCacheObject(stationKey, chargingStationDto);
Long internetUserId = xhpcInternetUserList.get(0).getId();
List<XhpcStationInternetBlacklist> xhpcStationInternetBlacklist =
xhpcStationInternetBlacklistRepo.findByInternetUserId(internetUserId);
List<String> stationKeys = new ArrayList<>(REDIS.keys("station:*"));
List<String> validStationKeys = new ArrayList<>();
for (String stationKey : stationKeys) {
boolean isValid = true;
for (XhpcStationInternetBlacklist xhpcStationInternetBlack : xhpcStationInternetBlacklist) {
if (stationKey.substring(8).equals(xhpcStationInternetBlack.getChargingStationId().toString())) {
isValid = false;
}
}
ChargingStationDto chargingStationDto = REDIS.getCacheObject(stationKey);
if (chargingStationDto.getPiles() != null) {
if (isValid) {
validStationKeys.add(stationKey);
}
}
}
stationInfo.setOperatorId(chargingStationDto.getOperatorId());
stationInfo.setEquipmentOwnerId(chargingStationDto.getOperatorId());
stationInfo.setStationName(chargingStationDto.getName());
stationInfo.setCountryCode("CN");
stationInfo.setAreaCode(chargingStationDto.getAreaCode());
stationInfo.setAddress(chargingStationDto.getAddress());
stationInfo.setServiceTel(chargingStationDto.getServiceTel());
stationInfo.setStationType(chargingStationDto.getType());
stationInfo.setParkNums(chargingStationDto.getParkNums());
stationInfo.setStationLng(chargingStationDto.getLng());
stationInfo.setStationLat(chargingStationDto.getLat());
stationInfo.setConstruction(chargingStationDto.getConstruction());
//获取该充电站的下的所有充电设备列表
Set<String> piles = chargingStationDto.getPiles();
List<EquipmentInfo> equipmentInfos = getEquipmentInfos(piles);
stationInfo.setEquipmentInfos(equipmentInfos);
stationInfos.add(stationInfo);
if (validStationKeys.size() > 0) {
Integer pageSize = pageRequest.getPageSize();
int totalPage = validStationKeys.size() % pageSize == 0 ? validStationKeys.size() / pageSize :
(validStationKeys.size() / pageSize) + 1;
response.setPageCount(totalPage);
}
//总记录条数
response.setItemSize(validStationKeys.size());
//充电站信息列表
List<StationInfo> stationInfos = new ArrayList<>();
//计算每页开始索引
int startIndex = -1;
int endIndex = -1;
if (pageNo == 1 && validStationKeys.size() <= 10) {
startIndex = 0;
endIndex = validStationKeys.size() - 1;
} else {
Integer pageSize = pageRequest.getPageSize();
startIndex = (pageNo - 1) * pageSize;
endIndex = startIndex + (pageSize - 1);
}
for (int i = startIndex; i <= endIndex; i++) {
String stationKey = null;
try {
stationKey = validStationKeys.get(i);
} catch (Exception e) {
continue;
}
ChargingStationDto chargingStationDto = REDIS.getCacheObject(stationKey);
StationInfo stationInfo = new StationInfo();
//站点状态
stationInfo.setStationStatus(50);
//充电站Id
String stationId = stationKey.replace("station:", "");
stationInfo.setStationId(stationId);
Double lat = chargingStationDto.getLat();
if (lat == null) {
XhpcChargingStation stationExample = new XhpcChargingStation();
stationExample.setId(Long.parseLong(stationId));
Example<XhpcChargingStation> example = Example.of(stationExample);
XhpcChargingStation xhpcChargingStation =
xhpcChargingStationRepository.findOne(example).orElse(null);
if (xhpcChargingStation != null) {
//纬度
chargingStationDto.setLat(new BigDecimal(xhpcChargingStation.getLatitude()).setScale(6,
RoundingMode.HALF_UP).doubleValue());
//经度
chargingStationDto.setLng(new BigDecimal(xhpcChargingStation.getLongitude()).setScale(6,
RoundingMode.HALF_UP).doubleValue());
//站点类型
chargingStationDto.setType(xhpcChargingStation.getType());
//运营商id
String operatorIdEvcs = xhpcChargingStation.getOperatorIdEvcs();
chargingStationDto.setOperatorId(operatorIdEvcs == null ? "MA6DFCTD5"
: operatorIdEvcs.length() == 9 ? operatorId : operatorIdEvcs.substring(9, 19));
//详细地址
chargingStationDto.setAddress(xhpcChargingStation.getAddress());
//充电站省直辖区编码
chargingStationDto.setAreaCode(xhpcChargingStation.getAreaCode().toString());
//充电站名称
chargingStationDto.setName(xhpcChargingStation.getName());
//服务电话
chargingStationDto.setServiceTel(xhpcChargingStation.getServiceTel());
//车位数量
chargingStationDto.setParkNums(xhpcChargingStation.getParkNums());
REDIS.setCacheObject(stationKey, chargingStationDto);
}
}
stationInfo.setOperatorId(chargingStationDto.getOperatorId());
stationInfo.setEquipmentOwnerId(chargingStationDto.getOperatorId());
stationInfo.setStationName(chargingStationDto.getName());
stationInfo.setCountryCode("CN");
stationInfo.setAreaCode(chargingStationDto.getAreaCode());
stationInfo.setAddress(chargingStationDto.getAddress());
stationInfo.setServiceTel(chargingStationDto.getServiceTel());
stationInfo.setStationType(chargingStationDto.getType());
stationInfo.setParkNums(chargingStationDto.getParkNums());
stationInfo.setStationLng(chargingStationDto.getLng());
stationInfo.setStationLat(chargingStationDto.getLat());
stationInfo.setConstruction(chargingStationDto.getConstruction());
//获取该充电站的下的所有充电设备列表
Set<String> piles = chargingStationDto.getPiles();
List<EquipmentInfo> equipmentInfos = getEquipmentInfos(piles);
stationInfo.setEquipmentInfos(equipmentInfos);
stationInfos.add(stationInfo);
}
response.setStationInfos(stationInfos);
resp.setRet("0");
resp.setMsg("Query station info success");
resp.setData(JSONUtil.toJSONString(response));
}
response.setStationInfos(stationInfos);
CommonResponse resp = new CommonResponse();
resp.setRet("0");
resp.setMsg("Query station info success");
resp.setData(JSONUtil.toJSONString(response));
return resp;
}
// String operatorId = commonRequest.getOperatorId();
// final PageRequest pageRequest = commonRequest.transformDataType(PageRequest.class);
// List<StationInfo> stations = new ArrayList<>();
// int pageCount = 0;
// List<XhpcStationInternetBlacklist> xhpcStationInternetBlacklist =
// xhpcStationInternetBlacklistRepo.findByInternetUserId(operatorId);
// List<String> stationKeys = new ArrayList<>(REDIS.keys("station:*"));
// List<String> validStationKeys = new ArrayList<>();
// for (String stationKey : stationKeys) {
// boolean isValid = true;
// for (XhpcStationInternetBlacklist xhpcStationInternetBlack : xhpcStationInternetBlacklist) {
// if (stationKey.substring(8).equals(xhpcStationInternetBlack.getChargingStationId().toString())) {
// isValid = false;
// }
// }
// ChargingStationDto chargingStationDto = REDIS.getCacheObject(stationKey);
// if (chargingStationDto.getPiles() != null) {
// if (isValid) {
// validStationKeys.add(stationKey);
// }
// }
// }
// if (validStationKeys.size() > 0) {
// final Integer pageSize = pageRequest.getPageSize();
// int fullpage = validStationKeys.size() == 0 ? 0 : validStationKeys.size() / pageSize;
// final Integer pageNo = pageRequest.getPageNo();
// int plus = validStationKeys.size() % pageSize;
// assert (pageCount >= pageNo) : "too large pageNo";
// pageCount = fullpage + (plus == 0 ? 0 : 1);
// if (pageCount >= pageNo) {
// Integer lastOne = validStationKeys.size() < pageNo * pageSize ? validStationKeys.size() : pageNo * pageSize;
// for (int i = ((pageNo - 1) * pageSize); i < lastOne; i++) {
// final String stationKey = validStationKeys.get(i);
// ChargingStationDto chargingStationDto = REDIS.getCacheObject(stationKey);
// if (chargingStationDto.getPiles() != null) { //todo rm dup or-else?
// StationInfo station = new StationInfo();
// station.setStationStatus(50);
// final String stationId = stationKey.replace("station:", "");
// station.setStationId(stationId);
// Double lat = chargingStationDto.getLat();
// if (lat == null) {
// XhpcChargingStation stationExample = new XhpcChargingStation();
// stationExample.setId(Long.parseLong(stationId));
// Example<XhpcChargingStation> example = Example.of(stationExample);
// XhpcChargingStation xhpcChargingStation =
// xhpcChargingStationRepository.findOne(example).orElse(null);
// if (null != xhpcChargingStation) {
// chargingStationDto.setLat(new BigDecimal(xhpcChargingStation.getLatitude()).setScale(6,
// RoundingMode.HALF_UP).doubleValue());
// chargingStationDto.setLng(new BigDecimal(xhpcChargingStation.getLongitude()).setScale(6,
// RoundingMode.HALF_UP).doubleValue());
// chargingStationDto.setType(xhpcChargingStation.getType());
// chargingStationDto.setOperatorId(xhpcChargingStation.getOperatorIdEvcs() == null ? "MA6DFCTD5"
// : xhpcChargingStation.getOperatorIdEvcs());
// chargingStationDto.setAddress(xhpcChargingStation.getAddress());
// chargingStationDto.setAreaCode(xhpcChargingStation.getAreaCode().toString());
// chargingStationDto.setName(xhpcChargingStation.getName());
// chargingStationDto.setServiceTel(xhpcChargingStation.getServiceTel());
// chargingStationDto.setParkNums(xhpcChargingStation.getParkNums());
// REDIS.setCacheObject(stationKey, chargingStationDto);
// }
// }
// station.setAddress(chargingStationDto.getAddress());
// station.setStationLat(chargingStationDto.getLat());
// station.setStationLng(chargingStationDto.getLng());
// station.setStationType(chargingStationDto.getType());
// station.setStationStatus(50);
// station.setParkNums(chargingStationDto.getParkNums());
// station.setOperatorId(chargingStationDto.getOperatorId());
// station.setEquipmentOwnerId(chargingStationDto.getOperatorId());
// station.setStationName(chargingStationDto.getName());
// station.setCountryCode("CN");
// station.setServiceTel(chargingStationDto.getServiceTel());
// station.setAreaCode(chargingStationDto.getAreaCode());
// station.setConstruction(chargingStationDto.getConstruction());
// String[] fees = getFees(chargingStationDto.getRateModelId());
// station.setElectricityFee(fees[0]);
// station.setServiceFee(fees[1]);
// List<EquipmentInfo> piles = getEquipmentInfos(chargingStationDto.getPiles());
// station.setEquipmentInfos(piles);
// stations.add(station);
// }
// }
// }
// }
// PageStationsInfoResponse response = new PageStationsInfoResponse();
// response.setItemSize(stations.size());
// response.setPageNo(pageRequest.getPageNo());
// response.setPageCount(pageCount);
// response.setStationInfos(stations);
// CommonResponse resp = new CommonResponse();
// resp.setRet("0");
// resp.setMsg("Query station info success");
// resp.setData(JSONUtil.toJSONString(response));
// return resp;
// }
private String[] getFees(Long rateModelId) {
CacheRateModel cacheRateModel = REDIS.getCacheObject("rateModel:".concat(rateModelId.toString()));

View File

@ -16,6 +16,6 @@ public interface XhpcStationInternetBlacklistRepository extends JpaRepository<Xh
Optional<XhpcStationInternetBlacklist> findByChargingStationIdAndInternetUserId(Long stationId, Long internetUserId);
List<XhpcStationInternetBlacklist> findByInternetUserId(String substring);
List<XhpcStationInternetBlacklist> findByInternetUserId(Long internetUserId);
}

View File

@ -18,10 +18,10 @@ spring:
nacos:
discovery:
# 服务注册地址
server-addr: 172.31.183.135:8848
server-addr: 127.0.0.1:8848
config:
# 配置中心地址
server-addr: 172.31.183.135:8848
server-addr: 127.0.0.1:8848
# 配置文件格式
file-extension: yml
# 共享配置