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

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.entity.RefundOrder;
import com.jeequan.jeepay.core.exception.BizException;
import com.jeequan.jeepay.core.model.context.MchAppConfigContext;
import com.jeequan.jeepay.core.model.rqrs.msg.ChannelRefundLimit;
import com.jeequan.jeepay.core.model.rqrs.msg.ChannelRetMsg;
import com.jeequan.jeepay.core.model.rqrs.refund.RefundOrderRQ;
import com.jeequan.jeepay.db.entity.MchApplyment;
import com.jeequan.jeepay.service.impl.MchApplymentService;
import com.jeequan.jeepay.thirdparty.channel.AbstractRefundService;
import com.jeequan.jeepay.thirdparty.channel.lklsb2bpay.Lklsb2bpayKit;
import com.jeequan.jeepay.thirdparty.channel.lklsb2bpay.model.Lklsb2bPayMethod;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.math.BigDecimal;

/**
 * 退款接口： 拉卡拉
 *
 * @author xiaoyu
 *
 * @date 2022/4/15 9:34
 */
@Service
public class Lklsb2bpayRefundService extends AbstractRefundService {

    @Autowired
    private MchApplymentService mchApplymentService;

    @Override
    public String getIfCode() {
        return CS.IF_CODE.LKLSB2BPAY;
    }

    @Override
    public String preCheck(RefundOrderRQ bizRQ, RefundOrder refundOrder, PayOrder payOrder) {
        ChannelRefundLimit refundLimit = this.isRefundLimit(payOrder.getSettleType(), payOrder.getMchExtNo());
        if(refundLimit.getIsPlatAccount()){
            if(ChannelRefundLimit.NO_OPEN == refundLimit.getBindStatus()){
                throw new BizException("拉卡拉通道D0退款需要先申请平台户退款");
            }else{
                this.checkPlatAccount(refundOrder);
            }
        }
        return null;
    }

    @Override
    public ChannelRetMsg refund(RefundOrderRQ bizRQ, RefundOrder refundOrder, PayOrder payOrder, MchAppConfigContext mchAppConfigContext) throws Exception {
        ChannelRetMsg channelRetMsg = new ChannelRetMsg();
        JSONObject bizData = new JSONObject();
        bizData.put("merchant_no",payOrder.getChannelMchNo());
        bizData.put("out_trade_no",refundOrder.getRefundOrderId());
        bizData.put("origin_trade_no",refundOrder.getPayOrderId());
        String channelBizData = payOrder.getChannelBizData();
        if(StringUtils.isNotEmpty(channelBizData)){
            JSONObject channelData = JSON.parseObject(channelBizData);
            bizData.put("term_no",channelData.getString("term_no"));
        }
        bizData.put("refund_amount",refundOrder.getRefundAmount());
        bizData.put("refund_reason",refundOrder.getRefundReason());
        bizData.put("origin_trade_no",payOrder.getPlatformOrderNo());
        JSONObject location_info = new JSONObject();
        location_info.put("request_ip",refundOrder.getClientIp());
        bizData.put("location_info",location_info);
        JSONObject respBizData = Lklsb2bpayKit.payRequest(Lklsb2bPayMethod.Api.ORDER_REFUND, mchAppConfigContext.getMchApplyment(), bizData);
        channelRetMsg.setChannelBizData(respBizData);
        channelRetMsg.setChannelOrderId(respBizData.getString("log_no"));
        channelRetMsg.setPlatformMchOrderNo(respBizData.getString("trade_no"));
        channelRetMsg.setPlatformOrderNo(respBizData.getString("acc_trade_no"));
        channelRetMsg.setChannelState(ChannelRetMsg.ChannelState.CONFIRM_SUCCESS);
        return channelRetMsg;
    }

    @Override
    public ChannelRetMsg query(RefundOrder refundOrder, MchAppConfigContext mchAppConfigContext) throws Exception {
        ChannelRetMsg channelRetMsg = new ChannelRetMsg();
        channelRetMsg.setChannelState(ChannelRetMsg.ChannelState.WAITING); //认为是处理中
        return channelRetMsg;
    }

    /**
     * 退款权限
     * @return
     */
    @Override
    public ChannelRefundLimit isRefundLimit(String settleType,String applyId) {
        ChannelRefundLimit channelRefundLimit = new ChannelRefundLimit(false,true);
        if(CS.SETTLEMENT_TYPE.D0.equals(settleType)){
            channelRefundLimit.setIsDefaultAccount(false);
            channelRefundLimit.setIsPlatAccount(true);
            MchApplyment applyment = mchApplymentService.getById(applyId);
            if(MchApplyment.REFUND_WAY_PLATFORM.equals(applyment.getRefundWay())){
                channelRefundLimit.setBindStatus(ChannelRefundLimit.SUCCESS);
            }else{
                channelRefundLimit.setBindStatus(ChannelRefundLimit.NO_OPEN);
            }
        }
        return channelRefundLimit;
    }
    @Override
    public void checkPlatAccount(RefundOrder refundOrder) {
        super.checkPlatAccount(refundOrder);
    }
}
