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

import cn.hutool.core.date.DateUnit;
import cn.hutool.core.date.DateUtil;
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.model.context.MchAppConfigContext;
import com.jeequan.jeepay.core.model.oauth2.WxpayOauth2Params;
import com.jeequan.jeepay.core.model.rqrs.AbstractRS;
import com.jeequan.jeepay.core.model.rqrs.msg.ChannelJsapiMsg;
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.WxJsapiOrderRQ;
import com.jeequan.jeepay.core.model.rqrs.payorder.payway.WxJsapiOrderRS;
import com.jeequan.jeepay.thirdparty.channel.kqpay.KqpayKit;
import com.jeequan.jeepay.thirdparty.channel.kqpay.KqpayPaymentService;
import com.jeequan.jeepay.thirdparty.channel.kqpay.model.KqpayResp;
import com.jeequan.jeepay.thirdparty.channel.kqpay.util.KqpayConst;
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 crystal
 *
 * @date 2022/6/13 17:40
 */
@Slf4j
@Service("kqpayPaymentByWxJsapiService")
public class WxJsapi extends KqpayPaymentService {

    @Override
    public String preCheck(UnifiedOrderRQ rq, PayOrder payOrder) {

        WxJsapiOrderRQ bizRQ = (WxJsapiOrderRQ) rq;
        if(StringUtils.isEmpty(bizRQ.getOpenid())){
            throw new BizException("[openid]不可为空");
        }
        return null;
    }

    @Override
    public AbstractRS pay(UnifiedOrderRQ rq, PayOrder payOrder, MchAppConfigContext mchAppConfigContext) throws Exception {
        WxJsapiOrderRS res = ApiResBuilder.buildSuccess(WxJsapiOrderRS.class);
        WxJsapiOrderRQ bizRQ = (WxJsapiOrderRQ) rq;
        ChannelRetMsg channelRetMsg = new ChannelRetMsg();
        res.setChannelRetMsg(channelRetMsg);
        JSONObject bizContent = new JSONObject();
        // 默认支付参数
        KqpayKit.setPayParams(mchAppConfigContext, bizContent);
        bizContent.put("amount",payOrder.getFindAmt());
        bizContent.put("userAgent","02");
        bizContent.put("subAppId",bizRQ.getSubAppid());
        bizContent.put("tr3Url",getNotifyUrl());
        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());
        }
        bizContent.put("openId", bizRQ.getOpenid());
        long between = DateUtil.between(payOrder.getCreatedAt(), payOrder.getExpiredTime(), DateUnit.SECOND);
        bizContent.put("txnExpireTime",between + "");
        bizContent.put("terminalIp",payOrder.getClientIp());
        bizContent.put("frontUrl",getReturnUrl(payOrder.getPayOrderId()));
        KqpayResp resp = KqpayKit.payRequest(KqpayConst.ReqMethod.A7009, mchAppConfigContext, bizContent,payOrder.getPayOrderId());
        this.initResult(resp,channelRetMsg);
        ChannelJsapiMsg jsapiMsg = channelRetMsg.getJsapiMsg();
        if(jsapiMsg != null ){
            String payInfo = JSON.toJSONString(jsapiMsg);
            res.setPayInfo(payInfo);
            res.setPayData(payInfo);
        }
        return res;
    }

}
