|  |  |  | 
|---|
|  |  |  | package com.zy.asrs.wms.common.config; | 
|---|
|  |  |  |  | 
|---|
|  |  |  | import com.baomidou.mybatisplus.extension.plugins.MybatisPlusInterceptor; | 
|---|
|  |  |  | import com.baomidou.mybatisplus.extension.plugins.handler.TenantLineHandler; | 
|---|
|  |  |  | import com.baomidou.mybatisplus.extension.plugins.inner.PaginationInnerInterceptor; | 
|---|
|  |  |  | import com.baomidou.mybatisplus.extension.plugins.inner.TenantLineInnerInterceptor; | 
|---|
|  |  |  | import com.zy.asrs.wms.system.entity.User; | 
|---|
|  |  |  | import net.sf.jsqlparser.expression.Expression; | 
|---|
|  |  |  | import net.sf.jsqlparser.expression.LongValue; | 
|---|
|  |  |  | import net.sf.jsqlparser.expression.NullValue; | 
|---|
|  |  |  | import org.springframework.context.annotation.Bean; | 
|---|
|  |  |  | import org.springframework.context.annotation.Configuration; | 
|---|
|  |  |  | import org.springframework.security.core.Authentication; | 
|---|
|  |  |  | import org.springframework.security.core.context.SecurityContextHolder; | 
|---|
|  |  |  |  | 
|---|
|  |  |  | import java.util.Arrays; | 
|---|
|  |  |  |  | 
|---|
|  |  |  | /** | 
|---|
|  |  |  | * MybatisPlus配置 | 
|---|
|  |  |  | * | 
|---|
|  |  |  | * @author vincent | 
|---|
|  |  |  | * @since 2018-02-22 11:29:28 | 
|---|
|  |  |  | */ | 
|---|
|  |  |  | @Configuration | 
|---|
|  |  |  | public class MybatisPlusConfig { | 
|---|
|  |  |  |  | 
|---|
|  |  |  | @Bean | 
|---|
|  |  |  | public MybatisPlusInterceptor mybatisPlusInterceptor() { | 
|---|
|  |  |  | MybatisPlusInterceptor interceptor = new MybatisPlusInterceptor(); | 
|---|
|  |  |  |  | 
|---|
|  |  |  | TenantLineHandler tenantLineHandler = new TenantLineHandler() { | 
|---|
|  |  |  |  | 
|---|
|  |  |  | @Override | 
|---|
|  |  |  | public Expression getTenantId() { | 
|---|
|  |  |  | return getLoginUserTenantId(); | 
|---|
|  |  |  | } | 
|---|
|  |  |  |  | 
|---|
|  |  |  | @Override | 
|---|
|  |  |  | public String getTenantIdColumn() { | 
|---|
|  |  |  | return "host_id"; | 
|---|
|  |  |  | } | 
|---|
|  |  |  |  | 
|---|
|  |  |  | @Override | 
|---|
|  |  |  | public boolean ignoreTable(String tableName) { | 
|---|
|  |  |  | return Arrays.asList( | 
|---|
|  |  |  | "sys_host", | 
|---|
|  |  |  | "sys_user_role", | 
|---|
|  |  |  | "sys_role_menu" | 
|---|
|  |  |  | ).contains(tableName); | 
|---|
|  |  |  | } | 
|---|
|  |  |  |  | 
|---|
|  |  |  | }; | 
|---|
|  |  |  | TenantLineInnerInterceptor tenantLineInnerInterceptor = new TenantLineInnerInterceptor(tenantLineHandler); | 
|---|
|  |  |  | interceptor.addInnerInterceptor(tenantLineInnerInterceptor); | 
|---|
|  |  |  |  | 
|---|
|  |  |  | PaginationInnerInterceptor paginationInnerInterceptor = new PaginationInnerInterceptor(); | 
|---|
|  |  |  | interceptor.addInnerInterceptor(paginationInnerInterceptor); | 
|---|
|  |  |  |  | 
|---|
|  |  |  | return interceptor; | 
|---|
|  |  |  | } | 
|---|
|  |  |  |  | 
|---|
|  |  |  | /** | 
|---|
|  |  |  | * 获取当前登录用户的租户id | 
|---|
|  |  |  | * | 
|---|
|  |  |  | * @return Integer | 
|---|
|  |  |  | */ | 
|---|
|  |  |  | public Expression getLoginUserTenantId() { | 
|---|
|  |  |  | try { | 
|---|
|  |  |  | Authentication authentication = SecurityContextHolder.getContext().getAuthentication(); | 
|---|
|  |  |  | if (authentication != null) { | 
|---|
|  |  |  | Object object = authentication.getPrincipal(); | 
|---|
|  |  |  | if (object instanceof User) { | 
|---|
|  |  |  | return new LongValue(((User) object).getHostId()); | 
|---|
|  |  |  | } | 
|---|
|  |  |  | } | 
|---|
|  |  |  | } catch (Exception e) { | 
|---|
|  |  |  | System.out.println(e.getMessage()); | 
|---|
|  |  |  | } | 
|---|
|  |  |  | return new NullValue(); | 
|---|
|  |  |  | } | 
|---|
|  |  |  |  | 
|---|
|  |  |  | } | 
|---|
|  |  |  | package com.zy.asrs.wms.common.config; | 
|---|
|  |  |  |  | 
|---|
|  |  |  | import com.baomidou.mybatisplus.autoconfigure.ConfigurationCustomizer; | 
|---|
|  |  |  | import com.baomidou.mybatisplus.extension.MybatisMapWrapperFactory; | 
|---|
|  |  |  | import com.baomidou.mybatisplus.extension.plugins.MybatisPlusInterceptor; | 
|---|
|  |  |  | import com.baomidou.mybatisplus.extension.plugins.handler.TenantLineHandler; | 
|---|
|  |  |  | import com.baomidou.mybatisplus.extension.plugins.inner.PaginationInnerInterceptor; | 
|---|
|  |  |  | import com.baomidou.mybatisplus.extension.plugins.inner.TenantLineInnerInterceptor; | 
|---|
|  |  |  | import com.zy.asrs.wms.common.interceptor.DataChangeInterceptor; | 
|---|
|  |  |  | import com.zy.asrs.wms.system.entity.User; | 
|---|
|  |  |  | import com.zy.asrs.wms.system.entity.UserLogin; | 
|---|
|  |  |  | import net.sf.jsqlparser.expression.Expression; | 
|---|
|  |  |  | import net.sf.jsqlparser.expression.LongValue; | 
|---|
|  |  |  | import net.sf.jsqlparser.expression.NullValue; | 
|---|
|  |  |  | import org.springframework.context.annotation.Bean; | 
|---|
|  |  |  | import org.springframework.context.annotation.Configuration; | 
|---|
|  |  |  | import org.springframework.security.core.Authentication; | 
|---|
|  |  |  | import org.springframework.security.core.context.SecurityContextHolder; | 
|---|
|  |  |  | import java.util.Arrays; | 
|---|
|  |  |  |  | 
|---|
|  |  |  | /** | 
|---|
|  |  |  | * MybatisPlus配置 | 
|---|
|  |  |  | * | 
|---|
|  |  |  | * @author vincent | 
|---|
|  |  |  | * @since 2018-02-22 11:29:28 | 
|---|
|  |  |  | */ | 
|---|
|  |  |  | @Configuration | 
|---|
|  |  |  | public class MybatisPlusConfig { | 
|---|
|  |  |  |  | 
|---|
|  |  |  |  | 
|---|
|  |  |  | @Bean | 
|---|
|  |  |  | public MybatisPlusInterceptor mybatisPlusInterceptor() { | 
|---|
|  |  |  | MybatisPlusInterceptor interceptor = new MybatisPlusInterceptor(); | 
|---|
|  |  |  |  | 
|---|
|  |  |  | TenantLineHandler tenantLineHandler = new TenantLineHandler() { | 
|---|
|  |  |  |  | 
|---|
|  |  |  | @Override | 
|---|
|  |  |  | public Expression getTenantId() { | 
|---|
|  |  |  | return getLoginUserTenantId(); | 
|---|
|  |  |  | } | 
|---|
|  |  |  |  | 
|---|
|  |  |  | @Override | 
|---|
|  |  |  | public String getTenantIdColumn() { | 
|---|
|  |  |  | return "host_id"; | 
|---|
|  |  |  | } | 
|---|
|  |  |  |  | 
|---|
|  |  |  | @Override | 
|---|
|  |  |  | public boolean ignoreTable(String tableName) { | 
|---|
|  |  |  | return Arrays.asList( | 
|---|
|  |  |  | "sys_host", | 
|---|
|  |  |  | "sys_user_role", | 
|---|
|  |  |  | "sys_role_menu", | 
|---|
|  |  |  | "sys_language", | 
|---|
|  |  |  | "man_loc_sts", | 
|---|
|  |  |  | "man_task_sts", | 
|---|
|  |  |  | "man_task_type", | 
|---|
|  |  |  | "man_order_settle", | 
|---|
|  |  |  | "view_man_loc_detl_field", | 
|---|
|  |  |  | "sys_cache_statistics" | 
|---|
|  |  |  | ).contains(tableName); | 
|---|
|  |  |  | } | 
|---|
|  |  |  |  | 
|---|
|  |  |  | }; | 
|---|
|  |  |  | TenantLineInnerInterceptor tenantLineInnerInterceptor = new TenantLineInnerInterceptor(tenantLineHandler); | 
|---|
|  |  |  | interceptor.addInnerInterceptor(tenantLineInnerInterceptor); | 
|---|
|  |  |  |  | 
|---|
|  |  |  | PaginationInnerInterceptor paginationInnerInterceptor = new PaginationInnerInterceptor(); | 
|---|
|  |  |  | interceptor.addInnerInterceptor(paginationInnerInterceptor); | 
|---|
|  |  |  |  | 
|---|
|  |  |  |  | 
|---|
|  |  |  | return interceptor; | 
|---|
|  |  |  | } | 
|---|
|  |  |  |  | 
|---|
|  |  |  | /** | 
|---|
|  |  |  | * 获取当前登录用户的租户id | 
|---|
|  |  |  | * | 
|---|
|  |  |  | * @return Integer | 
|---|
|  |  |  | */ | 
|---|
|  |  |  | public Expression getLoginUserTenantId() { | 
|---|
|  |  |  | try { | 
|---|
|  |  |  | Authentication authentication = SecurityContextHolder.getContext().getAuthentication(); | 
|---|
|  |  |  | if (authentication != null) { | 
|---|
|  |  |  | Object object = authentication.getPrincipal(); | 
|---|
|  |  |  | if (object instanceof User) { | 
|---|
|  |  |  | return new LongValue(((User) object).getHostId()); | 
|---|
|  |  |  | } | 
|---|
|  |  |  | if (object instanceof UserLogin) { | 
|---|
|  |  |  | return new LongValue(((UserLogin) object).getHostId()); | 
|---|
|  |  |  | } | 
|---|
|  |  |  | } else { | 
|---|
|  |  |  | return  new LongValue(1); | 
|---|
|  |  |  | } | 
|---|
|  |  |  | } catch (Exception e) { | 
|---|
|  |  |  | System.out.println(e.getMessage()); | 
|---|
|  |  |  | } | 
|---|
|  |  |  | //        return new NullValue(); | 
|---|
|  |  |  | return new LongValue(1); | 
|---|
|  |  |  | } | 
|---|
|  |  |  |  | 
|---|
|  |  |  | @Bean | 
|---|
|  |  |  | public ConfigurationCustomizer mybatisConfigurationCustomizer() { | 
|---|
|  |  |  | return configuration -> configuration.setObjectWrapperFactory(new MybatisMapWrapperFactory()); | 
|---|
|  |  |  | } | 
|---|
|  |  |  |  | 
|---|
|  |  |  | @Bean | 
|---|
|  |  |  | public DataChangeInterceptor sqlStatementInterceptor() { | 
|---|
|  |  |  | return new DataChangeInterceptor(); | 
|---|
|  |  |  | } | 
|---|
|  |  |  |  | 
|---|
|  |  |  | } | 
|---|