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.WxLiteOrderRQ;
import com.jeequan.jeepay.core.model.rqrs.payorder.payway.WxLiteOrderRS;
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;

/*
 * 快钱 小程序支付
 *
 * @author crystal
 *
 * @date 2021/6/8 18:08
 */
@Service("kqpayPaymentByKqWxLiteService") //Service Name需保持全局唯一性
@Slf4j
public class WxLite extends KqpayPaymentService {

    @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);
        WxLiteOrderRQ bizRQ = (WxLiteOrderRQ) rq;
        JSONObject bizContent = new JSONObject();
        // 默认支付参数
        KqpayKit.setPayParams(mchAppConfigContext, bizContent);
        bizContent.put("amount",payOrder.getFindAmt());
        bizContent.put("userAgent","01");
        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.getLiteAppId());
        }
        bizContent.put("tr3Url",getNotifyUrl());
        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 ){
            // 获取payInfo
            String payInfo = JSON.toJSONString(jsapiMsg);
            res.setPayInfo(payInfo);
            res.setPayData(payInfo);
        }
        return res;
    }

}
