package com.qiwu.chatbotbackend.db; import com.alibaba.fastjson.JSONObject; import com.qiwu.actiongenerate.entity.BotInfo; import com.qiwu.actiongenerate.entity.Condition; 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; import static com.qiwu.chatbotbackend.Config.Config.*; 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(); } } } //----------------------------------------condition操作--------------------------------------------------------------- //------------------------------------------------------------------------------------------------------------ public Boolean deleteUserData(int botId){ String sql1 = "delete from chatbot_config where botId = ?"; String sql2 = "delete from chatbot_condition where botId = ?"; String sql3 = "delete from config_categories where botId = ?"; String sql4 = "delete from chatbot_variable where botId = ?"; Boolean b1, b2, b3, b4; try{ PreparedStatement ps1 = conn.prepareStatement(sql1); ps1.setInt(1, botId); b1 = ps1.execute(); PreparedStatement ps2 = conn.prepareStatement(sql2); ps2.setInt(1, botId); b2 = ps1.execute(); PreparedStatement ps3 = conn.prepareStatement(sql3); ps3.setInt(1, botId); b3 = ps1.execute(); PreparedStatement ps4 = conn.prepareStatement(sql4); ps4.setInt(1, botId); b4 = ps1.execute(); } catch (SQLException e) { System.out.println(DELETE_USER_DATA_ERROR); e.printStackTrace(); return false; } return b1&&b2&&b3&&b4; } public Boolean batchInsertCondition(List insertList){ int count[]; boolean insertFlag = true; String sql = "", varName, varValue, description; try { conn.setAutoCommit(false); sql = "insert into chatbot_condition (botId, conditionData, categoryInfo, actionText, active) values(?,?,?,?,?)"; PreparedStatement ps = conn.prepareStatement(sql, Statement.RETURN_GENERATED_KEYS); for(String[] elements:insertList){ ps.setString(1, elements[0]); ps.setString(2, elements[1]); ps.setString(3, elements[2]); ps.setString(4, elements[3]); ps.setString(5, elements[4]); ps.addBatch(); } count = ps.executeBatch(); conn.commit(); for (int i : count) { if (i == 0) { conn.rollback(); System.out.println("======出现异常,回滚========="); insertFlag = false; } } ps.close(); } catch (SQLException e) { try { conn.rollback(); } catch (SQLException e1) { e1.printStackTrace(); } System.out.println("------出现异常,回滚----------"); e.printStackTrace(); } return insertFlag; } public List queryActionTextList(int botId){ String sql = "select actionText from chatbot_condition where botId= ?"; String pre_name = ""; List actionTextList = new ArrayList(); try{ PreparedStatement ps = conn.prepareStatement(sql); ps.setInt(1,botId); ResultSet rs = ps.executeQuery(); while(rs.next()){ actionTextList.add(rs.getString("actionText")); } }catch(Exception e){ e.printStackTrace(); } return actionTextList; } public Condition queryActiveCondition(int botId){ Condition condition = null; String sql = "select * from chatbot_condition where active= 1"; String pre_name = ""; List actionTextList = new ArrayList(); try{ PreparedStatement ps = conn.prepareStatement(sql); ResultSet rs = ps.executeQuery(); if(rs.next()){ condition = new Condition(rs.getInt("botId"), rs.getInt("conditionId"), rs.getString("conditionData"), rs.getString("actionText"), rs.getString("categoryStr"), rs.getString("active")); } }catch(Exception e){ e.printStackTrace(); } return condition; } public List queryConditionCategoriesById(int botId){ String sql = "select categoryStr from chatbot_condition where botId= ?"; String pre_name = ""; List categoryList = new ArrayList(); try{ PreparedStatement ps = conn.prepareStatement(sql); ps.setInt(1,botId); ResultSet rs = ps.executeQuery(); while(rs.next()){ categoryList.add(rs.getString("categoryStr")); } }catch(Exception e){ e.printStackTrace(); } return categoryList; } public BotInfo queryBotInfo(int botId){ String sql = "select * from bot_info where botId = ?"; BotInfo botInfo = null; try{ PreparedStatement ps = conn.prepareStatement(sql); ps.setInt(1,botId); ResultSet rs = ps.executeQuery(); if(rs.next()){ botInfo = new BotInfo(rs.getInt("botId"), rs.getString("welcomText"), rs.getString("goodbyeText"), rs.getString("postURL"), rs.getString("defaultConfigCategory"), rs.getString("bothValue"), rs.getString("queryTextTransfer")); } }catch(Exception e){ e.printStackTrace(); } return botInfo; } public Boolean updateConditionById(Condition condition){ boolean b; try { String sql = "update chatbot_condition set conditionData = ?, actionText = ?, categoryInfo = ?, active = ? where botId = ? and conditionId=?"; PreparedStatement ps1 = conn.prepareStatement(sql); ps1.setString(1, condition.getConditionJsonText()); ps1.setString(2, condition.getActionText()); ps1.setString(3, condition.getCategory()); ps1.setString(4, condition.getActive()); ps1.setInt(5, condition.getBotId()); ps1.setInt(6, condition.getConditionId()); b = ps1.execute(); ps1.close(); return b; } catch (SQLException e) { System.out.println(DELETE_OLD_CONFIG_TERM_ERROR); e.printStackTrace(); return false; } } public boolean batchUpdateCondition(int botId, List conditionList) { int count[]; boolean insertFlag = true; try { conn.setAutoCommit(false); String sql = "update chatbot_condition set conditionData = ?, actionText = ?, categoryInfo = ?, active = ? where botId = ? and conditionId=?"; PreparedStatement ps = conn.prepareStatement(sql, Statement.RETURN_GENERATED_KEYS); for(Condition condition:conditionList){ ps.setString(1, condition.getConditionJsonText()); ps.setString(2, condition.getActionText()); ps.setString(3, condition.getCategory()); ps.setString(4, condition.getActive()); ps.setInt(5, condition.getBotId()); ps.setInt(6, condition.getConditionId()); ps.addBatch(); } count = ps.executeBatch(); conn.commit(); for (int i : count) { if (i == 0) { conn.rollback(); System.out.println("======出现异常,回滚========="); insertFlag = false; } } ps.close(); } catch (SQLException e) { try { conn.rollback(); } catch (SQLException e1) { e1.printStackTrace(); } System.out.println("------出现异常,回滚----------"); e.printStackTrace(); } return insertFlag; } public List queryConditionByCategory(String category){ boolean b; List conditionList = new ArrayList(); Condition condition = null; try { String sql = "query chatbot_condition wher botId = ? where categoryStr = ?"; PreparedStatement ps = conn.prepareStatement(sql); ps.setString(1, category); ResultSet rs = ps.executeQuery(); while(rs.next()){ condition = new Condition(rs.getInt("botId"), rs.getInt("conditionId"), rs.getString("conditionData"), rs.getString("actionText"), rs.getString("categoryStr"), rs.getString("active")); conditionList.add(condition); } ps.close(); rs.close(); return conditionList; } catch (SQLException e) { System.out.println(DELETE_OLD_CONFIG_TERM_ERROR); e.printStackTrace(); return conditionList; } } public List queryConditionByCategory(int botId, String category){ boolean b; List conditionList = new ArrayList(); Condition condition = null; try { String sql = "select * from chatbot_condition wher botId = ? and categoryStr = ?"; PreparedStatement ps = conn.prepareStatement(sql); ps.setInt(1, botId); ps.setString(2, category); ResultSet rs = ps.executeQuery(); while(rs.next()){ condition = new Condition(rs.getInt("botId"), rs.getInt("conditionId"), rs.getString("conditionData"), rs.getString("actionText"), rs.getString("categoryStr"), rs.getString("active")); conditionList.add(condition); } ps.close(); rs.close(); return conditionList; } catch (SQLException e) { System.out.println(DELETE_OLD_CONFIG_TERM_ERROR); e.printStackTrace(); return conditionList; } } public Condition queryConditionByConditionId(int botId, int conditionId){ boolean b; List conditionList = new ArrayList(); Condition condition = null; try { String sql = "select * from chatbot_condition wher botId = ? and conditionId = ?"; PreparedStatement ps = conn.prepareStatement(sql); ps.setInt(1, botId); ps.setInt(2, conditionId); ResultSet rs = ps.executeQuery(); if(rs.next()){ condition = new Condition(rs.getInt("botId"), rs.getInt("conditionId"), rs.getString("conditionData"), rs.getString("actionText"), rs.getString("categoryStr"), rs.getString("active")); } ps.close(); rs.close(); return condition; } catch (SQLException e) { System.out.println(DELETE_OLD_CONFIG_TERM_ERROR); e.printStackTrace(); return condition; } } public List queryConditionAll(int botId){ boolean b; List conditionList = new ArrayList(); Condition condition = null; try { String sql = "select * from chatbot_condition wher botId = ? where botId = ?"; PreparedStatement ps = conn.prepareStatement(sql); ps.setInt(1, botId); ResultSet rs = ps.executeQuery(); while(rs.next()){ condition = new Condition(rs.getInt("botId"), rs.getInt("conditionId"), rs.getString("conditionData"), rs.getString("actionText"), rs.getString("categoryStr"), rs.getString("active")); conditionList.add(condition); } ps.close(); rs.close(); return conditionList; } catch (SQLException e) { System.out.println(DELETE_OLD_CONFIG_TERM_ERROR); e.printStackTrace(); return conditionList; } } //------------------------------------------------------------------------------------------------------------ //------------------------------------------------------------------------------------------------------------ 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(DELETE_OLD_CONFIG_TERM_ERROR); 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(DELETE_OLD_VARIABLE_ERROR); 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(DELETE_OLD_CATEGORY_ERROR); 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; } }