update 调整数据库

update superchat信息
This commit is contained in:
zlzw 2024-11-30 14:43:08 +08:00
parent 1bfca68514
commit dad4bcff15
9 changed files with 257 additions and 237 deletions

View File

@ -242,21 +242,22 @@
var chartView = null; var chartView = null;
function initChart(roomId, videoId) { function initChart(roomId, videoId) {
getVideoGiftInfo(roomId, videoId) getVideoGiftInfo(roomId, videoId)
.then(data => { .then(json => {
if (data.status != 100) { if (json.status != 100) {
return return
} }
var lables = []; var lables = [];
var values = []; var values = [];
data.data.guardInfo.forEach(item => { json.data.guardInfo.forEach(item => {
lables.push(item.gift_name + "\n" + item.total_price + "¥") lables.push(item.gift_name + "\n" + item.total_price + "¥")
values.push(item.total_num) values.push(item.total_num)
}); });
data.data.giftInfo.forEach(item => { json.data.giftInfo.forEach(item => {
lables.push(item.gift_name + "\n" + item.total_price + "¥") lables.push(item.gift_name + "\n" + item.total_price + "¥")
values.push(item.total_gift_num) 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) getDanmu(getParam('roomId'), videoId, page)
.then(data => { .then(data => {
if (page === 0) { if (page === 0) {
initSC(data.data.superChat)
$('#danmuSize').get(0).innerHTML = data.data.danmuCount; $('#danmuSize').get(0).innerHTML = data.data.danmuCount;
barrageRenderer.setBarrages(data.data.danmu) barrageRenderer.setBarrages(data.data.danmu)
} else { } else {

View File

@ -2,26 +2,23 @@ package com.yutou.biliapi.databases;
import com.alibaba.fastjson2.JSONArray; import com.alibaba.fastjson2.JSONArray;
import com.alibaba.fastjson2.JSONObject; import com.alibaba.fastjson2.JSONObject;
import com.alibaba.fastjson2.util.DateUtils;
import com.yutou.biliapi.bean.live.*; import com.yutou.biliapi.bean.live.*;
import com.yutou.biliapi.bean.live.database.*; import com.yutou.biliapi.bean.live.database.*;
import com.yutou.biliapi.bean.websocket.live.*; 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.AbsDatabasesBean;
import com.yutou.common.databases.SQLiteManager; import com.yutou.common.databases.SQLiteManager;
import com.yutou.common.okhttp.HttpDownloadUtils; import com.yutou.common.okhttp.HttpDownloadUtils;
import com.yutou.common.utils.Log; import com.yutou.common.utils.Log;
import lombok.Getter;
import org.apache.poi.ss.usermodel.DataFormat;
import java.io.File; import java.io.File;
import java.sql.PreparedStatement;
import java.sql.ResultSet; import java.sql.ResultSet;
import java.sql.SQLException; import java.sql.SQLException;
import java.util.ArrayList;
import java.util.Date; import java.util.Date;
import java.util.List; import java.util.List;
import static com.alibaba.fastjson2.util.DateUtils.DateTimeFormatPattern.DATE_FORMAT_10_DASH;
public class BiliLiveDatabase extends SQLiteManager { public class BiliLiveDatabase extends SQLiteManager {
LiveRoomConfig config; LiveRoomConfig config;
String fileName; String fileName;
@ -125,7 +122,7 @@ public class BiliLiveDatabase extends SQLiteManager {
"SUM(`gift_num`) AS `total_gift_num`," + "SUM(`gift_num`) AS `total_gift_num`," +
"CASE " + "CASE " +
"WHEN `coin_type` = 'silver' THEN 0 " + "WHEN `coin_type` = 'silver' THEN 0 " +
"ELSE SUM(`price` / 100 * `gift_num`) " + "ELSE SUM(`price` / 1000 * `gift_num`) " +
"END AS `total_price`" + "END AS `total_price`" +
"FROM " + "FROM " +
"`gift` " + "`gift` " +
@ -144,10 +141,10 @@ public class BiliLiveDatabase extends SQLiteManager {
"filtered_gifts " + "filtered_gifts " +
"GROUP BY " + "GROUP BY " +
"`gift_name`, `icon`;"; "`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 + "'" + "FROM `guardBuy` where `sql_time` >= '" + startTimeLong + "' and `sql_time` <= '" + endTimeLong + "'" +
"GROUP BY `gift_name`;"; "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 + "';"; "FROM `superChat` where `sql_time` >= '" + startTimeLong + "' and `sql_time` <= '" + endTimeLong + "';";
JSONObject json = new JSONObject(); JSONObject json = new JSONObject();
JSONArray giftInfo = get(giftSql); JSONArray giftInfo = get(giftSql);
@ -207,30 +204,50 @@ public class BiliLiveDatabase extends SQLiteManager {
return super.get(tableName, where, clazz); return super.get(tableName, where, clazz);
} }
public <T extends AbsDatabasesBean> List<T> getOfTime(Long startTime, Long endTime, int page,int pageSize, Class<T> clazz) {
String tableName = null; public List<LiveWebDanmuBean.Danmu> getDanmu(Long startTime, Long endTime, int page, int pageSize) {
StringBuilder sb = new StringBuilder(); List<LiveWebDanmuBean.Danmu> list=new ArrayList<>();
String where = null;
if (startTime != null) { String query = "SELECT danmu AS text, fontSize, color, (sql_time - ?) AS `time`, "
sb.append(" `sql_time` >= ").append("\"").append(startTime).append("\""); + "CASE WHEN model < 4 THEN 'scroll' "
} + " WHEN model = 4 THEN 'bottom' "
if (endTime != null) { + " WHEN model = 5 THEN 'top' "
if (!sb.isEmpty()) { + "END AS model "
sb.append(" and "); + "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); return list;
if (!sb.isEmpty()) {
where = sb.toString();
}
for (AbsDatabasesBean bean : getDataBean()) {
if (bean.getClass() == clazz) {
tableName = bean.getTableName();
break;
}
}
return super.get(tableName, where, clazz);
} }
@Override @Override

View File

@ -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> danmu = new ArrayList<>();
List<SuperChat> 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();
}
}
}

View File

@ -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> 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();
}
}

View File

@ -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();
}
}

View File

@ -5,15 +5,13 @@ import com.alibaba.fastjson2.JSONObject;
import com.yutou.biliapi.bean.live.LiveRoomConfig; import com.yutou.biliapi.bean.live.LiveRoomConfig;
import com.yutou.biliapi.bean.live.database.LiveConfigDatabaseBean; import com.yutou.biliapi.bean.live.database.LiveConfigDatabaseBean;
import com.yutou.biliapi.bean.live.database.LiveDanmuDatabaseBean; 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.live.database.LiveVideoDatabaseBean;
import com.yutou.biliapi.bean.websocket.live.WSData; import com.yutou.biliapi.bean.websocket.live.WSData;
import com.yutou.biliapi.databases.BiliLiveConfigDatabase;
import com.yutou.biliapi.databases.BiliLiveDatabase; import com.yutou.biliapi.databases.BiliLiveDatabase;
import com.yutou.biliapi.net.WebSocketServer; import com.yutou.biliapi.net.WebSocketServer;
import com.yutou.bilibili.Tools.AssTools; import com.yutou.bilibili.Tools.AssTools;
import com.yutou.bilibili.Tools.Tools; 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 com.yutou.common.utils.Log;
import jakarta.annotation.Resource; import jakarta.annotation.Resource;
import org.jetbrains.annotations.NotNull; import org.jetbrains.annotations.NotNull;
@ -95,39 +93,28 @@ public class LiveDanmuService {
return String.format("%d小时%d分钟%d秒", hours, minutes, finalRemainingSeconds); return String.format("%d小时%d分钟%d秒", hours, minutes, finalRemainingSeconds);
} }
public LiveVideoDanmu getDanmu(String roomId, String videoId, int page) { public LiveWebDanmuBean getDanmu(String roomId, String videoId, int page) {
LiveVideoDanmu danmus = new LiveVideoDanmu(); LiveWebDanmuBean danmus = new LiveWebDanmuBean();
BiliLiveDatabase liveDatabase = liveDatabasesService.getLiveDatabase(roomId); BiliLiveDatabase liveDatabase = liveDatabasesService.getLiveDatabase(roomId);
try { try {
LiveVideoDatabaseBean videoBean = liveDatabase.getVideo(videoId); LiveVideoDatabaseBean videoBean = liveDatabase.getVideo(videoId);
if (videoBean == null) { if (videoBean == null) {
return new LiveVideoDanmu(); return new LiveWebDanmuBean();
} }
long startTime = videoBean.getStartTime().getTime(); long startTime = videoBean.getStartTime().getTime();
long endTime = videoBean.getStopTime() == null ? System.currentTimeMillis() : videoBean.getStopTime().getTime(); long endTime = videoBean.getStopTime() == null ? System.currentTimeMillis() : videoBean.getStopTime().getTime();
List<LiveSuperChatDatabaseBean> superChatList = liveDatabase.getOfTime(startTime, endTime, LiveSuperChatDatabaseBean.class);
long count = liveDatabase.getCount(new LiveDanmuDatabaseBean().getTableName()); long count = liveDatabase.getCount(new LiveDanmuDatabaseBean().getTableName());
int pageSize = 10000; int pageSize = 3000;
int pageCount = (int) Math.ceil((double) count / pageSize); int pageCount = (int) Math.ceil((double) count / pageSize);
List<LiveVideoDanmu.Danmu> danmuList = liveDatabase.getOfTime(startTime, endTime, page, pageSize, LiveDanmuDatabaseBean.class) List<LiveWebDanmuBean.Danmu> danmuList = liveDatabase.getDanmu(startTime, endTime, page, pageSize);
.stream()
.map(item -> createDanmu(item, startTime))
.filter(item -> item.getTime() >= 0)
.toList();
danmus.getDanmu().addAll(danmuList); danmus.getDanmu().addAll(danmuList);
danmus.setDanmuCount(count); danmus.setDanmuCount(count);
if (page < pageCount) { if (page < pageCount) {
danmus.setNextDanmu(true); danmus.setNextDanmu(true);
} }
for (LiveSuperChatDatabaseBean bean : superChatList) {
LiveVideoDanmu.SuperChat superChat = new LiveVideoDanmu.SuperChat(startTime, bean);
danmus.getSuperChat().add(superChat);
}
} catch (Exception e) { } catch (Exception e) {
Log.e(e); Log.e(e);
} }
@ -136,19 +123,19 @@ public class LiveDanmuService {
} }
@NotNull @NotNull
private static LiveVideoDanmu.Danmu createDanmu(LiveDanmuDatabaseBean bean, long startTime) { private static LiveWebDanmuBean.Danmu createDanmu(LiveDanmuDatabaseBean bean, long startTime) {
LiveVideoDanmu.Danmu danmu = new LiveVideoDanmu.Danmu(); LiveWebDanmuBean.Danmu danmu = new LiveWebDanmuBean.Danmu();
danmu.setId(bean.getId() + ""); danmu.setId(bean.getId() + "");
danmu.setText(bean.getDanmu()); danmu.setText(bean.getDanmu());
danmu.setColor("#" + bean.getFontColor()); danmu.setColor("#" + bean.getFontColor());
danmu.setFontSize(bean.getFontSize()); danmu.setFontSize(bean.getFontSize());
danmu.setTime(bean.getTime().getTime() - startTime); danmu.setTime(bean.getTime().getTime() - startTime);
if (bean.getModel() < 4) { if (bean.getModel() < 4) {
danmu.setBarrageType(LiveVideoDanmu.Danmu.DANMU_TYPE_SCROLL); danmu.setBarrageType(LiveWebDanmuBean.Danmu.DANMU_TYPE_SCROLL);
} else if (bean.getModel() == 4) { } else if (bean.getModel() == 4) {
danmu.setBarrageType(LiveVideoDanmu.Danmu.DANMU_TYPE_BOTTOM); danmu.setBarrageType(LiveWebDanmuBean.Danmu.DANMU_TYPE_BOTTOM);
} else if (bean.getModel() == 5) { } else if (bean.getModel() == 5) {
danmu.setBarrageType(LiveVideoDanmu.Danmu.DANMU_TYPE_TOP); danmu.setBarrageType(LiveWebDanmuBean.Danmu.DANMU_TYPE_TOP);
} }
return danmu; return danmu;
} }

View File

@ -1,5 +1,7 @@
package com.yutou.bilibili.services; 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.LiveRoomConfig;
import com.yutou.biliapi.bean.live.database.LiveConfigDatabaseBean; import com.yutou.biliapi.bean.live.database.LiveConfigDatabaseBean;
import com.yutou.biliapi.databases.BiliLiveConfigDatabase; import com.yutou.biliapi.databases.BiliLiveConfigDatabase;
@ -10,30 +12,49 @@ import org.springframework.util.StringUtils;
import java.util.HashMap; import java.util.HashMap;
import java.util.Map; import java.util.Map;
import java.util.concurrent.ExecutionException;
import java.util.concurrent.TimeUnit;
@Getter
@Service @Service
public class LiveDatabasesService { public class LiveDatabasesService {
private final Map<String, BiliLiveDatabase> liveDatabases=new HashMap<>(); private static final Cache<String, BiliLiveDatabase> liveDatabases = CacheBuilder.newBuilder()
@Getter .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 final BiliLiveConfigDatabase configDatabase;
private LiveDatabasesService() { private LiveDatabasesService() {
configDatabase=new BiliLiveConfigDatabase(); 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();
} }
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); LiveConfigDatabaseBean bean = configDatabase.getConfig(roomId);
LiveRoomConfig config = new LiveRoomConfig(); LiveRoomConfig config = new LiveRoomConfig();
config.setLoginUid(bean.getRecordUid()); config.setLoginUid(bean.getRecordUid());

View File

@ -4,15 +4,13 @@ import com.alibaba.fastjson2.JSONArray;
import com.alibaba.fastjson2.JSONObject; import com.alibaba.fastjson2.JSONObject;
import com.yutou.biliapi.api.LiveApi; import com.yutou.biliapi.api.LiveApi;
import com.yutou.biliapi.bean.live.LiveAnchorInfo; import com.yutou.biliapi.bean.live.LiveAnchorInfo;
import com.yutou.biliapi.bean.live.database.LiveConfigDatabaseBean; import com.yutou.biliapi.bean.live.database.*;
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.databases.BiliLiveDatabase; import com.yutou.biliapi.databases.BiliLiveDatabase;
import com.yutou.biliapi.net.BiliLiveNetApiManager; import com.yutou.biliapi.net.BiliLiveNetApiManager;
import com.yutou.bilibili.Tools.DateFormatUtils; import com.yutou.bilibili.Tools.DateFormatUtils;
import com.yutou.bilibili.datas.web.LiveData; 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 com.yutou.common.okhttp.HttpLoggingInterceptor;
import jakarta.annotation.Resource; import jakarta.annotation.Resource;
import org.springframework.stereotype.Service; import org.springframework.stereotype.Service;
@ -111,7 +109,11 @@ public class LiveService {
} }
long startTime = videoBean.getStartTime().getTime(); long startTime = videoBean.getStartTime().getTime();
long endTime = videoBean.getStopTime() == null ? System.currentTimeMillis() : videoBean.getStopTime().getTime(); long endTime = videoBean.getStopTime() == null ? System.currentTimeMillis() : videoBean.getStopTime().getTime();
return database.getGiftInfo(startTime, endTime); List<SuperChatGiftBean> 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) { public JSONArray getGiftItemInfo(String roomId, String videoId, String giftName) {

View File

@ -105,132 +105,121 @@ public abstract class SQLiteManager {
protected <T extends AbsDatabasesBean> void add(T t) { protected <T extends AbsDatabasesBean> void add(T t) {
StringBuilder sb = new StringBuilder(); StringBuilder sb = new StringBuilder();
PreparedStatement statement = null; try (PreparedStatement statement = getConnection().prepareStatement(buildInsertSql(t))) {
try {
StringBuilder value = new StringBuilder();
sb.append("INSERT INTO `").append(t.getTableName()).append("` ");
sb.append("(");
value.append("(");
JSONObject json = t.toJson(); JSONObject json = t.toJson();
Set<String> keySet = json.keySet(); Set<String> 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; int i = 1;
for (String key : keySet) { for (String key : keySet) {
if ("id".equals(key)) { if ("id".equals(key)) {
continue; continue;
} }
if ("sql_time".equals(key)) { Object value = json.get(key);
statement.setLong(i++, json.getDate(key).getTime()); if ("sql_time".equals(key) && value instanceof Date) {
statement.setLong(i++, ((Date) value).getTime());
continue; continue;
} }
if (json.get(key) instanceof String) { if (value instanceof String) {
statement.setString(i++, json.get(key).toString()); statement.setString(i++, (String) value);
} else if (json.get(key) instanceof Integer) { } else if (value instanceof Integer) {
statement.setInt(i++, json.getInteger(key)); statement.setInt(i++, (Integer) value);
} else if (json.get(key) instanceof Long) { } else if (value instanceof Long) {
statement.setLong(i++, json.getLong(key)); statement.setLong(i++, (Long) value);
} else if (json.get(key) instanceof Boolean) { } else if (value instanceof Boolean) {
statement.setBoolean(i++, json.getBoolean(key)); statement.setBoolean(i++, (Boolean) value);
} else if (json.get(key) instanceof Date) { } else if (value instanceof Date) {
statement.setLong(i++, json.getDate(key).getTime()); statement.setTimestamp(i++, new Timestamp(((Date) value).getTime()));
} else { } else {
statement.setObject(i++, json.get(key)); statement.setObject(i++, value);
} }
} }
statement.execute(); statement.execute();
} catch (SQLException e) { } catch (SQLException e) {
Log.e(e, sb); Log.e(e, sb.toString());
throw new RuntimeException(e); throw new RuntimeException(e);
} finally {
try {
if (statement != null) {
statement.closeOnCompletion();
}
} catch (SQLException e) {
Log.e(e);
}
} }
} }
protected <T extends AbsDatabasesBean> void update(T t) { private String buildInsertSql(AbsDatabasesBean bean) {
PreparedStatement statement = null; StringBuilder sb = new StringBuilder();
try { StringBuilder value = new StringBuilder();
StringBuilder sb = new StringBuilder(); sb.append("INSERT INTO `").append(bean.getTableName()).append("` ");
StringBuilder setPart = new StringBuilder(); sb.append("(");
value.append("(");
JSONObject json = bean.toJson();
Set<String> 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 <T extends AbsDatabasesBean> void update(T t) {
try (PreparedStatement statement = getConnection().prepareStatement(buildUpdateSql(t))) {
JSONObject json = t.toJson(); JSONObject json = t.toJson();
Set<String> keySet = json.keySet(); Set<String> 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; int i = 1;
for (String key : keySet) { for (String key : keySet) {
if ("id".equals(key) || "sql_time".equals(key)) { if ("id".equals(key) || "sql_time".equals(key)) {
continue; continue;
} }
Object value = json.get(key);
if (json.get(key) instanceof String) { if (value instanceof String) {
statement.setString(i++, json.getString(key)); statement.setString(i++, (String) value);
} else if (json.get(key) instanceof Integer) { } else if (value instanceof Integer) {
statement.setInt(i++, json.getInteger(key)); statement.setInt(i++, (Integer) value);
} else if (json.get(key) instanceof Long) { } else if (value instanceof Long) {
statement.setLong(i++, json.getLong(key)); statement.setLong(i++, (Long) value);
} else if (json.get(key) instanceof Boolean) { } else if (value instanceof Boolean) {
statement.setBoolean(i++, json.getBoolean(key)); statement.setBoolean(i++, (Boolean) value);
} else if (json.get(key) instanceof Date) { } else if (value instanceof Date) {
statement.setLong(i++, json.getDate(key).getTime()); statement.setTimestamp(i++, new Timestamp(((Date) value).getTime()));
} else { } else {
statement.setObject(i++, json.get(key)); statement.setObject(i++, value);
} }
} }
// 设置 sql_time 的值 // 设置 sql_time 的值
long id = t.getSql_time().getTime(); long sqlTime = t.getSql_time().getTime();
statement.setLong(i, id); statement.setLong(i, sqlTime);
statement.executeUpdate(); statement.executeUpdate();
} catch (SQLException e) { } catch (SQLException e) {
Log.e(e); Log.e(e);
throw new RuntimeException(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<String> 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) { protected JSONArray getJSONArray(String table) {
return getJSONArray(table, null); return getJSONArray(table, null);
} }