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

import com.alibaba.fastjson.JSONObject;
import com.jeequan.jeepay.core.constants.CS;
import com.jeequan.jeepay.core.entity.PayOrder;
import com.jeequan.jeepay.core.exception.BizException;
import com.jeequan.jeepay.core.model.context.MchAppConfigContext;
import com.jeequan.jeepay.core.model.oauth2.WxpayOauth2Params;
import com.jeequan.jeepay.core.model.params.ruipay.RyxpayIsvParams;
import com.jeequan.jeepay.core.model.rqrs.AbstractRS;
import com.jeequan.jeepay.core.model.rqrs.msg.ChannelRetMsg;
import com.jeequan.jeepay.core.model.rqrs.payorder.UnifiedOrderRQ;
import com.jeequan.jeepay.core.model.rqrs.payorder.payway.AliBarOrderRQ;
import com.jeequan.jeepay.core.model.rqrs.payorder.payway.WxBarOrderRQ;
import com.jeequan.jeepay.core.model.rqrs.payorder.payway.WxBarOrderRS;
import com.jeequan.jeepay.core.utils.SpringBeansUtil;
import com.jeequan.jeepay.thirdparty.channel.ryxpay.RyxpayKit;
import com.jeequan.jeepay.thirdparty.channel.ryxpay.RyxpayPaymentService;
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 com.jeequan.jeepay.thirdparty.util.ApiResBuilder;
import org.apache.commons.lang3.StringUtils;
import org.springframework.stereotype.Service;

/**
 * 瑞银信微信条码支付
 */
@Service("ryxpayPaymentByWxBarService") //Service Name需保持全局唯一性
public class WxBar extends RyxpayPaymentService {

    @Override
    public String preCheck(UnifiedOrderRQ rq, PayOrder payOrder) {
        WxBarOrderRQ bizRQ = (WxBarOrderRQ) rq;
        if(StringUtils.isEmpty(bizRQ.getAuthCode())){
            throw new BizException("用户支付条码[authCode]不可为空");
        }
        return null;
    }

    @Override
    public AbstractRS pay(UnifiedOrderRQ rq, PayOrder payOrder, MchAppConfigContext mchAppConfigContext){
        WxBarOrderRQ bizRQ = (WxBarOrderRQ) rq;
        WxBarOrderRS res = ApiResBuilder.buildSuccess(WxBarOrderRS.class);
        ChannelRetMsg channelRetMsg = new ChannelRetMsg();
        res.setChannelRetMsg(channelRetMsg);
        JSONObject bizContent = new JSONObject();
        RyxpayKit.setPayParams(mchAppConfigContext,bizContent,payOrder,rq);
        //加载商户的地区信息 瑞银信必要
        loadMchExtInfo(bizContent,mchAppConfigContext);
        bizContent.put("authCode", bizRQ.getAuthCode());
        ConfigContextQueryService configContextQueryService = SpringBeansUtil.getBean(ConfigContextQueryService.class);
        RyxpayIsvParams isvParams = (RyxpayIsvParams) configContextQueryService.queryIsvParams(mchAppConfigContext.getMchApplyment().getIsvNo(), getIfCode());
        WxpayOauth2Params oauth2Params = (WxpayOauth2Params) configContextQueryService.queryIsvOauth2Params(mchAppConfigContext.getMchApplyment().getIsvNo(), CS.IF_CODE.WXPAY);
        if(StringUtils.isNotEmpty(bizRQ.getSubAppid())){
            bizContent.put("subAppId", bizRQ.getSubAppid());
        }else{
            bizContent.put("subAppId", oauth2Params.getAppId());
        }
        PayRespEntity response = RyxpayKit.payReqApi(ReqMethod.ServiceCode.SMZF003, isvParams, bizContent, payOrder.getPayOrderId(), getNotifyUrl());
        initCommonResult(response,channelRetMsg,payOrder.getWayCodeType());
        return res;
    }

}
