zjf
2023-03-06 392b76515f40376b6d36f40a114850ef63650384
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
//
//  PBMessageController.m
//  IphoneBIMe
//
//  Created by zjf on 2018/9/14.
//  Copyright © 2018年 ProBIM. All rights reserved.
//
 
#import "PBMessageController.h"
#import "PBUnReadTableViewCell.h"
#import "PBNetworkModel.h"
#import "PBUnReadMesModel.h"
#import "PBAddIssueController.h"
#import "PBNavigationController.h"
#import "PBProjectModel.h"
#import "PBIssueListModel.h"
#import "PBResultModel.h"
#import "PBAddExamineController.h"
#import "PBExamineListModel.h"
#import <JPUSHService.h>
#import "PBIssueNavModel.h"
#import "PBProjectModel.h"
#import "PBIssueListModel.h"
#import "PBRefreshGifHeader.h"
#define pageSize 15
 
static NSString *const cellID = @"cellID";
@interface PBMessageController ()
@property (nonatomic, strong) NSMutableArray *dataList;
@property (nonatomic, assign) NSInteger pageIndex;
@property (nonatomic, assign) NSInteger unReadCount;
@property (nonatomic, strong) PBRefreshGifHeader *headerView;
@end
 
@implementation PBMessageController
- (void)viewWillAppear:(BOOL)animated {
    [super viewWillAppear:animated];
    [self.navigationController setNavigationBarHidden:NO animated:YES];
}
 
- (void)viewDidLoad {
    [super viewDidLoad];
    [self setupRefresh];
    [self setupUI];
    [self loadDate];
}
 
- (void)setupRefresh {
    MJRefreshNormalHeader *header = [MJRefreshNormalHeader headerWithRefreshingBlock:^{
        self.pageIndex = 0;
        [self.dataList removeAllObjects];
        [self loadUnReadMessage];
    }];
    header.lastUpdatedTimeLabel.hidden = YES;
    header.stateLabel.textColor = TitleColor;
    [header setTitle:@"下拉刷新" forState:MJRefreshStateIdle];
    [header setTitle:@"释放更新" forState:MJRefreshStatePulling];
    [header setTitle:@"加载中..." forState:MJRefreshStateRefreshing];
    self.tableView.mj_header = header;
//    self.tableView.mj_header = self.headerView;
    
//    MJRefreshBackNormalFooter *footer = [MJRefreshBackNormalFooter footerWithRefreshingBlock:^{
//        [self loadUnReadMessage];
//    }];
//
//    self.tableView.mj_footer = footer;
//    CGFloat insetBottom;
//    if (IS_IPHONE_X) {
//        insetBottom = 34.f;
//    }else {
//        insetBottom = 0.f;
//    }
//    self.tableView.mj_footer.ignoredScrollViewContentInsetBottom =  insetBottom;
}
- (void)loadDate {
    dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(1.0 * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{
       // 需要延迟执行的代码
        [self.tableView.mj_header beginRefreshing];
    });
}
- (void)loadUnReadMessage {
    [[PBNetworkTools sharedTools] RequestGetNotReadMsgWithPageIndex:++self.pageIndex andPageSize:pageSize andIsRead:@"" andCallBack:^(NSURLSessionDataTask *task, id response, NSError *error) {
        [self.tableView.mj_header endRefreshing];
//        [self.tableView.mj_footer endRefreshing];
        if (error) {
            NSLog(@"%@",error);
//            --self.pageIndex;
            [YJProgressHUD showMessage:@"加载失败" inView:nil];
            return;
        }
        NSString *str = [[NSString alloc] initWithData:response encoding:NSUTF8StringEncoding];
        PBNetworkModel *networkModel = [PBNetworkModel yy_modelWithJSON:str];
        if (networkModel.Ret == 1) {
            NSArray *list = [networkModel.Data valueForKey:@"List"];
            for (NSDictionary *obj in list) {
                PBUnReadMesModel *mesModel = [PBUnReadMesModel yy_modelWithDictionary:obj];
                [self.dataList addObject:mesModel];
            }
            [JPUSHService setBadge:0];
            [UIApplication sharedApplication].applicationIconBadgeNumber = 0;
//            NSInteger mesCount = [[networkModel.Data valueForKey:@"msgCount"] integerValue];
//            self.unReadCount = mesCount;
//            [JPUSHService setBadge:mesCount];
//            [UIApplication sharedApplication].applicationIconBadgeNumber = mesCount;
            [self.tableView reloadData];
//            if ([[networkModel.Data valueForKey:@"msgArr"] count] < pageSize) {
//                self.tableView.mj_footer.hidden = YES;
//            }else {
//                self.tableView.mj_footer.hidden = NO;
//            }
        }else {
//            --self.pageIndex;
            [YJProgressHUD showMessage:@"加载失败" inView:nil];
        }
    }];
}
 
