123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264 |
- //
- // RQDataBaseManager.m
- // jiaPei
- //
- // Created by 张嵘 on 2018/12/4.
- // Copyright © 2018 JCZ. All rights reserved.
- //
- #import "RQDataBaseManager.h"
- /************************* 下载 *************************/
- NSString * const RQDownloadProgressNotification = @"RQDownloadProgressNotification";
- NSString * const RQDownloadStateChangeNotification = @"RQDownloadStateChangeNotification";
- NSString * const RQDownloadMaxConcurrentCountKey = @"RQDownloadMaxConcurrentCountKey";
- NSString * const RQDownloadMaxConcurrentCountChangeNotification = @"RQDownloadMaxConcurrentCountChangeNotification";
- NSString * const RQDownloadAllowsCellularAccessKey = @"RQDownloadAllowsCellularAccessKey";
- NSString * const RQDownloadAllowsCellularAccessChangeNotification = @"RQDownloadAllowsCellularAccessChangeNotification";
- typedef NS_ENUM(NSInteger, RQDBGetDateOption) {
- RQDBGetDateOptionAllCacheData = 0, // 所有缓存数据
- RQDBGetDateOptionAllDownloadingData, // 所有正在下载的数据
- RQDBGetDateOptionAllDownloadedData, // 所有下载完成的数据
- RQDBGetDateOptionAllUnDownloadedData, // 所有未下载完成的数据
- RQDBGetDateOptionAllWaitingData, // 所有等待下载的数据
- RQDBGetDateOptionModelWithUrl, // 通过url获取单条数据
- RQDBGetDateOptionWaitingModel, // 第一条等待的数据
- RQDBGetDateOptionLastDownloadingModel, // 最后一条正在下载的数据
- };
- @interface RQDataBaseManager ()
- @property (nonatomic, strong) FMDatabaseQueue *dbQueue;
- @end
- @implementation RQDataBaseManager
- + (instancetype)shareManager
- {
- static RQDataBaseManager *manager = nil;
-
- static dispatch_once_t onceToken;
- dispatch_once(&onceToken, ^{
- manager = [[self alloc] init];
- });
-
- return manager;
- }
- - (instancetype)init
- {
- if (self = [super init]) {
- [self creatVideoCachesTable];
- }
-
- return self;
- }
- // 创表
- - (void)creatVideoCachesTable
- {
- // 数据库文件路径
- NSString *path = [[NSSearchPathForDirectoriesInDomains(NSCachesDirectory, NSUserDomainMask, YES) lastObject] stringByAppendingPathComponent:@"RQDownloadVideoCaches.sqlite"];
-
- // 创建队列对象,内部会自动创建一个数据库, 并且自动打开
- _dbQueue = [FMDatabaseQueue databaseQueueWithPath:path];
-
- [_dbQueue inDatabase:^(FMDatabase *db) {
- // 创表
- BOOL result = [db executeUpdate:@"CREATE TABLE IF NOT EXISTS t_videoCaches (id integer PRIMARY KEY AUTOINCREMENT, vid text, subject text, urlString text, title text, imageUrl text, content text, fileName text, resumeData blob, fileTotalSize integer, fileDownloadSize integer, progress float, status integer, statusText text, lastSpeedTime integer, intervalFileSize integer, lastStateTime integer, destinationPath text)"];
- if (result) {
- NSLog(@"视频缓存数据表创建成功 --- %@",path);
- }else {
- NSLog(@"视频缓存数据表创建失败");
- }
- }];
- }
- // 插入数据
- - (void)insertModel:(RQDownloadModel *)model
- {
- [_dbQueue inDatabase:^(FMDatabase *db) {
- BOOL result = [db executeUpdate:@"INSERT INTO t_videoCaches (vid, subject, urlString, title, imageUrl, content, fileName, resumeData, fileTotalSize, fileDownloadSize, progress, status, statusText, lastSpeedTime, intervalFileSize, lastStateTime, destinationPath) VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?)", model.vid, model.subject, model.urlString, model.title, model.imageUrl, model.content, model.fileName, model.resumeData, [NSNumber numberWithInteger:model.fileTotalSize], [NSNumber numberWithInteger:model.fileDownloadSize], [NSNumber numberWithInteger:model.progress], [NSNumber numberWithFloat:model.status], model.statusText, [NSNumber numberWithInteger:0], [NSNumber numberWithInteger:0], [NSNumber numberWithInteger:0], model.destinationPath];
- if (result) {
- NSLog(@"插入成功:%@", model.fileName);
- }else {
- NSLog(@"插入失败:%@", model.fileName);
- }
- }];
- }
- // 获取单条数据
- - (RQDownloadModel *)getModelWithUrl:(NSString *)url
- {
- return [self getModelWithOption:RQDBGetDateOptionModelWithUrl url:url];
- }
- // 获取第一条等待的数据
- - (RQDownloadModel *)getWaitingModel
- {
- return [self getModelWithOption:RQDBGetDateOptionWaitingModel url:nil];
- }
- // 获取最后一条正在下载的数据
- - (RQDownloadModel *)getLastDownloadingModel
- {
- return [self getModelWithOption:RQDBGetDateOptionLastDownloadingModel url:nil];
- }
- // 获取所有数据
- - (NSArray<RQDownloadModel *> *)getAllCacheData
- {
- return [self getDateWithOption:RQDBGetDateOptionAllCacheData];
- }
- // 根据lastStateTime倒叙获取所有正在下载的数据
- - (NSArray<RQDownloadModel *> *)getAllDownloadingData
- {
- return [self getDateWithOption:RQDBGetDateOptionAllDownloadingData];
- }
- // 获取所有下载完成的数据
- - (NSArray<RQDownloadModel *> *)getAllDownloadedData
- {
- return [self getDateWithOption:RQDBGetDateOptionAllDownloadedData];
- }
- // 获取所有未下载完成的数据
- - (NSArray<RQDownloadModel *> *)getAllUnDownloadedData
- {
- return [self getDateWithOption:RQDBGetDateOptionAllUnDownloadedData];
- }
- // 获取所有等待下载的数据
- - (NSArray<RQDownloadModel *> *)getAllWaitingData
- {
- return [self getDateWithOption:RQDBGetDateOptionAllWaitingData];
- }
- // 获取单条数据
- - (RQDownloadModel *)getModelWithOption:(RQDBGetDateOption)option url:(NSString *)url
- {
- __block RQDownloadModel *model = nil;
-
- [_dbQueue inDatabase:^(FMDatabase *db) {
- FMResultSet *resultSet;
- switch (option) {
- case RQDBGetDateOptionModelWithUrl:
- resultSet = [db executeQuery:@"SELECT * FROM t_videoCaches WHERE urlString = ?", url];
- break;
-
- case RQDBGetDateOptionWaitingModel:
- resultSet = [db executeQuery:@"SELECT * FROM t_videoCaches WHERE status = ? order by lastStateTime asc limit 0,1", [NSNumber numberWithInteger:RQDownloadStatus_Waiting]];
- break;
-
- case RQDBGetDateOptionLastDownloadingModel:
- resultSet = [db executeQuery:@"SELECT * FROM t_videoCaches WHERE status = ? order by lastStateTime desc limit 0,1", [NSNumber numberWithInteger:RQDownloadStatus_Running]];
- break;
-
- default:
- break;
- }
-
- while ([resultSet next]) {
- model = [[RQDownloadModel alloc] initWithFMResultSet:resultSet];
- }
- }];
-
- return model;
- }
- // 获取数据集合
- - (NSArray<RQDownloadModel *> *)getDateWithOption:(RQDBGetDateOption)option
- {
- __block NSArray<RQDownloadModel *> *array = nil;
-
- [_dbQueue inDatabase:^(FMDatabase *db) {
- FMResultSet *resultSet;
- switch (option) {
- case RQDBGetDateOptionAllCacheData:
- resultSet = [db executeQuery:@"SELECT * FROM t_videoCaches"];
- break;
-
- case RQDBGetDateOptionAllDownloadingData:
- resultSet = [db executeQuery:@"SELECT * FROM t_videoCaches WHERE status = ? order by lastStateTime desc", [NSNumber numberWithInteger:RQDownloadStatus_Running]];
- break;
-
- case RQDBGetDateOptionAllDownloadedData:
- resultSet = [db executeQuery:@"SELECT * FROM t_videoCaches WHERE status = ?", [NSNumber numberWithInteger:RQDownloadStatus_Completed]];
- break;
-
- case RQDBGetDateOptionAllUnDownloadedData:
- resultSet = [db executeQuery:@"SELECT * FROM t_videoCaches WHERE status != ?", [NSNumber numberWithInteger:RQDownloadStatus_Completed]];
- break;
-
- case RQDBGetDateOptionAllWaitingData:
- resultSet = [db executeQuery:@"SELECT * FROM t_videoCaches WHERE status = ?", [NSNumber numberWithInteger:RQDownloadStatus_Waiting]];
- break;
-
- default:
- break;
- }
-
- NSMutableArray *tmpArr = [NSMutableArray array];
- while ([resultSet next]) {
- [tmpArr addObject:[[RQDownloadModel alloc] initWithFMResultSet:resultSet]];
- }
- array = tmpArr;
- }];
-
- return array;
- }
- // 更新数据
- - (void)updateWithModel:(RQDownloadModel *)model option:(RQDBUpdateOption)option
- {
- [_dbQueue inDatabase:^(FMDatabase *db) {
- if (option & RQDBUpdateOptionState) {
- [self postStateChangeNotificationWithFMDatabase:db model:model];
- [db executeUpdate:@"UPDATE t_videoCaches SET status = ? WHERE urlString = ?", [NSNumber numberWithInteger:model.status], model.urlString];
- }
- if (option & RQDBUpdateOptionLastStateTime) {
- [db executeUpdate:@"UPDATE t_videoCaches SET lastStateTime = ? WHERE urlString = ?", [NSNumber numberWithInteger:[RQ_SHARE_FUNCTION getTimeStampWithDate:[NSDate date]]], model.urlString];
- }
- if (option & RQDBUpdateOptionResumeData) {
- [db executeUpdate:@"UPDATE t_videoCaches SET resumeData = ? WHERE urlString = ?", model.resumeData, model.urlString];
- }
- if (option & RQDBUpdateOptionProgressData) {
- [db executeUpdate:@"UPDATE t_videoCaches SET fileDownloadSize = ?, fileTotalSize = ?, progress = ?, lastSpeedTime = ?, intervalFileSize = ? WHERE urlString = ?", [NSNumber numberWithInteger:model.fileDownloadSize], [NSNumber numberWithFloat:model.fileTotalSize], [NSNumber numberWithFloat:model.progress], [NSNumber numberWithInteger:model.lastSpeedTime], [NSNumber numberWithInteger:model.intervalFileSize], model.urlString];
- }
- if (option & RQDBUpdateOptionAllParam) {
- [self postStateChangeNotificationWithFMDatabase:db model:model];
- [db executeUpdate:@"UPDATE t_videoCaches SET resumeData = ?, fileTotalSize = ?, fileDownloadSize = ?, progress = ?, status = ?, statusText = ?, lastSpeedTime = ?, intervalFileSize = ?, lastStateTime = ? WHERE urlString = ?", model.resumeData, [NSNumber numberWithInteger:model.fileTotalSize], [NSNumber numberWithInteger:model.fileDownloadSize], [NSNumber numberWithFloat:model.progress], [NSNumber numberWithInteger:model.status], model.statusText, [NSNumber numberWithInteger:model.lastSpeedTime], [NSNumber numberWithInteger:model.intervalFileSize], [NSNumber numberWithInteger:[RQ_SHARE_FUNCTION getTimeStampWithDate:[NSDate date]]], model.urlString];
- }
- }];
- }
- // 状态变更通知
- - (void)postStateChangeNotificationWithFMDatabase:(FMDatabase *)db model:(RQDownloadModel *)model
- {
- // 原状态
- NSInteger oldState = [db intForQuery:@"SELECT status FROM t_videoCaches WHERE urlString = ?", model.urlString];
- if (oldState != model.status) {
- // 状态变更通知
- [[NSNotificationCenter defaultCenter] postNotificationName:RQDownloadStateChangeNotification object:model];
- }
- }
- // 删除数据
- - (void)deleteModelWithUrl:(NSString *)url
- {
- [_dbQueue inDatabase:^(FMDatabase *db) {
- BOOL result = [db executeUpdate:@"DELETE FROM t_videoCaches WHERE urlString = ?", url];
- if (result) {
- NSLog(@"删除成功:%@", url);
- }else {
- NSLog(@"删除失败:%@", url);
- }
- }];
- }
- @end
|