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

import com.alibaba.fastjson.JSON;
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.exception.ChannelException;
import com.jeequan.jeepay.core.model.context.MchAppConfigContext;
import com.jeequan.jeepay.core.model.oauth2.WxpayOauth2Params;
import com.jeequan.jeepay.core.model.params.sxfpay.SxfpayIsvParams;
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.WxLiteOrderRQ;
import com.jeequan.jeepay.core.model.rqrs.payorder.payway.WxLiteOrderRS;
import com.jeequan.jeepay.core.utils.SpringBeansUtil;
import com.jeequan.jeepay.thirdparty.channel.sxfpay.SxfpayKit;
import com.jeequan.jeepay.thirdparty.channel.sxfpay.SxfpayPaymentService;
import com.jeequan.jeepay.thirdparty.channel.sxfpay.model.ReqMethod;
import com.jeequan.jeepay.thirdparty.service.ConfigContextQueryService;
import com.jeequan.jeepay.thirdparty.util.ApiResBuilder;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.springframework.stereotype.Service;

/**
 * 银盛支付 微信jsapi
 *
 * @author xiaoyu
 *
 * @date 2022/6/13 17:52
 */
@Slf4j
@Service("sxfpayPaymentByWxLiteService") //Service Name需保持全局唯一性
public class WxLite extends SxfpayPaymentService {

    @Override
    public String preCheck(UnifiedOrderRQ rq, PayOrder payOrder) {
        WxLiteOrderRQ bizRQ = (WxLiteOrderRQ) rq;
        if(StringUtils.isEmpty(bizRQ.getOpenid())){
            throw new BizException("[openId]不可为空");
        }
        return null;
    }

    @Override
    public AbstractRS pay(UnifiedOrderRQ rq, PayOrder payOrder, MchAppConfigContext mchAppConfigContext) throws Exception {
        WxLiteOrderRS res = ApiResBuilder.buildSuccess(WxLiteOrderRS.class);
        ChannelRetMsg channelRetMsg = new ChannelRetMsg();
        res.setChannelRetMsg(channelRetMsg);
        try {
            // 设置请求参数
            WxLiteOrderRQ bizRQ = (WxLiteOrderRQ) rq;
            ConfigContextQueryService configContextQueryService = SpringBeansUtil.getBean(ConfigContextQueryService.class);
            WxpayOauth2Params oauth2Params = (WxpayOauth2Params) configContextQueryService.queryIsvOauth2Params(mchAppConfigContext.getMchApplyment().getIsvNo(), CS.IF_CODE.WXPAY);
            // 商户号
            // 设置请求参数
            JSONObject bizContent = new JSONObject();
            // 默认支付参数
            SxfpayKit.setPayParams(mchAppConfigContext, bizContent, payOrder, rq);
            // 商户订单号
            bizContent.put("payWay", "03");
            // 订单交易日期
            bizContent.put("userId", bizRQ.getOpenid());
            bizContent.put("notifyUrl", getNotifyUrl());
            // 接口上传的appId
            if (StringUtils.isNotEmpty(bizRQ.getSubAppid())) {
                // 商户appId
                bizContent.put("subAppid", bizRQ.getSubAppid());
            }else {
                // 商户appId
                bizContent.put("subAppid", oauth2Params.getLiteAppId());
            }
            SxfpayIsvParams isvParams = (SxfpayIsvParams) configContextQueryService.queryIsvParams(mchAppConfigContext.getMchApplyment().getIsvNo(), getIfCode());
            JSONObject respBizData = SxfpayKit.reqApi(ReqMethod.Method.TRADE_JSAPI_PAY, isvParams, bizContent);
            initCommonResult(respBizData,channelRetMsg);
            res.setChannelRetMsg(channelRetMsg);
            if(channelRetMsg.getJsapiMsg() != null){
                res.setPayInfo(JSON.toJSONString(channelRetMsg.getJsapiMsg()));
                res.setPayData(JSON.toJSONString(channelRetMsg.getJsapiMsg()));
            }
            return res;
        }catch (Exception e) {
            throw ChannelException.sysError(e.getMessage());
        }
    }
}
