package com.softwarebr.nunu.service.pay.impl;

import com.alipay.api.response.AlipayTradeRefundResponse;
import com.brframework.commondb.core.AbstractEntityService;
import com.brframework.commondb.core.CommonRepository;
import com.github.binarywang.wxpay.bean.result.WxPayRefundResult;
import com.softwarebr.nunu.dao.pay.PayInfoDao;
import com.softwarebr.nunu.domain.WxPayOrder;
import com.softwarebr.nunu.entity.pay.PayInfo;
import com.softwarebr.nunu.service.pay.PaymentService;
import com.softwarebr.nunu.service.pay.ali.AliPayService;
import com.softwarebr.nunu.service.pay.weixin.WeiXinPayService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.time.LocalDateTime;

import static com.softwarebr.nunu.entity.pay.PayInfo.*;

/**
 * 支付业务处理
 *
 * @author lilin
 * @date 2019/11/5
 * @time 17:16
 */
@Service
@Slf4j
public class PaymentServiceImpl extends AbstractEntityService<PayInfo, Long, Object> implements PaymentService {

    @Autowired
    private AliPayService aliPayService;

    @Autowired
    private WeiXinPayService weiXinPayService;

    @Autowired
    private PayInfoDao payInfoDao;

    @Override
    public CommonRepository<Long, PayInfo> getRepository() {
        return payInfoDao;
    }

    @Override
    @Transactional
    public String payBuy(Long buyMemberId, String outTradeNo, Long totalAmount, String body, String subject, Integer payMethod, Integer type, Long targetId) {
        WxPayOrder wxPayUnifiedOrderResult;
        String result = null;
        PayInfo payInfoLog = findPayInfoLog(buyMemberId, type, targetId);
        if (payInfoLog == null) {
            payInfoLog = PayInfo.builder()
                    .createDate(LocalDateTime.now())
                    .payOrderNo(outTradeNo)
                    .memberId(buyMemberId)
                    .totalAmount(totalAmount)
                    .payMethod(payMethod)
                    .message(subject)
                    .type(type)
                    .targetId(targetId)
                    .status(CONSTANT_PAY_INFO_STATUS_WAIT)
                    .build();
            save(payInfoLog);
        }else {
            payInfoLog.setPayOrderNo(outTradeNo);
            payInfoLog.setPayMethod(payMethod);
            save(payInfoLog);
        }
        /*switch (payMethod) {
            case BuyOrder.PAY_METHOD_ALIPAY:
                AlipayTradeAppPayResponse response = aliPayService.tradeAppPay(outTradeNo, totalAmount, body,
                        subject, payTimeout, AliPayService.CLIENT_APP);
                result = response.getBody();
                break;
            case BuyOrder.PAY_METHOD_WX:
                wxPayUnifiedOrderResult = weiXinPayService.tradeCreate("", outTradeNo, totalAmount, body, WeiXinPayService.WEIXIN_TRADE_TYPE_APP);
                result = JSON.toJSONString(wxPayUnifiedOrderResult);
                break;
            default:
                throw new HandleException("找不到" + payMethod + "相关的支付方式");
        }*/
        return result;
    }

    @Override
    public PayInfo findByPayOrderNo(String payOrderNo) {
        return payInfoDao.findTopByPayOrderNo(payOrderNo);
    }

    @Override
    @Transactional
    public void paySuccess(String outTradeNo) {
        PayInfo payInfo = findByPayOrderNo(outTradeNo);
        if (payInfo != null) {
            payInfo.setStatus(CONSTANT_PAY_INFO_STATUS_CALLBACK);
            save(payInfo);
            switch (payInfo.getType()) {
                case CONSTANT_PAY_INFO_TYPE_BUY_ORDER:
                   // buyOrderService.orderPaySuccess(outTradeNo);
                    break;
                default:
            }
        }
    }

    @Override
    public String buyRefunded(String outTradeNo, Long refundAmount, Integer payMethod, String remark) {
        AlipayTradeRefundResponse response;
        WxPayRefundResult wxPayRefundResult;
        /*switch (payMethod) {
            case BuyOrder.PAY_METHOD_ALIPAY:
                response = aliPayService.tradeRefund(outTradeNo, refundAmount, AliPayService.CLIENT_APP);
                return JSON.toJSONString(response);
            case BuyOrder.PAY_METHOD_WX:
                wxPayRefundResult = weiXinPayService.tradeRefund(outTradeNo, refundAmount, refundAmount, WeiXinPayService.WEIXIN_TRADE_TYPE_APP, remark);
                return JSON.toJSONString(wxPayRefundResult);
            default:
                throw new HandleException("找不到" + payMethod + "相关的支付方式");
        }*/
        return null;
    }

    @Override
    public PayInfo findPayInfoLog(Long memberId, Integer type, Long targetId) {
        return payInfoDao.findTopByMemberIdAndTypeAndTargetId(memberId, type, targetId);
    }
}
