package com.qiwu.chatbotbackend.actiongenerate.DB; import com.google.common.base.Joiner; import com.qiwu.chatbotbackend.actiongenerate.entity.EngineUnit; import com.qiwu.chatbotbackend.db.C3p0Utils; import java.sql.Connection; import java.sql.PreparedStatement; import java.sql.ResultSet; import java.sql.SQLException; import java.util.HashMap; import java.util.List; import java.util.Map; public class EngineUtils { private Connection conn = null; public EngineUtils() throws SQLException { conn = C3p0Utils.getEngineConnection(); } public void destroy() { if (conn != null) { try { conn.close(); } catch (Exception e) { e.printStackTrace(); } } } public String queryKeywordUnitText(int botId){ String sql = "select text from units where user_id = ? and name = 'keywords'"; String keywordText = ""; try{ PreparedStatement ps = conn.prepareStatement(sql); ps.setInt(1,botId); ResultSet rs = ps.executeQuery(); if(rs.next()){ keywordText = (rs.getString("text")); } ps.close(); }catch(Exception e){ e.printStackTrace(); } return keywordText; } public Boolean updateKeywordUnitText(int botId, String keywordText){ Boolean b = true; String sql = "update units set text = ? where user_id = ? and name = 'keywords'"; try{ PreparedStatement ps = conn.prepareStatement(sql); ps.setString(1, keywordText); ps.setInt(2,botId); b = ps.executeUpdate() == 1; ps.close(); }catch(Exception e){ e.printStackTrace(); } return b; } public Boolean updateWelcomeText(int botId, String welcome){ boolean b; try { String sql = "update users set welcome = ? where id=?"; PreparedStatement ps1 = conn.prepareStatement(sql); ps1.setString(1, welcome + "#welcome"); ps1.setInt(2, botId); b = ps1.executeUpdate() == 1; ps1.close(); return b; } catch (SQLException e) { e.printStackTrace(); return false; } } public Map queryConditionPatternText(int botId, List patternRelationList){ String sql = "select user_id, text from `users` join `units` on users.id = units.user_id where users.username in ('" + Joiner.on("','").join(patternRelationList)+ "') and units.name = 'actions'"; String keywordText = ""; Map patternTextMap = new HashMap(); try{ PreparedStatement ps = conn.prepareStatement(sql); //ps.setString(1, Joiner.on(",").join(patternRelationList)); ResultSet rs = ps.executeQuery(); while(rs.next()){ patternTextMap.put(rs.getInt("user_id"), rs.getString("text")); } ps.close(); rs.close(); }catch(Exception e){ e.printStackTrace(); } return patternTextMap; } public Map queryUnitsByIdList(List idList){ String sql = "select user_id, name, text from units where user_id in (" + Joiner.on(",").join(idList) + ") and name in ('actions','keywords')"; Integer id; Map id2unit= new HashMap(); try{ PreparedStatement ps = conn.prepareStatement(sql); //ps.setArray(1, idList.toArray()); ResultSet rs = ps.executeQuery(); while(rs.next()){ id = rs.getInt("user_id"); if(id2unit.get(id) == null){ id2unit.put(id, new EngineUnit()); } switch(rs.getString("name")){ case "actions": id2unit.get(id).setActions(rs.getString("text")); break; case "keywords": id2unit.get(id).setKeywords(rs.getString("text")); break; } } ps.close(); rs.close(); }catch(Exception e){ e.printStackTrace(); } return id2unit; } public String queryBotAccountByBotId(int botId){ String sql = "select username from users where id= ?"; String botAccount = ""; try { PreparedStatement ps = conn.prepareStatement(sql); ps.setInt(1, botId); ResultSet rs = ps.executeQuery(); if (rs.next()) botAccount = rs.getString("username"); ps.close(); rs.close(); conn.close(); } catch (Exception e) { e.printStackTrace(); } return botAccount; } public String queryBotNickNameByName(String userName){ String sql = "select nickname from users where username= ?"; String nickname = ""; try { PreparedStatement ps = conn.prepareStatement(sql); ps.setString(1, userName); ResultSet rs = ps.executeQuery(); if (rs.next()) nickname = rs.getString("nickname"); ps.close(); rs.close(); conn.close(); } catch (Exception e) { e.printStackTrace(); } return nickname; } }