ShangHaiMesParent/moduleService/plcConnectModule/src/main/java/com/mes/connect/IndustrialInterface/Api.java
@@ -2,6 +2,11 @@
import com.alibaba.fastjson.JSON;
import com.baomidou.dynamic.datasource.annotation.DS;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.mes.connect.entity.ApiConfig;
import com.mes.connect.entity.LogicItem;
import com.mes.connect.entity.PlcParameters;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.*;
import org.springframework.jdbc.core.JdbcTemplate;
@@ -19,7 +24,9 @@
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;
@Slf4j
@Service
public class Api implements ApiService {
@@ -37,6 +44,47 @@
        this.restTemplate = restTemplate;
        this.jdbcTemplate = jdbcTemplate;
    }
    /**
     * 发送调用接口请求 ,根据逻辑配置调用
     *
     * @param apiConfig 逻辑配置参数
     * @param plcParameters plc参数
     * @return 响应内容按行分割的数组
     */
    public List<String> callApi(ApiConfig apiConfig, PlcParameters plcParameters){
        try{
            List<String> result=new ArrayList<>();
            String connectType=apiConfig.getType();
            String connectAddress=apiConfig.getAddress();
            Map<String,Object> map=new HashMap<String,Object>();
            map.put("apiConfig",apiConfig);
            map.put("plcParameter",plcParameters);
            switch (connectType) {
                case "Http":
                    result= this.httpApi(connectAddress,map);
                    break;
                case "View": // 视图/表
                    result= this.viewApi(connectAddress,map);
                    break;
                case "Procedure": // 存储过程
                    result= this.procedureAPI(connectAddress,map);
                    break;
                default:
                    log.warn("不支持的连接类型: {}", connectType);
                    return null; // 不支持的方式
            }
            return result;
        }catch (Exception e){
            log.error("调用接口失败: {}", e.getMessage(), e);
        }
        return null;
    }
    /**
     * 发送HTTP请求,支持GET和POST方法
     *
@@ -52,8 +100,11 @@
            UriComponentsBuilder builder = UriComponentsBuilder.fromHttpUrl(url);
            // 处理响应
            String responseBody;
            String method=data.get("method").toString();
            data.remove("method");
            ObjectMapper mapper = new ObjectMapper();
            Map<String, Object> apiConfig= mapper.convertValue(data.get("apiConfig"), Map.class);
            Map<String, Object> parameters= mapper.convertValue(apiConfig.get("parameters"), Map.class);
            String method=parameters.get("method").toString();
            data.remove("parameters");
            if ("GET".equals(method)) {
                // GET请求:将参数添加到URL查询参数中
                if (data != null) {
@@ -97,12 +148,14 @@
    }
    @DS("mes_machine")
    @Override
    public List<String> viewApi(String viewName, Map<String, Object> params) {
    public List<String> viewApi(String viewName, Map<String, Object> parameters) {
        // 验证视图名是否合法,防止SQL注入
        if (!isValidViewName(viewName)) {
            throw new IllegalArgumentException("无效的视图名称");
        }
        ObjectMapper mapper = new ObjectMapper();
        Map<String, Object> apiConfig= mapper.convertValue(parameters.get("apiConfig"), Map.class);
        Map<String, Object> params= mapper.convertValue(apiConfig.get("parameters"), Map.class);
        // 使用预编译语句构建查询
        StringBuilder sql = new StringBuilder("SELECT * FROM " + viewName);
        MapSqlParameterSource paramSource = new MapSqlParameterSource();
@@ -130,66 +183,67 @@
    }
    @DS("jiumumes")
    @Override
    public List<String> procedureAPI(String procedureName, Map<String, Object> params,Map<String, Object> outParams) {
    public List<String> procedureAPI(String procedureName, Map<String, Object> params) {
        try {
            if (!isValidProcedureName(procedureName)) {
                throw new IllegalArgumentException("无效的存储过程名称");
            }
            ObjectMapper mapper = new ObjectMapper();
            Map<String, Object> apiConfig= mapper.convertValue(params.get("apiConfig"), Map.class);
            Map<String, Object> parameters=mapper.convertValue(apiConfig.get("parameters"), Map.class);
            Map<String, Object> inParams= mapper.convertValue(parameters.get("inParams"), Map.class);
            Map<String, Object> outParams= mapper.convertValue(parameters.get("outParams"), Map.class);
            // 创建新的 Map 并合并
            Map<String, Object> mergedMap = new HashMap<>(inParams);
            mergedMap.putAll(outParams);
            SimpleJdbcCall jdbcCall = new SimpleJdbcCall(jdbcTemplate)
                    .withProcedureName(procedureName)
                    .withoutProcedureColumnMetaDataAccess();
            if (params != null) {
                for (Map.Entry<String, Object> entry : params.entrySet()) {
                    // 确定参数类型
                    int sqlType = getSqlType(entry.getValue());
                    // 检查是否为输出参数
                    if (outParams != null && outParams.containsKey(entry.getKey())) {
                        Object outParamInfo = outParams.get(entry.getKey());
                        int outSqlType;
                        // 从输出参数信息中获取SQL类型
                        if (outParamInfo instanceof Integer) {
                            outSqlType = (Integer) outParamInfo;
                        } else if (outParamInfo instanceof Map) {
                            // 假设Map中包含"sqlType"键
                            Map<String, Object> outParamMap = (Map<String, Object>) outParamInfo;
                            outSqlType = (Integer) outParamMap.getOrDefault("sqlType", sqlType);
                        } else {
                            // 默认使用输入参数的SQL类型
                            outSqlType = sqlType;
                        }
                        // 使用指定的SQL类型作为输出参数
                        jdbcCall.declareParameters(
                                new SqlOutParameter(entry.getKey(), outSqlType)
                        );
                    } else {
                        // 作为输入参数
                        jdbcCall.declareParameters(
                                new SqlParameter(entry.getKey(), sqlType)
                        );
                    }
                }
            for (Map.Entry<String, Object> entry : inParams.entrySet()) {
                int sqlType = getSqlType(entry.getValue());
                // 作为输入参数
                jdbcCall.declareParameters(
                        new SqlParameter(entry.getKey(), sqlType)
                );
            }
            for (Map.Entry<String, Object> entry : outParams.entrySet()) {
                int outSqlType=12;
//                int sqlType = getSqlType(entry.getValue());
//                Object outParamInfo = outParams.get(entry.getKey());
//                // 从输出参数信息中获取SQL类型
//                if (outParamInfo instanceof Integer) {
//                    outSqlType = (Integer) outParamInfo;
//                } else if (outParamInfo instanceof Map) {
//                    // 假设Map中包含"sqlType"键
//                    Map<String, Object> outParamMap = (Map<String, Object>) outParamInfo;
//                    outSqlType = (Integer) outParamMap.getOrDefault("sqlType", sqlType);
//                } else {
//                    // 默认使用输入参数的SQL类型
//                    outSqlType = sqlType;
//                }
                // 使用指定的SQL类型作为输出参数
                jdbcCall.declareParameters(
                        new SqlOutParameter(entry.getKey(), outSqlType)
                );
            }
            // 执行存储过程并获取结果
            Map<String, Object> result = jdbcCall.execute(params);
            Map<String, Object> result = jdbcCall.execute(mergedMap);
            // 处理输出参数
            if (outParams != null) {
                for (String paramName : outParams.keySet()) {
                    if (result.containsKey(paramName)) {
                        // 将输出参数的值放回原参数Map中
                        params.put(paramName, result.get(paramName));
                        outParams.put(paramName, result.get(paramName));
                    }
                }
            }
            List<String> outParamsValues = outParams.values().stream()
                    .map(value -> value != null ? value.toString() : "null")
                    .collect(Collectors.toList());
            // 返回结果信息
            return null;
            return outParamsValues;
        } catch (Exception e) {
            return null;
        }