From dad4bcff159a102e260fbaeb3ae88d055258efa5 Mon Sep 17 00:00:00 2001 From: zlzw <583819556@qq.com> Date: Sat, 30 Nov 2024 14:43:08 +0800 Subject: [PATCH] =?UTF-8?q?update=20=E8=B0=83=E6=95=B4=E6=95=B0=E6=8D=AE?= =?UTF-8?q?=E5=BA=93=20update=20superchat=E4=BF=A1=E6=81=AF?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- Web/html/body/video.html | 12 +- .../biliapi/databases/BiliLiveDatabase.java | 79 +++++---- .../bilibili/datas/web/LiveVideoDanmu.java | 67 ------- .../bilibili/datas/web/LiveWebDanmuBean.java | 36 ++++ .../bilibili/datas/web/SuperChatGiftBean.java | 35 ++++ .../bilibili/services/LiveDanmuService.java | 35 ++-- .../services/LiveDatabasesService.java | 53 ++++-- .../yutou/bilibili/services/LiveService.java | 14 +- .../yutou/common/databases/SQLiteManager.java | 163 ++++++++---------- 9 files changed, 257 insertions(+), 237 deletions(-) delete mode 100644 src/main/java/com/yutou/bilibili/datas/web/LiveVideoDanmu.java create mode 100644 src/main/java/com/yutou/bilibili/datas/web/LiveWebDanmuBean.java create mode 100644 src/main/java/com/yutou/bilibili/datas/web/SuperChatGiftBean.java diff --git a/Web/html/body/video.html b/Web/html/body/video.html index e7e2316..7d33b11 100644 --- a/Web/html/body/video.html +++ b/Web/html/body/video.html @@ -242,21 +242,22 @@ var chartView = null; function initChart(roomId, videoId) { getVideoGiftInfo(roomId, videoId) - .then(data => { - if (data.status != 100) { + .then(json => { + if (json.status != 100) { return } var lables = []; var values = []; - data.data.guardInfo.forEach(item => { + json.data.guardInfo.forEach(item => { lables.push(item.gift_name + "\n" + item.total_price + "¥") values.push(item.total_num) }); - data.data.giftInfo.forEach(item => { + json.data.giftInfo.forEach(item => { lables.push(item.gift_name + "\n" + item.total_price + "¥") values.push(item.total_gift_num) }); - setChart(lables, values, data) + setChart(lables, values, json) + initSC(json.data.superChat) }) } @@ -364,7 +365,6 @@ getDanmu(getParam('roomId'), videoId, page) .then(data => { if (page === 0) { - initSC(data.data.superChat) $('#danmuSize').get(0).innerHTML = data.data.danmuCount; barrageRenderer.setBarrages(data.data.danmu) } else { diff --git a/src/main/java/com/yutou/biliapi/databases/BiliLiveDatabase.java b/src/main/java/com/yutou/biliapi/databases/BiliLiveDatabase.java index a7e6501..2ab2fb7 100644 --- a/src/main/java/com/yutou/biliapi/databases/BiliLiveDatabase.java +++ b/src/main/java/com/yutou/biliapi/databases/BiliLiveDatabase.java @@ -2,26 +2,23 @@ package com.yutou.biliapi.databases; import com.alibaba.fastjson2.JSONArray; import com.alibaba.fastjson2.JSONObject; -import com.alibaba.fastjson2.util.DateUtils; import com.yutou.biliapi.bean.live.*; import com.yutou.biliapi.bean.live.database.*; import com.yutou.biliapi.bean.websocket.live.*; -import com.yutou.bilibili.Tools.DateFormatUtils; +import com.yutou.bilibili.datas.web.LiveWebDanmuBean; import com.yutou.common.databases.AbsDatabasesBean; import com.yutou.common.databases.SQLiteManager; import com.yutou.common.okhttp.HttpDownloadUtils; import com.yutou.common.utils.Log; -import lombok.Getter; -import org.apache.poi.ss.usermodel.DataFormat; import java.io.File; +import java.sql.PreparedStatement; import java.sql.ResultSet; import java.sql.SQLException; +import java.util.ArrayList; import java.util.Date; import java.util.List; -import static com.alibaba.fastjson2.util.DateUtils.DateTimeFormatPattern.DATE_FORMAT_10_DASH; - public class BiliLiveDatabase extends SQLiteManager { LiveRoomConfig config; String fileName; @@ -125,7 +122,7 @@ public class BiliLiveDatabase extends SQLiteManager { "SUM(`gift_num`) AS `total_gift_num`," + "CASE " + "WHEN `coin_type` = 'silver' THEN 0 " + - "ELSE SUM(`price` / 100 * `gift_num`) " + + "ELSE SUM(`price` / 1000 * `gift_num`) " + "END AS `total_price`" + "FROM " + "`gift` " + @@ -144,10 +141,10 @@ public class BiliLiveDatabase extends SQLiteManager { "filtered_gifts " + "GROUP BY " + "`gift_name`, `icon`;"; - String guardSql = "SELECT `gift_name`, SUM(`num`) AS `total_num`,SUM(`price` /100 *`num`) as `total_price`" + + String guardSql = "SELECT `gift_name`, SUM(`num`) AS `total_num`,SUM(`price` / 1000 *`num`) as `total_price`" + "FROM `guardBuy` where `sql_time` >= '" + startTimeLong + "' and `sql_time` <= '" + endTimeLong + "'" + "GROUP BY `gift_name`;"; - String superChatSql = "SELECT SUM(`price`*100) as `total_price`, count(`price`) as `total_count`" + + String superChatSql = "SELECT SUM(`price`/ 1000) as `total_price`, count(`price`) as `total_count`" + "FROM `superChat` where `sql_time` >= '" + startTimeLong + "' and `sql_time` <= '" + endTimeLong + "';"; JSONObject json = new JSONObject(); JSONArray giftInfo = get(giftSql); @@ -207,30 +204,50 @@ public class BiliLiveDatabase extends SQLiteManager { return super.get(tableName, where, clazz); } - public List getOfTime(Long startTime, Long endTime, int page,int pageSize, Class clazz) { - String tableName = null; - StringBuilder sb = new StringBuilder(); - String where = null; - if (startTime != null) { - sb.append(" `sql_time` >= ").append("\"").append(startTime).append("\""); - } - if (endTime != null) { - if (!sb.isEmpty()) { - sb.append(" and "); + + public List getDanmu(Long startTime, Long endTime, int page, int pageSize) { + List list=new ArrayList<>(); + + String query = "SELECT danmu AS text, fontSize, color, (sql_time - ?) AS `time`, " + + "CASE WHEN model < 4 THEN 'scroll' " + + " WHEN model = 4 THEN 'bottom' " + + " WHEN model = 5 THEN 'top' " + + "END AS model " + + "FROM danmu " + + "WHERE sql_time BETWEEN ? AND ? " + + "ORDER BY sql_time " + + "LIMIT ?, ?"; + try (PreparedStatement pstmt = conn.prepareStatement(query)) { + + // 设置参数 + pstmt.setLong(1, startTime); + pstmt.setLong(2, startTime); + pstmt.setLong(3, endTime); + pstmt.setInt(4, (page - 1) * pageSize); // 计算偏移量 + pstmt.setInt(5, pageSize); + + pstmt.closeOnCompletion(); + ResultSet rs = pstmt.executeQuery(); + + while (rs.next()) { + String text = rs.getString("text"); + int fontSize = rs.getInt("fontSize"); + String color = rs.getString("color"); + long time = rs.getLong("time"); + String model = rs.getString("model"); + LiveWebDanmuBean.Danmu danmu = new LiveWebDanmuBean.Danmu(); + danmu.setText(text); + danmu.setFontSize(fontSize); + danmu.setColor(color); + danmu.setTime(time); + danmu.setBarrageType(model); + list.add(danmu); } - sb.append(" `sql_time` <= ").append("\"").append(endTime).append("\""); + rs.close(); + } catch (SQLException e) { + Log.e(e); } - sb.append("ORDER BY `sql_time` LIMIT ").append(page).append("*").append(pageSize).append(",").append(pageSize); - if (!sb.isEmpty()) { - where = sb.toString(); - } - for (AbsDatabasesBean bean : getDataBean()) { - if (bean.getClass() == clazz) { - tableName = bean.getTableName(); - break; - } - } - return super.get(tableName, where, clazz); + return list; } @Override diff --git a/src/main/java/com/yutou/bilibili/datas/web/LiveVideoDanmu.java b/src/main/java/com/yutou/bilibili/datas/web/LiveVideoDanmu.java deleted file mode 100644 index e4568b7..0000000 --- a/src/main/java/com/yutou/bilibili/datas/web/LiveVideoDanmu.java +++ /dev/null @@ -1,67 +0,0 @@ -package com.yutou.bilibili.datas.web; - -import com.yutou.biliapi.bean.live.database.LiveSuperChatDatabaseBean; -import lombok.Data; - -import java.util.ArrayList; -import java.util.List; - -@Data -public class LiveVideoDanmu { - - List danmu = new ArrayList<>(); - List superChat = new ArrayList<>(); - boolean isNextDanmu = false; - long danmuCount = 0; - - @Data - public static class Addition { - private int grade = 2; - } - - @Data - public static class Danmu { - public static final String DANMU_TYPE_SCROLL = "scroll"; - public static final String DANMU_TYPE_TOP = "top"; - public static final String DANMU_TYPE_BOTTOM = "bottom"; - private String id; - private String barrageType = DANMU_TYPE_SCROLL; - private long time; - private String text; - private int fontSize = 25; - private int lineHeight = 1; - private String color = "#ffffff"; - private Addition addition = new Addition(); - } - - @Data - public static class SuperChat { - private int id; - private long price; - private String uid; - private long start_time; - private long duration; - private String message; - private String message_trans; - private String message_font_color; - private String backgroundBottomColor; - private String userName; - private String userNameColor; - private String userAvatar; - - public SuperChat(long videoTime, LiveSuperChatDatabaseBean bean) { - this.id = bean.getId(); - this.price = bean.getPrice(); - this.uid = bean.getUid(); - this.start_time = (bean.getSql_time().getTime() - videoTime) / 1000; - this.duration = bean.getEnd_time() - bean.getStart_time(); - this.message = bean.getMessage(); - this.message_trans = bean.getMessage_trans(); - this.message_font_color = bean.getMessage_font_color(); - this.backgroundBottomColor = bean.getBackgroundBottomColor(); - this.userName = bean.getUserName(); - this.userAvatar = bean.getUserAvatar(); - this.userNameColor = bean.getUserNameColor(); - } - } -} diff --git a/src/main/java/com/yutou/bilibili/datas/web/LiveWebDanmuBean.java b/src/main/java/com/yutou/bilibili/datas/web/LiveWebDanmuBean.java new file mode 100644 index 0000000..9b64cfa --- /dev/null +++ b/src/main/java/com/yutou/bilibili/datas/web/LiveWebDanmuBean.java @@ -0,0 +1,36 @@ +package com.yutou.bilibili.datas.web; + +import lombok.Data; + +import java.util.ArrayList; +import java.util.List; + +@Data +public class LiveWebDanmuBean { + + List danmu = new ArrayList<>(); + boolean isNextDanmu = false; + long danmuCount = 0; + + @Data + public static class Addition { + private int grade = 2; + } + + @Data + public static class Danmu { + public static final String DANMU_TYPE_SCROLL = "scroll"; + public static final String DANMU_TYPE_TOP = "top"; + public static final String DANMU_TYPE_BOTTOM = "bottom"; + private String id; + private String barrageType = DANMU_TYPE_SCROLL; + private long time; + private String text; + private int fontSize = 25; + private int lineHeight = 1; + private String color = "#ffffff"; + private Addition addition = new Addition(); + } + + +} diff --git a/src/main/java/com/yutou/bilibili/datas/web/SuperChatGiftBean.java b/src/main/java/com/yutou/bilibili/datas/web/SuperChatGiftBean.java new file mode 100644 index 0000000..75d5fa7 --- /dev/null +++ b/src/main/java/com/yutou/bilibili/datas/web/SuperChatGiftBean.java @@ -0,0 +1,35 @@ +package com.yutou.bilibili.datas.web; + +import com.yutou.biliapi.bean.live.database.LiveSuperChatDatabaseBean; +import lombok.Data; + +@Data +public class SuperChatGiftBean { + private int id; + private long price; + private String uid; + private long start_time; + private long duration; + private String message; + private String message_trans; + private String message_font_color; + private String backgroundBottomColor; + private String userName; + private String userNameColor; + private String userAvatar; + + public SuperChatGiftBean(long videoTime, LiveSuperChatDatabaseBean bean) { + this.id = bean.getId(); + this.price = bean.getPrice(); + this.uid = bean.getUid(); + this.start_time = (bean.getSql_time().getTime() - videoTime) / 1000; + this.duration = bean.getEnd_time() - bean.getStart_time(); + this.message = bean.getMessage(); + this.message_trans = bean.getMessage_trans(); + this.message_font_color = bean.getMessage_font_color(); + this.backgroundBottomColor = bean.getBackgroundBottomColor(); + this.userName = bean.getUserName(); + this.userAvatar = bean.getUserAvatar(); + this.userNameColor = bean.getUserNameColor(); + } +} diff --git a/src/main/java/com/yutou/bilibili/services/LiveDanmuService.java b/src/main/java/com/yutou/bilibili/services/LiveDanmuService.java index 8ac6caa..2b095b4 100644 --- a/src/main/java/com/yutou/bilibili/services/LiveDanmuService.java +++ b/src/main/java/com/yutou/bilibili/services/LiveDanmuService.java @@ -5,15 +5,13 @@ import com.alibaba.fastjson2.JSONObject; import com.yutou.biliapi.bean.live.LiveRoomConfig; import com.yutou.biliapi.bean.live.database.LiveConfigDatabaseBean; import com.yutou.biliapi.bean.live.database.LiveDanmuDatabaseBean; -import com.yutou.biliapi.bean.live.database.LiveSuperChatDatabaseBean; import com.yutou.biliapi.bean.live.database.LiveVideoDatabaseBean; import com.yutou.biliapi.bean.websocket.live.WSData; -import com.yutou.biliapi.databases.BiliLiveConfigDatabase; import com.yutou.biliapi.databases.BiliLiveDatabase; import com.yutou.biliapi.net.WebSocketServer; import com.yutou.bilibili.Tools.AssTools; import com.yutou.bilibili.Tools.Tools; -import com.yutou.bilibili.datas.web.LiveVideoDanmu; +import com.yutou.bilibili.datas.web.LiveWebDanmuBean; import com.yutou.common.utils.Log; import jakarta.annotation.Resource; import org.jetbrains.annotations.NotNull; @@ -95,39 +93,28 @@ public class LiveDanmuService { return String.format("%d小时%d分钟%d秒", hours, minutes, finalRemainingSeconds); } - public LiveVideoDanmu getDanmu(String roomId, String videoId, int page) { - LiveVideoDanmu danmus = new LiveVideoDanmu(); + public LiveWebDanmuBean getDanmu(String roomId, String videoId, int page) { + LiveWebDanmuBean danmus = new LiveWebDanmuBean(); BiliLiveDatabase liveDatabase = liveDatabasesService.getLiveDatabase(roomId); try { LiveVideoDatabaseBean videoBean = liveDatabase.getVideo(videoId); if (videoBean == null) { - return new LiveVideoDanmu(); + return new LiveWebDanmuBean(); } long startTime = videoBean.getStartTime().getTime(); long endTime = videoBean.getStopTime() == null ? System.currentTimeMillis() : videoBean.getStopTime().getTime(); - List superChatList = liveDatabase.getOfTime(startTime, endTime, LiveSuperChatDatabaseBean.class); long count = liveDatabase.getCount(new LiveDanmuDatabaseBean().getTableName()); - int pageSize = 10000; + int pageSize = 3000; int pageCount = (int) Math.ceil((double) count / pageSize); - List danmuList = liveDatabase.getOfTime(startTime, endTime, page, pageSize, LiveDanmuDatabaseBean.class) - .stream() - .map(item -> createDanmu(item, startTime)) - .filter(item -> item.getTime() >= 0) - .toList(); + List danmuList = liveDatabase.getDanmu(startTime, endTime, page, pageSize); danmus.getDanmu().addAll(danmuList); danmus.setDanmuCount(count); if (page < pageCount) { danmus.setNextDanmu(true); } - - for (LiveSuperChatDatabaseBean bean : superChatList) { - LiveVideoDanmu.SuperChat superChat = new LiveVideoDanmu.SuperChat(startTime, bean); - danmus.getSuperChat().add(superChat); - } - } catch (Exception e) { Log.e(e); } @@ -136,19 +123,19 @@ public class LiveDanmuService { } @NotNull - private static LiveVideoDanmu.Danmu createDanmu(LiveDanmuDatabaseBean bean, long startTime) { - LiveVideoDanmu.Danmu danmu = new LiveVideoDanmu.Danmu(); + private static LiveWebDanmuBean.Danmu createDanmu(LiveDanmuDatabaseBean bean, long startTime) { + LiveWebDanmuBean.Danmu danmu = new LiveWebDanmuBean.Danmu(); danmu.setId(bean.getId() + ""); danmu.setText(bean.getDanmu()); danmu.setColor("#" + bean.getFontColor()); danmu.setFontSize(bean.getFontSize()); danmu.setTime(bean.getTime().getTime() - startTime); if (bean.getModel() < 4) { - danmu.setBarrageType(LiveVideoDanmu.Danmu.DANMU_TYPE_SCROLL); + danmu.setBarrageType(LiveWebDanmuBean.Danmu.DANMU_TYPE_SCROLL); } else if (bean.getModel() == 4) { - danmu.setBarrageType(LiveVideoDanmu.Danmu.DANMU_TYPE_BOTTOM); + danmu.setBarrageType(LiveWebDanmuBean.Danmu.DANMU_TYPE_BOTTOM); } else if (bean.getModel() == 5) { - danmu.setBarrageType(LiveVideoDanmu.Danmu.DANMU_TYPE_TOP); + danmu.setBarrageType(LiveWebDanmuBean.Danmu.DANMU_TYPE_TOP); } return danmu; } diff --git a/src/main/java/com/yutou/bilibili/services/LiveDatabasesService.java b/src/main/java/com/yutou/bilibili/services/LiveDatabasesService.java index 8bb2ced..e5f3116 100644 --- a/src/main/java/com/yutou/bilibili/services/LiveDatabasesService.java +++ b/src/main/java/com/yutou/bilibili/services/LiveDatabasesService.java @@ -1,5 +1,7 @@ package com.yutou.bilibili.services; +import com.google.common.cache.Cache; +import com.google.common.cache.CacheBuilder; import com.yutou.biliapi.bean.live.LiveRoomConfig; import com.yutou.biliapi.bean.live.database.LiveConfigDatabaseBean; import com.yutou.biliapi.databases.BiliLiveConfigDatabase; @@ -10,30 +12,49 @@ import org.springframework.util.StringUtils; import java.util.HashMap; import java.util.Map; +import java.util.concurrent.ExecutionException; +import java.util.concurrent.TimeUnit; +@Getter @Service public class LiveDatabasesService { - private final Map liveDatabases=new HashMap<>(); - @Getter + private static final Cache liveDatabases = CacheBuilder.newBuilder() + .maximumSize(1000) + .expireAfterAccess(30, TimeUnit.MINUTES) + .removalListener(it -> { + if (it.wasEvicted()) { + if (it.getValue() != null) { + ((BiliLiveDatabase) it.getValue()).close(); + } + } + }) + .build(); private final BiliLiveConfigDatabase configDatabase; private LiveDatabasesService() { - configDatabase=new BiliLiveConfigDatabase(); - } - public BiliLiveDatabase getLiveDatabase(String roomId) { - if(liveDatabases.containsKey(roomId)) { - return liveDatabases.get(roomId); - } - BiliLiveDatabase liveDatabase = new BiliLiveDatabase(buildConfig(roomId)); - liveDatabases.put(roomId, liveDatabase); - return liveDatabase; - } - public void closeAll(){ - liveDatabases.forEach((k,v)->v.close()); - configDatabase.close(); + configDatabase = new BiliLiveConfigDatabase(); } - public LiveRoomConfig buildConfig(String roomId){ + public BiliLiveDatabase getLiveDatabase(String roomId) { + try { + return liveDatabases.get(roomId, () -> new BiliLiveDatabase(buildConfig(roomId))); + } catch (ExecutionException e) { + throw new RuntimeException(e); + } + } + + public void closeAll() { + try { + for (BiliLiveDatabase db : liveDatabases.asMap().values()) { + db.close(); + } + } finally { + liveDatabases.invalidateAll(); + configDatabase.close(); + } + } + + public LiveRoomConfig buildConfig(String roomId) { LiveConfigDatabaseBean bean = configDatabase.getConfig(roomId); LiveRoomConfig config = new LiveRoomConfig(); config.setLoginUid(bean.getRecordUid()); diff --git a/src/main/java/com/yutou/bilibili/services/LiveService.java b/src/main/java/com/yutou/bilibili/services/LiveService.java index 43632a3..c946214 100644 --- a/src/main/java/com/yutou/bilibili/services/LiveService.java +++ b/src/main/java/com/yutou/bilibili/services/LiveService.java @@ -4,15 +4,13 @@ import com.alibaba.fastjson2.JSONArray; import com.alibaba.fastjson2.JSONObject; import com.yutou.biliapi.api.LiveApi; import com.yutou.biliapi.bean.live.LiveAnchorInfo; -import com.yutou.biliapi.bean.live.database.LiveConfigDatabaseBean; -import com.yutou.biliapi.bean.live.database.LiveGiftDatabaseBean; -import com.yutou.biliapi.bean.live.database.LiveGuardBuyBean; -import com.yutou.biliapi.bean.live.database.LiveVideoDatabaseBean; +import com.yutou.biliapi.bean.live.database.*; import com.yutou.biliapi.databases.BiliLiveDatabase; import com.yutou.biliapi.net.BiliLiveNetApiManager; import com.yutou.bilibili.Tools.DateFormatUtils; import com.yutou.bilibili.datas.web.LiveData; -import com.yutou.common.databases.AbsDatabasesBean; +import com.yutou.bilibili.datas.web.LiveWebDanmuBean; +import com.yutou.bilibili.datas.web.SuperChatGiftBean; import com.yutou.common.okhttp.HttpLoggingInterceptor; import jakarta.annotation.Resource; import org.springframework.stereotype.Service; @@ -111,7 +109,11 @@ public class LiveService { } long startTime = videoBean.getStartTime().getTime(); long endTime = videoBean.getStopTime() == null ? System.currentTimeMillis() : videoBean.getStopTime().getTime(); - return database.getGiftInfo(startTime, endTime); + List superChatList = database.getOfTime(startTime, endTime, LiveSuperChatDatabaseBean.class) + .stream().map(it-> new SuperChatGiftBean(startTime,it)).toList(); + JSONObject giftInfo = database.getGiftInfo(startTime, endTime); + giftInfo.put("superChat",superChatList); + return giftInfo; } public JSONArray getGiftItemInfo(String roomId, String videoId, String giftName) { diff --git a/src/main/java/com/yutou/common/databases/SQLiteManager.java b/src/main/java/com/yutou/common/databases/SQLiteManager.java index 1bdfc42..ee0822a 100644 --- a/src/main/java/com/yutou/common/databases/SQLiteManager.java +++ b/src/main/java/com/yutou/common/databases/SQLiteManager.java @@ -105,132 +105,121 @@ public abstract class SQLiteManager { protected void add(T t) { StringBuilder sb = new StringBuilder(); - PreparedStatement statement = null; - try { - - StringBuilder value = new StringBuilder(); - sb.append("INSERT INTO `").append(t.getTableName()).append("` "); - sb.append("("); - value.append("("); + try (PreparedStatement statement = getConnection().prepareStatement(buildInsertSql(t))) { JSONObject json = t.toJson(); Set keySet = json.keySet(); - for (String key : keySet) { - if ("id".equals(key)) { - continue; - } - sb.append("`").append(key).append("`,"); - //value.append("'").append(json.get(key)).append("',"); - value.append("?").append(","); - } - sb.deleteCharAt(sb.length() - 1); - value.deleteCharAt(value.length() - 1); - value.append(")"); - sb.append(") VALUES "); - sb.append(value); - statement = getConnection().prepareStatement(sb.toString()); int i = 1; for (String key : keySet) { if ("id".equals(key)) { continue; } - if ("sql_time".equals(key)) { - statement.setLong(i++, json.getDate(key).getTime()); + Object value = json.get(key); + if ("sql_time".equals(key) && value instanceof Date) { + statement.setLong(i++, ((Date) value).getTime()); continue; } - if (json.get(key) instanceof String) { - statement.setString(i++, json.get(key).toString()); - } else if (json.get(key) instanceof Integer) { - statement.setInt(i++, json.getInteger(key)); - } else if (json.get(key) instanceof Long) { - statement.setLong(i++, json.getLong(key)); - } else if (json.get(key) instanceof Boolean) { - statement.setBoolean(i++, json.getBoolean(key)); - } else if (json.get(key) instanceof Date) { - statement.setLong(i++, json.getDate(key).getTime()); + if (value instanceof String) { + statement.setString(i++, (String) value); + } else if (value instanceof Integer) { + statement.setInt(i++, (Integer) value); + } else if (value instanceof Long) { + statement.setLong(i++, (Long) value); + } else if (value instanceof Boolean) { + statement.setBoolean(i++, (Boolean) value); + } else if (value instanceof Date) { + statement.setTimestamp(i++, new Timestamp(((Date) value).getTime())); } else { - statement.setObject(i++, json.get(key)); + statement.setObject(i++, value); } } statement.execute(); } catch (SQLException e) { - Log.e(e, sb); + Log.e(e, sb.toString()); throw new RuntimeException(e); - } finally { - try { - if (statement != null) { - statement.closeOnCompletion(); - } - } catch (SQLException e) { - Log.e(e); - } } } - protected void update(T t) { - PreparedStatement statement = null; - try { - StringBuilder sb = new StringBuilder(); - StringBuilder setPart = new StringBuilder(); + private String buildInsertSql(AbsDatabasesBean bean) { + StringBuilder sb = new StringBuilder(); + StringBuilder value = new StringBuilder(); + sb.append("INSERT INTO `").append(bean.getTableName()).append("` "); + sb.append("("); + value.append("("); + JSONObject json = bean.toJson(); + Set keySet = json.keySet(); + for (String key : keySet) { + if ("id".equals(key)) { + continue; + } + sb.append("`").append(key).append("`,").append(" "); + value.append("?").append(","); + } + sb.deleteCharAt(sb.length() - 1); // 删除最后一个逗号 + value.deleteCharAt(value.length() - 1); // 删除最后一个逗号 + value.append(")"); + sb.append(") VALUES ").append(value); + return sb.toString(); + } + protected void update(T t) { + try (PreparedStatement statement = getConnection().prepareStatement(buildUpdateSql(t))) { JSONObject json = t.toJson(); Set keySet = json.keySet(); - - for (String key : keySet) { - if ("id".equals(key) || "sql_time".equals(key)) { - continue; - } - setPart.append("`").append(key).append("` = ?, "); - } - - setPart.deleteCharAt(setPart.length() - 2); - - sb.append("UPDATE `").append(t.getTableName()).append("` "); - sb.append("SET ").append(setPart); - sb.append(" WHERE `sql_time` = ?"); - - statement = getConnection().prepareStatement(sb.toString()); - int i = 1; for (String key : keySet) { if ("id".equals(key) || "sql_time".equals(key)) { continue; } - - if (json.get(key) instanceof String) { - statement.setString(i++, json.getString(key)); - } else if (json.get(key) instanceof Integer) { - statement.setInt(i++, json.getInteger(key)); - } else if (json.get(key) instanceof Long) { - statement.setLong(i++, json.getLong(key)); - } else if (json.get(key) instanceof Boolean) { - statement.setBoolean(i++, json.getBoolean(key)); - } else if (json.get(key) instanceof Date) { - statement.setLong(i++, json.getDate(key).getTime()); + Object value = json.get(key); + if (value instanceof String) { + statement.setString(i++, (String) value); + } else if (value instanceof Integer) { + statement.setInt(i++, (Integer) value); + } else if (value instanceof Long) { + statement.setLong(i++, (Long) value); + } else if (value instanceof Boolean) { + statement.setBoolean(i++, (Boolean) value); + } else if (value instanceof Date) { + statement.setTimestamp(i++, new Timestamp(((Date) value).getTime())); } else { - statement.setObject(i++, json.get(key)); + statement.setObject(i++, value); } } // 设置 sql_time 的值 - long id = t.getSql_time().getTime(); - statement.setLong(i, id); + long sqlTime = t.getSql_time().getTime(); + statement.setLong(i, sqlTime); statement.executeUpdate(); - } catch (SQLException e) { Log.e(e); throw new RuntimeException(e); - } finally { - try { - if (statement != null) { - statement.closeOnCompletion(); - } - } catch (SQLException e) { - Log.e(e); - } } } + private String buildUpdateSql(AbsDatabasesBean bean) { + StringBuilder sb = new StringBuilder(); + StringBuilder setPart = new StringBuilder(); + + JSONObject json = bean.toJson(); + Set keySet = json.keySet(); + + for (String key : keySet) { + if ("id".equals(key) || "sql_time".equals(key)) { + continue; + } + setPart.append("`").append(key).append("` = ?, "); + } + + setPart.deleteCharAt(setPart.length() - 2); // 删除最后一个逗号和空格 + + sb.append("UPDATE `").append(bean.getTableName()).append("` "); + sb.append("SET ").append(setPart); + sb.append(" WHERE `sql_time` = ?"); + + return sb.toString(); + } + protected JSONArray getJSONArray(String table) { return getJSONArray(table, null); }