package cn.pluss.platform.mcc.impl;

import cn.pluss.platform.entity.MccReflect;
import cn.pluss.platform.entity.MerchantChannel;
import cn.pluss.platform.exception.MsgException;
import cn.pluss.platform.mapper.MccReflectMapper;
import cn.pluss.platform.mcc.MccReflectService;
import cn.pluss.platform.merchantChannel.MerchantChannelService;
import cn.pluss.platform.vo.MccReflectVO;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import lombok.Setter;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

/**
 * <p>
 * 主通道(随行付)与其他通道之间mcc的关联表 服务实现类
 * </p>
 *
 * @author Djh
 * @since 2021-02-23
 */
@Service
public class MccReflectServiceImpl extends ServiceImpl<MccReflectMapper, MccReflect> implements MccReflectService {

    @Setter(onMethod_ = {@Autowired})
    private MerchantChannelService channelService;

    @Override
    public MccReflect getRyxMccReflect(String mcc) {
        MerchantChannel merchantChannel = new MerchantChannel();
        merchantChannel.setChannelCode("RYX");
        merchantChannel = channelService.getOne(new QueryWrapper<>(merchantChannel));

        MsgException.checkNull(merchantChannel, "通道信息缺失");

        MccReflect mccReflect = new MccReflect();
        mccReflect.setChannelId(merchantChannel.getId());
        mccReflect.setStandardMccCode(mcc);
        mccReflect = getOne(new QueryWrapper<>(mccReflect));

        if (mccReflect == null || StringUtils.isEmpty(mccReflect.getMccCode())) {
            MsgException.throwException("通道MCC映射信息缺失");
        }
        return mccReflect;
    }

    @Override
    public MccReflect getYsMccReflect(String mcc) {
        MerchantChannel merchantChannel = new MerchantChannel();
        merchantChannel.setChannelCode("YS");
        merchantChannel = channelService.getOne(new QueryWrapper<>(merchantChannel));

        MsgException.checkNull(merchantChannel, "通道信息缺失");

        MccReflect mccReflect = new MccReflect();
        mccReflect.setChannelId(merchantChannel.getId());
        mccReflect.setStandardMccCode(mcc);
        mccReflect = getOne(new QueryWrapper<>(mccReflect));

        if (mccReflect == null || StringUtils.isEmpty(mccReflect.getMccCode())) {
            MsgException.throwException("通道MCC映射信息缺失");
        }

        return mccReflect;
    }

    @Override
    public IPage<MccReflectVO> pageMccReflectVO(IPage<MccReflectVO> page, MccReflect entity) {
        return baseMapper.selectPageMccReflectVO(page, entity);
    }
}
