init
This commit is contained in:
@@ -0,0 +1,536 @@
|
||||
package com.yutou.bilibili.sqlite;
|
||||
|
||||
import com.yutou.bilibili.BiliBili.Datas.BiliBiliUpData;
|
||||
import com.yutou.bilibili.BiliBili.Datas.LiveData;
|
||||
import com.yutou.bilibili.BiliBili.Datas.LiveInfo;
|
||||
import com.alibaba.fastjson.JSONObject;
|
||||
import com.yutou.bilibili.BiliBili.LiveUtils;
|
||||
import com.yutou.bilibili.Tools.AppTools;
|
||||
import com.yutou.bilibili.mybatis.Bili.mybatis.model.BilibiliLiveData;
|
||||
|
||||
import java.sql.ResultSet;
|
||||
import java.sql.SQLException;
|
||||
import java.sql.Statement;
|
||||
import java.text.SimpleDateFormat;
|
||||
import java.util.*;
|
||||
|
||||
public class BiliBiliLiveDatabasesManager extends SQLiteManager {
|
||||
private static BiliBiliLiveDatabasesManager manager;
|
||||
private Statement statement;
|
||||
|
||||
public static BiliBiliLiveDatabasesManager getInstance() {
|
||||
if (manager == null) {
|
||||
manager = new BiliBiliLiveDatabasesManager();
|
||||
}
|
||||
return manager;
|
||||
}
|
||||
|
||||
private BiliBiliLiveDatabasesManager() {
|
||||
super();
|
||||
|
||||
}
|
||||
public void init(String fileName){
|
||||
JSONObject json = JSONObject.parseObject("{\"file\":\"bilibili.db\",\"table\":[{\"name\":\"up_info\",\"item\":[{\"name\":\"id\",\"type\":\"int\",\"isNull\":false,\"isKey\":true},{\"name\":\"name\",\"type\":\"String\",\"isNull\":false,\"isKey\":false},{\"name\":\"url\",\"type\":\"String\",\"isNull\":false,\"isKey\":false},{\"name\":\"roomid\",\"type\":\"int\",\"isNull\":false,\"isKey\":false},{\"name\":\"offlineListening\",\"type\":\"int\",\"isNull\":false,\"isKey\":false},{\"name\":\"saveDanmu\",\"type\":\"int\",\"isNull\":false,\"isKey\":false},{\"name\":\"enable\",\"type\":\"int\",\"isNull\":false,\"isKey\":false}]},{\"name\":\"live_data\",\"item\":[{\"name\":\"id\",\"type\":\"int\",\"isNull\":false,\"isKey\":true},{\"name\":\"uid\",\"type\":\"int\",\"isNull\":false,\"isKey\":false},{\"name\":\"roomid\",\"type\":\"int\",\"isNull\":false,\"isKey\":false},{\"name\":\"type\",\"type\":\"String\",\"isNull\":false,\"isKey\":false},{\"name\":\"msg\",\"type\":\"String\",\"isNull\":true,\"isKey\":false},{\"name\":\"giftId\",\"type\":\"int\",\"isNull\":true,\"isKey\":false},{\"name\":\"giftName\",\"type\":\"String\",\"isNull\":true,\"isKey\":false},{\"name\":\"giftIndex\",\"type\":\"int\",\"isNull\":true,\"isKey\":false},{\"name\":\"price\",\"type\":\"int\",\"isNull\":true,\"isKey\":false},{\"name\":\"priceOfcommission\",\"type\":\"int\",\"isNull\":true,\"isKey\":false},{\"name\":\"subtime\",\"type\":\"TIME\",\"isNull\":true,\"isKey\":false}]},{\"name\":\"live_info\",\"item\":[{\"name\":\"id\",\"type\":\"int\",\"isNull\":false,\"isKey\":true},{\"name\":\"roomid\",\"type\":\"int\",\"isNull\":false,\"isKey\":false},{\"name\":\"popular\",\"type\":\"int\",\"isNull\":true,\"isKey\":false},{\"name\":\"userIndex\",\"type\":\"int\",\"isNull\":true,\"isKey\":false},{\"name\":\"vipUserIndex\",\"type\":\"int\",\"isNull\":true,\"isKey\":false},{\"name\":\"giftUser\",\"type\":\"int\",\"isNull\":true,\"isKey\":false},{\"name\":\"subtime\",\"type\":\"TIME\",\"isNull\":false,\"isKey\":false}]}]}");
|
||||
json.put("file",fileName+".db");
|
||||
build(json);
|
||||
}
|
||||
|
||||
/**
|
||||
* 获取所有up信息
|
||||
* @return up列表
|
||||
*/
|
||||
public List<BiliBiliUpData> getUpInfo() {
|
||||
return getUpInfo("select * from `up_info`;");
|
||||
}
|
||||
|
||||
/**
|
||||
* 根据RoomId查询UP信息
|
||||
* @param data roomId
|
||||
* @return UP信息
|
||||
*/
|
||||
public BiliBiliUpData queryUp(BiliBiliUpData data) {
|
||||
String sql = "select * from `up_info` where `roomid`=" + data.getRoomId() + ";";
|
||||
List<BiliBiliUpData> list = getUpInfo(sql);
|
||||
if (list.isEmpty()) {
|
||||
return null;
|
||||
} else {
|
||||
return list.get(0);
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* 查询UP信息
|
||||
* @param sql sql
|
||||
* @return 列表
|
||||
*/
|
||||
private List<BiliBiliUpData> getUpInfo(String sql) {
|
||||
List<BiliBiliUpData> list = new ArrayList<>();
|
||||
try {
|
||||
Statement statement = conn.createStatement();
|
||||
ResultSet set = statement.executeQuery(sql);
|
||||
while (set.next()) {
|
||||
BiliBiliUpData upData = new BiliBiliUpData();
|
||||
upData.setId(set.getInt("id"));
|
||||
upData.setName(set.getString("name"));
|
||||
upData.setUrl(set.getString("url"));
|
||||
upData.setRoomId(set.getInt("roomid"));
|
||||
upData.setOfflineListening(set.getInt("offlineListening"));
|
||||
upData.setEnable(set.getInt("enable"));
|
||||
upData.setSaveDanmu(set.getInt("saveDanmu"));
|
||||
list.add(upData);
|
||||
}
|
||||
statement.closeOnCompletion();
|
||||
} catch (SQLException e) {
|
||||
e.printStackTrace();
|
||||
}
|
||||
return list;
|
||||
}
|
||||
|
||||
/**
|
||||
* 获取所有直播数据
|
||||
* @return 直播数据列表
|
||||
*/
|
||||
public List<LiveData> getLiveData() {
|
||||
return getLiveData("select * from `live_data`;");
|
||||
}
|
||||
|
||||
/**
|
||||
* 获取直播数据
|
||||
* @param sql sql
|
||||
* @return 直播数据
|
||||
*/
|
||||
private List<LiveData> getLiveData(String sql) {
|
||||
List<LiveData> list = new ArrayList<>();
|
||||
try {
|
||||
Statement statement = conn.createStatement();
|
||||
ResultSet set = statement.executeQuery(sql);
|
||||
while (set.next()) {
|
||||
LiveData data = new LiveData();
|
||||
data.setId(set.getInt("id"));
|
||||
data.setRoomId(set.getInt("roomid"));
|
||||
data.setUid(set.getInt("uid"));
|
||||
data.setType(set.getString("type"));
|
||||
data.setMsg(set.getString("msg"));
|
||||
data.setGiftName(set.getString("giftName"));
|
||||
data.setGiftIndex(set.getInt("giftIndex"));
|
||||
data.setPrice(set.getInt("price"));
|
||||
data.setPriceOfCommission(set.getInt("priceOfcommission"));
|
||||
data.setSubTime(new Date(Long.parseLong(set.getString("subtime"))));
|
||||
data.setGiftId(set.getInt("giftId"));
|
||||
list.add(data);
|
||||
}
|
||||
statement.closeOnCompletion();
|
||||
} catch (SQLException e) {
|
||||
e.printStackTrace();
|
||||
}
|
||||
return list;
|
||||
}
|
||||
|
||||
/**
|
||||
* 获取所有直播统计
|
||||
* @return 直播统计列表
|
||||
*/
|
||||
public List<LiveInfo> getLiveInfo() {
|
||||
return getLiveInfo("select * from `live_info`;");
|
||||
}
|
||||
|
||||
/**
|
||||
* 获取直播统计
|
||||
* @param sql sql
|
||||
* @return 直播统计列表
|
||||
*/
|
||||
private List<LiveInfo> getLiveInfo(String sql) {
|
||||
List<LiveInfo> list = new ArrayList<>();
|
||||
try {
|
||||
Statement statement = conn.createStatement();
|
||||
ResultSet set = statement.executeQuery(sql);
|
||||
while (set.next()) {
|
||||
LiveInfo data = new LiveInfo();
|
||||
data.setId(set.getInt("id"));
|
||||
data.setRoomId(set.getInt("roomid"));
|
||||
data.setPopular(set.getInt("popular"));
|
||||
data.setUserIndex(set.getInt("userIndex"));
|
||||
data.setVipUserIndex(set.getInt("vipUserIndex"));
|
||||
data.setSubTime(new Date(Long.parseLong(set.getString("subtime"))));
|
||||
list.add(data);
|
||||
}
|
||||
statement.closeOnCompletion();
|
||||
} catch (SQLException e) {
|
||||
e.printStackTrace();
|
||||
}
|
||||
return list;
|
||||
}
|
||||
|
||||
/**
|
||||
* 添加up信息
|
||||
* @param upData up信息
|
||||
* @return 是否成功
|
||||
*/
|
||||
public boolean addUpInfo(BiliBiliUpData upData) {
|
||||
if (queryUp(upData) != null) {
|
||||
return false;
|
||||
}
|
||||
try {
|
||||
Statement statement = conn.createStatement();
|
||||
String sql = "insert into `up_info` (`name`,`url`,`roomid`,`offlineListening`,`enable`,`saveDanmu`)" +
|
||||
" values ('"
|
||||
+ upData.getName()
|
||||
+ "','" + upData.getUrl()
|
||||
+ "'," + upData.getRoomId()
|
||||
+ " ," + (upData.isOfflineListening() ? 1 : 0)
|
||||
+ ", " + (upData.isEnable() ? 1 : 0)
|
||||
+ ", " + (upData.isSaveDanmu() ? 1 : 0) +
|
||||
");";
|
||||
System.out.println(sql);
|
||||
statement.execute(sql);
|
||||
statement.closeOnCompletion();
|
||||
return true;
|
||||
} catch (Exception e) {
|
||||
e.printStackTrace();
|
||||
}
|
||||
return false;
|
||||
}
|
||||
|
||||
/**
|
||||
* 更新UP信息
|
||||
* @param upData up信息
|
||||
* @return 是否成功
|
||||
*/
|
||||
public boolean updateUpInfo(BiliBiliUpData upData) {
|
||||
try {
|
||||
Statement statement = conn.createStatement();
|
||||
String sql = "update `up_info` set `name`='" + upData.getName() + "'" +
|
||||
",`url`='" + upData.getUrl() + "'" +
|
||||
", `roomid` = " + upData.getRoomId() + "" +
|
||||
",`offlineListening` = " + (upData.isOfflineListening() ? 1 : 0) + "" +
|
||||
", `enable` = " + (upData.isEnable() ? 1 : 0)
|
||||
+ " where `id` = " + upData.getId();
|
||||
statement.execute(sql);
|
||||
statement.closeOnCompletion();
|
||||
return true;
|
||||
} catch (Exception e) {
|
||||
e.printStackTrace();
|
||||
}
|
||||
return false;
|
||||
}
|
||||
|
||||
/**
|
||||
* 删除指定up信息
|
||||
* @param upData roomId
|
||||
* @return 是否成功
|
||||
*/
|
||||
public boolean deleteUp(BiliBiliUpData upData) {
|
||||
try {
|
||||
Statement statement = conn.createStatement();
|
||||
String sql = "DELETE from `up_info` where `roomid`=" + upData.getRoomId();
|
||||
statement.execute(sql);
|
||||
statement.closeOnCompletion();
|
||||
return true;
|
||||
} catch (Exception e) {
|
||||
e.printStackTrace();
|
||||
}
|
||||
return false;
|
||||
}
|
||||
|
||||
/**
|
||||
* 新增直播数据
|
||||
* @param data 直播数据
|
||||
* @return 是否成功
|
||||
*/
|
||||
public boolean addLiveData(BilibiliLiveData data) {
|
||||
String sql = "insert into `live_data` (`type`,`msg`,`giftName`,`giftIndex`,`price`,`priceOfcommission`,`subtime`,`giftId`,`roomid`,`uid`) values (" +
|
||||
"'" + data.getType() + "'" +
|
||||
",'" + data.getMsg() + "'" +
|
||||
",'" + data.getGiftname() + "'" +
|
||||
"," + data.getGiftindex() + "" +
|
||||
"," + data.getPrice() + "" +
|
||||
"," + data.getPriceofcommission() + "" +
|
||||
"," + data.getSubtime().getTime() + "" +
|
||||
"," + data.getGiftid() +
|
||||
"," + data.getRoomid() +
|
||||
"," + data.getUid() +
|
||||
")";
|
||||
try {
|
||||
Statement statement = conn.createStatement();
|
||||
statement.execute(sql);
|
||||
statement.closeOnCompletion();
|
||||
return true;
|
||||
} catch (Exception e) {
|
||||
e.printStackTrace();
|
||||
System.out.println(sql);
|
||||
}
|
||||
return false;
|
||||
}
|
||||
|
||||
/**
|
||||
* 添加直播统计
|
||||
* @param data 直播统计
|
||||
* @return 是否成功
|
||||
*/
|
||||
public boolean addLiveInfo(LiveInfo data) {
|
||||
try {
|
||||
Statement statement = conn.createStatement();
|
||||
String sql = "insert into `live_info` (`popular`,`userIndex`,`vipUserIndex`,`subtime`,`roomid`) values (" +
|
||||
"" + data.getPopular() + "" +
|
||||
"," + data.getUserIndex() + "" +
|
||||
"," + data.getVipUserIndex() + "" +
|
||||
"," + data.getSubTime().getTime() +
|
||||
"," + data.getRoomId() +
|
||||
")";
|
||||
statement.execute(sql);
|
||||
statement.closeOnCompletion();
|
||||
return true;
|
||||
} catch (Exception e) {
|
||||
e.printStackTrace();
|
||||
}
|
||||
return false;
|
||||
}
|
||||
|
||||
/**
|
||||
* 查询指定时段直播数据
|
||||
* @param roomId roomId
|
||||
* @param startTime 开始时间,为空表示为当天00:00:00
|
||||
* @param endTime 结束时间,为空表示为当时
|
||||
* @param type 类型
|
||||
* @return 列表
|
||||
*/
|
||||
public List<LiveData> queryLiveData(int roomId, Date startTime, Date endTime, String[] type) {
|
||||
String typeSql = "";
|
||||
for (String t : type) {
|
||||
typeSql += "or `type` ='" + t + "' ";
|
||||
}
|
||||
typeSql = typeSql.substring(2);
|
||||
String sql = String.format("select * from `live_data` where `subtime`>=%d and `subtime`<=%d and `roomid`=%d and (" + typeSql + ")"
|
||||
, startTime.getTime(), endTime.getTime(), roomId);
|
||||
return getLiveData(sql);
|
||||
}
|
||||
|
||||
/**
|
||||
* 查询指定时段直播某个礼物总数
|
||||
* @param roomId roomId
|
||||
* @param giftId 礼物id,-1则为全部
|
||||
* @param startTime 开始时间
|
||||
* @param endTime 结束时间
|
||||
* @return 数量
|
||||
*/
|
||||
public int queryGiftSize(int roomId, int giftId, Date startTime, Date endTime) {
|
||||
try {
|
||||
if (startTime == null || endTime == null) {
|
||||
startTime = new SimpleDateFormat("yyyy-MM-dd HH:mm").parse(AppTools.getToDayTime() + " " + "00:00");
|
||||
endTime = new Date();
|
||||
}
|
||||
String sql;
|
||||
if(giftId!=-1) {
|
||||
sql = String.format("select sum(data.giftIndex) as size from `live_data` as data where data.giftId =%d and data.roomid=%d and data.subtime >=%d and data.subtime <%d;"
|
||||
, giftId, roomId, startTime.getTime(), endTime.getTime());
|
||||
}else{
|
||||
sql = String.format("select sum(data.giftIndex) as size from `live_data` as data where data.roomid=%d and data.subtime >=%d and data.subtime <%d;"
|
||||
, roomId, startTime.getTime(), endTime.getTime());
|
||||
}
|
||||
Statement statement = conn.createStatement();
|
||||
ResultSet set = statement.executeQuery(sql);
|
||||
if (set.next()) {
|
||||
statement.closeOnCompletion();
|
||||
return set.getInt("size");
|
||||
}
|
||||
statement.closeOnCompletion();
|
||||
return 0;
|
||||
} catch (Exception e) {
|
||||
e.printStackTrace();
|
||||
}
|
||||
return -1;
|
||||
}
|
||||
|
||||
/**
|
||||
* 返回指定时段各个礼物总数
|
||||
* @param roomId roomId
|
||||
* @param startTime 开始时间
|
||||
* @param endTime 结束时间
|
||||
* @return {giftName:size}
|
||||
*/
|
||||
public Map<String, Integer> queryGiftSize(int roomId, Date startTime, Date endTime) {
|
||||
Map<String, Integer> map = new HashMap<>();
|
||||
try {
|
||||
if (startTime == null || endTime == null) {
|
||||
startTime = new SimpleDateFormat("yyyy-MM-dd HH:mm").parse(AppTools.getToDayTime() + " " + "00:00");
|
||||
endTime = new Date();
|
||||
}
|
||||
String sql = String.format("select giftName,sum(giftIndex) as size from `live_data` where subtime >=%d and subtime <=%d and roomid=%d group by giftName;"
|
||||
, startTime.getTime(), endTime.getTime(), roomId);
|
||||
Statement statement = conn.createStatement();
|
||||
ResultSet set = statement.executeQuery(sql);
|
||||
while (set.next()) {
|
||||
map.put(set.getString("giftName"), set.getInt("size"));
|
||||
}
|
||||
statement.closeOnCompletion();
|
||||
} catch (Exception e) {
|
||||
e.printStackTrace();
|
||||
}
|
||||
return map;
|
||||
}
|
||||
|
||||
/**
|
||||
* 查询指定时段收益情况
|
||||
* @param roomId roomId
|
||||
* @param startTime 开始时间
|
||||
* @param endTime 结束时间
|
||||
* @return 未扣除收成比例的金瓜子数量
|
||||
*/
|
||||
public int queryPriceSize(int roomId, Date startTime, Date endTime) {
|
||||
try {
|
||||
if (startTime == null || endTime == null) {
|
||||
startTime = new SimpleDateFormat("yyyy-MM-dd HH:mm").parse(AppTools.getToDayTime() + " " + "00:00");
|
||||
endTime = new Date();
|
||||
}
|
||||
String sql = String.format("select sum(data.price) as size from `live_data` as data where data.subtime >=%d and data.subtime <=%d and data.roomid=%d;"
|
||||
, startTime.getTime(), endTime.getTime(), roomId);
|
||||
Statement statement = conn.createStatement();
|
||||
ResultSet set = statement.executeQuery(sql);
|
||||
if (set.next()) {
|
||||
statement.closeOnCompletion();
|
||||
return set.getInt("size");
|
||||
}
|
||||
statement.closeOnCompletion();
|
||||
return 0;
|
||||
} catch (Exception e) {
|
||||
e.printStackTrace();
|
||||
}
|
||||
return -1;
|
||||
}
|
||||
|
||||
/**
|
||||
* 根据礼物id查询礼物信息,该方法主要用于阿B接口未收录的礼物信息
|
||||
* @param giftId 礼物id
|
||||
* @return 直播信息
|
||||
*/
|
||||
public LiveData queryGiftOfId(int giftId) {
|
||||
String sql = String.format("select * from `live_data` where `giftId` = %d;", giftId);
|
||||
List<LiveData> list = getLiveData(sql);
|
||||
if (list.isEmpty()) {
|
||||
return null;
|
||||
}
|
||||
return list.get(0);
|
||||
}
|
||||
|
||||
/**
|
||||
* 查询指定时段去重送礼人数
|
||||
*
|
||||
* @param upData up信息
|
||||
* @param startTime 开始时间
|
||||
* @param endTime 结束时间
|
||||
* @param type 礼物类型
|
||||
* @return 人数
|
||||
*/
|
||||
public int queryGiftUserToDistinct(BiliBiliUpData upData, Date startTime, Date endTime, String[] type) {
|
||||
try {
|
||||
if (startTime == null || endTime == null) {
|
||||
startTime = new SimpleDateFormat("yyyy-MM-dd HH:mm").parse(AppTools.getToDayTime() + " " + "00:00");
|
||||
endTime = new Date();
|
||||
}
|
||||
String typeSql = "";
|
||||
for (String t : type) {
|
||||
typeSql += "or `type` ='" + t + "' ";
|
||||
}
|
||||
typeSql = typeSql.substring(2);
|
||||
String sql = String.format("select count(distinct uid) as size from `live_data` where subtime>=%d and subtime <%d and roomid=%d and(%s);"
|
||||
, startTime.getTime(), endTime.getTime(), upData.getRoomId(), typeSql);
|
||||
return queryGiftCount(sql);
|
||||
} catch (Exception e) {
|
||||
e.printStackTrace();
|
||||
}
|
||||
return 0;
|
||||
}
|
||||
|
||||
/**
|
||||
* 查询礼物数量
|
||||
* @param sql sql
|
||||
* @return 礼物数量
|
||||
*/
|
||||
private int queryGiftCount(String sql) {
|
||||
int count = 0;
|
||||
try {
|
||||
|
||||
Statement statement = conn.createStatement();
|
||||
ResultSet set = statement.executeQuery(sql);
|
||||
if (set.next()) {
|
||||
count = set.getInt("size");
|
||||
}
|
||||
statement.closeOnCompletion();
|
||||
} catch (Exception e) {
|
||||
e.printStackTrace();
|
||||
}
|
||||
return count;
|
||||
}
|
||||
|
||||
/**
|
||||
* 查询时段内人气数量
|
||||
* @param upData up信息
|
||||
* @param startTime 开始时间
|
||||
* @param endTime 结束时间
|
||||
* @return 人气
|
||||
*/
|
||||
public int queryPopularCount(BiliBiliUpData upData,Date startTime, Date endTime){
|
||||
int count = 0;
|
||||
try{
|
||||
if (startTime == null || endTime == null) {
|
||||
startTime = new SimpleDateFormat("yyyy-MM-dd HH:mm").parse(AppTools.getToDayTime() + " " + "00:00");
|
||||
endTime = new Date();
|
||||
}
|
||||
String sql=String.format("select sum(data.popular) as size from `live_info` as data where data.subtime >=%d and data.subtime <%d and data.roomid=%d;"
|
||||
,startTime.getTime(),endTime.getTime(),upData.getRoomId());
|
||||
Statement statement = conn.createStatement();
|
||||
ResultSet set = statement.executeQuery(sql);
|
||||
if (set.next()) {
|
||||
count = set.getInt("size");
|
||||
}
|
||||
statement.closeOnCompletion();
|
||||
}catch (Exception e){
|
||||
e.printStackTrace();
|
||||
}
|
||||
return count;
|
||||
}
|
||||
|
||||
/**
|
||||
* 查询时段内进场人数
|
||||
* @param upData up信息
|
||||
* @param startTime 开始时间
|
||||
* @param endTime 结束时间
|
||||
* @return 人数
|
||||
*/
|
||||
public int queryUserCount(BiliBiliUpData upData,Date startTime, Date endTime){
|
||||
int count=0;
|
||||
try{
|
||||
if (startTime == null || endTime == null) {
|
||||
startTime = new SimpleDateFormat("yyyy-MM-dd HH:mm").parse(AppTools.getToDayTime() + " " + "00:00");
|
||||
endTime = new Date();
|
||||
}
|
||||
String sql=String.format("select sum(userIndex+vipUserIndex) as size from `live_info` where `subtime`>=%d and `subtime`<=%d and `roomid`=%d;"
|
||||
,startTime.getTime(),endTime.getTime(),upData.getRoomId());
|
||||
Statement statement = conn.createStatement();
|
||||
ResultSet set = statement.executeQuery(sql);
|
||||
if (set.next()) {
|
||||
count = set.getInt("size");
|
||||
}
|
||||
statement.closeOnCompletion();
|
||||
}catch (Exception e){
|
||||
e.printStackTrace();
|
||||
}
|
||||
return count;
|
||||
}
|
||||
public Map<String, Integer> queryUserGiftCount(BiliBiliUpData upData,Date startTime,Date endTime){
|
||||
Map<String,Integer> map=new HashMap<>();
|
||||
try{
|
||||
if (startTime == null || endTime == null) {
|
||||
startTime = new SimpleDateFormat("yyyy-MM-dd HH:mm").parse(AppTools.getToDayTime() + " " + "00:00");
|
||||
endTime = new Date();
|
||||
}
|
||||
String sql=String.format("select * from `live_info` where subtime >=%d and subtime <%d and roomid=%d;"
|
||||
,startTime.getTime()
|
||||
,endTime.getTime()
|
||||
,upData.getRoomId());
|
||||
}catch (Exception e){
|
||||
e.printStackTrace();
|
||||
}
|
||||
return map;
|
||||
}
|
||||
}
|
||||
136
src/main/java/com/yutou/bilibili/sqlite/SQLiteManager.java
Normal file
136
src/main/java/com/yutou/bilibili/sqlite/SQLiteManager.java
Normal file
@@ -0,0 +1,136 @@
|
||||
package com.yutou.bilibili.sqlite;
|
||||
|
||||
import com.alibaba.fastjson.JSONArray;
|
||||
import com.alibaba.fastjson.JSONObject;
|
||||
|
||||
import java.io.File;
|
||||
import java.sql.Connection;
|
||||
import java.sql.DriverManager;
|
||||
import java.sql.SQLException;
|
||||
import java.sql.Statement;
|
||||
|
||||
public class SQLiteManager {
|
||||
protected Connection conn;
|
||||
private String url = "jdbc:sqlite:";
|
||||
private File sql;
|
||||
|
||||
|
||||
public void startBatch() {
|
||||
try {
|
||||
conn.setAutoCommit(false);
|
||||
} catch (SQLException e) {
|
||||
e.printStackTrace();
|
||||
}
|
||||
}
|
||||
|
||||
public void closeBatch() {
|
||||
try {
|
||||
conn.setAutoCommit(true);
|
||||
} catch (SQLException e) {
|
||||
e.printStackTrace();
|
||||
}
|
||||
}
|
||||
|
||||
public void commit() {
|
||||
try {
|
||||
conn.commit();
|
||||
} catch (SQLException e) {
|
||||
e.printStackTrace();
|
||||
}
|
||||
}
|
||||
|
||||
protected SQLiteManager() {
|
||||
|
||||
}
|
||||
private void createSql(JSONObject json){
|
||||
try {
|
||||
sql.mkdirs();
|
||||
sql.delete();
|
||||
conn = DriverManager.getConnection(url + sql.getAbsolutePath());
|
||||
}catch (Exception e){
|
||||
e.printStackTrace();
|
||||
}
|
||||
startBatch();
|
||||
JSONArray array=json.getJSONArray("table");
|
||||
for (Object o : array) {
|
||||
System.out.println("创建表:"+((JSONObject)o).getString("name"));
|
||||
createSqlOfTable((JSONObject) o);
|
||||
}
|
||||
closeBatch();
|
||||
}
|
||||
private void createSqlOfTable(JSONObject table) {
|
||||
String tableName = table.getString("name");
|
||||
try {
|
||||
Statement statement = conn.createStatement();
|
||||
JSONArray items = table.getJSONArray("item");
|
||||
StringBuilder sql = new StringBuilder();
|
||||
sql.append("CREATE TABLE `")
|
||||
.append(tableName)
|
||||
.append("` (");
|
||||
for (Object item : items) {
|
||||
StringBuilder builder = new StringBuilder();
|
||||
JSONObject it = (JSONObject) item;
|
||||
String type;
|
||||
switch (it.getString("type")) {
|
||||
case "int":
|
||||
type = " INTEGER ";
|
||||
break;
|
||||
case "TIME":
|
||||
type = " NUMERIC ";
|
||||
break;
|
||||
default:
|
||||
type = " TEXT ";
|
||||
break;
|
||||
}
|
||||
builder.append("`")
|
||||
.append(it.getString("name"))
|
||||
.append("`")
|
||||
.append(type)
|
||||
.append(it.getBoolean("isNull") ? "" : " NOT NULL ")
|
||||
.append(it.getBoolean("isKey") ? " PRIMARY KEY AUTOINCREMENT " : "")
|
||||
.append(",");
|
||||
sql.append(builder.toString());
|
||||
}
|
||||
sql.append(");");
|
||||
statement.execute(sql.toString().replace(",);", ");"));
|
||||
statement.closeOnCompletion();
|
||||
} catch (Exception e) {
|
||||
e.printStackTrace();
|
||||
}
|
||||
}
|
||||
|
||||
protected void build(JSONObject json) {
|
||||
try {
|
||||
Class.forName("org.sqlite.JDBC");
|
||||
sql = new File("databases" + File.separator + json.getString("file"));
|
||||
if (!sql.exists()) {
|
||||
createSql(json);
|
||||
} else {
|
||||
conn = DriverManager.getConnection(url + sql.getAbsolutePath());
|
||||
}
|
||||
} catch (Exception e) {
|
||||
e.printStackTrace();
|
||||
}
|
||||
}
|
||||
|
||||
public boolean setDB(String fileName) {
|
||||
try {
|
||||
Class.forName("org.sqlite.JDBC");
|
||||
sql = new File("db" + File.separator + fileName);
|
||||
if (sql.exists()) {
|
||||
if (conn != null && !conn.isClosed()) {
|
||||
conn.close();
|
||||
}
|
||||
conn = DriverManager.getConnection(url + sql.getAbsolutePath());
|
||||
return true;
|
||||
}
|
||||
} catch (Exception e) {
|
||||
e.printStackTrace();
|
||||
}
|
||||
return false;
|
||||
}
|
||||
|
||||
public static void main(String[] args) {
|
||||
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,15 @@
|
||||
package com.yutou.bilibili.sqlite;
|
||||
|
||||
public class YouTubeLiveDatabasesManager extends SQLiteManager{
|
||||
private static YouTubeLiveDatabasesManager manager;
|
||||
public static YouTubeLiveDatabasesManager getInstance(){
|
||||
if(manager==null){
|
||||
manager=new YouTubeLiveDatabasesManager();
|
||||
}
|
||||
return manager;
|
||||
}
|
||||
public YouTubeLiveDatabasesManager() {
|
||||
super();
|
||||
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user