// // SDJKDBManager.m // SDJK // // Created by 张嵘 on 2021/8/26. // #import "SDJKDBManager.h" @interface SDJKDBManager () @property (nonatomic, readwrite, strong) FMDatabaseQueue *databaseQueue; @property (nonatomic, readwrite, assign) BOOL rightAutoJumpToNext; @property (nonatomic, readwrite, assign) BOOL exerciseSound; @property (nonatomic, readwrite, assign) NSInteger exerciseFontSize; @property (nonatomic, readwrite, assign) NSUInteger wrongModelsCount; @property (nonatomic, readwrite, assign) NSUInteger collectionModelsCount; @end @implementation SDJKDBManager static id rq_sdjkDBManager = nil; #pragma mark - init + (instancetype)sharedInstance { static dispatch_once_t onceToken; dispatch_once(&onceToken, ^{ rq_sdjkDBManager = [[self alloc] init]; }); return rq_sdjkDBManager; } - (instancetype)init { if (self = [super init]) { NSError *error; NSString *dbPath = [[NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES) firstObject] stringByAppendingPathComponent:[NSString stringWithFormat:@"user.db"]]; NSString *resourcePath = [[NSBundle mainBundle] pathForResource:@"user" ofType:@"db"]; if ([RQFileManager isPathExist:dbPath]) { self.databaseQueue = [FMDatabaseQueue databaseQueueWithPath:dbPath]; NSLog(@"数据库path-----%@",dbPath); } else { [[RQFileManager fileManager] copyItemAtPath:resourcePath toPath:[[NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES) firstObject] stringByAppendingPathComponent:[NSString stringWithFormat:@"user.db"]] error:&error]; self.databaseQueue = [FMDatabaseQueue databaseQueueWithPath:dbPath]; NSLog(@"数据库path-----%@",dbPath); } _rightAutoJumpToNext = YES; _exerciseSound = YES; _exerciseFontSize = ([self getExerciseFontSize] == 0)? 17 : [self getExerciseFontSize]; } return self; } #pragma mark - Public Method #pragma mark - WrongList - (void)addWrongRecordWithRQWrongModel:(RQWrongModel *)wrongModel { if (_databaseQueue) { [_databaseQueue inDatabase:^(FMDatabase * _Nonnull db) { BOOL isExist; FMResultSet *isExistResultSet = [db executeQuery:[NSString stringWithFormat:@"select COUNT(question_id) AS countNum from wrongList where question_id =%ld AND user_id = '%@'", (long)wrongModel.questionId, wrongModel.userId]]; while ([isExistResultSet next]) { NSInteger count = [isExistResultSet intForColumn:@"countNum"]; isExist = count > 0; if (isExist) { NSLog(@"%@",[NSString stringWithFormat:@"%ld--------数据库表存在该条数据",wrongModel.questionId]); if (RQStringIsNotEmpty(wrongModel.userId) && !RQObjectIsNil(@(wrongModel.questionId)) && RQStringIsNotEmpty(wrongModel.createTime)) { BOOL updateResultSet = [db executeUpdate:[NSString stringWithFormat:@"update wrongList set user_id = '%@', km = '%@', create_time = '%@' where question_id =%ld", wrongModel.userId, wrongModel.km, wrongModel.createTime, (long)wrongModel.questionId]]; if (updateResultSet) { NSLog(@"更新数据成功"); } else { NSLog(@"更新数据失败"); } } } else { NSLog(@"%@",[NSString stringWithFormat:@"%ld--------数据库表不存在该条数据",wrongModel.questionId]); BOOL insertResultSet = [db executeUpdate:[NSString stringWithFormat:@"insert into wrongList (question_id, user_id, km, create_time) values(%ld, '%@', '%@', '%@') ", (long)wrongModel.questionId, wrongModel.userId, wrongModel.km, wrongModel.createTime]]; if (insertResultSet) { NSLog(@"插入数据成功"); } else { NSLog(@"插入数据失败"); } } } }]; } } - (void)addWrongRecordWithRQWrongModelArr:(NSArray *)wrongModelArr isClearOldData:(BOOL)isClearOldData complete:(UpdateLocalDataBlock)complete { if (_databaseQueue) { [_databaseQueue inTransaction:^(FMDatabase * _Nonnull db, BOOL * _Nonnull rollback) { @try { if (isClearOldData) { NSString *deleteSqlStr = [NSString stringWithFormat:@"DELETE FROM wrongList WHERE km = '%@' AND user_id = '%@'", RQ_COMMON_MANAGER.subjectStr, RQ_USER_MANAGER.currentUserId]; BOOL resultSet = [db executeUpdate:deleteSqlStr]; if (resultSet) { NSLog(@"删除成功"); } else { NSLog(@"删除失败"); } } [[wrongModelArr.rac_sequence.signal deliverOnMainThread] subscribeNext:^(RQWrongModel *wrongModel) { BOOL isExist; if (RQStringIsEmpty(wrongModel.userId)) { wrongModel.userId = RQ_USER_MANAGER.currentUserId; } if (RQStringIsEmpty(wrongModel.km)) { wrongModel.km = RQ_COMMON_MANAGER.subjectStr; } if (RQStringIsEmpty(wrongModel.createTime)) { wrongModel.createTime = [NSDate rq_currentTimeSSSInterval]; } FMResultSet *isExistResultSet = [db executeQuery:[NSString stringWithFormat:@"select COUNT(question_id) AS countNum from wrongList where question_id =%ld AND user_id = '%@'", (long)wrongModel.questionId, wrongModel.userId]]; while ([isExistResultSet next]) { NSInteger count = [isExistResultSet intForColumn:@"countNum"]; isExist = count > 0; if (isExist) { NSLog(@"%@",[NSString stringWithFormat:@"%ld--------数据库表存在该条数据",wrongModel.questionId]); if (!RQObjectIsNil(@(wrongModel.questionId)) && RQStringIsNotEmpty(wrongModel.createTime)) { BOOL updateResultSet = [db executeUpdate:[NSString stringWithFormat:@"update wrongList set user_id = '%@', km = '%@', create_time = '%@' where question_id =%ld", wrongModel.userId, wrongModel.km, wrongModel.createTime, (long)wrongModel.questionId]]; if (updateResultSet) { NSLog(@"更新数据成功"); } else { NSLog(@"更新数据失败"); } } } else { NSLog(@"%@",[NSString stringWithFormat:@"%ld--------数据库表不存在该条数据",wrongModel.questionId]); BOOL insertResultSet = [db executeUpdate:[NSString stringWithFormat:@"insert into wrongList (question_id, user_id, km, create_time) values(%ld, '%@', '%@', '%@') ", (long)wrongModel.questionId, wrongModel.userId, wrongModel.km, wrongModel.createTime]]; if (insertResultSet) { NSLog(@"插入数据成功"); } else { NSLog(@"插入数据失败"); } } } } completed:^{ if (complete) { complete(YES, nil); } }]; } @catch (NSException *exception) { *rollback = YES; } @finally { *rollback = NO; } }]; } } - (BOOL)deleteWrongRecordWithQuestionId:(NSInteger)questionId { __block BOOL isDelete = false; if (_databaseQueue) { [_databaseQueue inDatabase:^(FMDatabase * _Nonnull db) { BOOL resultSet = [db executeUpdate:[NSString stringWithFormat:@"delete from wrongList where question_id =%ld AND user_id = '%@'", (long)questionId, RQ_USER_MANAGER.currentUserId]]; isDelete = resultSet; if (resultSet) { NSLog(@"删除数据成功"); } else { NSLog(@"删除数据失败"); } }]; } return isDelete; } - (BOOL)isExistWithRQWrongModel:(RQWrongModel *)wrongModel { __block BOOL isExist = false; if (_databaseQueue) { [_databaseQueue inDatabase:^(FMDatabase * _Nonnull db) { FMResultSet *resultSet; resultSet = [db executeQuery:[NSString stringWithFormat:@"select * from wrongList where question_id =%ld AND user_id = '%@'", (long)wrongModel.questionId, wrongModel.userId]]; while ([resultSet next]) { NSInteger questionId = [resultSet intForColumn:@"question_id"]; isExist = questionId == wrongModel.questionId; if (isExist) { NSLog(@"%@",[NSString stringWithFormat:@"%ld--------数据库表存在该条数据",wrongModel.questionId]); } else { NSLog(@"%@",[NSString stringWithFormat:@"%ld--------数据库表不存在该条数据",wrongModel.questionId]); } } }]; } return isExist; } - (void)deleteAllWrongModelsWithComplete:(VoidBlock_Bool)complete { if (_databaseQueue) { [_databaseQueue inDatabase:^(FMDatabase * _Nonnull db) { NSString *sqlStr = [NSString stringWithFormat:@"DELETE FROM wrongList WHERE km = '%@' AND user_id = '%@'", RQ_COMMON_MANAGER.subjectStr, RQ_USER_MANAGER.currentUserId]; BOOL resultSet = [db executeUpdate:sqlStr]; if (resultSet) { NSLog(@"删除成功"); self.wrongModelsCount = 0; } else { NSLog(@"删除失败"); } if (complete) complete(resultSet); }]; } } - (NSInteger)getAllWrongModelsCount { __block NSInteger count = 0; if (_databaseQueue) { [_databaseQueue inDatabase:^(FMDatabase * _Nonnull db) { FMResultSet *resultSet; NSString *sqlStr = [NSString stringWithFormat:@"SELECT COUNT(*) AS countNum FROM wrongList WHERE km = '%@' AND user_id = '%@' ", RQ_COMMON_MANAGER.subjectStr, RQ_USER_MANAGER.currentUserId]; resultSet = [db executeQuery:sqlStr]; while ([resultSet next]) { count = [resultSet intForColumn:@"countNum"]; self.wrongModelsCount = count; } }]; } return count; } - (NSArray *)queryWrongModelQuestionIdArrWithSubjectType:(RQHomePageSubjectType)subjectType { NSMutableArray *arr = @[].mutableCopy; if (_databaseQueue) { [_databaseQueue inDatabase:^(FMDatabase * _Nonnull db) { FMResultSet *resultSet; resultSet = [db executeQuery:[NSString stringWithFormat:@"select question_id from wrongList where km = '%@' AND user_id = '%@' order by create_time + 0 DESC",[RQ_COMMON_MANAGER getSubjectTypeStrWithSubjectType:subjectType], RQ_USER_MANAGER.currentUserId]]; while ([resultSet next]) { NSInteger questionId = [resultSet intForColumn:@"question_id"]; [arr addObject:@(questionId)]; } }]; } return arr.copy; } - (NSArray *)queryWrongModelQuestionIdAndTimeDicArrWithSubjectType:(RQHomePageSubjectType)subjectType { NSMutableArray *arr = @[].mutableCopy; if (_databaseQueue) { [_databaseQueue inDatabase:^(FMDatabase * _Nonnull db) { FMResultSet *resultSet; resultSet = [db executeQuery:[NSString stringWithFormat:@"select * from wrongList where km = '%@' AND user_id = '%@' order by create_time + 0 DESC",[RQ_COMMON_MANAGER getSubjectTypeStrWithSubjectType:subjectType], RQ_USER_MANAGER.currentUserId]]; while ([resultSet next]) { RQWrongModel *wrongModel = [RQWrongModel wrongModelWithFMResultSet:resultSet]; NSDictionary *dic = @{ @"id" : @(wrongModel.questionId), @"timestamp" : wrongModel.createTime, }; [arr addObject:dic]; } }]; } return arr.copy; } #pragma mark - CollectionList - (void)addCollectionRecordWithRQCollectionModel:(RQCollectionModel *)collectionModel { if (_databaseQueue) { [_databaseQueue inDatabase:^(FMDatabase * _Nonnull db) { BOOL isExist; FMResultSet *isExistResultSet = [db executeQuery:[NSString stringWithFormat:@"select COUNT(question_id) AS countNum from collectionList where question_id =%ld AND user_id = '%@'", (long)collectionModel.questionId, collectionModel.userId]]; while ([isExistResultSet next]) { NSInteger count = [isExistResultSet intForColumn:@"countNum"]; isExist = count > 0; if (isExist) { NSLog(@"%@",[NSString stringWithFormat:@"%ld--------数据库表存在该条数据",collectionModel.questionId]); if (RQStringIsNotEmpty(collectionModel.userId) && !RQObjectIsNil(@(collectionModel.questionId)) && RQStringIsNotEmpty(collectionModel.createTime)) { BOOL updateResultSet = [db executeUpdate:[NSString stringWithFormat:@"update collectionList set user_id = '%@', km = '%@', create_time = '%@' where question_id =%ld", collectionModel.userId, collectionModel.km, collectionModel.createTime, (long)collectionModel.questionId]]; if (updateResultSet) { NSLog(@"更新数据成功"); } else { NSLog(@"更新数据失败"); } } } else { NSLog(@"%@",[NSString stringWithFormat:@"%ld--------数据库表不存在该条数据",collectionModel.questionId]); BOOL insertResultSet = [db executeUpdate:[NSString stringWithFormat:@"insert into collectionList (question_id, user_id, km, create_time) values(%ld, '%@', '%@', '%@') ", (long)collectionModel.questionId, collectionModel.userId, collectionModel.km, collectionModel.createTime]]; if (insertResultSet) { NSLog(@"插入数据成功"); } else { NSLog(@"插入数据失败"); } } } }]; } } - (void)addCollectionRecordWithRQCollectionModelArr:(NSArray *)collectionModelArr isClearOldData:(BOOL)isClearOldData complete:(UpdateLocalDataBlock)complete { if (_databaseQueue) { [_databaseQueue inTransaction:^(FMDatabase * _Nonnull db, BOOL * _Nonnull rollback) { @try { if (isClearOldData) { NSString *deleteSqlStr = [NSString stringWithFormat:@"DELETE FROM collectionList WHERE km = '%@' AND user_id = '%@' ", RQ_COMMON_MANAGER.subjectStr, RQ_USER_MANAGER.currentUserId]; BOOL resultSet = [db executeUpdate:deleteSqlStr]; if (resultSet) { NSLog(@"删除成功"); } else { NSLog(@"删除失败"); } } [[collectionModelArr.rac_sequence.signal deliverOnMainThread] subscribeNext:^(RQCollectionModel *collectionModel) { BOOL isExist; if (RQStringIsEmpty(collectionModel.userId)) { collectionModel.userId = RQ_USER_MANAGER.currentUserId; } if (RQStringIsEmpty(collectionModel.km)) { collectionModel.km = RQ_COMMON_MANAGER.subjectStr; } if (RQStringIsEmpty(collectionModel.createTime)) { collectionModel.createTime = [NSDate rq_currentTimeSSSInterval]; } FMResultSet *isExistResultSet = [db executeQuery:[NSString stringWithFormat:@"select COUNT(question_id) AS countNum from collectionList where question_id =%ld AND user_id = '%@'", (long)collectionModel.questionId, collectionModel.userId]]; while ([isExistResultSet next]) { NSInteger count = [isExistResultSet intForColumn:@"countNum"]; isExist = count > 0; if (isExist) { NSLog(@"%@",[NSString stringWithFormat:@"%ld--------数据库表存在该条数据",collectionModel.questionId]); if (RQStringIsNotEmpty(collectionModel.userId) && !RQObjectIsNil(@(collectionModel.questionId)) && RQStringIsNotEmpty(collectionModel.createTime)) { BOOL updateResultSet = [db executeUpdate:[NSString stringWithFormat:@"update collectionList set user_id = '%@', km = '%@', create_time = '%@' where question_id =%ld", collectionModel.userId, collectionModel.km, collectionModel.createTime , (long)collectionModel.questionId]]; if (updateResultSet) { NSLog(@"更新数据成功"); } else { NSLog(@"更新数据失败"); } } } else { NSLog(@"%@",[NSString stringWithFormat:@"%ld--------数据库表不存在该条数据",collectionModel.questionId]); BOOL insertResultSet = [db executeUpdate:[NSString stringWithFormat:@"insert into collectionList (question_id, user_id, km, create_time) values(%ld, '%@', '%@', '%@') ", (long)collectionModel.questionId, collectionModel.userId, collectionModel.km, collectionModel.createTime]]; if (insertResultSet) { NSLog(@"插入数据成功"); } else { NSLog(@"插入数据失败"); } } } } completed:^{ if (complete) { complete(YES, nil); } }]; } @catch (NSException *exception) { *rollback = YES; } @finally { *rollback = NO; } }]; } } - (void)deleteCollectionRecordWithQuestionId:(NSInteger)questionId { if (_databaseQueue) { [_databaseQueue inDatabase:^(FMDatabase * _Nonnull db) { BOOL resultSet = [db executeUpdate:[NSString stringWithFormat:@"delete from collectionList where question_id =%ld AND user_id = '%@'", (long)questionId, RQ_USER_MANAGER.currentUserId]]; if (resultSet) { NSLog(@"删除数据成功"); } else { NSLog(@"删除数据失败"); } }]; } } - (BOOL)isExistWithRQCollectionModel:(RQCollectionModel *)collectionModel { __block BOOL isExist = false; if (_databaseQueue) { [_databaseQueue inDatabase:^(FMDatabase * _Nonnull db) { FMResultSet *resultSet; resultSet = [db executeQuery:[NSString stringWithFormat:@"select * from collectionList where question_id =%ld AND user_id = '%@'", (long)collectionModel.questionId, collectionModel.userId]]; while ([resultSet next]) { NSInteger questionId = [resultSet intForColumn:@"question_id"]; isExist = questionId == collectionModel.questionId; if (isExist) { NSLog(@"%@",[NSString stringWithFormat:@"%ld--------数据库表存在该条数据",collectionModel.questionId]); } else { NSLog(@"%@",[NSString stringWithFormat:@"%ld--------数据库表不存在该条数据",collectionModel.questionId]); } } }]; } return isExist; } - (void)deleteAllCollectionModelsWithComplete:(VoidBlock_Bool)complete { if (_databaseQueue) { [_databaseQueue inDatabase:^(FMDatabase * _Nonnull db) { NSString *sqlStr = [NSString stringWithFormat:@"DELETE FROM collectionList WHERE km = '%@' AND user_id = '%@'", RQ_COMMON_MANAGER.subjectStr, RQ_USER_MANAGER.currentUserId]; BOOL resultSet = [db executeUpdate:sqlStr]; if (resultSet) { NSLog(@"删除成功"); self.collectionModelsCount = 0; } else { NSLog(@"删除失败"); } if (complete) complete(resultSet); }]; } } - (NSInteger)getAllCollectionModelsCount { __block NSInteger count = 0; if (_databaseQueue) { [_databaseQueue inDatabase:^(FMDatabase * _Nonnull db) { FMResultSet *resultSet; NSString *sqlStr = [NSString stringWithFormat:@"SELECT COUNT(*) AS countNum FROM collectionList WHERE km = '%@' AND user_id = '%@'", RQ_COMMON_MANAGER.subjectStr, RQ_USER_MANAGER.currentUserId]; resultSet = [db executeQuery:sqlStr]; while ([resultSet next]) { count = [resultSet intForColumn:@"countNum"]; self.collectionModelsCount = count; } }]; } return count; } - (NSArray *)queryCollectionModelQuestionIdArrWithSubjectType:(RQHomePageSubjectType)subjectType { NSMutableArray *arr = @[].mutableCopy; if (_databaseQueue) { [_databaseQueue inDatabase:^(FMDatabase * _Nonnull db) { FMResultSet *resultSet; resultSet = [db executeQuery:[NSString stringWithFormat:@"select question_id from collectionList where km = '%@' AND user_id = '%@' order by create_time + 0 DESC",[RQ_COMMON_MANAGER getSubjectTypeStrWithSubjectType:subjectType], RQ_USER_MANAGER.currentUserId]]; while ([resultSet next]) { NSInteger questionId = [resultSet intForColumn:@"question_id"]; [arr addObject:@(questionId)]; } }]; } return arr.copy; } - (NSArray *)queryCollectionModelQuestionIdAndTimeDicArrWithSubjectType:(RQHomePageSubjectType)subjectType { NSMutableArray *arr = @[].mutableCopy; if (_databaseQueue) { [_databaseQueue inDatabase:^(FMDatabase * _Nonnull db) { FMResultSet *resultSet; resultSet = [db executeQuery:[NSString stringWithFormat:@"select * from collectionList where km = '%@' AND user_id = '%@' order by create_time + 0 DESC",[RQ_COMMON_MANAGER getSubjectTypeStrWithSubjectType:subjectType], RQ_USER_MANAGER.currentUserId]]; while ([resultSet next]) { RQCollectionModel *collectionModel = [RQCollectionModel collectionModelWithFMResultSet:resultSet]; NSDictionary *dic = @{ @"id" : @(collectionModel.questionId), @"timestamp" : collectionModel.createTime, }; [arr addObject:dic]; } }]; } return arr.copy; } #pragma mark - CommonList - (NSInteger)getExerciseFontSize { if (_databaseQueue) { [_databaseQueue inDatabase:^(FMDatabase * _Nonnull db) { FMResultSet *resultSet; resultSet = [db executeQuery:[NSString stringWithFormat:@"select exercise_fontsize from commonList where user_id = '%@'", RQ_USER_MANAGER.currentUserId]]; while ([resultSet next]) { _exerciseFontSize = [resultSet intForColumn:@"exercise_fontsize"]; } }]; } return _exerciseFontSize; } - (void)updateExerciseFontSizeWithFontSize:(NSInteger)fontSize { if (_databaseQueue) { [_databaseQueue inTransaction:^(FMDatabase * _Nonnull db, BOOL * _Nonnull rollback) { @try { BOOL isExist; FMResultSet *isExistResultSet = [db executeQuery:[NSString stringWithFormat:@"select COUNT(user_id) AS countNum from commonList where user_id = '%@'", RQ_USER_MANAGER.currentUserId]]; while ([isExistResultSet next]) { NSInteger count = [isExistResultSet intForColumn:@"countNum"]; isExist = count > 0; if (isExist) { NSString *sql = [NSString stringWithFormat:@"update commonList set exercise_fontsize = %ld where user_id = '%@'", (long)fontSize,RQ_USER_MANAGER.currentUserId]; BOOL updateResultSet = [db executeUpdate:sql]; if (updateResultSet) { NSLog(@"更新数据成功"); } else { NSLog(@"更新数据失败"); } } else { NSString *sql = [NSString stringWithFormat:@"insert into commonList (exercise_fontsize, right_auto_jump_to_next, exercise_sound, user_id) values(%ld, %ld, %ld, '%@') ", (long)fontSize, (long)_rightAutoJumpToNext, (long)_exerciseSound, RQ_USER_MANAGER.currentUserId]; BOOL insertResultSet = [db executeUpdate:sql]; if (insertResultSet) { NSLog(@"插入数据成功"); } else { NSLog(@"插入数据失败"); } } } } @catch (NSException *exception) { *rollback = YES; } @finally { *rollback = NO; } }]; } } - (BOOL)getRightAutoJumpToNext { if (_databaseQueue) { [_databaseQueue inDatabase:^(FMDatabase * _Nonnull db) { FMResultSet *resultSet; resultSet = [db executeQuery:[NSString stringWithFormat:@"select right_auto_jump_to_next from commonList where user_id = '%@'", RQ_USER_MANAGER.currentUserId]]; while ([resultSet next]) { _rightAutoJumpToNext = [resultSet boolForColumn:@"right_auto_jump_to_next"]; if (_rightAutoJumpToNext) { NSLog(@"自动跳转"); } else { NSLog(@"不自动跳转"); } } }]; } return _rightAutoJumpToNext; } - (void)updateRightAutoJumpToNextWithValue:(NSInteger)value { if (_databaseQueue) { [_databaseQueue inTransaction:^(FMDatabase * _Nonnull db, BOOL * _Nonnull rollback) { @try { BOOL isExist; FMResultSet *isExistResultSet = [db executeQuery:[NSString stringWithFormat:@"select COUNT(user_id) AS countNum from commonList where user_id = '%@'", RQ_USER_MANAGER.currentUserId]]; while ([isExistResultSet next]) { NSInteger count = [isExistResultSet intForColumn:@"countNum"]; isExist = count > 0; if (isExist) { BOOL updateResultSet = [db executeUpdate:[NSString stringWithFormat:@"update commonList set right_auto_jump_to_next = %ld where user_id = '%@'", (long)value,RQ_USER_MANAGER.currentUserId]]; if (updateResultSet) { NSLog(@"更新数据成功"); } else { NSLog(@"更新数据失败"); } } else { BOOL insertResultSet = [db executeUpdate:[NSString stringWithFormat:@"insert into commonList (exercise_fontsize, right_auto_jump_to_next, exercise_sound, user_id) values(%ld, %ld, %ld, '%@') ", (long)_exerciseFontSize, (long)value, (long)_exerciseSound, RQ_USER_MANAGER.currentUserId]]; if (insertResultSet) { NSLog(@"插入数据成功"); } else { NSLog(@"插入数据失败"); } } } } @catch (NSException *exception) { *rollback = YES; } @finally { *rollback = NO; } }]; } } - (BOOL)getExerciseSound { if (_databaseQueue) { [_databaseQueue inDatabase:^(FMDatabase * _Nonnull db) { FMResultSet *resultSet; resultSet = [db executeQuery:[NSString stringWithFormat:@"select exercise_sound from commonList where user_id = '%@'", RQ_USER_MANAGER.currentUserId]]; while ([resultSet next]) { _exerciseSound = [resultSet boolForColumn:@"exercise_sound"]; if (_exerciseSound) { NSLog(@"播放声音"); } else { NSLog(@"不播放声音"); } } }]; } return _exerciseSound; } - (void)updateExerciseSoundWithValue:(NSInteger)value { if (_databaseQueue) { [_databaseQueue inTransaction:^(FMDatabase * _Nonnull db, BOOL * _Nonnull rollback) { @try { BOOL isExist; FMResultSet *isExistResultSet = [db executeQuery:[NSString stringWithFormat:@"select COUNT(user_id) AS countNum from commonList where user_id = '%@'", RQ_USER_MANAGER.currentUserId]]; while ([isExistResultSet next]) { NSInteger count = [isExistResultSet intForColumn:@"countNum"]; isExist = count > 0; if (isExist) { BOOL updateResultSet = [db executeUpdate:[NSString stringWithFormat:@"update commonList set exercise_sound = %ld where user_id = '%@'", (long)value,RQ_USER_MANAGER.currentUserId]]; if (updateResultSet) { NSLog(@"更新数据成功"); } else { NSLog(@"更新数据失败"); } } else { BOOL insertResultSet = [db executeUpdate:[NSString stringWithFormat:@"insert into commonList (exercise_fontsize, right_auto_jump_to_next, exercise_sound, user_id) values(%ld, %ld, %ld, '%@') ", (long)_exerciseFontSize, (long)_rightAutoJumpToNext, (long)value, RQ_USER_MANAGER.currentUserId]]; if (insertResultSet) { NSLog(@"插入数据成功"); } else { NSLog(@"插入数据失败"); } } } } @catch (NSException *exception) { *rollback = YES; } @finally { *rollback = NO; } }]; } } #pragma mark - HistoryList - (void)addHistoryRecordWithRQHistoryModel:(RQHistoryModel *)historyModel { if (_databaseQueue) { [_databaseQueue inDatabase:^(FMDatabase * _Nonnull db) { BOOL isExist; FMResultSet *isExistResultSet = [db executeQuery:[NSString stringWithFormat:@"select COUNT(user_id) AS countNum from historyList where user_id = '%@' and km = '%@' and car_type = '%@' and seque_issue_name = '%@' and class_issue_name = '%@' and place_issue_name = '%@' and excell_issue_name = '%@'", historyModel.userId,historyModel.km, historyModel.carType,historyModel.seque_issue_name, historyModel.class_issue_name, historyModel.place_issue_name, historyModel.excell_issue_name]]; while ([isExistResultSet next]) { NSInteger count = [isExistResultSet intForColumn:@"countNum"]; isExist = count > 0; if (isExist) { NSLog(@"%@",[NSString stringWithFormat:@"%ld--------数据库表存在该条数据",historyModel.questionId]); NSString *setSql = [NSString stringWithFormat:@"question_id = %ld", (long)historyModel.questionId]; NSString *whereSql = [NSString stringWithFormat:@"user_id = '%@' and km = '%@' and car_type = '%@' and seque_issue_name = '%@' and class_issue_name = '%@' and place_issue_name = '%@' and excell_issue_name = '%@'", historyModel.userId, historyModel.km, historyModel.carType, historyModel.seque_issue_name, historyModel.class_issue_name, historyModel.place_issue_name, historyModel.excell_issue_name]; NSString *sql = [NSString stringWithFormat:@"update historyList set %@ where %@", setSql, whereSql]; BOOL updateResultSet = [db executeUpdate:sql]; if (updateResultSet) { NSLog(@"更新数据成功"); } else { NSLog(@"更新数据失败"); } } else { NSLog(@"%@",[NSString stringWithFormat:@"%ld--------数据库表不存在该条数据",historyModel.questionId]); BOOL insertResultSet = [db executeUpdate:[NSString stringWithFormat:@"insert into historyList (question_id, user_id, km, car_type, seque_issue_name, class_issue_name, place_issue_name, excell_issue_name) values(%ld, '%@', '%@', '%@', '%@', '%@', '%@', '%@') ", (long)historyModel.questionId, historyModel.userId, historyModel.km, historyModel.carType, historyModel.seque_issue_name, historyModel.class_issue_name, historyModel.place_issue_name, historyModel.excell_issue_name]]; if (insertResultSet) { NSLog(@"插入数据成功"); } else { NSLog(@"插入数据失败"); } } } }]; } } - (RQHistoryModel *)queryHistoryModelWithCarType:(RQHomePageCarType)carType subjectType:(RQHomePageSubjectType)subjectType homeSubPageTyp:(RQHomeSubPageType)homeSubPageType titleStr:(NSString *)titleStr { __block RQHistoryModel *historyModel; NSString *seque_issue_name = @""; NSString *class_issue_name = @""; NSString *place_issue_name = @""; NSString *excell_issue_name = @""; switch (homeSubPageType) { case RQHomeSubPageType_SequentialPractice: seque_issue_name = titleStr; break; case RQHomeSubPageType_LocalTopics: place_issue_name = titleStr; break; case RQHomeSubPageType_ClassificationExercise: class_issue_name = titleStr; break; case RQHomeSubPageType_SelectedTestQuestions: excell_issue_name = titleStr; break; default: break; } if (_databaseQueue) { [_databaseQueue inDatabase:^(FMDatabase * _Nonnull db) { FMResultSet *resultSet; resultSet = [db executeQuery:[NSString stringWithFormat:@"select * from historyList where user_id = '%@' and km = '%@' and car_type = '%@' and seque_issue_name = '%@' and class_issue_name = '%@' and place_issue_name = '%@' and excell_issue_name = '%@'",RQ_USER_MANAGER.currentUserId,[RQ_COMMON_MANAGER getSubjectTypeStrWithSubjectType:subjectType], [RQ_COMMON_MANAGER getCarTypeStrWithCarType:carType],seque_issue_name, class_issue_name, place_issue_name, excell_issue_name]]; while ([resultSet next]) { historyModel = [RQHistoryModel historyModelWithFMResultSet:resultSet]; } }]; } return historyModel; } - (void)refreshWrongAndCollectCount { [self getAllWrongModelsCount]; [self getAllCollectionModelsCount]; } - (void)refreshWrongCount { [self getAllWrongModelsCount]; } - (void)refreshCollectCount { [self getAllCollectionModelsCount]; } @end