- (void)setupUI {
    self.title = @"消息";
    PBBackNavItem *backNav = [PBBackNavItem backNacItem];
    backNav.title = @" ";
    [backNav addTarget:self action:@selector(backItemAction) forControlEvents:UIControlEventTouchUpInside];
    UIBarButtonItem *backNavItem = [[UIBarButtonItem alloc] initWithCustomView:backNav];
    self.navigationItem.leftBarButtonItem = backNavItem;
    UIBarButtonItem *allReadItem = [[UIBarButtonItem alloc] initWithTitle:@"设为已读" style:UIBarButtonItemStylePlain target:self action:@selector(setAllRead)];
    self.navigationItem.rightBarButtonItem = allReadItem;
 
    [self.tableView registerClass:[PBUnReadTableViewCell class] forCellReuseIdentifier:cellID];
    self.tableView.rowHeight = UITableViewAutomaticDimension;
    self.tableView.estimatedRowHeight = 80.f;
    self.tableView.tableFooterView = [[UIView alloc] initWithFrame:CGRectZero];
}
- (void)backItemAction {
    [self dismissViewControllerAnimated:YES completion:nil];
//    [self.navigationController popViewControllerAnimated:YES];
}
- (void)setAllRead {
    UIAlertController *alertVC = [UIAlertController alertControllerWithTitle:@"全部设为已读状态" message:@"" preferredStyle:UIAlertControllerStyleActionSheet];
    UIAlertAction *setAllRedAction = [UIAlertAction actionWithTitle:@"设为已读" style:(UIAlertActionStyleDestructive) handler:^(UIAlertAction * _Nonnull action) {
        [YJProgressHUD showProgress:@"" inView:nil];
        [[PBNetworkTools sharedTools] RequestSetMsgTypeReadWithMsgType:@"" andCallBack:^(NSURLSessionDataTask *task, id response, NSError *error) {
            if (error) {
                [YJProgressHUD showMessage:@"全部设为已读设置失败" inView:nil];
                NSLog(@"%@",error);
                return;
            }
            NSString *str = [[NSString alloc] initWithData:response encoding:NSUTF8StringEncoding];
            PBNetworkModel *networkModel =[PBNetworkModel yy_modelWithJSON:str];
            if (networkModel.Ret == 1) {
                [YJProgressHUD hide];
                [self.tableView.mj_header beginRefreshing];
            } else {
                [YJProgressHUD showMessage:@"全部设为已读设置失败" inView:nil];
            }
        }];
    }];
    UIAlertAction *cancelAction = [UIAlertAction actionWithTitle:@"取消" style:UIAlertActionStyleDefault handler:nil];
    [alertVC addAction:setAllRedAction];
    [alertVC addAction:cancelAction];
    [self presentViewController:alertVC animated:YES completion:nil];
}
 
