@@ -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 occupy Code) {
OrderPileOccupy orderPileOccupy = queryByOccupyCode ( occupyCode ) ;
public void stopOccupyPileOrder ( String pileSn , String connector Code) {
// 根据充电站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 o ccupyCode ) {
private BigDecimal calculateOccupyPileOrderAmount ( OrderPileOccupy orderPileO ccupy) {
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 ;
}
}