package com.iformall.tenant;

import com.iformall.plugin.TenantInfo;
import com.iformall.shiro.UserSession;
import org.apache.ibatis.mapping.MappedStatement;
import org.apache.shiro.SecurityUtils;
import org.apache.shiro.session.InvalidSessionException;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

public class TenantInfoImpl implements TenantInfo {
    private final Logger logger = LoggerFactory.getLogger(this.getClass());

    @Override
    public String getTenantId() {
        String tenantId = null;
        try {
            tenantId = (String) SecurityUtils.getSubject().getSession().getAttribute(UserSession.tenantId);
        } catch (InvalidSessionException e) {
            logger.error("InvalidSession: " + e.getMessage());
        }
        return tenantId;
    }

    @Override
    public boolean doTableFilter(String tableName) {
        if ("mall_permission".equals(tableName)) {
            return true;
        }
        if ("mall_user_role".equals(tableName)) {
            return true;
        }
        if ("wx_channel".equals(tableName)) {
            return true;
        }
        if ("wx_business".equals(tableName)) {
            return true;
        }
        if ("wx_coupon_type".equals(tableName)) {
            return true;
        }
        if ("wx_game_template".equals(tableName)) {
            return true;
        }
        if ("wx_group".equals(tableName)) {
            return true;
        }
        if ("wx_mall_apply".equals(tableName)) {
            return true;
        }
        if ("wx_tags".equals(tableName)) {
            return true;
        }
        if ("wx_tags_group".equals(tableName)) {
            return true;
        }
        if ("wx_tags_type".equals(tableName)) {
            return true;
        }
        if ("wx_component_verify_ticket".equals(tableName)) {
            return true;
        }
        if ("wx_authorizer_info".equals(tableName)) {
            return true;
        }
        if ("wx_user_channel".equals(tableName)) {
            return true;
        }
        return false;
    }

    @Override
    public boolean doMappedStatementFIlter(MappedStatement ms) {
        if ("com.iformall.mapper.MallUserInfoMapper.selectByUserName".equals(ms.getId()))
            return true;
        if ("com.iformall.mapper.WxCouponActionLogMapper.getCountByUserAndCoupon".equals(ms.getId()))
            return true;
        return false;
    }
}
