zjh
2025-02-07 59972ce8f9128858866c17655fc2d034d023b976
ltkj-framework/src/main/java/com/ltkj/framework/interceptor/DBChangeInterceptor.java
@@ -6,6 +6,7 @@
import com.google.gson.Gson;
import com.ltkj.db.DataSourceConfig;
import com.ltkj.db.DataSourceContextHolder;
import com.ltkj.framework.config.DruidConfig;
import com.ltkj.hosp.domain.DictHosp;
import com.ltkj.hosp.service.IDictHospService;
import org.springframework.beans.factory.annotation.Autowired;
@@ -28,8 +29,10 @@
    @Autowired
    private IDictHospService dictHospService;
//    @Autowired
//    private DataSourceConfig dataSourceConfig;
    @Autowired
    private DataSourceConfig dataSourceConfig;
    private DruidConfig druidConfig;
    @Override
    public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler) throws IOException {
@@ -40,23 +43,25 @@
            response.getWriter().write("{\"message\":\"401:找不到院区编号\"}");
            return false;
        }
        DictHosp hosp = null;
//        DictHosp hosp = null;
        try {
            DataSourceContextHolder.setDataSourceKey("default");
            LambdaQueryWrapper<DictHosp> wrapper = new LambdaQueryWrapper<>();
            wrapper.eq(DictHosp::getCode,hospId);
            hosp = dictHospService.getOne(wrapper);
            if (hosp == null || hosp.getDatabase() == null || hosp.getDatabase().trim().isEmpty()) {
            DictHosp hosp = dictHospService.getOne(wrapper);
            if (hosp == null || hosp.getDbname() == null || hosp.getDbname().trim().isEmpty()) {
                response.setStatus(HttpServletResponse.SC_UNAUTHORIZED);
                response.setContentType("application/json;charset=UTF-8");
                response.getWriter().write("{\"message\":\"401:找不到院区数据\"}");
                return false;
            }
            dataSourceConfig.addDataSource(hosp.getDatabase());
//            dataSourceConfig.addDataSource(hosp.getDbname());
            druidConfig.addDataSource(hosp.getDbname());
            DataSourceContextHolder.setDataSourceKey(hosp.getDbname());
        } catch (IOException e) {
            return false;
        }
        DataSourceContextHolder.setDataSourceKey(hosp.getDatabase());
        return true;
    }