package com.qtzl.alterSales.manager.service; import cn.com.taiji.common.manager.net.http.ServiceHandleException; import com.qtzl.alterSales.dao.entity.primary.AflSupplementaryPayOrder; import com.qtzl.alterSales.dao.entity.primary.FssPaccountPay; import com.qtzl.alterSales.dao.repo.jpa.primary.AflSupplementaryPayOrderRepo; import com.qtzl.alterSales.dao.repo.jpa.primary.FssPaccountPayRepo; import com.qtzl.alterSales.manager.enums.*; import com.qtzl.alterSales.manager.model.protocol.UcServiceError; import com.qtzl.alterSales.manager.model.protocol.sales.AflSupplementaryNotifyRequest; import org.apache.commons.lang3.StringUtils; import org.slf4j.Logger; import org.slf4j.LoggerFactory; import org.springframework.stereotype.Service; import org.springframework.util.CollectionUtils; import javax.annotation.Resource; import java.time.LocalDateTime; import java.time.format.DateTimeFormatter; import java.util.List; /*** *

* 补缴单 service *

* @author hou yi * {@code @date} 2023/11/16 9:47 **/ @Service public class AflSupplementaryPayOrderServiceImpl implements AflSupplementaryPayOrderService { private static final Logger log = LoggerFactory.getLogger(AflSupplementaryPayOrderServiceImpl.class); @Resource private AflSupplementaryPayOrderRepo supplementaryPayOrderRepo; @Resource private FssPaccountPayRepo accountPayRepo; @Resource private AflBlackInfoManager aflBlackInfoManager; @Resource AflPaccountRefundCountService aflPaccountRefundCountService; @Override public void notifyHandler(AflSupplementaryNotifyRequest request) throws ServiceHandleException { // final AflSupplementaryPayOrder supplementaryPayOrder = notifyHandlerVerify(request); if (SupplementartPayOrderStateEnum.SUCCESS.equals(supplementaryPayOrder.getState())) { log.warn("补缴状态为SUCCESS的补交单,不进行补缴通知的相关处理,响应渠道方时为成功... payId:{}", request.getPayId()); return; } DateTimeFormatter df = DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss"); final LocalDateTime successTime; try { successTime = LocalDateTime.parse(request.getSuccessTime(), df); } catch (Exception e) { throw UcServiceError.BUSINESS_VALIDATE_ERR.toHandleException("补缴成功时间格式错误"); } // 流水处理 final FssPaccountPay fssPaccountPay = accountPayRepo.findByPayId(request.getPayId()); if (null == fssPaccountPay) { throw UcServiceError.BUSINESS_VALIDATE_ERR.toHandleException("路网交易流水号对应流水不存在,payId:" + request.getPayId()); } if (null != fssPaccountPay.getStatus() && fssPaccountPay.getStatus().equals(1)) { throw UcServiceError.BUSINESS_VALIDATE_ERR.toHandleException("路网交易流水号对应流水状态为扣款成功,请核实,payId:" + request.getPayId()); } supplementaryPayOrder.setSupplementaryNo(request.getOutTradeNo()); supplementaryPayOrder.setSupplementaryTime(successTime); supplementaryPayOrder.setSupplementaryNotifyTime(LocalDateTime.now()); supplementaryPayOrder.setState(SupplementartPayOrderStateEnum.SUCCESS); //进行统计累加已收款金额 try { aflPaccountRefundCountService.saveFind(fssPaccountPay); } catch (Exception e) { log.error("进行统计累加已收款金额失败... payId:{},{}", request.getPayId(),e.getMessage()); } fssPaccountPay.setStatus(1); fssPaccountPay.setMsg("请款成功"); fssPaccountPay.setWxPayStatus(1); fssPaccountPay.setWxPayTime(supplementaryPayOrder.getSupplementaryTime()); fssPaccountPay.setWxOrderId(request.getOutTradeNo()); // 进行是否需要进行返白操作 userStateHandler(fssPaccountPay, request.getPayId()); // supplementaryPayOrderRepo.save(supplementaryPayOrder); accountPayRepo.save(fssPaccountPay); } @Override public void paySuccessNotify(String payId, LocalDateTime supplementaryTime, String supplementaryNo) { // 补缴成功后通知渠道 AflSupplementaryPayOrder supplementaryPayOrder = supplementaryPayOrderRepo.findByPayId(payId); if (null == supplementaryPayOrder) { // 未生成补缴单的流水补扣成功后不推送 return; } supplementaryPayOrder.setSupplementaryTime(supplementaryTime); supplementaryPayOrder.setSupplementaryNo(supplementaryNo); supplementaryPayOrder.setState(SupplementartPayOrderStateEnum.SUCCESS); supplementaryPayOrder.setSupplementaryNotifyTime(LocalDateTime.now()); supplementaryPayOrder.setQtState(QtStateEnum.FAIL); supplementaryPayOrderRepo.save(supplementaryPayOrder); } private void userStateHandler(FssPaccountPay fssPaccountPay, String payId) { // 用户是否返白处理 final List accountPays = accountPayRepo.findByAgreementNum(fssPaccountPay.getAgreementNum()); final List payStream = accountPays.stream().filter(vo -> !vo.getPayId().equals(payId)).toList(); if (CollectionUtils.isEmpty(payStream)) { // 不存在其他待扣款流水,进行返白 aflBlackInfoManager.execute(fssPaccountPay.getAgreementNum(), BlacklistOpType.OUT, "渠道方通知补缴成功,查询无其他待补缴流水,进行返白", fssPaccountPay.getOrderId(), payId, null, null, null); } } private AflSupplementaryPayOrder notifyHandlerVerify(AflSupplementaryNotifyRequest request) throws ServiceHandleException { // if (StringUtils.isEmpty(request.getPayId())) { throw UcServiceError.BUSINESS_VALIDATE_ERR.toHandleException("路网交易流水号不能为空"); } if (null == request.getStatus()) { throw UcServiceError.BUSINESS_VALIDATE_ERR.toHandleException("补缴状态不能为空"); } if (null == request.getTotalFee()) { throw UcServiceError.BUSINESS_VALIDATE_ERR.toHandleException("补缴金额不能为空"); } if (!SupplementaryNotifyStatusEnum.SUPPLEMENTARY_SUCCESS.getCode().equals(request.getStatus())) { throw UcServiceError.BUSINESS_VALIDATE_ERR.toHandleException("非补缴成功的补缴单,请在补缴成功后再进行通知"); } if (StringUtils.isEmpty(request.getOutTradeNo())) { throw UcServiceError.BUSINESS_VALIDATE_ERR.toHandleException("补缴单号不能为空"); } if (StringUtils.isEmpty(request.getSuccessTime())) { throw UcServiceError.BUSINESS_VALIDATE_ERR.toHandleException("补缴成功时间不能为空"); } final AflSupplementaryPayOrder supplementaryPayOrder = supplementaryPayOrderRepo.findByPayId(request.getPayId()); if (null == supplementaryPayOrder) { throw UcServiceError.BUSINESS_VALIDATE_ERR.toHandleException("路网交易流水号对应补缴单不存在,请核实"); } if (!request.getTotalFee().equals(supplementaryPayOrder.getReceiptAmt())) { throw UcServiceError.BUSINESS_VALIDATE_ERR.toHandleException("补缴金额与补缴单金额不一致,请核实"); } return supplementaryPayOrder; } }