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

import cn.hutool.core.date.DatePattern;
import cn.hutool.core.date.DateUtil;
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.ruipay.RyxpayIsvParams;
import com.jeequan.jeepay.core.model.rqrs.msg.ChannelRetMsg;
import com.jeequan.jeepay.thirdparty.channel.ryxpay.model.PayRespEntity;
import com.jeequan.jeepay.thirdparty.channel.ryxpay.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 RyxpayPayOrderQueryService implements IPayOrderQueryService {

    @Autowired
    private ConfigContextQueryService configContextQueryService;

    @Override
    public ChannelRetMsg query(PayOrder payOrder, MchAppConfigContext mchAppConfigContext) throws Exception {
        try {
            RyxpayIsvParams isvParams = (RyxpayIsvParams) configContextQueryService.queryIsvParams(mchAppConfigContext.getMchApplyment().getIsvNo(),payOrder.getIfCode());
            JSONObject bizContent = new JSONObject();
            bizContent.put("merchantCode", payOrder.getChannelMchNo());
            bizContent.put("oriReqDate", DateUtil.format(payOrder.getCreatedAt(), DatePattern.PURE_DATE_PATTERN));  
            bizContent.put("oriReqMsgId",payOrder.getPayOrderId());
            PayRespEntity response = RyxpayKit.payReqApi(ReqMethod.ServiceCode.SMZF006, isvParams, bizContent, null, null);
            JSONObject bizData = response.getData();
            String oriRespType = bizData.getString("oriRespType");
            if(RyxpayKit.RESP_BIZ_ING.equals(oriRespType)){
                return ChannelRetMsg.waiting();
            }else if(RyxpayKit.RESP_BIZ_FAIL.equals(oriRespType)){
                return ChannelRetMsg.confirmFail(response.getData().getString("oriRespCode"),response.getData().getString("oriRespMsg"));
            }else if(RyxpayKit.RESP_BIZ_SUCCESS.equals(oriRespType)){
                String payType = response.getData().getString("payType");
                String drType = CS.DrType.OTHER.getType();
                if(StringUtils.isNotEmpty(payType)){
                    switch (payType){
                        case "1":
                            drType = CS.DrType.DEBIT.getType();
                            break;
                        case "2":
                            drType = CS.DrType.DEBIT.getType();
                            break;
                    }
                }
                return ChannelRetMsg.confirmSuccess(response.getSmzfMsgId(),response.getData().getString("channelTradeNo") ,response.getChannelMsgId(),drType);
            }
            return ChannelRetMsg.waiting();
        }catch (Exception e) {
            //支付中
            return ChannelRetMsg.waiting();
        }
    }
}
