package cn.pluss.platform.merchantChannelStatus;

import cn.pluss.platform.entity.MerchantChannelStatus;
import cn.pluss.platform.mapper.MerchantChannelStatusMapper;
import cn.pluss.platform.vo.ChannelStatusVO;
import cn.pluss.platform.vo.MerchantChannelStatusVO;
import cn.pluss.platform.vo.UserNumVO;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.service.IService;
import org.apache.commons.lang3.StringUtils;

import java.util.List;
import java.util.Map;

/**
 * @author DJH
 */
public interface MerchantChannelStatusService extends IService<MerchantChannelStatus> {

    default MerchantChannelStatus getByIdLock(Integer id) {
        return ((MerchantChannelStatusMapper) getBaseMapper()).getByIdLock(id);
    }

    default List<MerchantChannelStatusVO> listByMerchantCodeList(List<String> merchantCodeList) {
        return ((MerchantChannelStatusMapper) getBaseMapper()).selectListByMerchantCode(merchantCodeList);
    }

    default MerchantChannelStatus getByMerchantCodeAndChannelType(String merchantCode, String channelType) {
        LambdaQueryWrapper<MerchantChannelStatus> qWrapper = Wrappers.lambdaQuery();
        qWrapper.eq(MerchantChannelStatus::getVirChannelFlag, channelType);
        qWrapper.eq(MerchantChannelStatus::getMerchantCode, merchantCode);

        return getOne(qWrapper);
    }

    /**
     * 进件请求前参数校验
     * @param merchantCode 商户编码
     *
     * @return  返回一个驳回的进件记录（若有）
     */
    MerchantChannelStatus auditCheck(String merchantCode);

    default MerchantChannelStatus getByMerchantCodeWithChannel(String merchantCode, String channelType) {
        QueryWrapper<MerchantChannelStatus> qWrapper = new QueryWrapper<>();
        qWrapper.eq("merchantCode", merchantCode)
                .eq("channel", channelType);
        return getBaseMapper().selectOne(qWrapper);
    }


    /**
     * 获取当前启用的渠道下商户的渠道信息
     * @param merchantCode
     * @return
     */
    default MerchantChannelStatus getByMerchantCode(String merchantCode) {
        if (StringUtils.isEmpty(merchantCode)) {
            return null;
        }

        return ((MerchantChannelStatusMapper) getBaseMapper()).getByMerchantCode(merchantCode);
    }

    default MerchantChannelStatus getValidData(String merchantCode) {
        if (StringUtils.isEmpty(merchantCode)) {
            return null;
        }

        // 先查询进件通过了的进件信息
        LambdaQueryWrapper<MerchantChannelStatus> qWrapper2 = Wrappers.lambdaQuery();
        qWrapper2.eq(MerchantChannelStatus::getMerchantCode, merchantCode)
                .in(MerchantChannelStatus::getStatus,
                        MerchantChannelStatus.AUDIT_STATUS_SUCCESS,
                        MerchantChannelStatus.AUDIT_STATUS_DATA_EDIT)
                .orderByDesc(MerchantChannelStatus::getValid)
                .orderByAsc(MerchantChannelStatus::getId)
                .last("limit 1");
        MerchantChannelStatus mcs = getBaseMapper().selectOne(qWrapper2);
        if (mcs != null) {
            return mcs;
        }

        LambdaQueryWrapper<MerchantChannelStatus> qWrapper = new LambdaQueryWrapper<MerchantChannelStatus>()
                .eq(MerchantChannelStatus::getMerchantCode, merchantCode)
                .orderByDesc(MerchantChannelStatus::getValid)
                .orderByAsc(MerchantChannelStatus::getId)
                .last("limit 1");

        return getBaseMapper().selectOne(qWrapper);
    }

    /**
     * 获取没有通道信息的进件记录信息
     * @param merchantCode  商户编号
     * @return  进件记录信息
     */
    default MerchantChannelStatus getByMerchantCodeNoChannel(String merchantCode) {
        LambdaQueryWrapper<MerchantChannelStatus> qWrapper = new LambdaQueryWrapper<MerchantChannelStatus>()
                .eq(MerchantChannelStatus::getMerchantCode, merchantCode)
                .isNull(MerchantChannelStatus::getChannel);

        return getOne(qWrapper);
    }

