package com.jeequan.jeepay.thirdparty.channel;


import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.jeequan.jeepay.core.constants.CS;
import com.jeequan.jeepay.core.entity.PayOrder;
import com.jeequan.jeepay.core.interfaces.paychannel.IPaymentService;
import com.jeequan.jeepay.core.model.context.MchAppConfigContext;
import com.jeequan.jeepay.core.model.rqrs.payorder.UnifiedOrderRQ;
import com.jeequan.jeepay.core.model.terminal.TerminalChannelModel;
import com.jeequan.jeepay.db.entity.MchStoreTerminal;
import com.jeequan.jeepay.service.impl.MchStoreTerminalService;
import com.jeequan.jeepay.service.impl.SysConfigService;
import com.jeequan.jeepay.thirdparty.service.ConfigContextQueryService;
import com.jeequan.jeepay.thirdparty.util.ChannelCertConfigKitBean;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;

/*
* 支付接口抽象类
*
* @author terrfly
*
* @date 2021/6/8 17:18
*/
public abstract class AbstractPaymentService implements IPaymentService {

    @Autowired protected SysConfigService sysConfigService;
    @Autowired protected ChannelCertConfigKitBean channelCertConfigKitBean;
    @Autowired protected ConfigContextQueryService configContextQueryService;
    @Autowired private MchStoreTerminalService mchStoreTerminalService;

    @Override
    public String getIfCode(PayOrder payOrder) {
        return payOrder.getIfCode();
    }

    /** 订单分账（一般用作 如微信订单将在下单处做标记） */
    protected boolean isDivisionOrder(PayOrder payOrder){
        //订单分账， 将冻结商户资金。
        if(payOrder.getDivisionMode() != null && (PayOrder.DIVISION_MODE_AUTO == payOrder.getDivisionMode() || PayOrder.DIVISION_MODE_MANUAL == payOrder.getDivisionMode() )){
            return true;
        }
        return false;
    }

    protected String getPaySiteUrl() {
        return sysConfigService.getDBApplicationConfig().getPaySiteUrl();
    }

    protected String getNotifyUrl(){
        return getPaySiteUrl() + "/api/pay/notify/" + getIfCode();
    }

    protected String getNotifyUrl(String payOrderId){
        return getPaySiteUrl() + "/api/pay/notify/" + getIfCode() + "/" + payOrderId;
    }

    protected String getNotifyUrlByFixMchApp(String mchAppId){
        return getPaySiteUrl() + "/api/pay/notify/" + getIfCode() + "/" + CS.PAY_RETURNURL_FIX_MCHAPP_PREFIX + mchAppId;
    }

    protected String getPosNotifyUrl(){
        return getPaySiteUrl() + "/api/pos/pay/notify/" + getIfCode();
    }

    protected String getReturnUrl(){
        return getPaySiteUrl() + "/api/pay/return/" + getIfCode();
    }

    protected String getReturnUrl(String payOrderId){
        return sysConfigService.getDBApplicationConfig().getPaySiteUrl() + "/api/pay/return/" + getIfCode() + "/" + payOrderId;
    }

    protected String getReturnUrlOnlyJump(String payOrderId){
        return getPaySiteUrl() + "/api/pay/return/" + getIfCode() + "/" + CS.PAY_RETURNURL_FIX_ONLY_JUMP_PREFIX + payOrderId;
    }


    /**
    * 查找出对应的设备信息
    *
    * @author terrfly
    *
    * @date 2022/4/29 15:20
    */
    protected String queryStoreTerminalChannelTrmNo(String trmNo, PayOrder payOrder){

        MchStoreTerminal mchStoreTerminal = null;

        LambdaQueryWrapper<MchStoreTerminal> lambdaQueryWrapper = MchStoreTerminal.gw()
                .eq(MchStoreTerminal::getMchNo, payOrder.getMchNo())
                .eq(MchStoreTerminal::getAppId, payOrder.getAppId())
                .eq(MchStoreTerminal::getStoreId, payOrder.getStoreId())
                .eq(MchStoreTerminal::getState, CS.YES);

        // 指定编号查询
        if(StringUtils.isNotEmpty(trmNo)){

            lambdaQueryWrapper.eq(MchStoreTerminal::getTrmNo, trmNo);
        }else{ // 查询默认
            lambdaQueryWrapper .eq(MchStoreTerminal::getDefaultFlag, CS.YES);
        }

        mchStoreTerminal = mchStoreTerminalService.getOne(lambdaQueryWrapper,false);
        if(mchStoreTerminal == null || mchStoreTerminal.getChannelBindInfo() == null){
            return null;
        }

        TerminalChannelModel model = mchStoreTerminal.getChannelBindInfo().getObject(payOrder.getIfCode(), TerminalChannelModel.class);

        if(model != null && model.getState() != null && model.getState() == TerminalChannelModel.STATE_SUCCESS && StringUtils.isNotEmpty(model.getChannelTrmNo())){

            return model.getChannelTrmNo();
        }

        return null;
    }

}
