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.AlipayOauth2Params;
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.AliLiteOrderRQ;
import com.jeequan.jeepay.core.model.rqrs.payorder.payway.AliLiteOrderRS;
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("kqpayPaymentByKqAliLiteService") //Service Name需保持全局唯一性
@Slf4j
public class AliLite extends KqpayPaymentService {

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

        AliLiteOrderRQ bizRQ = (AliLiteOrderRQ) rq;
        if(StringUtils.isEmpty(bizRQ.getBuyerUserId())){
            throw new BizException("[buyerUserId]参数不可为空");
        }
        return null;
    }

    @Override
    public AbstractRS pay(UnifiedOrderRQ rq, PayOrder payOrder, MchAppConfigContext mchAppConfigContext) throws Exception{
        AliLiteOrderRS res = ApiResBuilder.buildSuccess(AliLiteOrderRS.class);
        ChannelRetMsg channelRetMsg = new ChannelRetMsg();
        res.setChannelRetMsg(channelRetMsg);
        AliLiteOrderRQ bizRQ = (AliLiteOrderRQ) rq;
        JSONObject bizContent = new JSONObject();
        // 默认支付参数
        KqpayKit.setPayParams(mchAppConfigContext, bizContent);

        bizContent.put("amount",payOrder.getFindAmt());
        bizContent.put("userAgent","04");
        AlipayOauth2Params oauth2Params = (AlipayOauth2Params) configContextQueryService.queryIsvOauth2Params(mchAppConfigContext.getMchApplyment().getIsvNo(), CS.IF_CODE.ALIPAY);
        bizContent.put("subAppId",oauth2Params.getPid());
        bizContent.put("tr3Url",getNotifyUrl());
        bizContent.put("openId", bizRQ.getBuyerUserId());
        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
            res.setAlipayTradeNo(jsapiMsg.getTradeNo());
            res.setPayData(JSON.toJSONString(jsapiMsg));
        }
        return res;
    }

}
