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

import com.alibaba.fastjson.JSON;
import com.jeequan.jeepay.core.exception.AccessMsgException;
import com.jeequan.jeepay.core.exception.BizException;
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.jsapi.JsapiBizReq;
import com.jeequan.jeepay.core.model.openapi.biz.jsapi.JsapiBizResp;
import com.jeequan.jeepay.core.model.rqrs.AbstractRS;
import com.jeequan.jeepay.core.model.rqrs.msg.ChannelAppletPayMsg;
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.WxJsapiOrderRQ;
import com.jeequan.jeepay.db.entity.MchApplyment;
import com.jeequan.jeepay.db.entity.PayOrder;
import com.jeequan.jeepay.pay.model.OpenApiExt;
import com.jeequan.jeepay.pay.service.ValidateService;
import com.jeequan.jeepay.pay.service.openapi.AbstractPayOpenApiService;
import com.jeequan.jeepay.service.impl.PayOrderService;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

/**
 * TODO
 * jsapi支付逻辑
 * @author crystal
 * @date 2024/4/8 11:49
 */
@Service("jsapiOpenApiService")
public class JsapiOpenApiService extends AbstractPayOpenApiService {

    @Autowired
    private PayOrderService payOrderService;

    /**
     * jsapi支付
     * @param request
     * @return
     * @throws AccessMsgException
     */
    @Override
    public AccessResp execute(AccessReq request) throws AccessMsgException {
        JsapiBizReq bizReq = JSON.parseObject(request.getBizData(), JsapiBizReq.class);
        UnifiedOrderRQ req = bizReq.preCheck(request).buildBizRQ();
        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 {
            if(bizReq.getIsScreen()){
                ChannelAppletPayMsg litePayData = loadLitePayData(payOrder);
                JsapiBizResp resp = new JsapiBizResp(payOrder,litePayData);
                return AccessResp.success(request,resp);
            }
            UnifiedOrderRS rs = (UnifiedOrderRS) paymentService.pay(req, payOrder, apiExt.getConfigContext());
            this.processChannelMsg(rs,rs.getChannelRetMsg(),payOrder);
            JsapiBizResp resp = new JsapiBizResp(payOrder,rs.getChannelRetMsg().getJsapiMsg());
            return AccessResp.success(request,resp);
        } catch (Exception e) {
            throw new AccessMsgException(AccessError.commonError(e.getMessage()));
        }
    }
}
