package org.springblade.yeePay.listener;

import lombok.extern.slf4j.Slf4j;
import org.springblade.modules.guosen.billRecord.entity.BillRecord;
import org.springblade.modules.guosen.bussiness.entity.Bussiness;
import org.springblade.modules.guosen.bussiness.entity.BussinessBonusAccount;
import org.springblade.modules.guosen.bussiness.service.IBussinessBonusAccountService;
import org.springblade.modules.guosen.bussiness.service.IBussinessDebitRequestService;
import org.springblade.modules.guosen.bussiness.service.IBussinessService;
import org.springblade.modules.system.service.IParamService;
import org.springblade.yeePay.entity.PayOrderDto;
import org.springblade.yeePay.event.RechargeServiceFeePayNotifyEvent;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.event.EventListener;
import org.springframework.scheduling.annotation.Async;
import org.springframework.stereotype.Component;

import java.math.BigDecimal;
import java.util.List;
import java.util.stream.Collectors;

@Component
@Slf4j
public class RechargeServiceFeePayNotifyListener {

    @Autowired
    private IBussinessDebitRequestService bussinessDebitRequestService;
    @Autowired
    private IBussinessService bussinessService;
    @Autowired
    private IParamService paramService;
    @Autowired
    private IBussinessBonusAccountService bussinessBonusAccountService;

    @EventListener
    @Async
    public void notify(RechargeServiceFeePayNotifyEvent rsfpEvent) {
        BillRecord billRecord = rsfpEvent.getBillRecord();
        if(billRecord != null){
            Long bussinessId = billRecord.getUserId();
            Bussiness bussiness = this.bussinessService.getById(bussinessId);
            //服务费金额 = 充值原始金额 * (总服务费率-基础手续费率-SaaS费率)
            BigDecimal rechargeAmount = billRecord.getPrice();
            BigDecimal totalServiceRate = bussiness.getServiceRate();
            BigDecimal baseRate = new BigDecimal(paramService.getValue("yee_base_rate")).multiply(new BigDecimal(10));//0.21 * 10 = 2.1
            BigDecimal saasBaseRate = new BigDecimal(paramService.getValue("yee_saas_rate")).multiply(new BigDecimal(10));//0.1 * 10 = 1
            BigDecimal payRate = (totalServiceRate.subtract(baseRate).subtract(saasBaseRate)).divide(new BigDecimal(1000));
            BigDecimal payAmount = rechargeAmount.multiply(payRate).setScale(2, BigDecimal.ROUND_HALF_UP);
            if(totalServiceRate.compareTo(baseRate) == -1){
                log.error("=======================充值服务费转账业务校验失败：服务费率低于基础费率，请重新设置服务费率========================");
                log.error("服务费率:" + totalServiceRate.toString());
                return;
            }
            PayOrderDto payOrderDto = new PayOrderDto();
            payOrderDto.setMerchantNo(bussiness.getMerchantNo());
            payOrderDto.setReceiveType("REAL_TIME");
            payOrderDto.setOrderAmount(payAmount.doubleValue());
            payOrderDto.setFeeChargeSide("PAYER");
            payOrderDto.setBussinessId(billRecord.getUserId());
            List<BussinessBonusAccount> bonusAccountList = bussinessBonusAccountService.list();
            BussinessBonusAccount account = bonusAccountList.stream().filter(item -> item.getReceiverAccountName().equals("广州士多啤梨传媒有限公司")).collect(Collectors.toList()).get(0);
            payOrderDto.setReceiverAccountNo(account.getReceiverAccountNo());
            payOrderDto.setReceiverAccountName(account.getReceiverAccountName());
            payOrderDto.setReceiverBankCode(account.getReceiverBankCode());
            this.bussinessDebitRequestService.payOrder(payOrderDto, "易宝回调");
        }
    }
}
