package com.qiwu.chatbotbackend.db; import com.alibaba.fastjson.JSONObject; import com.google.common.base.Joiner; import com.qiwu.chatbotbackend.actiongenerate.entity.*; 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(); } } } //---------------------------------------变量的操作--------------------------------------------------------------- //------------------------------------------------------------------------------------------------------------ public List queryVariableAll(int botId){ String sql = "select * from chatbot_variable where botId= ?"; List variables = new ArrayList(); Variable variable = null; try{ PreparedStatement ps = conn.prepareStatement(sql); ps.setInt(1,botId); ResultSet rs = ps.executeQuery(); while(rs.next()){ variable = new Variable(rs.getInt("botId"), rs.getInt("variableId"), rs.getInt("categoryId"), rs.getString("name"), rs.getString("description"), rs.getString("defaultValue")); variables.add(variable); } ps.close(); rs.close(); }catch(Exception e){ e.printStackTrace(); } return variables; } public Variable queryVariableByName(int botId, String name){ String sql = "select * from chatbot_variable where botId= ? and name= ?"; Variable variable = null; try{ PreparedStatement ps = conn.prepareStatement(sql); ps.setInt(1,botId); ps.setString(2, name); ResultSet rs = ps.executeQuery(); if(rs.next()){ variable = new Variable(rs.getInt("botId"), rs.getInt("variableId"), rs.getInt("categoryId"), rs.getString("name"), rs.getString("description"), rs.getString("defaultValue")); } ps.close(); rs.close(); }catch(Exception e){ e.printStackTrace(); } return variable; } public Variable queryVariableByVariableId(int botId, int variableId){ String sql = "select * from chatbot_variable where botId= ? and variableId= ?"; Variable variable = null; try{ PreparedStatement ps = conn.prepareStatement(sql); ps.setInt(1,botId); ps.setInt(2, variableId); ResultSet rs = ps.executeQuery(); if(rs.next()){ variable = new Variable(rs.getInt("botId"), rs.getInt("variableId"), rs.getInt("categoryId"), rs.getString("name"), rs.getString("description"), rs.getString("defaultValue")); } ps.close(); rs.close(); }catch(Exception e){ e.printStackTrace(); } return variable; } public Integer insertVariable(Variable variable) { String sql = "insert into chatbot_variable (botId, categoryId, name, description, defaultValue) VALUES (?, ?, ?, ?, ?)"; String userName = ""; String result = "empty"; int id = 0; try { conn.setAutoCommit(false); // 需要用到事务,不能让他自动提交,需要手动提交 PreparedStatement ps = conn.prepareStatement(sql, Statement.RETURN_GENERATED_KEYS); ps.setInt(1, variable.getBotId()); ps.setInt(2, variable.getCategoryId()); ps.setString(3, variable.getName()); ps.setString(4, variable.getDescription()); ps.setString(5, variable.getValue()); ps.executeUpdate(); ResultSet rs = ps.getGeneratedKeys(); if(rs.next()){ id = rs.getInt(1); } conn.commit(); rs.close(); ps.close(); } catch (Exception e) { e.printStackTrace(); } return id; } public Integer insertBotInfo(BotInfo botInfo) { String sql = "insert into bot_info (botId, defaultConfigCategory, welcomeText, goodbyeText, postURL, queryTextTransfer) VALUES (?, ?, ?, ?, ?, 1)"; String userName = ""; String result = "empty"; int id = 0; try { conn.setAutoCommit(false); // 需要用到事务,不能让他自动提交,需要手动提交 PreparedStatement ps = conn.prepareStatement(sql, Statement.RETURN_GENERATED_KEYS); ps.setInt(1, botInfo.getBotId()); ps.setString(2, botInfo.getDefaultConfigCategory()); ps.setString(3, botInfo.getWelcomeText()); ps.setString(4, botInfo.getGoodbyeText()); ps.setString(5, botInfo.getPostURL()); ps.executeUpdate(); ResultSet rs = ps.getGeneratedKeys(); if(rs.next()){ id = rs.getInt(1); } conn.commit(); rs.close(); ps.close(); } catch (Exception e) { e.printStackTrace(); } return id; } public boolean deleteVariable(int botId, int variableId) { String sql = "delete from chatbot_variable where botId= ? and variableId = ?"; String userName = ""; String result = "empty"; boolean b = false; try { PreparedStatement ps = conn.prepareStatement(sql); ps.setInt(1, botId); ps.setInt(2, variableId); b = ps.executeUpdate() == 1; ps.close(); } catch (Exception e) { e.printStackTrace(); } return b; } public Boolean updateVariable(Variable variable){ boolean b = false; try { String sql = "update chatbot_variable set categoryId = ?, name = ?, description = ?, defaultValue = ? where botId = ? and variableId = ?"; PreparedStatement ps = conn.prepareStatement(sql); ps.setInt(1, variable.getCategoryId()); ps.setString(2, variable.getName()); ps.setString(3, variable.getDescription()); ps.setString(4, variable.getValue()); ps.setInt(5, variable.getBotId()); ps.setInt(6, variable.getVariableId()); b = ps.executeUpdate() == 1; ps.close(); return b; } catch (SQLException e) { System.out.println(DELETE_OLD_CONFIG_TERM_ERROR); e.printStackTrace(); return false; } } //----------------------------------------配置条目类别的操作--------------------------------------------------------------- //------------------------------------------------------------------------------------------------------------ public boolean deleteCategoryByCategoryIds(int botId, List idList) { String sql = "delete from config_categories where botId= ? and categoryId in (?)"; boolean b = false; try { PreparedStatement ps = conn.prepareStatement(sql); ps.setInt(1, botId); ps.setString(2, Joiner.on(",").join(idList)); b = ps.executeUpdate() == 1; ps.close(); return b; } catch (SQLException e) { System.out.println(DELETE_OLD_CATEGORY_ERROR); e.printStackTrace(); return false; } } public Integer insertCategoryByBotId(Category category) { String sql = "INSERT INTO config_categories (botId, categoryParentId, categoryName, categoryStatus) VALUES (?, ?, ?, ?)"; String userName = ""; String result = "empty"; boolean b = false; int id = 0; try { conn.setAutoCommit(false); PreparedStatement ps = conn.prepareStatement(sql, Statement.RETURN_GENERATED_KEYS); ps.setInt(1, category.getBotId()); ps.setInt(2, category.getParentId()); ps.setString(3, category.getName()); ps.setString(4, category.getStatus()); ps.executeUpdate(); ResultSet rs = ps.getGeneratedKeys(); if(rs.next()){ id = rs.getInt(1); } conn.commit(); rs.close(); ps.close(); } catch (Exception e) { e.printStackTrace(); } return id; } public List queryCategoryAll(int botId){ String sql = "select * from config_categories where botId= ?"; List categories = new ArrayList(); Category category = null; try{ PreparedStatement ps = conn.prepareStatement(sql); ps.setInt(1,botId); ResultSet rs = ps.executeQuery(); while(rs.next()){ category = new Category(rs.getInt("botId"), rs.getInt("categoryId"), rs.getInt("categoryParentId"), rs.getString("categoryName")); categories.add(category); } ps.close(); rs.close(); }catch(Exception e){ e.printStackTrace(); } return categories; } public Boolean updateCategory(Category category){ boolean b = false; try { String sql = "update config_categories set categoryName = ?, categoryParentId = ? where botId = ? and categoryId = ?"; PreparedStatement ps1 = conn.prepareStatement(sql); ps1.setString(1,category.getName()); ps1.setInt(2, category.getParentId()); ps1.setInt(3, category.getBotId()); ps1.setInt(4, category.getCategoryId()); b = ps1.executeUpdate() ==1; ps1.close(); return b; } catch (SQLException e) { System.out.println(DELETE_OLD_CONFIG_TERM_ERROR); e.printStackTrace(); return false; } } //----------------------------------------配置条目的操作--------------------------------------------------------------- //------------------------------------------------------------------------------------------------------------ public boolean deleteConfigByCategories(int botId, List idList) { boolean b = false; try { PreparedStatement ps1 = conn.prepareStatement("delete from chatbot_config where botId= ? and categoryId in (?)"); ps1.setInt(1, botId); ps1.setString(2, Joiner.on(",").join(idList)); b = ps1.executeUpdate() == 1; ps1.close(); return b; } catch (SQLException e) { System.out.println(DELETE_OLD_CONFIG_TERM_ERROR); e.printStackTrace(); return false; } } public boolean deleteConfigByConfigId(int botId, int configId) { boolean b = false; try { PreparedStatement ps1 = conn.prepareStatement("delete from chatbot_config where botId= ? and configId = ?"); ps1.setInt(1, botId); ps1.setInt(2, configId); b = ps1.executeUpdate() == 1; ps1.close(); return b; } catch (SQLException e) { System.out.println(DELETE_OLD_CONFIG_TERM_ERROR); e.printStackTrace(); return false; } } public List queryConfigByCategoryId(int botId, int configId){ String sql = "select * from chatbot_config where botId= ? and categoryId = ?"; List configTermList = new ArrayList(); ConfigTerm configTerm = null; try{ PreparedStatement ps = conn.prepareStatement(sql); ps.setInt(1,botId); ps.setInt(2, configId); ResultSet rs = ps.executeQuery(); while(rs.next()){ configTerm = new ConfigTerm(rs.getInt("botId"), rs.getInt("configId"), rs.getInt("categoryId"), rs.getString("configData"), rs.getString("configName")); configTermList.add(configTerm); } ps.close(); rs.close(); }catch(Exception e){ e.printStackTrace(); } return configTermList; } public List querySleepingConfig(int botId){ String sql = "select * from chatbot_config where configName != '' and botId = ?"; List configTermList = new ArrayList(); ConfigTerm configTerm = null; try{ PreparedStatement ps = conn.prepareStatement(sql); ps.setInt(1,botId); ResultSet rs = ps.executeQuery(); while(rs.next()){ configTerm = new ConfigTerm(rs.getInt("botId"), rs.getInt("configId"), rs.getInt("categoryId"), rs.getString("configData"), rs.getString("configName")); configTermList.add(configTerm); } ps.close(); rs.close(); }catch(Exception e){ e.printStackTrace(); } return configTermList; } public List queryConfigByCategoryIdList(int botId, List categoryIdList){ String sql = "select * from chatbot_config where botId= ? and categoryId in (" + Joiner.on(",").join(categoryIdList) + ")"; List configTermList = new ArrayList(); ConfigTerm configTerm = null; try{ PreparedStatement ps = conn.prepareStatement(sql); ps.setInt(1,botId); //ps.setString(2, Joiner.on(",").join(categoryIdList)); ResultSet rs = ps.executeQuery(); while(rs.next()){ configTerm = new ConfigTerm(rs.getInt("botId"), rs.getInt("configId"), rs.getInt("categoryId"), rs.getString("configData"), rs.getString("configName")); configTermList.add(configTerm); } ps.close(); rs.close(); }catch(Exception e){ e.printStackTrace(); } return configTermList; } public ConfigTerm queryConfigByConfigId(int botId, int configId){ String sql = "select * from chatbot_config where botId= ? and configId = ?"; List configTermList = new ArrayList(); ConfigTerm configTerm = null; try{ PreparedStatement ps = conn.prepareStatement(sql); ps.setInt(1,botId); ps.setInt(2, configId); ResultSet rs = ps.executeQuery(); if(rs.next()){ configTerm = new ConfigTerm(rs.getInt("botId"), rs.getInt("configId"), rs.getInt("categoryId"), rs.getString("configData"), rs.getString("configName")); //actionTextList.add(rs.getString("actionText")); //configTermList.add(configTerm); } ps.close(); rs.close(); }catch(Exception e){ e.printStackTrace(); } return configTerm; } public int insertConfig(int botId, JSONObject configData, int categoryId) { boolean insertFlag = true; Boolean b = false; int id = 0; String sql = "insert into chatbot_config (botId, categoryId, configData, configName) VALUES (?, ?, ?, ?)"; try { conn.setAutoCommit(false); // 需要用到事务,不能让他自动提交,需要手动提交 PreparedStatement ps = conn.prepareStatement(sql, Statement.RETURN_GENERATED_KEYS); ps.setString(1, String.valueOf(botId)); ps.setString(2, String.valueOf(categoryId)); ps.setString(3, configData.toJSONString()); ps.setString(4, (String)configData.get("configName")); b = ps.executeUpdate() == 1; ResultSet rs = ps.getGeneratedKeys(); if(rs.next()){ id = rs.getInt(1); } conn.commit(); rs.close(); ps.close(); } catch (SQLException e) { e.printStackTrace(); } return id; } public Boolean updateConfig(ConfigTerm configTerm){ boolean b = false; try { String sql = "update chatbot_config set configData = ? ,categoryId = ?, configName = ? where botId=? and configId=?"; PreparedStatement ps1 = conn.prepareStatement(sql); ps1.setString(1, configTerm.getConfigJsonText()); ps1.setInt(2, configTerm.getCategoryId()); ps1.setString(3, configTerm.getConfigName()); ps1.setInt(4, configTerm.getBotId()); ps1.setInt(5, configTerm.getConfigId()); b = ps1.executeUpdate() == 1; ps1.close(); return b; } catch (SQLException e) { System.out.println(DELETE_OLD_CONFIG_TERM_ERROR); e.printStackTrace(); return false; } } //----------------------------------------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(); ps1.close(); PreparedStatement ps2 = conn.prepareStatement(sql2); ps2.setInt(1, botId); b2 = ps2.execute(); ps2.close(); PreparedStatement ps3 = conn.prepareStatement(sql3); ps3.setInt(1, botId); b3 = ps3.execute(); ps3.close(); PreparedStatement ps4 = conn.prepareStatement(sql4); ps4.setInt(1, botId); b4 = ps4.execute(); ps4.close(); } catch (SQLException e) { System.out.println(DELETE_USER_DATA_ERROR); e.printStackTrace(); return false; } return b1&&b2&&b3&&b4; } public Boolean batchInsertCondition(List conditionList){ 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(Condition condition:conditionList){ ps.setInt(1, condition.getBotId()); ps.setString(2, condition.getConditionJsonText()); ps.setString(3, condition.getCategory()); ps.setString(4, condition.getActionText()); ps.setString(5, condition.getActive()); 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")); } ps.close(); rs.close(); }catch(Exception e){ e.printStackTrace(); } return actionTextList; } public Condition queryActiveCondition(int botId){ Condition condition = null; String sql = "select * from chatbot_condition where active= 1 and botId = ?"; String pre_name = ""; List actionTextList = new ArrayList(); try{ PreparedStatement ps = conn.prepareStatement(sql); ps.setInt(1, botId); ResultSet rs = ps.executeQuery(); if(rs.next()){ condition = new Condition(rs.getInt("botId"), rs.getInt("conditionId"), rs.getString("conditionData"), rs.getString("actionText"), rs.getString("categoryInfo"), rs.getString("active"), rs.getInt("displayOrder")); } ps.close(); rs.close(); }catch(Exception e){ e.printStackTrace(); } return condition; } public boolean deleteConditionBy2Id(int botId, int conditionId) { boolean b; try { PreparedStatement ps1 = conn.prepareStatement("delete from chatbot_condition where botId= ? and conditionId=?"); ps1.setInt(1, botId); ps1.setInt(2, conditionId); b = ps1.executeUpdate() == 1; ps1.close(); return b; } catch (SQLException e) { System.out.println(DELETE_OLD_CONFIG_TERM_ERROR); e.printStackTrace(); return false; } } public List queryConditionCategoriesById(int botId){ String sql = "select categoryInfo 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("categoryInfo")); } ps.close(); rs.close(); }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("welcomeText"), rs.getString("goodbyeText"), rs.getString("postURL"), rs.getString("defaultConfigCategory"), rs.getString("bothValue"), rs.getString("queryTextTransfer")); } ps.close(); rs.close(); }catch(Exception e){ e.printStackTrace(); } return botInfo; } public Boolean updateBotInfo(BotInfo botInfo){ boolean b; try { String sql = "update bot_info set welcomeText = ?, goodbyeText = ?, postURL = ?, defaultConfigCategory = ?, bothValue = ?, queryTextTransfer = ? where botId = ?"; PreparedStatement ps1 = conn.prepareStatement(sql); ps1.setString(1, botInfo.getWelcomeText()); ps1.setString(2, botInfo.getGoodbyeText()); ps1.setString(3, botInfo.getPostURL()); ps1.setString(4, botInfo.getDefaultConfigCategory()); ps1.setString(5, botInfo.getBothValue()); ps1.setString(6, botInfo.getQueryTextTransfer()); ps1.setInt(7, botInfo.getBotId()); b = ps1.executeUpdate() == 1; ps1.close(); return b; } catch (SQLException e) { System.out.println(DELETE_OLD_CONFIG_TERM_ERROR); e.printStackTrace(); return false; } } public Boolean updateConditionById(Condition condition){ boolean b; try { String sql = "update chatbot_condition set conditionData = ?, actionText = ?, categoryInfo = ?, active = ?, displayOrder = ? 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.getDisplayOrder()); ps1.setInt(6, condition.getBotId()); ps1.setInt(7, condition.getConditionId()); b = ps1.executeUpdate() == 1; 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(int botId, String category){ boolean b; List conditionList = new ArrayList(); Condition condition = null; try { String sql = "select * from chatbot_condition wher botId = ? and categoryInfo = ? order by displayOrder asc"; 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("categoryInfo"), rs.getString("active"), rs.getInt("displayOrder")); 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 where 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("categoryInfo"), rs.getString("active"), rs.getInt("displayOrder")); } ps.close(); rs.close(); return condition; } catch (SQLException e) { System.out.println(CONFIG_DELETE_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 where botId = ? order by displayOrder asc"; 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("categoryInfo"), rs.getString("active"), rs.getInt("displayOrder")); conditionList.add(condition); } ps.close(); rs.close(); return conditionList; } catch (SQLException e) { System.out.println(CONFIG_DELETE_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.executeUpdate() == 1; 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.executeUpdate() == 1; 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.executeUpdate() == 1; ps.close(); return b; } catch (SQLException e) { System.out.println(DELETE_OLD_CATEGORY_ERROR); e.printStackTrace(); return false; } } public boolean deleteConditionByBotId(int botId) { String sql = "delete from chatbot_condition where botId= ?"; boolean b; try { PreparedStatement ps = conn.prepareStatement(sql); ps.setString(1, String.valueOf(botId)); b = ps.executeUpdate() == 1; 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); //unknown文本可能特别长,就不插入数据库了 if(varValue.length() > 8000) continue; 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 batchInsertVariable(int botId, List variableList) { 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); for(Variable variable:variableList){ ps2.setInt(1, botId); ps2.setString(2, variable.getName()); ps2.setString(3, variable.getDescription()); ps2.setString(4, variable.getValue()); ps2.addBatch(); } count = ps2.executeBatch(); conn.commit(); 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 batchInsertConfig(int botId, List configTermList) { 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 (ConfigTerm configTerm : configTermList) { ps.setInt(1, botId); ps.setInt(2, configTerm.getCategoryId()); ps.setString(3, configTerm.getConfigJsonText()); ps.setString(4, configTerm.getConfigName()); 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 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.setInt(1, 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 queryGameElementsByProjectId(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.executeUpdate() == 1; 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; } }