package com.sonic.lion.service.impl;

import com.sonic.lion.domain.entity.PayTrade;
import com.sonic.lion.domain.input.CheckoutInput;
import com.sonic.lion.domain.input.PrePaymentInput;
import com.sonic.lion.domain.output.PrePaymentOutput;
import com.sonic.lion.domain.req.PrePaymentReq;
import com.sonic.lion.domain.resp.BuffCheckoutResp;
import com.sonic.lion.domain.resp.CheckoutResp;
import com.sonic.lion.enums.PayChannel;
import com.sonic.lion.enums.PaymentType;
import com.sonic.lion.enums.ToastResultCode;
import com.sonic.lion.enums.TradeStatus;
import com.sonic.lion.service.AccountBuffBillService;
import com.sonic.lion.service.CheckOutService;
import com.sonic.lion.service.PayTradeService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;

import static com.sonic.lion.enums.PaymentType.BALANCE;


@Slf4j
@Service
public class CheckOutServiceImpl implements CheckOutService {

    @Autowired
    private PayTradeService payTradeService;
    @Autowired
    private AccountBuffBillService accountBuffBillService;

    /**
     * 充值且支付操作，充值成功后调用付款的方法
     *
     * @param paymentTradeNo
     * @param srcUid
     */
    @Transactional(propagation = Propagation.REQUIRES_NEW)
    @Override
    public void chargeAfterPayment(String paymentTradeNo, Long srcUid) {
        log.info("充值并支付，充值后支付处理, paymentTradeNo: {}", paymentTradeNo);
        PayTrade paymentTrade = payTradeService.getByTradeNo(paymentTradeNo);
        if (paymentTrade == null || paymentTrade.getStatus() != TradeStatus.WAITPAY) {
            return;
        }
        try {
            CheckoutInput checkoutInput = CheckoutInput.builder()
                    .uid(srcUid)
                    .tradeNo(paymentTradeNo)
                    .paymentType(PaymentType.BALANCE)
                    .build();
            payTradeService.checkout(checkoutInput);
        } catch (Exception e) {
            log.warn("充值后支付处理扣款GAME异常", e);
        }
    }

    @Transactional(rollbackFor = Exception.class)
    @Override
    public BuffCheckoutResp checkout(PrePaymentReq req) {
        ToastResultCode.PARAM_ERROR.check(req.getDesAccountId() == null);
        PrePaymentInput input = PrePaymentInput.builder()
                .platform(req.getPlatform())
                .platformFee(req.getPlatformFee())
                .outTradeNo(req.getOutTradeNo())
                .outTradeNoRelationNo(req.getOutTradeNoRelationNo())
                .bizType(req.getBizType())
                .name(req.getName())
                .srcAccountId(req.getSrcAccountId())
                .desAccountNo(req.getDesAccountId().toString())
                .productAmount(req.getProductAmount())
                .promoAmount(req.getPromoAmount())
                .remark(req.getRemark())
                .srcAccountName("")
                .closeTime(req.getCloseTime())
                .resourceKey(req.getResourceKey())
                .resourceNum(req.getResourceNum())
                .extend(req.getExtend())
                .payChannel(PayChannel.STRIPE)
                .ip(req.getIp())
                .build();
        //结账预下单
        PrePaymentOutput prePaymentOutput = payTradeService.prePayment(input);
        //结账扣款
        CheckoutInput checkoutInput = CheckoutInput.builder()
                .uid(req.getSrcAccountId())
                .tradeNo(prePaymentOutput.getTradeNo())
                .payChannel(PayChannel.BUFF)
                .paymentType(BALANCE)
                .build();
        CheckoutResp checkoutResp = payTradeService.checkout(checkoutInput);
        BuffCheckoutResp result = new BuffCheckoutResp();
        BeanUtils.copyProperties(checkoutResp, result);
        return result;
    }

}
