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

import com.alibaba.fastjson.JSONObject;
import com.jeequan.jeepay.core.entity.MchApplyment;
import com.jeequan.jeepay.core.entity.PayOrder;
import com.jeequan.jeepay.core.interfaces.paychannel.IPayOrderCloseService;
import com.jeequan.jeepay.core.model.context.MchAppConfigContext;
import com.jeequan.jeepay.core.model.rqrs.msg.ChannelRetMsg;
import com.jeequan.jeepay.thirdparty.channel.lklsb2bpay.Lklsb2bpayKit;
import com.jeequan.jeepay.thirdparty.channel.lklsb2bpay.model.Lklsb2bOrderStatus;
import com.jeequan.jeepay.thirdparty.channel.lklsb2bpay.model.Lklsb2bPayMethod;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;

/**
 * 关闭订单接口实现类
 *
 * @author crystal
 *
 */
@Slf4j
@Service
public class Lklsb2bpayPayOrderCloseService implements IPayOrderCloseService {

    @Override
    public ChannelRetMsg close(PayOrder payOrder, MchAppConfigContext mchAppConfigContext){
        try {
            JSONObject bizContent = new JSONObject();
            bizContent.put("merchant_no", payOrder.getChannelMchNo());
            bizContent.put("out_order_no", payOrder.getPayOrderId());
            MchApplyment mchApplyment = mchAppConfigContext.getMchApplyment();
            JSONObject respBizData = Lklsb2bpayKit.payRequest(Lklsb2bPayMethod.Api.CASHIER_CLOSE, mchApplyment, bizContent);
            String trade_no = respBizData.getString("pay_order_no");
            Lklsb2bOrderStatus order_status = Lklsb2bOrderStatus.getVal(respBizData.getIntValue("order_status"));
            switch (order_status){
                case CLOSE:
                    return ChannelRetMsg.confirmSuccess(trade_no);
            }
            return ChannelRetMsg.confirmFail(order_status.getDesc());
        }catch (Exception e) {
            return ChannelRetMsg.sysError(e.getMessage());
        }
    }
}
