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.entity.PayOrder;
import com.jeequan.jeepay.core.exception.BizException;
import com.jeequan.jeepay.core.model.context.MchAppConfigContext;
import com.jeequan.jeepay.core.model.rqrs.AbstractRS;
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.AliJsapiOrderRQ;
import com.jeequan.jeepay.core.model.rqrs.payorder.payway.AliJsapiOrderRS;
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支付
 */
@Slf4j
@Service("lklspayPaymentByAliJsapiService")
public class AliJsapi extends LklspayPaymentService {

    @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();
        AliJsapiOrderRQ bizRQ = (AliJsapiOrderRQ) rq;
        JSONObject bizContent = new JSONObject();
        LklspayKit.setPayParams(mchAppConfigContext,bizContent,payOrder.getClientIp());
        bizContent.put("out_trade_no",payOrder.getPayOrderId());
        bizContent.put("subject",payOrder.getSubject());
        bizContent.put("remark",payOrder.getBuyerRemark());
        bizContent.put("account_type", "ALIPAY");
        bizContent.put("trans_type","51");
        bizContent.put("total_amount",payOrder.getFindAmt());
        bizContent.put("notify_url",getNotifyUrl());
        bizContent.put("settle_type","0");
        JSONObject acc_busi_fields = new JSONObject();
        Integer hbFqNum = bizRQ.getHbFqNum();
        if(hbFqNum != null){
            JSONObject extend_params = new JSONObject();
            extend_params.put("hb_fq_num",hbFqNum);
            extend_params.put("hb_fq_seller_percent",0);
            acc_busi_fields.put("extend_params",extend_params);
        }
        Integer limitPay = bizRQ.getLimitPay();
        if(limitPay != null && limitPay == 1){
            acc_busi_fields.put("disable_pay_channels","credit_group");
        }
        acc_busi_fields.put("user_id",bizRQ.getBuyerUserId());
        String expiredTime = DateUtil.formatBetween(DateUtil.between(payOrder.getCreatedAt(), payOrder.getExpiredTime(), DateUnit.MINUTE)).replace("毫秒", "");
        acc_busi_fields.put("timeout_express",expiredTime);
        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.setAlipayTradeNo(payInfo.getString("prepay_id"));
            ChannelJsapiMsg jsapiMsg = new ChannelJsapiMsg(payInfo.getString("prepay_id"));
            res.setPayData(JSON.toJSONString(jsapiMsg));
        }
        res.setChannelRetMsg(channelRetMsg);
        return res;
    }

}
