mirror of
https://codeup.aliyun.com/67c68d4e484ca2f0a13ac3c1/ydc/jsowell-charger-web.git
synced 2026-04-20 02:55:04 +08:00
781 lines
32 KiB
Java
781 lines
32 KiB
Java
package com.jsowell.service;
|
||
|
||
import com.alibaba.fastjson2.JSON;
|
||
import com.github.pagehelper.PageHelper;
|
||
import com.github.pagehelper.PageInfo;
|
||
import com.google.common.collect.Lists;
|
||
import com.jsowell.common.constant.Constants;
|
||
import com.jsowell.common.core.domain.ykc.RealTimeMonitorData;
|
||
import com.jsowell.common.core.page.PageResponse;
|
||
import com.jsowell.common.enums.DelFlagEnum;
|
||
import com.jsowell.common.enums.SoftwareProtocolEnum;
|
||
import com.jsowell.common.enums.ykc.*;
|
||
import com.jsowell.common.exception.BusinessException;
|
||
import com.jsowell.common.util.*;
|
||
import com.jsowell.common.util.spring.SpringUtils;
|
||
import com.jsowell.pile.domain.*;
|
||
import com.jsowell.pile.dto.*;
|
||
import com.jsowell.pile.service.*;
|
||
import com.jsowell.pile.transaction.dto.PileTransactionDTO;
|
||
import com.jsowell.pile.transaction.service.TransactionService;
|
||
import com.jsowell.pile.vo.base.ConnectorInfoVO;
|
||
import com.jsowell.pile.vo.base.MerchantInfoVO;
|
||
import com.jsowell.pile.vo.base.PileInfoVO;
|
||
import com.jsowell.pile.vo.uniapp.customer.*;
|
||
import com.jsowell.pile.vo.web.PileDetailVO;
|
||
import com.jsowell.pile.vo.web.PileStationVO;
|
||
import org.apache.commons.collections4.CollectionUtils;
|
||
import org.apache.commons.lang3.RandomStringUtils;
|
||
import org.slf4j.Logger;
|
||
import org.slf4j.LoggerFactory;
|
||
import org.springframework.beans.factory.annotation.Autowired;
|
||
import org.springframework.scheduling.concurrent.ThreadPoolTaskExecutor;
|
||
import org.springframework.stereotype.Service;
|
||
|
||
import javax.annotation.Resource;
|
||
import java.math.BigDecimal;
|
||
import java.util.*;
|
||
import java.util.concurrent.CompletableFuture;
|
||
import java.util.concurrent.ExecutionException;
|
||
import java.util.stream.Collectors;
|
||
|
||
|
||
@Service
|
||
public class PileService {
|
||
private final Logger log = LoggerFactory.getLogger(PileService.class);
|
||
|
||
// 引入线程池
|
||
private ThreadPoolTaskExecutor executor = SpringUtils.getBean("threadPoolTaskExecutor");
|
||
|
||
@Autowired
|
||
private TransactionService pileTransactionService;
|
||
|
||
@Resource
|
||
private PileSnGenerateService snUtils;
|
||
|
||
@Autowired
|
||
private PileBasicInfoService pileBasicInfoService;
|
||
|
||
@Autowired
|
||
private PileConnectorInfoService pileConnectorInfoService;
|
||
|
||
@Autowired
|
||
private PileStationInfoService pileStationInfoService;
|
||
|
||
@Autowired
|
||
private PileMerchantInfoService pileMerchantInfoService;
|
||
|
||
@Autowired
|
||
private PileBillingTemplateService pileBillingTemplateService;
|
||
|
||
@Autowired
|
||
private PileMemberRelationService pileMemberRelationService;
|
||
|
||
@Autowired
|
||
private MemberBasicInfoService memberBasicInfoService;
|
||
|
||
@Autowired
|
||
private MemberService memberService;
|
||
|
||
@Autowired
|
||
private OrderBasicInfoService orderBasicInfoService;
|
||
|
||
@Autowired
|
||
private PileRemoteService pileRemoteService;
|
||
|
||
@Autowired
|
||
private PileReservationInfoService pileReservationInfoService;
|
||
|
||
@Autowired
|
||
private PersonalChargingRecordService personalChargingRecordService;
|
||
|
||
/**
|
||
* 查询设备信息
|
||
*
|
||
* @param pileConnectorCode 充电枪编号 880000000000000101
|
||
* @return 设备信息集合
|
||
*/
|
||
public PileConnectorDetailVO queryPileConnectorDetail(String pileConnectorCode) {
|
||
return pileBasicInfoService.queryPileConnectorDetail(pileConnectorCode);
|
||
}
|
||
|
||
/**
|
||
* 查询设备信息
|
||
*
|
||
* @param pileSn 充电枪编号 8800000000000001
|
||
* @param connectorCode 充电枪口号 01
|
||
* @return 设备信息集合
|
||
*/
|
||
public PileConnectorDetailVO queryPileConnectorDetail(String pileSn, String connectorCode) {
|
||
return queryPileConnectorDetail(pileSn + connectorCode);
|
||
}
|
||
|
||
public int batchCreatePile(BatchCreatePileDTO dto) {
|
||
String softwareProtocol = dto.getSoftwareProtocol();
|
||
if (StringUtils.isBlank(softwareProtocol)) {
|
||
throw new BusinessException(ReturnCodeEnum.valueOf("软件协议不能为空"));
|
||
}
|
||
// 批量生成sn号
|
||
List<String> snList = null;
|
||
if (StringUtils.equals(SoftwareProtocolEnum.YOU_DIAN.getValue(), softwareProtocol)) {
|
||
snList = snUtils.generateEBikeSN(dto.getNum());
|
||
} else {
|
||
snList = snUtils.generateEVPileSN(dto.getNum());
|
||
}
|
||
|
||
//
|
||
List<PileBasicInfo> basicInfoList = Lists.newArrayList();
|
||
List<PileConnectorInfo> connectorInfoList = Lists.newArrayList();
|
||
PileBasicInfo basicInfo = null;
|
||
PileConnectorInfo connectorInfo = null;
|
||
for (String sn : snList) {
|
||
// 组装pile_basic_info表数据
|
||
basicInfo = new PileBasicInfo();
|
||
basicInfo.setSn(sn); // sn号
|
||
basicInfo.setBusinessType(dto.getChargerPileType()); // 经营类型 1-运营桩;2-个人桩
|
||
if (StringUtils.equals(BusinessTypeEnum.INDIVIDUAL_PILE.getValue(), dto.getChargerPileType())) {
|
||
// 个人桩生成一个八位的密钥(字母加数字)
|
||
String secretKey = RandomStringUtils.randomAlphanumeric(8).toUpperCase(Locale.ROOT);
|
||
basicInfo.setSecretKey(secretKey);
|
||
}
|
||
basicInfo.setSoftwareProtocol(softwareProtocol); // 软件协议
|
||
basicInfo.setMerchantId(Long.valueOf(dto.getMerchantId())); // 运营商id
|
||
basicInfo.setStationId(Long.valueOf(dto.getStationId())); // 站点id
|
||
basicInfo.setModelId(Long.valueOf(dto.getModelId())); // 型号id
|
||
basicInfo.setProductionDate(dto.getProductionDate()); // 生产日期
|
||
basicInfo.setLicenceId(null); // TODO 证书编号
|
||
basicInfo.setSimId(null); // TODO sim卡
|
||
basicInfo.setRemark(dto.getRemark()); // 备注
|
||
basicInfo.setCreateBy(SecurityUtils.getUsername()); // 创建人
|
||
basicInfo.setDelFlag(DelFlagEnum.NORMAL.getValue()); // 删除标识
|
||
basicInfoList.add(basicInfo);
|
||
|
||
for (int i = 1; i < dto.getConnectorNum() + 1; i++) {
|
||
// 组装pile_connector_info表数据
|
||
connectorInfo = new PileConnectorInfo();
|
||
connectorInfo.setPileSn(sn); // sn号
|
||
String connectorCode = String.format("%1$02d", i);
|
||
connectorInfo.setPileConnectorCode(sn + connectorCode); // 枪口号
|
||
connectorInfo.setStatus(Constants.ZERO); //状态,默认 0-离网
|
||
connectorInfo.setCreateBy(SecurityUtils.getUsername()); // 创建人
|
||
connectorInfo.setDelFlag(DelFlagEnum.NORMAL.getValue()); // 删除标识
|
||
connectorInfoList.add(connectorInfo);
|
||
}
|
||
}
|
||
|
||
// 批量入库
|
||
PileTransactionDTO transactionDTO = PileTransactionDTO.builder()
|
||
.pileBasicInfoList(basicInfoList)
|
||
.pileConnectorInfoList(connectorInfoList)
|
||
.build();
|
||
return pileTransactionService.doCreatePileTransaction(transactionDTO);
|
||
}
|
||
|
||
/**
|
||
* 前端扫码跳转接口
|
||
*/
|
||
public AppletPileDetailVO getPileDetailByPileSnV2(String param) throws Exception {
|
||
AppletPileDetailVO vo = null;
|
||
log.info("查询充电枪口详情-getPileDetailByPileSn, param:{}", param);
|
||
if (StringUtils.isBlank(param)) {
|
||
throw new BusinessException(ReturnCodeEnum.CODE_PARAM_NOT_NULL_ERROR);
|
||
}
|
||
String pileSn = YKCUtils.getPileSn(param);
|
||
log.info("查询充电枪口详情-getPileDetailByPileSn, pileSn:{}", pileSn);
|
||
// 查询充电桩信息
|
||
PileInfoVO pileInfoVO = pileBasicInfoService.selectPileInfoBySn(pileSn);
|
||
log.info("查询充电枪口详情-pileInfoVO:{}", JSON.toJSONString(pileInfoVO));
|
||
if (pileInfoVO == null) {
|
||
return null;
|
||
}
|
||
|
||
// 判断桩是否在线
|
||
boolean onLineStatus = pileConnectorInfoService.checkPileOffLine(pileInfoVO.getPileSn());
|
||
log.info("查询充电枪口详情-判断桩是否在线:{}, onLineStatus:{}", pileInfoVO.getPileSn(), onLineStatus);
|
||
if (onLineStatus) {
|
||
// true为离线
|
||
throw new BusinessException(ReturnCodeEnum.CODE_PILE_CONNECTOR_STATUS_OFF_LINE);
|
||
}
|
||
|
||
// 查询站点信息
|
||
PileStationVO stationInfo = pileStationInfoService.getStationInfo(pileInfoVO.getStationId());
|
||
log.info("查询充电枪口详情-getStationInfo:{}, onLineStatus:{}", pileInfoVO.getStationId(), JSON.toJSONString(stationInfo));
|
||
if (stationInfo == null || StringUtils.equals(stationInfo.getOpenFlag(), Constants.ZERO)) {
|
||
throw new BusinessException(ReturnCodeEnum.CODE_STATION_IS_NOT_OPEN);
|
||
}
|
||
|
||
List<ConnectorInfoVO> connectorInfoList = pileConnectorInfoService.selectConnectorInfoList(pileSn);
|
||
List<BillingPriceVO> billingPriceVOS = pileBillingTemplateService.queryBillingPrice(pileInfoVO.getStationId());
|
||
MerchantInfoVO merchantInfoVO = pileMerchantInfoService.getMerchantInfoVO(pileInfoVO.getMerchantId());
|
||
|
||
vo = AppletPileDetailVO.builder()
|
||
.pileInfo(pileInfoVO)
|
||
.connectorInfoList(connectorInfoList)
|
||
.merchantInfo(merchantInfoVO)
|
||
.stationInfo(stationInfo)
|
||
.billingPriceList(billingPriceVOS)
|
||
.build();
|
||
return vo;
|
||
}
|
||
|
||
public AppletPileDetailVO getPileDetailByPileSn(String param) throws Exception {
|
||
AppletPileDetailVO vo = null;
|
||
log.info("查询充电枪口详情-getPileDetailByPileSn, param:{}", param);
|
||
if (StringUtils.isBlank(param)) {
|
||
throw new BusinessException(ReturnCodeEnum.CODE_PARAM_NOT_NULL_ERROR);
|
||
}
|
||
String pileSn = YKCUtils.getPileSn(param);
|
||
log.info("查询充电枪口详情-getPileDetailByPileSn, pileSn:{}", pileSn);
|
||
// 查询充电桩信息
|
||
PileInfoVO pileInfoVO = pileBasicInfoService.selectPileInfoBySn(pileSn);
|
||
log.info("查询充电枪口详情-pileInfoVO:{}", JSON.toJSONString(pileInfoVO));
|
||
if (pileInfoVO == null) {
|
||
return null;
|
||
}
|
||
|
||
// 判断桩是否在线
|
||
boolean onLineStatus = pileConnectorInfoService.checkPileOffLine(pileInfoVO.getPileSn());
|
||
String activeProfile = SpringUtils.getActiveProfile();
|
||
log.info("查询充电枪口详情-判断桩是否在线:{}, onLineStatus:{}, activeProfile:{}", pileInfoVO.getPileSn(), onLineStatus, activeProfile);
|
||
if ("prd".equals(activeProfile)) {
|
||
if (onLineStatus) {
|
||
// true为离线
|
||
throw new BusinessException(ReturnCodeEnum.CODE_PILE_CONNECTOR_STATUS_OFF_LINE);
|
||
}
|
||
}
|
||
|
||
// 查询站点信息
|
||
PileStationVO stationInfo = pileStationInfoService.getStationInfo(pileInfoVO.getStationId());
|
||
log.info("查询充电枪口详情-getStationInfo:{}, onLineStatus:{}", pileInfoVO.getStationId(), JSON.toJSONString(stationInfo));
|
||
if (stationInfo == null || StringUtils.equals(stationInfo.getOpenFlag(), Constants.ZERO)) {
|
||
throw new BusinessException(ReturnCodeEnum.CODE_STATION_IS_NOT_OPEN);
|
||
}
|
||
|
||
// 查询充电桩下枪口信息
|
||
CompletableFuture<List<ConnectorInfoVO>> connectorInfoListFuture = CompletableFuture.supplyAsync(() -> pileConnectorInfoService.selectConnectorInfoList(pileSn), executor);
|
||
// log.info("查询充电枪口详情-supplyAsync-selectConnectorInfoList:{}", connectorInfoListFuture);
|
||
|
||
// 查计费模板信息
|
||
CompletableFuture<List<BillingPriceVO>> billingPriceFuture = CompletableFuture.supplyAsync(() -> pileBillingTemplateService.queryBillingPrice(pileInfoVO.getStationId()), executor);
|
||
// log.info("查询充电枪口详情-supplyAsync-queryBillingPrice:{}", billingPriceFuture);
|
||
|
||
// 查询运营商信息
|
||
CompletableFuture<MerchantInfoVO> merchantInfoVOFuture = CompletableFuture.supplyAsync(() -> pileMerchantInfoService.getMerchantInfoVO(pileInfoVO.getMerchantId()), executor);
|
||
// log.info("查询充电枪口详情-supplyAsync-getMerchantInfoVO:{}", merchantInfoVOFuture);
|
||
|
||
CompletableFuture all = FutureUtil.allOf(connectorInfoListFuture, merchantInfoVOFuture, billingPriceFuture);
|
||
// .join()和.get()都会阻塞并获取线程的执行情况
|
||
// .join()会抛出未经检查的异常,不会强制开发者处理异常 .get()会抛出检查异常,需要开发者处理
|
||
all.join();
|
||
all.get();
|
||
List<ConnectorInfoVO> connectorInfoList = connectorInfoListFuture.get();
|
||
// List<ConnectorInfoVO> connectorInfoList = pileConnectorInfoService.selectConnectorInfoList(pileSn);
|
||
log.info("查询充电枪口详情-connectorInfoList:{}", JSON.toJSONString(connectorInfoList));
|
||
// PileStationVO pileStationVO = pileStationVOFuture.get();
|
||
MerchantInfoVO merchantInfoVO = merchantInfoVOFuture.get();
|
||
// List<BillingPriceVO> billingPriceVOS= pileBillingTemplateService.queryBillingPrice(pileInfoVO.getStationId());
|
||
log.info("查询充电枪口详情-merchantInfoVO:{}", JSON.toJSONString(merchantInfoVO));
|
||
List<BillingPriceVO> billingPriceVOS = billingPriceFuture.get();
|
||
// MerchantInfoVO merchantInfoVO = pileMerchantInfoService.getMerchantInfoVO(pileInfoVO.getMerchantId());
|
||
log.info("查询充电枪口详情-billingPriceVOS:{}", JSON.toJSONString(billingPriceVOS));
|
||
|
||
vo = AppletPileDetailVO.builder()
|
||
.pileInfo(pileInfoVO)
|
||
.connectorInfoList(connectorInfoList)
|
||
.merchantInfo(merchantInfoVO)
|
||
.stationInfo(stationInfo)
|
||
.billingPriceList(billingPriceVOS)
|
||
.build();
|
||
return vo;
|
||
}
|
||
|
||
/**
|
||
* 获取枪口详情信息
|
||
* 仅小程序扫码调用
|
||
* @param pileConnectorCode
|
||
* @return
|
||
* @throws ExecutionException
|
||
* @throws InterruptedException
|
||
*/
|
||
public AppletPileDetailVO getConnectorDetail(String pileConnectorCode) throws Exception {
|
||
log.info("查询充电枪口详情, pileConnectorCode:{}", pileConnectorCode);
|
||
PileConnectorDetailVO pileConnectorDetailVO = queryPileConnectorDetail(pileConnectorCode);
|
||
log.info("查询充电枪口详情, pileConnectorDetailVO:{}", JSON.toJSONString(pileConnectorDetailVO));
|
||
if (pileConnectorDetailVO == null) {
|
||
return null;
|
||
}
|
||
// 不是电单车的桩, 需要判断插枪状态
|
||
if (!StringUtils.equals(Constants.THREE, pileConnectorDetailVO.getChargePortType())) {
|
||
// 枪口状态不为2:占用(未充电)
|
||
if (!StringUtils.equals(PileConnectorDataBaseStatusEnum.OCCUPIED_NOT_CHARGED.getValue(), pileConnectorDetailVO.getConnectorStatus())) {
|
||
throw new BusinessException(ReturnCodeEnum.CODE_PILE_CONNECTOR_STATUS_ERROR);
|
||
}
|
||
}
|
||
String pileSn = pileConnectorDetailVO.getPileSn();
|
||
AppletPileDetailVO resultVO = getPileDetailByPileSn(pileSn);
|
||
log.info("查询充电枪口详情getConnectorDetail, pileSn:{}, pileConnectorDetailVO:{}, resultVO:{}", pileSn, JSON.toJSONString(pileConnectorDetailVO), JSON.toJSONString(resultVO));
|
||
List<ConnectorInfoVO> connectorInfoList = resultVO.getConnectorInfoList();
|
||
if (connectorInfoList.size() > 1 && !StringUtils.equals(pileConnectorDetailVO.getChargePortType(), "3")) {
|
||
List<ConnectorInfoVO> list = Lists.newArrayList();
|
||
// 枪口大于1个,此充电桩非单枪设备,根据参数展示对应枪口的信息
|
||
for (ConnectorInfoVO connectorInfoVO : connectorInfoList) {
|
||
if (StringUtils.equals(pileConnectorCode, pileSn + connectorInfoVO.getConnectorCode())) {
|
||
list.add(connectorInfoVO);
|
||
}
|
||
}
|
||
resultVO.setConnectorInfoList(list);
|
||
} else {
|
||
resultVO.setConnectorInfoList(connectorInfoList);
|
||
}
|
||
return resultVO;
|
||
}
|
||
|
||
public AppletPileDetailVO getConnectorDetailV2(String pileConnectorCode) throws Exception {
|
||
log.info("查询充电枪口详情, pileConnectorCode:{}", pileConnectorCode);
|
||
PileConnectorDetailVO pileConnectorDetailVO = queryPileConnectorDetail(pileConnectorCode);
|
||
log.info("查询充电枪口详情, pileConnectorDetailVO:{}", JSON.toJSONString(pileConnectorDetailVO));
|
||
if (pileConnectorDetailVO == null) {
|
||
return null;
|
||
}
|
||
// 不是电单车的桩, 需要判断插枪状态
|
||
if (!StringUtils.equals(Constants.THREE, pileConnectorDetailVO.getChargePortType())) {
|
||
// 枪口状态不为2:占用(未充电)
|
||
if (!StringUtils.equals(PileConnectorDataBaseStatusEnum.OCCUPIED_NOT_CHARGED.getValue(), pileConnectorDetailVO.getConnectorStatus())) {
|
||
throw new BusinessException(ReturnCodeEnum.CODE_PILE_CONNECTOR_STATUS_ERROR);
|
||
}
|
||
}
|
||
String pileSn = pileConnectorDetailVO.getPileSn();
|
||
AppletPileDetailVO resultVO = getPileDetailByPileSn(pileSn);
|
||
log.info("查询充电枪口详情getConnectorDetail, pileSn:{}, pileConnectorDetailVO:{}, resultVO:{}", pileSn, JSON.toJSONString(pileConnectorDetailVO), JSON.toJSONString(resultVO));
|
||
List<ConnectorInfoVO> connectorInfoList = resultVO.getConnectorInfoList();
|
||
if (connectorInfoList.size() > 1 && !StringUtils.equals(pileConnectorDetailVO.getChargePortType(), "3")) {
|
||
List<ConnectorInfoVO> list = Lists.newArrayList();
|
||
// 枪口大于1个,此充电桩非单枪设备,根据参数展示对应枪口的信息
|
||
for (ConnectorInfoVO connectorInfoVO : connectorInfoList) {
|
||
if (StringUtils.equals(pileConnectorCode, pileSn + connectorInfoVO.getConnectorCode())) {
|
||
list.add(connectorInfoVO);
|
||
}
|
||
}
|
||
resultVO.setConnectorInfoList(list);
|
||
} else {
|
||
resultVO.setConnectorInfoList(connectorInfoList);
|
||
}
|
||
return resultVO;
|
||
}
|
||
|
||
public PileInfoVO selectPileInfoBySn(String pileSn) {
|
||
return pileBasicInfoService.selectPileInfoBySn(pileSn);
|
||
}
|
||
|
||
/**
|
||
* 用户绑定个人桩
|
||
*
|
||
* @param dto
|
||
* @return
|
||
*/
|
||
public int pileMemberBinding(PileMemberBindingDTO dto){
|
||
// 校验短信验证码
|
||
MemberRegisterAndLoginDTO registerAndLoginDTO = MemberRegisterAndLoginDTO.builder()
|
||
.mobileNumber(dto.getPhoneNumber())
|
||
.verificationCode(dto.getVerificationCode())
|
||
.build();
|
||
memberService.checkVerificationCode(registerAndLoginDTO);
|
||
|
||
// 判断桩是否为个人桩
|
||
PileBasicInfo pileBasicInfo = pileBasicInfoService.selectPileBasicInfoBySN(dto.getPileSn());
|
||
if (pileBasicInfo == null) {
|
||
// 未查到则说明没有此桩
|
||
throw new BusinessException(ReturnCodeEnum.CODE_PILE_NOT_INFO);
|
||
}
|
||
String businessType = pileBasicInfo.getBusinessType();
|
||
if (!StringUtils.equals(BusinessTypeEnum.INDIVIDUAL_PILE.getValue(), businessType)){
|
||
// 不一致则此桩不是个人桩
|
||
throw new BusinessException(ReturnCodeEnum.CODE_THIS_PILE_NOT_PERSONAL_PILE);
|
||
}
|
||
|
||
// 初始化个人桩预约信息
|
||
pileReservationInfoService.initPersonalPileReservation(dto.getPileSn(), null);
|
||
|
||
// 检查桩是否已经被绑定
|
||
PileMemberRelation pileMemberRelation = new PileMemberRelation();
|
||
pileMemberRelation.setPileSn(dto.getPileSn());
|
||
List<PileMemberRelation> list = pileMemberRelationService.selectPileMemberRelationList(pileMemberRelation);
|
||
if (CollectionUtils.isNotEmpty(list)){
|
||
// 说明已经被绑定过,抛出异常
|
||
throw new BusinessException(ReturnCodeEnum.CODE_PILE_HAS_BEEN_BINDING_ERROR);
|
||
}
|
||
// 如果没被绑定,先校验桩密钥是否一致
|
||
if (!StringUtils.equals(pileBasicInfo.getSecretKey(), dto.getSecretKey())) {
|
||
// 错误的密钥信息
|
||
throw new BusinessException(ReturnCodeEnum.CODE_SECRET_KEY_ERROR);
|
||
}
|
||
// 密钥正确,且桩未被绑定,则此用户为管理员
|
||
pileMemberRelation.setMemberId(dto.getMemberId());
|
||
pileMemberRelation.setType(Constants.ONE); // 1-管理员
|
||
return pileMemberRelationService.insertPileMemberRelation(pileMemberRelation);
|
||
}
|
||
|
||
/**
|
||
* 用户绑定个人桩(蓝牙)
|
||
*/
|
||
public int pileMemberBindingForBT(PileMemberBindingDTO dto) {
|
||
// 判断桩是否为个人桩
|
||
PileBasicInfo pileBasicInfo = pileBasicInfoService.selectPileBasicInfoBySN(dto.getPileSn());
|
||
if (pileBasicInfo == null) {
|
||
// 未查到则说明没有此桩
|
||
throw new BusinessException(ReturnCodeEnum.CODE_PILE_NOT_INFO);
|
||
}
|
||
String businessType = pileBasicInfo.getBusinessType();
|
||
if (!StringUtils.equals(BusinessTypeEnum.INDIVIDUAL_PILE.getValue(), businessType)){
|
||
// 不一致则此桩不是个人桩
|
||
throw new BusinessException(ReturnCodeEnum.CODE_THIS_PILE_NOT_PERSONAL_PILE);
|
||
}
|
||
// 初始化个人桩预约信息
|
||
pileReservationInfoService.initPersonalPileReservation(dto.getPileSn(), null);
|
||
// 检查桩是否已经被绑定
|
||
PileMemberRelation pileMemberRelation = new PileMemberRelation();
|
||
pileMemberRelation.setPileSn(dto.getPileSn());
|
||
List<PileMemberRelation> list = pileMemberRelationService.selectPileMemberRelationList(pileMemberRelation);
|
||
if (CollectionUtils.isNotEmpty(list)){
|
||
// 说明已经被绑定过,抛出异常
|
||
throw new BusinessException(ReturnCodeEnum.CODE_PILE_HAS_BEEN_BINDING_ERROR);
|
||
}
|
||
// 保存绑定关系
|
||
pileMemberRelation.setMemberId(dto.getMemberId());
|
||
pileMemberRelation.setType(Constants.ONE); // 1-管理员
|
||
pileMemberRelation.setDeviceName(dto.getDeviceName());
|
||
pileMemberRelation.setDeviceId(dto.getDeviceId());
|
||
return pileMemberRelationService.insertPileMemberRelation(pileMemberRelation);
|
||
}
|
||
/**
|
||
* 用户解绑个人桩
|
||
* @param dto
|
||
*
|
||
*/
|
||
public int userUnbindPile(PileMemberBindingDTO dto) {
|
||
// 查询该用户的身份,是管理员还是普通用户
|
||
PileMemberRelation relation = new PileMemberRelation();
|
||
relation.setPileSn(dto.getPileSn());
|
||
relation.setMemberId(dto.getMemberId());
|
||
PileMemberRelation pileMemberRelation = pileMemberRelationService.selectPileMemberRelation(relation);
|
||
if (pileMemberRelation == null) {
|
||
throw new BusinessException(ReturnCodeEnum.CODE_THIS_PILE_INFO_IS_NULL);
|
||
}
|
||
String type = pileMemberRelation.getType();
|
||
List<Integer> idList = new ArrayList<>();
|
||
// 管理员解绑则会将此桩下面所有用户解绑
|
||
if (StringUtils.equals(Constants.ONE, type)) {
|
||
List<PileMemberRelation> pileMemberRelations = pileMemberRelationService.selectPileMemberRelationByPileSn(dto.getPileSn());
|
||
idList = pileMemberRelations.stream().map(PileMemberRelation::getId).collect(Collectors.toList());
|
||
|
||
// 管理员解绑,需要删除该桩的预约信息
|
||
pileReservationInfoService.deleteReservationByPileSn(dto.getPileSn());
|
||
}else {
|
||
idList.add(pileMemberRelation.getId());
|
||
}
|
||
return pileMemberRelationService.deleteRelationByIds(idList);
|
||
}
|
||
|
||
/**
|
||
* 个人桩管理员下发给其他用户使用
|
||
*
|
||
* @param dto
|
||
*/
|
||
public void adminIssuePile(PileMemberBindingDTO dto) {
|
||
// 通过前端传的手机号查询是否有此用户
|
||
MemberBasicInfo memberBasicInfo = memberBasicInfoService.selectInfoByMobileNumber(dto.getPhoneNumber(), dto.getFirstLevelMerchantId());
|
||
if (memberBasicInfo == null) {
|
||
// 为空说明此用户未注册平台账号
|
||
throw new BusinessException(ReturnCodeEnum.CODE_USER_IS_NOT_REGISTER);
|
||
}
|
||
|
||
List<PileMemberRelation> relationList = pileMemberRelationService.selectPileMemberRelationByPileSn(dto.getPileSn());
|
||
if (CollectionUtils.isEmpty(relationList)) {
|
||
// 充电桩没有绑定任何人
|
||
}
|
||
|
||
List<String> adminList = relationList.stream()
|
||
.filter(x -> x.getType().equals(Constants.ONE)) // 1-管理员用户
|
||
.map(PileMemberRelation::getMemberId)
|
||
.collect(Collectors.toList());
|
||
if (CollectionUtils.isEmpty(adminList)) {
|
||
// 没有管理员
|
||
}
|
||
|
||
// 校验身份
|
||
if (adminList.contains(dto.getMemberId())) {
|
||
// 如果不为空,说明被分享的用户是管理员,抛出异常
|
||
throw new BusinessException(ReturnCodeEnum.CODE_AUTHENTICATION_ERROR);
|
||
}
|
||
|
||
List<String> userList = relationList.stream()
|
||
.filter(x -> !x.getType().equals(Constants.TWO)) // 2-普通用户
|
||
.map(PileMemberRelation::getMemberId)
|
||
.collect(Collectors.toList());
|
||
|
||
if (userList.contains(memberBasicInfo.getMemberId())) {
|
||
// 不为空说明已绑定
|
||
throw new BusinessException(ReturnCodeEnum.CODE_USER_HAS_BEEN_THIS_PILE);
|
||
} else {
|
||
// 进行绑定,此用户为普通用户
|
||
PileMemberRelation info = new PileMemberRelation();
|
||
info.setPileSn(dto.getPileSn());
|
||
info.setMemberId(memberBasicInfo.getMemberId());
|
||
info.setType(Constants.TWO);
|
||
pileMemberRelationService.insertPileMemberRelation(info);
|
||
}
|
||
}
|
||
|
||
/**
|
||
* 获取枪口实时数据
|
||
* @param dto
|
||
* @return
|
||
*/
|
||
public PersonPileRealTimeVO getConnectorRealTimeInfo(QueryPersonPileDTO dto) {
|
||
// 根据memberId查出该用户 正在充电、个人桩启动(白名单支付方式)的订单号
|
||
OrderBasicInfo orderBasicInfo = OrderBasicInfo.builder()
|
||
// .memberId(dto.getMemberId())
|
||
.orderStatus(OrderStatusEnum.IN_THE_CHARGING.getValue())
|
||
.pileConnectorCode(dto.getPileConnectorCode())
|
||
.payMode(OrderPayModeEnum.PAYMENT_OF_WHITELIST.getValue()) // 3- 白名单支付
|
||
.build();
|
||
OrderBasicInfo basicInfo = orderBasicInfoService.getOrderBasicInfo(orderBasicInfo);
|
||
if (basicInfo == null){
|
||
throw new BusinessException(ReturnCodeEnum.CODE_NO_CHARGING_ORDER_ERROR);
|
||
}
|
||
// 根据订单号从redis中获取实时数据信息(默认时间倒叙排列,所以取第一条)
|
||
List<RealTimeMonitorData> chargingRealTimeData = orderBasicInfoService.getChargingRealTimeData(basicInfo.getTransactionCode());
|
||
if (CollectionUtils.isEmpty(chargingRealTimeData)) {
|
||
throw new BusinessException(ReturnCodeEnum.CODE_NO_REAL_TIME_INFO);
|
||
}
|
||
RealTimeMonitorData realTimeMonitorData = chargingRealTimeData.get(0);
|
||
// 将需要的数据set
|
||
PersonPileRealTimeVO vo = PersonPileRealTimeVO.builder()
|
||
.chargingDegree(realTimeMonitorData.getChargingDegree())
|
||
.chargingTime(realTimeMonitorData.getSumChargingTime())
|
||
.startTime(DateUtils.formatDateTime(orderBasicInfo.getChargeStartTime()))
|
||
.instantCurrent(realTimeMonitorData.getOutputCurrent())
|
||
.instantVoltage(realTimeMonitorData.getOutputVoltage())
|
||
.instantPower(realTimeMonitorData.getOutputPower())
|
||
.status(orderBasicInfo.getOrderStatus())
|
||
.soc(realTimeMonitorData.getSOC())
|
||
.build();
|
||
return vo;
|
||
}
|
||
|
||
/**
|
||
* 获取某枪口某段时间内累计信息
|
||
* @param dto
|
||
* @return
|
||
*/
|
||
public PersonPileConnectorSumInfoVO getAccumulativeInfo(QueryPersonPileDTO dto) {
|
||
List<PersonPileConnectorSumInfoVO> accumulativeInfo = orderBasicInfoService.getAccumulativeInfo(dto);
|
||
if (CollectionUtils.isEmpty(accumulativeInfo)) {
|
||
throw new BusinessException(ReturnCodeEnum.CODE_QUERY_ORDER_INFO_IS_NULL);
|
||
}
|
||
Map<String, String> sumInfo = getSumInfo(accumulativeInfo);
|
||
// set 对象
|
||
PersonPileConnectorSumInfoVO vo = new PersonPileConnectorSumInfoVO();
|
||
vo.setStartTime(dto.getStartTime());
|
||
vo.setEndTime(dto.getEndTime());
|
||
vo.setMemberId(dto.getMemberId());
|
||
vo.setSumChargingDegree(sumInfo.get("sumUsedElectricity"));
|
||
vo.setSumChargingTime(sumInfo.get("sumChargingTime"));
|
||
return vo;
|
||
}
|
||
|
||
/**
|
||
* 获取充电记录(默认30天)
|
||
*
|
||
* @param dto
|
||
*/
|
||
public PageResponse getChargingRecord(QueryPersonPileDTO dto) {
|
||
int pageNum = dto.getPageNum() == 0 ? 1 : dto.getPageNum();
|
||
int pageSize = dto.getPageSize() == 0 ? 10 : dto.getPageSize();
|
||
// 获取三十天前的数据
|
||
Date date = DateUtils.addMonths(new Date(), -1);
|
||
String dateStr = DateUtils.parseDateToStr(DateUtils.YYYY_MM_DD_HH_MM_SS, date);
|
||
dto.setStartTime(dateStr);
|
||
dto.setEndTime(DateUtils.parseDateToStr(DateUtils.YYYY_MM_DD_HH_MM_SS, new Date()));
|
||
|
||
// 分页查询
|
||
PageHelper.startPage(pageNum, pageSize);
|
||
List<PersonPileConnectorSumInfoVO> accumulativeInfo = orderBasicInfoService.getAccumulativeInfo(dto);
|
||
if (CollectionUtils.isEmpty(accumulativeInfo)) {
|
||
throw new BusinessException(ReturnCodeEnum.CODE_QUERY_ORDER_INFO_IS_NULL);
|
||
}
|
||
PageInfo<PersonPileConnectorSumInfoVO> pageInfo = new PageInfo<>(accumulativeInfo);
|
||
for (PersonPileConnectorSumInfoVO personPileConnectorSumInfoVO : pageInfo.getList()) {
|
||
if (StringUtils.isNotBlank(personPileConnectorSumInfoVO.getChargeStartTime()) && StringUtils.isNotBlank(personPileConnectorSumInfoVO.getChargeEndTime())){
|
||
String datePoor = DateUtils.getDatePoor(DateUtils.parseDate(personPileConnectorSumInfoVO.getChargeEndTime()),
|
||
DateUtils.parseDate(personPileConnectorSumInfoVO.getChargeStartTime()));
|
||
personPileConnectorSumInfoVO.setSumChargingTime(datePoor);
|
||
}
|
||
}
|
||
return PageResponse.builder()
|
||
.pageNum(pageInfo.getPageNum())
|
||
.pageSize(pageInfo.getPageSize())
|
||
.list(pageInfo.getList())
|
||
.pages(pageInfo.getPages())
|
||
.total(pageInfo.getTotal())
|
||
.build();
|
||
}
|
||
|
||
public PageResponse getChargingRecordV2(QueryPersonPileDTO dto) {
|
||
int pageNum = dto.getPageNum() == 0 ? 1 : dto.getPageNum();
|
||
int pageSize = dto.getPageSize() == 0 ? 10 : dto.getPageSize();
|
||
// 获取三十天前的数据
|
||
Date date = DateUtils.addMonths(new Date(), -1);
|
||
String dateStr = DateUtils.parseDateToStr(DateUtils.YYYY_MM_DD_HH_MM_SS, date);
|
||
dto.setStartTime(dateStr);
|
||
dto.setEndTime(DateUtils.parseDateToStr(DateUtils.YYYY_MM_DD_HH_MM_SS, new Date()));
|
||
|
||
// 分页查询
|
||
PageHelper.startPage(pageNum, pageSize);
|
||
List<PersonalChargingRecord> personalChargingRecordList = personalChargingRecordService.getPersonalChargingRecord(dto);
|
||
if (CollectionUtils.isEmpty(personalChargingRecordList)) {
|
||
throw new BusinessException(ReturnCodeEnum.CODE_QUERY_ORDER_INFO_IS_NULL);
|
||
}
|
||
PageInfo<PersonalChargingRecord> pageInfo = new PageInfo<>(personalChargingRecordList);
|
||
List<PersonPileConnectorSumInfoVO> list = Lists.newArrayList();
|
||
PersonPileConnectorSumInfoVO vo;
|
||
for (PersonalChargingRecord personalChargingRecord : pageInfo.getList()) {
|
||
vo = new PersonPileConnectorSumInfoVO();
|
||
vo.setSourceType(personalChargingRecord.getSourceType());
|
||
if (personalChargingRecord.getChargeStartTime() != null && personalChargingRecord.getChargeEndTime() != null){
|
||
vo.setChargeStartTime(DateUtils.formatDateTime(personalChargingRecord.getChargeStartTime()));
|
||
vo.setChargeEndTime(DateUtils.formatDateTime(personalChargingRecord.getChargeEndTime()));
|
||
String datePoor = DateUtils.getDatePoor(personalChargingRecord.getChargeEndTime(), personalChargingRecord.getChargeStartTime());
|
||
vo.setSumChargingTime(datePoor);
|
||
}
|
||
vo.setMemberId(personalChargingRecord.getMemberId());
|
||
vo.setSumChargingDegree(personalChargingRecord.getTotalUsedElectricity().toString());
|
||
vo.setStopReasonMsg(personalChargingRecord.getReason());
|
||
list.add(vo);
|
||
}
|
||
return PageResponse.builder()
|
||
.pageNum(pageInfo.getPageNum())
|
||
.pageSize(pageInfo.getPageSize())
|
||
.list(list)
|
||
.pages(pageInfo.getPages())
|
||
.total(pageInfo.getTotal())
|
||
.build();
|
||
}
|
||
|
||
/**
|
||
* 获取总充电时长、总充电量
|
||
*
|
||
* @param accumulativeInfo
|
||
* @return
|
||
*/
|
||
private Map<String, String> getSumInfo(List<PersonPileConnectorSumInfoVO> accumulativeInfo){
|
||
Map<String, String> resultMap = new HashMap<>();
|
||
BigDecimal sumChargingTime = BigDecimal.ZERO;
|
||
BigDecimal sumUsedElectricity = BigDecimal.ZERO;
|
||
// 将返回的结果进行for循环,将总充电量、总充电时长进行累加(充电时长需要通过 充电开始时间 和 充电结束时间 进行计算)
|
||
for (PersonPileConnectorSumInfoVO personPileConnectorSumInfoVO : accumulativeInfo) {
|
||
if (StringUtils.isNotBlank(personPileConnectorSumInfoVO.getChargeStartTime()) && StringUtils.isNotBlank(personPileConnectorSumInfoVO.getChargeEndTime())){
|
||
long longChargingTime = DateUtils.intervalTime(personPileConnectorSumInfoVO.getChargeStartTime(), personPileConnectorSumInfoVO.getChargeEndTime());
|
||
BigDecimal chargingTime = new BigDecimal(String.valueOf(longChargingTime));
|
||
sumChargingTime = sumChargingTime.add(chargingTime);
|
||
}
|
||
BigDecimal chargingDegree = StringUtils.isBlank(personPileConnectorSumInfoVO.getSumChargingDegree())
|
||
? BigDecimal.ZERO
|
||
: new BigDecimal(personPileConnectorSumInfoVO.getSumChargingDegree());
|
||
sumUsedElectricity = sumUsedElectricity.add(chargingDegree);
|
||
}
|
||
resultMap.put("sumChargingTime", String.valueOf(sumChargingTime));
|
||
resultMap.put("sumUsedElectricity", String.valueOf(sumUsedElectricity));
|
||
return resultMap;
|
||
}
|
||
|
||
|
||
/**
|
||
* 后管调用,给站点下所有桩下发二维码
|
||
* @param dto
|
||
* @return
|
||
*/
|
||
public int updateStationQRCodePrefix(QueryStationDTO dto) {
|
||
String stationId = dto.getStationId();
|
||
// 通过站点id查询数据
|
||
PileStationInfo pileStationInfo = pileStationInfoService.selectPileStationInfoById(Long.parseLong(stationId));
|
||
if (pileStationInfo == null) {
|
||
return 0;
|
||
}
|
||
// 修改站点列表的 qrcodePrefix 字段
|
||
pileStationInfo.setQrcodePrefix(dto.getQrcodePrefix());
|
||
int i = pileStationInfoService.updatePileStationInfo(pileStationInfo);
|
||
if (i == 0) {
|
||
return 0;
|
||
}
|
||
// 查出这个站点下的所有桩
|
||
List<PileDetailVO> pileDetailVOS = pileBasicInfoService.selectPileListByStationIds(Lists.newArrayList(Long.parseLong(stationId)));
|
||
List<String> pileSnList = pileDetailVOS.stream()
|
||
.filter(x -> StringUtils.equals(x.getStatus(), PileStatusEnum.ON_LINE.getValue()))
|
||
.map(PileDetailVO::getPileSn)
|
||
.collect(Collectors.toList());
|
||
// IssueQRCodeCommand command;
|
||
for (String pileSn : pileSnList) {
|
||
// 给桩下发二维码
|
||
pileRemoteService.issueQRCode(pileSn, pileStationInfo.getQrcodePrefix());
|
||
}
|
||
return 1;
|
||
}
|
||
|
||
/**
|
||
* 查询充电桩预约记录
|
||
*/
|
||
|
||
|
||
/**
|
||
* 修改预约充电状态
|
||
* @param dto
|
||
*/
|
||
public void updateReservedStatus(PileReservationDTO dto) {
|
||
if (StringUtils.equals(dto.getStatus(), Constants.ZERO)) {
|
||
// 停用
|
||
pileReservationInfoService.deactivateReserved(dto);
|
||
} else if (StringUtils.equals(dto.getStatus(), Constants.ONE)) {
|
||
// 启用
|
||
pileReservationInfoService.activateReserved(dto);
|
||
}
|
||
}
|
||
|
||
public void deleteReservation(PileReservationDTO dto) {
|
||
pileReservationInfoService.deleteReservation(dto);
|
||
}
|
||
|
||
/**
|
||
* 个人桩启动充电
|
||
* @param dto
|
||
* @return
|
||
*/
|
||
public String startPersonalPileCharging(StartPersonPileDTO dto) {
|
||
return pileBasicInfoService.startPersonalPileCharging(dto);
|
||
}
|
||
|
||
/**
|
||
* 生成桩密钥
|
||
* @return
|
||
*/
|
||
public String generatePileSecret() {
|
||
// 获取 年月日 日期
|
||
String dateStr = DateUtils.dateTimeNow(DateUtils.YYYYMMDD);
|
||
// System.out.println("dateStr:" + dateStr);
|
||
int dateInt = Integer.parseInt(dateStr);
|
||
// 将年月日加上 160829
|
||
String data = String.valueOf(dateInt + 160829);
|
||
byte[] bytes = BytesUtil.str2Bcd(data);
|
||
// crc16加密
|
||
String crc = String.format("%04x", CRC16Util.calcCrc16(bytes)); // 050C
|
||
// System.out.println(crc);
|
||
// 将16进制转成10进制
|
||
int i = Integer.parseInt(crc, 16); // 1292
|
||
// System.out.println(i);
|
||
return String.valueOf(i);
|
||
}
|
||
|
||
|
||
|
||
}
|