// // TDInterLeterSearchVC.m // smartRhino // // Created by niuzhen on 2020/3/23. // Copyright © 2020 tederen. All rights reserved. // #import "TDInterLeterSearchVC.h" #import "ChatMsgNoticeCell.h" #import "NSDate+Extension.h" @interface TDInterLeterSearchVC () @property (nonatomic, copy) NSMutableArray *listArray; @property (nonatomic, assign) NSUInteger currentPage ; @property (nonatomic, assign) NSUInteger totalPage ; @property (nonatomic, assign) NSUInteger totalRecord ; @end @implementation TDInterLeterSearchVC - (void)viewDidLoad { [super viewDidLoad]; self.fd_prefersNavigationBarHidden = YES; self.view.backgroundColor = RGB(255, 255, 255); self.tableView.delegate = self; self.tableView.dataSource = self; [self addRefresh]; self.historySearchType = HistorySearchType_InterMail; [self addObserver:self forKeyPath:@"searchText" options:NSKeyValueObservingOptionNew context:nil]; } - (void)observeValueForKeyPath:(NSString *)keyPath ofObject:(id)object change:(NSDictionary *)change context:(void *)context { id obj = [change objectForKey:@"new"]; NSString * text = @""; if ([obj isKindOfClass:[NSString class]]) { text = obj; }else{ text = [obj stringValue]; } if (text.length > 0) { [self headRefresh]; } } - (void)addRefresh { WS(weakSelf); if (self.searchText.length > 0) { self.tableView.mj_header = [MJRefreshNormalHeader headerWithRefreshingBlock:^{ [weakSelf headRefresh]; }]; self.tableView.mj_footer = [MJRefreshBackStateFooter footerWithRefreshingBlock:^{ [weakSelf footerRefresh]; }]; } } - (void)removeRefresh { [self.tableView.mj_header endRefreshing]; [self.tableView.mj_header removeFromSuperview]; [self.tableView.mj_footer endRefreshing]; [self.tableView.mj_footer removeFromSuperview]; } - (void)headRefresh{ self.currentPage = 1; [self.listArray removeAllObjects]; [self getData:self.searchText]; } - (void)footerRefresh{ self.currentPage += 1; if (self.totalRecord == self.listArray.count) { self.currentPage --; return ; } [self getData:self.searchText]; } - (void)getData:(NSString *)searchKey { NSMutableDictionary *dict = [[NSMutableDictionary alloc] init]; NSUInteger deleteType = 0; switch (self.type) { case PageTypeFromTitleAll: { deleteType = 1; } break; case PageTypeFromTitleMy: { deleteType = 2; } break; case PageTypeFromTitleDraftBox: { deleteType = 3; } break; case PageTypeFromTitleDeleted: { deleteType = 4; } break; default: { deleteType = 5; } break; } [dict setObject:@(self.currentPage) forKey:@"Page"]; [dict setObject:@"20" forKey:@"PerPage"]; [dict setObject:@(deleteType) forKey:@"SearchType"]; [dict setObject:searchKey forKey:@"Key"]; [dict setObject:@(self.FolderId) forKey:@"FolderId"]; WS(weakSelf); [[HttpManager sharedHttpManager] POSTUrl:Host(API_InterLetter) parameters:dict responseStyle:JOSN success:^(id _Nonnull responseObject) { NSLog(@"站内信返回数据%@",responseObject); InterLeterModel *listModel = [[InterLeterModel alloc] initWithDictionary:responseObject error:nil]; [weakSelf.tableView.mj_header endRefreshing]; [weakSelf.tableView.mj_footer endRefreshing]; weakSelf.totalRecord = listModel.Total; [weakSelf.listArray addObjectsFromArray:listModel.Items]; if (weakSelf.totalRecord == weakSelf.listArray.count) { [weakSelf.tableView.mj_footer resetNoMoreData]; [weakSelf.tableView.mj_footer endRefreshingWithNoMoreData]; } [weakSelf.tableView reloadData]; } failure:^(NSError * _Nonnull error) { SHOWERROR([ZYCTool handerResultData:error]); [weakSelf.tableView.mj_header endRefreshing]; [weakSelf.tableView.mj_footer endRefreshing]; }]; } - (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section { return self.listArray.count; } - (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath { return [ChatMsgNoticeCell configCell2Height]; } - (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath { InterLeterSubModel *model = self.listArray[indexPath.row]; switch (self.type) { case PageTypeFromTitleAll:{ ChatMsgNoticeCell *cell = [ChatMsgNoticeCell configCell2:tableView indexPath:indexPath]; cell.cell2UserImgView.image = [UIImage imageNamed:@"站内信图"]; cell.cell2TitleLabel.attributedText = [ZYCTool checkOfString:model.Title withSearchText:self.searchText]; [self setIntroLabelWithModel:model introLabel:cell.cell2IntroLabel unreadNumberLabel:cell.cell2UnReadNumberLbl]; cell.cell2TimeLabel.text = [NSDate getTimeStringAutoShort2:model.CreatedDate]; cell.IsTopImgV.hidden = !model.IsTop; cell.cell2RedBgView.hidden = model.IsRead; return cell; } break; case PageTypeFromTitleMy: { ChatMsgNoticeCell *cell = [ChatMsgNoticeCell configCell2:tableView indexPath:indexPath]; cell.cell2UserImgView.image = [UIImage imageNamed:@"站内信图"]; cell.cell2TitleLabel.attributedText = [ZYCTool checkOfString:model.Title withSearchText:self.searchText]; [self setIntroLabelWithModel:model introLabel:cell.cell2IntroLabel unreadNumberLabel:cell.cell2UnReadNumberLbl]; cell.cell2TimeLabel.text = [NSDate getTimeStringAutoShort2:model.CreatedDate]; cell.cell2RedBgView.hidden = model.IsRead; return cell; } break; case PageTypeFromTitleDraftBox: { ChatMsgNoticeCell *cell = [ChatMsgNoticeCell configCell2:tableView indexPath:indexPath]; cell.cell2UserImgView.image = [UIImage imageNamed:@"站内信图"]; cell.cell2TitleLabel.attributedText = [ZYCTool checkOfString:model.Title withSearchText:self.searchText]; cell.cell2UnReadNumberLbl.hidden = YES; if (model.MissiveType == 2) { cell.cell2IntroLabel.text = @"匿名"; }else{ if (model.EnjoyUser.length == 0) { cell.cell2IntroLabel.hidden = YES; }else{ if (model.EnjoyUser.length > 13) { NSRange range1 = NSMakeRange(10, (model.EnjoyUser.length - 10)); NSString *resultStr1 = [model.EnjoyUser stringByReplacingCharactersInRange:range1 withString:@"..."]; cell.cell2IntroLabel.text = [NSString stringWithFormat:@"收件人:%@",resultStr1]; }else{ cell.cell2IntroLabel.text = [NSString stringWithFormat:@"收件人:%@",model.EnjoyUser]; } } } cell.cell2EditBtn.hidden = NO; WS(weakSelf); [cell.cell2EditBtn setAction:^{ NSLog(@"编辑草稿箱站内信"); [weakSelf editorButtonHander:indexPath]; }]; cell.cell2RedBgView.hidden = model.IsRead; cell.cell2TimeLabel.hidden = YES; return cell; } break; case PageTypeFromTitleDeleted: { ChatMsgNoticeCell *cell = [ChatMsgNoticeCell configCell2:tableView indexPath:indexPath]; cell.cell2UserImgView.image = [UIImage imageNamed:@"站内信图"]; cell.cell2TitleLabel.attributedText = [ZYCTool checkOfString:model.Title withSearchText:self.searchText]; [self setIntroLabelWithModel:model introLabel:cell.cell2IntroLabel unreadNumberLabel:cell.cell2UnReadNumberLbl]; cell.cell2EditBtn.hidden = NO; cell.cell2RedBgView.hidden = YES; cell.cell2TimeLabel.hidden = YES; [cell.cell2EditBtn setTitle:@"恢复" forState:UIControlStateNormal]; WS(weakSelf); [cell.cell2EditBtn setAction:^{ NSLog(@"恢复已删除站内信"); [weakSelf reDoDeleteFind:@[model].mutableCopy]; }]; return cell; }break; default: { ChatMsgNoticeCell *cell = [ChatMsgNoticeCell configCell2:tableView indexPath:indexPath]; cell.cell2UserImgView.image = [UIImage imageNamed:@"站内信图"]; cell.cell2TitleLabel.attributedText = [ZYCTool checkOfString:model.Title withSearchText:self.searchText]; //cell.cell2IntroLabel.text = @"匿名"; cell.IsTopImgV.hidden = !model.IsTop; [self setIntroLabelWithModel:model introLabel:cell.cell2IntroLabel unreadNumberLabel:cell.cell2UnReadNumberLbl]; cell.cell2RedBgView.hidden = model.IsRead; return cell; } break; } } - (void)setIntroLabelWithModel:(InterLeterSubModel *)model introLabel:(UILabel *)introLabel unreadNumberLabel:(UILabel *)unreadNumberLabel { //判断是否匿名 if (model.MissiveType == 2) { introLabel.text = @"匿名"; unreadNumberLabel.text = nil; }else { if (model.IsAdmin == YES) { if (model.EnjoyUser.length > 10) { NSRange range1 = NSMakeRange(10, (model.EnjoyUser.length - 10)); NSString *resultStr1 = [model.EnjoyUser stringByReplacingCharactersInRange:range1 withString:@"..."]; introLabel.text = [NSString stringWithFormat:@"收件人:%@",resultStr1]; }else{ introLabel.text = [NSString stringWithFormat:@"收件人:%@",model.EnjoyUser]; } }else { introLabel.text = model.SendName; } unreadNumberLabel.text = [NSString stringWithFormat:@"已读:%ld/%ld",(long)model.ReadCount,(long)model.AllCount]; } } - (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath { InterLeterSubModel *model = self.listArray[indexPath.row]; if (self.type == PageTypeFromTitleDraftBox) { [self editorButtonHander:indexPath]; }else{ MyTDTopicDetailVC *vc = [MyTDTopicDetailVC initMyTDTopicDetailVC]; vc.Id = model.MiddleId; vc.type = CollectModel_InterMail; [self.navigationController pushViewController:vc animated:YES]; } } /// 恢复文件夹 - (void)reDoDeleteFind:(NSMutableArray*)findModelArray{ NSMutableArray *MiddleIds = [NSMutableArray array]; for (InterLeterSubModel *model in findModelArray) { [MiddleIds addObject:@(model.Id)]; } NSMutableDictionary *dic = [[NSMutableDictionary alloc]init]; [dic setValue:MiddleIds forKey:@"MiddleIds"]; [dic setValue:@(5) forKey:@"FolderType"]; [dic setValue:@(0) forKey:@"IsDelete"]; [dic setValue:@([AppUserModel sharedAppUserModel].Id) forKey:@"UserId"]; SHOWLOADING WEAKSELF [[HttpManager sharedHttpManager] POSTUrl:Host(API_Find_DeleteRecord_Redo) parameters:dic responseStyle:DATA success:^(id _Nonnull responseObject) { STRONGSELF REMOVESHOW [strongSelf headRefresh]; } failure:^(NSError * _Nonnull error) { SHOWERROR([ZYCTool handerResultData:error]); }]; } - (void)editorButtonHander:(NSIndexPath *)indexPath{ WS(weakSelf); InterLeterSubModel *model = self.listArray[indexPath.row]; MyTDTopicCreateVC * vc = [MyTDTopicCreateVC initMyTDTopicCreateVC]; vc.type = CollectModel_InterMail; vc.isEdit = YES; vc.Id = model.MiddleId; vc.upDateBlock = ^{ [weakSelf headRefresh]; }; [self.navigationController pushViewController:vc animated:YES]; } - (NSMutableArray *)listArray{ if (!_listArray) { _listArray = [NSMutableArray new]; } return _listArray; } @end