package com.ywt.alipaympapi.service.impl;

import com.alipay.api.domain.AlipayTradeCreateModel;
import com.alipay.api.domain.AlipayTradeQueryModel;
import com.alipay.api.response.AlipayTradeCreateResponse;
import com.alipay.api.response.AlipayTradeQueryResponse;
import com.ywt.alipaympapi.face.IPayFace;
import com.ywt.model.PageVO;
import com.ywt.request.pay.*;
import com.ywt.response.pay.*;
import com.ywt.alipaympapi.service.IsvPayService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;

import javax.annotation.Resource;

@Slf4j
@Service
public class IsvPayServiceImpl implements IsvPayService {
    @Resource
    private IPayFace payFace;

    @Override
    public TradeNoResponseData queryTradeNo(OutTradeNoRequest request) {
        return null;
    }

    @Override
    public TradeResultResponseData informTradeResult(TradeResultRequest request) {
        TradeResultResponseData data = new TradeResultResponseData();

        AlipayTradeQueryModel alipayTradeQueryModel = new AlipayTradeQueryModel();
        alipayTradeQueryModel.setTradeNo(request.getTradeNo());
        AlipayTradeQueryResponse response = payFace.tradeQuery(request.getAppId(), alipayTradeQueryModel);
        if ("TRADE_SUCCESS".equals(response.getTradeStatus())) {
            data.setStatus("TRADE_SUCCESS");
        }

        return data;
    }

    @Override
    public PayDetailResponseData queryPayDetail(PayDetailRequest request) {
        return null;
    }

    @Override
    public PageVO<PayHistoryResponseData> queryPayHistory(PayHistoryRequest request) {
        return null;
    }

    @Override
    public ComputeSelfPaidOrderResponseData computeSelfPaidOrder(ComputeSelfPaidOrderRequest request) {
        return null;
    }

    @Override
    public ComputeMedicarePayOrderResponseData computeMedicarePayOrder(ComputeMedicarePayOrderRequest request) {
        return null;
    }

    @Override
    public TradeNoForOrderResponseData tradeNoForOrder(TradeNoForOrderRequest request) {
        TradeNoForOrderResponseData data = new TradeNoForOrderResponseData();
        data.setNeedPay(true);//设置是否需要支付宝自费支付
        data.setOutTradeNo("查询订单系统获取订单号");
        data.setTradeNo(null);

        if (data.isNeedPay()) {
            String tradeNo = getTradeNo(request.getAppId(), data.getOutTradeNo());
            data.setTradeNo(tradeNo);
        }
        return data;
    }


    private String getTradeNo(String appId, String outTradeNo) {
        AlipayTradeCreateModel createModel = new AlipayTradeCreateModel();
        createModel.setOutTradeNo(outTradeNo);
        createModel.setTotalAmount("使用真正的系统");
        createModel.setSubject("主题主题");
        createModel.setBuyerId("购买者UID");

        AlipayTradeCreateResponse response = payFace.tradeCreate(appId, createModel);

        if (!StringUtils.isEmpty(response.getOutTradeNo())) {
            return response.getTradeNo();// 获取返回的tradeNO。
        } else {
            throw new RuntimeException("查询交易失败");
        }
    }
}
