package com.jeequan.jeepay.pay.service.openapi.impl;

import com.alibaba.fastjson.JSON;
import com.jeequan.jeepay.core.constants.CS;
import com.jeequan.jeepay.core.exception.AccessMsgException;
import com.jeequan.jeepay.core.exception.openapi.ParamException;
import com.jeequan.jeepay.core.interfaces.paychannel.IPayOrderCloseService;
import com.jeequan.jeepay.core.interfaces.paychannel.IPaymentService;
import com.jeequan.jeepay.core.model.context.MchAppConfigContext;
import com.jeequan.jeepay.core.model.openapi.AccessError;
import com.jeequan.jeepay.core.model.openapi.AccessReq;
import com.jeequan.jeepay.core.model.openapi.AccessResp;
import com.jeequan.jeepay.core.model.openapi.biz.PayResultResp;
import com.jeequan.jeepay.core.model.openapi.biz.app.AppBizReq;
import com.jeequan.jeepay.core.model.openapi.biz.cashier.CashierBizResp;
import com.jeequan.jeepay.core.model.openapi.biz.query.QueryBizReq;
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.ChannelRetMsg;
import com.jeequan.jeepay.core.model.rqrs.payorder.UnifiedOrderRQ;
import com.jeequan.jeepay.core.model.rqrs.payorder.UnifiedOrderRS;
import com.jeequan.jeepay.core.model.rqrs.payorder.payway.ChannelCashierOrderRQ;
import com.jeequan.jeepay.core.model.rqrs.payorder.payway.ChannelCashierOrderRS;
import com.jeequan.jeepay.core.utils.JeepayKit;
import com.jeequan.jeepay.core.utils.SpringBeansUtil;
import com.jeequan.jeepay.db.entity.PayOrder;
import com.jeequan.jeepay.pay.model.OpenApiExt;
import com.jeequan.jeepay.pay.service.openapi.AbstractPayOpenApiService;
import com.jeequan.jeepay.service.impl.PayOrderService;
import com.jeequan.jeepay.thirdparty.service.ConfigContextQueryService;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

/**
 * TODO
 * 聚合收银台
 * @author crystal
 * @date 2024/4/8 11:49
 */
@Slf4j
@Service("cashierOpenApiService")
public class CashierOpenApiService extends AbstractPayOpenApiService {

    @Autowired
    private PayOrderService payOrderService;

    /**
     * 聚合收银台支付
     * @param request
     * @return
     * @throws AccessMsgException
     */
    @Override
    public AccessResp execute(AccessReq request) throws AccessMsgException {
        ChannelCashierOrderRQ bizReq = JSON.parseObject(request.getBizData(), ChannelCashierOrderRQ.class);
        UnifiedOrderRQ req = bizReq.preCheck().setIsAppletCheck(true);
        OpenApiExt apiExt = checkMchInfo(req, request.getAppInfo());
        checkPay(apiExt.getConfigContext(),req, request.getAppInfo());
        //获取支付接口
        IPaymentService paymentService = getPayRoute(apiExt.getConfigContext());
        PayOrder payOrder = genPayOrder(req, apiExt.getConfigContext(),apiExt.getRouteManage());
        //校验参数
        paymentService.preCheck(req,payOrder);
        payOrderService.saveOrUpdate(payOrder);
        try {
            CashierBizResp resp = null;
            if(paymentService.isSupport(req.getWayCode())){
                ChannelCashierOrderRS rs = (ChannelCashierOrderRS)paymentService.pay(bizReq, payOrder, apiExt.getConfigContext());
                resp = new CashierBizResp(payOrder,rs.getPayUrl(),rs.getChannelRetMsg().getLiteMsg());
                this.processChannelMsg(rs,rs.getChannelRetMsg(),payOrder);
            }else{
                String payUrl = getH5payUrl(payOrder);
                ChannelAppletPayMsg payMsg = loadLitePayData(payOrder);
                resp = new CashierBizResp(payOrder,payUrl,payMsg);
//                rs = new UnifiedOrderRS(payOrder.getPayOrderId(),payUrl,payMsg);
            }
            return  AccessResp.success(request,resp);
        } catch (Exception e) {
            log.info("【收银台订单下单异常】:{}",e);
            throw new AccessMsgException(AccessError.COMMON_ERROR);
        }
    }
}
