123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704 |
- //
- // 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
|