    /**
     * 获取当前启用的渠道下商户的渠道信息
     * @param merchantCode
     * @return
     */
    default MerchantChannelStatus getByMerchantCode(String merchantCode, Integer channel) {
        if (StringUtils.isEmpty(merchantCode)) {
            return null;
        }

        MerchantChannelStatus result = ((MerchantChannelStatusMapper) getBaseMapper()).getByMerchantCode(merchantCode, channel);

        if (result == null) {
            result = getByMerchantCodeNoChannel(merchantCode);
        }

        return result;
    }

    MerchantChannelStatus getSxfMerchantStatusInfo(String merchantCode);

    /**
     * 商户申请进件（未提交给随行付）
     * @param merchantCode  商户code
     */
    void reqAudit(String merchantCode);

    /**
     * 驳回商户进件申请
     * @param merchantCode  商户code
     * @param errMsg        驳回原因
     */
    void rejectAudit(String merchantCode, String errMsg);

    default List<MerchantChannelStatus> listWithUserId(QueryWrapper<MerchantChannelStatus> queryWrapper) {
        return ((MerchantChannelStatusMapper)getBaseMapper()).selectListWithUserId(queryWrapper);
    }

    /**
     * app获取快速到账的通道，当前快速到账通道为银盛
     * @return  银盛通道的进件记录
     */
    ChannelStatusVO appQuickCash();

    /**
     * 快速到账通道和常规通道切换
     * @param flag 通道标识，目前是0：常规通道， 1： 快速到账通道
     * @return  切换后的通道记录
     */
    ChannelStatusVO appSwitchChannel(int flag);

    /**
     * 查询用户当前的通道信息
     * @return  通道进件信息
     */
    ChannelStatusVO appCurrent();

    /**
     * 清理驳回的信息中的通道相关数据
     * @param merchantCode  商户编号
     */
    default void clearRejectedChannel(String merchantCode) {
        // 只能清理审核驳回，以及待后台审核状态下的进件记录通道信息
        LambdaUpdateWrapper<MerchantChannelStatus> uWrapper = new LambdaUpdateWrapper<MerchantChannelStatus>()
                .eq(MerchantChannelStatus::getMerchantCode, merchantCode)
                .and(wrapper -> wrapper.in(MerchantChannelStatus::getStatus, 2, 0)
                        .or(wrapper2 -> wrapper2.eq(MerchantChannelStatus::getStatus, 1)
                                .eq(MerchantChannelStatus::getThirdStatus, -999)));

        uWrapper.set(MerchantChannelStatus::getChannel, null)
                .set(MerchantChannelStatus::getStatus, "1")
                .set(MerchantChannelStatus::getThirdStatus, "-100")
                .set(MerchantChannelStatus::getMerchantId, null)
                .set(MerchantChannelStatus::getApplicationId, null);

        getBaseMapper().update(null, uWrapper);
    }

    default MerchantChannelStatus getByMerchantIdLock(String merchantId) {
        return ((MerchantChannelStatusMapper) getBaseMapper()).getByMerchantIdLock(merchantId);
    }

    MerchantChannelStatus getChannelByMerchantId(String uid);

    /**
     * 确认微信认证的状态
     * @param mcs  商户信息
     */
    void verifyWxAuthStatus(MerchantChannelStatus mcs);

    /**
     * @description:根据通道商编查询数据
     * @date: 2021/10/28 10:48
     * @param mno:
     * @return cn.pluss.platform.entity.MerchantChannelStatus
     */
    MerchantChannelStatus getByMerchantId(String mno);

    /**
     * 发送签约短信
     * @param merchantCode
     */
    void sendSignMsg(String merchantCode);

    /**
     * 当前用户指定时间段 进件商户数量
     */
    UserNumVO getUserNumInfo(Long userId, String date, Integer dateType);
}
