package com.jeequan.jeepay.thirdparty.channel.sxfpay;

import com.alibaba.fastjson.JSONObject;
import com.jeequan.jeepay.core.constants.CS;
import com.jeequan.jeepay.core.entity.PayOrder;
import com.jeequan.jeepay.core.interfaces.paychannel.IPayOrderQueryService;
import com.jeequan.jeepay.core.model.context.MchAppConfigContext;
import com.jeequan.jeepay.core.model.params.sxfpay.SxfpayIsvParams;
import com.jeequan.jeepay.core.model.rqrs.msg.ChannelRetMsg;
import com.jeequan.jeepay.thirdparty.channel.sxfpay.model.ReqMethod;
import com.jeequan.jeepay.thirdparty.service.ConfigContextQueryService;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

/**
 * 查询订单 随行付
 *
 * @author xiaoyu
 *
 * @date 2022/4/15 14:29
 */
@Service
@Slf4j
public class SxfpayPayOrderQueryService implements IPayOrderQueryService {

    public static final String SUCCESS = "SUCCESS";

    public static final String FAIL = "FAIL";

    public static final String PAYING = "PAYING";

    public static final String CLOSED = "CLOSED";

    public static final String CANCELED = "CANCELED";

    @Autowired
    private ConfigContextQueryService configContextQueryService;

    @Override
    public ChannelRetMsg query(PayOrder payOrder, MchAppConfigContext mchAppConfigContext) throws Exception {
        try {
            SxfpayIsvParams isvParams = (SxfpayIsvParams) configContextQueryService.queryIsvParams(mchAppConfigContext.getMchApplyment().getIsvNo(),payOrder.getIfCode());
            JSONObject bizContent = new JSONObject();
            // 商户号
            bizContent.put("mno", payOrder.getChannelMchNo());
            // 商户订单号
            bizContent.put("ordNo", payOrder.getPayOrderId());
            JSONObject respBizData = SxfpayKit.reqApi(ReqMethod.Method.TRADE_ORDER_QUERY, isvParams, bizContent);
            String tranSts = respBizData.getString("tranSts");
            String uuid = respBizData.getString("uuid");
            String sxfUuid = respBizData.getString("sxfUuid");
            String buyerId = respBizData.getString("buyerId");
            String channelDrType = respBizData.getString("drType");
            String drType = CS.DrType.OTHER.getType();
            if(StringUtils.isNotEmpty(channelDrType)){
                switch (channelDrType){
                    case "1":
                        drType = CS.DrType.DEBIT.getType();
                        break;
                    case "2":
                        drType = CS.DrType.CREDIT.getType();
                        break;
                }
            }
            String transactionId = respBizData.getString("transactionId");
            switch (tranSts){
                case SUCCESS:
                    ChannelRetMsg retMsg = ChannelRetMsg.confirmSuccess(uuid, transactionId, sxfUuid, drType);
                    retMsg.setChannelUserId(buyerId);
                    return retMsg;
                case FAIL:
                    return ChannelRetMsg.confirmFail(uuid,respBizData.getString("bizCode"),respBizData.getString("bizMsg"));
            }
            //支付中
            return ChannelRetMsg.waiting();
        }catch (Exception e) {
            //支付中
            return ChannelRetMsg.waiting();
        }
    }

}