#pragma mark - Table view data source
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    return self.dataList.count;
}
 
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    PBUnReadTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:cellID forIndexPath:indexPath];
    cell.unReadMesModel = self.dataList[indexPath.row];
    return cell;
}
#pragma mark - UITableViewDelegate
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
    PBUnReadMesModel *unReadMesModel = self.dataList[indexPath.row];
    if ([unReadMesModel.isRead isEqualToString:@"0"]) {
        [YJProgressHUD showProgress:@"" inView:self.view];
        PBUnReadMesModel *unReadMesModel = self.dataList[indexPath.row];
        [[PBNetworkTools sharedTools] RequestSetReadWithMu_guid:unReadMesModel.mu_guid andCallBack:^(NSURLSessionDataTask *task, id response, NSError *error) {
            if (error) {
                [YJProgressHUD showMessage:@"设置失败" inView:self.view];
                return;
            }
            NSString *str = [[NSString alloc] initWithData:response encoding:NSUTF8StringEncoding];
            PBNetworkModel *networkModel = [PBNetworkModel yy_modelWithJSON:str];
            if (networkModel.Ret == 1) {
                unReadMesModel.isRead = @"1";
                [self.tableView reloadRowsAtIndexPaths:[NSArray arrayWithObject:indexPath] withRowAnimation:UITableViewRowAnimationFade];
                //加载详情  判断是否是删除
//                if (unReadMesModel.LogAndMsgType == 4 || unReadMesModel.LogAndMsgType == 5 || unReadMesModel.LogAndMsgType == 7 || unReadMesModel.LogAndMsgType == 8 || unReadMesModel.LogAndMsgType == 9 || unReadMesModel.LogAndMsgType == 10 || unReadMesModel.LogAndMsgType == 1007){
//                    if (unReadMesModel.mm_objid == nil || [unReadMesModel.mm_objid isEqualToString:@""]) {
//                        [self presentAlertVC];
//                    }else {
//                        [self getProjectParentIdWithModel:unReadMesModel];
//                    }
//                }else {
                    [YJProgressHUD hide];
                    [self presentAlertVC:unReadMesModel];
//                }
            }else {
                [YJProgressHUD showMessage:networkModel.Msg inView:self.view];
            }
        }];
    }else {
        //加载详情  判断是否是删除
//        if (unReadMesModel.LogAndMsgType == 4 || unReadMesModel.LogAndMsgType == 5 || unReadMesModel.LogAndMsgType == 7 || unReadMesModel.LogAndMsgType == 8 || unReadMesModel.LogAndMsgType == 9 || unReadMesModel.LogAndMsgType == 10 || unReadMesModel.LogAndMsgType == 1007){
//            if (unReadMesModel.mm_objid == nil || [unReadMesModel.mm_objid isEqualToString:@""]) {
//                [self presentAlertVC];
//            }else {
//                [self getProjectParentIdWithModel:unReadMesModel];
//            }
//        }else {
//            [self presentAlertVC];
//        }
    }
}
- (void)getProjectParentIdWithModel:(PBUnReadMesModel *)unReadMesModel {
//    [YJProgressHUD showCustomAnimation:@"" inview:self.view];
    [YJProgressHUD showProgress:@"" inView:self.view];
    [[PBNetworkTools sharedTools] getProjectParentIDWithProjectID:unReadMesModel.mme_ProjectID andCallBack:^(NSURLSessionDataTask *task, id response, NSError *error) {
        if (error) {
            NSLog(@"%@",error);
            [YJProgressHUD showMessage:@"加载状态失败" inView:self.view];
            return;
        }
        NSString *str = [[NSString alloc] initWithData:response encoding:NSUTF8StringEncoding];
        NSLog(@"%@",str);
        PBNetworkModel *networkModel = [PBNetworkModel yy_modelWithJSON:str];
        if (networkModel.Ret == 1){
            [self loadTypeAndStatusWith:networkModel.Data andModel:unReadMesModel];
        }else {
            [YJProgressHUD showMessage:@"加载状态、类型失败" inView:self.view];
        }
    }];
}
- (void)loadTypeAndStatusWith:(NSString *)parentId andModel:(PBUnReadMesModel *)unreadMesModel {
    __block NSArray *statusArr; __block NSArray *typeArr;
    dispatch_semaphore_t sem = dispatch_semaphore_create(0);
    NSInteger commandCount = 2;
    __block NSInteger httpFinishCount = 0;
    dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), ^{
        [[PBNetworkTools sharedTools] getIssueStatusWithCompanyId:parentId andCallBack:^(NSURLSessionDataTask *task, id response, NSError *error) {
            if (error) {
                NSLog(@"%@",error);
                [YJProgressHUD showMessage:@"加载状态失败" inView:self.view];
                return;
            }
            NSString *str = [[NSString alloc] initWithData:response encoding:NSUTF8StringEncoding];
            PBNetworkModel *networkModel = [PBNetworkModel yy_modelWithJSON:str];
            if ([networkModel.Msg isEqualToString:@"OK"]) {
                statusArr = networkModel.Data;
                if (++httpFinishCount == commandCount) {
                    dispatch_semaphore_signal(sem);
                }
            }else {
                [YJProgressHUD showMessage:@"加载状态失败" inView:self.view];
                return;
            }
        }];
        
        [[PBNetworkTools sharedTools] getIssueTypesWithCompanyId:parentId andCallBack:^(NSURLSessionDataTask *task, id response, NSError *error) {
            if (error) {
                NSLog(@"%@",error);
                [YJProgressHUD showMessage:@"加载类型失败" inView:self.view];
                return;
            }
            NSString *str = [[NSString alloc] initWithData:response encoding:NSUTF8StringEncoding];
            PBNetworkModel *networkModel = [PBNetworkModel yy_modelWithJSON:str];
            if ([networkModel.Msg isEqualToString:@"OK"]) {
                typeArr = networkModel.Data;
                if (++httpFinishCount == commandCount) {
                    dispatch_semaphore_signal(sem);
                }
            }else {
                [YJProgressHUD showMessage:@"加载类型失败" inView:self.view];
                return;
            }
        }];
        //如果全部请求没有返回则该线程会一直阻塞
        dispatch_semaphore_wait(sem, DISPATCH_TIME_FOREVER);
        NSLog(@"全部加载完成==================: %@", [NSThread currentThread]);
        dispatch_async(dispatch_get_main_queue(), ^{
            NSLog(@"更新视图UI update in main thread!:%@",[NSThread currentThread]);
            [YJProgressHUD hide];
            NSMutableArray *statusArrM = [[NSMutableArray alloc] init];
            for (NSDictionary *dict in statusArr) {
                PBIssueNavModel *issueNavModel = [PBIssueNavModel yy_modelWithDictionary:dict];
                [statusArrM addObject:issueNavModel];
            }
            NSMutableArray *typeArrM = [[NSMutableArray alloc] init];
            for (NSDictionary *dict in typeArr) {
                PBIssueNavModel *issueNavModel = [PBIssueNavModel yy_modelWithDictionary:dict];
                [typeArrM addObject:issueNavModel];
            }
            NSDictionary *statusAndTypeData = @{@"IssueStatus":statusArrM.mutableCopy, @"IssueTypes":typeArrM.mutableCopy};
            PBProjectModel *projectModel = [[PBProjectModel alloc] init];
            projectModel.organizeid = unreadMesModel.mme_ProjectID;
            projectModel.bimcomposerid = unreadMesModel.BIMComposerId;
            PBAddIssueController *detailsVC = [[PBAddIssueController alloc] init];
            detailsVC.isAddIssue = NO;
            detailsVC.projectModel = projectModel;
            PBIssueListModel *issueListModel = [[PBIssueListModel alloc] init];
            issueListModel.IssueId = unreadMesModel.mm_objid;
            detailsVC.issueListModel = issueListModel;
            detailsVC.statusAndTypeData = statusAndTypeData;
            PBNavigationController *nav = [[PBNavigationController alloc] initWithRootViewController:detailsVC];
            nav.modalPresentationStyle = UIModalPresentationFullScreen;
            [self presentViewController:nav animated:YES completion:nil];
        });
    });
}
- (NSArray *)tableView:(UITableView *)tableView editActionsForRowAtIndexPath:(nonnull NSIndexPath *)indexPath {
    UITableViewRowAction *deleteAction =[UITableViewRowAction rowActionWithStyle:(UITableViewRowActionStyleDestructive) title:@"设为已读" handler:^(UITableViewRowAction *action, NSIndexPath *indexPath) {
        [self deleteMesWithIndexPath:indexPath];
    }];
    deleteAction.backgroundColor = WarningColor;
   return @[deleteAction];
}
- (void)deleteMesWithIndexPath:(NSIndexPath *)indexPath {
    [YJProgressHUD showProgress:@"" inView:self.view];
    PBUnReadMesModel *unReadMesModel = self.dataList[indexPath.row];
    [[PBNetworkTools sharedTools] RequestSetReadWithMu_guid:unReadMesModel.mu_guid andCallBack:^(NSURLSessionDataTask *task, id response, NSError *error) {
        if (error) {
            [YJProgressHUD showMessage:@"设置失败" inView:self.view];
            return;
        }
        NSString *str = [[NSString alloc] initWithData:response encoding:NSUTF8StringEncoding];
        PBNetworkModel *networkModel = [PBNetworkModel yy_modelWithJSON:str];
        if (networkModel.Ret == 1) {
            [YJProgressHUD hide];
            unReadMesModel.isRead = @"1";
            [self.tableView reloadRowsAtIndexPaths:[NSArray arrayWithObject:indexPath] withRowAnimation:UITableViewRowAnimationFade];
        }else {
            [YJProgressHUD showMessage:networkModel.Msg inView:self.view];
        }
    }];
}
 
