大致完成query_station_info接口(未测试)

This commit is contained in:
wen 2021-11-02 16:59:16 +08:00
parent 771a4c8baf
commit 68387d099f

View File

@ -3,7 +3,6 @@ package com.xhpc.evcs.api;
import com.xhpc.common.api.dto.ChargingStationDto; import com.xhpc.common.api.dto.ChargingStationDto;
import com.xhpc.common.data.redis.CacheRateModel; import com.xhpc.common.data.redis.CacheRateModel;
import com.xhpc.evcs.domain.XhpcChargingPile; import com.xhpc.evcs.domain.XhpcChargingPile;
import com.xhpc.evcs.domain.XhpcChargingStation;
import com.xhpc.evcs.domain.XhpcStationInternetBlacklist; import com.xhpc.evcs.domain.XhpcStationInternetBlacklist;
import com.xhpc.evcs.dto.*; import com.xhpc.evcs.dto.*;
import com.xhpc.evcs.jpa.XhpcChargingPileRepository; import com.xhpc.evcs.jpa.XhpcChargingPileRepository;
@ -18,7 +17,6 @@ import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RestController; import org.springframework.web.bind.annotation.RestController;
import java.math.BigDecimal; import java.math.BigDecimal;
import java.math.RoundingMode;
import java.util.ArrayList; import java.util.ArrayList;
import java.util.List; import java.util.List;
import java.util.Map; import java.util.Map;
@ -41,13 +39,14 @@ public class QueryStationsInfoController {
@PostMapping("/v1/query_stations_info") @PostMapping("/v1/query_stations_info")
public CommonResponse queryStationsInfo(@RequestBody(required = false) CommonRequest<PageRequest> commonRequest) throws Exception { public CommonResponse queryStationsInfo(@RequestBody(required = false) CommonRequest<PageRequest> commonRequest) throws Exception {
assert false; //当前页数
PageRequest pageRequest1 = commonRequest.transformDataType(PageRequest.class);
Integer pageNo1 = pageRequest1.getPageNo();
PageStationsInfoResponse response = new PageStationsInfoResponse();
response.setPageNo(pageNo1);
//页码总数
String operatorId = commonRequest.getOperatorId(); String operatorId = commonRequest.getOperatorId();
final PageRequest pageRequest = commonRequest.transformDataType(PageRequest.class); List<XhpcStationInternetBlacklist> xhpcStationInternetBlacklist = xhpcStationInternetBlacklistRepo.findByInternetUserId(operatorId);
List<StationInfo> stations = new ArrayList<>();
int pageCount = 0;
List<XhpcStationInternetBlacklist> xhpcStationInternetBlacklist =
xhpcStationInternetBlacklistRepo.findByInternetUserId(operatorId);
List<String> stationKeys = new ArrayList<>(REDIS.keys("station:*")); List<String> stationKeys = new ArrayList<>(REDIS.keys("station:*"));
List<String> validStationKeys = new ArrayList<>(); List<String> validStationKeys = new ArrayList<>();
for (String stationKey : stationKeys) { for (String stationKey : stationKeys) {
@ -65,73 +64,44 @@ public class QueryStationsInfoController {
} }
} }
if (validStationKeys.size() > 0) { if (validStationKeys.size() > 0) {
final Integer pageSize = pageRequest.getPageSize(); Integer pageSize1 = pageRequest1.getPageSize();
int fullpage = validStationKeys.size() == 0 ? 0 : validStationKeys.size() / pageSize; int totalPage = validStationKeys.size() % pageSize1 == 0 ? validStationKeys.size() % pageSize1 : (validStationKeys.size() % pageSize1) + 1;
final Integer pageNo = pageRequest.getPageNo(); response.setPageCount(totalPage);
int plus = validStationKeys.size() % pageSize; }
assert (pageCount >= pageNo) : "too large pageNo"; //总记录条数
pageCount = fullpage + (plus == 0 ? 0 : 1); response.setItemSize(validStationKeys.size());
if (pageCount >= pageNo) {
Integer lastOne = validStationKeys.size() < pageNo * pageSize ? validStationKeys.size() : pageNo * pageSize; //充电站信息列表
for (int i = ((pageNo - 1) * pageSize); i < lastOne; i++) { List<StationInfo> stationInfos = new ArrayList<>();
final String stationKey = validStationKeys.get(i); //计算每页开始索引
Integer pageSize1 = pageRequest1.getPageSize();
int startIndex = (pageNo1 - 1) * pageSize1;
int endIndex = startIndex + (pageSize1 - 1);
for (int i = startIndex; i <= endIndex; i++) {
String stationKey = validStationKeys.get(i);
ChargingStationDto chargingStationDto = REDIS.getCacheObject(stationKey); ChargingStationDto chargingStationDto = REDIS.getCacheObject(stationKey);
if (chargingStationDto.getPiles() != null) { //todo rm dup or-else? StationInfo stationInfo = new StationInfo();
StationInfo station = new StationInfo(); stationInfo.setStationId(String.valueOf(chargingStationDto.getStationId()));
station.setStationStatus(50); stationInfo.setOperatorId(chargingStationDto.getOperatorId());
final String stationId = stationKey.replace("station:", ""); stationInfo.setEquipmentOwnerId(chargingStationDto.getOperatorId());
station.setStationId(stationId); stationInfo.setStationName(chargingStationDto.getName());
Double lat = chargingStationDto.getLat(); stationInfo.setCountryCode("CN");
if (lat == null) { stationInfo.setAreaCode(chargingStationDto.getAreaCode());
XhpcChargingStation stationExample = new XhpcChargingStation(); stationInfo.setAddress(chargingStationDto.getAddress());
stationExample.setId(Long.parseLong(stationId)); stationInfo.setServiceTel(chargingStationDto.getServiceTel());
Example<XhpcChargingStation> example = Example.of(stationExample); stationInfo.setStationType(chargingStationDto.getType());
XhpcChargingStation xhpcChargingStation = stationInfo.setStationStatus(50);
xhpcChargingStationRepository.findOne(example).orElse(null); stationInfo.setParkNums(chargingStationDto.getParkNums());
if (null != xhpcChargingStation) { stationInfo.setStationLng(chargingStationDto.getLng());
chargingStationDto.setLat(new BigDecimal(xhpcChargingStation.getLatitude()).setScale(6, stationInfo.setStationLat(chargingStationDto.getLat());
RoundingMode.HALF_UP).doubleValue()); stationInfo.setConstruction(chargingStationDto.getConstruction());
chargingStationDto.setLng(new BigDecimal(xhpcChargingStation.getLongitude()).setScale(6, //获取该充电站的下的所有充电设备列表
RoundingMode.HALF_UP).doubleValue()); Set<String> piles = chargingStationDto.getPiles();
chargingStationDto.setType(xhpcChargingStation.getType()); List<EquipmentInfo> equipmentInfos = getEquipmentInfos(piles);
chargingStationDto.setOperatorId(xhpcChargingStation.getOperatorIdEvcs() == null ? "MA6DFCTD5" stationInfo.setEquipmentInfos(equipmentInfos);
: xhpcChargingStation.getOperatorIdEvcs()); stationInfos.add(stationInfo);
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);
} }
} response.setStationInfos(stationInfos);
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(); CommonResponse resp = new CommonResponse();
resp.setRet("0"); resp.setRet("0");
resp.setMsg("Query station info success"); resp.setMsg("Query station info success");
@ -139,6 +109,104 @@ public class QueryStationsInfoController {
return resp; 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) { private String[] getFees(Long rateModelId) {
CacheRateModel cacheRateModel = REDIS.getCacheObject("rateModel:".concat(rateModelId.toString())); CacheRateModel cacheRateModel = REDIS.getCacheObject("rateModel:".concat(rateModelId.toString()));