package com.jeequan.jeepay.thirdparty.channel.lklspay.payway;

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.WxBarOrderRQ;
import com.jeequan.jeepay.core.model.rqrs.payorder.payway.WxBarOrderRS;
import com.jeequan.jeepay.core.utils.SpringBeansUtil;
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.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;

/**
 * 拉卡拉 微信 条码支付
 *
 * @author crystal
 */
@Slf4j
@Service("lklspayPaymentByWxBarService") //Service Name需保持全局唯一性
public class WxBar extends LklspayPaymentService {

    @Override
    public String preCheck(UnifiedOrderRQ rq, PayOrder payOrder) {
        WxBarOrderRQ bizRQ = (WxBarOrderRQ) rq;
        if(StringUtils.isEmpty(bizRQ.getAuthCode())){
            throw new BizException("用户支付条码[authCode]不可为空");
        }
        return null;
    }

    @Override
    public AbstractRS pay(UnifiedOrderRQ rq, PayOrder payOrder, MchAppConfigContext mchAppConfigContext) throws Exception {

        WxBarOrderRQ bizRQ = (WxBarOrderRQ) rq;
        WxBarOrderRS res = ApiResBuilder.buildSuccess(WxBarOrderRS.class);
        ChannelRetMsg channelRetMsg = new ChannelRetMsg();
        JSONObject bizContent = new JSONObject();
        ConfigContextQueryService configContextQueryService = SpringBeansUtil.getBean(ConfigContextQueryService.class);
        WxpayOauth2Params oauth2Params = (WxpayOauth2Params) configContextQueryService.queryIsvOauth2Params(mchAppConfigContext.getMchApplyment().getIsvNo(), CS.IF_CODE.WXPAY);
        // 默认支付参数
        LklspayKit.setPayParams(mchAppConfigContext, bizContent,payOrder.getClientIp());
        bizContent.put("out_trade_no",payOrder.getPayOrderId());
        bizContent.put("auth_code",bizRQ.getAuthCode());
        bizContent.put("total_amount",payOrder.getFindAmt());
        bizContent.put("subject",payOrder.getSubject());
        bizContent.put("notify_url",getNotifyUrl());
        bizContent.put("settle_type","0");
        bizContent.put("remark",payOrder.getBuyerRemark());
        JSONObject acc_busi_fields = new JSONObject();
        Integer limitPay = bizRQ.getLimitPay();
        if(limitPay != null && limitPay == 1){
            acc_busi_fields.put("limit_pay","no_credit");
        }
        if(StringUtils.isNotEmpty(bizRQ.getSubAppid())){
            bizContent.put("sub_appid",bizRQ.getSubAppid());
        }else{
            bizContent.put("sub_appid",oauth2Params.getAppId());
        }
        bizContent.put("acc_busi_fields",acc_busi_fields);
        JSONObject response = LklspayKit.payRequest(LklPayMethod.Api.TRANS_MICROPAY, mchAppConfigContext.getMchApplyment(), bizContent);
        this.initResult(response,channelRetMsg);
        res.setChannelRetMsg(channelRetMsg);
        return res;
    }

}
