异常订单,修复历史订单没有运营商编号

This commit is contained in:
yuyang 2021-10-21 14:36:43 +08:00
parent d9ec513913
commit 055edbc56b
2 changed files with 330 additions and 33 deletions

View File

@ -1,8 +1,10 @@
package com.xhpc.order.api;
import cn.hutool.core.date.DateTime;
import cn.hutool.core.date.DateUnit;
import cn.hutool.core.date.DateUtil;
import com.alibaba.fastjson.JSONObject;
import com.xhpc.common.api.SmsService;
import com.xhpc.common.api.WebSocketService;
import com.xhpc.common.core.domain.R;
import com.xhpc.common.core.web.controller.BaseController;
@ -13,6 +15,7 @@ import com.xhpc.common.redis.service.RedisService;
import com.xhpc.order.domain.XhpcChargeOrder;
import com.xhpc.order.domain.XhpcHistoryOrder;
import com.xhpc.order.service.IXhpcChargeOrderService;
import com.xhpc.order.service.IXhpcHistoryOrderService;
import com.xhpc.order.service.IXhpcRealTimeOrderService;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
@ -24,10 +27,10 @@ import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import java.math.BigDecimal;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.math.RoundingMode;
import java.util.*;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
@RestController
public class XhpcPileOrderController extends BaseController {
@ -44,6 +47,14 @@ public class XhpcPileOrderController extends BaseController {
@Autowired
private WebSocketService webSocketService;
@Autowired
private IXhpcHistoryOrderService xhpcHistoryOrderService;
@Autowired
private SmsService smsService;
private ExecutorService executorService = Executors.newFixedThreadPool(20);
private static final Logger logger = LoggerFactory.getLogger(XhpcPileOrderController.class);
/**
* 测试
@ -368,19 +379,31 @@ public class XhpcPileOrderController extends BaseController {
@Transactional
@PostMapping("/chargeOrder/abnormalOrder")
public R abnormalOrder(@RequestParam(value = "orderNo") String orderNo) {
//(调用时间-启动时间3分钟 自动结算)
logger.info("订单异常回调接口>>>>>orderNo" + orderNo);
//获取实时订单
CacheRealtimeData cacheRealtimeData = redisService.getCacheObject("order:" + orderNo + ".lord");
XhpcChargeOrder xhpcChargeOrder = xhpcChargeOrderService.getSerialNumberMessage(orderNo);
Long userId = xhpcChargeOrder.getUserId();
try {
//开始时间
Date startTime2 = xhpcChargeOrder.getStartTime();
//当checking为true时自动结算
boolean checking = false;
if((System.currentTimeMillis()-startTime2.getTime())<180000){
checking=true;
}
long l = xhpcChargeOrder.getStartTime().getTime() + Long.parseLong(String.valueOf(cacheRealtimeData.getChargingTime()));
//毫秒
Date date = new Date(l);
xhpcChargeOrder.setEndTime(date);
xhpcChargeOrder.setUpdateTime(date);
xhpcChargeOrder.setStatus(2);
if(checking){
xhpcChargeOrder.setStatus(1);
}else{
xhpcChargeOrder.setStatus(2);
}
xhpcChargeOrder.setEndSoc(cacheRealtimeData.getSoc()+"");
//充电时长
Long tiem = Long.valueOf(cacheRealtimeData.getChargingTime() / 1000);
@ -398,20 +421,249 @@ public class XhpcPileOrderController extends BaseController {
xhpcChargeOrder.setChargingTimeNumber(Long.valueOf(cacheRealtimeData.getChargingTime()));
xhpcChargeOrderService.updateXhpcChargeOrder(xhpcChargeOrder);
//结算
if(checking){
BigDecimal money =xhpcChargeOrder.getAmountCharged();
//结算
//总电费
BigDecimal powerPrice =new BigDecimal(0);
//算时间是否跨天
Long rateModelId = xhpcChargeOrder.getRateModelId();
Date updateTime2 = xhpcChargeOrder.getEndTime();
long betweenDay = DateUtil.between(startTime2, updateTime2, DateUnit.DAY);
if(betweenDay==0){
powerPrice=getBigDecimal(rateModelId,startTime2,updateTime2,xhpcChargeOrder.getChargingDegree());
}else{
//跨天
String startTime = DateUtil.format(startTime2, "yyyy-MM-dd");
Date updateTime = DateUtil.parse(startTime+" 23:59:59");
powerPrice=getBigDecimal(rateModelId,startTime2,updateTime,xhpcChargeOrder.getChargingDegree());
//明天
DateTime tomorrow = DateUtil.offsetDay(startTime2,1);
Date startTime3 =DateUtil.beginOfDay(tomorrow);
powerPrice=powerPrice.add(getBigDecimal(rateModelId,startTime3,updateTime2,xhpcChargeOrder.getChargingDegree()));
}
//总服务费
BigDecimal servicePrice = money.subtract(powerPrice);
//剩余的电费
BigDecimal surplusPowerPrice = powerPrice;
//剩余的服务费
BigDecimal surplusServicePrice = servicePrice;
Map<String, Object> userMessage = xhpcChargeOrderService.getUserMessage(userId);
XhpcHistoryOrder xhpcHistoryOrder = new XhpcHistoryOrder();
xhpcHistoryOrder.setStopReasonEvcs(5);
xhpcHistoryOrder.setPowerPriceTotal(powerPrice);
xhpcHistoryOrder.setServicePriceTotal(servicePrice);
xhpcHistoryOrder.setTotalPower(cacheRealtimeData.getAmountCharged().doubleValue()/10000);
//电表总起值
//xhpcHistoryOrder.setMeterValueStartEvcs(cacheOrderData.getElectricMeterStart().doubleValue());
//电表总止值
//xhpcHistoryOrder.setMeterValueEndEvcs(cacheOrderData.getElectricMeterEnd().doubleValue());
//vin
//xhpcHistoryOrder.setVinNormal(vinNormal);
BigDecimal balance = new BigDecimal(userMessage.get("balance").toString()).divide(new BigDecimal(100));
//电站活动抵扣--抵扣的总金额
BigDecimal promotionDiscount = new BigDecimal(0);
//实际价格-用户支付的钱
BigDecimal actPrice =money;
//流量方总金额抽成
BigDecimal internetCommission =new BigDecimal(0);
//流量方服务费抽成
BigDecimal internetSvcCommission =new BigDecimal(0);
//平台总金额抽成
BigDecimal platformCommission =new BigDecimal(0);
//平台服务费抽成
BigDecimal platformSvcCommission =new BigDecimal(0);
//运维总抽成
BigDecimal operationCommission =new BigDecimal(0);
//运维服务费抽成
BigDecimal operationSvcCommission =new BigDecimal(0);
//判断是C端用户还是流量端用户
if (xhpcChargeOrder.getSource() == 0) {
String state = "";
BigDecimal discount = new BigDecimal(0);
//用户第几次充电
int count = xhpcChargeOrderService.getCount(userId,xhpcChargeOrder.getChargeOrderId());
if (count == 0) {
//活动折扣
Map<String, Object> promotion = xhpcChargeOrderService.getPromotion();
if (promotion != null && promotion.get("state") != null && promotion.get("discount") != null) {
//state 1.总金额 2.电费 3.服务费 discount 折扣率
state = promotion.get("state").toString();
discount =new BigDecimal(promotion.get("discount").toString());
}
}
if(!"".equals(state)){
//查看是否优惠为0
boolean fan = true;
if(discount.compareTo(new BigDecimal(0))==0){
fan=false;
}
if("1".equals(state)){
//总金额
if(fan){
promotionDiscount=money.multiply(discount);
actPrice = money.subtract(promotionDiscount);
surplusPowerPrice = surplusPowerPrice.subtract(promotionDiscount.divide(new BigDecimal(2)));
surplusServicePrice= surplusServicePrice.subtract(promotionDiscount.divide(new BigDecimal(2)));
}
}else if("2".equals(state)){
if(fan){
//电费
promotionDiscount =powerPrice.multiply(balance);
actPrice = money.subtract(promotionDiscount);
surplusPowerPrice=surplusPowerPrice.subtract(promotionDiscount);
}
}else if("3".equals(state)){
if(fan){
//服务费
promotionDiscount = servicePrice.multiply(balance);
actPrice = money.subtract(promotionDiscount);
surplusServicePrice =surplusServicePrice.subtract(promotionDiscount);
}
}
}
xhpcHistoryOrder.setInternetCommission(internetCommission);
xhpcHistoryOrder.setInternetSvcCommission(internetSvcCommission);
}else{
//流量方未实现新增接口
}
//获取运营商
Map<String, Object> operatorMessage = xhpcChargeOrderService.getOperatorMessage(xhpcChargeOrder.getChargingStationId());
if(operatorMessage !=null){
if(operatorMessage.get("operatorIdEvcs")!=null){
if(operatorMessage.get("operatorIdEvcs") !=null && !"".equals(operatorMessage.get("operatorIdEvcs").toString())){
String stw = operatorMessage.get("operatorIdEvcs").toString();
if(stw.length()>9){
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));
//平台提成
BigDecimal platformCommissionRate = new BigDecimal(operatorMessage.get("platformCommissionRate").toString()).divide(new BigDecimal(100));
//提成类型0总金额提成 1服务费提成
if(commissionType==0){
BigDecimal multiply1 = surplusPowerPrice.multiply(platformCommissionRate);
BigDecimal multiply2 = surplusServicePrice.multiply(platformCommissionRate);
platformCommission = multiply1.add(multiply2);
//剩下的钱
surplusPowerPrice = surplusPowerPrice.subtract(multiply1);
surplusServicePrice = surplusServicePrice.subtract(multiply2);
BigDecimal multiply3 = surplusPowerPrice.multiply(maintenanceCommissionRate);
BigDecimal multiply4 = surplusServicePrice.multiply(maintenanceCommissionRate);
operationCommission = multiply1.add(multiply2);
//剩下的钱
surplusPowerPrice = surplusPowerPrice.subtract(multiply3);
surplusServicePrice = surplusServicePrice.subtract(multiply4);
}else if(commissionType==1){
BigDecimal multiply2 = surplusServicePrice.multiply(platformCommissionRate);
platformSvcCommission=multiply2;
//剩下的钱
surplusServicePrice = surplusServicePrice.subtract(multiply2);
BigDecimal multiply4 = surplusServicePrice.multiply(maintenanceCommissionRate);
operationCommission = multiply4;
//剩下的钱
surplusServicePrice = surplusServicePrice.subtract(multiply4);
}
}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.setStartSoc(xhpcChargeOrder.getStartSoc());
xhpcHistoryOrder.setReconciliationStatus(0);
xhpcHistoryOrder.setSortingStatus(0);
xhpcHistoryOrder.setType(1);
//订单总价---运维服务费抽成
xhpcHistoryOrder.setTotalPrice(money.setScale(2, BigDecimal.ROUND_HALF_UP));
xhpcHistoryOrder.setPromotionDiscount(promotionDiscount.setScale(2, BigDecimal.ROUND_HALF_UP));
xhpcHistoryOrder.setActPrice(actPrice.setScale(2, BigDecimal.ROUND_HALF_UP));
xhpcHistoryOrder.setActPowerPrice(surplusPowerPrice.setScale(2, BigDecimal.ROUND_HALF_UP));
xhpcHistoryOrder.setActServicePrice(surplusServicePrice.setScale(2, BigDecimal.ROUND_HALF_UP));
xhpcHistoryOrder.setInternetCommission(internetCommission.setScale(2, BigDecimal.ROUND_HALF_UP));
xhpcHistoryOrder.setInternetSvcCommission(internetSvcCommission.setScale(2, BigDecimal.ROUND_HALF_UP));
xhpcHistoryOrder.setPlatformCommission(platformCommission.setScale(2, BigDecimal.ROUND_HALF_UP));
xhpcHistoryOrder.setPlatformSvcCommisssion(platformSvcCommission.setScale(2, BigDecimal.ROUND_HALF_UP));
xhpcHistoryOrder.setOperationCommission(operationCommission.setScale(2, BigDecimal.ROUND_HALF_UP));
xhpcHistoryOrder.setOperationSvcCommission(operationSvcCommission.setScale(2, BigDecimal.ROUND_HALF_UP));
xhpcHistoryOrder.setStartSoc(xhpcChargeOrder.getStartSoc());
xhpcHistoryOrder.setEndSoc(xhpcChargeOrder.getEndSoc());
xhpcHistoryOrder.setReconciliationStatus(0);
xhpcHistoryOrder.setCreateTime(new Date());
xhpcHistoryOrder.setChargeModelEvcs(3);
xhpcHistoryOrder.setUserNameEvcs(userMessage.get("phone").toString());
xhpcHistoryOrder.setPhone(userMessage.get("phone").toString());
xhpcHistoryOrder.setConnectorPowerEvcs(Double.parseDouble(xhpcChargeOrder.getPower()));
//扣除用户实际消费金额添加消费记录
Map<String, Object> user = xhpcChargeOrderService.getUserMessage(userId);
//剩余的钱
BigDecimal balance1 =(BigDecimal) user.get("balance");
BigDecimal subtract = balance1.subtract(actPrice);
int i = xhpcChargeOrderService.updateUserBalance(userId, subtract);
if(i==0){
//扣钱失败
xhpcChargeOrder.setStatus(2);
xhpcChargeOrder.setErroRemark("扣钱失败");
}else{
xhpcHistoryOrderService.insert(xhpcHistoryOrder);
//添加流水
xhpcChargeOrderService.addUserAccountStatement(userId, actPrice.negate(), subtract, xhpcChargeOrder.getChargeOrderId(), 3, date);
try{
Map<String, Object> xhpcChargingPile = xhpcChargeOrderService.getXhpcChargingPile(xhpcChargeOrder.getTerminalId());
if(xhpcChargingPile !=null){
//发送短信
if(user.get("phone") !=null){
if("1".equals(xhpcChargingPile.get("type").toString())){
String content = "【小华停止充电】尊敬的用户,你的爱车已停止充电,电量为:" + xhpcChargeOrder.getEndSoc() + "%,总费用为:" + actPrice + "元,充电费用明细,请查询小华充电小程序,谢谢。";
smsService.sendNotice(user.get("phone").toString(),content);
}else{
String content = "【小华停止充电】尊敬的用户,你的爱车已停止充电,总费用为:" + actPrice + "元,充电费用明细,请查询小华充电小程序,谢谢。";
smsService.sendNotice(user.get("phone").toString(),content);
}
}
}
}catch (Exception e){
logger.info("<<<<<<<<<<<<<<<<发送短信失败>>>>>>>>>>>>>>>>>");
}
// 另起线程处理业务上传redis数据
executorService.execute(new Runnable() {
@Override
public void run() {
xhpcRealTimeOrderService.addPileEndOrder(xhpcHistoryOrder, xhpcChargeOrder, xhpcChargeOrder.getSerialNumber(),1);
}
});
}
}
} catch (Exception e) {
Map<String, Object> map = new HashMap<>();
map.put("code", 500);
map.put("userId", xhpcChargeOrder.getUserId());
JSONObject json = new JSONObject(map);
//消息对了内容
webSocketService.getMessage(userId+"",json.toString());
return R.fail(500,"无实时数据");
logger.info("异常订单回调失败");
e.printStackTrace();
}
Map<String, Object> map = new HashMap<>();
@ -420,23 +672,10 @@ public class XhpcPileOrderController extends BaseController {
JSONObject json = new JSONObject(map);
//消息对了内容
webSocketService.getMessage(userId+"",json.toString());
logger.info("桩实时数据发送WebSocket成功>>>>>orderNo" + orderNo);
logger.info("异常桩实时数据发送WebSocket成功>>>>>orderNo" + orderNo);
return R.ok();
}
/**
* @param status 状态
* @param remark 备注
@ -469,9 +708,61 @@ public class XhpcPileOrderController extends BaseController {
}
/**
*
* @param rateModelId 费率id
* @param startTime2 开始时间
* @param updateTime2 结算时间
* @param chargingDegree 总度数
* @return
*/
private BigDecimal getBigDecimal(Long rateModelId, Date startTime2, Date updateTime2, BigDecimal chargingDegree) {
//算出相差时间,
BigDecimal decimal = new BigDecimal((updateTime2.getTime()-startTime2.getTime())/60000).setScale(2);
//每分钟多少度
BigDecimal degree =chargingDegree.divide(decimal).setScale(2);
BigDecimal powerPriceTotal =new BigDecimal(0);
//获取费率时间段
String startTime = DateUtil.formatTime(startTime2);
String endTime = DateUtil.formatTime(updateTime2);
List<Map<String, Object>> reatTimeList = xhpcHistoryOrderService.getReatTimeList(startTime, endTime, rateModelId);
if(reatTimeList !=null && reatTimeList.size()>0){
int size = reatTimeList.size();
if(size==1){
BigDecimal powerFee = new BigDecimal(reatTimeList.get(0).get("powerFee").toString());
powerPriceTotal = powerPriceTotal.add(powerFee.multiply(degree).setScale(2, RoundingMode.HALF_UP));
}else{
for (int i = 0; i <reatTimeList.size() ; i++) {
Map<String, Object> objectMap = reatTimeList.get(i);
BigDecimal powerPrice =new BigDecimal(objectMap.get("powerFee").toString());
String startTime1 = objectMap.get("startTime").toString();
String endTime1 = objectMap.get("endTime").toString();
if(i==0){
powerPriceTotal = powerPriceTotal.add(getRateTimeList(startTime,endTime1,degree,powerPrice));
}else{
if(size==2){
powerPriceTotal = powerPriceTotal.add(getRateTimeList(startTime,endTime,degree,powerPrice));
}else{
//三个时段以上
if(i<size-1){
powerPriceTotal = powerPriceTotal.add(getRateTimeList(startTime1,endTime1,degree,powerPrice));
}else{
powerPriceTotal = powerPriceTotal.add(getRateTimeList(startTime1,endTime,degree,powerPrice));
}
}
}
}
}
}
return powerPriceTotal;
}
private BigDecimal getRateTimeList(String startTime,String endTime,BigDecimal degree,BigDecimal powerPrice){
BigDecimal v = new BigDecimal((DateUtil.parse(endTime).getTime() - DateUtil.parse(startTime).getTime())/60000).multiply(degree);
BigDecimal multiply = powerPrice.multiply(v).setScale(2, RoundingMode.HALF_UP);
return multiply;
}
@ -493,4 +784,10 @@ public class XhpcPileOrderController extends BaseController {
// }
// }
public static void main(String[] args) {
String stw ="91510105MA6DFCTD5U";
System.out.println(stw.substring(8, stw.length() - 1));
}
}

View File

@ -281,7 +281,7 @@ public class XhpcRealTimeOrderServiceImpl implements IXhpcRealTimeOrderService {
if(operatorMessage !=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();
if(stw.length()>9){
xhpcHistoryOrder.setOperatorIdEvcs(stw.substring(8, stw.length() - 1));