DataBase.m 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394
  1. //
  2. // DataBase.m
  3. // APPBaseDemo
  4. //
  5. // Created by 翟玉磊 on 16/5/4.
  6. // Copyright © 2016年 翟玉磊. All rights reserved.
  7. //
  8. #import "DataBase.h"
  9. #import "CoreDataManager.h"
  10. @implementation DataBase
  11. //当要做表数据插入的时候,调用
  12. +(NSManagedObject *)getEntityWithName:(NSString *)name{
  13. NSManagedObject *enti = [NSEntityDescription insertNewObjectForEntityForName:name inManagedObjectContext:MCoreDataManager.managedObjectContext];
  14. return enti;
  15. }
  16. //检索表数据
  17. +(NSArray *)searchDatabaseWithEntityName:(NSString *)name{
  18. NSEntityDescription *entityDescription = [NSEntityDescription
  19. entityForName:name
  20. inManagedObjectContext:MCoreDataManager.managedObjectContext];
  21. NSFetchRequest *request = [[NSFetchRequest alloc] init];
  22. [request setEntity:entityDescription];
  23. NSError *error;
  24. NSArray *objects;
  25. @try {
  26. objects = [MCoreDataManager.managedObjectContext executeFetchRequest:request error:&error];
  27. } @catch (NSException *exception) {
  28. objects = [NSArray array];
  29. NSLog(@"searchDatabaseWithEntityName数据库查询异常:%@", exception.reason);
  30. } @finally {
  31. return objects;
  32. }
  33. }
  34. +(NSArray *)searchDatabaseWithEntityName:(NSString *)name sortDescriptors:(NSArray *)sortDescriptors{
  35. NSEntityDescription *entityDescription = [NSEntityDescription
  36. entityForName:name
  37. inManagedObjectContext:MCoreDataManager.managedObjectContext];
  38. NSFetchRequest *request = [[NSFetchRequest alloc] init];
  39. [request setEntity:entityDescription];
  40. [request setSortDescriptors:sortDescriptors];
  41. NSError *error;
  42. NSArray *objects;
  43. @try {
  44. objects = [MCoreDataManager.managedObjectContext executeFetchRequest:request error:&error];
  45. } @catch (NSException *exception) {
  46. objects = [NSArray array];
  47. NSLog(@"searchDatabaseWithEntityName数据库查询异常:%@", exception.reason);
  48. } @finally {
  49. return objects;
  50. }
  51. }
  52. + (NSArray*)getSystemMessageTypes:(NSArray*)types IsRead:(BOOL)read {
  53. return [DataBase getSystemMessageTypes:types IsRead:read sortAscending:NO];
  54. }
  55. /// 获取指定类型的系统消息
  56. /// @param types 消息类型
  57. /// @param read 是否只获取未读消息 YES只返回未读消息 NO全部返回
  58. /// @param ascending 是否升序
  59. + (NSArray*)getSystemMessageTypes:(NSArray*)types IsRead:(BOOL)read sortAscending:(BOOL)ascending {
  60. NSEntityDescription* entityDescription = [NSEntityDescription entityForName:@"Notification" inManagedObjectContext:MCoreDataManager.managedObjectContext];
  61. NSSortDescriptor* descriptor = [NSSortDescriptor sortDescriptorWithKey:@"time" ascending:ascending];
  62. NSMutableString* format = [NSMutableString stringWithFormat:@"messageType IN {'%@'", types[0]];
  63. for (int i = 1; i < [types count]; i++) {
  64. [format appendFormat:@",'%@'", types[i]];
  65. }
  66. [format appendFormat:@"}"];
  67. NSMutableArray* predicates = [NSMutableArray array];
  68. NSPredicate *predicate1 = [NSPredicate predicateWithFormat:format];
  69. NSPredicate *predicate2 = [NSPredicate predicateWithFormat:@"user==%@", [DataBase nowUser]];
  70. [predicates addObject:predicate1];
  71. [predicates addObject:predicate2];
  72. NSPredicate *predicate3 = nil;
  73. if (read) {
  74. predicate3 = [NSPredicate predicateWithFormat:@"readStatus==1"]; //未读
  75. [predicates addObject:predicate3];
  76. }
  77. NSPredicate *andCompoundPredicate =[NSCompoundPredicate andPredicateWithSubpredicates:[predicates copy]];
  78. NSFetchRequest* request = [NSFetchRequest new];
  79. [request setPredicate:andCompoundPredicate];
  80. [request setEntity:entityDescription];
  81. [request setSortDescriptors:@[descriptor]];
  82. [request setIncludesSubentities:YES];
  83. [request setReturnsObjectsAsFaults:NO];
  84. NSError* error;
  85. NSArray* array;
  86. @try {
  87. array = [MCoreDataManager.managedObjectContext executeFetchRequest:request error:&error];
  88. } @catch (NSException *exception) {
  89. array = [NSArray array];
  90. NSLog(@"getSystemMessageTypes数据库查询异常:%@", exception.reason);
  91. } @finally {
  92. return array;
  93. }
  94. }
  95. + (void)deleteEntity:(NSManagedObject *)aEntity
  96. {
  97. // 删除实体
  98. [MCoreDataManager.managedObjectContext deleteObject:aEntity];
  99. // 保存数据,持久化存储
  100. NSError *error =nil;
  101. if (![MCoreDataManager.managedObjectContext save:&error]) {
  102. //handle error
  103. }
  104. }
  105. //提交数据库更改
  106. + (void)commitDataBase{
  107. NSError *error;
  108. @try {
  109. [[MCoreDataManager getCurrentPersistentContainer].persistentStoreCoordinator lock];
  110. if ([MCoreDataManager.managedObjectContext hasChanges] && ![MCoreDataManager.managedObjectContext save:&error]) {
  111. NSLog(@"Unresolved error %@, %@", error, [error userInfo]);
  112. abort();
  113. }
  114. [[MCoreDataManager getCurrentPersistentContainer].persistentStoreCoordinator unlock];
  115. }
  116. @catch (NSException *exception) {
  117. NSLog(@"commitDatabase exception %@",exception);
  118. }
  119. @finally {
  120. }
  121. }
  122. + (User*)isUser:(NSString *)userId {
  123. NSArray *users = [DataBase searchDatabaseWithEntityName:@"User"];
  124. for (User *user in users) {
  125. if ([user.userId isEqualToString:userId]) {
  126. return user;
  127. break;
  128. }
  129. }
  130. return nil;
  131. }
  132. + (User *)nowUser {
  133. NSArray *users = [DataBase searchDatabaseWithEntityName:@"User"];
  134. if (users != nil) {
  135. for (User *user in users) {
  136. if ([user isKindOfClass:User.class]) {
  137. if (user.loginStatus == 1) {
  138. return (User *)user;
  139. break;
  140. }
  141. }
  142. }
  143. }
  144. return nil;
  145. }
  146. + (User *)lastUser {
  147. NSSortDescriptor *descriptor = [NSSortDescriptor sortDescriptorWithKey:@"lastLoginTime" ascending:YES];
  148. NSArray *users = [DataBase searchDatabaseWithEntityName:@"User" sortDescriptors:@[descriptor]];
  149. if (users.count > 0) {
  150. return [users lastObject];
  151. }
  152. return nil;
  153. }
  154. /*
  155. 获取最后一条系统消息文本
  156. */
  157. + (NSString *)getLastSystemMessageText {
  158. NSArray *systems = [DataBase getSystemMessageTypes:@[SYSTEM_MSG] IsRead:NO];
  159. if (systems.count > 0) {
  160. Notification *system = [systems firstObject];
  161. return system.content;
  162. }
  163. return kLocalizedString(@"暂无消息");
  164. }
  165. /*
  166. 获取最后一条系统消息接收时间
  167. */
  168. + (NSString *)getLastSystemMessageTime {
  169. NSArray *systems = [DataBase getSystemMessageTypes:@[SYSTEM_MSG] IsRead:NO];
  170. if (systems.count > 0) {
  171. Notification *system = [systems firstObject];
  172. return system.time;
  173. }
  174. return @"0";
  175. }
  176. /*
  177. 获取系统消息未读数量
  178. */
  179. + (NSInteger)getSystemMessageUnreadNum {
  180. NSArray *systems = [DataBase getSystemMessageTypes:@[SYSTEM_MSG] IsRead:YES];
  181. return systems.count;
  182. }
  183. /*
  184. 获取所有系统消息
  185. */
  186. + (NSArray *)getAllSystemMessage {
  187. NSArray *systems = [DataBase getSystemMessageTypes:@[SYSTEM_MSG] IsRead:NO sortAscending:NO];
  188. return systems;
  189. }
  190. /*
  191. 全部系统消息设置为已读
  192. */
  193. + (void)setupAllSystemMessageRead {
  194. NSArray *systems = [DataBase getSystemMessageTypes:@[SYSTEM_MSG] IsRead:YES];
  195. for (Notification *system in systems) {
  196. system.readStatus = 0;
  197. }
  198. [DataBase commitDataBase];
  199. }
  200. + (Music *)isMusic:(NSString *)musicId {
  201. NSArray *musics = [DataBase searchDatabaseWithEntityName:@"Music"];
  202. for (Music *music in musics) {
  203. if ([music.musicId isEqualToString:musicId]) {
  204. return music;
  205. break;
  206. }
  207. }
  208. return nil;
  209. }
  210. /// 获取音乐列表
  211. /// @param play YES则是获取播放列表
  212. + (NSArray *)getLocalMusicListWithPlay:(BOOL)play {
  213. NSEntityDescription* entityDescription = [NSEntityDescription entityForName:@"Music" inManagedObjectContext:MCoreDataManager.managedObjectContext];
  214. NSSortDescriptor* descriptor = [NSSortDescriptor sortDescriptorWithKey:play?@"addTime":@"downloadTime" ascending:NO];
  215. NSMutableArray* predicates = [NSMutableArray array];
  216. if (play) {
  217. NSPredicate *predicate = [NSPredicate predicateWithFormat:@"isSelected==YES"];
  218. [predicates addObject:predicate];
  219. }
  220. NSPredicate *andCompoundPredicate =[NSCompoundPredicate andPredicateWithSubpredicates:[predicates copy]];
  221. NSFetchRequest* request = [NSFetchRequest new];
  222. [request setPredicate:andCompoundPredicate];
  223. [request setEntity:entityDescription];
  224. [request setSortDescriptors:@[descriptor]];
  225. [request setIncludesSubentities:YES];
  226. [request setReturnsObjectsAsFaults:NO];
  227. NSError* error;
  228. NSArray* array = [MCoreDataManager.managedObjectContext executeFetchRequest:request error:&error];
  229. return array;
  230. }
  231. #pragma mark — 好友
  232. /*
  233. * 根据userid和消息类型获取指定发送者的消息体
  234. */
  235. + (Notification *)isFriendNotification:(NSString *)userId types:(NSArray *)types {
  236. NSArray *notifications = [DataBase getSystemMessageTypes:types IsRead:NO];
  237. for (Notification *notification in notifications) {
  238. if ([notification.userId isEqualToString:userId]) {
  239. return notification;
  240. break;
  241. }
  242. }
  243. return nil;
  244. }
  245. /*
  246. 获取最后一条好友申请消息文本
  247. */
  248. + (NSString *)getLastFriendApplyText {
  249. NSArray *friends = [DataBase getSystemMessageTypes:@[FRIEND_ADD_REQUEST, FRIEND_ADD_AGREE, FRIEND_ADD_DENY] IsRead:NO];
  250. if (friends.count > 0) {
  251. Notification *friend = [friends firstObject];
  252. return friend.content;
  253. }
  254. return kLocalizedString(@"暂无消息");
  255. }
  256. /*
  257. 获取好友申请未读消息数量
  258. */
  259. + (NSInteger)getFriendApplyUnreadNum {
  260. NSArray *friends = [DataBase getSystemMessageTypes:@[FRIEND_ADD_REQUEST, FRIEND_ADD_AGREE, FRIEND_ADD_DENY] IsRead:YES];
  261. return friends.count;
  262. }
  263. /*
  264. 获取所有申请好友消息
  265. */
  266. + (NSArray *)getAllFriendApplyMessage {
  267. NSArray *friends = [DataBase getSystemMessageTypes:@[FRIEND_ADD_REQUEST, FRIEND_ADD_AGREE, FRIEND_ADD_DENY] IsRead:NO];
  268. return friends;
  269. }
  270. /*
  271. 好友申请全部设置为已读
  272. */
  273. + (void)setupAllFriendApplyMessageRead {
  274. NSArray *friends = [DataBase getSystemMessageTypes:@[FRIEND_ADD_REQUEST, FRIEND_ADD_AGREE, FRIEND_ADD_DENY] IsRead:YES];
  275. for (Notification *friend in friends) {
  276. friend.readStatus = 0;
  277. }
  278. [DataBase commitDataBase];
  279. }
  280. #pragma mark — 关注消息
  281. /*
  282. 获取指定类型最后一条消息文本
  283. */
  284. + (NSString *)getLastMessageWithTypes:(NSArray *)types {
  285. NSArray *messages = [DataBase getSystemMessageTypes:types IsRead:NO];
  286. if (messages.count > 0) {
  287. Notification *message = [messages firstObject];
  288. return message.content;
  289. }
  290. return kLocalizedString(@"暂无消息");
  291. }
  292. /*
  293. 获取最后一条关注消息接收时间
  294. */
  295. + (NSString *)getLastfollowMessageTime {
  296. NSArray *messages = [DataBase getSystemMessageTypes:@[USER_FOLLOW] IsRead:NO];
  297. if (messages.count > 0) {
  298. Notification *message = [messages firstObject];
  299. return message.time;
  300. }
  301. return @"0";
  302. }
  303. /*
  304. 获取最后一条关注消息文本
  305. */
  306. + (NSString *)getLastFollowMessageText {
  307. return [DataBase getLastMessageWithTypes:@[USER_FOLLOW]];
  308. }
  309. /*
  310. 获取关注消息未读数量
  311. */
  312. + (NSInteger)getFollowMessageUnreadNum {
  313. NSArray *friends = [DataBase getSystemMessageTypes:@[USER_FOLLOW] IsRead:YES];
  314. return friends.count;
  315. }
  316. /*
  317. 获取所有关注消息
  318. */
  319. + (NSArray *)getAllFollowMessage {
  320. NSArray *friends = [DataBase getSystemMessageTypes:@[USER_FOLLOW] IsRead:NO];
  321. return friends;
  322. }
  323. /*
  324. 关注消息全部设置为已读
  325. */
  326. + (void)setupAllFollowMessageRead {
  327. NSArray *friends = [DataBase getSystemMessageTypes:@[USER_FOLLOW] IsRead:YES];
  328. for (Notification *friend in friends) {
  329. friend.readStatus = 0;
  330. }
  331. [DataBase commitDataBase];
  332. }
  333. @end