MMDraftModel.m 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112
  1. //
  2. // MMDraftModel.m
  3. // RichTextEditDemo
  4. //
  5. // Created by aron on 2017/7/25.
  6. // Copyright © 2017年 aron. All rights reserved.
  7. //
  8. #import "MMDraftModel.h"
  9. #import "MMDatabaseConn.h"
  10. #import <YYModel.h>
  11. #import "MMDraftUtil.h"
  12. #import "UtilMacro.h"
  13. #import "MMRichImageModel.h"
  14. static NSString* draft_tableName = @"t_draft";
  15. @implementation MMDraftModel
  16. - (void)setContentModels:(NSArray<MMBaseRichContentModel *> *)contentModels {
  17. for (MMBaseRichContentModel* obj in contentModels) {
  18. if ([obj isKindOfClass:[MMRichImageModel class]]) {
  19. MMRichImageModel* imageModel = (MMRichImageModel*)obj;
  20. if (imageModel.remoteImageUrlString != nil
  21. && imageModel.remoteImageUrlString.length > 0) {
  22. imageModel.isFailed = NO;
  23. imageModel.isDone = YES;
  24. } else {
  25. imageModel.isFailed = YES;
  26. imageModel.isDone = NO;
  27. }
  28. }
  29. }
  30. _contentModels = contentModels;
  31. }
  32. /**
  33. * 创建表
  34. */
  35. + (void)createTableIfNotExists {
  36. NSString* sql = [NSString stringWithFormat:@"CREATE TABLE IF NOT EXISTS %@ (draftID INTEGER, userId TEXT, tid TEXT, contentJsonString TEXT, createTimeString TEXT, modifyTimeString TEXT, PRIMARY KEY(draftID));", draft_tableName];
  37. [[MMDatabaseConn sharedInstance].databaseQueue inDatabase:^(FMDatabase *db) {
  38. [db executeUpdate:sql];
  39. }];
  40. }
  41. /**
  42. 获取草稿
  43. */
  44. + (void)retriveDraftWithCompletion:(void (^)(NSArray *aDrafts, NSError *aError))aCompletionBlock {
  45. NSMutableArray *results = [NSMutableArray array];
  46. NSMutableString* sql = [NSMutableString stringWithFormat:@"SELECT * FROM %@ WHERE 1=1", draft_tableName];
  47. [sql appendFormat:@" AND userId = '%@'", @"TEST_USER_ID"];
  48. [sql appendString:@" ORDER BY modifyTimeString DESC"];
  49. [[MMDatabaseConn sharedInstance].databaseQueue inDatabase:^(FMDatabase *db) {
  50. FMResultSet *set = [db executeQuery:sql];
  51. while (set.next) {
  52. NSString* contentJsonString = [set objectForColumnName:@"contentJsonString"];
  53. contentJsonString = [contentJsonString stringByReplacingOccurrencesOfString:@"''" withString:@"'"];
  54. MMDraftModel* draftData = [MMDraftUtil draftModelWithDraftDataString:contentJsonString];
  55. if (draftData) {
  56. [results addObject:draftData];
  57. }
  58. }
  59. }];
  60. !aCompletionBlock ?: aCompletionBlock(results, nil);
  61. }
  62. /**
  63. 插入一条草稿
  64. */
  65. + (void)insertDraft:(MMDraftModel*)draft
  66. error:(NSError **)pError {
  67. NSString* contentJsonString = [draft yy_modelToJSONString];
  68. contentJsonString = [contentJsonString stringByReplacingOccurrencesOfString:@"'" withString:@"''"];
  69. NSMutableString* sql = [NSMutableString stringWithFormat:@"REPLACE INTO %@ (draftID, userId, tid, contentJsonString, createTimeString, modifyTimeString) VALUES (%@, '%@', '%@', '%@', '%@', '%@')", draft_tableName,
  70. @(draft.draftId),
  71. ValueOrEmpty(draft.userId),
  72. ValueOrEmpty(draft.tid),
  73. ValueOrEmpty(contentJsonString),
  74. ValueOrEmpty(draft.createTimeString),
  75. ValueOrEmpty(draft.modifyTimeString)];
  76. [self.class doUpdateWithSql:sql error:pError];
  77. }
  78. /**
  79. 删除草稿
  80. */
  81. + (void)deleteDraft:(MMDraftModel*)draft
  82. error:(NSError **)pError {
  83. NSMutableString* sql = [NSMutableString stringWithFormat:@"DELETE FROM %@ WHERE 1=1 AND draftID = %@", draft_tableName, @(draft.draftId)];
  84. [self.class doUpdateWithSql:sql error:pError];
  85. }
  86. #pragma mark - ......::::::: helper :::::::......
  87. + (void)doUpdateWithSql:(NSString* )sql error:(NSError **)pError {
  88. __block BOOL result = NO;
  89. [[MMDatabaseConn sharedInstance].databaseQueue inDatabase:^(FMDatabase *db) {
  90. result = [db executeUpdate:sql];
  91. }];
  92. if (result == NO && pError) {
  93. *pError = [NSError errorWithDomain:@"" code:1 userInfo:@{}];
  94. }
  95. }
  96. @end