package com.iformall.tenant;

import com.iformall.plugin.TenantInfo;
import com.iformall.utils.Constant;
import org.apache.ibatis.mapping.MappedStatement;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.web.context.request.RequestContextHolder;
import org.springframework.web.context.request.ServletRequestAttributes;

import javax.servlet.http.HttpServletRequest;

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

    @Override
    public String getTenantId() {
        String tenantId = null;
        try {
            HttpServletRequest request = ((ServletRequestAttributes) RequestContextHolder.getRequestAttributes()).getRequest();
            tenantId = (String) request.getAttribute(Constant.TENANT_ID);
            return tenantId;
        } catch (Exception e) {
            logger.error("getTenantId: " + e.getMessage());
        }
        return tenantId;
    }

    @Override
    public boolean doTableFilter(String tableName) {
        if ("mall_permission".equals(tableName)) {
            return true;
        }
        if ("mall_sale_type".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_user_channel".equals(tableName)) {
            return true;
        }
        // wx_profit_sharing_receiver
        // wx_profit_sharing_result
        return false;
    }

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