123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394 |
- //
- // DataBase.m
- // APPBaseDemo
- //
- // Created by 翟玉磊 on 16/5/4.
- // Copyright © 2016年 翟玉磊. All rights reserved.
- //
- #import "DataBase.h"
- #import "CoreDataManager.h"
- @implementation DataBase
- //当要做表数据插入的时候,调用
- +(NSManagedObject *)getEntityWithName:(NSString *)name{
- NSManagedObject *enti = [NSEntityDescription insertNewObjectForEntityForName:name inManagedObjectContext:MCoreDataManager.managedObjectContext];
- return enti;
- }
- //检索表数据
- +(NSArray *)searchDatabaseWithEntityName:(NSString *)name{
-
- NSEntityDescription *entityDescription = [NSEntityDescription
- entityForName:name
- inManagedObjectContext:MCoreDataManager.managedObjectContext];
- NSFetchRequest *request = [[NSFetchRequest alloc] init];
- [request setEntity:entityDescription];
-
- NSError *error;
- NSArray *objects;
- @try {
- objects = [MCoreDataManager.managedObjectContext executeFetchRequest:request error:&error];
- } @catch (NSException *exception) {
- objects = [NSArray array];
- NSLog(@"searchDatabaseWithEntityName数据库查询异常:%@", exception.reason);
- } @finally {
- return objects;
- }
- }
- +(NSArray *)searchDatabaseWithEntityName:(NSString *)name sortDescriptors:(NSArray *)sortDescriptors{
-
- NSEntityDescription *entityDescription = [NSEntityDescription
- entityForName:name
- inManagedObjectContext:MCoreDataManager.managedObjectContext];
- NSFetchRequest *request = [[NSFetchRequest alloc] init];
- [request setEntity:entityDescription];
- [request setSortDescriptors:sortDescriptors];
-
- NSError *error;
- NSArray *objects;
- @try {
- objects = [MCoreDataManager.managedObjectContext executeFetchRequest:request error:&error];
- } @catch (NSException *exception) {
- objects = [NSArray array];
- NSLog(@"searchDatabaseWithEntityName数据库查询异常:%@", exception.reason);
- } @finally {
- return objects;
- }
- }
- + (NSArray*)getSystemMessageTypes:(NSArray*)types IsRead:(BOOL)read {
- return [DataBase getSystemMessageTypes:types IsRead:read sortAscending:NO];
- }
- /// 获取指定类型的系统消息
- /// @param types 消息类型
- /// @param read 是否只获取未读消息 YES只返回未读消息 NO全部返回
- /// @param ascending 是否升序
- + (NSArray*)getSystemMessageTypes:(NSArray*)types IsRead:(BOOL)read sortAscending:(BOOL)ascending {
-
- NSEntityDescription* entityDescription = [NSEntityDescription entityForName:@"Notification" inManagedObjectContext:MCoreDataManager.managedObjectContext];
- NSSortDescriptor* descriptor = [NSSortDescriptor sortDescriptorWithKey:@"time" ascending:ascending];
-
- NSMutableString* format = [NSMutableString stringWithFormat:@"messageType IN {'%@'", types[0]];
- for (int i = 1; i < [types count]; i++) {
- [format appendFormat:@",'%@'", types[i]];
- }
- [format appendFormat:@"}"];
-
- NSMutableArray* predicates = [NSMutableArray array];
- NSPredicate *predicate1 = [NSPredicate predicateWithFormat:format];
- NSPredicate *predicate2 = [NSPredicate predicateWithFormat:@"user==%@", [DataBase nowUser]];
- [predicates addObject:predicate1];
- [predicates addObject:predicate2];
- NSPredicate *predicate3 = nil;
- if (read) {
- predicate3 = [NSPredicate predicateWithFormat:@"readStatus==1"]; //未读
- [predicates addObject:predicate3];
- }
- NSPredicate *andCompoundPredicate =[NSCompoundPredicate andPredicateWithSubpredicates:[predicates copy]];
- NSFetchRequest* request = [NSFetchRequest new];
- [request setPredicate:andCompoundPredicate];
- [request setEntity:entityDescription];
- [request setSortDescriptors:@[descriptor]];
- [request setIncludesSubentities:YES];
- [request setReturnsObjectsAsFaults:NO];
-
- NSError* error;
- NSArray* array;
- @try {
- array = [MCoreDataManager.managedObjectContext executeFetchRequest:request error:&error];
- } @catch (NSException *exception) {
- array = [NSArray array];
- NSLog(@"getSystemMessageTypes数据库查询异常:%@", exception.reason);
- } @finally {
- return array;
- }
- }
- + (void)deleteEntity:(NSManagedObject *)aEntity
- {
- // 删除实体
- [MCoreDataManager.managedObjectContext deleteObject:aEntity];
- // 保存数据,持久化存储
- NSError *error =nil;
- if (![MCoreDataManager.managedObjectContext save:&error]) {
- //handle error
- }
- }
- //提交数据库更改
- + (void)commitDataBase{
-
- NSError *error;
- @try {
- [[MCoreDataManager getCurrentPersistentContainer].persistentStoreCoordinator lock];
- if ([MCoreDataManager.managedObjectContext hasChanges] && ![MCoreDataManager.managedObjectContext save:&error]) {
-
- NSLog(@"Unresolved error %@, %@", error, [error userInfo]);
- abort();
- }
-
- [[MCoreDataManager getCurrentPersistentContainer].persistentStoreCoordinator unlock];
- }
- @catch (NSException *exception) {
- NSLog(@"commitDatabase exception %@",exception);
- }
- @finally {
- }
- }
- + (User*)isUser:(NSString *)userId {
- NSArray *users = [DataBase searchDatabaseWithEntityName:@"User"];
- for (User *user in users) {
- if ([user.userId isEqualToString:userId]) {
- return user;
- break;
- }
- }
- return nil;
- }
- + (User *)nowUser {
- NSArray *users = [DataBase searchDatabaseWithEntityName:@"User"];
- if (users != nil) {
- for (User *user in users) {
- if ([user isKindOfClass:User.class]) {
- if (user.loginStatus == 1) {
- return (User *)user;
- break;
- }
- }
- }
- }
- return nil;
- }
- + (User *)lastUser {
- NSSortDescriptor *descriptor = [NSSortDescriptor sortDescriptorWithKey:@"lastLoginTime" ascending:YES];
-
- NSArray *users = [DataBase searchDatabaseWithEntityName:@"User" sortDescriptors:@[descriptor]];
- if (users.count > 0) {
- return [users lastObject];
- }
- return nil;
- }
- /*
- 获取最后一条系统消息文本
- */
- + (NSString *)getLastSystemMessageText {
-
- NSArray *systems = [DataBase getSystemMessageTypes:@[SYSTEM_MSG] IsRead:NO];
- if (systems.count > 0) {
- Notification *system = [systems firstObject];
- return system.content;
- }
- return kLocalizedString(@"暂无消息");
- }
- /*
- 获取最后一条系统消息接收时间
- */
- + (NSString *)getLastSystemMessageTime {
-
- NSArray *systems = [DataBase getSystemMessageTypes:@[SYSTEM_MSG] IsRead:NO];
- if (systems.count > 0) {
- Notification *system = [systems firstObject];
- return system.time;
- }
- return @"0";
- }
- /*
- 获取系统消息未读数量
- */
- + (NSInteger)getSystemMessageUnreadNum {
-
- NSArray *systems = [DataBase getSystemMessageTypes:@[SYSTEM_MSG] IsRead:YES];
- return systems.count;
- }
- /*
- 获取所有系统消息
- */
- + (NSArray *)getAllSystemMessage {
-
- NSArray *systems = [DataBase getSystemMessageTypes:@[SYSTEM_MSG] IsRead:NO sortAscending:NO];
- return systems;
- }
- /*
- 全部系统消息设置为已读
- */
- + (void)setupAllSystemMessageRead {
-
- NSArray *systems = [DataBase getSystemMessageTypes:@[SYSTEM_MSG] IsRead:YES];
- for (Notification *system in systems) {
- system.readStatus = 0;
- }
- [DataBase commitDataBase];
- }
- + (Music *)isMusic:(NSString *)musicId {
- NSArray *musics = [DataBase searchDatabaseWithEntityName:@"Music"];
- for (Music *music in musics) {
- if ([music.musicId isEqualToString:musicId]) {
- return music;
- break;
- }
- }
- return nil;
- }
- /// 获取音乐列表
- /// @param play YES则是获取播放列表
- + (NSArray *)getLocalMusicListWithPlay:(BOOL)play {
-
- NSEntityDescription* entityDescription = [NSEntityDescription entityForName:@"Music" inManagedObjectContext:MCoreDataManager.managedObjectContext];
- NSSortDescriptor* descriptor = [NSSortDescriptor sortDescriptorWithKey:play?@"addTime":@"downloadTime" ascending:NO];
- NSMutableArray* predicates = [NSMutableArray array];
- if (play) {
- NSPredicate *predicate = [NSPredicate predicateWithFormat:@"isSelected==YES"];
- [predicates addObject:predicate];
- }
- NSPredicate *andCompoundPredicate =[NSCompoundPredicate andPredicateWithSubpredicates:[predicates copy]];
- NSFetchRequest* request = [NSFetchRequest new];
- [request setPredicate:andCompoundPredicate];
- [request setEntity:entityDescription];
- [request setSortDescriptors:@[descriptor]];
- [request setIncludesSubentities:YES];
- [request setReturnsObjectsAsFaults:NO];
-
- NSError* error;
- NSArray* array = [MCoreDataManager.managedObjectContext executeFetchRequest:request error:&error];
-
- return array;
- }
- #pragma mark — 好友
- /*
- * 根据userid和消息类型获取指定发送者的消息体
- */
- + (Notification *)isFriendNotification:(NSString *)userId types:(NSArray *)types {
- NSArray *notifications = [DataBase getSystemMessageTypes:types IsRead:NO];
- for (Notification *notification in notifications) {
- if ([notification.userId isEqualToString:userId]) {
- return notification;
- break;
- }
- }
- return nil;
- }
- /*
- 获取最后一条好友申请消息文本
- */
- + (NSString *)getLastFriendApplyText {
-
- NSArray *friends = [DataBase getSystemMessageTypes:@[FRIEND_ADD_REQUEST, FRIEND_ADD_AGREE, FRIEND_ADD_DENY] IsRead:NO];
- if (friends.count > 0) {
- Notification *friend = [friends firstObject];
- return friend.content;
- }
- return kLocalizedString(@"暂无消息");
- }
- /*
- 获取好友申请未读消息数量
- */
- + (NSInteger)getFriendApplyUnreadNum {
-
- NSArray *friends = [DataBase getSystemMessageTypes:@[FRIEND_ADD_REQUEST, FRIEND_ADD_AGREE, FRIEND_ADD_DENY] IsRead:YES];
- return friends.count;
- }
- /*
- 获取所有申请好友消息
- */
- + (NSArray *)getAllFriendApplyMessage {
-
- NSArray *friends = [DataBase getSystemMessageTypes:@[FRIEND_ADD_REQUEST, FRIEND_ADD_AGREE, FRIEND_ADD_DENY] IsRead:NO];
- return friends;
- }
- /*
- 好友申请全部设置为已读
- */
- + (void)setupAllFriendApplyMessageRead {
-
- NSArray *friends = [DataBase getSystemMessageTypes:@[FRIEND_ADD_REQUEST, FRIEND_ADD_AGREE, FRIEND_ADD_DENY] IsRead:YES];
- for (Notification *friend in friends) {
- friend.readStatus = 0;
- }
- [DataBase commitDataBase];
- }
- #pragma mark — 关注消息
- /*
- 获取指定类型最后一条消息文本
- */
- + (NSString *)getLastMessageWithTypes:(NSArray *)types {
-
- NSArray *messages = [DataBase getSystemMessageTypes:types IsRead:NO];
- if (messages.count > 0) {
- Notification *message = [messages firstObject];
- return message.content;
- }
- return kLocalizedString(@"暂无消息");
- }
- /*
- 获取最后一条关注消息接收时间
- */
- + (NSString *)getLastfollowMessageTime {
-
- NSArray *messages = [DataBase getSystemMessageTypes:@[USER_FOLLOW] IsRead:NO];
- if (messages.count > 0) {
- Notification *message = [messages firstObject];
- return message.time;
- }
- return @"0";
- }
- /*
- 获取最后一条关注消息文本
- */
- + (NSString *)getLastFollowMessageText {
-
- return [DataBase getLastMessageWithTypes:@[USER_FOLLOW]];
- }
- /*
- 获取关注消息未读数量
- */
- + (NSInteger)getFollowMessageUnreadNum {
-
- NSArray *friends = [DataBase getSystemMessageTypes:@[USER_FOLLOW] IsRead:YES];
- return friends.count;
- }
- /*
- 获取所有关注消息
- */
- + (NSArray *)getAllFollowMessage {
-
- NSArray *friends = [DataBase getSystemMessageTypes:@[USER_FOLLOW] IsRead:NO];
- return friends;
- }
- /*
- 关注消息全部设置为已读
- */
- + (void)setupAllFollowMessageRead {
-
- NSArray *friends = [DataBase getSystemMessageTypes:@[USER_FOLLOW] IsRead:YES];
- for (Notification *friend in friends) {
- friend.readStatus = 0;
- }
- [DataBase commitDataBase];
- }
- @end
|