Files
jsowell-charger-web/jsowell-admin/src/main/java/com/jsowell/service/SplitBillService.java
2025-10-11 15:29:29 +08:00

239 lines
10 KiB
Java
Raw Blame History

This file contains ambiguous Unicode characters
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
package com.jsowell.service;
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;
import com.jsowell.adapay.service.AdapayService;
import com.jsowell.common.constant.Constants;
import com.jsowell.common.util.DateUtils;
import com.jsowell.pile.dto.DebugOrderDTO;
import com.jsowell.pile.dto.QueryOrderSplitRecordDTO;
import com.jsowell.pile.service.OrderBasicInfoService;
import com.jsowell.pile.service.PileStationInfoService;
import com.jsowell.pile.service.SettleOrderReportService;
import com.jsowell.pile.vo.web.OrderPaymentDetailVO;
import com.jsowell.pile.vo.web.PileStationVO;
import org.apache.commons.collections4.CollectionUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import java.util.List;
import java.util.Map;
import java.util.Set;
@Service
public class SplitBillService {
private final Logger log = LoggerFactory.getLogger(SplitBillService.class);
@Autowired
private OrderBasicInfoService orderBasicInfoService;
@Autowired
private TempService tempService;
@Autowired
private AdapayService adapayService;
@Autowired
private PileStationInfoService pileStationInfoService;
@Autowired
private SettleOrderReportService settleOrderReportService;
/**
* 重试运营商分账
* @param dto
*/
public void retryMerchantSplit(QueryOrderSplitRecordDTO dto) throws BaseAdaPayException {
String startTime = dto.getStartTime();
String endTime = dto.getEndTime();
// 首先根据 merchantId、startTime、endTime 查出日期区间内所有订单信息(包括 paymentId
List<OrderPaymentDetailVO> recordList = orderBasicInfoService.getSplitOrders(dto);
if (CollectionUtils.isEmpty(recordList)) {
log.info("重试运营商分账-根据参数未查询到符合条件的订单, param:{}", dto);
return;
}
// 循环一次获得paymentIdList 与 Map
List<String> paymentIdList = Lists.newArrayList();
Map<String, OrderPaymentDetailVO> paymentIdMap = Maps.newHashMap();
Set<String> stationIdSet = Sets.newHashSet();
for (OrderPaymentDetailVO orderPaymentDetailVO : recordList) {
paymentIdList.add(orderPaymentDetailVO.getPaymentId());
paymentIdMap.put(orderPaymentDetailVO.getPaymentId(), orderPaymentDetailVO);
stationIdSet.add(orderPaymentDetailVO.getStationId());
}
// 判断订单是否已经成功分账, 将未分账的订单进行筛选、汇总
List<String> unSplitPaymentIdList = adapayService.getSplitInfoByPaymentIdList(paymentIdList);
if (CollectionUtils.isEmpty(unSplitPaymentIdList)) {
log.info("重试运营商分账-所有订单都已经成功分账");
return;
}
// 将 paymentIdList 与 recordList 进行匹配,筛选出 orderCodeList
List<String> orderCodeList = Lists.newArrayList();
for (String unSplitPaymentId : unSplitPaymentIdList) {
OrderPaymentDetailVO orderPaymentDetailVO = paymentIdMap.get(unSplitPaymentId);
if (orderPaymentDetailVO != null) {
orderCodeList.add(orderPaymentDetailVO.getOrderCode());
}
}
// 调用 debugOrder 接口进行重新分账
for (String orderCode : orderCodeList) {
DebugOrderDTO debugOrderDTO = new DebugOrderDTO();
debugOrderDTO.setOrderCode(orderCode);
debugOrderDTO.setReSplitFlag(Constants.ONE);
try {
Map<String, Object> stringObjectMap = tempService.debugOrder(debugOrderDTO);
log.info("重试运营商分账-debugOrder接口调用成功, param:{}, result:{}", debugOrderDTO, stringObjectMap);
} catch (Exception e) {
log.error("重试运营商分账-debugOrder接口调用异常, param:{}", debugOrderDTO, e);
}
}
// 重新计算站点日报和运营商日报
// 获取日期区间内所有日期
List<String> dateList = DateUtils.getAllDatesInTheDateRange(startTime, endTime);
stationIdSet.parallelStream().forEach(stationId -> {
PileStationVO stationInfo = pileStationInfoService.getStationInfo(stationId);
for (String tradeDate : dateList) {
try {
settleOrderReportService.generateDailyOrderReports(stationInfo, tradeDate);
log.info("重试运营商分账-生成站点日报成功, param:{}", tradeDate);
}catch (Exception e) {
log.error("重试运营商分账-生成站点日报异常, param:{}", tradeDate, e);
}
}
});
// 重新计算运营商日报
for (String tradeDate : dateList) {
try {
orderBasicInfoService.generateMerchantBill(dto.getMerchantId(), tradeDate);
log.info("重试运营商分账-生成运营商日报成功, param:{}", tradeDate);
}catch (Exception e) {
log.error("重试运营商分账-生成运营商日报异常, param:{}", tradeDate, e);
}
}
}
/**
* 重试运营商分账V2
* @param dto
* @throws BaseAdaPayException
*/
public void retryMerchantSplitV2(QueryOrderSplitRecordDTO dto) throws BaseAdaPayException {
String startTime = dto.getStartTime();
String endTime = dto.getEndTime();
// 首先根据 merchantId、startTime、endTime 查出日期区间内所有订单信息(包括 paymentId
List<OrderPaymentDetailVO> recordList = orderBasicInfoService.getSplitOrders(dto);
if (CollectionUtils.isEmpty(recordList)) {
log.info("重试运营商分账-根据参数未查询到符合条件的订单, param:{}", dto);
return;
}
// 循环一次获得paymentIdList 与 Map
List<String> paymentIdList = Lists.newArrayList();
Map<String, OrderPaymentDetailVO> paymentIdMap = Maps.newHashMap();
Set<String> stationIdSet = Sets.newHashSet();
for (OrderPaymentDetailVO orderPaymentDetailVO : recordList) {
paymentIdList.add(orderPaymentDetailVO.getPaymentId());
paymentIdMap.put(orderPaymentDetailVO.getPaymentId(), orderPaymentDetailVO);
stationIdSet.add(orderPaymentDetailVO.getStationId());
}
// 判断订单是否已经成功分账, 将未分账的订单进行筛选、汇总
Map<String, List<String>> splitInfoMap = adapayService.getSplitInfoMapByPaymentIdList(paymentIdList);
if (splitInfoMap == null || splitInfoMap.isEmpty()) {
return;
}
// 未分帐paymentId列表
List<String> unSplitPaymentIdList = splitInfoMap.get("unSplitList");
// 已分帐paymentId列表
List<String> splitPaymentIdList = splitInfoMap.get("splitList");
/*
如果未分帐paymentId列表不为空则循环每个paymentId进行debugOrder
*/
if (CollectionUtils.isNotEmpty(unSplitPaymentIdList)) {
// 将 paymentIdList 与 recordList 进行匹配,筛选出 orderCodeList
List<String> orderCodeList = Lists.newArrayList();
for (String unSplitPaymentId : unSplitPaymentIdList) {
OrderPaymentDetailVO orderPaymentDetailVO = paymentIdMap.get(unSplitPaymentId);
if (orderPaymentDetailVO != null) {
orderCodeList.add(orderPaymentDetailVO.getOrderCode());
}
}
// 调用 debugOrder 接口进行重新分账
for (String orderCode : orderCodeList) {
DebugOrderDTO debugOrderDTO = new DebugOrderDTO();
debugOrderDTO.setOrderCode(orderCode);
debugOrderDTO.setReSplitFlag(Constants.ONE);
try {
Map<String, Object> stringObjectMap = tempService.debugOrder(debugOrderDTO);
log.info("重试运营商分账-debugOrder接口调用成功, param:{}, result:{}", debugOrderDTO, stringObjectMap);
} catch (Exception e) {
log.error("重试运营商分账-debugOrder接口调用异常, param:{}", debugOrderDTO, e);
}
}
}
/*
如果已分帐paymentId列表不为空则循环每个paymentId进行校验OrderSplitRecord数据是否正确
*/
if (CollectionUtils.isNotEmpty(splitPaymentIdList)) {
// 将 paymentIdList 与 recordList 进行匹配,筛选出 orderCodeList
List<String> orderCodeList = Lists.newArrayList();
for (String unSplitPaymentId : splitPaymentIdList) {
OrderPaymentDetailVO orderPaymentDetailVO = paymentIdMap.get(unSplitPaymentId);
if (orderPaymentDetailVO != null) {
orderCodeList.add(orderPaymentDetailVO.getOrderCode());
}
}
for (String orderCode : orderCodeList) {
tempService.checkOrderSplitRecord(orderCode);
}
}
/*
重新计算站点日报和运营商日报
*/
// 获取日期区间内所有日期
List<String> dateList = DateUtils.getAllDatesInTheDateRange(startTime, endTime);
stationIdSet.parallelStream().forEach(stationId -> {
PileStationVO stationInfo = pileStationInfoService.getStationInfo(stationId);
for (String tradeDate : dateList) {
try {
settleOrderReportService.generateDailyOrderReports(stationInfo, tradeDate);
log.info("重试运营商分账-生成站点日报成功, param:{}", tradeDate);
}catch (Exception e) {
log.error("重试运营商分账-生成站点日报异常, param:{}", tradeDate, e);
}
}
});
// 重新计算运营商日报
for (String tradeDate : dateList) {
try {
orderBasicInfoService.generateMerchantBill(dto.getMerchantId(), tradeDate);
log.info("重试运营商分账-生成运营商日报成功, param:{}", tradeDate);
}catch (Exception e) {
log.error("重试运营商分账-生成运营商日报异常, param:{}", tradeDate, e);
}
}
}
}