package org.springblade.yeePay.listener;

import cn.hutool.json.JSONUtil;
import lombok.extern.slf4j.Slf4j;
import org.springblade.common.constant.SystemConstant;
import org.springblade.modules.guosen.billRecord.entity.BillRecord;
import org.springblade.modules.guosen.bussiness.entity.Bussiness;
import org.springblade.modules.guosen.bussiness.service.IBussinessService;
import org.springblade.modules.guosen.bussinessInfo.entity.BussinessInfo;
import org.springblade.modules.guosen.bussinessInfo.service.IBussinessInfoService;
import org.springblade.modules.guosen.bussinessTransferOrder.entity.BussinessTransferOrder;
import org.springblade.modules.guosen.bussinessTransferOrder.service.IBussinessTransferOrderService;
import org.springblade.modules.system.service.IParamService;
import org.springblade.yeePay.common.YeePayConst;
import org.springblade.yeePay.event.RechargeServiceFeeTransferNotifyEvent;
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.UUID;

@Component
@Slf4j
public class RechargeServiceFeeTransferNotifyListener {

    @Autowired
    private IBussinessService bussinessService;
    @Autowired
    private IParamService paramService;
    @Autowired
    private IBussinessTransferOrderService bussinessTransferOrderService;
    @Autowired
    private YeePayConst yeePayConst;
    @Autowired
    private IBussinessInfoService bussinessInfoService;

    @EventListener
    @Async
    public void notify(RechargeServiceFeeTransferNotifyEvent rsftEvent) {
        BillRecord billRecord = rsftEvent.getBillRecord();
        if(billRecord != null){
            Long bussinessId = billRecord.getUserId();
            Bussiness bussiness = bussinessService.getById(bussinessId);
            BussinessInfo parentBussinessInfo = this.bussinessInfoService.getByBussinessId(bussiness.getParentBussinessId());
            //转账金额 = 充值原始金额 * (商户服务费率-基础费率)
            BigDecimal rechargeAmount = billRecord.getPrice();
            BigDecimal serviceRate = 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 transferRate = (serviceRate.subtract(baseRate).subtract(saasBaseRate)).divide(new BigDecimal(1000));
            BigDecimal transferAmount = rechargeAmount.multiply(transferRate).setScale(2, BigDecimal.ROUND_HALF_UP);

            BussinessTransferOrder transferOrder = new BussinessTransferOrder();
            transferOrder.setRequestNo(UUID.randomUUID().toString().replaceAll("-","").toUpperCase());
            transferOrder.setOrderAmount(transferAmount.toString());
            transferOrder.setFromMerchantNo(bussiness.getMerchantNo());
            transferOrder.setToMerchantNo(parentBussinessInfo.getMerchantNo());
            transferOrder.setTransferUsage("采购");
            transferOrder.setFeeChargeSide("OUTSIDE");
            transferOrder.setTransferScene(SystemConstant.TransferScene.FOR_SERVICE_FEE.getValue());
            transferOrder.setTransferStatus(SystemConstant.TransferOrderStatus.WAIT.getValue());
            boolean save = bussinessTransferOrderService.save(transferOrder);
            BussinessInfo bussinessInfo = this.bussinessInfoService.getByMerchantNo(transferOrder.getFromMerchantNo());
            if(save){
                //发起充值服务费转账。
                bussinessTransferOrderService.transferOrder(transferOrder, bussinessInfo.getParentMerchantNo());
            }else{
                log.error("=======================充值服务费转账业务订单保存失败========================");
                log.error(JSONUtil.toJsonStr(transferOrder));
            }

        }
    }

    public static void main(String[] args) {
        BigDecimal rechargeAmount = new BigDecimal(307080);
        BigDecimal serviceRate = new BigDecimal(28);
        BigDecimal baseRate = new BigDecimal("0.21").multiply(new BigDecimal(10));//0.21 * 10 = 2.1
        BigDecimal saasBaseRate = new BigDecimal("0.1").multiply(new BigDecimal(10));//0.1 * 10 = 1
        BigDecimal transferRate = (serviceRate.subtract(baseRate).subtract(saasBaseRate)).divide(new BigDecimal(1000));
        BigDecimal transferAmount = rechargeAmount.multiply(transferRate).setScale(2, BigDecimal.ROUND_HALF_UP);
        System.out.println(transferAmount);
    }
}
