Files
jsowell-charger-web/jsowell-admin/src/main/java/com/jsowell/service/TempService.java

1429 lines
74 KiB
Java
Raw Normal View History

2023-08-26 18:01:25 +08:00
package com.jsowell.service;
import com.alibaba.fastjson2.JSON;
2024-11-13 17:06:46 +08:00
import com.alibaba.fastjson2.JSONArray;
2023-08-26 18:01:25 +08:00
import com.alibaba.fastjson2.JSONObject;
2025-04-29 11:59:06 +08:00
import com.google.common.collect.ImmutableMap;
2023-08-26 18:01:25 +08:00
import com.google.common.collect.Lists;
import com.google.common.collect.Maps;
import com.google.common.collect.Sets;
import com.huifu.adapay.core.exception.BaseAdaPayException;
2023-10-24 14:49:49 +08:00
import com.jsowell.adapay.common.AdaPayment;
2024-12-26 10:04:39 +08:00
import com.jsowell.adapay.common.PaymentConfirmInfo;
2023-08-26 18:01:25 +08:00
import com.jsowell.adapay.dto.QueryPaymentConfirmDTO;
import com.jsowell.adapay.response.PaymentConfirmResponse;
2023-08-26 18:01:25 +08:00
import com.jsowell.adapay.response.QueryPaymentConfirmDetailResponse;
import com.jsowell.adapay.service.AdapayService;
2024-09-24 16:39:42 +08:00
import com.jsowell.adapay.vo.OrderSplitResult;
2023-09-26 15:41:05 +08:00
import com.jsowell.adapay.vo.PaymentInfo;
2024-11-21 16:01:26 +08:00
import com.jsowell.common.annotation.CostTime;
2025-10-10 13:32:53 +08:00
import com.jsowell.common.constant.Constants;
2025-03-26 21:37:12 +08:00
import com.jsowell.common.constant.RabbitConstants;
2025-03-26 13:49:31 +08:00
import com.jsowell.common.core.domain.ykc.TransactionRecordsData;
2024-11-14 17:02:03 +08:00
import com.jsowell.common.core.redis.RedisCache;
2023-10-24 15:33:58 +08:00
import com.jsowell.common.enums.adapay.AdapayStatusEnum;
2025-03-31 14:29:29 +08:00
import com.jsowell.common.enums.adapay.MerchantDelayModeEnum;
2023-08-26 18:01:25 +08:00
import com.jsowell.common.enums.ykc.*;
import com.jsowell.common.exception.BusinessException;
2024-11-15 13:11:21 +08:00
import com.jsowell.common.util.DateUtils;
2023-08-26 18:01:25 +08:00
import com.jsowell.common.util.StringUtils;
import com.jsowell.pile.domain.*;
2025-04-25 17:31:51 +08:00
import com.jsowell.pile.dto.*;
2023-08-26 18:01:25 +08:00
import com.jsowell.pile.mapper.OrderBasicInfoMapper;
import com.jsowell.pile.service.*;
2025-03-26 13:49:31 +08:00
import com.jsowell.pile.service.programlogic.AbstractProgramLogic;
2025-03-26 14:42:57 +08:00
import com.jsowell.pile.service.programlogic.ProgramLogicFactory;
import com.jsowell.pile.transaction.dto.OrderTransactionDTO;
import com.jsowell.pile.transaction.service.TransactionService;
import com.jsowell.pile.vo.base.StationInfoVO;
2025-03-25 15:16:51 +08:00
import com.jsowell.pile.vo.web.*;
import com.jsowell.thirdparty.common.NotificationDTO;
import com.jsowell.thirdparty.common.NotificationService;
import com.jsowell.thirdparty.platform.dto.PushOrderDTO;
import com.jsowell.thirdparty.service.ThirdpartySecretInfoService;
2023-08-26 18:01:25 +08:00
import org.apache.commons.collections4.CollectionUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.amqp.rabbit.core.RabbitTemplate;
2023-08-26 18:01:25 +08:00
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
2024-11-15 10:34:23 +08:00
import org.springframework.transaction.annotation.Transactional;
2023-08-26 18:01:25 +08:00
import java.math.BigDecimal;
2025-03-25 15:16:51 +08:00
import java.math.RoundingMode;
2025-04-29 11:30:35 +08:00
import java.text.MessageFormat;
import java.time.LocalDate;
2025-03-26 20:32:12 +08:00
import java.time.LocalDateTime;
import java.time.LocalTime;
import java.time.format.DateTimeFormatter;
import java.util.*;
2024-11-14 17:02:03 +08:00
import java.util.concurrent.TimeUnit;
2023-08-26 18:01:25 +08:00
import java.util.function.Function;
import java.util.stream.Collectors;
@Service
public class TempService {
private Logger logger = LoggerFactory.getLogger(this.getClass());
@Autowired
private OrderBasicInfoMapper orderBasicInfoMapper;
@Autowired
2024-01-06 15:13:50 +08:00
private OrderBasicInfoService orderBasicInfoService;
2023-08-26 18:01:25 +08:00
@Autowired
private ClearingWithdrawInfoService clearingWithdrawInfoService;
@Autowired
private OrderPayRecordService orderPayRecordService;
2023-08-26 18:01:25 +08:00
@Autowired
private AdapayCallbackRecordService adapayCallbackRecordService;
2025-07-24 14:55:56 +08:00
@Autowired
private MemberBasicInfoService memberBasicInfoService;
@Autowired
protected TransactionService transactionService;
@Autowired
protected RabbitTemplate rabbitTemplate;
2025-03-25 15:16:51 +08:00
@Autowired
protected MemberGroupService memberGroupService;
2023-08-26 18:01:25 +08:00
@Autowired
private WxpayRefundCallbackService wxpayRefundCallbackService;
@Autowired
2024-01-06 15:20:28 +08:00
private PileMerchantInfoService pileMerchantInfoService;
2023-08-26 18:01:25 +08:00
@Autowired
2024-01-06 15:20:28 +08:00
private SettleOrderReportService settleOrderReportService;
2023-08-26 18:01:25 +08:00
@Autowired
2023-11-29 11:55:33 +08:00
private AdapayMemberAccountService adapayMemberAccountService;
2023-08-26 18:01:25 +08:00
@Autowired
private AdapayService adapayService;
@Autowired
private MemberAdapayRecordService memberAdapayRecordService;
2024-11-07 15:02:52 +08:00
@Autowired
2025-03-26 20:32:12 +08:00
private PileMsgRecordService pileMsgRecordService;
2024-11-07 15:02:52 +08:00
2024-11-15 10:34:23 +08:00
@Autowired
private OrderDetailService orderDetailService;
2024-11-13 17:06:46 +08:00
@Autowired
2024-11-14 17:02:03 +08:00
private OrderMonitorDataService orderMonitorDataService;
2024-11-13 17:06:46 +08:00
@Autowired
2024-11-14 17:02:03 +08:00
private RedisCache redisCache;
2024-11-13 17:06:46 +08:00
2025-04-16 15:50:26 +08:00
@Autowired
private OrderUnsplitRecordService orderUnsplitRecordService;
@Autowired
private ThirdpartySecretInfoService thirdpartySecretInfoService;
@Autowired
private ThirdPartyStationRelationService thirdPartyStationRelationService;
@Autowired
private NotificationService notificationService;
2025-07-24 14:55:56 +08:00
@Autowired
private MemberWalletInfoService memberWalletInfoService;
2024-11-07 15:02:52 +08:00
/**
* 计算订单耗电量
* 内蒙古站点
*/
public void calculateOrderElectricity(QueryOrderDTO dto) {
// 根据站点id查询充电桩列表
// List<String> pileSnList = Lists.newArrayList("88240000006708", "88240000006709", "88240000006713", "88240000006714");
// 查询充电桩的订单列表
List<OrderListVO> orderListVOS = orderBasicInfoService.selectOrderBasicInfoList(dto);
logger.info("查询订单列表:{}", JSON.toJSONString(orderListVOS));
// 根据充电桩编号,查询报文
for (OrderListVO orderVO : orderListVOS) {
String pileSn = orderVO.getPileSn();
2025-03-26 20:32:12 +08:00
// List<PileMsgRecord> pileFeedList = pileMsgRecordMapper.getPileFeedList(pileSn);
2024-11-07 15:02:52 +08:00
}
//
}
2023-08-26 18:01:25 +08:00
/**
* 手动接口执行订单分账逻辑
*/
public void tempOrderSplittingOperations(String merchantId, String tradeDate) {
logger.info("手动接口执行订单分账逻辑-运营商:{}, 交易日期:{}, 进行分账处理start", merchantId, tradeDate);
// 查询运营商有没有开通结算账户
AdapayMemberAccount adapayMemberAccount = adapayMemberAccountService.selectByMerchantId(merchantId);
if (adapayMemberAccount == null) {
logger.error("手动接口执行订单分账逻辑-订单分账逻辑error, 运营商id:{}, 未配置结算账户", merchantId);
return;
}
String appId = pileMerchantInfoService.queryAppIdByMerchantId(merchantId);
// 根据交易日期查询运营商下面所有站点的交易日报
List<SettleOrderReport> stationReportList = settleOrderReportService.selectByMerchantIdAndDate(merchantId, tradeDate);
for (SettleOrderReport orderReport : stationReportList) {
String orderCodes = orderReport.getOrderCodes();
if (StringUtils.isBlank(orderCodes)) {
logger.info("手动接口执行订单分账逻辑-站点:{}, 日期:{}, 没有查到订单数据", orderReport.getStationId(), tradeDate);
continue;
}
List<String> orderCodeList = Lists.newArrayList(StringUtils.split(orderCodes, ","));
List<OrderBasicInfo> orderBasicInfos = orderBasicInfoService.queryOrderList(orderCodeList);
if (CollectionUtils.isEmpty(orderBasicInfos)) {
logger.info("手动接口执行订单分账逻辑-站点:{}, 日期:{}, 没有查到订单数据", orderReport.getStationId(), tradeDate);
continue;
}
// 执行分账
for (OrderBasicInfo orderBasicInfo : orderBasicInfos) {
try {
2024-09-24 16:39:42 +08:00
OrderSplitResult orderSettleResult = null;
2023-08-26 18:01:25 +08:00
logger.info("手动接口执行订单分账逻辑-orderCode:{}, payMode:{}", orderBasicInfo.getOrderCode(), orderBasicInfo.getPayMode());
2024-09-12 13:24:04 +08:00
if (OrderPayModeEnum.PAYMENT_OF_PRINCIPAL_BALANCE.getValue().equals(orderBasicInfo.getPayMode())) {
2023-08-26 18:01:25 +08:00
logger.info("手动接口执行订单分账逻辑-order:{}, result:{}", JSON.toJSONString(orderBasicInfo), JSON.toJSONString(orderSettleResult));
// 余额支付的订单 只用余额支付转账
// orderSettleResult = doBalancePayment(orderBasicInfo, adapayMemberAccount);
} else {
// 在线支付,进行支付确认分账
// orderSettleResult = orderBasicInfo.doPaymentConfirmWithDelay(orderBasicInfo, adapayMemberAccount, appId);
}
// if (orderSettleResult != null && AdapayStatusEnum.SUCCEEDED.getValue().equals(orderSettleResult.getStatus())) {
// JSONObject jsonObject = JSON.parseObject(orderSettleResult.getDescription());
// String orderCode = (String) jsonObject.get("orderCode");
// }
} catch (Exception e) {
logger.error("手动接口执行订单分账逻辑-订单交易确认失败:{}", orderBasicInfo.getOrderCode(), e);
}
}
}
logger.info("手动接口执行订单分账逻辑-运营商:{}, 交易日期:{}, 进行分账处理end", merchantId, tradeDate);
}
public String tempUpdateVirtualAmount(QueryOrderDTO dto) {
dto.setOrderStatus(OrderStatusEnum.ORDER_COMPLETE.getValue());
List<OrderListVO> orderListVOS = orderBasicInfoMapper.selectOrderBasicInfoList(dto);
if (CollectionUtils.isEmpty(orderListVOS)) {
return "没有订单需要修改";
}
int i = 0;
for (OrderListVO orderListVO : orderListVOS) {
try {
tempUpdateVirtualAmount(orderListVO);
i += 1;
} catch (Exception e) {
logger.error("修改虚拟金额字段失败", e);
}
}
return "共查询到" + orderListVOS.size() + "条订单,修改成功" + i + "条订单数据";
}
private void tempUpdateVirtualAmount(OrderListVO orderListVO) {
if (orderListVO.getVirtualAmount() != null) {
return;
}
// 订单总消费金额
BigDecimal orderAmount = new BigDecimal(orderListVO.getOrderAmount());
// 支付金额
BigDecimal payAmount = new BigDecimal(orderListVO.getPayAmount());
if (orderAmount.compareTo(payAmount) > 0) {
orderAmount = payAmount;
}
// 使用虚拟金额消费 金额
BigDecimal virtualAmount = BigDecimal.ZERO;
// 结算金额
BigDecimal settleAmount = BigDecimal.ZERO;
if (orderAmount.compareTo(BigDecimal.ZERO) > 0) {
2024-09-12 13:24:04 +08:00
if (StringUtils.equals(orderListVO.getPayMode(), OrderPayModeEnum.PAYMENT_OF_PRINCIPAL_BALANCE.getValue())) {
2023-08-26 18:01:25 +08:00
/*
余额支付 查询支付记录如全部用本金支付则虚拟金额为0结算金额为订单消费金额
如果使用了赠送金额虚拟金额为赠送金额支付部分结算金额=订单消费金额-虚拟金额消费部分
*/
// 查询支付记录
List<OrderPayRecord> orderPayRecordList = orderPayRecordService.getOrderPayRecordList(orderListVO.getOrderCode());
for (OrderPayRecord orderPayRecord : orderPayRecordList) {
if (StringUtils.equals(orderPayRecord.getPayMode(), OrderPayRecordEnum.GIFT_BALANCE_PAYMENT.getValue())) {
BigDecimal refundAmount = orderPayRecord.getRefundAmount();
if (refundAmount == null) {
// 退款金额为null, 需要退款的金额 = 支付金额 - 订单金额
refundAmount = orderPayRecord.getPayAmount().subtract(orderAmount);
}
// 赠送金额消费部分 = 支付金额 - 需要退款金额
virtualAmount = orderPayRecord.getPayAmount().subtract(refundAmount);
// 结算金额 = 订单金额 - 赠送金额消费部分
settleAmount = orderAmount.subtract(virtualAmount);
} else {
// 没有使用赠送金额支付那么虚拟金额就是0结算金额就等于订单金额
settleAmount = orderAmount;
}
}
} else {
/*
微信支付 虚拟金额为0 结算金额等于订单消费金额
*/
settleAmount = orderAmount;
}
}
OrderBasicInfo build = OrderBasicInfo.builder()
2024-01-14 15:55:53 +08:00
.id(Integer.parseInt(orderListVO.getId()))
2023-08-26 18:01:25 +08:00
.orderCode(orderListVO.getOrderCode())
.orderAmount(orderAmount)
.virtualAmount(virtualAmount)
.settleAmount(settleAmount)
.build();
orderBasicInfoService.updateOrderBasicInfo(build);
}
/**
* 临时订单退款
*/
public void tempOrderRefund() {
// 查询出2023-05-29 12:00:00到2023-05-30 23:59:59中使用微信支付并且有退款金额的订单
String startTime = "2023-05-29 12:00:00";
String endTime = "2023-05-30 23:59:59";
List<OrderBasicInfo> orderList = orderBasicInfoMapper.tempQueryWeChatRefundOrders(startTime, endTime);
Map<String, OrderBasicInfo> orderBasicInfoMap = orderList.stream().collect(Collectors.toMap(OrderBasicInfo::getOrderCode, Function.identity(), (k1, k2) -> k1));
Set<String> orderCodes = orderBasicInfoMap.keySet();
// 根据上面的订单号,查微信退款回调,找出没有记录的订单,重新发起一遍退款
List<WxpayRefundCallback> wxpayRefundCallbacks = wxpayRefundCallbackService.selectByOrderCodeList(Lists.newArrayList(orderCodes));
Set<String> refundOrders = wxpayRefundCallbacks.stream().map(WxpayRefundCallback::getOrderCode).collect(Collectors.toSet());
// orderCodeList refundOrders 取差集
Sets.SetView<String> difference = Sets.difference(orderCodes, refundOrders);
for (String orderCode : difference) {
OrderBasicInfo orderBasicInfo = orderBasicInfoMap.get(orderCode);
if (orderBasicInfo == null) {
continue;
}
BigDecimal refundAmount = orderBasicInfo.getRefundAmount();
// 微信退款逻辑
ApplyRefundDTO weChatRefundDTO = new ApplyRefundDTO();
weChatRefundDTO.setOrderCode(orderBasicInfo.getOrderCode());
weChatRefundDTO.setRefundType("1");
weChatRefundDTO.setRefundAmount(refundAmount);
try {
2024-08-13 14:05:48 +08:00
// orderBasicInfoService.weChatRefund(weChatRefundDTO);
2023-08-26 18:01:25 +08:00
} catch (Exception e) {
logger.error("临时订单退款接口发生异常 orderCode:{}", orderBasicInfo.getOrderCode(), e);
}
}
}
public void splitTheBillForOrderTemp(String merchantId, List<String> orderCodeList) {
String wechatAppId = pileMerchantInfoService.queryAppIdByMerchantId(merchantId);
if (StringUtils.isBlank(wechatAppId)) {
throw new BusinessException(ReturnCodeEnum.CODE_QUERY_WECHAT_APP_ID_IS_NULL);
}
AdapayMemberAccount adapayMemberAccount = adapayMemberAccountService.selectByMerchantId(merchantId);
if (adapayMemberAccount == null) {
throw new BusinessException(ReturnCodeEnum.CODE_ADAPAY_MEMBER_IS_NULL_ERROR);
}
List<OrderBasicInfo> orderBasicInfos = orderBasicInfoService.queryOrderList(orderCodeList);
for (OrderBasicInfo orderBasicInfo : orderBasicInfos) {
try {
splitTheBillForOrderTemp(adapayMemberAccount, orderBasicInfo, wechatAppId);
} catch (BaseAdaPayException e) {
logger.error("余额支付订单分账工具, 发生异常", e);
throw new RuntimeException(e);
}
}
}
public void splitTheBillForOrderTemp(AdapayMemberAccount adapayMemberAccount, OrderBasicInfo orderBasicInfo, String wechatAppId) throws BaseAdaPayException {
logger.info("");
// 查询会员余额支付记录
List<MemberAdapayRecord> recordList = memberAdapayRecordService.selectAdapayRecordList(orderBasicInfo.getMemberId(), ScenarioEnum.BALANCE.getValue());
if (CollectionUtils.isEmpty(recordList)) {
logger.info("余额支付订单分账工具, memberId:{}, 未查询到余额充值记录", orderBasicInfo.getMemberId());
return;
}
String orderCode = orderBasicInfo.getOrderCode();
if (recordList.size() > 1) {
logger.info("余额支付订单分账工具, 会员充值记录存在多条orderCode:{}由人工手动处理", orderCode);
return;
}
// 订单分账金额
BigDecimal amount = queryBalanceOrderSettleInfo(orderCode, orderBasicInfo.getMemberId(), wechatAppId);
// 判断分账金额 和结算金额是否相等
if (amount.compareTo(BigDecimal.ZERO) == 0) {
logger.info("余额支付订单分账工具, orderCode:{}, 已分账金额为0, 订单结算金额为:{}, 重试执行订单分账", orderCode, orderBasicInfo.getSettleAmount());
// 修改会员的汇付支付记录
for (MemberAdapayRecord memberAdapayRecord : recordList) {
BigDecimal spendAmt = memberAdapayRecord.getSpendAmt();
spendAmt = spendAmt.subtract(orderBasicInfo.getOrderAmount());
memberAdapayRecord.setSpendAmt(spendAmt);
BigDecimal balanceAmt = memberAdapayRecord.getBalanceAmt();
balanceAmt = balanceAmt.add(orderBasicInfo.getOrderAmount());
memberAdapayRecord.setBalanceAmt(balanceAmt);
memberAdapayRecordService.updateByPrimaryKeySelective(memberAdapayRecord);
}
// 分账金额为0表示该订单没有执行过分账再次执行分账
2024-09-12 13:24:04 +08:00
if (OrderPayModeEnum.PAYMENT_OF_PRINCIPAL_BALANCE.getValue().equals(orderBasicInfo.getPayMode())) {
2023-08-26 18:01:25 +08:00
// 余额支付的订单
orderBasicInfoService.doBalancePaymentWithDelay(orderBasicInfo, adapayMemberAccount, wechatAppId);
}
// 再次查询分账
BigDecimal bigDecimal = queryBalanceOrderSettleInfo(orderCode, orderBasicInfo.getMemberId(), wechatAppId);
logger.info("余额支付订单分账工具, orderCode:{}, 重新分账完成, 分账金额:{}, 订单结算金额:{}", orderCode, bigDecimal, orderBasicInfo.getSettleAmount());
} else {
logger.info("余额支付订单分账工具, orderCode:{}, 已分账金额为:{}, 订单结算金额为:{}, 不再执行订单分账", orderCode, amount, orderBasicInfo.getSettleAmount());
}
}
2023-09-26 15:41:05 +08:00
/**
* 查询余额订单 分账信息
* @param orderCode
* @param memberId
* @param wechatAppId
* @return
*/
2023-08-26 18:01:25 +08:00
private BigDecimal queryBalanceOrderSettleInfo(String orderCode, String memberId, String wechatAppId) {
BigDecimal resultAmt = BigDecimal.ZERO;
// 查询会员余额支付记录
List<MemberAdapayRecord> recordList = memberAdapayRecordService.selectAdapayRecordList(memberId, ScenarioEnum.BALANCE.getValue());
if (CollectionUtils.isEmpty(recordList)) {
return resultAmt;
}
// 用汇付api查询分账信息
for (MemberAdapayRecord record : recordList) {
QueryPaymentConfirmDTO dto = new QueryPaymentConfirmDTO();
dto.setWechatAppId(wechatAppId);
dto.setPaymentId(record.getPaymentId());
QueryPaymentConfirmDetailResponse response = adapayService.queryPaymentConfirmList(dto);
if (response != null && CollectionUtils.isNotEmpty(response.getPaymentConfirms())) {
2024-12-26 10:04:39 +08:00
for (PaymentConfirmInfo paymentConfirm : response.getPaymentConfirms()) {
2023-08-26 18:01:25 +08:00
JSONObject jsonObject = JSON.parseObject(paymentConfirm.getDescription());
if (StringUtils.equals(jsonObject.getString("orderCode"), orderCode)) {
// 订单号对的上,累计分账金额
resultAmt = resultAmt.add(new BigDecimal(paymentConfirm.getConfirmAmt()));
}
}
}
}
return resultAmt;
}
2023-09-26 15:41:05 +08:00
/**
* 查询未分账订单
* @param dto
* @return
*/
2023-08-26 18:01:25 +08:00
public Map<String, Object> queryUndividedOrder(SettleOrderReportDTO dto) {
Map<String, Object> resultMap = Maps.newHashMap();
SettleOrderReport orderReport = settleOrderReportService.selectByStationIdAndDate(dto.getStationId(), dto.getTradeDate());
if (orderReport == null) {
throw new BusinessException("00300002", "查询站点订单日报为空");
}
String orderCodes = orderReport.getOrderCodes();
List<String> orderCodeList = Lists.newArrayList(StringUtils.split(orderCodes, ","));
List<ClearingBillVO> clearingBillVOList = clearingWithdrawInfoService.selectWithdrawInfoByOrderCodeList(orderCodeList);
if (org.springframework.util.CollectionUtils.isEmpty(clearingBillVOList)) {
throw new BusinessException("00300005", "查询清分信息为空");
}
String wechatAppId = pileMerchantInfoService.queryAppIdByMerchantId(orderReport.getMerchantId());
// clearing
List<ClearingBillVO> clearingList = Lists.newArrayList();
List<ClearingBillVO> unClearingList = Lists.newArrayList();
for (ClearingBillVO clearingBillVO : clearingBillVOList) {
String paymentId = clearingBillVO.getPaymentId();
QueryPaymentConfirmDTO build = QueryPaymentConfirmDTO.builder()
.wechatAppId(wechatAppId)
.paymentId(paymentId)
.build();
QueryPaymentConfirmDetailResponse response = adapayService.queryPaymentConfirmList(build);
2024-12-26 10:04:39 +08:00
List<PaymentConfirmInfo> paymentConfirms = response.getPaymentConfirms();
2023-09-19 10:38:48 +08:00
if (CollectionUtils.isEmpty(paymentConfirms)) {
unClearingList.add(clearingBillVO);
} else {
2023-08-26 18:01:25 +08:00
clearingBillVO.setConfirmInfo(paymentConfirms.get(0));
clearingList.add(clearingBillVO);
}
}
resultMap.put("clearingList", clearingList);
resultMap.put("unClearingList", unClearingList);
2023-09-19 10:38:48 +08:00
resultMap.put("orderSize", clearingBillVOList.size());
2023-08-26 18:01:25 +08:00
return resultMap;
}
2023-08-31 17:31:19 +08:00
public void batchRefundOrder(List<String> orderCodeList) throws BaseAdaPayException {
List<OrderBasicInfo> orderBasicInfos = orderBasicInfoService.queryOrderList(orderCodeList);
for (OrderBasicInfo orderBasicInfo : orderBasicInfos) {
String payMode = orderBasicInfo.getPayMode();
if (StringUtils.equals(payMode, OrderPayModeEnum.PAYMENT_OF_WECHATPAY.getValue())) {
orderBasicInfoService.retryRefundOrder(orderBasicInfo);
} else {
orderBasicInfoService.orderPaymentSettlementAndRefund(orderBasicInfo);
}
// logger.info("批量进行问题订单退款orderCode:{}, result:{}", orderBasicInfo.getOrderCode(), JSON.toJSONString(result));
}
}
2023-09-05 16:42:33 +08:00
public void updateMemberAdapayRecord(String memberId) {
// 查询
List<MemberAdapayRecord> memberAdapayRecordList = memberAdapayRecordService.selectAdapayRecordList(memberId, ScenarioEnum.BALANCE.getValue());
}
2023-09-26 11:12:54 +08:00
2023-09-26 15:41:05 +08:00
/**
* 余额支付分账
* @param orderBasicInfo
* @param adapayMemberAccount
* @param wechatAppId
*/
public void doBalancePaymentTemp(OrderBasicInfo orderBasicInfo, AdapayMemberAccount adapayMemberAccount, String wechatAppId) throws BaseAdaPayException {
2023-09-26 11:12:54 +08:00
String orderCode = orderBasicInfo.getOrderCode();
// 查询订单支付记录
List<OrderPayRecord> orderPayRecordList = orderPayRecordService.getOrderPayRecordList(orderCode);
2023-09-26 15:56:36 +08:00
logger.info("doBalancePaymentTemp-查询订单:{}支付记录:{}", orderCode, JSON.toJSONString(orderPayRecordList));
2023-09-26 11:12:54 +08:00
if (CollectionUtils.isEmpty(orderPayRecordList)) {
return;
}
2023-09-27 10:57:36 +08:00
BigDecimal settleAmount = orderBasicInfo.getSettleAmount();
2023-09-26 11:12:54 +08:00
OrderPayRecord record = orderPayRecordList.get(0);
String deductionRecord = record.getDeductionRecord();
List<PaymentInfo> paymentInfos = orderPayRecordService.parseDeductionRecord(deductionRecord);
2023-09-26 11:12:54 +08:00
2023-09-26 15:41:05 +08:00
// 通过paymentId查询 分账记录
for (PaymentInfo paymentInfo : paymentInfos) {
QueryPaymentConfirmDTO dto = new QueryPaymentConfirmDTO();
String paymentId = paymentInfo.getPaymentId();
dto.setPaymentId(paymentId);
dto.setWechatAppId(wechatAppId);
QueryPaymentConfirmDetailResponse response = adapayService.queryPaymentConfirmList(dto);
if (response == null) {
continue;
}
2024-12-26 10:04:39 +08:00
List<PaymentConfirmInfo> confirms = response.getPaymentConfirms();
2023-09-26 15:41:05 +08:00
if (CollectionUtils.isNotEmpty(confirms)) {
2023-09-27 10:57:36 +08:00
// 已经分过账
2024-12-26 10:04:39 +08:00
for (PaymentConfirmInfo confirm : confirms) {
2023-09-26 15:41:05 +08:00
JSONObject jsonObject = JSON.parseObject(confirm.getDescription());
2023-09-26 11:12:54 +08:00
2023-09-26 15:41:05 +08:00
// 找到orderCode的分账记录
if (StringUtils.equals(jsonObject.getString("orderCode"), orderCode)) {
// 分账金额
BigDecimal confirmAmt = new BigDecimal(confirm.getConfirmAmt());
// 校验是不是分给正确的商户
String adapayMemberId = jsonObject.getString("adapayMemberId");
if (!StringUtils.equals(adapayMemberId, adapayMemberAccount.getAdapayMemberId())) {
// 撤销原分账
adapayService.createConfirmReverse(confirm.getId(), wechatAppId);
// 重新分账
adapayService.createPaymentConfirmRequest(paymentId, adapayMemberAccount, confirmAmt, orderCode, wechatAppId);
}
}
}
2023-09-27 10:57:36 +08:00
} else {
// 未分账情况
adapayService.createPaymentConfirmRequest(paymentId, adapayMemberAccount, settleAmount, orderCode, wechatAppId);
2023-09-26 15:41:05 +08:00
}
}
2023-09-26 11:12:54 +08:00
}
2023-10-13 15:33:13 +08:00
public void verifyUndividedPayment(QueryOrderDTO dto) {
String wechatAppId = "wxbb3e0d474569481d";
String startTime = dto.getStartTime();
String endTime = dto.getEndTime();
2023-10-13 15:33:13 +08:00
// 查询时间段的订单
// List<AdapayCallbackRecord> list = adapayCallbackRecordService.selectByDateTime(startTime, endTime);
2024-08-06 15:46:43 +08:00
List<OrderPaymentDetailVO> orderPayDetailVOS = orderBasicInfoService.queryOrderPayDetail(startTime, endTime);
logger.info("校验未分账的支付单-在 {} - {} 期间,共查询到{}条支付记录", startTime, endTime, orderPayDetailVOS.size());
2023-10-13 15:33:13 +08:00
// 查询支付单 分账情况
2024-08-06 15:46:43 +08:00
for (OrderPaymentDetailVO record : orderPayDetailVOS) {
String paymentId = record.getPaymentId();
BigDecimal confirmAmt = record.getSettleAmount();
String orderCode = record.getOrderCode();
QueryPaymentConfirmDTO queryPaymentConfirmDTO = new QueryPaymentConfirmDTO();
queryPaymentConfirmDTO.setWechatAppId(wechatAppId);
queryPaymentConfirmDTO.setPaymentId(paymentId);
QueryPaymentConfirmDetailResponse response = adapayService.queryPaymentConfirmList(queryPaymentConfirmDTO);
logger.info("校验未分账的支付单-支付id:{}, 查询到的分账信息:{}", paymentId, JSON.toJSONString(response));
2024-12-26 10:04:39 +08:00
List<PaymentConfirmInfo> confirms = response.getPaymentConfirms();
if (CollectionUtils.isEmpty(confirms)) {
logger.info("校验未分账的支付单-支付id:{}没有进行过分账", paymentId);
// 没有配置结算账户的,默认分到本商户
AdapayMemberAccount defaultAccount = adapayMemberAccountService.getDefault();
PaymentConfirmResponse paymentConfirmRequest = adapayService.createPaymentConfirmRequest(paymentId, defaultAccount, confirmAmt, orderCode, wechatAppId);
logger.info("校验未分账的支付单-支付id:{}, 执行分账结果:{}", paymentId, JSON.toJSONString(paymentConfirmRequest));
}
}
2023-10-24 13:14:33 +08:00
}
2023-10-24 13:14:33 +08:00
/**
* 检查支付单
* @param dto
*/
public void checkPayment(QueryOrderDTO dto) throws BaseAdaPayException {
// 根据时间段查询支付对象列表
2023-10-24 14:49:49 +08:00
List<AdaPayment> adaPayments = adapayService.queryPaymentList(dto.getStartTime(), dto.getEndTime());
if (CollectionUtils.isEmpty(adaPayments)) {
return;
}
2023-10-24 15:33:58 +08:00
List<String> paymentIdList = Lists.newArrayList();
BigDecimal total = BigDecimal.ZERO;
2023-10-26 16:54:50 +08:00
AdapayMemberAccount aDefault = adapayMemberAccountService.getDefault();
String wechatAppId = "wxbb3e0d474569481d";
2023-10-24 14:49:49 +08:00
for (AdaPayment adaPayment : adaPayments) {
2023-10-26 16:54:50 +08:00
String paymentId = adaPayment.getId();
2023-10-24 14:49:49 +08:00
String pay_mode = adaPayment.getPay_mode();
if (StringUtils.isBlank(pay_mode)) {
2023-10-24 15:33:58 +08:00
// 实时分账的不检查
continue;
}
String status = adaPayment.getStatus();
if (!StringUtils.equals(status, AdapayStatusEnum.SUCCEEDED.getValue())) {
// 不是交易成功的不检查
continue;
}
2023-10-26 16:54:50 +08:00
if (StringUtils.equals(paymentId, "002212023102509474610563069333816762368")) {
continue;
}
2023-10-24 15:33:58 +08:00
// 交易金额
BigDecimal payAmt = new BigDecimal(adaPayment.getPay_amt());
// 若为延时分账时,已发起支付撤销的总金额
BigDecimal reservedAmt = new BigDecimal(adaPayment.getReserved_amt());
// 若为延时分账时,已发起支付确认的总金额
BigDecimal confirmedAmt = new BigDecimal(adaPayment.getConfirmed_amt());
BigDecimal subtract = payAmt.subtract(reservedAmt).subtract(confirmedAmt);
2023-10-24 14:49:49 +08:00
2023-10-24 15:33:58 +08:00
if (subtract.compareTo(BigDecimal.ZERO) > 0) {
2023-10-26 16:54:50 +08:00
paymentIdList.add(paymentId);
2023-10-24 15:33:58 +08:00
total = total.add(subtract);
logger.info("支付单:{}, 支付金额:{}, 总撤销金额:{}, 总确认金额:{}, 剩余金额:{}",
2023-10-26 16:54:50 +08:00
paymentId, payAmt, reservedAmt, confirmedAmt, subtract);
// adapayService.createPaymentConfirmRequest(paymentId, aDefault, subtract, null, wechatAppId);
2023-10-24 14:49:49 +08:00
}
}
2023-10-24 15:33:58 +08:00
logger.info("{} - {} 期间,共有{}笔支付单存在剩余金额, 共计:{}list:{}", dto.getStartTime(), dto.getEndTime(), paymentIdList.size(), total, JSON.toJSONString(paymentIdList));
2023-10-13 15:33:13 +08:00
}
2024-11-13 17:06:46 +08:00
/**
* 校验是否为并充订单
*/
2024-11-21 16:01:26 +08:00
@CostTime
2024-11-13 17:06:46 +08:00
public Map<String, List<String>> checkCombinedChargingOrder(List<String> orderCodeList) throws BaseAdaPayException {
Map<String, List<String>> resultMap = Maps.newHashMap();
2024-11-13 17:10:47 +08:00
List<String> combinedChargingOrderList = Lists.newArrayList();
List<String> notCombinedChargingOrderList = Lists.newArrayList();
List<String> noDataOrderList = Lists.newArrayList();
2024-11-13 17:06:46 +08:00
for (String orderCode : orderCodeList) {
// 查询orderMonitorData
OrderMonitorData orderMonitorData = orderMonitorDataService.selectByOrderCode(orderCode);
if (orderMonitorData == null) {
logger.info("订单:{}不存在", orderCode);
2024-11-13 17:10:47 +08:00
noDataOrderList.add(orderCode);
2024-11-13 17:06:46 +08:00
continue;
}
JSONArray jsonArray = JSONArray.parseArray(orderMonitorData.getMonitorData());
2024-11-13 17:10:47 +08:00
List<BigDecimal> orderOutputCurrentList = Lists.newArrayList();
2024-11-13 17:06:46 +08:00
for (int i = 0; i < jsonArray.size(); i++) {
JSONObject jsonObject = jsonArray.getJSONObject(i);
String outputCurrent = jsonObject.getString("outputCurrent");
2024-11-13 17:10:47 +08:00
orderOutputCurrentList.add(new BigDecimal(outputCurrent));
}
// 如果orderOutputCurrentList中任意一值大于250判定为并充订单
if (orderOutputCurrentList.stream().anyMatch(current -> current.compareTo(new BigDecimal("250")) > 0)) {
combinedChargingOrderList.add(orderCode);
} else {
2024-11-13 17:06:46 +08:00
notCombinedChargingOrderList.add(orderCode);
}
}
2024-11-13 17:10:47 +08:00
resultMap.put("combinedChargingOrderList", combinedChargingOrderList);
resultMap.put("notCombinedChargingOrderList", notCombinedChargingOrderList);
resultMap.put("noDataOrderList", noDataOrderList);
2024-11-13 17:06:46 +08:00
return resultMap;
}
2024-11-14 17:02:03 +08:00
/**
* 修正并充订单数据
* @param dto
*/
2024-11-15 10:34:23 +08:00
@Transactional(rollbackFor = Exception.class)
2024-11-15 10:59:42 +08:00
public List<String> correctCombinedChargingOrder(QueryOrderDTO dto) {
2024-11-14 17:02:03 +08:00
List<String> orderCodeList = dto.getOrderCodeList();
2024-11-15 10:34:23 +08:00
logger.info("修正并充订单数据工具,订单号:{}", JSON.toJSONString(orderCodeList));
2024-11-14 17:02:03 +08:00
// 查询临时表,不要重复修正
String redisKey = "correct_combined_charging_order";
List<String> redisResult = redisCache.getCacheList(redisKey);
if (CollectionUtils.isNotEmpty(redisResult)) {
2024-11-15 10:34:23 +08:00
logger.info("修正并充订单数据工具, 已修正订单:{}", JSON.toJSONString(redisResult));
2024-11-14 17:02:03 +08:00
// 过滤掉已修正的订单
orderCodeList.removeAll(redisResult);
2024-11-15 10:34:23 +08:00
logger.info("修正并充订单数据工具. 过滤掉已修正的订单剩余:{}", JSON.toJSONString(orderCodeList));
2024-11-14 17:02:03 +08:00
}
if (CollectionUtils.isEmpty(orderCodeList)) {
2024-11-15 10:34:23 +08:00
logger.info("修正并充订单数据工具, 无需要修正的订单");
2024-11-15 10:59:42 +08:00
return Lists.newArrayList();
2024-11-14 17:02:03 +08:00
}
2024-12-20 13:25:22 +08:00
BigDecimal multiple = new BigDecimal("0.5");
2024-11-14 17:02:03 +08:00
// 查询订单主表数据
List<OrderBasicInfo> orderBasicInfos = orderBasicInfoService.queryOrderList(orderCodeList);
2024-11-15 10:34:23 +08:00
for (OrderBasicInfo orderBasicInfo : orderBasicInfos) {
orderBasicInfo.setOrderAmount(orderBasicInfo.getOrderAmount().multiply(multiple));
2024-11-15 10:59:42 +08:00
orderBasicInfo.setVirtualAmount(orderBasicInfo.getVirtualAmount().multiply(multiple));
2024-11-15 13:11:21 +08:00
orderBasicInfo.setUpdateTime(DateUtils.getNowDate());
2024-11-15 10:34:23 +08:00
}
2024-11-14 17:02:03 +08:00
// 查询订单详情数据
List<OrderDetail> orderDetailList = orderBasicInfoService.getOrderDetailList(orderCodeList);
2024-11-15 10:34:23 +08:00
for (OrderDetail orderDetail : orderDetailList) {
// 订单总用电量
orderDetail.setTotalUsedElectricity(orderDetail.getTotalUsedElectricity().multiply(multiple));
// 订单总电费金额
orderDetail.setTotalElectricityAmount(orderDetail.getTotalElectricityAmount().multiply(multiple));
// 订单总服务费金额
orderDetail.setTotalServiceAmount(orderDetail.getTotalServiceAmount().multiply(multiple));
// 订单总金额
orderDetail.setTotalOrderAmount(orderDetail.getTotalOrderAmount().multiply(multiple));
// 尖时段
if (orderDetail.getSharpUsedElectricity() != null && orderDetail.getSharpUsedElectricity().compareTo(BigDecimal.ZERO) > 0) {
orderDetail.setSharpUsedElectricity(orderDetail.getSharpUsedElectricity().multiply(multiple));
2024-11-15 13:11:21 +08:00
// orderDetail.setSharpAmount(orderDetail.getSharpAmount().multiply(multiple));
orderDetail.setSharpAmount(orderDetail.getSharpPrice().multiply(orderDetail.getSharpUsedElectricity()));
2024-11-15 10:34:23 +08:00
}
// 峰时段
if (orderDetail.getPeakUsedElectricity() != null && orderDetail.getPeakUsedElectricity().compareTo(BigDecimal.ZERO) > 0) {
orderDetail.setPeakUsedElectricity(orderDetail.getPeakUsedElectricity().multiply(multiple));
2024-11-15 13:11:21 +08:00
// orderDetail.setPeakAmount(orderDetail.getPeakAmount().multiply(multiple));
orderDetail.setPeakAmount(orderDetail.getPeakPrice().multiply(orderDetail.getPeakUsedElectricity()));
2024-11-15 10:34:23 +08:00
}
// 平时段
if (orderDetail.getFlatUsedElectricity() != null && orderDetail.getFlatUsedElectricity().compareTo(BigDecimal.ZERO) > 0) {
orderDetail.setFlatUsedElectricity(orderDetail.getFlatUsedElectricity().multiply(multiple));
2024-11-15 13:11:21 +08:00
// orderDetail.setFlatAmount(orderDetail.getFlatAmount().multiply(multiple));
orderDetail.setFlatAmount(orderDetail.getFlatPrice().multiply(orderDetail.getFlatUsedElectricity()));
2024-11-15 10:34:23 +08:00
}
// 谷时段
if (orderDetail.getValleyUsedElectricity() != null && orderDetail.getValleyUsedElectricity().compareTo(BigDecimal.ZERO) > 0) {
orderDetail.setValleyUsedElectricity(orderDetail.getValleyUsedElectricity().multiply(multiple));
2024-11-15 13:11:21 +08:00
// orderDetail.setValleyAmount(orderDetail.getValleyAmount().multiply(multiple));
orderDetail.setValleyAmount(orderDetail.getValleyPrice().multiply(orderDetail.getValleyUsedElectricity()));
2024-11-15 10:34:23 +08:00
}
2024-11-15 13:11:21 +08:00
orderDetail.setUpdateTime(DateUtils.getNowDate());
2024-11-15 10:34:23 +08:00
}
// 更新数据库
2024-11-15 11:07:10 +08:00
int updateCount = 0;
if (CollectionUtils.isNotEmpty(orderBasicInfos)) {
updateCount = orderBasicInfoService.updateBatch(orderBasicInfos);
}
if (CollectionUtils.isNotEmpty(orderDetailList)) {
orderDetailService.updateBatch(orderDetailList);
}
2024-11-15 10:34:23 +08:00
2024-11-15 11:07:10 +08:00
if (orderCodeList.size() == updateCount) {
logger.info("修正并充订单数据工具, 修正成功, 订单号:{}", JSON.toJSONString(orderCodeList));
Map<String, List<String>> redisMap = Maps.newHashMap();
redisMap.put(redisKey, orderCodeList);
redisCache.batchSetCacheList(redisMap, 300, TimeUnit.DAYS);
}
2024-11-15 10:59:42 +08:00
return orderCodeList;
2024-11-14 17:02:03 +08:00
}
2024-12-31 09:47:45 +08:00
/**
* 关闭未支付订单
*/
public void closeUnpaidOrder(QueryOrderDTO dto) {
// 查询未支付订单
List<OrderBasicInfo> unpaidOrderList = orderBasicInfoService.getUnpaidOrderList(dto.getStartTime(), dto.getEndTime());
logger.info("closeUnpaidOrders, startTime:{}, endTime:{}, 未支付订单:{}", dto.getStartTime(), dto.getEndTime(), JSON.toJSONString(unpaidOrderList));
// 关闭未支付订单
orderBasicInfoService.closeUnpaidOrders(unpaidOrderList);
}
2025-03-25 15:16:51 +08:00
public void calculateOrderDiscountsV2(OrderBasicInfo orderBasicInfo, OrderDetail orderDetail) {
2025-03-25 15:44:30 +08:00
String orderCode = orderBasicInfo.getOrderCode();
String transactionCode = orderBasicInfo.getTransactionCode();
2025-03-25 15:16:51 +08:00
String memberId = orderBasicInfo.getMemberId(); // 会员id
String stationId = orderBasicInfo.getStationId(); // 站点id
String merchantId = orderBasicInfo.getMerchantId(); // 运营商id
if (orderDetail == null) {
2025-03-25 15:44:30 +08:00
logger.info("计算订单折扣V2, OrderDetail为空(orderCode:{}, transactionCode:{}), 直接返回!", orderCode, transactionCode);
2025-03-25 15:16:51 +08:00
return;
}
// 原始电费金额
BigDecimal originalTotalElectricityAmount = orderDetail.getTotalElectricityAmount();
// 原始服务费金额
BigDecimal originalTotalServiceAmount = orderDetail.getTotalServiceAmount();
2025-03-25 15:44:30 +08:00
logger.info("计算订单折扣V2, orderCode:{}, 原始电费金额:{}, 原始服务费金额:{}", orderCode, originalTotalElectricityAmount, originalTotalServiceAmount);
2025-03-25 15:16:51 +08:00
// 电费折扣金额 初始值为0
BigDecimal electricityAmountDiscount = BigDecimal.ZERO;
// 服务费折扣金额 初始值为0
BigDecimal serviceAmountDiscount = BigDecimal.ZERO;
// 优惠后电费金额 初始值为优惠前金额
BigDecimal afterDiscountElectricityAmount = BigDecimal.ZERO;
// 优惠后服务费金额 初始值为优惠前金额
BigDecimal afterDiscountServiceAmount = BigDecimal.ZERO;
// 查询会员在该站点享受的优惠
MemberDiscountVO memberDiscountVO = memberGroupService.queryMemberDiscountV2(merchantId, stationId, memberId);
if (memberDiscountVO == null) {
// 如果没有优惠,则优惠后金额 等于优惠前金额
afterDiscountServiceAmount = orderDetail.getTotalServiceAmount();
afterDiscountElectricityAmount = orderDetail.getTotalElectricityAmount();
} else {
// 集团会员的订单,记录集团编号
orderBasicInfo.setGroupCode(memberDiscountVO.getGroupCode());
// 集团会员的优惠计费模板
BillingTemplateVO billingTemplateVO = memberDiscountVO.getBillingTemplateVO();
// 尖时段用电量
BigDecimal sharpUsedElectricity = orderDetail.getSharpUsedElectricity();
if (sharpUsedElectricity != null) {
// 计算该时段电费 = 电费单价 x 用电量
BigDecimal sharpElectricityAmount = billingTemplateVO.getSharpElectricityPrice()
.multiply(sharpUsedElectricity)
.setScale(2, RoundingMode.DOWN);
// 计算该时段服务费
BigDecimal sharpServiceAmount = billingTemplateVO.getSharpServicePrice()
.multiply(sharpUsedElectricity)
.setScale(2, RoundingMode.DOWN);
// 汇总
afterDiscountElectricityAmount = afterDiscountElectricityAmount.add(sharpElectricityAmount);
afterDiscountServiceAmount = afterDiscountServiceAmount.add(sharpServiceAmount);
2025-03-25 15:52:56 +08:00
logger.info("计算订单折扣V2, orderCode:{}, 尖时段用电量:{}, 电费单价:{}, 电费金额:{}, 服务费单价:{}, 服务费金额:{}", orderCode, sharpUsedElectricity,
2025-03-25 15:44:30 +08:00
billingTemplateVO.getSharpElectricityPrice(), sharpElectricityAmount, billingTemplateVO.getSharpServicePrice(), sharpServiceAmount);
2025-03-25 15:16:51 +08:00
}
// 峰时段用电量
BigDecimal peakUsedElectricity = orderDetail.getPeakUsedElectricity();
if (peakUsedElectricity != null) {
// 计算该时段电费 = 电费单价 x 用电量
BigDecimal peakElectricityAmount = billingTemplateVO.getPeakElectricityPrice()
.multiply(peakUsedElectricity)
.setScale(2, RoundingMode.DOWN);
// 计算该时段服务费
BigDecimal peakServiceAmount = billingTemplateVO.getPeakServicePrice()
.multiply(peakUsedElectricity)
.setScale(2, RoundingMode.DOWN);
// 汇总
afterDiscountElectricityAmount = afterDiscountElectricityAmount.add(peakElectricityAmount);
afterDiscountServiceAmount = afterDiscountServiceAmount.add(peakServiceAmount);
2025-03-25 15:52:56 +08:00
logger.info("计算订单折扣V2, orderCode:{}, 峰时段用电量:{}, 电费单价:{}, 电费金额:{}, 服务费单价:{}, 服务费金额:{}", orderCode, peakUsedElectricity,
2025-03-25 15:44:30 +08:00
billingTemplateVO.getPeakElectricityPrice(), peakElectricityAmount, billingTemplateVO.getPeakServicePrice(), peakServiceAmount);
2025-03-25 15:16:51 +08:00
}
// 平时段用电量
BigDecimal flatUsedElectricity = orderDetail.getFlatUsedElectricity();
if (flatUsedElectricity != null) {
// 计算该时段电费 = 电费单价 x 用电量
BigDecimal flatElectricityAmount = billingTemplateVO.getFlatElectricityPrice()
.multiply(flatUsedElectricity)
.setScale(2, RoundingMode.DOWN);
// 计算该时段服务费
BigDecimal flatServiceAmount = billingTemplateVO.getFlatServicePrice()
.multiply(flatUsedElectricity)
.setScale(2, RoundingMode.DOWN);
// 汇总
afterDiscountElectricityAmount = afterDiscountElectricityAmount.add(flatElectricityAmount);
afterDiscountServiceAmount = afterDiscountServiceAmount.add(flatServiceAmount);
2025-03-25 15:52:56 +08:00
logger.info("计算订单折扣V2, orderCode:{}, 平时段用电量:{}, 电费单价:{}, 电费金额:{}, 服务费单价:{}, 服务费金额:{}", orderCode, flatUsedElectricity,
2025-03-25 15:44:30 +08:00
billingTemplateVO.getFlatElectricityPrice(), flatElectricityAmount, billingTemplateVO.getFlatServicePrice(), flatServiceAmount);
2025-03-25 15:16:51 +08:00
}
// 谷时段用电量
BigDecimal valleyUsedElectricity = orderDetail.getValleyUsedElectricity();
if (valleyUsedElectricity != null) {
// 计算该时段电费 = 电费单价 x 用电量
BigDecimal valleyElectricityAmount = billingTemplateVO.getValleyElectricityPrice()
.multiply(valleyUsedElectricity)
.setScale(2, RoundingMode.DOWN);
// 计算该时段服务费
BigDecimal valleyServiceAmount = billingTemplateVO.getValleyServicePrice()
.multiply(valleyUsedElectricity)
.setScale(2, RoundingMode.DOWN);
// 汇总
afterDiscountElectricityAmount = afterDiscountElectricityAmount.add(valleyElectricityAmount);
afterDiscountServiceAmount = afterDiscountServiceAmount.add(valleyServiceAmount);
2025-03-25 15:52:56 +08:00
logger.info("计算订单折扣V2, orderCode:{}, 谷时段用电量:{}, 电费单价:{}, 电费金额:{}, 服务费单价:{}, 服务费金额:{}", orderCode, valleyUsedElectricity,
2025-03-25 15:44:30 +08:00
billingTemplateVO.getValleyElectricityPrice(), valleyElectricityAmount, billingTemplateVO.getValleyServicePrice(), valleyServiceAmount);
2025-03-25 15:16:51 +08:00
}
// 计算优惠了多少钱
// 电费折扣金额 = 优惠前电费 - 优惠后电费
electricityAmountDiscount = originalTotalElectricityAmount.subtract(afterDiscountElectricityAmount);
// 服务费折扣金额 = 优惠前服务费 - 优惠后服务费
serviceAmountDiscount = originalTotalServiceAmount.subtract(afterDiscountServiceAmount);
}
/*
更新 数据
*/
// 订单折扣金额 = 电费折扣金额 + 服务费折扣金额
BigDecimal discountAmount = electricityAmountDiscount.add(serviceAmountDiscount);
orderBasicInfo.setDiscountAmount(discountAmount);
// 更新结算金额 结算金额 = 消费金额 - 虚拟金额 - 优惠金额
BigDecimal newSettleAmount = orderBasicInfo.getSettleAmount().subtract(discountAmount);
if (newSettleAmount.compareTo(BigDecimal.ZERO) < 0) {
// 如果结算金额为0减去优惠金额后为负数还设置为0
newSettleAmount = BigDecimal.ZERO;
}
orderBasicInfo.setSettleAmount(newSettleAmount);
2025-05-16 14:08:48 +08:00
orderBasicInfo.setActualReceivedAmount(newSettleAmount);
2025-03-25 15:16:51 +08:00
// 优惠后总消费金额 = 折扣后电费 + 折扣后服务费
BigDecimal totalConsumeAmount = afterDiscountServiceAmount.add(afterDiscountElectricityAmount);
// 更新退款金额 = 支付金额 - 优惠后总消费金额
BigDecimal refundAmount = orderBasicInfo.getPayAmount().subtract(totalConsumeAmount).setScale(2, RoundingMode.DOWN);
orderBasicInfo.setRefundAmount(refundAmount);
// 电费折扣金额(电费便宜了多少钱)
orderDetail.setDiscountElectricityAmount(electricityAmountDiscount);
// 服务费折扣金额(服务费便宜了多少钱)
orderDetail.setDiscountServiceAmount(serviceAmountDiscount);
logger.info("计算订单折扣, orderCode:{}, memberId:{}, 订单折扣金额:{}, 电费折扣金额:{}, 服务费折扣金额:{}, 优惠后总消费金额:{}",
2025-03-25 15:44:30 +08:00
orderCode, memberId, discountAmount, electricityAmountDiscount, serviceAmountDiscount, totalConsumeAmount);
2025-03-25 15:16:51 +08:00
}
2025-03-26 13:49:31 +08:00
2025-03-26 20:32:12 +08:00
public void testSettleOrderLogicV2(String orderCode) {
// 查询订单主表
OrderBasicInfo orderBasicInfo = orderBasicInfoService.getOrderInfoByOrderCode(orderCode);
LocalDateTime localDateTime = DateUtils.date2LocalDateTime(orderBasicInfo.getSettlementTime());
// 开始时间为localDateTime减30秒
LocalDateTime startTime = localDateTime.minusSeconds(30);
// 结束时间为localDateTime加30秒
LocalDateTime endTime = localDateTime.plusSeconds(30);
// 查询这笔订单的交易记录原始数据
List<PileMsgRecord> pileFeedListV2 = pileMsgRecordService.getPileFeedListV2(orderBasicInfo.getPileSn(), "0x3B", startTime, endTime);
if (CollectionUtils.isEmpty(pileFeedListV2)) {
// 如果没有交易记录原始数据,则使用停止充电时间作为开始时间和结束时间再查一次
localDateTime = DateUtils.date2LocalDateTime(orderBasicInfo.getChargeEndTime());
startTime = localDateTime.minusSeconds(30);
endTime = localDateTime.plusSeconds(30);
pileFeedListV2 = pileMsgRecordService.getPileFeedListV2(orderBasicInfo.getPileSn(), "0x3B", startTime, endTime);
}
2025-03-26 21:11:13 +08:00
TransactionRecordsData data = null;
for (PileMsgRecord pileMsgRecord : pileFeedListV2) {
TransactionRecordsData parse = JSONObject.parseObject(pileMsgRecord.getJsonMsg(), TransactionRecordsData.class);
if (StringUtils.equals(parse.getTransactionCode(), orderBasicInfo.getTransactionCode())) {
data = parse;
2025-04-02 09:50:39 +08:00
data.setVinCode("");
2025-03-26 21:11:13 +08:00
}
}
2025-03-26 13:49:31 +08:00
2025-03-26 21:11:13 +08:00
logger.info("testSettleOrderLogicV2, orderCode:{}, data:{}", orderCode, JSON.toJSONString(data));
2025-03-26 21:23:47 +08:00
if (data != null) {
testSettleOrderLogic(data, orderBasicInfo);
}
2025-03-26 20:32:12 +08:00
}
2025-03-26 13:49:31 +08:00
2025-03-26 20:32:12 +08:00
public void testSettleOrderLogic(TransactionRecordsData data, OrderBasicInfo orderBasicInfo) {
2025-03-31 14:33:59 +08:00
AbstractProgramLogic programLogic = ProgramLogicFactory.getProgramLogic(MerchantDelayModeEnum.DELAY.getValue());
2025-03-26 17:15:54 +08:00
// 查询订单详情
OrderDetail orderDetail = orderBasicInfoService.getOrderDetailByOrderCode(orderBasicInfo.getOrderCode());
2025-03-26 19:26:06 +08:00
2025-03-26 17:15:54 +08:00
programLogic.updateOrderBasicInfoAndOrderDetail(data, orderBasicInfo, orderDetail);
2025-03-26 13:49:31 +08:00
// 计算订单折扣
programLogic.calculateOrderDiscountsV2(orderBasicInfo, orderDetail);
2025-03-26 19:26:06 +08:00
logger.info("testSettleOrderLogic, orderCode:{}, 支付金额:{}, 消费金额:{}, 折扣金额:{}, 退款金额:{}, 结算金额:{}",
orderBasicInfo.getOrderCode(), orderBasicInfo.getPayAmount(), orderBasicInfo.getOrderAmount(),
orderBasicInfo.getDiscountAmount(), orderBasicInfo.getRefundAmount(), orderBasicInfo.getSettleAmount());
// 更新数据库
OrderTransactionDTO dto = new OrderTransactionDTO();
dto.setOrderBasicInfo(orderBasicInfo);
dto.setOrderDetail(orderDetail);
2025-03-26 21:37:12 +08:00
transactionService.doUpdateOrder(dto);
// 组装after参数
AfterSettleOrderDTO afterSettleOrderDTO = AfterSettleOrderDTO.builder()
.orderCode(orderBasicInfo.getOrderCode())
.merchantId(orderBasicInfo.getMerchantId())
.stationId(orderBasicInfo.getStationId())
.orderPayAmount(orderBasicInfo.getPayAmount()) // 支付金额
.orderConsumeAmount(orderBasicInfo.getOrderAmount()) // 消费金额
.orderSettleAmount(orderBasicInfo.getSettleAmount()) // 结算金额
.orderElectricityAmount(orderDetail.getTotalElectricityAmount()) // 电费金额
.orderElectricityDiscountAmount(orderDetail.getDiscountElectricityAmount()) // 电费折扣金额
.orderServiceAmount(orderDetail.getTotalServiceAmount()) // 服务费金额
.orderServiceDiscountAmount(orderDetail.getDiscountServiceAmount()) // 服务费折扣金额
.orderRefundAmount(orderBasicInfo.getRefundAmount()) // 退款金额
.build();
2025-03-26 21:23:47 +08:00
logger.info("testSettleOrderLogic, afterSettleOrderDTO:{}", JSON.toJSONString(afterSettleOrderDTO));
2025-03-26 21:37:12 +08:00
rabbitTemplate.convertAndSend(RabbitConstants.YKC_EXCHANGE_NAME, RabbitConstants.QUEUE_CHARGE_ORDER_DATA, afterSettleOrderDTO);
2025-03-26 13:49:31 +08:00
}
2025-03-31 13:41:09 +08:00
public void testSettleOrderLogicNotDelay(String orderCode) {
// 查询订单主表
OrderBasicInfo orderBasicInfo = orderBasicInfoService.getOrderInfoByOrderCode(orderCode);
2025-03-31 13:53:28 +08:00
Date date = orderBasicInfo.getSettlementTime();
if (date == null) {
date = orderBasicInfo.getChargeEndTime();
}
LocalDateTime localDateTime = DateUtils.date2LocalDateTime(date);
2025-03-31 13:41:09 +08:00
// 开始时间为localDateTime减30秒
LocalDateTime startTime = localDateTime.minusSeconds(30);
// 结束时间为localDateTime加30秒
LocalDateTime endTime = localDateTime.plusSeconds(30);
// logger.info("testSettleOrderLogicV2, orderCode:{}, settlementTime:{}, startTime:{}, endTime:{}", orderCode, orderBasicInfo.getSettlementTime(), startTime, endTime);
// logger.info("转换为Date类型, settlementTime:{}, startTime:{}, endTime:{}", orderBasicInfo.getSettlementTime(), DateUtils.localDateTime2Date(startTime), DateUtils.localDateTime2Date(endTime));
// 查询这笔订单的交易记录原始数据
List<PileMsgRecord> pileFeedListV2 = pileMsgRecordService.getPileFeedListV2(orderBasicInfo.getPileSn(), "0x3B", startTime, endTime);
// logger.info("testSettleOrderLogicV2, orderCode:{}, pileSn:{}, startTime:{}, endTime:{}, pileFeedListV2:{}",
// orderCode, orderBasicInfo.getPileSn(), startTime, endTime, JSON.toJSONString(pileFeedListV2));
TransactionRecordsData data = null;
for (PileMsgRecord pileMsgRecord : pileFeedListV2) {
TransactionRecordsData parse = JSONObject.parseObject(pileMsgRecord.getJsonMsg(), TransactionRecordsData.class);
if (StringUtils.equals(parse.getTransactionCode(), orderBasicInfo.getTransactionCode())) {
data = parse;
2025-03-31 14:20:48 +08:00
data.setVinCode("");
2025-03-31 13:41:09 +08:00
}
}
logger.info("testSettleOrderLogicNotDelay, orderCode:{}, data:{}", orderCode, JSON.toJSONString(data));
if (data != null) {
2025-03-31 14:29:29 +08:00
AbstractProgramLogic programLogic = ProgramLogicFactory.getProgramLogic(MerchantDelayModeEnum.NOT_DELAY.getValue());
2025-03-31 13:41:09 +08:00
programLogic.settleOrder(data, orderBasicInfo);
}
}
2025-04-11 14:39:00 +08:00
2025-04-11 15:28:38 +08:00
public void retrySplittingMethod(QueryOrderDTO dto) {
List<String> orderCodeList = Lists.newArrayList();
if (StringUtils.isNotBlank(dto.getOrderCode())) {
orderCodeList.add(dto.getOrderCode());
}
if (CollectionUtils.isNotEmpty(dto.getOrderCodeList())) {
orderCodeList.addAll(dto.getOrderCodeList());
}
if (CollectionUtils.isNotEmpty(orderCodeList)) {
for (String orderCode : orderCodeList) {
try {
retrySplittingMethod(orderCode);
} catch (Exception e) {
logger.error("retrySplittingMethod error, orderCode:{}", orderCode, e);
}
}
}
}
2025-04-11 14:39:00 +08:00
/**
* 重试分账逻辑
* 用于订单金额都已经计算好, 但是没有分账, 用来重新执行分账
*/
public void retrySplittingMethod(String orderCode) {
if (StringUtils.isBlank(orderCode)) {
return;
}
// 查询数据
OrderBasicInfo orderBasicInfo = orderBasicInfoService.getOrderInfoByOrderCode(orderCode);
if (orderBasicInfo == null) {
return;
}
OrderDetail orderDetail = orderBasicInfoService.getOrderDetailByOrderCode(orderCode);
if (orderDetail == null) {
return;
}
// 组装请求dto 组装after参数
AfterSettleOrderDTO afterSettleOrderDTO = AfterSettleOrderDTO.builder()
.orderCode(orderBasicInfo.getOrderCode())
.merchantId(orderBasicInfo.getMerchantId())
.stationId(orderBasicInfo.getStationId())
.orderPayAmount(orderBasicInfo.getPayAmount()) // 支付金额
.orderConsumeAmount(orderBasicInfo.getOrderAmount()) // 消费金额
.orderSettleAmount(orderBasicInfo.getSettleAmount()) // 结算金额
.orderElectricityAmount(orderDetail.getTotalElectricityAmount()) // 电费金额
.orderElectricityDiscountAmount(orderDetail.getDiscountElectricityAmount()) // 电费折扣金额
.orderServiceAmount(orderDetail.getTotalServiceAmount()) // 服务费金额
.orderServiceDiscountAmount(orderDetail.getDiscountServiceAmount()) // 服务费折扣金额
.orderRefundAmount(orderBasicInfo.getRefundAmount()) // 退款金额
.build();
logger.info("retrySplittingMethod, afterSettleOrderDTO:{}", JSON.toJSONString(afterSettleOrderDTO));
orderBasicInfoService.splittingMethod(afterSettleOrderDTO);
}
2025-04-16 14:10:36 +08:00
2025-04-25 17:31:51 +08:00
/**
* 调试订单
* @param dto
*/
2025-04-29 11:30:35 +08:00
public Map<String, Object> debugOrder(DebugOrderDTO dto) {
Map<String, Object> resultMap = Maps.newHashMap();
2025-04-29 11:45:48 +08:00
if (dto == null || StringUtils.isBlank(dto.getOrderCode())) {
2025-04-29 11:14:24 +08:00
logger.info("debugOrder, 参数为空直接放回");
2025-04-29 11:30:35 +08:00
return resultMap;
2025-04-25 17:31:51 +08:00
}
2025-04-29 11:45:48 +08:00
String orderCode = dto.getOrderCode();
2025-04-29 11:30:35 +08:00
resultMap.put("orderCode", orderCode);
2025-04-29 11:14:24 +08:00
// 根据订单号查询订单信息
2025-04-29 11:30:35 +08:00
OrderBasicInfo orderBasicInfo = orderBasicInfoService.getOrderInfoByOrderCode(orderCode);
OrderDetail orderDetail = orderBasicInfoService.getOrderDetailByOrderCode(orderCode);
2025-05-05 14:06:01 +08:00
if (orderBasicInfo == null || orderDetail == null) {
logger.info("debugOrder, orderCode:{}, orderBasicInfo:{}, orderDetail:{}", orderCode, orderBasicInfo, orderDetail);
return resultMap;
}
2025-04-29 11:14:24 +08:00
2025-04-25 17:31:51 +08:00
// 是否重新计算
2025-04-29 11:59:06 +08:00
String reCalculateFlag = dto.getReCalculateFlag();
String reCalculateFlagResult = orderCode + "本次未重新计算订单";
2025-10-10 13:32:53 +08:00
if (StringUtils.equals(reCalculateFlag, Constants.ONE)) {
2025-04-29 11:14:24 +08:00
LocalDateTime localDateTime = DateUtils.date2LocalDateTime(orderBasicInfo.getSettlementTime());
// 开始时间为localDateTime减30秒
LocalDateTime startTime = localDateTime.minusSeconds(30);
// 结束时间为localDateTime加30秒
LocalDateTime endTime = localDateTime.plusSeconds(30);
// 查询这笔订单的交易记录原始数据
List<PileMsgRecord> pileFeedListV2 = pileMsgRecordService.getPileFeedListV2(orderBasicInfo.getPileSn(), "0x3B", startTime, endTime);
if (CollectionUtils.isEmpty(pileFeedListV2)) {
// 如果没有交易记录原始数据,则使用停止充电时间作为开始时间和结束时间再查一次
localDateTime = DateUtils.date2LocalDateTime(orderBasicInfo.getChargeEndTime());
startTime = localDateTime.minusSeconds(30);
endTime = localDateTime.plusSeconds(30);
pileFeedListV2 = pileMsgRecordService.getPileFeedListV2(orderBasicInfo.getPileSn(), "0x3B", startTime, endTime);
}
TransactionRecordsData data = null;
for (PileMsgRecord pileMsgRecord : pileFeedListV2) {
TransactionRecordsData parse = JSONObject.parseObject(pileMsgRecord.getJsonMsg(), TransactionRecordsData.class);
if (StringUtils.equals(parse.getTransactionCode(), orderBasicInfo.getTransactionCode())) {
data = parse;
data.setVinCode("");
}
}
if (data != null) {
AbstractProgramLogic programLogic = ProgramLogicFactory.getProgramLogic(MerchantDelayModeEnum.DELAY.getValue());
// 更新订单主表和订单详情中的各种金额
programLogic.updateOrderBasicInfoAndOrderDetail(data, orderBasicInfo, orderDetail);
// 计算订单折扣
programLogic.calculateOrderDiscountsV2(orderBasicInfo, orderDetail);
2025-04-25 17:31:51 +08:00
2025-04-29 11:59:06 +08:00
reCalculateFlagResult = MessageFormat.format("debugOrder重新计算订单, orderCode:{0}, 支付金额:{1}, 消费金额:{2}, 折扣金额:{3}, 退款金额:{4}, 结算金额:{5}",
2025-04-29 11:14:24 +08:00
orderBasicInfo.getOrderCode(), orderBasicInfo.getPayAmount(), orderBasicInfo.getOrderAmount(),
orderBasicInfo.getDiscountAmount(), orderBasicInfo.getRefundAmount(), orderBasicInfo.getSettleAmount());
2025-04-29 11:30:35 +08:00
// logger.info("debugOrder重新计算订单, orderCode:{}, 支付金额:{}, 消费金额:{}, 折扣金额:{}, 退款金额:{}, 结算金额:{}",
// orderBasicInfo.getOrderCode(), orderBasicInfo.getPayAmount(), orderBasicInfo.getOrderAmount(),
// orderBasicInfo.getDiscountAmount(), orderBasicInfo.getRefundAmount(), orderBasicInfo.getSettleAmount());
2025-04-29 11:59:06 +08:00
logger.info(reCalculateFlagResult);
2025-04-29 11:30:35 +08:00
2025-04-29 11:14:24 +08:00
// 更新数据库
OrderTransactionDTO orderTransactionDTO = new OrderTransactionDTO();
orderTransactionDTO.setOrderBasicInfo(orderBasicInfo);
orderTransactionDTO.setOrderDetail(orderDetail);
transactionService.doUpdateOrder(orderTransactionDTO);
2025-04-29 11:59:06 +08:00
} else {
reCalculateFlagResult = orderCode + "本次未重新计算订单, 原因: 未找到交易记录原始数据";
2025-04-29 11:14:24 +08:00
}
2025-04-25 17:31:51 +08:00
}
2025-04-29 11:59:06 +08:00
resultMap.put("reCalculate", ImmutableMap.of("value", reCalculateFlag, "message", reCalculateFlagResult));
2025-04-29 11:14:24 +08:00
// 组装after参数
AfterSettleOrderDTO afterSettleOrderDTO = AfterSettleOrderDTO.builder()
.orderCode(orderBasicInfo.getOrderCode())
.merchantId(orderBasicInfo.getMerchantId())
.stationId(orderBasicInfo.getStationId())
.orderPayAmount(orderBasicInfo.getPayAmount()) // 支付金额
.orderConsumeAmount(orderBasicInfo.getOrderAmount()) // 消费金额
.orderSettleAmount(orderBasicInfo.getSettleAmount()) // 结算金额
.orderElectricityAmount(orderDetail.getTotalElectricityAmount()) // 电费金额
.orderElectricityDiscountAmount(orderDetail.getDiscountElectricityAmount()) // 电费折扣金额
.orderServiceAmount(orderDetail.getTotalServiceAmount()) // 服务费金额
.orderServiceDiscountAmount(orderDetail.getDiscountServiceAmount()) // 服务费折扣金额
.orderRefundAmount(orderBasicInfo.getRefundAmount()) // 退款金额
2025-06-03 16:19:46 +08:00
.orderBasicInfo(orderBasicInfo)
2025-04-29 11:14:24 +08:00
.build();
2025-04-25 17:31:51 +08:00
// 是否重新退款
2025-04-29 11:59:06 +08:00
String reRefundFlag = dto.getReRefundFlag();
String reRefundFlagResult = orderCode + "本次未重新退款";
2025-10-10 13:32:53 +08:00
if (StringUtils.equals(reRefundFlag, Constants.ONE)) {
2025-04-29 11:14:24 +08:00
// 订单退款,汇付退款
try {
orderBasicInfoService.refundMethod(afterSettleOrderDTO);
} catch (Exception e) {
logger.error("debugOrder-订单:{}, 订单退款,汇付退款异常", afterSettleOrderDTO.getOrderCode(), e);
}
2025-04-29 11:59:06 +08:00
reRefundFlagResult = MessageFormat.format("debugOrder-订单:{0}, 订单退款,汇付退款成功", afterSettleOrderDTO.getOrderCode());
logger.info(reRefundFlagResult);
2025-04-25 17:31:51 +08:00
}
2025-04-29 11:59:06 +08:00
resultMap.put("reRefund", ImmutableMap.of("value", reRefundFlag, "message", reRefundFlagResult));
2025-04-29 11:30:35 +08:00
2025-04-25 17:31:51 +08:00
// 是否重新分账
2025-04-29 11:59:06 +08:00
String reSplitFlag = dto.getReSplitFlag();
String reSplitFlagResult = orderCode + "本次未重新分账";
2025-10-10 13:32:53 +08:00
if (StringUtils.equals(reSplitFlag, Constants.ONE)) {
2025-04-29 11:14:24 +08:00
try {
orderBasicInfoService.splittingMethod(afterSettleOrderDTO);
} catch (Exception e) {
logger.error("debugOrder-订单:{}, 订单结算金额,汇付分账异常", afterSettleOrderDTO.getOrderCode(), e);
}
2025-04-29 11:59:06 +08:00
reSplitFlagResult = MessageFormat.format("debugOrder-订单:{0}, 订单结算金额,汇付分账成功", afterSettleOrderDTO.getOrderCode());
logger.info(reSplitFlagResult);
2025-04-25 17:31:51 +08:00
}
2025-04-29 11:59:06 +08:00
resultMap.put("reSplit", ImmutableMap.of("value", reSplitFlag, "message", reSplitFlagResult));
2025-05-05 13:51:17 +08:00
logger.info("debugOrder结束, resultMap:{}", JSONObject.toJSONString(resultMap));
2025-04-29 11:30:35 +08:00
return resultMap;
2025-04-25 17:31:51 +08:00
}
2025-07-03 08:41:21 +08:00
public void sendOrderData2Mq(String orderCode) {
// 通过订单编号查询订单信息
OrderBasicInfo orderBasicInfo = orderBasicInfoService.getOrderInfoByOrderCode(orderCode);
OrderDetail orderDetail = orderBasicInfoService.getOrderDetailByOrderCode(orderCode);
if (orderBasicInfo == null || orderDetail == null) {
return;
}
// 发送消息
AfterSettleOrderDTO afterSettleOrderDTO = AfterSettleOrderDTO.builder()
.orderCode(orderBasicInfo.getOrderCode())
.merchantId(orderBasicInfo.getMerchantId())
.stationId(orderBasicInfo.getStationId())
.orderPayAmount(orderBasicInfo.getPayAmount()) // 支付金额
.orderConsumeAmount(orderBasicInfo.getOrderAmount()) // 消费金额
.orderSettleAmount(orderBasicInfo.getSettleAmount()) // 结算金额
.orderElectricityAmount(orderDetail.getTotalElectricityAmount()) // 电费金额
.orderElectricityDiscountAmount(orderDetail.getDiscountElectricityAmount()) // 电费折扣金额
.orderServiceAmount(orderDetail.getTotalServiceAmount()) // 服务费金额
.orderServiceDiscountAmount(orderDetail.getDiscountServiceAmount()) // 服务费折扣金额
.orderRefundAmount(orderBasicInfo.getRefundAmount()) // 退款金额
.build();
rabbitTemplate.convertAndSend(RabbitConstants.YKC_EXCHANGE_NAME, RabbitConstants.QUEUE_CHARGE_ORDER_DATA, afterSettleOrderDTO);
}
/**
* 根据时间区间批量推送订单
* @param dto
* @return
*/
public void pushOrderInfoBatch(PushOrderDTO dto) {
// 根据type查出对接的stationIds
List<StationInfoVO> stationInfoVOS = thirdPartyStationRelationService.selectStationList(dto.getThirdPartyType());
List<String> stationIds = stationInfoVOS.stream()
.map(StationInfoVO::getStationId)
.collect(Collectors.toList());
// 批量查询需要推送的订单
List<OrderBasicInfo> orderInfos = orderBasicInfoService.getOrderBasicInfoByTimeInterval(stationIds, dto.getStartTime(), dto.getEndTime());
if (CollectionUtils.isEmpty(orderInfos)) {
return;
}
NotificationDTO notificationDTO = new NotificationDTO();
orderInfos.forEach(orderBasicInfo -> {
// 推送第三方平台
notificationDTO.setOrderCode(orderBasicInfo.getOrderCode());
notificationDTO.setStationId(orderBasicInfo.getStationId());
notificationDTO.setPlatformType(dto.getThirdPartyType());
2025-09-01 15:45:23 +08:00
try {
notificationService.notificationChargeOrderInfoHistory(notificationDTO);
}catch (Exception e) {
logger.error("notificationChargeOrderInfoHistory推送订单失败订单号:{}", orderBasicInfo.getOrderCode(), e);
}
try {
//常畅充
// 补推送充电账单信息
notificationService.notificationChargeOrderInfo(notificationDTO);
}catch (Exception e){
logger.error("notificationChargeOrderInfo推送订单失败订单号:{}", orderBasicInfo.getOrderCode(), e);
}
});
}
/**
* 根据时间区间批量查询订单数量
* @param dto
* @return
*/
2025-09-26 14:55:18 +08:00
public OrderCountByTimeVO queryOrderCountByTime(QueryOrderDTO dto) {
if (dto == null) {
dto = new QueryOrderDTO();
}
2025-09-27 13:46:26 +08:00
DateTimeFormatter dateTimeFormatter = DateTimeFormatter.ofPattern(DateUtils.YYYY_MM_DD_HH_MM_SS);
// 开始时间默认:当天零点
if (StringUtils.isEmpty(dto.getStartTime())) {
LocalDateTime todayStart = LocalDateTime.of(LocalDate.now(), LocalTime.MIN);
2025-09-27 13:46:26 +08:00
dto.setStartTime(todayStart.format(dateTimeFormatter));
}
2025-09-27 13:46:26 +08:00
// 结束时间默认:当前时间
if (StringUtils.isEmpty(dto.getEndTime())) {
dto.setEndTime(LocalDateTime.now().format(dateTimeFormatter));
}
2025-09-27 13:46:26 +08:00
// ====== 查询订单数据 ======
List<OrderListVO> orderList = orderBasicInfoMapper.selectOrderBasicInfoList(dto);
2025-09-27 13:46:26 +08:00
// ====== 统计逻辑 ======
2025-09-26 14:55:18 +08:00
List<OrderCountByTimeVO.OrderCountByTimeListVO> result = new ArrayList<>();
2025-09-27 13:46:26 +08:00
BigDecimal totalInsuranceAmount = BigDecimal.ZERO; // 总保险金额
2025-09-27 13:46:26 +08:00
try {
// 转换查询时间
LocalDateTime queryStart = LocalDateTime.parse(dto.getStartTime(), dateTimeFormatter);
LocalDateTime queryEnd = LocalDateTime.parse(dto.getEndTime(), dateTimeFormatter);
// 按小时分组订单
2025-09-27 13:46:26 +08:00
Map<LocalDateTime, List<OrderListVO>> ordersByHour = orderList.stream()
.collect(Collectors.groupingBy(order -> {
if (order.getCreateTime() != null) {
2025-09-27 13:46:26 +08:00
return LocalDateTime.parse(order.getCreateTime() , dateTimeFormatter)
.withMinute(0).withSecond(0).withNano(0);
} else {
logger.warn("订单创建时间为空,订单号:{}", order.getOrderCode());
2025-09-27 13:46:26 +08:00
return LocalDateTime.now().withMinute(0).withSecond(0).withNano(0);
}
}));
2025-09-27 13:46:26 +08:00
// 从开始时间到结束时间,逐小时统计
LocalDateTime currentHour = queryStart.withMinute(0).withSecond(0).withNano(0);
while (currentHour.isBefore(queryEnd)) {
LocalDateTime nextHour = currentHour.plusHours(1);
2025-09-27 13:46:26 +08:00
// 当前小时的订单集合
List<OrderListVO> ordersInHour = ordersByHour.getOrDefault(currentHour, Collections.emptyList());
int count = ordersInHour.size();
// 统计该小时保险金额
BigDecimal hourInsuranceAmount = ordersInHour.stream()
.map(OrderListVO::getInsuranceAmount) // 假设 VO 里有该字段
.filter(Objects::nonNull)
.reduce(BigDecimal.ZERO, BigDecimal::add);
// 封装结果
2025-09-26 14:55:18 +08:00
OrderCountByTimeVO.OrderCountByTimeListVO vo = new OrderCountByTimeVO.OrderCountByTimeListVO();
vo.setStartTime(currentHour.format(dateTimeFormatter));
vo.setEndTime(nextHour.format(dateTimeFormatter));
vo.setCount(count);
2025-09-26 14:55:18 +08:00
vo.setOrderCount(count);
2025-09-27 13:46:26 +08:00
vo.setInsuranceAmount(hourInsuranceAmount);
result.add(vo);
2025-09-27 13:46:26 +08:00
// 累加总保险金额
totalInsuranceAmount = totalInsuranceAmount.add(hourInsuranceAmount);
currentHour = nextHour;
}
2025-09-27 13:46:26 +08:00
// 排序(确保结果有序)
2025-09-26 14:55:18 +08:00
result.sort(Comparator.comparing(OrderCountByTimeVO.OrderCountByTimeListVO::getStartTime));
2025-09-27 13:46:26 +08:00
} catch (Exception e) {
logger.error("统计订单数量失败", e);
}
2025-09-27 13:46:26 +08:00
// ====== 封装返回结果 ======
2025-09-26 14:55:18 +08:00
OrderCountByTimeVO orderCountByTimeVO = new OrderCountByTimeVO();
orderCountByTimeVO.setOrderCountByTimeList(result);
2025-09-27 13:46:26 +08:00
// 计算总订单数
int totalCount = 0;
2025-09-26 14:55:18 +08:00
for (OrderCountByTimeVO.OrderCountByTimeListVO vo : result) {
totalCount += vo.getCount();
}
orderCountByTimeVO.setTotalCount(totalCount);
2025-09-27 13:46:26 +08:00
orderCountByTimeVO.setTotalInsuranceAmount(totalInsuranceAmount);
logger.info("查询订单数量结果:{}", JSONObject.toJSONString(orderCountByTimeVO));
2025-09-26 14:55:18 +08:00
return orderCountByTimeVO;
}
2025-07-24 14:55:56 +08:00
public void updateWalletCode() {
// 查询钱包dode为空的数据
List<MemberWalletInfo> memberWalletInfoList = memberWalletInfoService.queryWalletIsNull();
// 分批处理, 1000条为一批
List<List<MemberWalletInfo>> partition = Lists.partition(memberWalletInfoList, 1000);
partition.parallelStream().forEach(walletInfoList -> {
walletInfoList.forEach(walletInfo -> {
// 生成钱包code
2025-07-24 15:26:55 +08:00
if (StringUtils.isBlank(walletInfo.getWalletCode())) {
walletInfo.setWalletCode(memberBasicInfoService.generateWalletCode());
}
2025-07-24 14:55:56 +08:00
});
// 批量更新钱包code
memberWalletInfoService.updateBatchSelective(walletInfoList);
});
}
/**
* 处理OrderSplitRecord中值为0的数据
* @throws BaseAdaPayException
*/
public void handleOrderSplitRecord(String orderCode) {
// 根据订单号查询订单信息
OrderBasicInfo orderBasicInfo = orderBasicInfoService.getOrderInfoByOrderCode(orderCode);
OrderDetail orderDetail = orderBasicInfoService.getOrderDetailByOrderCode(orderCode);
// 组装after参数
AfterSettleOrderDTO afterSettleOrderDTO = AfterSettleOrderDTO.builder()
.orderCode(orderBasicInfo.getOrderCode())
.merchantId(orderBasicInfo.getMerchantId())
.stationId(orderBasicInfo.getStationId())
.orderPayAmount(orderBasicInfo.getPayAmount()) // 支付金额
.orderConsumeAmount(orderBasicInfo.getOrderAmount()) // 消费金额
.orderSettleAmount(orderBasicInfo.getSettleAmount()) // 结算金额
.orderElectricityAmount(orderDetail.getTotalElectricityAmount()) // 电费金额
.orderElectricityDiscountAmount(orderDetail.getDiscountElectricityAmount()) // 电费折扣金额
.orderServiceAmount(orderDetail.getTotalServiceAmount()) // 服务费金额
.orderServiceDiscountAmount(orderDetail.getDiscountServiceAmount()) // 服务费折扣金额
.orderRefundAmount(orderBasicInfo.getRefundAmount()) // 退款金额
.orderBasicInfo(orderBasicInfo)
.build();
orderBasicInfoService.splittingMethodTemp(afterSettleOrderDTO);
}
2023-08-26 18:01:25 +08:00
}
2024-11-13 17:06:46 +08:00