package com.izouma.nineth.service;

import com.izouma.nineth.config.RedisKeys;
import com.izouma.nineth.domain.DivMoney;
import com.izouma.nineth.domain.TransferMoneyInfo;
import com.izouma.nineth.enums.DivStatus;
import com.izouma.nineth.enums.TransferStatus;
import com.izouma.nineth.exception.BusinessException;
import com.izouma.nineth.lock.RedisLockable;
import com.izouma.nineth.repo.DivMoneyRepo;
import com.izouma.nineth.repo.TransferMoneyInfoRepo;
import com.izouma.nineth.service.pay.UniformPayConstants;
import lombok.AllArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;

@Service
@AllArgsConstructor
@Slf4j
public class DivMoneyExecuteService {

    private IkudotpayService ikudotpayService;

    private SandpayService sandpayService;

    private AlipayService alipayService;

    private SumpayMerchantService sumpayMerchantService;

    private PayeasenetService payeasenetService;

    private DivMoneyRepo divMoneyRepo;

    private TransferMoneyInfoRepo transferMoneyInfoRepo;

    /**
     * 执行分账操作
     *
     * @param divMoneyId
     * @param divReason
     * @return
     */
    @RedisLockable(prefix = RedisKeys.TRANSFER_MONEY_LOCK, key = "#divMoneyId", expiration = 360)
    public TransferMoneyInfo executeDivMoney(Long divMoneyId, String divReason, String batchNo) {
        TransferMoneyInfo transferMoneyInfo = null;
        DivMoney divMoney = divMoneyRepo.findById(divMoneyId).orElseThrow(new BusinessException("分账记录不存在"));
        if (divMoney.getStatus() == DivStatus.PROCESSED) {
            throw new BusinessException("分账记录已被处理，请勿重复操作");
        }

        if (UniformPayConstants.IKUDOTPAY_PLATFORM.equals(divMoney.getPayPlatform())) {
            transferMoneyInfo = ikudotpayService.transferDivMoney(divMoneyId, divReason);
        } else if (UniformPayConstants.SANDPAY_PLATFORM.equals(divMoney.getPayPlatform())) {
            transferMoneyInfo = sandpayService.transferDivMoney(divMoneyId, divReason);
        } else if (UniformPayConstants.CUSTOM_PLATFORM.equals(divMoney.getPayPlatform())) {
            transferMoneyInfo = alipayService.transferDivMoney(divMoneyId, divReason);
        } else if (UniformPayConstants.SUMPAY_PLATFORM.equals(divMoney.getPayPlatform())) {
            transferMoneyInfo = sumpayMerchantService.withdrawDivMoney(divMoneyId, divReason);
        } else if(UniformPayConstants.PAYEASENET_PLATFORM.equals(divMoney.getPayPlatform())){
            transferMoneyInfo = payeasenetService.transferDivMoney(divMoneyId, divReason);
        } else {
            throw new BusinessException("不支持的分账:" + divMoney.getId() + ",交易平台：" + divMoney.getPayPlatform());
        }

        transferMoneyInfo.setBatchNo(batchNo);
        transferMoneyInfoRepo.save(transferMoneyInfo);
        return transferMoneyInfo;
    }


    /**
     * 检查转账结果
     *
     * @param transferMoneyId
     * @return
     */
    public TransferMoneyInfo checkTransferResult(Long transferMoneyId) throws Exception {
        TransferMoneyInfo transferMoneyInfo = transferMoneyInfoRepo.findById(transferMoneyId).orElseThrow(new BusinessException("转账记录不存在"));
        if (UniformPayConstants.SANDPAY_PLATFORM.equals(transferMoneyInfo.getPayPlatform())) {
            transferMoneyInfo = sandpayService.checkTransferResult(transferMoneyInfo.getId());
        } else if (UniformPayConstants.SUMPAY_PLATFORM.equals(transferMoneyInfo.getPayPlatform())) {
            transferMoneyInfo = sumpayMerchantService.checkTransferResult(transferMoneyInfo.getId());
        }else if(UniformPayConstants.PAYEASENET_PLATFORM.equals(transferMoneyInfo.getPayPlatform())){
            transferMoneyInfo = payeasenetService.checkTransferResult(transferMoneyInfo.getId());
        }



        if (transferMoneyInfo != null && transferMoneyInfo.getStatus() == TransferStatus.FAILED) {
            //更新分账记录为失败
            DivMoney divMoney = divMoneyRepo.findById(transferMoneyInfo.getDivMoneyId()).orElse(null);
            if (divMoney != null) {
                divMoney.setStatus(DivStatus.ERROR);
                divMoney.setTransferResult(transferMoneyInfo.getResult());
                divMoneyRepo.save(divMoney);
            }
        }

        return transferMoneyInfo;

    }


}
