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.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.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.WxJsapiOrderRQ;
import com.jeequan.jeepay.core.model.rqrs.payorder.payway.WxJsapiOrderRS;
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 lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.springframework.stereotype.Service;

/**
 * 瑞银信 微信jsapi
 *
 * @author xiaoyu
 *
 * @date 2022/4/15 15:37
 */
@Slf4j
@Service("ryxpayPaymentByWxJsapiService") //Service Name需保持全局唯一性
public class WxJsapi extends RyxpayPaymentService {

    @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);
        ChannelRetMsg channelRetMsg = new ChannelRetMsg();
        res.setChannelRetMsg(channelRetMsg);
        try {
            ConfigContextQueryService configContextQueryService = SpringBeansUtil.getBean(ConfigContextQueryService.class);
            WxpayOauth2Params oauth2Params = (WxpayOauth2Params) configContextQueryService.queryIsvOauth2Params(mchAppConfigContext.getMchApplyment().getIsvNo(), CS.IF_CODE.WXPAY);
            // 设置请求参数
            WxJsapiOrderRQ bizRQ = (WxJsapiOrderRQ) 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);
            bizContent.put("userId", bizRQ.getOpenid());
            if (StringUtils.isNotEmpty(bizRQ.getSubAppid())) {
                bizContent.put("subAppId", bizRQ.getSubAppid());
            }else {
                bizContent.put("subAppId", oauth2Params.getAppId());
            }
            RyxpayIsvParams isvParams = (RyxpayIsvParams) configContextQueryService.queryIsvParams(mchAppConfigContext.getMchApplyment().getIsvNo(), getIfCode());
            PayRespEntity response = RyxpayKit.payReqApi(ReqMethod.ServiceCode.SMZF010, isvParams, bizContent,payOrder.getPayOrderId(),getNotifyUrl());
            initCommonResult(response,channelRetMsg,payOrder.getWayCodeType());
            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());
        }
    }

}
