package com.qiwu.chatbotbackend.db; import com.alibaba.fastjson.JSONObject; import com.qiwu.chatbotbackend.entity.ArchiveInfo; import com.qiwu.chatbotbackend.gameDesign.ElementConstructor; import java.sql.*; import java.util.ArrayList; import java.util.Iterator; import java.util.List; import java.util.Map; public class SqlHelper { private Connection conn = null; /* public SqlHelper(){ conn = DB.getConnection(); } */ public SqlHelper() throws SQLException { conn = C3p0Utils.getConnection(); } public void destroy() { if (conn != null) { try { conn.close(); } catch (Exception e) { e.printStackTrace(); } } } /* public boolean insertAction(QuestionKeyWord qkw){ String sql = "insert into user_info (chat_key,action_name) values(?,?)"; boolean b = false; try{ PreparedStatement ps = conn.prepareStatement(sql); ps.setString(1,qkw.getChatKey()); ps.setString(2,qkw.getActionName()); b = ps.execute(); return b; } catch(Exception e){ e.printStackTrace(); } return b; } public boolean updateAction(QuestionKeyWord qkw){ String sql = "update user_info set action_name=? where chat_key=?"; boolean b = false; try{ PreparedStatement ps = conn.prepareStatement(sql); ps.setString(1,qkw.getActionName()); ps.setString(2,qkw.getChatKey()); b = ps.execute(); return b; } catch (SQLException e) { e.printStackTrace(); } return b; } public String queryActionByKey(String chat_key){ String sql = "select * from user_info where chat_key= ?"; String pre_name = ""; try{ PreparedStatement ps = conn.prepareStatement(sql); ps.setString(1,chat_key); ResultSet rs = ps.executeQuery(); if(rs.next()) return rs.getString("action_name"); }catch(Exception e){ e.printStackTrace(); } return "empty"; } */ private static void releaseSource(Connection _conn, PreparedStatement _st, ResultSet _rs) throws SQLException { C3p0Utils.close(_conn, _st, _rs); } public String queryUsernameByKey(String chat_key) { String sql = "select * from chats where chat_key = ?"; String userName = ""; String result = "empty"; try { PreparedStatement ps = conn.prepareStatement(sql); ps.setString(1, chat_key); ResultSet rs = ps.executeQuery(); if (rs.next()) result = rs.getString("bot"); ps.close(); rs.close(); } catch (Exception e) { e.printStackTrace(); } return result; } public boolean deleteConfigByIdGroup(int botId, List idGroup) throws SQLException { StringBuilder inClause = new StringBuilder(); boolean firstValue = true; int batchSize = idGroup.size(); for (int i = 0; i < batchSize; i++) { inClause.append('?'); if (firstValue) { firstValue = false; } else { inClause.append(','); } } String sql = "delete from chatbot_config where botId = ? and configId in (" + inClause.toString() + ')'; String userName = ""; boolean b = true; try { PreparedStatement ps = conn.prepareStatement(sql); ps.setString(1, String.valueOf(botId)); b = ps.execute(); ps.close(); return b; } catch (Exception e) { e.printStackTrace(); } return b; } public boolean deleteConfigByBotId(int botId) { boolean b; try { PreparedStatement ps1 = conn.prepareStatement("delete from chatbot_config where botId= ?"); ps1.setString(1, String.valueOf(botId)); b = ps1.execute(); ps1.close(); return b; } catch (SQLException e) { System.out.println("删除旧数据config失败"); e.printStackTrace(); return false; } } public boolean deleteVariableByBotId(int botId) { String sql = "delete from chatbot_variable where botId= ?"; boolean b; try { PreparedStatement ps = conn.prepareStatement(sql); ps.setString(1, String.valueOf(botId)); b = ps.execute(); ps.close(); return b; } catch (SQLException e) { System.out.println("删除旧数据variable失败"); e.printStackTrace(); return false; } } public boolean deleteCategoryByBotId(int botId) { String sql = "delete from config_categories where botId= ?"; boolean b; try { PreparedStatement ps = conn.prepareStatement(sql); ps.setString(1, String.valueOf(botId)); b = ps.execute(); ps.close(); return b; } catch (SQLException e) { System.out.println("删除旧数据category失败"); e.printStackTrace(); return false; } } public boolean batchInsertVariableByBotId(int botId, ElementConstructor constructor) { int count[]; boolean insertFlag = true; String sql = "", varName, varValue, description; try { conn.setAutoCommit(false); sql = "insert into chatbot_variable (botId, name, categoryId, description, defaultValue) VALUES (?, ?, 0, ?, ?)"; PreparedStatement ps2 = conn.prepareStatement(sql, Statement.RETURN_GENERATED_KEYS); Iterator iit = constructor.getVarInitialMap().keySet().iterator(); while (iit.hasNext()) { varName = iit.next(); varValue = constructor.getVarInitialMap().get(varName); if (constructor.getConditionDescription().get(varName) == null) description = ""; else description = constructor.getConditionDescription().get(varName); ps2.setString(1, String.valueOf(botId)); ps2.setString(2, varName); ps2.setString(3, description); ps2.setString(4, varValue); ps2.addBatch(); } count = ps2.executeBatch(); conn.commit(); for (int i : count) { if (i == 0) { conn.rollback(); System.out.println("======出现异常,回滚========="); insertFlag = false; } } ps2.close(); } catch (SQLException e) { try { conn.rollback(); } catch (SQLException e1) { e1.printStackTrace(); } System.out.println("------出现异常,回滚----------"); e.printStackTrace(); } return insertFlag; } public boolean batchInsertConfigByBotId(int botId, List jsonList, List idList, int categoryId) { int count[]; boolean insertFlag = true; String sql = "insert into chatbot_config (botId, categoryId, configData, configName) VALUES (?, ?, ?, ?)"; try { conn.setAutoCommit(false); // 需要用到事务,不能让他自动提交,需要手动提交 PreparedStatement ps = conn.prepareStatement(sql, Statement.RETURN_GENERATED_KEYS); for (JSONObject json : jsonList) { ps.setString(1, String.valueOf(botId)); ps.setString(2, String.valueOf(categoryId)); ps.setString(3, json.toJSONString()); ps.setString(4, (String)json.get("configName")); ps.addBatch(); } count = ps.executeBatch(); conn.commit(); for (int i : count) { if (i == 0) { conn.rollback(); System.out.println("======出现异常,回滚========="); insertFlag = false; } } ResultSet rs = ps.getGeneratedKeys(); while (rs.next()) idList.add(rs.getInt(1)); ps.close(); rs.close(); } catch (SQLException e) { try { conn.rollback(); } catch (SQLException e1) { e1.printStackTrace(); } System.out.println("------出现异常,回滚----------"); e.printStackTrace(); } return insertFlag; } public boolean batchInsertCategoryByBotId(int botId, Map categoryMap) { int count[], iterNum = 0, id; boolean insertFlag = true; String sql = "", varName, varValue, description; ArrayList nameList = new ArrayList(); try { conn.setAutoCommit(false); sql = "insert into config_categories (botId, categoryParentId, categoryName, categoryStatus) VALUES (?, -1, ?, 'avaliable')"; PreparedStatement ps = conn.prepareStatement(sql, Statement.RETURN_GENERATED_KEYS); Iterator iit = categoryMap.keySet().iterator(); while (iit.hasNext()) { varName = iit.next(); nameList.add(varName); ps.setString(1, String.valueOf(botId)); ps.setString(2, varName); ps.addBatch(); } count = ps.executeBatch(); conn.commit(); for (int i : count) { if (i == 0) { conn.rollback(); System.out.println("======出现异常,回滚========="); insertFlag = false; } } ResultSet rs = ps.getGeneratedKeys(); while (rs.next()){ id = rs.getInt(1); categoryMap.put(nameList.get(iterNum), id); iterNum++; } ps.close(); rs.close(); } catch (SQLException e) { try { conn.rollback(); } catch (SQLException e1) { e1.printStackTrace(); } System.out.println("------出现异常,回滚----------"); e.printStackTrace(); } return insertFlag; } public JSONObject queryGameElementsByBotId(int id) { String sql = "select * from generate_json_tb where project_id = ?"; String userName = ""; String result = "{}"; try { PreparedStatement ps = conn.prepareStatement(sql); ps.setString(1, String.valueOf(id)); ResultSet rs = ps.executeQuery(); if (rs.next()) result = rs.getString("json_text"); ps.close(); rs.close(); } catch (Exception e) { e.printStackTrace(); } return JSONObject.parseObject(result); } public static String queryEnvJsonByBotName(String botName) throws SQLException { String sql = "select * from generate_json_tb where bot_name = ?"; Connection conn = C3p0Utils.getConnection(); String result = "{}"; try { PreparedStatement ps = conn.prepareStatement(sql); ps.setString(1, String.valueOf(botName)); ResultSet rs = ps.executeQuery(); if (rs.next()) result = rs.getString("env_json_text"); ps.close(); rs.close(); conn.close(); } catch (Exception e) { e.printStackTrace(); } return result; } public boolean insertVariableByBotId(int id, String variableName, String description, String defaultValue) { String sql = "insert into chatbot_variable (botId, name, description, defaultValue) VALUES (?, ?, ?, ?)"; String userName = ""; String result = "empty"; boolean b = true; try { PreparedStatement ps = conn.prepareStatement(sql); ps.setString(1, String.valueOf(id)); ps.setString(2, variableName); ps.setString(3, description); ps.setString(4, defaultValue); b = ps.execute(); ps.close(); conn.commit(); } catch (Exception e) { e.printStackTrace(); } return b; } public boolean insertConfigByBotId(int botId, JSONObject json, List idList, int categoryId) { String sql = "insert into chatbot_config (botId, categoryId, configData, configName) VALUES (?, ?, ?, ?)"; int id = 0; boolean b = true; int bNum = 0; try { PreparedStatement ps = conn.prepareStatement(sql, Statement.RETURN_GENERATED_KEYS); ps.setString(1, String.valueOf(botId)); ps.setString(2, String.valueOf(categoryId)); ps.setString(3, json.toJSONString()); ps.setString(4, (String)json.get("configName")); System.out.println(ps.toString()); bNum = ps.executeUpdate(); ResultSet rs = ps.getGeneratedKeys(); if(rs.next()){ id = rs.getInt(1); idList.add(id); } ps.close(); rs.close(); conn.commit(); } catch (Exception e) { e.printStackTrace(); } return b; } public Boolean insertArchiveInfo(String archiveName, String userKey, String site){ String sql = "insert into game_archive (archive_id, user_key, site, plot) VALUES (?, ?, ?, '')"; int id = 0; boolean b = true; int bNum = 0; try { PreparedStatement ps = conn.prepareStatement(sql); ps.setString(1, String.valueOf(archiveName)); ps.setString(2, String.valueOf(userKey)); ps.setString(3, String.valueOf(site)); System.out.println(ps.toString()); bNum = ps.executeUpdate(); ps.close(); } catch (Exception e) { e.printStackTrace(); } return bNum == 1; } public ArchiveInfo queryUserLastGameArchive(String userKey){ String sql = "select * from game_archive where user_key = ? order by update_time desc"; String result = "{}"; ArchiveInfo archiveInfo = null; try { PreparedStatement ps = conn.prepareStatement(sql); ps.setString(1, String.valueOf(userKey)); ResultSet rs = ps.executeQuery(); if (rs.next()) archiveInfo = new ArchiveInfo(rs.getString("archive_id"),rs.getString("user_key"), rs.getString("site"),rs.getString("update_time"),rs.getString("plot")); ps.close(); rs.close(); conn.close(); } catch (Exception e) { e.printStackTrace(); } return archiveInfo; } public ArchiveInfo queryUserLastGameArchiveBySite(String userKey, String site){ String sql = "select * from game_archive where user_key = ? and site = ? order by update_time desc"; String result = "{}"; ArchiveInfo archiveInfo = null; try { PreparedStatement ps = conn.prepareStatement(sql); ps.setString(1, userKey); ps.setString(2, site); ResultSet rs = ps.executeQuery(); if (rs.next()) { archiveInfo = new ArchiveInfo(rs.getString("archive_id"), rs.getString("user_key"), rs.getString("site"), rs.getString("update_time"), rs.getString("plot")); } ps.close(); rs.close(); conn.close(); } catch (Exception e) { e.printStackTrace(); } return archiveInfo; } public boolean deleteArchiveByBotAccount(String botAccount) { String sql = "delete from game_archive where user_key like ?"; boolean b; try { PreparedStatement ps = conn.prepareStatement(sql); ps.setString(1, "%" + botAccount); b = ps.execute(); ps.close(); return b; } catch (SQLException e) { System.out.println("删除游戏存档失败"); e.printStackTrace(); return false; } } public List queryArchiveByUserKey(String userKey){ String sql = "select * from game_archive where user_key = ? order by update_time desc"; List archiveList = new ArrayList(); ArchiveInfo archiveInfo = null; try { PreparedStatement ps = conn.prepareStatement(sql); ps.setString(1, userKey); ResultSet rs = ps.executeQuery(); if (rs.next()) { archiveInfo = new ArchiveInfo(rs.getString("archive_id"), rs.getString("user_key"), rs.getString("site"), rs.getString("update_time"), rs.getString("plot")); archiveList.add(archiveInfo); } ps.close(); rs.close(); conn.close(); } catch (Exception e) { e.printStackTrace(); } return archiveList; } }