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.entity.RefundOrder;
import com.jeequan.jeepay.core.interfaces.paychannel.IRefundService;
import com.jeequan.jeepay.core.model.rqrs.msg.ChannelRefundLimit;
import com.jeequan.jeepay.core.model.terminal.TerminalChannelModel;
import com.jeequan.jeepay.db.entity.AccountInfo;
import com.jeequan.jeepay.db.entity.MchStoreTerminal;
import com.jeequan.jeepay.service.impl.AccountInfoService;
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/17 9:37
*/
public abstract class AbstractRefundService implements IRefundService {

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

    @Override
    public String getIfCode(RefundOrder refundOrder) {
        return refundOrder.getIfCode();
    }

    protected String getNotifyUrl(){
        return sysConfigService.getDBApplicationConfig().getPaySiteUrl() + "/api/refund/notify/" + getIfCode();
    }

    protected String getNotifyUrl(String refundOrderId){
        return sysConfigService.getDBApplicationConfig().getPaySiteUrl() + "/api/refund/notify/" + getIfCode() + "/" + refundOrderId;
    }

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

    /**
     * 查找出对应的设备信息
     *
     * @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(getIfCode(), TerminalChannelModel.class);

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

            return model.getChannelTrmNo();
        }

        return null;
    }

    /**
     * 退款权限判断
     * @param settleType
     * @param applyId
     * @return
     */
    @Override
    public ChannelRefundLimit isRefundLimit(String settleType,String applyId){
        ChannelRefundLimit refundLimit = new ChannelRefundLimit(false,true);
        return refundLimit;
    }

    /**
     * 校验平台账户余额
     * @param refundOrder
     */
    @Override
    public void checkPlatAccount(RefundOrder refundOrder){
        if(RefundOrder.REFUND_ACCOUNT_PLAT.equals(refundOrder.getExtParam())){
            accountInfoService.checkBalance(refundOrder.getMchNo(), AccountInfo.Type.REFUND,refundOrder.getRefundAmount());
        }
    }
}
