修改社区、B端用户充电逻辑

This commit is contained in:
yuyang 2022-01-27 17:20:30 +08:00
parent 575cab9590
commit 5e4c675bb4
10 changed files with 470 additions and 387 deletions

View File

@ -197,4 +197,9 @@ public class Constants
* 模板id为null * 模板id为null
*/ */
public static final String LNVALID_RATE_MODE = "lnvalid rateModelId"; public static final String LNVALID_RATE_MODE = "lnvalid rateModelId";
/**
* 数据问题报错
*/
public static final String DATA_EXCEPTION = "data exception";
} }

View File

@ -326,7 +326,7 @@ public class XhpcInvoiceServiceImpl implements XhpcInvoiceService {
OrderDatasDTO orderDatasDTO = new OrderDatasDTO(); OrderDatasDTO orderDatasDTO = new OrderDatasDTO();
orderDatasDTO.setOrderId((Long) data.get("historyOrderId")); orderDatasDTO.setOrderId((Long) data.get("historyOrderId"));
orderDatasDTO.setOrderNumber((String) data.get("serialNumber")); orderDatasDTO.setOrderNumber((String) data.get("serialNumber"));
orderDatasDTO.setOrderMoney((BigDecimal) data.get("actPrice")); orderDatasDTO.setOrderMoney(new BigDecimal(data.get("actPrice").toString()));
//因为数据库中存储的时间类型为datetime所以Mybatis会将其转换为LocalDateTime类型对象我们不能直接得到Date所以我们需要对其进行转换 //因为数据库中存储的时间类型为datetime所以Mybatis会将其转换为LocalDateTime类型对象我们不能直接得到Date所以我们需要对其进行转换
LocalDateTime createTime = (LocalDateTime) data.get("createTime"); LocalDateTime createTime = (LocalDateTime) data.get("createTime");
ZoneId zoneId = ZoneId.systemDefault(); ZoneId zoneId = ZoneId.systemDefault();

View File

@ -396,22 +396,26 @@ public class XhpcPileOrderController extends BaseController {
} }
xhpcHistoryOrder.setType(1); xhpcHistoryOrder.setType(1);
//结算 //结算
xhpcRealTimeOrderService.addSettlement(powerPrice,servicePrice,money,surplusPowerPrice,surplusServicePrice,xhpcChargeOrder,userId,userMessage,1,cacheOrderData.getVinNormal(),xhpcHistoryOrder); R r = xhpcRealTimeOrderService.addSettlement(powerPrice,servicePrice,money,surplusPowerPrice,surplusServicePrice,xhpcChargeOrder,userId,userMessage,1,cacheOrderData.getVinNormal(),xhpcHistoryOrder);
Map<String, Object> map = new HashMap<>(); if(r.getCode()==200){
map.put("code", 500); Map<String, Object> map = new HashMap<>();
map.put("userId", userId); map.put("code", 500);
JSONObject json = new JSONObject(map); map.put("userId", userId);
JSONObject json = new JSONObject(map);
if(!UserTypeUtil.INTERNET_TYPE.equals(source)){ if(!UserTypeUtil.INTERNET_TYPE.equals(source)){
String message =""; String message ="";
if(UserTypeUtil.USER_TYPE.equals(source)){ if(UserTypeUtil.USER_TYPE.equals(source)){
message=tenantId+UserTypeUtil.USER+userId; message=tenantId+UserTypeUtil.USER+userId;
}else if(UserTypeUtil.COMMUNIT_TYPE.equals(source)){ }else if(UserTypeUtil.COMMUNIT_TYPE.equals(source)){
message=tenantId+UserTypeUtil.COMMUNIT+userId; message=tenantId+UserTypeUtil.COMMUNIT+userId;
}else{ }else{
message=tenantId+UserTypeUtil.CUSTOMERS+userId; message=tenantId+UserTypeUtil.CUSTOMERS+userId;
}
webSocketService.getMessage(message,json.toString());
} }
webSocketService.getMessage(message,json.toString()); }else{
return R.fail();
} }
}catch (Exception e){ }catch (Exception e){
e.printStackTrace(); e.printStackTrace();

View File

@ -44,13 +44,29 @@ public interface XhpcChargeOrderMapper {
Map<String,Object> getUserMessage(@Param("userId")Long userId); Map<String,Object> getUserMessage(@Param("userId")Long userId);
/** /**
* 修改余额 * C端用户修改余额
* @param userId * @param userId
* @param balance * @param balance
* @return * @return
*/ */
int updateUserBalance(@Param("userId")Long userId, @Param("balance")BigDecimal balance); int updateUserBalance(@Param("userId")Long userId, @Param("balance")BigDecimal balance);
/**
* 社区用户修改余额
* @param userId
* @param balance
* @return
*/
int updateCommunityBalance(@Param("userId")Long userId, @Param("balance")BigDecimal balance);
/**
* B端用户修改余额
* @param userId
* @param balance
* @return
*/
int updateCustomersBalance(@Param("userId")Long userId, @Param("balance")BigDecimal balance);
/** /**
* 判断用户是否在充电中 * 判断用户是否在充电中
*/ */
@ -137,7 +153,7 @@ public interface XhpcChargeOrderMapper {
* @param date 时间 * @param date 时间
* @return * @return
*/ */
int insertUserAccountStatement(@Param("userId") Long userId,@Param("amount") BigDecimal amount,@Param("remainingSum") BigDecimal remainingSum,@Param("chargeOrderId") Long chargeOrderId,@Param("type") Integer type,@Param("date") Date date); int insertUserAccountStatement(@Param("userId") Long userId,@Param("amount") BigDecimal amount,@Param("remainingSum") BigDecimal remainingSum,@Param("chargeOrderId") Long chargeOrderId,@Param("type") Integer type,@Param("date") Date date,@Param("source")Integer source);
/** /**
* 获取费率 * 获取费率

View File

@ -129,7 +129,7 @@ public interface IXhpcChargeOrderService {
* @return * @return
*/ */
int addUserAccountStatement(Long userId, BigDecimal amount, BigDecimal remainingSum, Long chargeOrderId, Integer type, int addUserAccountStatement(Long userId, BigDecimal amount, BigDecimal remainingSum, Long chargeOrderId, Integer type,
Date date); Date date,Integer source);
XhpcChargeOrder getChargingOrderId(Long chargingOrderId); XhpcChargeOrder getChargingOrderId(Long chargingOrderId);

View File

@ -1,5 +1,6 @@
package com.xhpc.order.service; package com.xhpc.order.service;
import com.xhpc.common.core.domain.R;
import com.xhpc.common.core.web.domain.AjaxResult; import com.xhpc.common.core.web.domain.AjaxResult;
import com.xhpc.common.data.redis.CacheRealtimeData; import com.xhpc.common.data.redis.CacheRealtimeData;
import com.xhpc.order.domain.*; import com.xhpc.order.domain.*;
@ -115,9 +116,9 @@ public interface IXhpcRealTimeOrderService {
* @param type 0 不发短信 1发短信 * @param type 0 不发短信 1发短信
* @param vinNormal VUN * @param vinNormal VUN
*/ */
void addSettlement(BigDecimal powerPrice, BigDecimal servicePrice, BigDecimal money, BigDecimal surplusPowerPrice, R addSettlement(BigDecimal powerPrice, BigDecimal servicePrice, BigDecimal money, BigDecimal surplusPowerPrice,
BigDecimal surplusServicePrice, XhpcChargeOrder xhpcChargeOrder, Long userId, BigDecimal surplusServicePrice, XhpcChargeOrder xhpcChargeOrder, Long userId,
Map<String, Object> userMessage, Integer type, String vinNormal, XhpcHistoryOrder xhpcHistoryOrder); Map<String, Object> userMessage, Integer type, String vinNormal, XhpcHistoryOrder xhpcHistoryOrder);
/** /**
* 添加redis到数据库 * 添加redis到数据库

View File

@ -32,6 +32,7 @@ import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired; import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service; import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional; import org.springframework.transaction.annotation.Transactional;
import org.springframework.transaction.interceptor.TransactionAspectSupport;
import javax.annotation.PostConstruct; import javax.annotation.PostConstruct;
import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletRequest;
@ -230,8 +231,8 @@ public class XhpcChargeOrderServiceImpl extends BaseService implements IXhpcChar
//用户平台最小的 //用户平台最小的
int number =0; int number =0;
if(!"".equals(userMessage.get("soc")) && userMessage.get("soc") !=null && !"".equals(userMessage.get("soc"))){ if(!"".equals(userMessage.get("socUser")) && userMessage.get("socUser") !=null && !"".equals(userMessage.get("socUser"))){
number =Integer.parseInt(userMessage.get("soc").toString()); number =Integer.parseInt(userMessage.get("socUser").toString());
} }
//平台 //平台
String soc = redisService.getCacheObject("global:SOC"); String soc = redisService.getCacheObject("global:SOC");
@ -381,8 +382,15 @@ public class XhpcChargeOrderServiceImpl extends BaseService implements IXhpcChar
@Override @Override
public int updateUserBalance(Long userId, BigDecimal balance,Integer source,String tenantId) { public int updateUserBalance(Long userId, BigDecimal balance,Integer source,String tenantId) {
//修改用户余额
return xhpcChargeOrderMapper.updateUserBalance(userId, balance); if(UserTypeUtil.USER_TYPE.equals(source)){
return xhpcChargeOrderMapper.updateUserBalance(userId, balance);
}else if (UserTypeUtil.COMMUNIT_TYPE.equals(source)){
return xhpcChargeOrderMapper.updateCommunityBalance(userId, balance);
}else if(UserTypeUtil.CUSTOMERS_TYPE.equals(source)){
return xhpcChargeOrderMapper.updateCustomersBalance(userId, balance);
}
return 0;
} }
@Override @Override
@ -399,9 +407,9 @@ public class XhpcChargeOrderServiceImpl extends BaseService implements IXhpcChar
@Override @Override
public int addUserAccountStatement(Long userId, BigDecimal amount, BigDecimal remainingSum, Long chargeOrderId, public int addUserAccountStatement(Long userId, BigDecimal amount, BigDecimal remainingSum, Long chargeOrderId,
Integer type, Date date) { Integer type, Date date,Integer source) {
return xhpcChargeOrderMapper.insertUserAccountStatement(userId, amount, remainingSum, chargeOrderId, type, date); return xhpcChargeOrderMapper.insertUserAccountStatement(userId, amount, remainingSum, chargeOrderId, type, date,source);
} }
@Override @Override
@ -629,116 +637,148 @@ public class XhpcChargeOrderServiceImpl extends BaseService implements IXhpcChar
@Override @Override
public R cardStartup(Long userId, String serialNumber, Integer userType,String tenantId, Integer type, String grantOperator,String rateModelId) { public R cardStartup(Long userId, String serialNumber, Integer userType,String tenantId, Integer type, String grantOperator,String rateModelId) {
try{
R user = userTypeService.getUser(null, userId, userType, null,tenantId);
if(user ==null || user.getData() ==null){
return R.fail(Constants.LNVALID_USER);
}
R user = userTypeService.getUser(null, userId, userType, null,tenantId); Map<String, Object> userMessage = (Map<String, Object>)user.getData();
if(user ==null || user.getData() ==null){ //桩号是否有效
return R.fail(Constants.LNVALID_USER); String pattern = "^([0-9]{16})";
} Pattern compile = Pattern.compile(pattern);
Matcher m = compile.matcher(serialNumber);
if (serialNumber.length() != 16 || !m.matches()) {
return R.fail(Constants.LNVALID_TERMINAL);
}
Map<String, Object> userMessage = (Map<String, Object>)user.getData(); //终端信息
//桩号是否有效 XhpcTerminal xhpcTerminal = xhpcChargeOrderMapper.getXhpcTerminalSerialNumber(serialNumber,tenantId);
String pattern = "^([0-9]{16})"; if (xhpcTerminal == null || xhpcTerminal.getTerminalId() == null || xhpcTerminal.getChargingPileId() == null || xhpcTerminal.getPileSerialNumber() == null) {
Pattern compile = Pattern.compile(pattern); return R.fail(Constants.LNVALID_TERMINAL);
Matcher m = compile.matcher(serialNumber); }
if (serialNumber.length() != 16 || !m.matches()) {
return R.fail(Constants.LNVALID_TERMINAL);
}
//终端信息 if(type !=0){
XhpcTerminal xhpcTerminal = xhpcChargeOrderMapper.getXhpcTerminalSerialNumber(serialNumber,tenantId); //获取桩信息
if (xhpcTerminal == null || xhpcTerminal.getTerminalId() == null || xhpcTerminal.getChargingPileId() == null || xhpcTerminal.getPileSerialNumber() == null) { Map<String, Object> xhpcChargingPileById =xhpcChargeOrderMapper.getXhpcChargingPileById(xhpcTerminal.getChargingPileId(),tenantId);
return R.fail(Constants.LNVALID_TERMINAL); if(xhpcChargingPileById ==null || !xhpcChargingPileById.get("operatorId").toString().equals(grantOperator)){
} return R.fail(Constants.LNVALID_OPERATOR);
}
}
if(type !=0){
BigDecimal a = new BigDecimal(5);
if (userMessage == null || userMessage.get("balance") == null || a.compareTo(new BigDecimal(userMessage.get("balance").toString())) == 1) {
return R.fail(Constants.INSUFFICIENT_ACCOUNT);
}
//充电用户是否存在异常的订单
int j = xhpcChargeOrderMapper.countXhpcChargeOrder(userId,userType,tenantId);
if (j > 0) {
return R.fail(Constants.EXCEPTION_ORDER);
}
//查看充电用户是否有申请退款的订单还未处理
if (Integer.parseInt(userMessage.get("isRefundApplication").toString()) != 0) {
return R.fail(Constants.REFUND_ORDER);
}
//充电用户是否在充电中
String i = xhpcChargeOrderMapper.countXhpcRealTimeOrder(userId,userType,tenantId);
if (!"".equals(i) && i!=null) {
return R.fail(Constants.CHARGE_ORDER);
}
Map<String, Object> cacheMap = REDIS.getCacheMap("gun:" + serialNumber);
if(cacheMap==null){
return R.fail(Constants.UNREGISTERED_TERMINAL);
}else{
if(cacheMap.get("status") ==null){
return R.fail(Constants.UNKNOWN_TERMINAL);
}else{
String status = cacheMap.get("status").toString();
//不同的状态
if("离线".equals(status)){
return R.fail(Constants.OFFLINE_TERMINAL);
}
if("故障".equals(status)){
return R.fail(Constants.FAULT_TERMINAL);
}
if("充电".equals(status)){
return R.fail(Constants.CHARGE_TERMINAL);
}
}
if(cacheMap.get("vehicleGunStatus") ==null){
return R.fail(Constants.UNKNOWN_GUN);
}else{
String vehicleGunStatus = cacheMap.get("vehicleGunStatus").toString();
if(!"".equals(vehicleGunStatus)){
return R.fail(Constants.INSERT_THE_GUN);
}
}
}
//创建订单
//订单流水号 终端号+年月日时分秒+自增4位 共32位
String orderNo = genOrder(serialNumber);
XhpcChargeOrder xhpcChargeOrder = new XhpcChargeOrder();
xhpcChargeOrder.setChargingStationId(xhpcTerminal.getChargingStationId());
xhpcChargeOrder.setUserId(userId);
xhpcChargeOrder.setTerminalId(xhpcTerminal.getTerminalId());
xhpcChargeOrder.setGunId(xhpcTerminal.getSerialNumber());
xhpcChargeOrder.setSerialNumber(orderNo);
xhpcChargeOrder.setSource(userType);
xhpcChargeOrder.setStatus(-1);
xhpcChargeOrder.setTenantId(tenantId);
//获取桩信息 //获取桩信息
Map<String, Object> xhpcChargingPileById =xhpcChargeOrderMapper.getXhpcChargingPileById(xhpcTerminal.getChargingPileId(),tenantId); Map<String, Object> xhpcChargingPileById =xhpcChargeOrderMapper.getXhpcChargingPileById(xhpcTerminal.getChargingPileId(),tenantId);
if(xhpcChargingPileById ==null || !xhpcChargingPileById.get("operatorId").toString().equals(grantOperator)){ if (xhpcChargingPileById != null && xhpcChargingPileById.get("power") != null) {
return R.fail(Constants.LNVALID_OPERATOR); xhpcChargeOrder.setPower(xhpcChargingPileById.get("power").toString());
} }
} xhpcChargeOrder.setRateModelId(Long.valueOf(rateModelId));
xhpcChargeOrder.setChargingMode("刷卡");
xhpcChargeOrder.setCreateTime(Calendar.getInstance().getTime());
xhpcChargeOrder.setType(40);
xhpcChargeOrderMapper.addXhpcChargeOrder(xhpcChargeOrder);
BigDecimal a = new BigDecimal(5); //插一帧实时数据
if (userMessage == null || userMessage.get("balance") == null || a.compareTo(new BigDecimal(userMessage.get("balance").toString())) == 1) { extracted(xhpcChargeOrder);
return R.fail(Constants.INSUFFICIENT_ACCOUNT);
}
//充电用户是否存在异常的订单 Map<String,Object> map =new HashMap<>();
int j = xhpcChargeOrderMapper.countXhpcChargeOrder(userId,userType,tenantId); map.put("balance",userMessage.get("balance"));
if (j > 0) { map.put("serialNumber",orderNo);
return R.fail(Constants.EXCEPTION_ORDER); //用户平台最小的
} int number =0;
//查看充电用户是否有申请退款的订单还未处理 if(!"".equals(userMessage.get("socUser")) && userMessage.get("socUser") !=null && !"".equals(userMessage.get("socUser"))){
if (Integer.parseInt(userMessage.get("isRefundApplication").toString()) != 0) { number =Integer.parseInt(userMessage.get("socUser").toString());
return R.fail(Constants.REFUND_ORDER); }
} //平台
//充电用户是否在充电中 String soc = redisService.getCacheObject("global:SOC");
String i = xhpcChargeOrderMapper.countXhpcRealTimeOrder(userId,userType,tenantId); if(!"".equals(soc) && soc!=null){
if (!"".equals(i) && i!=null) { if(number!=0){
return R.fail(Constants.CHARGE_ORDER); if(Integer.parseInt(soc)-number<0){
} number=Integer.parseInt(soc);
Map<String, Object> cacheMap = REDIS.getCacheMap("gun:" + serialNumber); }
if(cacheMap==null){ }else{
return R.fail(Constants.UNREGISTERED_TERMINAL); number=Integer.parseInt(soc);
}else{
if(cacheMap.get("status") ==null){
return R.fail(Constants.UNKNOWN_TERMINAL);
}else{
String status = cacheMap.get("status").toString();
//不同的状态
if("离线".equals(status)){
return R.fail(Constants.OFFLINE_TERMINAL);
}
if("故障".equals(status)){
return R.fail(Constants.FAULT_TERMINAL);
}
if("充电".equals(status)){
return R.fail(Constants.CHARGE_TERMINAL);
} }
} }
if(cacheMap.get("vehicleGunStatus") ==null){ Map<String, Object> operatorMessage = xhpcChargeOrderMapper.getOperatorMessage(xhpcTerminal.getChargingStationId());
return R.fail(Constants.UNKNOWN_GUN); if(operatorMessage !=null && operatorMessage.get("soc") !=null && !"".equals(operatorMessage.get("soc"))){
}else{ if(number!=0){
String vehicleGunStatus = cacheMap.get("vehicleGunStatus").toString(); if(Integer.parseInt(operatorMessage.get("soc").toString())-number<0){
if(!"".equals(vehicleGunStatus)){ number=Integer.parseInt(operatorMessage.get("soc").toString());
return R.fail(Constants.INSERT_THE_GUN); }
}else{
number=Integer.parseInt(operatorMessage.get("soc").toString());
} }
} }
map.put("soc",number);
return R.ok(map);
}catch (Exception e){
//数据回滚
TransactionAspectSupport.currentTransactionStatus().setRollbackOnly();
} }
return R.fail(Constants.DATA_EXCEPTION);
//创建订单
//订单流水号 终端号+年月日时分秒+自增4位 共32位
String orderNo = genOrder(serialNumber);
XhpcChargeOrder xhpcChargeOrder = new XhpcChargeOrder();
xhpcChargeOrder.setChargingStationId(xhpcTerminal.getChargingStationId());
xhpcChargeOrder.setUserId(userId);
xhpcChargeOrder.setTerminalId(xhpcTerminal.getTerminalId());
xhpcChargeOrder.setGunId(xhpcTerminal.getSerialNumber());
xhpcChargeOrder.setSerialNumber(orderNo);
xhpcChargeOrder.setSource(userType);
xhpcChargeOrder.setStatus(-1);
xhpcChargeOrder.setTenantId(tenantId);
//获取桩信息
Map<String, Object> xhpcChargingPileById =xhpcChargeOrderMapper.getXhpcChargingPileById(xhpcTerminal.getChargingPileId(),tenantId);
if (xhpcChargingPileById != null && xhpcChargingPileById.get("power") != null) {
xhpcChargeOrder.setPower(xhpcChargingPileById.get("power").toString());
}
xhpcChargeOrder.setRateModelId(Long.valueOf(rateModelId));
xhpcChargeOrder.setChargingMode("刷卡");
xhpcChargeOrder.setCreateTime(Calendar.getInstance().getTime());
xhpcChargeOrder.setType(40);
xhpcChargeOrderMapper.addXhpcChargeOrder(xhpcChargeOrder);
//插一帧实时数据
extracted(xhpcChargeOrder);
Map<String,Object> map =new HashMap<>();
map.put("balance",userMessage.get("balance"));
map.put("serialNumber",orderNo);
return R.ok(map);
} }
public static boolean isValidDate(String str) { public static boolean isValidDate(String str) {

View File

@ -650,7 +650,7 @@ public class XhpcHistoryOrderServiceImpl extends BaseService implements IXhpcHis
} }
//扣除用户实际消费金额添加消费记录-余的钱 //扣除用户实际消费金额添加消费记录-余的钱
BigDecimal balance1 =(BigDecimal) userMessage.get("balance"); BigDecimal balance1 =new BigDecimal(userMessage.get("balance").toString());
BigDecimal subtract = balance1.subtract(actPrice); BigDecimal subtract = balance1.subtract(actPrice);
int i = xhpcChargeOrderService.updateUserBalance(userId, subtract,source,xhpcChargeOrder.getTenantId()); int i = xhpcChargeOrderService.updateUserBalance(userId, subtract,source,xhpcChargeOrder.getTenantId());
if(i==0){ if(i==0){
@ -659,7 +659,7 @@ public class XhpcHistoryOrderServiceImpl extends BaseService implements IXhpcHis
xhpcChargeOrder.setErroRemark("扣钱失败"); xhpcChargeOrder.setErroRemark("扣钱失败");
}else{ }else{
//添加流水 //添加流水
xhpcChargeOrderService.addUserAccountStatement(userId, actPrice.negate(), subtract, xhpcChargeOrder.getChargeOrderId(), 3, date); xhpcChargeOrderService.addUserAccountStatement(userId, actPrice.negate(), subtract, xhpcChargeOrder.getChargeOrderId(), 3, date,source);
map.put("userId",userId); map.put("userId",userId);
map.put("amount",subtract); map.put("amount",subtract);
if(userMessage.get("weixinOpenId") !=null && !"".equals(userMessage.get("weixinOpenId").toString())){ if(userMessage.get("weixinOpenId") !=null && !"".equals(userMessage.get("weixinOpenId").toString())){
@ -749,8 +749,8 @@ public class XhpcHistoryOrderServiceImpl extends BaseService implements IXhpcHis
Date updateTime = DateUtil.endOfDay(startTime2); Date updateTime = DateUtil.endOfDay(startTime2);
Map<String, Object> map1 = getBigDecimal(actPrice, powerPriceTotal, servicePriceTotal, chargeOrder, rateModelId, startTime2, updateTime, chargingDegree, list); Map<String, Object> map1 = getBigDecimal(actPrice, powerPriceTotal, servicePriceTotal, chargeOrder, rateModelId, startTime2, updateTime, chargingDegree, list);
//获取 //获取
BigDecimal powerPriceTotal1 = (BigDecimal) map1.get("powerPriceTotal"); BigDecimal powerPriceTotal1 = new BigDecimal(map1.get("powerPriceTotal").toString());
BigDecimal servicePriceTotal1 = (BigDecimal) map1.get("servicePriceTotal"); BigDecimal servicePriceTotal1 = new BigDecimal(map1.get("servicePriceTotal").toString());
//明天 //明天
DateTime tomorrow = DateUtil.offsetDay(startTime2, 1); DateTime tomorrow = DateUtil.offsetDay(startTime2, 1);
Date startTime3 = DateUtil.beginOfDay(tomorrow); Date startTime3 = DateUtil.beginOfDay(tomorrow);

View File

@ -5,6 +5,7 @@ import cn.hutool.core.date.DateUnit;
import cn.hutool.core.date.DateUtil; import cn.hutool.core.date.DateUtil;
import com.xhpc.common.api.RefundOrderService; import com.xhpc.common.api.RefundOrderService;
import com.xhpc.common.api.SmsService; import com.xhpc.common.api.SmsService;
import com.xhpc.common.core.domain.R;
import com.xhpc.common.core.utils.SecurityUtils; import com.xhpc.common.core.utils.SecurityUtils;
import com.xhpc.common.core.web.domain.AjaxResult; import com.xhpc.common.core.web.domain.AjaxResult;
import com.xhpc.common.core.web.service.BaseService; import com.xhpc.common.core.web.service.BaseService;
@ -23,6 +24,7 @@ import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired; import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service; import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional; import org.springframework.transaction.annotation.Transactional;
import org.springframework.transaction.interceptor.TransactionAspectSupport;
import java.math.BigDecimal; import java.math.BigDecimal;
import java.util.*; import java.util.*;
@ -355,287 +357,296 @@ public class XhpcRealTimeOrderServiceImpl extends BaseService implements IXhpcRe
* @param type 0 不发短信 1发短信 * @param type 0 不发短信 1发短信
*/ */
@Override @Override
public void addSettlement(BigDecimal powerPrice, BigDecimal servicePrice, BigDecimal money, BigDecimal surplusPowerPrice, BigDecimal surplusServicePrice, XhpcChargeOrder xhpcChargeOrder, Long userId, Map<String, Object> userMessage,Integer type,String vinNormal,XhpcHistoryOrder xhpcHistoryOrder) { public R addSettlement(BigDecimal powerPrice, BigDecimal servicePrice, BigDecimal money, BigDecimal surplusPowerPrice, BigDecimal surplusServicePrice, XhpcChargeOrder xhpcChargeOrder, Long userId, Map<String, Object> userMessage, Integer type, String vinNormal, XhpcHistoryOrder xhpcHistoryOrder) {
xhpcHistoryOrder.setPowerPriceTotal(powerPrice);
xhpcHistoryOrder.setServicePriceTotal(servicePrice);
xhpcHistoryOrder.setVinNormal(vinNormal);
//电站活动抵扣--抵扣的总金额 try{
BigDecimal promotionDiscount = new BigDecimal(0); xhpcHistoryOrder.setPowerPriceTotal(powerPrice);
//实际价格-用户支付的钱 xhpcHistoryOrder.setServicePriceTotal(servicePrice);
BigDecimal actPrice =money; xhpcHistoryOrder.setVinNormal(vinNormal);
//流量方总金额抽成
BigDecimal internetCommission =new BigDecimal(0); //电站活动抵扣--抵扣的总金额
//流量方服务费抽成 BigDecimal promotionDiscount = new BigDecimal(0);
BigDecimal internetSvcCommission =new BigDecimal(0); //实际价格-用户支付的钱
//流量方电量抽成 BigDecimal actPrice =money;
BigDecimal internetDegreeCommission =new BigDecimal(0); //流量方总金额抽成
//平台总金额抽成 BigDecimal internetCommission =new BigDecimal(0);
BigDecimal platformCommission =new BigDecimal(0); //流量方服务费抽成
//平台服务费抽成 BigDecimal internetSvcCommission =new BigDecimal(0);
BigDecimal platformSvcCommission =new BigDecimal(0); //流量方电量抽成
//运维总抽成 BigDecimal internetDegreeCommission =new BigDecimal(0);
BigDecimal operationCommission =new BigDecimal(0); //平台总金额抽成
//运维服务费抽成 BigDecimal platformCommission =new BigDecimal(0);
BigDecimal operationSvcCommission =new BigDecimal(0); //平台服务费抽成
//判断是C端用户还是流量端用户 BigDecimal platformSvcCommission =new BigDecimal(0);
//先计算第三方优惠力度 //运维总抽成
Integer source = xhpcChargeOrder.getSource(); BigDecimal operationCommission =new BigDecimal(0);
String internetSerialNumber = xhpcChargeOrder.getInternetSerialNumber(); //运维服务费抽成
if(source==1 && internetSerialNumber!=null){ BigDecimal operationSvcCommission =new BigDecimal(0);
String substring = internetSerialNumber.substring(0, 9); //判断是C端用户还是流量端用户
xhpcHistoryOrder.setInternetSerialNumber(xhpcChargeOrder.getInternetSerialNumber()); //先计算第三方优惠力度
Map<String, Object> operatorIdEvcs = xhpcRealTimeOrderMapper.getOperatorIdEvcs(substring); Integer source = xhpcChargeOrder.getSource();
if(operatorIdEvcs !=null){ String internetSerialNumber = xhpcChargeOrder.getInternetSerialNumber();
if(operatorIdEvcs.get("operatorIdEvcs")!=null && operatorIdEvcs.get("commissionType")!=null && operatorIdEvcs.get("commissionRate")!=null){ if(source==1 && internetSerialNumber!=null){
BigDecimal commissionRate = new BigDecimal(operatorIdEvcs.get("commissionRate").toString()).divide(new BigDecimal(100)); String substring = internetSerialNumber.substring(0, 9);
String commissionType = operatorIdEvcs.get("commissionType").toString(); xhpcHistoryOrder.setInternetSerialNumber(xhpcChargeOrder.getInternetSerialNumber());
if(operatorIdEvcs.get("internetUserId") !=null){ Map<String, Object> operatorIdEvcs = xhpcRealTimeOrderMapper.getOperatorIdEvcs(substring);
xhpcHistoryOrder.setChargingMode(operatorIdEvcs.get("internetUserId").toString()); if(operatorIdEvcs !=null){
} if(operatorIdEvcs.get("operatorIdEvcs")!=null && operatorIdEvcs.get("commissionType")!=null && operatorIdEvcs.get("commissionRate")!=null){
//0总金额提成 1服务费提成 BigDecimal commissionRate = new BigDecimal(operatorIdEvcs.get("commissionRate").toString()).divide(new BigDecimal(100));
if(new BigDecimal(0).compareTo(commissionRate)==-1){ String commissionType = operatorIdEvcs.get("commissionType").toString();
if("0".equals(commissionType) || "1".equals(commissionType)|| "2".equals(commissionType)){ if(operatorIdEvcs.get("internetUserId") !=null){
//流量方的钱 xhpcHistoryOrder.setChargingMode(operatorIdEvcs.get("internetUserId").toString());
if("0".equals(commissionType)){ }
BigDecimal decimal1 = surplusPowerPrice.multiply(commissionRate).setScale(2, BigDecimal.ROUND_DOWN); //0总金额提成 1服务费提成
BigDecimal decimal2 = surplusServicePrice.multiply(commissionRate).setScale(2, BigDecimal.ROUND_DOWN); if(new BigDecimal(0).compareTo(commissionRate)==-1){
internetCommission = decimal1.add(decimal2); if("0".equals(commissionType) || "1".equals(commissionType)|| "2".equals(commissionType)){
surplusPowerPrice =surplusPowerPrice.subtract(decimal1); //流量方的钱
surplusServicePrice =surplusServicePrice.subtract(decimal2); if("0".equals(commissionType)){
}else if("1".equals(commissionType)){ BigDecimal decimal1 = surplusPowerPrice.multiply(commissionRate).setScale(2, BigDecimal.ROUND_DOWN);
BigDecimal decimal2 = surplusServicePrice.multiply(commissionRate).setScale(2, BigDecimal.ROUND_DOWN); BigDecimal decimal2 = surplusServicePrice.multiply(commissionRate).setScale(2, BigDecimal.ROUND_DOWN);
internetSvcCommission = decimal2; internetCommission = decimal1.add(decimal2);
surplusServicePrice = surplusServicePrice.subtract(decimal2); surplusPowerPrice =surplusPowerPrice.subtract(decimal1);
}else{ surplusServicePrice =surplusServicePrice.subtract(decimal2);
//电量抽成 }else if("1".equals(commissionType)){
BigDecimal chargingDegree = xhpcChargeOrder.getChargingDegree(); BigDecimal decimal2 = surplusServicePrice.multiply(commissionRate).setScale(2, BigDecimal.ROUND_DOWN);
internetDegreeCommission = chargingDegree.multiply(new BigDecimal(operatorIdEvcs.get("commissionRate").toString())).setScale(2, BigDecimal.ROUND_DOWN); internetSvcCommission = decimal2;
//减服务费 surplusServicePrice = surplusServicePrice.subtract(decimal2);
surplusServicePrice = surplusServicePrice.subtract(internetDegreeCommission); }else{
//电量抽成
BigDecimal chargingDegree = xhpcChargeOrder.getChargingDegree();
internetDegreeCommission = chargingDegree.multiply(new BigDecimal(operatorIdEvcs.get("commissionRate").toString())).setScale(2, BigDecimal.ROUND_DOWN);
//减服务费
surplusServicePrice = surplusServicePrice.subtract(internetDegreeCommission);
}
} }
} }
} }
} }
} }
} if ( !UserTypeUtil.INTERNET_TYPE.equals(source)) {
if ( !UserTypeUtil.INTERNET_TYPE.equals(source)) { //用户第几次充电
//用户第几次充电 int count = xhpcChargeOrderService.getCount(userId,null,source,xhpcChargeOrder.getTenantId());
int count = xhpcChargeOrderService.getCount(userId,null,source,xhpcChargeOrder.getTenantId()); if (count == 0) {
if (count == 0) { //活动折扣
//活动折扣 Map<String, Object> promotion = xhpcChargeOrderService.getPromotion();
Map<String, Object> promotion = xhpcChargeOrderService.getPromotion(); if (promotion != null && promotion.get("state") != null && promotion.get("discount") != null) {
if (promotion != null && promotion.get("state") != null && promotion.get("discount") != null) { //state 1.总金额 2.电费 3.服务费 discount 折扣率
//state 1.总金额 2.电费 3.服务费 discount 折扣率 String state = promotion.get("state").toString();
String state = promotion.get("state").toString(); BigDecimal discount =new BigDecimal(promotion.get("discount").toString()).divide(new BigDecimal(100));
BigDecimal discount =new BigDecimal(promotion.get("discount").toString()).divide(new BigDecimal(100));
if(discount.compareTo(new BigDecimal(0))==1){ if(discount.compareTo(new BigDecimal(0))==1){
if("1".equals(state) || "2".equals(state) || "3".equals(state)){ if("1".equals(state) || "2".equals(state) || "3".equals(state)){
//折扣的钱
promotionDiscount=money.multiply(discount).setScale(2,BigDecimal.ROUND_DOWN);
//剩余的总金额
actPrice = money.subtract(promotionDiscount);
BigDecimal decimal1 = surplusPowerPrice.multiply(discount).setScale(2, BigDecimal.ROUND_DOWN);
BigDecimal decimal2 = surplusServicePrice.multiply(discount).setScale(2, BigDecimal.ROUND_DOWN);
if("1".equals(state)){
//总金额
actPrice =actPrice.subtract(decimal1.add(decimal2));
//折扣的钱 //折扣的钱
promotionDiscount = decimal1.add(decimal2); promotionDiscount=money.multiply(discount).setScale(2,BigDecimal.ROUND_DOWN);
surplusPowerPrice = surplusPowerPrice.subtract(decimal1); //剩余的总金额
surplusServicePrice= surplusServicePrice.subtract(decimal2); actPrice = money.subtract(promotionDiscount);
}else if("2".equals(state)){
//电费 BigDecimal decimal1 = surplusPowerPrice.multiply(discount).setScale(2, BigDecimal.ROUND_DOWN);
surplusPowerPrice=surplusPowerPrice.subtract(decimal1); BigDecimal decimal2 = surplusServicePrice.multiply(discount).setScale(2, BigDecimal.ROUND_DOWN);
}else{ if("1".equals(state)){
//服务费 //总金额
surplusServicePrice =surplusServicePrice.subtract(decimal2); actPrice =actPrice.subtract(decimal1.add(decimal2));
//折扣的钱
promotionDiscount = decimal1.add(decimal2);
surplusPowerPrice = surplusPowerPrice.subtract(decimal1);
surplusServicePrice= surplusServicePrice.subtract(decimal2);
}else if("2".equals(state)){
//电费
surplusPowerPrice=surplusPowerPrice.subtract(decimal1);
}else{
//服务费
surplusServicePrice =surplusServicePrice.subtract(decimal2);
}
} }
} }
} }
} }
} }
} //获取运营商
//获取运营商 Map<String, Object> operatorMessage = xhpcChargeOrderService.getOperatorMessage(xhpcChargeOrder.getChargingStationId());
Map<String, Object> operatorMessage = xhpcChargeOrderService.getOperatorMessage(xhpcChargeOrder.getChargingStationId()); if(operatorMessage !=null){
if(operatorMessage !=null){ if(operatorMessage.get("operatorIdEvcs")!=null){
if(operatorMessage.get("operatorIdEvcs")!=null){
if(operatorMessage.get("operatorIdEvcs") !=null && !"".equals(operatorMessage.get("operatorIdEvcs").toString())){ if(operatorMessage.get("operatorIdEvcs") !=null && !"".equals(operatorMessage.get("operatorIdEvcs").toString())){
String stw = operatorMessage.get("operatorIdEvcs").toString(); String stw = operatorMessage.get("operatorIdEvcs").toString();
if(stw.length()>9){ if(stw.length()>9){
xhpcHistoryOrder.setOperatorIdEvcs(stw.substring(8, stw.length() - 1)); xhpcHistoryOrder.setOperatorIdEvcs(stw.substring(8, stw.length() - 1));
}
}
}
if(operatorMessage.get("maintenanceCommissionRate") !=null && operatorMessage.get("commissionType") !=null && operatorMessage.get("platformCommissionRate") !=null){
Integer commissionType = (Integer) operatorMessage.get("commissionType");
//运维提成
BigDecimal maintenanceCommissionRate = new BigDecimal(operatorMessage.get("maintenanceCommissionRate").toString()).divide(new BigDecimal(100),2,BigDecimal.ROUND_DOWN);
//平台提成
BigDecimal platformCommissionRate = new BigDecimal(operatorMessage.get("platformCommissionRate").toString()).divide(new BigDecimal(100),2,BigDecimal.ROUND_DOWN);
//提成类型0总金额提成 1服务费提成
if(commissionType==0){
//平台总抽成
BigDecimal multiply1 = surplusPowerPrice.multiply(platformCommissionRate).setScale(2,BigDecimal.ROUND_DOWN);
BigDecimal multiply2 = surplusServicePrice.multiply(platformCommissionRate).setScale(2,BigDecimal.ROUND_DOWN);
//运维总抽成
BigDecimal multiply3 = surplusPowerPrice.multiply(maintenanceCommissionRate).setScale(2,BigDecimal.ROUND_DOWN);
BigDecimal multiply4 = surplusServicePrice.multiply(maintenanceCommissionRate).setScale(2,BigDecimal.ROUND_DOWN);
//平台总金额抽成
platformCommission = multiply1.add(multiply2);
//剩下的钱电费和服务
surplusPowerPrice = surplusPowerPrice.subtract(multiply1);
surplusServicePrice = surplusServicePrice.subtract(multiply2);
//运维总金额抽成
operationCommission = multiply3.add(multiply4);
//剩下的钱电费和服务
surplusPowerPrice = surplusPowerPrice.subtract(multiply3);
surplusServicePrice = surplusServicePrice.subtract(multiply4);
}else if(commissionType==1){
BigDecimal multiply1 = surplusServicePrice.multiply(platformCommissionRate).setScale(2,BigDecimal.ROUND_DOWN);
BigDecimal multiply2 = surplusServicePrice.multiply(maintenanceCommissionRate).setScale(2,BigDecimal.ROUND_DOWN);
//平台服务费抽成金额
platformSvcCommission=multiply1;
//剩下的服务费钱
surplusServicePrice = surplusServicePrice.subtract(multiply1);
//运维服务费抽成金额
operationCommission = multiply2;
//剩下的钱
surplusServicePrice = surplusServicePrice.subtract(multiply2);
}
if("微信".equals(xhpcChargeOrder.getChargingMode())){
xhpcHistoryOrder.setChargingMode("微信");
}
if("支付宝".equals(xhpcChargeOrder.getChargingMode())){
xhpcHistoryOrder.setChargingMode("支付宝");
}
}else{
//订单异常
xhpcChargeOrder.setStatus(2);
//异常原因
xhpcChargeOrder.setErroRemark("运营商数据为空");
}
}
Long chargeOrderId = xhpcChargeOrder.getChargeOrderId();
xhpcHistoryOrder.setChargeOrderId(chargeOrderId);
xhpcHistoryOrder.setChargingStationId(xhpcChargeOrder.getChargingStationId());
xhpcHistoryOrder.setUserId(userId);
xhpcHistoryOrder.setTerminalId(xhpcChargeOrder.getTerminalId());
xhpcHistoryOrder.setStartTime(xhpcChargeOrder.getStartTime());
xhpcHistoryOrder.setStopReasonEvcs(xhpcChargeOrder.getStopReasonEvcs());//todo 从头开始设置所有evcs字段
xhpcHistoryOrder.setChargeModelEvcs(xhpcChargeOrder.getChargeModelEvcs());
xhpcHistoryOrder.setSerialNumber(xhpcChargeOrder.getSerialNumber());
xhpcHistoryOrder.setRateModelId(xhpcChargeOrder.getRateModelId());
xhpcHistoryOrder.setStartSoc(xhpcChargeOrder.getStartSoc());
xhpcHistoryOrder.setReconciliationStatus(0);
xhpcHistoryOrder.setSortingStatus(0);
//订单总价---运维服务费抽成
xhpcHistoryOrder.setTotalPrice(money.setScale(2, BigDecimal.ROUND_DOWN));
xhpcHistoryOrder.setPromotionDiscount(promotionDiscount.setScale(2, BigDecimal.ROUND_DOWN));
xhpcHistoryOrder.setActPrice(actPrice.setScale(2, BigDecimal.ROUND_DOWN));
xhpcHistoryOrder.setActPowerPrice(surplusPowerPrice.setScale(2, BigDecimal.ROUND_DOWN));
xhpcHistoryOrder.setActServicePrice(surplusServicePrice.setScale(2, BigDecimal.ROUND_DOWN));
xhpcHistoryOrder.setInternetCommission(internetCommission.setScale(2, BigDecimal.ROUND_DOWN));
xhpcHistoryOrder.setInternetSvcCommission(internetSvcCommission.setScale(2, BigDecimal.ROUND_DOWN));
xhpcHistoryOrder.setInternetDegreeCommission(internetDegreeCommission.setScale(2, BigDecimal.ROUND_DOWN));
xhpcHistoryOrder.setPlatformCommission(platformCommission.setScale(2, BigDecimal.ROUND_DOWN));
xhpcHistoryOrder.setPlatformSvcCommisssion(platformSvcCommission.setScale(2, BigDecimal.ROUND_DOWN));
xhpcHistoryOrder.setOperationCommission(operationCommission.setScale(2, BigDecimal.ROUND_DOWN));
xhpcHistoryOrder.setOperationSvcCommission(operationSvcCommission.setScale(2, BigDecimal.ROUND_DOWN));
xhpcHistoryOrder.setStartSoc(xhpcChargeOrder.getStartSoc());
xhpcHistoryOrder.setEndSoc(xhpcChargeOrder.getEndSoc());
xhpcHistoryOrder.setReconciliationStatus(0);
xhpcHistoryOrder.setSource(xhpcChargeOrder.getSource());
Date date = new Date();
xhpcHistoryOrder.setCreateTime(date);
xhpcHistoryOrder.setChargeModelEvcs(3);
if(xhpcChargeOrder.getPower()!=null){
xhpcHistoryOrder.setConnectorPowerEvcs(Double.parseDouble(xhpcChargeOrder.getPower()));
}
Map<String, Object> map =new HashMap<>();
if(!UserTypeUtil.INTERNET_TYPE.equals(source)){
xhpcHistoryOrder.setUserNameEvcs(userMessage.get("phone").toString());
xhpcHistoryOrder.setPhone(userMessage.get("phone").toString());
//增加流水订单号
if(operatorMessage!=null && operatorMessage.get("operatorId")!=null){
String evcs = EvcsUtil.transferInternetOrderNo(xhpcChargeOrder.getSerialNumber(), operatorMessage.get("operatorId").toString());
xhpcHistoryOrder.setEvcsOrderNo(evcs);
}
//扣除用户实际消费金额添加消费记录 剩余的钱
BigDecimal balance1 =(BigDecimal) userMessage.get("balance");
BigDecimal subtract = balance1.subtract(actPrice);
int i = xhpcChargeOrderService.updateUserBalance(userId, subtract,xhpcChargeOrder.getSource(),xhpcChargeOrder.getTenantId());
if(i==0){
//扣钱失败
xhpcChargeOrder.setStatus(2);
xhpcChargeOrder.setErroRemark("扣钱失败");
}else{
//添加流水
xhpcChargeOrderService.addUserAccountStatement(userId, actPrice.negate(), subtract, xhpcChargeOrder.getChargeOrderId(), 3, date);
map.put("userId",userId);
map.put("amount",subtract);
if(userMessage.get("weixinOpenId") !=null && !"".equals(userMessage.get("weixinOpenId").toString())){
map.put("openid",userMessage.get("weixinOpenId").toString());
map.put("type",1);
}else{
map.put("openid",userMessage.get("alipayOpenId").toString());
map.put("type",2);
}
map.put("remark","充电结算自动申请退款");
}
try{
Map<String, Object> xhpcChargingPile = xhpcChargeOrderService.getXhpcChargingPile(xhpcChargeOrder.getTerminalId());
if(xhpcChargingPile !=null){
//发送短信
if(userMessage.get("phone") !=null){
if("1".equals(xhpcChargingPile.get("type").toString())){
HashMap<String, String> paramMap = new HashMap<>();
paramMap.put("elec", xhpcChargeOrder.getEndSoc());
paramMap.put("sumMoney", actPrice.toString());
paramMap.put("phone", userMessage.get("phone").toString());
paramMap.put("content", "【小华停止充电】尊敬的用户,你的爱车已停止充电,电量为:" + xhpcChargeOrder.getEndSoc() + "%,总费用为:" + actPrice + "元,充电费用明细,请查询小华充电小程序,谢谢。");
smsService.sendNotice(paramMap);
}else {
HashMap<String, String> paramMap = new HashMap<>();
paramMap.put("sumMoney", actPrice.toString());
paramMap.put("phone", userMessage.get("phone").toString());
paramMap.put("content", "【小华停止充电】尊敬的用户,你的爱车已停止充电,总费用为:" + actPrice + "元,充电费用明细,请查询小华充电小程序,谢谢。");
smsService.sendNotice(paramMap);
} }
} }
} }
}catch (Exception e){ if(operatorMessage.get("maintenanceCommissionRate") !=null && operatorMessage.get("commissionType") !=null && operatorMessage.get("platformCommissionRate") !=null){
logger.info("<<<<<<<<<<<<<<<<发送短信失败>>>>>>>>>>>>>>>>>"); Integer commissionType = (Integer) operatorMessage.get("commissionType");
} //运维提成
BigDecimal maintenanceCommissionRate = new BigDecimal(operatorMessage.get("maintenanceCommissionRate").toString()).divide(new BigDecimal(100),2,BigDecimal.ROUND_DOWN);
//平台提成
BigDecimal platformCommissionRate = new BigDecimal(operatorMessage.get("platformCommissionRate").toString()).divide(new BigDecimal(100),2,BigDecimal.ROUND_DOWN);
//提成类型0总金额提成 1服务费提成
if(commissionType==0){
//平台总抽成
BigDecimal multiply1 = surplusPowerPrice.multiply(platformCommissionRate).setScale(2,BigDecimal.ROUND_DOWN);
BigDecimal multiply2 = surplusServicePrice.multiply(platformCommissionRate).setScale(2,BigDecimal.ROUND_DOWN);
//运维总抽成
BigDecimal multiply3 = surplusPowerPrice.multiply(maintenanceCommissionRate).setScale(2,BigDecimal.ROUND_DOWN);
BigDecimal multiply4 = surplusServicePrice.multiply(maintenanceCommissionRate).setScale(2,BigDecimal.ROUND_DOWN);
} //平台总金额抽成
platformCommission = multiply1.add(multiply2);
//剩下的钱电费和服务
surplusPowerPrice = surplusPowerPrice.subtract(multiply1);
surplusServicePrice = surplusServicePrice.subtract(multiply2);
xhpcHistoryOrderService.insert(xhpcHistoryOrder); //运维总金额抽成
operationCommission = multiply3.add(multiply4);
//剩下的钱电费和服务
surplusPowerPrice = surplusPowerPrice.subtract(multiply3);
surplusServicePrice = surplusServicePrice.subtract(multiply4);
xhpcChargeOrderService.updateXhpcChargeOrder(xhpcChargeOrder); }else if(commissionType==1){
// 另起线程处理业务上传redis数据 BigDecimal multiply1 = surplusServicePrice.multiply(platformCommissionRate).setScale(2,BigDecimal.ROUND_DOWN);
executorService.execute(new Runnable() { BigDecimal multiply2 = surplusServicePrice.multiply(maintenanceCommissionRate).setScale(2,BigDecimal.ROUND_DOWN);
@Override //平台服务费抽成金额
public void run() { platformSvcCommission=multiply1;
//充电结算后自动申请退款 //剩下的服务费钱
if(userMessage !=null && "1".equals(userMessage.get("isRefund").toString()) && map !=null){ surplusServicePrice = surplusServicePrice.subtract(multiply1);
refundOrderService.sendNotice(map);
//运维服务费抽成金额
operationCommission = multiply2;
//剩下的钱
surplusServicePrice = surplusServicePrice.subtract(multiply2);
}
if("微信".equals(xhpcChargeOrder.getChargingMode())){
xhpcHistoryOrder.setChargingMode("微信");
}
if("支付宝".equals(xhpcChargeOrder.getChargingMode())){
xhpcHistoryOrder.setChargingMode("支付宝");
}
}else{
//订单异常
xhpcChargeOrder.setStatus(2);
//异常原因
xhpcChargeOrder.setErroRemark("运营商数据为空");
} }
addPileEndOrder(xhpcHistoryOrder, xhpcChargeOrder, xhpcChargeOrder.getSerialNumber(),1);
} }
}); Long chargeOrderId = xhpcChargeOrder.getChargeOrderId();
// redisService.deleteObject("pushOrder:"+xhpcChargeOrder.getSerialNumber()); xhpcHistoryOrder.setChargeOrderId(chargeOrderId);
xhpcHistoryOrder.setChargingStationId(xhpcChargeOrder.getChargingStationId());
xhpcHistoryOrder.setUserId(userId);
xhpcHistoryOrder.setTerminalId(xhpcChargeOrder.getTerminalId());
xhpcHistoryOrder.setStartTime(xhpcChargeOrder.getStartTime());
xhpcHistoryOrder.setStopReasonEvcs(xhpcChargeOrder.getStopReasonEvcs());//todo 从头开始设置所有evcs字段
xhpcHistoryOrder.setChargeModelEvcs(xhpcChargeOrder.getChargeModelEvcs());
xhpcHistoryOrder.setSerialNumber(xhpcChargeOrder.getSerialNumber());
xhpcHistoryOrder.setRateModelId(xhpcChargeOrder.getRateModelId());
xhpcHistoryOrder.setStartSoc(xhpcChargeOrder.getStartSoc());
xhpcHistoryOrder.setReconciliationStatus(0);
xhpcHistoryOrder.setSortingStatus(0);
//订单总价---运维服务费抽成
xhpcHistoryOrder.setTotalPrice(money.setScale(2, BigDecimal.ROUND_DOWN));
xhpcHistoryOrder.setPromotionDiscount(promotionDiscount.setScale(2, BigDecimal.ROUND_DOWN));
xhpcHistoryOrder.setActPrice(actPrice.setScale(2, BigDecimal.ROUND_DOWN));
xhpcHistoryOrder.setActPowerPrice(surplusPowerPrice.setScale(2, BigDecimal.ROUND_DOWN));
xhpcHistoryOrder.setActServicePrice(surplusServicePrice.setScale(2, BigDecimal.ROUND_DOWN));
xhpcHistoryOrder.setInternetCommission(internetCommission.setScale(2, BigDecimal.ROUND_DOWN));
xhpcHistoryOrder.setInternetSvcCommission(internetSvcCommission.setScale(2, BigDecimal.ROUND_DOWN));
xhpcHistoryOrder.setInternetDegreeCommission(internetDegreeCommission.setScale(2, BigDecimal.ROUND_DOWN));
xhpcHistoryOrder.setPlatformCommission(platformCommission.setScale(2, BigDecimal.ROUND_DOWN));
xhpcHistoryOrder.setPlatformSvcCommisssion(platformSvcCommission.setScale(2, BigDecimal.ROUND_DOWN));
xhpcHistoryOrder.setOperationCommission(operationCommission.setScale(2, BigDecimal.ROUND_DOWN));
xhpcHistoryOrder.setOperationSvcCommission(operationSvcCommission.setScale(2, BigDecimal.ROUND_DOWN));
xhpcHistoryOrder.setStartSoc(xhpcChargeOrder.getStartSoc());
xhpcHistoryOrder.setEndSoc(xhpcChargeOrder.getEndSoc());
xhpcHistoryOrder.setReconciliationStatus(0);
xhpcHistoryOrder.setSource(xhpcChargeOrder.getSource());
Date date = new Date();
xhpcHistoryOrder.setCreateTime(date);
xhpcHistoryOrder.setChargeModelEvcs(3);
if(xhpcChargeOrder.getPower()!=null){
xhpcHistoryOrder.setConnectorPowerEvcs(Double.parseDouble(xhpcChargeOrder.getPower()));
}
Map<String, Object> map =new HashMap<>();
if(!UserTypeUtil.INTERNET_TYPE.equals(source)){
xhpcHistoryOrder.setUserNameEvcs(userMessage.get("phone").toString());
xhpcHistoryOrder.setPhone(userMessage.get("phone").toString());
//增加流水订单号
if(operatorMessage!=null && operatorMessage.get("operatorId")!=null){
String evcs = EvcsUtil.transferInternetOrderNo(xhpcChargeOrder.getSerialNumber(), operatorMessage.get("operatorId").toString());
xhpcHistoryOrder.setEvcsOrderNo(evcs);
}
//扣除用户实际消费金额添加消费记录 剩余的钱
BigDecimal balance1 =new BigDecimal(userMessage.get("balance").toString());
BigDecimal subtract = balance1.subtract(actPrice);
int i = xhpcChargeOrderService.updateUserBalance(userId, subtract,xhpcChargeOrder.getSource(),xhpcChargeOrder.getTenantId());
if(i==0){
//扣钱失败
xhpcChargeOrder.setStatus(2);
xhpcChargeOrder.setErroRemark("扣钱失败");
}else{
//添加流水
xhpcChargeOrderService.addUserAccountStatement(userId, actPrice.negate(), subtract, xhpcChargeOrder.getChargeOrderId(), 3, date,xhpcChargeOrder.getSource());
map.put("userId",userId);
map.put("amount",subtract);
if(userMessage.get("weixinOpenId") !=null && !"".equals(userMessage.get("weixinOpenId").toString())){
map.put("openid",userMessage.get("weixinOpenId").toString());
map.put("type",1);
}else{
map.put("openid",userMessage.get("alipayOpenId").toString());
map.put("type",2);
}
map.put("remark","充电结算自动申请退款");
}
}
xhpcHistoryOrderService.insert(xhpcHistoryOrder);
xhpcChargeOrderService.updateXhpcChargeOrder(xhpcChargeOrder);
logger.info("<<<<<<<<<<<<<<<<订单结束异步之前>>>>>>>>>>>>>>>>>"+xhpcHistoryOrder.getSerialNumber());
final BigDecimal balance = actPrice;
// 另起线程处理业务上传redis数据
executorService.execute(new Runnable() {
@Override
public void run() {
try{
Map<String, Object> xhpcChargingPile = xhpcChargeOrderService.getXhpcChargingPile(xhpcChargeOrder.getTerminalId());
//充电结算后自动申请退款
if(userMessage !=null && "1".equals(userMessage.get("isRefund").toString()) && map !=null){
logger.info("<<<<<<<<<<<<<<<<进入自动退款111>>>>>>>>>>>>>>>>>");
if(UserTypeUtil.USER_TYPE.equals(source) || UserTypeUtil.COMMUNIT_TYPE.equals(source)){
logger.info("<<<<<<<<<<<<<<<<进入自动退款222>>>>>>>>>>>>>>>>>");
refundOrderService.sendNotice(map);
}
}
if(!UserTypeUtil.INSERT_BALANCE.equals(source)){
Map<String, Object> userMassage = xhpcChargeOrderService.getUserMessage(userId);
if(xhpcChargingPile !=null){
//发送短信
if(userMassage !=null && userMassage.get("phone") !=null){
if("1".equals(xhpcChargingPile.get("type").toString())){
HashMap<String, String> paramMap = new HashMap<>();
paramMap.put("elec", xhpcChargeOrder.getEndSoc());
paramMap.put("sumMoney", balance.toString());
paramMap.put("phone", userMassage.get("phone").toString());
paramMap.put("content", "【小华停止充电】尊敬的用户,你的爱车已停止充电,电量为:" + xhpcChargeOrder.getEndSoc() + "%,总费用为:" + balance + "元,充电费用明细,请查询小华充电小程序,谢谢。");
smsService.sendNotice(paramMap);
}else {
HashMap<String, String> paramMap = new HashMap<>();
paramMap.put("sumMoney", balance.toString());
paramMap.put("phone", userMassage.get("phone").toString());
paramMap.put("content", "【小华停止充电】尊敬的用户,你的爱车已停止充电,总费用为:" + balance.toString() + "元,充电费用明细,请查询小华充电小程序,谢谢。");
smsService.sendNotice(paramMap);
}
}
}
}
}catch (Exception e){
logger.info("<<<<<<<<<<<<<<<<发送短信失败>>>>>>>>>>>>>>>>>");
}
addPileEndOrder(xhpcHistoryOrder, xhpcChargeOrder, xhpcChargeOrder.getSerialNumber(),1);
}
});
}catch (Exception e){
logger.info("<<<<<<<<<<<<<<<<运行异常,结算失败,数据回滚>>>>>>>>>>>>>>>>>");
TransactionAspectSupport.currentTransactionStatus().setRollbackOnly();
return R.fail();
}
return R.ok();
} }
@Override @Override

View File

@ -127,6 +127,12 @@
<update id="updateUserBalance"> <update id="updateUserBalance">
update xhpc_app_user set balance=#{balance} where app_user_id=#{userId} update xhpc_app_user set balance=#{balance} where app_user_id=#{userId}
</update> </update>
<update id="updateCommunityBalance">
update xhpc_community_personnel set surplus_money=#{balance} where community_personnel_id=#{userId}
</update>
<update id="updateCustomersBalance">
update xhpc_customers_personnel set surplus_money=#{balance} where customers_personnel_id=#{userId}
</update>
<select id="countXhpcRealTimeOrder" resultType="String"> <select id="countXhpcRealTimeOrder" resultType="String">
select select
charge_order_id as chargeOrderId charge_order_id as chargeOrderId
@ -518,8 +524,8 @@
</insert> </insert>
<insert id="insertUserAccountStatement"> <insert id="insertUserAccountStatement">
INSERT INTO xhpc_user_account_statement(user_id, amount, remaining_sum, charge_order_id, type, create_time) INSERT INTO xhpc_user_account_statement(user_id, amount, remaining_sum, charge_order_id, type, create_time,source)
values (#{userId}, #{amount}, #{remainingSum}, #{chargeOrderId}, #{type}, #{date}) values (#{userId}, #{amount}, #{remainingSum}, #{chargeOrderId}, #{type}, #{date},#{source})
</insert> </insert>
<select id="getRateModelId" resultMap="XhpcRateMap"> <select id="getRateModelId" resultMap="XhpcRateMap">