SDJKDBManager.m 35 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704
  1. //
  2. // SDJKDBManager.m
  3. // SDJK
  4. //
  5. // Created by 张嵘 on 2021/8/26.
  6. //
  7. #import "SDJKDBManager.h"
  8. @interface SDJKDBManager ()
  9. @property (nonatomic, readwrite, strong) FMDatabaseQueue *databaseQueue;
  10. @property (nonatomic, readwrite, assign) BOOL rightAutoJumpToNext;
  11. @property (nonatomic, readwrite, assign) BOOL exerciseSound;
  12. @property (nonatomic, readwrite, assign) NSInteger exerciseFontSize;
  13. @property (nonatomic, readwrite, assign) NSUInteger wrongModelsCount;
  14. @property (nonatomic, readwrite, assign) NSUInteger collectionModelsCount;
  15. @end
  16. @implementation SDJKDBManager
  17. static id rq_sdjkDBManager = nil;
  18. #pragma mark - init
  19. + (instancetype)sharedInstance {
  20. static dispatch_once_t onceToken;
  21. dispatch_once(&onceToken, ^{
  22. rq_sdjkDBManager = [[self alloc] init];
  23. });
  24. return rq_sdjkDBManager;
  25. }
  26. - (instancetype)init {
  27. if (self = [super init]) {
  28. NSError *error;
  29. NSString *dbPath = [[NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES) firstObject] stringByAppendingPathComponent:[NSString stringWithFormat:@"user.db"]];
  30. NSString *resourcePath = [[NSBundle mainBundle] pathForResource:@"user" ofType:@"db"];
  31. if ([RQFileManager isPathExist:dbPath]) {
  32. self.databaseQueue = [FMDatabaseQueue databaseQueueWithPath:dbPath];
  33. NSLog(@"数据库path-----%@",dbPath);
  34. } else {
  35. [[RQFileManager fileManager] copyItemAtPath:resourcePath toPath:[[NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES) firstObject] stringByAppendingPathComponent:[NSString stringWithFormat:@"user.db"]] error:&error];
  36. self.databaseQueue = [FMDatabaseQueue databaseQueueWithPath:dbPath];
  37. NSLog(@"数据库path-----%@",dbPath);
  38. }
  39. _rightAutoJumpToNext = YES;
  40. _exerciseSound = YES;
  41. _exerciseFontSize = ([self getExerciseFontSize] == 0)? 17 : [self getExerciseFontSize];
  42. }
  43. return self;
  44. }
  45. #pragma mark - Public Method
  46. #pragma mark - WrongList
  47. - (void)addWrongRecordWithRQWrongModel:(RQWrongModel *)wrongModel {
  48. if (_databaseQueue) {
  49. [_databaseQueue inDatabase:^(FMDatabase * _Nonnull db) {
  50. BOOL isExist;
  51. 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]];
  52. while ([isExistResultSet next]) {
  53. NSInteger count = [isExistResultSet intForColumn:@"countNum"];
  54. isExist = count > 0;
  55. if (isExist) {
  56. NSLog(@"%@",[NSString stringWithFormat:@"%ld--------数据库表存在该条数据",wrongModel.questionId]);
  57. if (RQStringIsNotEmpty(wrongModel.userId) && !RQObjectIsNil(@(wrongModel.questionId)) && RQStringIsNotEmpty(wrongModel.createTime)) {
  58. 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]];
  59. if (updateResultSet) {
  60. NSLog(@"更新数据成功");
  61. } else {
  62. NSLog(@"更新数据失败");
  63. }
  64. }
  65. } else {
  66. NSLog(@"%@",[NSString stringWithFormat:@"%ld--------数据库表不存在该条数据",wrongModel.questionId]);
  67. 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]];
  68. if (insertResultSet) {
  69. NSLog(@"插入数据成功");
  70. } else {
  71. NSLog(@"插入数据失败");
  72. }
  73. }
  74. }
  75. }];
  76. }
  77. }
  78. - (void)addWrongRecordWithRQWrongModelArr:(NSArray *)wrongModelArr isClearOldData:(BOOL)isClearOldData complete:(UpdateLocalDataBlock)complete {
  79. if (_databaseQueue) {
  80. [_databaseQueue inTransaction:^(FMDatabase * _Nonnull db, BOOL * _Nonnull rollback) {
  81. @try {
  82. if (isClearOldData) {
  83. NSString *deleteSqlStr = [NSString stringWithFormat:@"DELETE FROM wrongList WHERE km = '%@' AND user_id = '%@'", RQ_COMMON_MANAGER.subjectStr, RQ_USER_MANAGER.currentUserId];
  84. BOOL resultSet = [db executeUpdate:deleteSqlStr];
  85. if (resultSet) {
  86. NSLog(@"删除成功");
  87. } else {
  88. NSLog(@"删除失败");
  89. }
  90. }
  91. [[wrongModelArr.rac_sequence.signal deliverOnMainThread] subscribeNext:^(RQWrongModel *wrongModel) {
  92. BOOL isExist;
  93. if (RQStringIsEmpty(wrongModel.userId)) {
  94. wrongModel.userId = RQ_USER_MANAGER.currentUserId;
  95. }
  96. if (RQStringIsEmpty(wrongModel.km)) {
  97. wrongModel.km = RQ_COMMON_MANAGER.subjectStr;
  98. }
  99. if (RQStringIsEmpty(wrongModel.createTime)) {
  100. wrongModel.createTime = [NSDate rq_currentTimeSSSInterval];
  101. }
  102. 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]];
  103. while ([isExistResultSet next]) {
  104. NSInteger count = [isExistResultSet intForColumn:@"countNum"];
  105. isExist = count > 0;
  106. if (isExist) {
  107. NSLog(@"%@",[NSString stringWithFormat:@"%ld--------数据库表存在该条数据",wrongModel.questionId]);
  108. if (!RQObjectIsNil(@(wrongModel.questionId)) && RQStringIsNotEmpty(wrongModel.createTime)) {
  109. 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]];
  110. if (updateResultSet) {
  111. NSLog(@"更新数据成功");
  112. } else {
  113. NSLog(@"更新数据失败");
  114. }
  115. }
  116. } else {
  117. NSLog(@"%@",[NSString stringWithFormat:@"%ld--------数据库表不存在该条数据",wrongModel.questionId]);
  118. 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]];
  119. if (insertResultSet) {
  120. NSLog(@"插入数据成功");
  121. } else {
  122. NSLog(@"插入数据失败");
  123. }
  124. }
  125. }
  126. } completed:^{
  127. if (complete) {
  128. complete(YES, nil);
  129. }
  130. }];
  131. } @catch (NSException *exception) {
  132. *rollback = YES;
  133. } @finally {
  134. *rollback = NO;
  135. }
  136. }];
  137. }
  138. }
  139. - (BOOL)deleteWrongRecordWithQuestionId:(NSInteger)questionId {
  140. __block BOOL isDelete = false;
  141. if (_databaseQueue) {
  142. [_databaseQueue inDatabase:^(FMDatabase * _Nonnull db) {
  143. BOOL resultSet = [db executeUpdate:[NSString stringWithFormat:@"delete from wrongList where question_id =%ld AND user_id = '%@'", (long)questionId, RQ_USER_MANAGER.currentUserId]];
  144. isDelete = resultSet;
  145. if (resultSet) {
  146. NSLog(@"删除数据成功");
  147. } else {
  148. NSLog(@"删除数据失败");
  149. }
  150. }];
  151. }
  152. return isDelete;
  153. }
  154. - (BOOL)isExistWithRQWrongModel:(RQWrongModel *)wrongModel {
  155. __block BOOL isExist = false;
  156. if (_databaseQueue) {
  157. [_databaseQueue inDatabase:^(FMDatabase * _Nonnull db) {
  158. FMResultSet *resultSet;
  159. resultSet = [db executeQuery:[NSString stringWithFormat:@"select * from wrongList where question_id =%ld AND user_id = '%@'", (long)wrongModel.questionId, wrongModel.userId]];
  160. while ([resultSet next]) {
  161. NSInteger questionId = [resultSet intForColumn:@"question_id"];
  162. isExist = questionId == wrongModel.questionId;
  163. if (isExist) {
  164. NSLog(@"%@",[NSString stringWithFormat:@"%ld--------数据库表存在该条数据",wrongModel.questionId]);
  165. } else {
  166. NSLog(@"%@",[NSString stringWithFormat:@"%ld--------数据库表不存在该条数据",wrongModel.questionId]);
  167. }
  168. }
  169. }];
  170. }
  171. return isExist;
  172. }
  173. - (void)deleteAllWrongModelsWithComplete:(VoidBlock_Bool)complete {
  174. if (_databaseQueue) {
  175. [_databaseQueue inDatabase:^(FMDatabase * _Nonnull db) {
  176. NSString *sqlStr = [NSString stringWithFormat:@"DELETE FROM wrongList WHERE km = '%@' AND user_id = '%@'", RQ_COMMON_MANAGER.subjectStr, RQ_USER_MANAGER.currentUserId];
  177. BOOL resultSet = [db executeUpdate:sqlStr];
  178. if (resultSet) {
  179. NSLog(@"删除成功");
  180. self.wrongModelsCount = 0;
  181. } else {
  182. NSLog(@"删除失败");
  183. }
  184. if (complete) complete(resultSet);
  185. }];
  186. }
  187. }
  188. - (NSInteger)getAllWrongModelsCount {
  189. __block NSInteger count = 0;
  190. if (_databaseQueue) {
  191. [_databaseQueue inDatabase:^(FMDatabase * _Nonnull db) {
  192. FMResultSet *resultSet;
  193. NSString *sqlStr = [NSString stringWithFormat:@"SELECT COUNT(*) AS countNum FROM wrongList WHERE km = '%@' AND user_id = '%@' ", RQ_COMMON_MANAGER.subjectStr, RQ_USER_MANAGER.currentUserId];
  194. resultSet = [db executeQuery:sqlStr];
  195. while ([resultSet next]) {
  196. count = [resultSet intForColumn:@"countNum"];
  197. self.wrongModelsCount = count;
  198. }
  199. }];
  200. }
  201. return count;
  202. }
  203. - (NSArray *)queryWrongModelQuestionIdArrWithSubjectType:(RQHomePageSubjectType)subjectType {
  204. NSMutableArray *arr = @[].mutableCopy;
  205. if (_databaseQueue) {
  206. [_databaseQueue inDatabase:^(FMDatabase * _Nonnull db) {
  207. FMResultSet *resultSet;
  208. 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]];
  209. while ([resultSet next]) {
  210. NSInteger questionId = [resultSet intForColumn:@"question_id"];
  211. [arr addObject:@(questionId)];
  212. }
  213. }];
  214. }
  215. return arr.copy;
  216. }
  217. - (NSArray *)queryWrongModelQuestionIdAndTimeDicArrWithSubjectType:(RQHomePageSubjectType)subjectType {
  218. NSMutableArray *arr = @[].mutableCopy;
  219. if (_databaseQueue) {
  220. [_databaseQueue inDatabase:^(FMDatabase * _Nonnull db) {
  221. FMResultSet *resultSet;
  222. 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]];
  223. while ([resultSet next]) {
  224. RQWrongModel *wrongModel = [RQWrongModel wrongModelWithFMResultSet:resultSet];
  225. NSDictionary *dic = @{
  226. @"id" : @(wrongModel.questionId),
  227. @"timestamp" : wrongModel.createTime,
  228. };
  229. [arr addObject:dic];
  230. }
  231. }];
  232. }
  233. return arr.copy;
  234. }
  235. #pragma mark - CollectionList
  236. - (void)addCollectionRecordWithRQCollectionModel:(RQCollectionModel *)collectionModel {
  237. if (_databaseQueue) {
  238. [_databaseQueue inDatabase:^(FMDatabase * _Nonnull db) {
  239. BOOL isExist;
  240. 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]];
  241. while ([isExistResultSet next]) {
  242. NSInteger count = [isExistResultSet intForColumn:@"countNum"];
  243. isExist = count > 0;
  244. if (isExist) {
  245. NSLog(@"%@",[NSString stringWithFormat:@"%ld--------数据库表存在该条数据",collectionModel.questionId]);
  246. if (RQStringIsNotEmpty(collectionModel.userId) && !RQObjectIsNil(@(collectionModel.questionId)) && RQStringIsNotEmpty(collectionModel.createTime)) {
  247. 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]];
  248. if (updateResultSet) {
  249. NSLog(@"更新数据成功");
  250. } else {
  251. NSLog(@"更新数据失败");
  252. }
  253. }
  254. } else {
  255. NSLog(@"%@",[NSString stringWithFormat:@"%ld--------数据库表不存在该条数据",collectionModel.questionId]);
  256. 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]];
  257. if (insertResultSet) {
  258. NSLog(@"插入数据成功");
  259. } else {
  260. NSLog(@"插入数据失败");
  261. }
  262. }
  263. }
  264. }];
  265. }
  266. }
  267. - (void)addCollectionRecordWithRQCollectionModelArr:(NSArray *)collectionModelArr isClearOldData:(BOOL)isClearOldData complete:(UpdateLocalDataBlock)complete {
  268. if (_databaseQueue) {
  269. [_databaseQueue inTransaction:^(FMDatabase * _Nonnull db, BOOL * _Nonnull rollback) {
  270. @try {
  271. if (isClearOldData) {
  272. NSString *deleteSqlStr = [NSString stringWithFormat:@"DELETE FROM collectionList WHERE km = '%@' AND user_id = '%@' ", RQ_COMMON_MANAGER.subjectStr, RQ_USER_MANAGER.currentUserId];
  273. BOOL resultSet = [db executeUpdate:deleteSqlStr];
  274. if (resultSet) {
  275. NSLog(@"删除成功");
  276. } else {
  277. NSLog(@"删除失败");
  278. }
  279. }
  280. [[collectionModelArr.rac_sequence.signal deliverOnMainThread] subscribeNext:^(RQCollectionModel *collectionModel) {
  281. BOOL isExist;
  282. if (RQStringIsEmpty(collectionModel.userId)) {
  283. collectionModel.userId = RQ_USER_MANAGER.currentUserId;
  284. }
  285. if (RQStringIsEmpty(collectionModel.km)) {
  286. collectionModel.km = RQ_COMMON_MANAGER.subjectStr;
  287. }
  288. if (RQStringIsEmpty(collectionModel.createTime)) {
  289. collectionModel.createTime = [NSDate rq_currentTimeSSSInterval];
  290. }
  291. 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]];
  292. while ([isExistResultSet next]) {
  293. NSInteger count = [isExistResultSet intForColumn:@"countNum"];
  294. isExist = count > 0;
  295. if (isExist) {
  296. NSLog(@"%@",[NSString stringWithFormat:@"%ld--------数据库表存在该条数据",collectionModel.questionId]);
  297. if (RQStringIsNotEmpty(collectionModel.userId) && !RQObjectIsNil(@(collectionModel.questionId)) && RQStringIsNotEmpty(collectionModel.createTime)) {
  298. 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]];
  299. if (updateResultSet) {
  300. NSLog(@"更新数据成功");
  301. } else {
  302. NSLog(@"更新数据失败");
  303. }
  304. }
  305. } else {
  306. NSLog(@"%@",[NSString stringWithFormat:@"%ld--------数据库表不存在该条数据",collectionModel.questionId]);
  307. 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]];
  308. if (insertResultSet) {
  309. NSLog(@"插入数据成功");
  310. } else {
  311. NSLog(@"插入数据失败");
  312. }
  313. }
  314. }
  315. } completed:^{
  316. if (complete) {
  317. complete(YES, nil);
  318. }
  319. }];
  320. } @catch (NSException *exception) {
  321. *rollback = YES;
  322. } @finally {
  323. *rollback = NO;
  324. }
  325. }];
  326. }
  327. }
  328. - (void)deleteCollectionRecordWithQuestionId:(NSInteger)questionId {
  329. if (_databaseQueue) {
  330. [_databaseQueue inDatabase:^(FMDatabase * _Nonnull db) {
  331. BOOL resultSet = [db executeUpdate:[NSString stringWithFormat:@"delete from collectionList where question_id =%ld AND user_id = '%@'", (long)questionId, RQ_USER_MANAGER.currentUserId]];
  332. if (resultSet) {
  333. NSLog(@"删除数据成功");
  334. } else {
  335. NSLog(@"删除数据失败");
  336. }
  337. }];
  338. }
  339. }
  340. - (BOOL)isExistWithRQCollectionModel:(RQCollectionModel *)collectionModel {
  341. __block BOOL isExist = false;
  342. if (_databaseQueue) {
  343. [_databaseQueue inDatabase:^(FMDatabase * _Nonnull db) {
  344. FMResultSet *resultSet;
  345. resultSet = [db executeQuery:[NSString stringWithFormat:@"select * from collectionList where question_id =%ld AND user_id = '%@'", (long)collectionModel.questionId, collectionModel.userId]];
  346. while ([resultSet next]) {
  347. NSInteger questionId = [resultSet intForColumn:@"question_id"];
  348. isExist = questionId == collectionModel.questionId;
  349. if (isExist) {
  350. NSLog(@"%@",[NSString stringWithFormat:@"%ld--------数据库表存在该条数据",collectionModel.questionId]);
  351. } else {
  352. NSLog(@"%@",[NSString stringWithFormat:@"%ld--------数据库表不存在该条数据",collectionModel.questionId]);
  353. }
  354. }
  355. }];
  356. }
  357. return isExist;
  358. }
  359. - (void)deleteAllCollectionModelsWithComplete:(VoidBlock_Bool)complete {
  360. if (_databaseQueue) {
  361. [_databaseQueue inDatabase:^(FMDatabase * _Nonnull db) {
  362. NSString *sqlStr = [NSString stringWithFormat:@"DELETE FROM collectionList WHERE km = '%@' AND user_id = '%@'", RQ_COMMON_MANAGER.subjectStr, RQ_USER_MANAGER.currentUserId];
  363. BOOL resultSet = [db executeUpdate:sqlStr];
  364. if (resultSet) {
  365. NSLog(@"删除成功");
  366. self.collectionModelsCount = 0;
  367. } else {
  368. NSLog(@"删除失败");
  369. }
  370. if (complete) complete(resultSet);
  371. }];
  372. }
  373. }
  374. - (NSInteger)getAllCollectionModelsCount {
  375. __block NSInteger count = 0;
  376. if (_databaseQueue) {
  377. [_databaseQueue inDatabase:^(FMDatabase * _Nonnull db) {
  378. FMResultSet *resultSet;
  379. NSString *sqlStr = [NSString stringWithFormat:@"SELECT COUNT(*) AS countNum FROM collectionList WHERE km = '%@' AND user_id = '%@'", RQ_COMMON_MANAGER.subjectStr, RQ_USER_MANAGER.currentUserId];
  380. resultSet = [db executeQuery:sqlStr];
  381. while ([resultSet next]) {
  382. count = [resultSet intForColumn:@"countNum"];
  383. self.collectionModelsCount = count;
  384. }
  385. }];
  386. }
  387. return count;
  388. }
  389. - (NSArray *)queryCollectionModelQuestionIdArrWithSubjectType:(RQHomePageSubjectType)subjectType {
  390. NSMutableArray *arr = @[].mutableCopy;
  391. if (_databaseQueue) {
  392. [_databaseQueue inDatabase:^(FMDatabase * _Nonnull db) {
  393. FMResultSet *resultSet;
  394. 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]];
  395. while ([resultSet next]) {
  396. NSInteger questionId = [resultSet intForColumn:@"question_id"];
  397. [arr addObject:@(questionId)];
  398. }
  399. }];
  400. }
  401. return arr.copy;
  402. }
  403. - (NSArray *)queryCollectionModelQuestionIdAndTimeDicArrWithSubjectType:(RQHomePageSubjectType)subjectType {
  404. NSMutableArray *arr = @[].mutableCopy;
  405. if (_databaseQueue) {
  406. [_databaseQueue inDatabase:^(FMDatabase * _Nonnull db) {
  407. FMResultSet *resultSet;
  408. 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]];
  409. while ([resultSet next]) {
  410. RQCollectionModel *collectionModel = [RQCollectionModel collectionModelWithFMResultSet:resultSet];
  411. NSDictionary *dic = @{
  412. @"id" : @(collectionModel.questionId),
  413. @"timestamp" : collectionModel.createTime,
  414. };
  415. [arr addObject:dic];
  416. }
  417. }];
  418. }
  419. return arr.copy;
  420. }
  421. #pragma mark - CommonList
  422. - (NSInteger)getExerciseFontSize {
  423. if (_databaseQueue) {
  424. [_databaseQueue inDatabase:^(FMDatabase * _Nonnull db) {
  425. FMResultSet *resultSet;
  426. resultSet = [db executeQuery:[NSString stringWithFormat:@"select exercise_fontsize from commonList where user_id = '%@'", RQ_USER_MANAGER.currentUserId]];
  427. while ([resultSet next]) {
  428. _exerciseFontSize = [resultSet intForColumn:@"exercise_fontsize"];
  429. }
  430. }];
  431. }
  432. return _exerciseFontSize;
  433. }
  434. - (void)updateExerciseFontSizeWithFontSize:(NSInteger)fontSize {
  435. if (_databaseQueue) {
  436. [_databaseQueue inTransaction:^(FMDatabase * _Nonnull db, BOOL * _Nonnull rollback) {
  437. @try {
  438. BOOL isExist;
  439. FMResultSet *isExistResultSet = [db executeQuery:[NSString stringWithFormat:@"select COUNT(user_id) AS countNum from commonList where user_id = '%@'", RQ_USER_MANAGER.currentUserId]];
  440. while ([isExistResultSet next]) {
  441. NSInteger count = [isExistResultSet intForColumn:@"countNum"];
  442. isExist = count > 0;
  443. if (isExist) {
  444. NSString *sql = [NSString stringWithFormat:@"update commonList set exercise_fontsize = %ld where user_id = '%@'", (long)fontSize,RQ_USER_MANAGER.currentUserId];
  445. BOOL updateResultSet = [db executeUpdate:sql];
  446. if (updateResultSet) {
  447. NSLog(@"更新数据成功");
  448. } else {
  449. NSLog(@"更新数据失败");
  450. }
  451. } else {
  452. 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];
  453. BOOL insertResultSet = [db executeUpdate:sql];
  454. if (insertResultSet) {
  455. NSLog(@"插入数据成功");
  456. } else {
  457. NSLog(@"插入数据失败");
  458. }
  459. }
  460. }
  461. } @catch (NSException *exception) {
  462. *rollback = YES;
  463. } @finally {
  464. *rollback = NO;
  465. }
  466. }];
  467. }
  468. }
  469. - (BOOL)getRightAutoJumpToNext {
  470. if (_databaseQueue) {
  471. [_databaseQueue inDatabase:^(FMDatabase * _Nonnull db) {
  472. FMResultSet *resultSet;
  473. resultSet = [db executeQuery:[NSString stringWithFormat:@"select right_auto_jump_to_next from commonList where user_id = '%@'", RQ_USER_MANAGER.currentUserId]];
  474. while ([resultSet next]) {
  475. _rightAutoJumpToNext = [resultSet boolForColumn:@"right_auto_jump_to_next"];
  476. if (_rightAutoJumpToNext) {
  477. NSLog(@"自动跳转");
  478. } else {
  479. NSLog(@"不自动跳转");
  480. }
  481. }
  482. }];
  483. }
  484. return _rightAutoJumpToNext;
  485. }
  486. - (void)updateRightAutoJumpToNextWithValue:(NSInteger)value {
  487. if (_databaseQueue) {
  488. [_databaseQueue inTransaction:^(FMDatabase * _Nonnull db, BOOL * _Nonnull rollback) {
  489. @try {
  490. BOOL isExist;
  491. FMResultSet *isExistResultSet = [db executeQuery:[NSString stringWithFormat:@"select COUNT(user_id) AS countNum from commonList where user_id = '%@'", RQ_USER_MANAGER.currentUserId]];
  492. while ([isExistResultSet next]) {
  493. NSInteger count = [isExistResultSet intForColumn:@"countNum"];
  494. isExist = count > 0;
  495. if (isExist) {
  496. BOOL updateResultSet = [db executeUpdate:[NSString stringWithFormat:@"update commonList set right_auto_jump_to_next = %ld where user_id = '%@'", (long)value,RQ_USER_MANAGER.currentUserId]];
  497. if (updateResultSet) {
  498. NSLog(@"更新数据成功");
  499. } else {
  500. NSLog(@"更新数据失败");
  501. }
  502. } else {
  503. 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]];
  504. if (insertResultSet) {
  505. NSLog(@"插入数据成功");
  506. } else {
  507. NSLog(@"插入数据失败");
  508. }
  509. }
  510. }
  511. } @catch (NSException *exception) {
  512. *rollback = YES;
  513. } @finally {
  514. *rollback = NO;
  515. }
  516. }];
  517. }
  518. }
  519. - (BOOL)getExerciseSound {
  520. if (_databaseQueue) {
  521. [_databaseQueue inDatabase:^(FMDatabase * _Nonnull db) {
  522. FMResultSet *resultSet;
  523. resultSet = [db executeQuery:[NSString stringWithFormat:@"select exercise_sound from commonList where user_id = '%@'", RQ_USER_MANAGER.currentUserId]];
  524. while ([resultSet next]) {
  525. _exerciseSound = [resultSet boolForColumn:@"exercise_sound"];
  526. if (_exerciseSound) {
  527. NSLog(@"播放声音");
  528. } else {
  529. NSLog(@"不播放声音");
  530. }
  531. }
  532. }];
  533. }
  534. return _exerciseSound;
  535. }
  536. - (void)updateExerciseSoundWithValue:(NSInteger)value {
  537. if (_databaseQueue) {
  538. [_databaseQueue inTransaction:^(FMDatabase * _Nonnull db, BOOL * _Nonnull rollback) {
  539. @try {
  540. BOOL isExist;
  541. FMResultSet *isExistResultSet = [db executeQuery:[NSString stringWithFormat:@"select COUNT(user_id) AS countNum from commonList where user_id = '%@'", RQ_USER_MANAGER.currentUserId]];
  542. while ([isExistResultSet next]) {
  543. NSInteger count = [isExistResultSet intForColumn:@"countNum"];
  544. isExist = count > 0;
  545. if (isExist) {
  546. BOOL updateResultSet = [db executeUpdate:[NSString stringWithFormat:@"update commonList set exercise_sound = %ld where user_id = '%@'", (long)value,RQ_USER_MANAGER.currentUserId]];
  547. if (updateResultSet) {
  548. NSLog(@"更新数据成功");
  549. } else {
  550. NSLog(@"更新数据失败");
  551. }
  552. } else {
  553. 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]];
  554. if (insertResultSet) {
  555. NSLog(@"插入数据成功");
  556. } else {
  557. NSLog(@"插入数据失败");
  558. }
  559. }
  560. }
  561. } @catch (NSException *exception) {
  562. *rollback = YES;
  563. } @finally {
  564. *rollback = NO;
  565. }
  566. }];
  567. }
  568. }
  569. #pragma mark - HistoryList
  570. - (void)addHistoryRecordWithRQHistoryModel:(RQHistoryModel *)historyModel {
  571. if (_databaseQueue) {
  572. [_databaseQueue inDatabase:^(FMDatabase * _Nonnull db) {
  573. BOOL isExist;
  574. 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]];
  575. while ([isExistResultSet next]) {
  576. NSInteger count = [isExistResultSet intForColumn:@"countNum"];
  577. isExist = count > 0;
  578. if (isExist) {
  579. NSLog(@"%@",[NSString stringWithFormat:@"%ld--------数据库表存在该条数据",historyModel.questionId]);
  580. NSString *setSql = [NSString stringWithFormat:@"question_id = %ld", (long)historyModel.questionId];
  581. 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];
  582. NSString *sql = [NSString stringWithFormat:@"update historyList set %@ where %@", setSql, whereSql];
  583. BOOL updateResultSet = [db executeUpdate:sql];
  584. if (updateResultSet) {
  585. NSLog(@"更新数据成功");
  586. } else {
  587. NSLog(@"更新数据失败");
  588. }
  589. } else {
  590. NSLog(@"%@",[NSString stringWithFormat:@"%ld--------数据库表不存在该条数据",historyModel.questionId]);
  591. 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]];
  592. if (insertResultSet) {
  593. NSLog(@"插入数据成功");
  594. } else {
  595. NSLog(@"插入数据失败");
  596. }
  597. }
  598. }
  599. }];
  600. }
  601. }
  602. - (RQHistoryModel *)queryHistoryModelWithCarType:(RQHomePageCarType)carType subjectType:(RQHomePageSubjectType)subjectType homeSubPageTyp:(RQHomeSubPageType)homeSubPageType titleStr:(NSString *)titleStr {
  603. __block RQHistoryModel *historyModel;
  604. NSString *seque_issue_name = @"";
  605. NSString *class_issue_name = @"";
  606. NSString *place_issue_name = @"";
  607. NSString *excell_issue_name = @"";
  608. switch (homeSubPageType) {
  609. case RQHomeSubPageType_SequentialPractice:
  610. seque_issue_name = titleStr;
  611. break;
  612. case RQHomeSubPageType_LocalTopics:
  613. place_issue_name = titleStr;
  614. break;
  615. case RQHomeSubPageType_ClassificationExercise:
  616. class_issue_name = titleStr;
  617. break;
  618. case RQHomeSubPageType_SelectedTestQuestions:
  619. excell_issue_name = titleStr;
  620. break;
  621. default:
  622. break;
  623. }
  624. if (_databaseQueue) {
  625. [_databaseQueue inDatabase:^(FMDatabase * _Nonnull db) {
  626. FMResultSet *resultSet;
  627. 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]];
  628. while ([resultSet next]) {
  629. historyModel = [RQHistoryModel historyModelWithFMResultSet:resultSet];
  630. }
  631. }];
  632. }
  633. return historyModel;
  634. }
  635. - (void)refreshWrongAndCollectCount {
  636. [self getAllWrongModelsCount];
  637. [self getAllCollectionModelsCount];
  638. }
  639. - (void)refreshWrongCount {
  640. [self getAllWrongModelsCount];
  641. }
  642. - (void)refreshCollectCount {
  643. [self getAllCollectionModelsCount];
  644. }
  645. @end