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

import cn.hutool.core.date.DatePattern;
import cn.hutool.core.date.DateUnit;
import cn.hutool.core.date.DateUtil;
import cn.hutool.core.util.URLUtil;
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.ChannelAppletPayMsg;
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.*;
import com.jeequan.jeepay.core.utils.SpringBeansUtil;
import com.jeequan.jeepay.thirdparty.channel.lklsb2bpay.Lklsb2bpayKit;
import com.jeequan.jeepay.thirdparty.channel.lklsb2bpay.Lklsb2bpayPaymentService;
import com.jeequan.jeepay.thirdparty.channel.lklsb2bpay.model.ILklTkReqMethod;
import com.jeequan.jeepay.thirdparty.channel.lklsb2bpay.model.Lklsb2bPayMethod;
import com.jeequan.jeepay.thirdparty.channel.lklspay.model.LklPayMethod;
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;

import java.math.BigDecimal;

/**
 * TODO
 * 收银台(B2B)
 * @author crystal
 * @date 2024/4/22 18:36
 */
@Service("lklsb2bpayPaymentByCashierService")
public class Cashier extends Lklsb2bpayPaymentService {

    @Override
    public String preCheck(UnifiedOrderRQ rq, PayOrder payOrder) {
        AliLiteOrderRQ bizRQ = (AliLiteOrderRQ) rq;
        if(StringUtils.isEmpty(bizRQ.getBuyerUserId())){
            throw new BizException("[buyerUserId]参数不可为空");
        }
        this.preCheckAli(bizRQ);
        return null;
    }

    /**
     * 渠道收银台
     * @param rq
     * @param payOrder
     * @param mchAppConfigContext
     * @return
     * @throws Exception
     */
    @Override
    public AbstractRS pay(UnifiedOrderRQ rq, PayOrder payOrder, MchAppConfigContext mchAppConfigContext) throws Exception {
        ChannelCashierOrderRS res = ApiResBuilder.buildSuccess(ChannelCashierOrderRS.class);
        ChannelRetMsg retMsg = ChannelRetMsg.waiting();
        res.setChannelRetMsg(retMsg);
        ChannelCashierOrderRQ bizRQ = (ChannelCashierOrderRQ) rq;
        JSONObject bizData = new JSONObject();
        bizData.put("merchant_no",payOrder.getChannelMchNo());
        bizData.put("total_amount",payOrder.getFindAmt());
        bizData.put("settle_type","0");
        bizData.put("notify_url", getNotifyUrl());
        bizData.put("out_order_no",payOrder.getPayOrderId());
        bizData.put("order_info",payOrder.getSubject());
        bizData.put("order_efficient_time", DateUtil.format(payOrder.getExpiredTime(), DatePattern.PURE_DATETIME_PATTERN));
        bizData.put("callback_url",payOrder.getReturnUrl());
        bizData.put("counter_remark",payOrder.getBuyerRemark());
        Integer hbFqNum = bizRQ.getHbFqNum();
        if(hbFqNum != null){
            JSONObject order_scene_field = new JSONObject(2);
            order_scene_field.put("order_scene_type","HB_FQ");
            JSONObject scene_info = new JSONObject(2);
            scene_info.put("hbFqNum",hbFqNum);
            //间连只支持0
            scene_info.put("hbFqSellerPercent",0);
            order_scene_field.put("scene_info",scene_info);
            bizData.put("order_scene_field",order_scene_field);
        }
        //支持退款
        bizData.put("support_refund","1");
        //支持多次支付
        bizData.put("support_repeat_pay","1");
        //TODO 针对多次支付的订单 增加以下处理 repeat_pay_auto_refund 0:重复支付后不自动退货 1：重复支付后自动退货
        bizData.put("repeat_pay_auto_refund","1");
        //TODO 针对多次支付的订单 增加以下处理 repeat_pay_notify 0:重复支付订单不通知；1:重复支付订单通知
        bizData.put("repeat_pay_notify","0");
        //TODO 针对多次支付的订单 增加以下处理 close_order_auto_refund 0:不自动退货；1:关闭订单后支付成功触发自动退货
        bizData.put("close_order_auto_refund","1");
        JSONObject respBizData = Lklsb2bpayKit.payRequest(Lklsb2bPayMethod.Api.CASHIER_ORDER, mchAppConfigContext.getMchApplyment(), bizData);
        String counter_url = respBizData.getString("counter_url");
        retMsg.setChannelBizData(respBizData);
        retMsg.setChannelOrderId(respBizData.getString("pay_order_no"));
        res.setPayUrl(counter_url);
        bulidLitePayInfo(res);
        return res;
    }

    public void bulidLitePayInfo(ChannelCashierOrderRS res) {
        ChannelAppletPayMsg payMsg = new ChannelAppletPayMsg();
        // 默认为：拉卡拉聚合收银台小程序的appId
        String appId = "wx889424d565967811";
        String ghId = "gh_a73225dbbe6b";
        if (res.getPayUrl().contains("q.huijingcai.top") || res.getPayUrl().contains("q.lakala.com")) {
            // 拉卡拉收款宝小程序appId
            appId = "wxc3e4d1682da3053c";
            ghId = "gh_3ca1d7144083";
        }
        String path = "payment-cashier/pages/checkout/index?source=WECHATMINI&counterUrl=" + URLUtil.encode(res.getPayUrl());
        payMsg.setAppId(appId);
        payMsg.setPath(path);
        payMsg.setGhId(ghId);
        res.getChannelRetMsg().setLiteMsg(payMsg);
    }
}