- (NSMutableArray *)dataList {
    if (_dataList == nil) {
        _dataList = [[NSMutableArray alloc] init];
    }
    return _dataList;
}
 
- (void)presentAlertVC:(PBUnReadMesModel *)unReadMesModel {
    NSString *message = @"我知道了!";
    if (unReadMesModel.LogAndMsgType == 47 || unReadMesModel.LogAndMsgType == 48 || unReadMesModel.LogAndMsgType == 49) {
        message = @"您收到一条任务流转提醒,如需处理请到网页端";
    }
    UIAlertController *alertVC = [UIAlertController alertControllerWithTitle:@"" message:message preferredStyle:UIAlertControllerStyleAlert];
    UIAlertAction *sureAction = [UIAlertAction actionWithTitle:@"确定" style:UIAlertActionStyleCancel handler:^(UIAlertAction * _Nonnull action) {
    }];
    [alertVC addAction:sureAction];
    [self presentViewController:alertVC animated:YES completion:nil];
}
 
- (void)loadExamineDetailWithReadMesModel:(PBUnReadMesModel *)unReadMesModel {
//    [YJProgressHUD showProgress:@"" inView:nil];
//    [[PBNetworkTools sharedTools] RequestGetExamineDetailWithExamineID:unReadMesModel.mm_objid andCallBack:^(NSURLResponse *response, id responseObject, NSError *error) {
//        if (error) {
//            NSLog(@"%@",error);
//            [YJProgressHUD showMessage:@"加载检查详情失败" inView:nil];
//            return;
//        }
//        NSString *str = [[NSString alloc] initWithData:responseObject encoding:NSUTF8StringEncoding];
//        PBResultModel *resultModel = [PBResultModel yy_modelWithJSON:str];
//        if ([resultModel.Message isEqualToString:@"Success"]) {
//            [YJProgressHUD hide];
//            if(resultModel.ResultData == nil) {
//                [YJProgressHUD showMessage:@"此检查已删除" inView:nil];
//                return;
//            }
//            PBAddExamineController *addExamineVC = [[PBAddExamineController alloc] init];
//            addExamineVC.isAddExamine = NO;
//            PBProjectModel *projectModel = [[PBProjectModel alloc] init];
//            projectModel.bimcomposerid = unReadMesModel.mm_organizeid;
//            addExamineVC.projectModel = projectModel;
//            addExamineVC.examineListDict = resultModel.ResultData;
//            addExamineVC.examineListModel = [PBExamineListModel yy_modelWithDictionary:resultModel.ResultData];
//            PBNavigationController *nav = [[PBNavigationController alloc] initWithRootViewController:addExamineVC];
//            nav.modalPresentationStyle = UIModalPresentationFullScreen;
//            [self presentViewController:nav animated:YES completion:nil];
//        }else {
//            [YJProgressHUD showMessage:@"加载检查详情失败" inView:nil];
//            return;
//        }
//    }];
}
- (PBRefreshGifHeader *)headerView {
    if(_headerView == nil) {
        _headerView = [[PBRefreshGifHeader alloc] init];
        __weak typeof(self) weakSelf = self;
        [self.headerView setRefreshingBlock:^{
            weakSelf.pageIndex = 0;
            [weakSelf.dataList removeAllObjects];
            [weakSelf loadUnReadMessage];
        }];
    }
    return _headerView;
}
/*
// Override to support conditional editing of the table view.
- (BOOL)tableView:(UITableView *)tableView canEditRowAtIndexPath:(NSIndexPath *)indexPath {
    // Return NO if you do not want the specified item to be editable.
    return YES;
}
*/
 
/*
// Override to support editing the table view.
- (void)tableView:(UITableView *)tableView commitEditingStyle:(UITableViewCellEditingStyle)editingStyle forRowAtIndexPath:(NSIndexPath *)indexPath {
    if (editingStyle == UITableViewCellEditingStyleDelete) {
        // Delete the row from the data source
        [tableView deleteRowsAtIndexPaths:@[indexPath] withRowAnimation:UITableViewRowAnimationFade];
    } else if (editingStyle == UITableViewCellEditingStyleInsert) {
        // Create a new instance of the appropriate class, insert it into the array, and add a new row to the table view
    }   
}
*/
 
/*
// Override to support rearranging the table view.
- (void)tableView:(UITableView *)tableView moveRowAtIndexPath:(NSIndexPath *)fromIndexPath toIndexPath:(NSIndexPath *)toIndexPath {
}
*/
 
/*
// Override to support conditional rearranging of the table view.
- (BOOL)tableView:(UITableView *)tableView canMoveRowAtIndexPath:(NSIndexPath *)indexPath {
    // Return NO if you do not want the item to be re-orderable.
    return YES;
}
*/
 
/*
#pragma mark - Navigation
 
// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/
 
@end