package com.web.kanban.service.internal; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.dao.DataAccessException; import org.springframework.jdbc.core.CallableStatementCallback; import org.springframework.jdbc.core.CallableStatementCreator; import org.springframework.jdbc.core.JdbcTemplate; import java.sql.CallableStatement; import java.sql.Connection; import java.sql.ResultSet; import java.sql.SQLException; import java.util.ArrayList; import java.util.HashMap; import java.util.List; import java.util.Map; public class GltReportPrcUtils { @Autowired private JdbcTemplate jdbcTemplate; /** * 2023-03-25 获取IQC待检看板存储过程 **/ public List getPRC_KB_IQC_prc( String prc_name,Integer size, Integer page) throws Exception { List resultList = (List) jdbcTemplate.execute(new CallableStatementCreator() { @Override public CallableStatement createCallableStatement(Connection con) throws SQLException { String storedProc = "{call " + prc_name + " (?,?,?,?)}";// 调用的sql CallableStatement cs = con.prepareCall(storedProc); cs.registerOutParameter(1, java.sql.Types.INTEGER);// 输出参数 返回标识-标识 cs.registerOutParameter(2, java.sql.Types.VARCHAR);// 输出参数 返回标识 cs.registerOutParameter(3, -10);// 输出参数 追溯数据 cs.registerOutParameter(4, -10);// 输出参数 追溯数据 return cs; } }, new CallableStatementCallback() { public Object doInCallableStatement(CallableStatement cs) throws SQLException, DataAccessException { List result = new ArrayList<>(); List> l = new ArrayList(); List cols = new ArrayList(); List> l2 = new ArrayList(); List cols2 = new ArrayList(); cs.execute(); result.add(cs.getInt(1)); result.add(cs.getString(2)); if (cs.getString(1).toString().equals("0")) { // 游标处理 ResultSet rs = (ResultSet) cs.getObject(3); System.out.println(rs); ResultSet resultSet = (ResultSet) cs.getObject(4); System.out.println(resultSet); try { l = fitMap(rs); cols=fitMapCols(rs); l2 = fitMap(resultSet); cols2=fitMapCols(resultSet); } catch (Exception e) { e.printStackTrace(); } result.add(l); result.add(cols); result.add(l2); result.add(cols2); } System.out.println(l); System.out.println(l2); return result; } }); return resultList; } /** * 获取仓库看板 * @param prc_name 存储过程名字 * @return * @throws Exception */ public List getWarehouseKanban( String prc_name) throws Exception { List resultList = (List) jdbcTemplate.execute(new CallableStatementCreator() { @Override public CallableStatement createCallableStatement(Connection con) throws SQLException { String storedProc = "{call " + prc_name + " (?,?,?,?)}";// 调用的sql CallableStatement cs = con.prepareCall(storedProc); cs.registerOutParameter(1, java.sql.Types.INTEGER);// 输出参数 返回标识-标识 cs.registerOutParameter(2, java.sql.Types.VARCHAR);// 输出参数 返回标识 cs.registerOutParameter(3, -10);// 输出参数 追溯数据 cs.registerOutParameter(4, -10);// 输出参数 追溯数据 return cs; } }, new CallableStatementCallback() {//取数据 public Object doInCallableStatement(CallableStatement cs) throws SQLException, DataAccessException { List result = new ArrayList<>(); List> l = new ArrayList(); List cols = new ArrayList(); List> l2 = new ArrayList(); List cols2 = new ArrayList(); cs.execute(); result.add(cs.getInt(1)); result.add(cs.getString(2)); if (cs.getString(1).toString().equals("0")) { // 游标处理 ResultSet rs = (ResultSet) cs.getObject(3); System.out.println(rs); ResultSet resultSet = (ResultSet) cs.getObject(4); System.out.println(resultSet); try { l = fitMap(rs); cols=fitMapCols(rs); l2 = fitMap(resultSet); cols2=fitMapCols(resultSet); } catch (Exception e) { e.printStackTrace(); } result.add(l); result.add(cols); result.add(l2); result.add(cols2); } System.out.println(l); System.out.println(l2); return result; } }); return resultList; } private List> fitMap(ResultSet rs) throws Exception { List> list = new ArrayList<>(); if (null != rs) { Map map; int colNum = rs.getMetaData().getColumnCount(); List columnNames = new ArrayList(); for (int i = 1; i <= colNum; i++) { columnNames.add(rs.getMetaData().getColumnName(i)); } while (rs.next()) { map = new HashMap(); for (String columnName : columnNames) { map.put(columnName, rs.getString(columnName)); } list.add(map); } } return list; } /** * 2023-3-25 返回一个数组 * @param rs * @return * @throws Exception */ private List> fitMap2(ResultSet rs) throws Exception { List> list = new ArrayList<>(); if (null != rs) { int colNum = rs.getMetaData().getColumnCount(); List columnNames = new ArrayList(); for (int i = 1; i <= colNum; i++) { columnNames.add(rs.getMetaData().getColumnName(i)); } while (rs.next()) { List list2 = new ArrayList<>(); for (String columnName : columnNames) { list2.add(rs.getString(columnName)); } list.add(list2); } } return list; } /** * 2021-01-29 lst 只返回列名 **/ private List fitMapCols(ResultSet rs) throws Exception { List list = new ArrayList<>(); if (null != rs) { int colNum = rs.getMetaData().getColumnCount(); List columnNames = new ArrayList(); for (int i = 1; i <= colNum; i++) { columnNames.add(rs.getMetaData().getColumnName(i)); } list = columnNames; } return list; } }