package com.jeequan.jeepay.thirdparty.channel.ryxpay.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.entity.PayOrder;
import com.jeequan.jeepay.core.exception.BizException;
import com.jeequan.jeepay.core.model.context.MchAppConfigContext;
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.AliJsapiOrderRQ;
import com.jeequan.jeepay.core.model.rqrs.payorder.payway.AliJsapiOrderRS;
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;

/**
 * 瑞银信 支付宝 jsapi
 *
 */
@Service("ryxpayPaymentByAliJsapiService")
public class AliJsapi extends RyxpayPaymentService {

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

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

    @Override
    public AbstractRS pay(UnifiedOrderRQ rq, PayOrder payOrder, MchAppConfigContext mchAppConfigContext){
        AliJsapiOrderRS res = ApiResBuilder.buildSuccess(AliJsapiOrderRS.class);
        ChannelRetMsg channelRetMsg = new ChannelRetMsg();
        res.setChannelRetMsg(channelRetMsg);
        AliJsapiOrderRQ bizRQ = (AliJsapiOrderRQ) rq;
        JSONObject bizContent = new JSONObject();
        // 默认支付参数
        RyxpayKit.setPayParams(mchAppConfigContext, bizContent, payOrder,rq);
        //加载商户的地区信息 瑞银信必要
        loadMchExtInfo(bizContent,mchAppConfigContext);
        long between = DateUtil.between(payOrder.getCreatedAt(), payOrder.getExpiredTime(), DateUnit.MINUTE);
        bizContent.put("timeExpire", between);
        // 买家支付宝用户ID
        bizContent.put("buyerId", bizRQ.getBuyerUserId());
        // 配置参数
        ConfigContextQueryService configContextQueryService = SpringBeansUtil.getBean(ConfigContextQueryService.class);
        RyxpayIsvParams isvParams = (RyxpayIsvParams) configContextQueryService.queryIsvParams(mchAppConfigContext.getMchApplyment().getIsvNo(), getIfCode());
        PayRespEntity response = RyxpayKit.payReqApi(ReqMethod.ServiceCode.SMZF016, isvParams, bizContent, payOrder.getPayOrderId(), getNotifyUrl());
        initCommonResult(response,channelRetMsg,payOrder.getWayCodeType());
        res.setChannelRetMsg(channelRetMsg);
        if(channelRetMsg.getJsapiMsg() != null){
            res.setAlipayTradeNo(channelRetMsg.getJsapiMsg().getTradeNo());
            res.setPayData(JSON.toJSONString(channelRetMsg.getJsapiMsg()));
        }
        return res;
    }

}
