// // GroupDBMgr.m // zhuxun // // Created by winsoft on 17/4/24. // // #import "GroupDBMgr.h" #import "DBOperation.h" //#import "HttpManager.h" //#import "GroupMsgRSMgrCenter.h" //#import "MessageHandleCenter.h" #define MuiltTextPicExpression @"([\\s\\S]*?)\\[image\\-src=\"([a-zA-Z0-9]{32})\"\\]([\\s\\S]*?)" #define TeamShareExpression @"\\(.*)\\<\\/TeamShare\\>" @interface GroupDBMgr() /** * addrbook */ @property (nonatomic , strong) NSMutableDictionary *allUserDict; /** * usergroupEntity */ @property (nonatomic , strong) NSMutableDictionary *allUserGroupsDict; /** * recentEntiy */ @property (nonatomic , strong) NSMutableDictionary *allUserRecentsDict; @end @implementation GroupDBMgr + (instancetype)shareInstance { static GroupDBMgr * groupDBMgr; static dispatch_once_t onceToken; dispatch_once(&onceToken, ^{ groupDBMgr = [[GroupDBMgr alloc] init]; }); return groupDBMgr; } - (NSString *)getMessageID { NSString *uuid = [[[[NSUUID UUID]UUIDString]stringByReplacingOccurrencesOfString:@"-" withString:@""]lowercaseString]; return uuid; } - (NSMutableDictionary *)allUserDict { if (!_allUserDict) { _allUserDict = [NSMutableDictionary dictionary]; } return _allUserDict; } - (NSMutableDictionary *)allUserRecentsDict { if (!_allUserRecentsDict) { _allUserRecentsDict = [NSMutableDictionary dictionary]; } return _allUserRecentsDict; } - (NSMutableDictionary *)allUserGroupsDict { if (!_allUserGroupsDict) { _allUserGroupsDict = [NSMutableDictionary dictionary]; } return _allUserGroupsDict; } //- (void)getGroupEntityWithGroupID:(NSString *)groupID resultGroupEntityBlock:(void(^)(UserGroupEntity *userGroupEntity))resultGroupEntityBlock //{ // __block UserGroupEntity *userGroupEntity = [self.allUserGroupsDict objectForKey:groupID]; // // __weak typeof(self)weakSelf = self; // // if (!userGroupEntity) { // //此类型的待移至dboperation. // userGroupEntity = [UserGroupEntity findUserGroupEntityWithGroupID:groupID]; // // if (!userGroupEntity) { // // // [[GroupMsgRSMgrCenter shareInstance]getGroupDetailWithGroupID:groupID withResultBlock:^(BOOL result, NSString *uuid, NSDictionary *message) { // // // userGroupEntity = [weakSelf createGroupEntityWithGroupParams:message[@"group"] resultBlock:^{ // // resultGroupEntityBlock(userGroupEntity); // // }]; // // // }]; // // } // } // // if (userGroupEntity) { // resultGroupEntityBlock(userGroupEntity); // } // //} //- (void)getMyAllGroupsWithResultGroupsBlock:(void(^)(NSArray *))groups //{ // NSArray *array = nil; // // if (self.allUserGroupsDict.allValues.count) { // array = self.allUserGroupsDict.allValues; // }else{ // // array = [UserGroupEntity MR_findAll]; // // // for (UserGroupEntity *userGroupEntity in array) { // // [self.allUserGroupsDict setObject:userGroupEntity forKey:userGroupEntity.iD]; // } // } // // groups(array); //} //暂时无对重复数据的过滤 //发 //- (MessageEntity *)createMessageWithUUID:(NSString *)uuid sessionID:(NSString *)sessionID content:(NSString *)content voiceLength:(NSNumber *)voiceLength localPath:(NSString *)localPath msgContentType:(int)conteType msgType:(int)msgType fileDownUpStatus:(int)fileDownUpStatus msgState:(int)msgState name:(NSString *)name time:(NSDate *)date unreadCount:(int)unreadCount resultBlock:(void(^)())resultBlock //{ // // // NSString *messageId = nil; // if (!uuid) { // messageId = [self getMessageID]; // }else messageId = uuid; // // //创建发送消息时间 // // // // // NSDate *msgTime = date; // if (!date) msgTime = [NSDate date]; // // NSString* senderID = User_Name; // // MessageEntity *msgEntity = [MessageEntity MR_createEntity]; // // msgEntity.uuid = messageId; // // msgEntity.msgType = @(msgType); // // msgEntity.msgTime = msgTime; // // msgEntity.senderId = senderID; // // msgEntity.receiverId = sessionID; // // msgEntity.sessionId = sessionID; // // //默认为upload // msgEntity.fileDownUpStatus = @(fileDownUpStatus); // //默认为doing // msgEntity.msgState = @(msgState); // // msgEntity.msgContentType = @(conteType); // // msgEntity.msgContent = content; // // msgEntity.localPath = localPath; // // msgEntity.voiceLength = voiceLength; // // msgEntity.voicePlayed = @(1); // // //注意对富文本的处理. // // [[msgEntity managedObjectContext]MR_saveWithBlock:^(NSManagedObjectContext * _Nonnull localContext) { // // if (resultBlock) { // resultBlock(); // } // }]; // //// [self updateRecent]; // [self updateRecentEntityWithMsgEntity:msgEntity unreadCount:unreadCount title:name resultBlock:^{ // // // }]; // // return msgEntity; //} //待移出数据层,此逻辑应该归入消息处理层 //收 //- (NSArray *)createSingleMessageWithJSONParams:(NSDictionary *)params resultBlock:(void(^)())resultBlock //{ // // NSString *isDes = params[@"isDes"]; // NSString *msgContent = params[@"content"]; // if (isDes.intValue) { // msgContent = [[MessageHandleCenter alloc]decryptMsgWithMsgDict:params]; // } // NSInteger status = [params[@"status"] integerValue]; // NSString *msgContentType = params[@"msgType"]; // NSString *receiver = params[@"receiver"]; // receiver = [receiver stringByReplacingOccurrencesOfString:ContentServerID(USERDEFAULT_ContentServer) withString:@""]; // NSString *sender = params[@"sender"]; // sender = [sender stringByReplacingOccurrencesOfString:ContentServerID(USERDEFAULT_ContentServer) withString:@""]; // NSString *time = params[@"time"]; // NSDate *date = [NSDate dateWithTimeIntervalSince1970:[time doubleValue]/1000]; // NSString *uuid = params[@"uuid"]; // NSString *length = params[@"length"]; //#warning 2017-05-24 接口补充. // // // // NSString *sessionID = nil; // //SESSION 先判断是否同步消息,否则为sender. // if ([receiver isEqualToString:User_Name]) // { // sessionID = sender; // // }else sessionID = receiver; // // AddrBook *addrBook = [self getAddrbookWithUerID:sessionID]; // // int unreadCount = 0; // // if (status == 0 && ![sender isEqualToString:User_Name]) { // unreadCount = 1; // } // // //先判断是否是富文本 // NSMutableArray *msgArray = [NSMutableArray array]; // // if (msgContentType.intValue == MessageTypeText) { // // __block NSArray *judgeStringArray = nil; // NSRange range = [msgContent rangeOfString:MuiltTextPicExpression options:NSRegularExpressionSearch]; // // if (range.location != NSNotFound) { // [Utils getPCScreenPhotoAndTextWithExpression:MuiltTextPicExpression content:msgContent resultBlock:^(NSArray *subData) { // judgeStringArray = subData; // }]; // // // int i = 0; // for (NSArray *onePicTextArray in judgeStringArray) { // // i++; // // NSString *textFront = onePicTextArray[1]; // NSString *imageMD5 = onePicTextArray[2]; // NSString *textBack = onePicTextArray[3]; // // NSString *childUUID= [NSString stringWithFormat:@"%@-%d",uuid,i]; // // // if (textFront.length) { // // // // MessageEntity *msgEntity = [self createMessageWithUUID:childUUID sessionID:sessionID content:textFront voiceLength:0 localPath:nil msgContentType:MessageTypeText msgType:MESSAGE_TYPE_SINGLE fileDownUpStatus:FileDownUpStatusDown msgState:MessageSendSuccess name:addrBook.disPlayName time:date unreadCount:unreadCount resultBlock:^{ // // // // }]; // // [msgArray addObject:msgEntity]; // } // // if (imageMD5.length) { // // // MessageEntity *msgEntity = [self createMessageWithUUID:childUUID sessionID:sessionID content:[NSString stringWithFormat:@"http://%@:%@/file/%@",[userDefaults objectForKey:USERDEFAULT_GROUP_STORAGE_HOST], [userDefaults objectForKey:USERDEFAULT_GROUP_STORAGE_PORT],imageMD5] voiceLength:0 localPath:nil msgContentType:MessageTypeImage msgType:MESSAGE_TYPE_SINGLE fileDownUpStatus:FileDownUpStatusDown msgState:MessageSendSuccess name:addrBook.disPlayName time:date unreadCount:unreadCount resultBlock:^{ // // // // }]; // // [msgArray addObject:msgEntity]; // } // // if (textBack.length) { // // MessageEntity *msgEntity = [self createMessageWithUUID:childUUID sessionID:sessionID content:textBack voiceLength:0 localPath:nil msgContentType:MessageTypeText msgType:MESSAGE_TYPE_SINGLE fileDownUpStatus:FileDownUpStatusDown msgState:MessageSendSuccess name:addrBook.disPlayName time:date unreadCount:unreadCount resultBlock:^{ // // // // }]; // // [msgArray addObject:msgEntity]; // } // // } // }else{ // // // MessageEntity *msgEntity = [self createMessageWithUUID:uuid sessionID:sessionID content:msgContent voiceLength:@([length doubleValue]) localPath:nil msgContentType:[msgContentType intValue] msgType:MESSAGE_TYPE_SINGLE fileDownUpStatus:FileDownUpStatusDown msgState:MessageSendSuccess name:addrBook.disPlayName time:date unreadCount:unreadCount resultBlock:^{ // // if (resultBlock) { // resultBlock(); // } // // }]; // // [msgArray addObject:msgEntity]; // // } // // // // }else{ // // // MessageEntity *msgEntity = [self createMessageWithUUID:uuid sessionID:sessionID content:msgContent voiceLength:@([length doubleValue]) localPath:nil msgContentType:[msgContentType intValue] msgType:MESSAGE_TYPE_SINGLE fileDownUpStatus:FileDownUpStatusDown msgState:MessageSendSuccess name:addrBook.disPlayName time:date unreadCount:unreadCount resultBlock:^{ // // if (resultBlock) { // resultBlock(); // } // // }]; // // [msgArray addObject:msgEntity]; // // } // // // // // // return msgArray; //} //- (NSArray *)createGroupMessageWithJSONParams:(NSDictionary *)params resultBlock:(void(^)())resultBlock //{ // NSString *uuid = params[@"id"]; // // NSNumber *voiceLength = params[@"length"]; // // NSString *content = params[@"content"]; // // if(!content) content = params[@"txt"]; // //// NSString *isDes = params[@"isDes"]; //// NSString *msgContent = params[@"content"]; //// if (isDes.intValue) { //// msgContent = [[MessageHandleCenter alloc]decryptMsgWithMsgDict:params]; //// } // NSPredicate *predicate = [NSPredicate predicateWithFormat:@"SELF MATCHES %@",TeamShareExpression]; // // if ([predicate evaluateWithObject:content]) { // return nil; // } // // NSDictionary *receiveGroupDict = params[@"group"]; // // NSString *sender = params[@"sender"]; // // sender = [sender stringByReplacingOccurrencesOfString:[NSString stringWithFormat:@"%@",ContentServerID(USERDEFAULT_ContentServer)] withString:@""]; // // int unreadCount = 0 ; // // if (![sender isEqualToString:User_Name]) { // unreadCount = 1; // } // // NSString *receiver = receiveGroupDict[@"id"]; // // NSNumber *time = params[@"ts"]; // // NSNumber *isDes = params[@"isDes"]; // // NSNumber *fromClient = params[@"fromClient"]; // // NSDate *msgTime = [NSDate dateWithTimeIntervalSince1970:[time doubleValue]/1000]; // // MessageContentType msgContentType = [params[@"msgType"]integerValue]; // // if (msgContentType == 0) { // msgContentType = MessageTypeText; // } // // UserGroupEntity *userGroup = [self.allUserGroupsDict objectForKey:receiver]; // // if (!userGroup) { // userGroup = [UserGroupEntity findUserGroupEntityWithGroupID:receiver]; // // //理论上不可能. // if (!userGroup) { // // // userGroup = [UserGroupEntity MR_createEntity]; // userGroup.iD = receiver; // userGroup.name = receiver; // // [userGroup.managedObjectContext MR_saveWithBlock:^(NSManagedObjectContext * _Nonnull localContext) { // // }]; // // // } // // [self.allUserGroupsDict setObject:userGroup forKey:receiver]; // // } // // //先判断是否是富文本 // NSMutableArray *msgArray = [NSMutableArray array]; // // if (msgContentType == MessageTypeText) { // // __block NSArray *judgeStringArray = nil; // NSRange range = [content rangeOfString:MuiltTextPicExpression options:NSRegularExpressionSearch]; // // if (range.location != NSNotFound) { // [Utils getPCScreenPhotoAndTextWithExpression:MuiltTextPicExpression content:content resultBlock:^(NSArray *subData) { // judgeStringArray = subData; // }]; // // // int i = 0; // for (NSArray *onePicTextArray in judgeStringArray) { // // i++; // // NSString *textFront = onePicTextArray[1]; // NSString *imageMD5 = onePicTextArray[2]; // NSString *textBack = onePicTextArray[3]; // // NSString *childUUID= [NSString stringWithFormat:@"%@-%d",uuid,i]; // // // if (textFront.length) { // // // // MessageEntity *msgEntity = [self createMessageWithUUID:childUUID sessionID:receiver content:textFront voiceLength:0 localPath:nil msgContentType:MessageTypeText msgType:MESSAGE_TYPE_GROUP fileDownUpStatus:FileDownUpStatusDown msgState:MessageSendSuccess name:userGroup.name time:msgTime unreadCount:unreadCount resultBlock:^{ // // // // }]; // // [msgArray addObject:msgEntity]; // } // // if (imageMD5.length) { // // // MessageEntity *msgEntity = [self createMessageWithUUID:childUUID sessionID:receiver content:[NSString stringWithFormat:@"http://%@:%@/file/%@",[userDefaults objectForKey:USERDEFAULT_GROUP_STORAGE_HOST], [userDefaults objectForKey:USERDEFAULT_GROUP_STORAGE_PORT],imageMD5] voiceLength:0 localPath:nil msgContentType:MessageTypeImage msgType:MESSAGE_TYPE_GROUP fileDownUpStatus:FileDownUpStatusDown msgState:MessageSendSuccess name:userGroup.name time:msgTime unreadCount:unreadCount resultBlock:^{ // // // // }]; // // [msgArray addObject:msgEntity]; // } // // if (textBack.length) { // // MessageEntity *msgEntity = [self createMessageWithUUID:childUUID sessionID:receiver content:textFront voiceLength:0 localPath:nil msgContentType:MessageTypeText msgType:MESSAGE_TYPE_GROUP fileDownUpStatus:FileDownUpStatusDown msgState:MessageSendSuccess name:userGroup.name time:msgTime unreadCount:unreadCount resultBlock:^{ // // // // }]; // // [msgArray addObject:msgEntity]; // } // // } // }else{ // // // MessageEntity *msgEntity = [self createMessageWithUUID:uuid sessionID:receiver content:content voiceLength:voiceLength localPath:nil msgContentType:msgContentType msgType:MESSAGE_TYPE_GROUP fileDownUpStatus:FileDownUpStatusDown msgState:MessageSendSuccess name:userGroup.name time:msgTime unreadCount:unreadCount resultBlock:^{ // // if (resultBlock) { // resultBlock(); // } // // }]; // // [msgArray addObject:msgEntity]; // // } // // // // }else{ // // // MessageEntity *msgEntity = [self createMessageWithUUID:uuid sessionID:receiver content:content voiceLength:voiceLength localPath:nil msgContentType:msgContentType msgType:MESSAGE_TYPE_GROUP fileDownUpStatus:FileDownUpStatusDown msgState:MessageSendSuccess name:userGroup.name time:msgTime unreadCount:unreadCount resultBlock:^{ // // if (resultBlock) { // resultBlock(); // } // // }]; // // [msgArray addObject:msgEntity]; // // } // // return msgArray; //} //- (void)deleteMessageWithTitle:(NSString *)title UUID:(NSString *)uuid sessionID:(NSString *)sessionID resultBlock:(void(^)())resultBlock{ // // //删除后获取最后一条新消息..update recentEntity // MessageEntity *msgEntity = [MessageEntity findMessageWithUUID:uuid]; // // NSDate *lastTime = msgEntity.msgTime; // // // if (msgEntity) { // [msgEntity MR_deleteEntity]; // // [msgEntity.managedObjectContext MR_saveWithBlock:^(NSManagedObjectContext * _Nonnull localContext) { // // if (resultBlock) { // resultBlock(); // } // // NSPredicate *predicte = [NSPredicate predicateWithFormat:@"sessionId==%@ AND msgTime<=%@", sessionID,lastTime]; // // //限制条数 // NSFetchRequest *request = [NSFetchRequest fetchRequestWithEntityName:@"MessageEntity"]; // request.fetchLimit = 1; // request.predicate = predicte; // // NSSortDescriptor *sortDES = [NSSortDescriptor sortDescriptorWithKey:@"msgTime" ascending:NO]; // request.sortDescriptors = [NSArray arrayWithObject:sortDES]; // // NSArray *msgEntitys = [MessageEntity MR_executeFetchRequest:request inContext:defaultContext]; // // if (msgEntitys.count) { // // MessageEntity *msgEntity = msgEntitys[0]; // // [self updateRecentEntityWithMsgEntity:msgEntity unreadCount:0 title:title resultBlock:^{ // // }]; // // } // // // }]; // } //} //- (void)deleteAllMessagesWithSessionID:(NSString *)sessionID resultBlock:(void(^)())resultBlock //{ // NSArray * chatMessages = [MessageEntity MR_findAllWithPredicate:[NSPredicate predicateWithFormat:@"sessionId == %@",sessionID]]; // // [chatMessages enumerateObjectsUsingBlock:^(id obj, NSUInteger idx, BOOL *stop) { // //最新的消息在前,所以要反着加 // // MessageEntity *tempMessage = offlineMessages[offlineMessages.count - 1 - idx]; // MessageEntity *tempMessage = chatMessages[idx]; // // [tempMessage MR_deleteEntity]; // // }]; // // dispatch_async(dispatch_get_global_queue(0, 0), ^{ // NSManagedObjectContext *privateContext= [NSManagedObjectContext MR_contextWithParent:[NSManagedObjectContext MR_defaultContext]]; // [privateContext MR_saveWithBlock:^(NSManagedObjectContext * _Nonnull localContext) { // // }]; // }); // // RecentEntity *recentEntity = [self.allUserRecentsDict objectForKey:sessionID]; // // if (!recentEntity) { // // recentEntity = [RecentEntity fetchEntityByRecentID:sessionID]; // // } // // [recentEntity MR_deleteEntity]; // // [[recentEntity managedObjectContext]MR_saveWithBlock:^(NSManagedObjectContext * _Nonnull localContext) { // // // }]; // //} //- (void)getLatestMessageInGroupWithGroupID:(NSString *)groupID resultBlock:(void(^)(MessageEntity *msgEntity))resultBlock //{ // NSDate *lastTime = [NSDate date]; // // NSPredicate *predicte = [NSPredicate predicateWithFormat:@"sessionId==%@ AND msgTime<=%@", groupID,lastTime]; // // //限制条数 // NSFetchRequest *request = [NSFetchRequest fetchRequestWithEntityName:@"MessageEntity"]; // request.fetchLimit = 1; // request.predicate = predicte; // // NSSortDescriptor *sortDES = [NSSortDescriptor sortDescriptorWithKey:@"msgTime" ascending:NO]; // request.sortDescriptors = [NSArray arrayWithObject:sortDES]; // // NSArray *msgEntitys = [MessageEntity MR_executeFetchRequest:request inContext:defaultContext]; // // if (msgEntitys.count) { // // MessageEntity *msgEntity = msgEntitys[0]; // // if (resultBlock) { // // resultBlock(msgEntity); // } // // } // //} //- (NoticeModels *)createNoticeModelWithJSONParams:(NSDictionary *)params resultBlock:(void(^)())resultBlock{ // // NoticeModels *noticeModel = [NoticeModels MR_findFirstWithPredicate:[NSPredicate predicateWithFormat:@"uuid = %@",params[@"uuid"]]]; // // if (noticeModel) { // return nil; // }else{ // // noticeModel = [NoticeModels MR_createEntity]; // // noticeModel.sender = params[@"sender"]; // noticeModel.receiver = params[@"receiver"]; // noticeModel.title = params[@"title"]; // noticeModel.content = params[@"content"]; // noticeModel.url = params[@"url"]; // // //字典新增几个字段. // NSMutableArray *tempArray = [NSMutableArray array]; // // int i = 0; // for (NSDictionary *attachmentDict in params [@"attachments"]) { // // // NSString *uuid = [NSString stringWithFormat:@"%@-%d",params[@"uuid"],i]; // // NSMutableDictionary *newAttachmentDict = [NSMutableDictionary dictionaryWithDictionary:attachmentDict]; // [newAttachmentDict setObject:uuid forKey:@"uuid"]; // [newAttachmentDict setObject:@0 forKey:@"isDownloaded"]; // [newAttachmentDict setObject:@"null" forKey:@"localPath"]; // // [tempArray addObject:newAttachmentDict]; // // i++; // // // } // // noticeModel.attachments = tempArray; // //来源 // noticeModel.appName = params[@"appName"]; // noticeModel.uuid = params[@"uuid"]; // noticeModel.unreadIndifier = @1; // // NSString *time = [NSString stringWithFormat:@"%@",params[@"timestamp"]]; // // NSDate *date = [NSDate dateWithTimeIntervalSince1970:[time doubleValue]/1000]; // noticeModel.timeStamp = date; // // [noticeModel.managedObjectContext MR_saveWithBlock:^(NSManagedObjectContext * _Nonnull localContext) { // // // }]; // // // return noticeModel; // } // //} //- (RecentEntity *)updateRecentEntityWithSessionID:(NSString *)sessionID type:(int)type title:(NSString *)title uuid:(NSString *)uuid lastTime:(NSDate *)lastTime unreadCount:(NSInteger)unreadCount lastMsg:(NSString *)lastMsg resultBlock:(void(^)())resultBlock //{ // // return nil; //} //- (RecentEntity *)updateRecentEntityWithMsgEntity:(MessageEntity *)msgEntity unreadCount:(int)unreadCount title:(NSString *)title resultBlock:(void(^)())resultBlock //{ // // RecentEntity *recentEntity = [self.allUserRecentsDict objectForKey:msgEntity.sessionId]; // // if (!recentEntity) { // recentEntity = [RecentEntity fetchEntityByRecentID:msgEntity.sessionId]; // // if (!recentEntity) { // recentEntity = [RecentEntity MR_createEntity]; // } // // // // recentEntity.recentId = msgEntity.sessionId; // // recentEntity.type = msgEntity.msgType; // // //待调整... // recentEntity.title = title; // // [self.allUserRecentsDict setObject:recentEntity forKey:msgEntity.sessionId]; // } // // if([msgEntity.msgTime compare:recentEntity.lastTime] == NSOrderedDescending || !recentEntity.lastTime) // { // //未读消息数有问题. // recentEntity.uuid = msgEntity.uuid; // // recentEntity.lastTime = msgEntity.msgTime; // // recentEntity.unreadCount = @([recentEntity.unreadCount intValue] + unreadCount); // // switch (msgEntity.msgContentType.integerValue) { // // case MessageTypeText: // // recentEntity.lastMsg = msgEntity.msgContent; // break; // case MessageTypeNotice: // // recentEntity.lastMsg = msgEntity.msgContent; // break; // case MessageTypeFile: // // recentEntity.lastMsg = @"[文件]"; // break; // case MessageTypeVoice: // // recentEntity.lastMsg = @"[语音]"; // break; // case MessageTypeImage: // // // recentEntity.lastMsg = @"[图片]"; // break;break; // case MessageTypeLocation: // // recentEntity.lastMsg = @"[位置]"; // break;break; // default: // break; // } // // [[recentEntity managedObjectContext]MR_saveWithBlock:^(NSManagedObjectContext * _Nonnull localContext) { // // if (resultBlock) { // resultBlock(); // } // // }]; // // // } // // return recentEntity; //} //- (RecentEntity *)updateRecentEntityWithSessionID:(NSString *)sessionID type:(MessageType)type title:(NSString *)title lastTime:(NSDate *)date lastMsg:(NSString *)lastMsg unreadCount:(int)unreadCount resultBlock:(void(^)())resultBlock //{ // RecentEntity *recentEntity = [self.allUserRecentsDict objectForKey:sessionID]; // // if (!recentEntity) { // recentEntity = [RecentEntity fetchEntityByRecentID:sessionID]; // // if (!recentEntity) { // recentEntity = [RecentEntity MR_createEntity]; // } // // // // recentEntity.recentId = sessionID; // // recentEntity.type = @(type); // // recentEntity.title = title; // // [self.allUserRecentsDict setObject:recentEntity forKey:sessionID]; // } // // if([date compare:recentEntity.lastTime] == NSOrderedDescending || !recentEntity.lastTime) // { // // // recentEntity.lastTime = [NSDate date]; // recentEntity.lastMsg = lastMsg; // recentEntity.unreadCount = @(recentEntity.unreadCount.intValue + unreadCount); // [recentEntity.managedObjectContext MR_saveWithBlock:^(NSManagedObjectContext * _Nonnull localContext) { // // if (resultBlock) { // resultBlock(); // } // }]; // // } // // return recentEntity; //} //手动创造 //- (UserGroupEntity *)createGroupEntityWithGroupID:(NSString *)groupID groupName:(NSString *)groupName creator:(NSString *)creator managers:(NSArray *)managers groupMembers:(NSArray *)groupMembers groupHeaderImage:(UIImage *)groupHeaderImage createTs:(NSDate *)ts ActiveAs:(NSDate *)as JoinJs:(NSDate *)js ignores:(NSArray *)ignors tags:(NSArray *)tags notice:(NSString *)notice intro:(NSString *)intro invite:(NSNumber *)invite type:(NSNumber *)type ver:(NSNumber *)ver auth:(NSNumber *)auth msgReceiveLevel:(NSNumber *)msgReceiveLevel unreadMsgCount:(NSNumber *)unradMsgCount unreadMsgIdentifierDict:(NSDictionary *)unreadMsgIdentifierDict resultBlock:(void(^)())resultBlock{ // // UserGroupEntity *userGroupEntity = [UserGroupEntity findUserGroupEntityWithGroupID:groupID]; // // if (userGroupEntity) { // return nil; // }else{ // // userGroupEntity = [UserGroupEntity MR_createEntity]; // // userGroupEntity.iD = groupID; // // NSComparator cmptr = ^(NSDictionary *userA, NSDictionary *userB){ // if ([userA[@"ts"]integerValue] > [userB[@"ts"]integerValue]) { // return (NSComparisonResult)NSOrderedDescending; // } // // if ([userA[@"ts"]integerValue] < [userB[@"ts"]integerValue]) { // return (NSComparisonResult)NSOrderedAscending; // } // return (NSComparisonResult)NSOrderedSame; // // }; // NSArray *array = [groupMembers sortedArrayUsingComparator:cmptr]; // // userGroupEntity.members = array; // // userGroupEntity.managers = managers; // // userGroupEntity.tags = tags; // // userGroupEntity.ignores = ignors; // // userGroupEntity.as = as; // // userGroupEntity.ts = ts; // // //自己创建时... // userGroupEntity.js = [NSDate date]; // // userGroupEntity.invite = @(invite.boolValue); // // userGroupEntity.creator = creator; // // userGroupEntity.intro = intro; // // userGroupEntity.name = groupName; // // userGroupEntity.notice = notice; // // userGroupEntity.ver = ver; // // userGroupEntity.type = type; // // userGroupEntity.auth = auth; // // userGroupEntity.groupHeaderImage = groupHeaderImage; // // [userGroupEntity.managedObjectContext MR_saveWithBlock:^(NSManagedObjectContext * _Nonnull localContext) { // // if (resultBlock) { // resultBlock(); // } // }]; // // [self.allUserGroupsDict setObject:userGroupEntity forKey:groupID]; // // } // // //update recent.. // [self updateRecentEntityWithSessionID:userGroupEntity.iD type:MESSAGE_TYPE_GROUP title:userGroupEntity.name lastTime:[NSDate date] lastMsg:@"群创建成功" unreadCount:1 resultBlock:^{ // // // }]; // // return userGroupEntity; // // //} //- (UserGroupEntity *)createGroupEntityWithGroupParams:(NSDictionary *)dict resultBlock:(void(^)())resultBlock //{ // // NSDictionary *memebersDict = dict[@"members"]; // //头像 // GroupHeaderImageView *headerImageView = [[GroupHeaderImageView alloc]initWithHeaderWH:UserGroupShowCellHeaderViewWH]; // headerImageView.bounds = CGRectMake(0, 0, UserGroupShowCellHeaderViewWH, UserGroupShowCellHeaderViewWH); // headerImageView.groupMembersArray = memebersDict.allValues; // UIImage *groupHeaderImage = [Utils imageWithView:headerImageView]; // // // UserGroupEntity *userGroupEntity = [self createGroupEntityWithGroupID:dict[@"id"] groupName: dict[@"name"] creator:dict[@"creator"] managers: dict[@"managers"]groupMembers:dict[@"members"] groupHeaderImage:groupHeaderImage createTs:[NSDate dateWithTimeIntervalSince1970:[dict[@"ts"] doubleValue]/1000] ActiveAs:[NSDate dateWithTimeIntervalSince1970:[dict[@"as"] doubleValue]/1000] JoinJs:[NSDate date] ignores: dict[@"ignores"] tags: dict[@"tags"] notice: dict[@"notice"] intro:dict[@"intro"] invite:dict[@"invite"] type:dict[@"type"] ver: dict[@"ver"] auth: dict[@"auth"] msgReceiveLevel:nil unreadMsgCount:nil unreadMsgIdentifierDict:nil resultBlock:^{ // // }]; // // return userGroupEntity; // //} //待新增启动更新 //- (UserGroupEntity *)updateGroupEntityWithParams:(NSDictionary *)params resultBlock:(void(^)())resultBlock //{ // NSString *ID = params[@"id"]; // UserGroupEntity *userGroupEntity = [self.allUserGroupsDict objectForKey:ID]; // // if (!userGroupEntity) { // userGroupEntity = [UserGroupEntity findUserGroupEntityWithGroupID:ID]; // // if (!userGroupEntity) { // // NSDictionary *memebersDict = params[@"members"]; // //头像 // GroupHeaderImageView *headerImageView = [[GroupHeaderImageView alloc]initWithHeaderWH:UserGroupShowCellHeaderViewWH]; // headerImageView.bounds = CGRectMake(0, 0, UserGroupShowCellHeaderViewWH, UserGroupShowCellHeaderViewWH); // headerImageView.groupMembersArray = memebersDict.allValues; // UIImage *groupHeaderImage = [Utils imageWithView:headerImageView]; // //#warning 2017-05-27 js 不清不楚. // userGroupEntity = [self createGroupEntityWithGroupID:ID groupName:params[@"name"] creator:params[@"creator"] managers:params[@"managers"] groupMembers:params[@"members"] groupHeaderImage:groupHeaderImage createTs:[NSDate dateWithTimeIntervalSince1970:[params[@"ts"] doubleValue]/1000] ActiveAs:[NSDate dateWithTimeIntervalSince1970:[params[@"as"] doubleValue]/1000] JoinJs:nil ignores:params[@"ignores"] tags:params[@"tags"] notice:params[@"notice"] intro:params[@"intro"] invite:[NSNumber numberWithBool:[params[@"invite"]boolValue]] type:params[@"type"] ver:params[@"ver"] auth:params[@"auth"] msgReceiveLevel:nil unreadMsgCount:nil unreadMsgIdentifierDict:nil resultBlock:^{ // // if (resultBlock) { // resultBlock(); // } // // }]; // // // } // // [self.allUserGroupsDict setObject:userGroupEntity forKey:ID]; // } // // if (userGroupEntity && [userGroupEntity.ver longLongValue] < [params[@"ver"]longLongValue]) { // // NSDictionary *memebersDict = params[@"members"]; // //头像 // GroupHeaderImageView *headerImageView = [[GroupHeaderImageView alloc]initWithHeaderWH:UserGroupShowCellHeaderViewWH]; // headerImageView.bounds = CGRectMake(0, 0, UserGroupShowCellHeaderViewWH, UserGroupShowCellHeaderViewWH); // headerImageView.groupMembersArray = memebersDict.allValues; // UIImage *groupHeaderImage = [Utils imageWithView:headerImageView]; // // [self updateGroupEntityWithGroupID:ID groupName:params[@"name"] creator:params[@"creator"] managers:params[@"managers"] groupMembers:params[@"members"] groupHeaderImage:groupHeaderImage createTs:[NSDate dateWithTimeIntervalSince1970:[params[@"ts"] doubleValue]/1000] ActiveAs:[NSDate dateWithTimeIntervalSince1970:[params[@"as"] doubleValue]/1000] JoinJs:[NSDate date] ignores:params[@"ignores"] tags:params[@"tags"] notice:params[@"notice"] intro:params[@"intro"] invite:[NSNumber numberWithBool:[params[@"invite"]boolValue]] type:params[@"type"] ver:params[@"ver"] auth:params[@"auth"] resultBlock:^{ // // if (resultBlock) { // resultBlock(); // } // // }]; // } // // // return userGroupEntity; // //} //群解散 //- (void)deleteGroupWithGroupID:(NSString *)groupID resultBlock:(void(^)())resultBlock //{ // UserGroupEntity *userGroupEntity = [self.allUserGroupsDict objectForKey:groupID]; // // if (!userGroupEntity) { // userGroupEntity = [UserGroupEntity findUserGroupEntityWithGroupID:groupID]; // } // // NSString *title = userGroupEntity.name; // // [userGroupEntity MR_deleteEntity]; // // [userGroupEntity.managedObjectContext MR_saveWithBlock:^(NSManagedObjectContext * _Nonnull localContext) { // // RecentEntity *recentEntity = [self.allUserRecentsDict objectForKey:groupID]; // // if (!recentEntity) { // recentEntity = [RecentEntity fetchEntityByRecentID:groupID]; // // if (!recentEntity) { // //#warning ... // [self updateRecentEntityWithSessionID:groupID type:MESSAGE_TYPE_GROUP title:title lastTime:[NSDate date] lastMsg:@"该群组已被解散" unreadCount:1 resultBlock:^{ // // if (resultBlock) { // resultBlock(); // } // // }]; // // } // } // // // }]; //} //- (void)kickOutFromGroupWithGroupID:(NSString *)groupID resultBlock:(void(^)())resultBlock //{ // UserGroupEntity *userGroupEntity = [self.allUserGroupsDict objectForKey:groupID]; // // if (!userGroupEntity) { // userGroupEntity = [UserGroupEntity findUserGroupEntityWithGroupID:groupID]; // } // // // [userGroupEntity MR_deleteEntity]; // // [userGroupEntity.managedObjectContext MR_saveWithBlock:^(NSManagedObjectContext * _Nonnull localContext) { // // RecentEntity *recentEntity = [self.allUserRecentsDict objectForKey:groupID]; // // if (!recentEntity) { // recentEntity = [RecentEntity fetchEntityByRecentID:groupID]; // // [recentEntity MR_deleteEntity]; // // [[recentEntity managedObjectContext]MR_saveWithBlock:^(NSManagedObjectContext * _Nonnull localContext) { // // }]; // // } // // // }]; //} //群未读消息标记及群的提醒等级单独接口. //msgReceiveLevel:(NSNumber *)msgReceiveLevel unreadMsgCount:(NSNumber *)unradMsgCount unreadMsgIdentifierDict:(NSDictionary *)unreadMsgIdentifierDict //- (UserGroupEntity *)updateGroupEntityWithGroupID:(NSString *)groupID groupName:(NSString *)groupName creator:(NSString *)creator managers:(NSArray *)managers groupMembers:(NSArray *)groupMembers groupHeaderImage:(UIImage *)groupHeaderImage createTs:(NSDate *)ts ActiveAs:(NSDate *)as JoinJs:(NSDate *)js ignores:(NSArray *)ignors tags:(NSArray *)tags notice:(NSString *)notice intro:(NSString *)intro invite:(NSNumber *)invite type:(NSNumber *)type ver:(NSNumber *)ver auth:(NSNumber *)auth resultBlock:(void(^)())resultBlock{ // // //理论上无需判断是否group存在.. // // UserGroupEntity *userGroup = [self.allUserGroupsDict objectForKey:groupID]; // // if (groupName) { // userGroup.name = groupName; // } // // if (creator) { // userGroup.creator = creator; // } // // if (managers) { // userGroup.managers = managers; // } // // if (groupMembers) { // // NSComparator cmptr = ^(NSDictionary *userA, NSDictionary *userB){ // if ([userA[@"ts"]integerValue] > [userB[@"ts"]integerValue]) { // return (NSComparisonResult)NSOrderedDescending; // } // // if ([userA[@"ts"]integerValue] < [userB[@"ts"]integerValue]) { // return (NSComparisonResult)NSOrderedAscending; // } // return (NSComparisonResult)NSOrderedSame; // // }; // NSArray *array = [groupMembers sortedArrayUsingComparator:cmptr]; // // userGroup.members = array; // } // // if (groupHeaderImage) { // userGroup.groupHeaderImage = groupHeaderImage; // } // // if (ts) { // userGroup.ts = ts; // } // // if (as) { // userGroup.as = as; // } // // if (js) { // userGroup.js = js; // } // // if (ignors) { // userGroup.ignores = ignors; // } // // if (tags) { // userGroup.tags = tags; // } // // if (notice) { // userGroup.notice = notice; // } // // if (intro) { // userGroup.intro = intro; // } // // userGroup.invite = invite; // // userGroup.type = type; // // userGroup.ver = ver; // // userGroup.auth = auth; // // [[userGroup managedObjectContext]MR_saveWithBlock:^(NSManagedObjectContext * _Nonnull localContext) { // if (resultBlock) { // resultBlock(); // } // }]; // // //更新recent的title. // RecentEntity *recentEntity = [self.allUserRecentsDict objectForKey:groupID]; // // if (!recentEntity) { // // recentEntity = [RecentEntity fetchEntityByRecentID:groupID]; // // } // // // if (recentEntity) { // recentEntity.title = groupName; // // [[recentEntity managedObjectContext]MR_saveWithBlock:^(NSManagedObjectContext * _Nonnull localContext) { // // }]; // } // // // return userGroup; //} //返回用户对象<用户信息已经得到> //-(AddrBook*) p_fetchAddrBookForReceivedMessage:(NSString*)senderId //{ // /* // componentsSeparatedByString: 将字符串切割成数组, 参数是作为分割辨认用的 // */ // //通过 '-' 为切割点,将字符串切割成数组 // NSArray *tempArray = [senderId componentsSeparatedByString:@"-"]; // //获取字符 // if (tempArray.count != 2) { // return nil; // } // NSString *loginName = tempArray[1]; // NSString *serverID = tempArray[0]; // /* // fetchEntityByLoginName: serverID: 是通过这 loginName 和 serverID 作为唯一标识符 , 来查询对象 // */ // //获取对象 <相当于创建了一个对象 对象里面有信息> // __block AddrBook *addrbook = [AddrBook fetchEntityByLoginName:loginName serverID:serverID]; // if (!addrbook) { // // // // NSDictionary *params = @{ // @"serverID":serverID, // @"userName":loginName // }; // [HttpManager postRequestToUrlSynchornize:FULL_URL(@"users/getUserDetail") params:params complete:^(BOOL successed, NSDictionary *result) { // //成功并返回一个正确的 bool 值 // if (successed && [result[@"success"]boolValue]) { // //从 result 取得字典数据 // NSDictionary *userDic = result[@"data"]; // //从字典数据中获取 id 数据 // NSNumber *idx = userDic[@"id"]; // // // NSLog(@"userDic: - - - > %@ < - - - ",userDic); // // // if (idx!=0) { // //获取用户对象 // addrbook = [AddrBook insertEntityByID:idx]; // //获取对象的所有属性数据 // [addrbook updatePropertyByDictionary:userDic]; // } // //保存 // // dispatch_async(dispatch_get_global_queue(0, 0), ^{ // NSManagedObjectContext *privateContext= [NSManagedObjectContext MR_contextWithParent:[NSManagedObjectContext MR_defaultContext]]; [privateContext MR_saveToPersistentStoreAndWait]; // }); // } // }]; // } // return addrbook; //} //- (AddrBook *)getAddrbookWithUerID:(NSString *)userID //{ // // AddrBook *addrBook = [self.allUserDict objectForKey:userID]; // // if (!addrBook) { // addrBook = [self p_fetchAddrBookForReceivedMessage:userID]; // // [self.allUserDict setObject:addrBook forKey:userID]; // } // // // return addrBook; // //} //- (AddrBook *)createUserDetailsWithJSONParams:(NSDictionary *)prams resultBlock:(void(^)())resultBlock //{ // /* // Dictionary2Object(dictionary, @"id", self.addrBookID); // self.postion = [NSString stringWithFormat:@"%@",dictionary[@"position"]]; // // Dictionary2Object(dictionary, @"position", self.postion); // Dictionary2Object(dictionary, @"groupId", self.groupID); // Dictionary2Object(dictionary, @"defaultLoginName", self.defaultLoginName); // Dictionary2Object(dictionary, @"telephone", self.telephone); // Dictionary2Object(dictionary, @"cellphone", self.cellTelephone); // Dictionary2Object(dictionary, @"celltelephone", self.cellTelephone); // Dictionary2Object(dictionary, @"serverID", self.serverID); // Dictionary2Object(dictionary, @"email", self.email); // Dictionary2Object(dictionary, @"remark", self.remark); // Dictionary2Object(dictionary, @"postion", self.postion); // Dictionary2Object(dictionary, @"branchId", self.branchId); // Dictionary2Object(dictionary, @"branchID", self.branchId); // Dictionary2Object(dictionary, @"mobile", self.cellTelephone); // // Dictionary2Object(dictionary, @"shortMobile", self.shortMobile); // Dictionary2Object(dictionary, @"tel", self.telephone); // // Dictionary2Object(dictionary, @"department", self.department); // // Dictionary2Object(dictionary, @"company", self.company); // Dictionary2Object(dictionary, @"secret", self.secret); // // self.secret = Numberi(2); // // // Dictionary2TimeObject(dictionary, @"createTime", self.createTime); // Dictionary2Object(dictionary, @"deleted", self.deleted); // Dictionary2Object(dictionary, @"branch", self.department); // Dictionary2Object(dictionary, @"duty", self.duty); // Dictionary2Object(dictionary, @"gender", self.gender); // Dictionary2Object(dictionary, @"loginName", self.name); // // Dictionary2Object(dictionary, @"version", self.version); // if (self.loginState) { // Dictionary2Object(dictionary, @"loginState", self.loginState); // } // if (self.appState){ // Dictionary2Object(dictionary, @"appState", self.appState); // } // // Dictionary2Object(dictionary, @"loginName", self.loginName); // Dictionary2Object(dictionary, @"displayName", self.disPlayName); // Dictionary2Object(dictionary, @"headImageUrl", self.avatar); // Dictionary2Object(dictionary, @"company", self.company); // Dictionary2Object(dictionary, @"watchword", self.signature); // // // Dictionary2Object(dictionary, @"avatarColor", self.avatarColor); // //同步loginName 和 defaultLoginName // if (self.loginName.length!=0) { // self.defaultLoginName = self.loginName; // }else{ // self.loginName = self.defaultLoginName; // } // // self.state = @(0); // // */ // return nil; //} //- (AddrBook *)updateUserDetailsWithJSONParams:(NSDictionary *)prams resultBlock:(void(^)())resultBlock //{ //// [AddrBook updateUsersDetail:prams]; // return nil; //} @end