// // QXRoomDetailViewController.m // IsLandVoice // // Created by 启星 on 2025/3/5. // #import "QXRoomDetailViewController.h" #import "QXRoomDetailHeaderView.h" #import "QXRoomWaterListCell.h" #import "NSArray+YYAdd.h" #import "NSString+YYAdd.h" #import "QXRoomSubsidyViewController.h" #import "QXRoomWaterModel.h" #import "QXMineNetwork.h" @interface QXRoomDetailViewController () @property (nonatomic,strong)UITableView *tableView; @property (nonatomic,strong)NSMutableDictionary *dict; @property (nonatomic,strong)NSMutableArray *allDateArray; @property (nonatomic,strong)QXRoomDetailHeaderView *headerView; @property (nonatomic,strong)NSString* startTime; @property (nonatomic,strong)NSString* endTime; @end @implementation QXRoomDetailViewController - (void)viewDidLoad { [super viewDidLoad]; // Do any additional setup after loading the view. } -(void)viewDidAppear:(BOOL)animated{ [super viewDidAppear:animated]; self.navigationController.interactivePopGestureRecognizer.enabled = YES; } -(void)setNavgationItems{ [super setNavgationItems]; self.navigationItem.title = @"房间明细"; } -(void)initSubViews{ self.page = 1; UIButton *rightBtn = [[UIButton alloc] initWithFrame:CGRectMake(0, 0, 55, 44)]; [rightBtn setTitle:@"补贴明细" forState:(UIControlStateNormal)]; rightBtn.titleLabel.font = [UIFont systemFontOfSize:13]; [rightBtn setTitleColor:[UIColor colorWithHexString:@"#FF8ACC"] forState:(UIControlStateNormal)]; [rightBtn addTarget:self action:@selector(detailAction) forControlEvents:(UIControlEventTouchUpInside)]; self.navigationItem.rightBarButtonItem = [[UIBarButtonItem alloc] initWithCustomView:rightBtn]; [self.view addSubview:self.headerView]; [self.view addSubview:self.tableView]; self.dict = [NSMutableDictionary dictionary]; [self getPriceData]; } -(void)detailAction{ QXRoomSubsidyViewController *vc = [[QXRoomSubsidyViewController alloc] init]; vc.room_id = self.room_id; [self.navigationController pushViewController:vc animated:YES]; } -(void)getPriceData{ MJWeakSelf [QXMineNetwork roomWaterDetailWithRoomId:self.room_id start_time:self.startTime end_time:self.endTime page:self.page type:self.type successBlock:^(QXRoomDetailModel * _Nonnull model) { weakSelf.headerView.model = model; if (weakSelf.page == 1) { [weakSelf.dict removeAllObjects]; } NSArray *arr = [weakSelf.dict allKeys]; for (QXRoomWaterModel*md in model.list) { BOOL haveKey = NO; for (NSString *s in arr) { if ([s isEqualToString:md.time]) { haveKey = YES; break; } } if (!haveKey) { // key没有包含某个日期,设置日期的流水json [weakSelf.dict setObject:[md yy_modelToJSONString] forKey:md.time]; }else{ //有包含某个日期,先取出之前已有的数据 NSString *json = [weakSelf.dict objectForKey:md.time]; //转换为模型 QXRoomWaterModel *m = [QXRoomWaterModel yy_modelWithJSON:json]; //旧数据 NSMutableArray *newArr = [NSMutableArray arrayWithArray:m.list]; //添加新数据 [newArr addObjectsFromArray:md.list]; //赋值于历史数据 m.list = newArr; // 重新设置键值对 [weakSelf.dict setObject:[m yy_modelToJSONString] forKey:md.time]; } } NSArray *keys = [weakSelf.dict.allKeys sortedArrayUsingComparator:^NSComparisonResult(id _Nonnull obj1, id _Nonnull obj2) { // NSString *s1 = obj1; // NSString *s2 = obj2; NSDateFormatter *dateFormatter1 = [[NSDateFormatter alloc] init]; [dateFormatter1 setDateFormat:@"yyyy-MM-dd"]; NSDate *date1 = [dateFormatter1 dateFromString:obj1]; NSDateFormatter *dateFormatter2 = [[NSDateFormatter alloc] init]; [dateFormatter2 setDateFormat:@"yyyy-MM-dd"]; NSDate *date2 = [dateFormatter2 dateFromString:obj2]; NSComparisonResult result = [date1 compare:date2]; if (result == NSOrderedAscending) { return NSOrderedDescending; }else if (result == NSOrderedDescending){ return NSOrderedAscending; }else{ return NSOrderedSame; } }]; self.allDateArray = [NSMutableArray arrayWithArray:keys]; [weakSelf.tableView reloadData]; [weakSelf.tableView.mj_header endRefreshing]; if (model.list.count == 0) { weakSelf.tableView.mj_footer.state = MJRefreshStateNoMoreData; }else{ [weakSelf.tableView.mj_footer endRefreshing]; } } failBlock:^(NSError * _Nonnull error, NSString * _Nonnull msg) { showToast(msg); [weakSelf.tableView.mj_footer endRefreshing]; [weakSelf.tableView.mj_header endRefreshing]; }]; } -(NSInteger)numberOfSectionsInTableView:(UITableView *)tableView{ return self.allDateArray.count; } -(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{ NSString *json = [self.dict objectForKey:self.allDateArray[section]]; QXRoomWaterModel*model = [QXRoomWaterModel yy_modelWithJSON:json]; return model.list.count; } -(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{ QXRoomWaterListCell *cell = [QXRoomWaterListCell cellWithTableView:tableView]; NSString *json = [self.dict objectForKey:self.allDateArray[indexPath.section]]; //转换为数组 QXRoomWaterModel*m = [QXRoomWaterModel yy_modelWithJSON:json]; QXRoomWaterDetailModel *model = m.list[indexPath.row]; cell.model = model; return cell; } -(UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section{ NSString *time = [self.allDateArray objectAtIndex:section]; NSString *json = [self.dict objectForKey:time]; //转换为数组 QXRoomWaterModel*model = [QXRoomWaterModel yy_modelWithJSON:json]; UIView *header = [[UIView alloc] initWithFrame:CGRectMake(0, 0, SCREEN_WIDTH, 25)]; header.backgroundColor = [UIColor whiteColor]; UILabel *timeLabel = [[UILabel alloc] initWithFrame:CGRectMake(16, 0, 130, 25)]; timeLabel.font = [UIFont systemFontOfSize:13]; timeLabel.textColor = [UIColor colorWithHexString:@"#333333"]; timeLabel.text = time; [header addSubview:timeLabel]; UILabel *waterLabel = [[UILabel alloc] initWithFrame:CGRectMake(SCREEN_WIDTH-16-130, 0, SCREEN_WIDTH-130-32, 25)]; waterLabel.font = [UIFont systemFontOfSize:13]; waterLabel.textColor = [UIColor colorWithHexString:@"#333333"]; NSString *total_earning = [NSString stringWithFormat:@"%.2f",model.total_earning]; NSString *total_price = [NSString stringWithFormat:@"%.2f",model.total_price]; NSString *s = [NSString stringWithFormat:@"收益 %@ 流水 %@",total_earning,total_price]; NSMutableAttributedString *attr = [[NSMutableAttributedString alloc] initWithString:s]; [attr yy_setColor:[UIColor colorWithHexString:@"#FF8ACC"] range:[s rangeOfString:total_earning]]; [attr yy_setColor:[UIColor colorWithHexString:@"#FF8ACC"] range:[s rangeOfString:total_price]]; // waterLabel.text = [NSString stringWithFormat:@"收益 %@ 流水 %@",model.total_earning,model.total_price]; waterLabel.attributedText = attr; waterLabel.textAlignment = NSTextAlignmentRight; [waterLabel sizeToFit]; waterLabel.frame = CGRectMake(SCREEN_WIDTH-16-waterLabel.size.width, 0, waterLabel.size.width, 25); [header addSubview:waterLabel]; return header; } -(CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section{ return 25; } -(UIView *)tableView:(UITableView *)tableView viewForFooterInSection:(NSInteger)section{ return [UIView new]; } -(CGFloat)tableView:(UITableView *)tableView heightForFooterInSection:(NSInteger)section{ return 0.01; } -(UITableView *)tableView{ if (!_tableView) { _tableView = [[UITableView alloc] initWithFrame:CGRectMake(0, self.headerView.bottom, SCREEN_WIDTH, SCREEN_HEIGHT-self.headerView.bottom) style:(UITableViewStylePlain)]; _tableView.dataSource = self; _tableView.delegate = self; _tableView.backgroundColor = [UIColor clearColor]; _tableView.tableFooterView = [UIView new]; _tableView.separatorStyle = UITableViewCellSeparatorStyleNone; _tableView.rowHeight = 65; if (@available(iOS 15.0, *)) { _tableView.sectionHeaderTopPadding = 0; } else { // Fallback on earlier versions } MJWeakSelf _tableView.mj_header = [MJRefreshNormalHeader headerWithRefreshingBlock:^{ weakSelf.page = 1; [weakSelf getPriceData]; }]; _tableView.mj_footer = [MJRefreshBackNormalFooter footerWithRefreshingBlock:^{ weakSelf.page++; [weakSelf getPriceData]; }]; } return _tableView; } -(NSMutableArray *)allDateArray{ if (!_allDateArray) { _allDateArray = [NSMutableArray array]; } return _allDateArray; } -(QXRoomDetailHeaderView *)headerView{ if (!_headerView) { UIImage*img = [UIImage imageNamed:@"mine_room_water_bg"]; CGFloat height = (SCREEN_WIDTH-32)/img.size.width*img.size.height; _headerView = [[QXRoomDetailHeaderView alloc] initWithFrame:CGRectMake(0, 0, SCREEN_WIDTH, 11+ kSafeAreaTop+44 + height)]; MJWeakSelf _headerView.chooseDateBlock = ^(NSString * _Nonnull startTime, NSString * _Nonnull endTime) { weakSelf.startTime = startTime; weakSelf.endTime = endTime; weakSelf.page = 1; [weakSelf getPriceData]; }; } return _headerView; } /* #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