update 调整数据库
update superchat信息
This commit is contained in:
parent
1bfca68514
commit
dad4bcff15
@ -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 {
|
||||
|
@ -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 <T extends AbsDatabasesBean> List<T> getOfTime(Long startTime, Long endTime, int page,int pageSize, Class<T> clazz) {
|
||||
String tableName = null;
|
||||
StringBuilder sb = new StringBuilder();
|
||||
String where = null;
|
||||
if (startTime != null) {
|
||||
sb.append(" `sql_time` >= ").append("\"").append(startTime).append("\"");
|
||||
|
||||
public List<LiveWebDanmuBean.Danmu> getDanmu(Long startTime, Long endTime, int page, int pageSize) {
|
||||
List<LiveWebDanmuBean.Danmu> 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);
|
||||
}
|
||||
if (endTime != null) {
|
||||
if (!sb.isEmpty()) {
|
||||
sb.append(" and ");
|
||||
rs.close();
|
||||
} catch (SQLException e) {
|
||||
Log.e(e);
|
||||
}
|
||||
sb.append(" `sql_time` <= ").append("\"").append(endTime).append("\"");
|
||||
}
|
||||
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
|
||||
|
@ -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();
|
||||
}
|
||||
}
|
||||
}
|
@ -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();
|
||||
}
|
||||
|
||||
|
||||
}
|
@ -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();
|
||||
}
|
||||
}
|
@ -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<LiveSuperChatDatabaseBean> 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<LiveVideoDanmu.Danmu> danmuList = liveDatabase.getOfTime(startTime, endTime, page, pageSize, LiveDanmuDatabaseBean.class)
|
||||
.stream()
|
||||
.map(item -> createDanmu(item, startTime))
|
||||
.filter(item -> item.getTime() >= 0)
|
||||
.toList();
|
||||
List<LiveWebDanmuBean.Danmu> 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;
|
||||
}
|
||||
|
@ -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,28 +12,47 @@ 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<String, BiliLiveDatabase> liveDatabases=new HashMap<>();
|
||||
@Getter
|
||||
private static final Cache<String, BiliLiveDatabase> 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);
|
||||
try {
|
||||
return liveDatabases.get(roomId, () -> new BiliLiveDatabase(buildConfig(roomId)));
|
||||
} catch (ExecutionException e) {
|
||||
throw new RuntimeException(e);
|
||||
}
|
||||
BiliLiveDatabase liveDatabase = new BiliLiveDatabase(buildConfig(roomId));
|
||||
liveDatabases.put(roomId, liveDatabase);
|
||||
return liveDatabase;
|
||||
}
|
||||
|
||||
public void closeAll() {
|
||||
liveDatabases.forEach((k,v)->v.close());
|
||||
try {
|
||||
for (BiliLiveDatabase db : liveDatabases.asMap().values()) {
|
||||
db.close();
|
||||
}
|
||||
} finally {
|
||||
liveDatabases.invalidateAll();
|
||||
configDatabase.close();
|
||||
}
|
||||
}
|
||||
|
||||
public LiveRoomConfig buildConfig(String roomId) {
|
||||
LiveConfigDatabaseBean bean = configDatabase.getConfig(roomId);
|
||||
|
@ -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<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) {
|
||||
|
@ -105,74 +105,103 @@ public abstract class SQLiteManager {
|
||||
|
||||
protected <T extends AbsDatabasesBean> 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<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;
|
||||
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);
|
||||
}
|
||||
}
|
||||
|
||||
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<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) {
|
||||
PreparedStatement statement = null;
|
||||
try {
|
||||
try (PreparedStatement statement = getConnection().prepareStatement(buildUpdateSql(t))) {
|
||||
JSONObject json = t.toJson();
|
||||
Set<String> keySet = json.keySet();
|
||||
int i = 1;
|
||||
for (String key : keySet) {
|
||||
if ("id".equals(key) || "sql_time".equals(key)) {
|
||||
continue;
|
||||
}
|
||||
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++, value);
|
||||
}
|
||||
}
|
||||
|
||||
// 设置 sql_time 的值
|
||||
long sqlTime = t.getSql_time().getTime();
|
||||
statement.setLong(i, sqlTime);
|
||||
|
||||
statement.executeUpdate();
|
||||
} catch (SQLException e) {
|
||||
Log.e(e);
|
||||
throw new RuntimeException(e);
|
||||
}
|
||||
}
|
||||
|
||||
private String buildUpdateSql(AbsDatabasesBean bean) {
|
||||
StringBuilder sb = new StringBuilder();
|
||||
StringBuilder setPart = new StringBuilder();
|
||||
|
||||
JSONObject json = t.toJson();
|
||||
JSONObject json = bean.toJson();
|
||||
Set<String> keySet = json.keySet();
|
||||
|
||||
for (String key : keySet) {
|
||||
@ -182,53 +211,13 @@ public abstract class SQLiteManager {
|
||||
setPart.append("`").append(key).append("` = ?, ");
|
||||
}
|
||||
|
||||
setPart.deleteCharAt(setPart.length() - 2);
|
||||
setPart.deleteCharAt(setPart.length() - 2); // 删除最后一个逗号和空格
|
||||
|
||||
sb.append("UPDATE `").append(t.getTableName()).append("` ");
|
||||
sb.append("UPDATE `").append(bean.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());
|
||||
} else {
|
||||
statement.setObject(i++, json.get(key));
|
||||
}
|
||||
}
|
||||
|
||||
// 设置 sql_time 的值
|
||||
long id = t.getSql_time().getTime();
|
||||
statement.setLong(i, id);
|
||||
|
||||
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);
|
||||
}
|
||||
}
|
||||
return sb.toString();
|
||||
}
|
||||
|
||||
protected JSONArray getJSONArray(String table) {
|
||||
|
Loading…
Reference in New Issue
Block a user