zhaowenxuan
2025-06-24 2a9583fd376451ca77a0400af7f3a02d1b80e7f5
ltkj-admin/src/main/java/com/ltkj/web/controller/system/SysRoleController.java
@@ -1,10 +1,30 @@
package com.ltkj.web.controller.system;
import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStreamReader;
import java.nio.charset.StandardCharsets;
import java.nio.file.Files;
import java.nio.file.Paths;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import javax.servlet.http.HttpServletResponse;
import cn.hutool.core.util.IdUtil;
import cn.hutool.core.util.RandomUtil;
import com.ltkj.db.DataSourceConfig;
import com.ltkj.db.DataSourceContextHolder;
import com.ltkj.hosp.domain.DictHosp;
import com.ltkj.hosp.service.IDictHospService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.core.io.ClassPathResource;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.util.FileCopyUtils;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.GetMapping;
@@ -55,6 +75,13 @@
    @Autowired
    private ISysDeptService deptService;
    @Autowired
    private JdbcTemplate jdbcTemplate;
    @Autowired
    private IDictHospService dictHospService;
    @Value("${config.path}")
    private String path;
//    @PreAuthorize("@ss.hasPermi('system:role:list')")
    @GetMapping("/list")
@@ -245,4 +272,59 @@
        ajax.put("depts", deptService.selectDeptTreeList(new SysDept()));
        return ajax;
    }
    @Autowired
    private DataSourceConfig dataSourceConfig;
    /**
     * 从库批量执行sql
     * 读取本地sql文件
     * @return
     */
    @GetMapping("/execUpdateSql")
    public AjaxResult execUpdateSql() {
        DataSourceContextHolder.setDataSourceKey("default");
        List<DictHosp> list = dictHospService.list();
        List<Map<String, Object>> resultList = new ArrayList<>();
        boolean flag = false;
        for (DictHosp dictHosp : list) {
            String dbName = dictHosp.getDbname();
            Map<String, Object> dbResult = new HashMap<>();
            dbResult.put("database", dbName);
            List<String> successList = new ArrayList<>();
            List<String> errorList = new ArrayList<>();
            try {
                InputStreamReader reader = new InputStreamReader(Files.newInputStream(Paths.get(path + File.separator + "update.sql")), StandardCharsets.UTF_8);
                String sqlContent = FileCopyUtils.copyToString(reader);
                String[] sqlStatements = sqlContent.split("\\|-\\|");
                for (String sql : sqlStatements) {
                    sql = sql.trim();
                    if (!sql.isEmpty()) {
                        // INSERT INTO `api_config` (`id`, `api_url`, `api_method`, `tab_name`, `is_response`, `primary_keys`, `remark`, `result_code_key`, `result_data_key`, `type`) VALUES (${id}, '${dbName}', '${randowmStr}', '${randowmStr}', 1, 'ResultData', '1.pas 检查申请信息作废', 'ResultCode', 'ResultData', 'pacs');
//                        sql = sql.replace("${dbName}",dbName).replace("${id}", IdUtil.getSnowflake().nextIdStr()).replace("${randowmStr}", RandomUtil.randomString(10));
                        try {
                            dataSourceConfig.addDataSource(dbName);
                            DataSourceContextHolder.setDataSourceKey(dbName);
                            jdbcTemplate.execute(sql);
                            successList.add(sql);
                        } catch (Exception e) {
                            errorList.add(sql + "\nERROR: " + e.getMessage());
                        }
                    }
                }
            } catch (IOException e) {
                return AjaxResult.error("读取SQL文件失败:" + e.getMessage());
            }
            dbResult.put("successSQL", successList);
            dbResult.put("failedSQL", errorList);
            resultList.add(dbResult);
            if (!errorList.isEmpty())
                flag = true;
        }
        DataSourceContextHolder.clear();
        if (flag)
            return AjaxResult.error("执行sql中存在失败",resultList);
        return AjaxResult.success(resultList);
    }
}