占桩订单逻辑

This commit is contained in:
2023-08-14 16:44:39 +08:00
parent e4b04c4462
commit d537e1d995
7 changed files with 255 additions and 27 deletions

View File

@@ -2,6 +2,7 @@ package com.jsowell.pile.domain;
import lombok.*;
import java.math.BigDecimal;
import java.util.Date;
/**
@@ -24,7 +25,7 @@ public class OrderPileOccupy {
private String occupyCode;
/**
* 状态0-占桩中1-订单完成)
* 状态0-占桩中1-订单完成; 2-订单挂起
*/
private String status;
@@ -33,6 +34,11 @@ public class OrderPileOccupy {
*/
private String memberId;
/**
* 充电站id
*/
private String stationId;
/**
* 订单号
*/
@@ -58,6 +64,11 @@ public class OrderPileOccupy {
*/
private String payStatus;
/**
* 占桩订单金额
*/
private BigDecimal orderAmount;
/**
* 充电桩编号
*/

View File

@@ -68,4 +68,12 @@ public interface OrderPileOccupyMapper {
OrderPileOccupy queryByOccupyCode(@Param("occupyCode") String occupyCode);
List<OrderPileOccupy> queryOccupyOrderList(@Param("dto") QueryOccupyOrderDTO dto);
/**
* 根据桩号和枪号查询占桩中的订单
*
* @param pileSn
* @param connectorCode
*/
OrderPileOccupy queryOccupiedOrder(@Param("pileSn") String pileSn, @Param("connectorCode") String connectorCode);
}

View File

@@ -36,5 +36,6 @@ public interface OrderPileOccupyService{
*/
void generateOccupyPileOrder(String memberId, String pileSn, String connectorCode);
void stopOccupyPileOrder(String occupyCode);
void stopOccupyPileOrder(String pileSn, String connectorCode);
}

View File

@@ -3,15 +3,25 @@ package com.jsowell.pile.service.impl;
import com.jsowell.common.constant.Constants;
import com.jsowell.common.enums.DelFlagEnum;
import com.jsowell.common.util.DateUtils;
import com.jsowell.common.util.StringUtils;
import com.jsowell.common.util.id.IdUtils;
import com.jsowell.pile.domain.OrderPileOccupy;
import com.jsowell.pile.dto.QueryOccupyOrderDTO;
import com.jsowell.pile.dto.QueryOrderDTO;
import com.jsowell.pile.mapper.OrderPileOccupyMapper;
import com.jsowell.pile.service.IOrderBasicInfoService;
import com.jsowell.pile.service.IPileBasicInfoService;
import com.jsowell.pile.service.IPileBillingTemplateService;
import com.jsowell.pile.service.OrderPileOccupyService;
import com.jsowell.pile.vo.base.PileInfoVO;
import com.jsowell.pile.vo.web.BillingTemplateVO;
import com.jsowell.pile.vo.web.OrderListVO;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;
import javax.annotation.Resource;
import java.math.BigDecimal;
import java.util.Date;
import java.util.List;
@Slf4j
@@ -21,6 +31,15 @@ public class OrderPileOccupyServiceImpl implements OrderPileOccupyService{
@Resource
private OrderPileOccupyMapper orderPileOccupyMapper;
@Resource
private IPileBasicInfoService pileBasicInfoService;
@Resource
private IOrderBasicInfoService orderBasicInfoService;
@Resource
private IPileBillingTemplateService pileBillingTemplateService;
@Override
public int deleteByPrimaryKey(Integer id) {
return orderPileOccupyMapper.deleteByPrimaryKey(id);
@@ -83,6 +102,9 @@ public class OrderPileOccupyServiceImpl implements OrderPileOccupyService{
*/
@Override
public OrderPileOccupy queryByOccupyCode(String occupyCode) {
if (StringUtils.isBlank(occupyCode)) {
return null;
}
return orderPileOccupyMapper.queryByOccupyCode(occupyCode);
}
@@ -113,6 +135,10 @@ public class OrderPileOccupyServiceImpl implements OrderPileOccupyService{
orderPileOccupy.setStatus(Constants.ZERO); // 占桩中
orderPileOccupy.setPayStatus(Constants.ZERO); // 未支付
orderPileOccupy.setPileSn(pileSn);
PileInfoVO pileInfoVO = pileBasicInfoService.selectPileInfoBySn(pileSn);
if (pileInfoVO != null) {
orderPileOccupy.setStationId(pileInfoVO.getStationId());
}
orderPileOccupy.setConnectorCode(connectorCode);
orderPileOccupy.setPileConnectorCode(pileSn + connectorCode);
orderPileOccupy.setStartTime(DateUtils.getNowDate());
@@ -121,16 +147,34 @@ public class OrderPileOccupyServiceImpl implements OrderPileOccupyService{
}
/**
* 收到地锁升起指令,调用这个方法,停止计时
* 占桩订单停止计费/停止占桩订单计费
* @param pileSn
* @param connectorCode
*/
@Override
public void stopOccupyPileOrder(String occupyCode) {
OrderPileOccupy orderPileOccupy = queryByOccupyCode(occupyCode);
public void stopOccupyPileOrder(String pileSn, String connectorCode) {
// 根据充电站sn和枪口号查询占桩中的占桩订单
OrderPileOccupy orderPileOccupy = orderPileOccupyMapper.queryOccupiedOrder(pileSn, connectorCode);
if (orderPileOccupy == null) {
log.error("根据占桩订单编号:{}, 查询为空", occupyCode);
return;
}
// 设置结束时间
orderPileOccupy.setEndTime(DateUtils.getNowDate());
// 计算金额
BigDecimal orderAmount = calculateOccupyPileOrderAmount(orderPileOccupy);
if (orderAmount.compareTo(BigDecimal.ZERO) > 0) {
// 需要支付金额,订单挂起
orderPileOccupy.setStatus(Constants.TWO); // 2-订单挂起
} else {
// 订单金额为0
orderPileOccupy.setPayStatus(Constants.TWO); // 2-无需支付
orderPileOccupy.setStatus(Constants.ONE); // 1-订单完成
}
orderPileOccupy.setOrderAmount(orderAmount);
// 更新数据库
orderPileOccupyMapper.updateByPrimaryKeySelective(orderPileOccupy);
}
@@ -138,8 +182,74 @@ public class OrderPileOccupyServiceImpl implements OrderPileOccupyService{
* 计算占桩订单金额
* calculateTheAmountOfTheOccupancyOrder
*/
public void calculateOccupyPileOrderAmount(String occupyCode) {
private BigDecimal calculateOccupyPileOrderAmount(OrderPileOccupy orderPileOccupy) {
BigDecimal resultAmount = BigDecimal.ZERO;
String occupyCode = orderPileOccupy.getOccupyCode();
Date startTime = orderPileOccupy.getStartTime(); // 占桩开始时间
Date endTime = orderPileOccupy.getEndTime(); // 占桩结束时间
/*
查新站点计费模板
*/
BillingTemplateVO billingTemplate = pileBillingTemplateService.queryUsedBillingTemplate(orderPileOccupy.getStationId());
if (billingTemplate == null) {
log.info("计算占桩订单金额-查询站点:{}计费模板为空, 不收取占桩费用", orderPileOccupy.getStationId());
return resultAmount;
}
Integer freeTime = billingTemplate.getFreeTime(); // 免费时长
BigDecimal occupyFee = billingTemplate.getOccupyFee(); // 占桩费率
if (freeTime == null || occupyFee == null) {
log.info("计算占桩订单金额-站点:{}计费模板未设置免费时长和占桩费率, 不收取占桩费用", orderPileOccupy.getStationId());
return resultAmount;
}
// 占桩时长
long occupyTime = DateUtils.intervalTime(startTime, endTime);
if (freeTime > occupyTime) {
log.info("计算占桩订单金额-站点:{}的占桩订单:{}, 免费时长:{}大于占桩时长:{}, 不收取占桩费用", orderPileOccupy.getStationId(), occupyCode, freeTime, occupyTime);
return resultAmount;
}
/*
查询会员在占用时段内有没有充电订单
*/
QueryOrderDTO dto = new QueryOrderDTO();
dto.setMemberId(orderPileOccupy.getMemberId());
dto.setStationId(orderPileOccupy.getStationId());
// 查询在占桩期间创建的订单
dto.setStartTime(DateUtils.formatDateTime(startTime));
dto.setEndTime(DateUtils.formatDateTime(endTime));
List<OrderListVO> orderListVOS = orderBasicInfoService.selectOrderBasicInfoList(dto);
/*
计算充电时长
*/
long sumChargingTime = 0;
for (OrderListVO orderListVO : orderListVOS) {
String chargeStartTime = orderListVO.getChargeStartTime();
String chargeEndTime = orderListVO.getChargeEndTime();
// 如果没有开始时间或者结束时间,就不计入充电时长
if (StringUtils.isBlank(chargeStartTime) || StringUtils.isNotBlank(chargeEndTime)) {
continue;
}
long l = DateUtils.intervalTime(chargeStartTime, chargeEndTime);
sumChargingTime = sumChargingTime + l;
}
/*
计算应收金额
应收金额 = (总占用时长 - (免费时长 + 充电时长)) * 占桩费率
*/
BigDecimal totalOccupyTime = new BigDecimal(occupyTime + "");
BigDecimal totalFreeTime = new BigDecimal(freeTime).add(new BigDecimal(sumChargingTime));
if (totalFreeTime.compareTo(totalOccupyTime) >= 0) {
log.info("计算占桩订单金额-站点:{}的占桩订单:{}, 总免费时长:{}大于占桩时长:{}, 不收取占桩费用", orderPileOccupy.getStationId(), occupyCode, totalFreeTime, totalOccupyTime);
return resultAmount;
}
// 需要计费的时间
BigDecimal time = totalOccupyTime.subtract(totalFreeTime);
resultAmount = time.multiply(occupyFee);
return resultAmount;
}
}

View File

@@ -43,6 +43,12 @@ public class BillingTemplateVO {
// 设备类型 1-电动汽车桩2-电动自行车桩
private String deviceType;
// 免费时长(单位:分钟)
private Integer freeTime;
// 占桩费率(单位:元/分钟)
private BigDecimal occupyFee;
// 尖时段电费
private BigDecimal sharpElectricityPrice;
// 尖时段服务费