SDJKDBManager.m 39 KB

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