2023-03-04 16:29:55 +08:00
|
|
|
package com.jsowell.netty.handler;
|
|
|
|
|
|
|
|
|
|
import com.alibaba.fastjson2.JSONObject;
|
|
|
|
|
import com.google.common.primitives.Bytes;
|
|
|
|
|
import com.jsowell.common.constant.Constants;
|
|
|
|
|
import com.jsowell.common.core.domain.ykc.YKCDataProtocol;
|
|
|
|
|
import com.jsowell.common.core.domain.ykc.YKCFrameTypeCode;
|
2023-03-23 11:10:49 +08:00
|
|
|
import com.jsowell.common.enums.ykc.OrderStatusEnum;
|
2023-03-23 15:08:53 +08:00
|
|
|
import com.jsowell.common.enums.ykc.PayModeEnum;
|
2023-03-18 10:59:02 +08:00
|
|
|
import com.jsowell.common.enums.ykc.ReturnCodeEnum;
|
2023-03-23 15:08:53 +08:00
|
|
|
import com.jsowell.common.enums.ykc.StartModeEnum;
|
2023-03-18 10:59:02 +08:00
|
|
|
import com.jsowell.common.exception.BusinessException;
|
2023-03-04 16:29:55 +08:00
|
|
|
import com.jsowell.common.util.BytesUtil;
|
2023-03-18 10:59:02 +08:00
|
|
|
import com.jsowell.common.util.StringUtils;
|
2023-03-04 16:29:55 +08:00
|
|
|
import com.jsowell.common.util.YKCUtils;
|
|
|
|
|
import com.jsowell.common.util.id.IdUtils;
|
|
|
|
|
import com.jsowell.netty.factory.YKCOperateFactory;
|
2023-03-23 11:10:49 +08:00
|
|
|
import com.jsowell.pile.domain.OrderBasicInfo;
|
|
|
|
|
import com.jsowell.pile.domain.OrderDetail;
|
2023-03-18 10:59:02 +08:00
|
|
|
import com.jsowell.pile.domain.PileAuthCard;
|
2023-03-23 15:08:53 +08:00
|
|
|
import com.jsowell.pile.domain.PileBasicInfo;
|
2023-03-18 10:59:02 +08:00
|
|
|
import com.jsowell.pile.service.IPileAuthCardService;
|
2023-03-23 15:08:53 +08:00
|
|
|
import com.jsowell.pile.service.IPileBasicInfoService;
|
|
|
|
|
import com.jsowell.pile.service.IPileBillingTemplateService;
|
2023-03-04 16:29:55 +08:00
|
|
|
import com.jsowell.pile.service.impl.MemberBasicInfoServiceImpl;
|
2023-03-23 11:10:49 +08:00
|
|
|
import com.jsowell.pile.transaction.dto.OrderTransactionDTO;
|
|
|
|
|
import com.jsowell.pile.transaction.service.TransactionService;
|
2023-03-23 15:08:53 +08:00
|
|
|
import com.jsowell.pile.vo.uniapp.CurrentTimePriceDetails;
|
2023-03-04 16:29:55 +08:00
|
|
|
import com.jsowell.pile.vo.uniapp.MemberVO;
|
2023-03-23 15:08:53 +08:00
|
|
|
import com.jsowell.pile.vo.web.BillingTemplateVO;
|
2023-03-04 16:29:55 +08:00
|
|
|
import io.netty.channel.Channel;
|
|
|
|
|
import lombok.extern.slf4j.Slf4j;
|
|
|
|
|
import org.springframework.beans.factory.annotation.Autowired;
|
|
|
|
|
import org.springframework.stereotype.Component;
|
|
|
|
|
|
|
|
|
|
import java.math.BigDecimal;
|
2023-03-23 15:08:53 +08:00
|
|
|
import java.util.Date;
|
2023-03-04 16:29:55 +08:00
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* 充电桩主动申请启动充电 0x31
|
|
|
|
|
*
|
|
|
|
|
* 启动充电鉴权结果
|
|
|
|
|
* @author JS-ZZA
|
|
|
|
|
* @date 2022/9/19 14:29
|
|
|
|
|
*/
|
|
|
|
|
@Slf4j
|
|
|
|
|
@Component
|
|
|
|
|
public class ConfirmStartChargingRequestHandler extends AbstractHandler{
|
|
|
|
|
private final String type = YKCUtils.frameType2Str(YKCFrameTypeCode.REQUEST_START_CHARGING_CODE.getBytes());
|
|
|
|
|
|
|
|
|
|
@Autowired
|
|
|
|
|
private MemberBasicInfoServiceImpl memberBasicInfoService;
|
|
|
|
|
|
2023-03-18 10:59:02 +08:00
|
|
|
@Autowired
|
|
|
|
|
private IPileAuthCardService pileAuthCardService;
|
|
|
|
|
|
2023-03-23 11:10:49 +08:00
|
|
|
@Autowired
|
|
|
|
|
private TransactionService pileTransactionService;
|
|
|
|
|
|
2023-03-23 15:08:53 +08:00
|
|
|
@Autowired
|
|
|
|
|
private IPileBasicInfoService pileBasicInfoService;
|
|
|
|
|
|
|
|
|
|
@Autowired
|
|
|
|
|
private IPileBillingTemplateService pileBillingTemplateService;
|
|
|
|
|
|
2023-03-04 16:29:55 +08:00
|
|
|
@Override
|
|
|
|
|
public void afterPropertiesSet() throws Exception {
|
|
|
|
|
YKCOperateFactory.register(type, this);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
@Override
|
|
|
|
|
public byte[] supplyProcess(YKCDataProtocol ykcDataProtocol, Channel channel) {
|
|
|
|
|
log.info("[===充电桩主动申请启动充电===] param:{}, channel:{}", JSONObject.toJSONString(ykcDataProtocol), channel.toString());
|
|
|
|
|
// 获取消息体
|
|
|
|
|
byte[] msgBody = ykcDataProtocol.getMsgBody();
|
|
|
|
|
|
|
|
|
|
int startIndex = 0;
|
2023-03-18 10:59:02 +08:00
|
|
|
int length = 7;
|
2023-03-04 16:29:55 +08:00
|
|
|
|
|
|
|
|
// 桩编码
|
|
|
|
|
byte[] pileSnByteArr = BytesUtil.copyBytes(msgBody, startIndex, length);
|
2023-03-18 10:59:02 +08:00
|
|
|
String pileSn = BytesUtil.binary(pileSnByteArr, 16);
|
2023-03-04 16:29:55 +08:00
|
|
|
|
|
|
|
|
// 保存时间
|
|
|
|
|
saveLastTime(pileSn);
|
|
|
|
|
|
|
|
|
|
// 枪号
|
|
|
|
|
startIndex += length;
|
|
|
|
|
length = 1;
|
2023-03-18 10:59:02 +08:00
|
|
|
byte[] connectorNumByteArr = BytesUtil.copyBytes(msgBody, startIndex, length);
|
|
|
|
|
String connectorCode = BytesUtil.bcd2Str(connectorNumByteArr);
|
2023-03-04 16:29:55 +08:00
|
|
|
|
|
|
|
|
// 启动方式
|
|
|
|
|
// 0x01 表示通过刷卡启动充电
|
|
|
|
|
// 0x02 表求通过帐号启动充电 (暂不支持)
|
|
|
|
|
// 0x03 表示vin码启动充电
|
|
|
|
|
startIndex += length;
|
|
|
|
|
byte[] startModeByteArr = BytesUtil.copyBytes(msgBody, startIndex, length);
|
2023-03-18 10:59:02 +08:00
|
|
|
String startMode = BytesUtil.bcd2Str(startModeByteArr);
|
2023-03-04 16:29:55 +08:00
|
|
|
|
|
|
|
|
// 是否需要密码 0x00 不需要 0x01 需要
|
|
|
|
|
startIndex += length;
|
|
|
|
|
byte[] needPasswordFlagByteArr = BytesUtil.copyBytes(msgBody, startIndex, length);
|
2023-03-18 10:59:02 +08:00
|
|
|
String needPasswordFlag = BytesUtil.bcd2Str(needPasswordFlagByteArr);
|
2023-03-04 16:29:55 +08:00
|
|
|
|
|
|
|
|
// 物理卡号 不足 8 位补 0
|
|
|
|
|
startIndex += length;
|
|
|
|
|
length = 8;
|
|
|
|
|
byte[] cardNumByteArr = BytesUtil.copyBytes(msgBody, startIndex, length);
|
2023-03-18 10:59:02 +08:00
|
|
|
String physicsCard = BytesUtil.binary(cardNumByteArr, 16);
|
|
|
|
|
log.info("物理卡号:{}", physicsCard);
|
2023-03-04 16:29:55 +08:00
|
|
|
|
|
|
|
|
// 输入密码 对用户输入的密码进行16 位MD5 加密,采用小写上传
|
|
|
|
|
startIndex += length;
|
|
|
|
|
length = 16;
|
|
|
|
|
byte[] inputPasswordByteArr = BytesUtil.copyBytes(msgBody, startIndex, length);
|
|
|
|
|
|
|
|
|
|
// VIN码
|
|
|
|
|
startIndex += length;
|
|
|
|
|
length = 17;
|
|
|
|
|
byte[] vinCodeByteArr = BytesUtil.copyBytes(msgBody, startIndex, length);
|
|
|
|
|
|
2023-03-18 10:59:02 +08:00
|
|
|
String logicCard = "";
|
|
|
|
|
byte[] authenticationFlagByteArr = Constants.zeroByteArray;
|
|
|
|
|
byte[] accountBalanceByteArr = Constants.zeroByteArray;
|
2023-03-30 10:30:43 +08:00
|
|
|
String transactionCode = "";
|
2023-03-18 10:59:02 +08:00
|
|
|
try {
|
|
|
|
|
if (StringUtils.equals("01", startMode)) {
|
|
|
|
|
// 刷卡启动充电
|
|
|
|
|
// 根据传过来的物理卡号查询数据库中此卡信息
|
|
|
|
|
PileAuthCard pileAuthCard = PileAuthCard.builder()
|
|
|
|
|
.logicCard(physicsCard)
|
|
|
|
|
.build();
|
|
|
|
|
PileAuthCard pileAuthCardInfo = pileAuthCardService.selectPileAuthCardInfo(pileAuthCard);
|
|
|
|
|
if (pileAuthCardInfo == null) {
|
|
|
|
|
// 未查到此卡信息
|
|
|
|
|
throw new BusinessException(ReturnCodeEnum.CODE_THIS_CARD_HAS_NO_INFO);
|
|
|
|
|
}
|
|
|
|
|
if (StringUtils.isBlank(pileAuthCardInfo.getMemberId())) {
|
|
|
|
|
// 卡未绑定用户
|
|
|
|
|
throw new BusinessException(ReturnCodeEnum.CODE_THIS_CARD_NOT_BIND_USER);
|
|
|
|
|
}
|
|
|
|
|
// 卡状态
|
|
|
|
|
String cardStatus = pileAuthCardInfo.getStatus();
|
|
|
|
|
// 逻辑卡号
|
|
|
|
|
logicCard = pileAuthCardInfo.getLogicCard();
|
|
|
|
|
// 通过memberId获取账户余额
|
|
|
|
|
MemberVO memberVO = memberBasicInfoService.queryMemberInfoByMemberId(pileAuthCardInfo.getMemberId());
|
|
|
|
|
BigDecimal principalBalance = memberVO.getPrincipalBalance(); // 本金金额
|
|
|
|
|
double accountBalance = principalBalance.add(memberVO.getGiftBalance()).setScale(2, BigDecimal.ROUND_HALF_UP).doubleValue();
|
|
|
|
|
accountBalanceByteArr = YKCUtils.getPriceByte(String.valueOf(accountBalance), 2);
|
2023-03-30 10:30:43 +08:00
|
|
|
if (!StringUtils.equals("1", cardStatus)) {
|
|
|
|
|
return null;
|
2023-03-18 16:55:15 +08:00
|
|
|
}
|
2023-03-30 10:30:43 +08:00
|
|
|
// 鉴权成功标识 0x00 失败 0x01 成功
|
|
|
|
|
authenticationFlagByteArr = Constants.oneByteArray;
|
|
|
|
|
transactionCode = IdUtils.generateTransactionCode(pileSn, connectorCode);
|
2023-03-23 15:08:53 +08:00
|
|
|
// 通过桩号查询所属站点
|
|
|
|
|
PileBasicInfo pileBasicInfo = pileBasicInfoService.selectPileBasicInfoBySN(pileSn);
|
|
|
|
|
Long stationId = pileBasicInfo.getStationId();
|
|
|
|
|
// TODO 个人桩站点不计费
|
2023-03-18 16:55:15 +08:00
|
|
|
|
2023-03-23 15:08:53 +08:00
|
|
|
// 将此订单信息存入订单表
|
2023-03-23 11:10:49 +08:00
|
|
|
String orderCode = IdUtils.getOrderCode();
|
|
|
|
|
// 订单基本信息
|
|
|
|
|
OrderBasicInfo orderBasicInfo = OrderBasicInfo.builder()
|
|
|
|
|
.orderCode(orderCode)
|
|
|
|
|
.transactionCode(transactionCode)
|
|
|
|
|
.orderStatus(OrderStatusEnum.NOT_START.getValue())
|
|
|
|
|
.memberId(memberVO.getMemberId())
|
2023-03-23 15:08:53 +08:00
|
|
|
.stationId(String.valueOf(stationId))
|
|
|
|
|
.pileSn(pileSn)
|
|
|
|
|
.connectorCode(connectorCode)
|
|
|
|
|
.pileConnectorCode(pileSn + connectorCode)
|
2023-03-30 08:56:10 +08:00
|
|
|
.logicCard(logicCard)
|
2023-03-23 15:08:53 +08:00
|
|
|
.startMode(StartModeEnum.CARD.getValue())
|
2023-03-23 11:10:49 +08:00
|
|
|
.payStatus(Constants.ZERO)
|
2023-03-23 15:08:53 +08:00
|
|
|
.payAmount(new BigDecimal(String.valueOf(accountBalance)))
|
|
|
|
|
.payTime(new Date())
|
|
|
|
|
.payMode(PayModeEnum.PAYMENT_OF_BALANCE.getValue())
|
2023-03-23 11:10:49 +08:00
|
|
|
.orderAmount(BigDecimal.ZERO)
|
|
|
|
|
.build();
|
2023-03-23 15:08:53 +08:00
|
|
|
// 根据桩编码查询当前计费模板
|
|
|
|
|
BillingTemplateVO billingTemplateVO = pileBillingTemplateService.selectBillingTemplateDetailByPileSn(pileSn);
|
2023-03-23 11:10:49 +08:00
|
|
|
// 订单详情
|
|
|
|
|
OrderDetail orderDetail = OrderDetail.builder()
|
|
|
|
|
.orderCode(orderCode)
|
2023-03-23 15:08:53 +08:00
|
|
|
.sharpElectricityPrice(billingTemplateVO.getSharpElectricityPrice())
|
|
|
|
|
.sharpServicePrice(billingTemplateVO.getSharpServicePrice())
|
|
|
|
|
.peakElectricityPrice(billingTemplateVO.getPeakElectricityPrice())
|
|
|
|
|
.peakServicePrice(billingTemplateVO.getPeakServicePrice())
|
|
|
|
|
.flatElectricityPrice(billingTemplateVO.getFlatElectricityPrice())
|
|
|
|
|
.flatServicePrice(billingTemplateVO.getFlatServicePrice())
|
|
|
|
|
.valleyElectricityPrice(billingTemplateVO.getValleyElectricityPrice())
|
|
|
|
|
.valleyServicePrice(billingTemplateVO.getValleyServicePrice())
|
2023-03-23 11:10:49 +08:00
|
|
|
.build();
|
|
|
|
|
|
|
|
|
|
OrderTransactionDTO createOrderTransactionDTO = OrderTransactionDTO.builder()
|
|
|
|
|
.orderBasicInfo(orderBasicInfo)
|
|
|
|
|
.orderDetail(orderDetail)
|
|
|
|
|
.build();
|
|
|
|
|
pileTransactionService.doCreateOrder(createOrderTransactionDTO);
|
2023-03-30 10:30:43 +08:00
|
|
|
|
|
|
|
|
// 将卡状态改为启动锁定
|
|
|
|
|
pileAuthCard.setId(pileAuthCardInfo.getId());
|
|
|
|
|
pileAuthCard.setStatus("2");
|
|
|
|
|
pileAuthCardService.updatePileAuthCard(pileAuthCard);
|
2023-03-18 10:59:02 +08:00
|
|
|
}
|
|
|
|
|
} catch (BusinessException e){
|
|
|
|
|
log.error("刷卡启动充电鉴权 error:{}, {}", e.getCode(), e.getMessage());
|
|
|
|
|
}catch (Exception e){
|
|
|
|
|
log.error("刷卡启动充电鉴权 error", e);
|
|
|
|
|
}
|
2023-03-04 16:29:55 +08:00
|
|
|
|
|
|
|
|
// 应答
|
|
|
|
|
// 交易流水号
|
2023-03-23 15:08:53 +08:00
|
|
|
// String transactionCode = IdUtils.generateTransactionCode(pileSn, connectorCode);
|
2023-03-18 10:59:02 +08:00
|
|
|
byte[] serialNumByteArr = BytesUtil.str2Bcd(transactionCode);
|
2023-03-04 16:29:55 +08:00
|
|
|
|
|
|
|
|
// 逻辑卡号
|
2023-03-18 10:59:02 +08:00
|
|
|
// String logicCardNum = "00000000";
|
2023-03-04 16:29:55 +08:00
|
|
|
|
|
|
|
|
// 账户余额 保留两位小数
|
2023-03-18 10:59:02 +08:00
|
|
|
// MemberVO memberVO = memberBasicInfoService.selectInfoByPhysicsCard(physicsCard);
|
|
|
|
|
// BigDecimal principalBalance = memberVO.getPrincipalBalance(); // 本金金额
|
|
|
|
|
// double accountBalance = principalBalance.add(memberVO.getGiftBalance()).setScale(2, BigDecimal.ROUND_HALF_UP).doubleValue();
|
|
|
|
|
// byte[] accountBalanceByteArr = BytesUtil.str2Bcd(String.valueOf(accountBalance));
|
2023-03-04 16:29:55 +08:00
|
|
|
|
|
|
|
|
// 鉴权成功标识 0x00 失败 0x01 成功
|
2023-03-18 10:59:02 +08:00
|
|
|
// byte[] authenticationFlagByteArr = Constants.oneByteArray;
|
2023-03-04 16:29:55 +08:00
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* 失败原因
|
|
|
|
|
* 0x01 账户不存在
|
|
|
|
|
* 0x02 账户冻结
|
|
|
|
|
* 0x03 账户余额不足
|
|
|
|
|
* 0x04 该卡存在未结账记录
|
|
|
|
|
* 0x05 桩停用
|
|
|
|
|
* 0x06 该账户不能在此桩上充电
|
|
|
|
|
* 0x07 密码错误
|
|
|
|
|
* 0x08 电站电容不足
|
|
|
|
|
* 0x09 系统中vin 码不存在
|
|
|
|
|
* 0x0A 该桩存在未结账记录
|
|
|
|
|
* 0x0B 该桩不支持刷卡
|
|
|
|
|
*/
|
|
|
|
|
byte[] defeatReasonByteArr = Constants.zeroByteArray;
|
|
|
|
|
|
|
|
|
|
// 拼装消息体
|
2023-03-18 10:59:02 +08:00
|
|
|
byte[] msgBodyByteArr = Bytes.concat(serialNumByteArr, pileSnByteArr, connectorNumByteArr, cardNumByteArr, accountBalanceByteArr,
|
2023-03-04 16:29:55 +08:00
|
|
|
authenticationFlagByteArr, defeatReasonByteArr);
|
|
|
|
|
|
|
|
|
|
return getResult(ykcDataProtocol, msgBodyByteArr);
|
|
|
|
|
|
|
|
|
|
}
|
|
|
|
|
}
|