package com.jeequan.jeepay.thirdparty.channel.lklspay.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.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.lklspay.LklspayKit;
import com.jeequan.jeepay.thirdparty.channel.lklspay.LklspayPaymentService;
import com.jeequan.jeepay.thirdparty.channel.lklspay.model.LklPayMethod;
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/6/13 17:52
 */
@Slf4j
@Service("lklspayPaymentByWxLiteService") //Service Name需保持全局唯一性
public class WxLite extends LklspayPaymentService {

    @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 {
        WxpayOauth2Params oauth2Params = (WxpayOauth2Params) configContextQueryService.queryIsvOauth2Params(mchAppConfigContext.getMchApplyment().getIsvNo(), CS.IF_CODE.WXPAY);
        WxLiteOrderRS res = ApiResBuilder.buildSuccess(WxLiteOrderRS.class);
        ChannelRetMsg channelRetMsg = new ChannelRetMsg();
        // 设置请求参数
        WxLiteOrderRQ bizRQ = (WxLiteOrderRQ) rq;
        JSONObject bizContent = new JSONObject();
        LklspayKit.setPayParams(mchAppConfigContext, bizContent,payOrder.getClientIp());
        bizContent.put("out_trade_no",payOrder.getPayOrderId());
        bizContent.put("account_type","WECHAT");
        bizContent.put("trans_type","71");
        bizContent.put("total_amount",payOrder.getFindAmt());
        bizContent.put("subject",payOrder.getSubject());
        bizContent.put("notify_url",getNotifyUrl());
        bizContent.put("remark",payOrder.getBuyerRemark());
        JSONObject acc_busi_fields = new JSONObject();
        String expiredTime = DateUtil.formatBetween(DateUtil.between(payOrder.getCreatedAt(), payOrder.getExpiredTime(), DateUnit.MINUTE)).replace("毫秒", "");
        acc_busi_fields.put("timeout_express",expiredTime);
        if(StringUtils.isNotEmpty(bizRQ.getSubAppid())){
            acc_busi_fields.put("sub_appid",bizRQ.getSubAppid());
        }else{
            acc_busi_fields.put("sub_appid",oauth2Params.getLiteAppId());
        }
        acc_busi_fields.put("user_id",bizRQ.getOpenid());
        if(bizRQ.getLimitPay() != null && bizRQ.getLimitPay() == 1){
            acc_busi_fields.put("limit_pay","no_credit");
        }
        bizContent.put("acc_busi_fields",acc_busi_fields);
        JSONObject response = LklspayKit.payRequest(LklPayMethod.Api.TRANS_PREORDER, mchAppConfigContext.getMchApplyment(), bizContent);
        JSONObject payInfo = this.initResult(response, channelRetMsg);
        if(payInfo != null){
            res.setPayInfo(JSON.toJSONString(channelRetMsg.getJsapiMsg()));
            res.setPayData(JSON.toJSONString(channelRetMsg.getJsapiMsg()));
        }
        res.setChannelRetMsg(channelRetMsg);
        return res;
    }
}
