// // SPSBPurchasersListTableViewController.m // 我的社保 // // Created by shanp on 2021/5/14. // #import "SPSBPurchasersListTableViewController.h" #import "SPSBUIGeneralHeader.h" #import "SPSBBusinessManager.h" #import "SPSBPurchasersModel.h" #import "UITableView+SPSBDefault.h" #import "SPSBPurchasersListTableViewCell.h" #import "SPSBConfirmAlertViewController.h" #import "UIViewController+SPSBNetworkManager.h" #import "SPSBSQLProfile.h" #import "SPSBMinePurchasersDetailsViewController.h" #import "SPSBCreatePurchaserViewController.h" @interface SPSBPurchasersListTableViewController () { NSMutableArray *_dataArray; bool _firstLoad; NSUInteger _chose; UIView *_headerView; UIView *_footerView; } @end @implementation SPSBPurchasersListTableViewController - (instancetype)init { self = [super initWithStyle:UITableViewStyleGrouped backgroundColor:SPSBTableViewBackgroundColorGray]; if (!self) return nil; return self; } - (void)viewDidLoad { [super viewDidLoad]; [self initData]; [self setupTableView]; } - (void)viewWillAppear:(BOOL)animated { [super viewWillAppear:animated]; if (_firstLoad) { _firstLoad = true; } else { _dataArray = [spsb_purchasersArray() mutableCopy]; [self.tableView reloadData]; if (_dataArray.count == 0) { [self setDefaultView]; } else { self.tableView.backgroundColor = spsb_F5F5F5_color(); [self.tableView removeDefaultFootView]; } } } - (void)initData { _firstLoad = true; _dataArray = [spsb_purchasersArray() mutableCopy]; if (_spsb_type != SPSBPurchasersListTypeManage) { _chose = 0; } } #pragma mark - Action - (void)addPurchasersAction { SPSBCreatePurchaserViewController *vc = SPSBCreatePurchaserViewController.new; vc.spsb_type = _spsb_bussessType; [self.navigationController pushViewController:vc animated:true]; } - (void)editPurchasers:(SPSBPurchasersListTableViewCell *)cell { NSIndexPath *indexPath = [self.tableView indexPathForCell:cell]; if (!indexPath || indexPath.row >= _dataArray.count) return; SPSBMinePurchasersDetailsViewController *vc = SPSBMinePurchasersDetailsViewController.new; vc.spsb_purchasers = _dataArray[indexPath.row]; vc.spsb_isEdit = true; @weakify(self) vc.spsb_hadChnaged = ^{ @strongify(self) self->_dataArray = [spsb_purchasersArray() mutableCopy]; [self.tableView reloadData]; }; [self.navigationController pushViewController:vc animated:true]; } - (void)deletePurchasers:(SPSBPurchasersListTableViewCell *)cell { NSIndexPath *indexPath = [self.tableView indexPathForCell:cell]; if (!indexPath || indexPath.row >= _dataArray.count) return; SPSBPurchasersModel *model = _dataArray[indexPath.row]; SPSBConfirmAlertViewController *vc = [SPSBConfirmAlertViewController alertWithTitle:@"删除后将无法恢复" content:@"是否删除TA"]; [vc setCancelButtonTitle:nil titleColor:nil action:^{ }]; @weakify(self) [vc setConfirmButtonTitle:@"删除" titleColor:nil action:^{ @strongify(self) [self->_dataArray removeObjectAtIndex:indexPath.row]; [self.tableView reloadData]; [self deletePurchasersWithId:model.spsb_id]; [self handelCacheWithId:model.spsb_id]; }]; [self presentViewController:vc animated:false completion:nil]; } - (void)handelCacheWithId:(NSString *)purchasersId { dispatch_async(dispatch_get_global_queue(0, 0), ^{ @synchronized(spsb_purchasersArray()) { NSMutableArray *arr; NSArray *cacheArray = [JXHFMDBManager searchWithPath:SPSBShanpSocialInsuranceLocationSQL key:SPSBmyPurchasersSQLKey condition:^id(FMDatabase *database) { return [database executeQuery:@"select * from cache_data where key = ?", SPSBmyPurchasersSQLKey]; }]; if (cacheArray) { arr = [NSMutableArray arrayWithArray:cacheArray]; for (NSInteger i = 0; i < arr.count; i ++) { NSDictionary *dic = arr[i]; if ([[NSString stringWithFormat:@"%@", dic[@"id"]] isEqualToString:purchasersId]) { [arr removeObject:dic]; break; } } [JXHFMDBManager updateWithPath:SPSBShanpSocialInsuranceLocationSQL condition:^id(FMDatabase *database) { return @([database executeUpdate:@"delete from cache_data where key = ?", SPSBmyPurchasersSQLKey]); }]; spsb_setPurchasersArray([SPSBPurchasersModel getModelListWithArray:arr]); [JXHFMDBManager updateWithPath:SPSBShanpSocialInsuranceLocationSQL condition:^id(FMDatabase *database) { return @([database executeUpdate:@"insert into cache_data values(?, ?)", SPSBmyPurchasersSQLKey, jxh_archiver(arr, SPSBmyPurchasersSQLKey)]); }]; } } }); } - (SPSBPurchasersModel *)spsb_chosePurchasers { if (_chose >= _dataArray.count) return nil; return _dataArray[_chose]; } #pragma mark - Network Action - (void)deletePurchasersWithId:(NSString *)purchasersId { [self networkUseMethod:SPSBNetworkMethodPOST loadingTips:nil isLogin:true url:spsb_appUrl(SPSBUrlDeletePurchasers) filtrationKey:purchasersId urlParameters:nil parameters:^NSDictionary * _Nonnull{ return @{@"id": purchasersId}; } success:nil failure:nil]; } #pragma mark - Delegate & DataSource static NSString * const reuseIdentifier = @"SPSBPurchasersListTableViewCell"; - (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath { SPSBPurchasersListTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:reuseIdentifier]; if (!cell) { cell = [[SPSBPurchasersListTableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:reuseIdentifier type:_spsb_type]; @weakify(self) cell.spsb_action = ^(SPSBPurchasersListTableViewCell * _Nonnull cell, bool isEdit) { @strongify(self) if (isEdit) { [self editPurchasers:cell]; } else { [self deletePurchasers:cell]; } }; } [cell reloadData:_dataArray[indexPath.row] isFirstRow:indexPath.row == 0 isChose:indexPath.row == _chose]; return cell; } - (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section { return _dataArray.count; } - (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath { [tableView deselectRowAtIndexPath:indexPath animated:false]; if (_spsb_type == SPSBPurchasersListTypeManage) { SPSBMinePurchasersDetailsViewController *vc = SPSBMinePurchasersDetailsViewController.new; vc.spsb_purchasers = _dataArray[indexPath.row]; vc.spsb_isEdit = false; @weakify(self) vc.spsb_hadChnaged = ^{ @strongify(self) self->_dataArray = [spsb_purchasersArray() mutableCopy]; [self.tableView reloadData]; }; [self.navigationController pushViewController:vc animated:true]; } else { _chose = indexPath.row; [self.tableView reloadData]; } } - (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section { return 90; } - (UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section { return [self headerView]; } - (CGFloat)tableView:(UITableView *)tableView heightForFooterInSection:(NSInteger)section { if (_spsb_type == SPSBPurchasersListTypePurchase) { return 112; } return jxh_onePixe(); } - (UIView *)tableView:(UITableView *)tableView viewForFooterInSection:(NSInteger)section { if (_spsb_type == SPSBPurchasersListTypePurchase) { return [self footerView]; } return UIView.new; } #pragma mark - UI - (void)setupTableView { self.tableView.rowHeight = 112.f; if (_dataArray.count == 0) { [self setDefaultView]; } } - (UIView *)footerView { if (!_footerView) { _footerView = UIView.new; _footerView.backgroundColor = spsb_F5F5F5_color(); UIView *bg = UIView.new; bg.backgroundColor = spsb_FFFFFF_color(1.f); [bg setLayerCornerRadius:4 clipToBounds:false]; [_footerView addSubview:bg]; [bg makeConstraints:^(JXHConstraintMaker *make) { make.top.equalTo(0); make.leading.equalTo(12); make.trailing.equalTo(-12); make.height.equalTo(100); }]; UIView *contentView = UIView.new; contentView.backgroundColor = spsb_FFFFFF_color(1.f); [bg addSubview: contentView]; [contentView makeConstraints:^(JXHConstraintMaker *make) { make.center.equalTo(bg); }]; UIImageView *icon = [[UIImageView alloc] initWithImage:jxh_getImage(purchasers_add)]; [contentView addSubview:icon]; [icon makeConstraints:^(JXHConstraintMaker *make) { make.leading.equalTo(0); make.top.greaterThanOrEqualTo(0); make.bottom.lessThanOrEqualTo(0); make.centerY.equalTo(contentView); }]; UILabel *label = [UILabel convenienceWithFont:spsb_font(15) text:@"新增人员" textColor:spsb_3296FB_color()]; [contentView addSubview:label]; [label makeConstraints:^(JXHConstraintMaker *make) { make.trailing.equalTo(0); make.leading.equalTo(icon.trailing).offset(8); make.top.greaterThanOrEqualTo(0); make.bottom.lessThanOrEqualTo(0); make.centerY.equalTo(contentView); }]; UITapGestureRecognizer *tap = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(addPurchasersAction)]; [_footerView addGestureRecognizer:tap]; } return _footerView; } - (UIView *)headerView { if (!_headerView) { _headerView = [[UIView alloc] init]; _headerView.backgroundColor = spsb_28AFF0_color(); UIImageView *icon = [[UIImageView alloc] initWithImage:jxh_getImage(pei_c)]; [_headerView addSubview:icon]; [icon makeConstraints:^(JXHConstraintMaker *make) { make.trailing.equalTo(-22); make.bottom.equalTo(0); }]; UILabel *firstLabel = [UILabel convenienceWithFont:spsb_font(18) text:_spsb_type == SPSBPurchasersListTypeManage ? @"人员信息管理" : @"选择人员" textColor:spsb_FFFFFF_color(1.f)]; [_headerView addSubview:firstLabel]; [firstLabel makeConstraints:^(JXHConstraintMaker *make) { make.leading.equalTo(15); make.top.equalTo(20); }]; UILabel *secondLabel = [UILabel convenienceWithFont:spsb_font(12) text:_spsb_type == SPSBPurchasersListTypeManage ? @"可对人员信息进行编辑、查看、删除" : @"系统会默认上次最后购买的人" textColor:spsb_FFFFFF_color(1.f)]; [_headerView addSubview:secondLabel]; [secondLabel makeConstraints:^(JXHConstraintMaker *make) { make.leading.equalTo(15); make.top.equalTo(firstLabel.bottom).offset(5); }]; } return _headerView; } - (void)setDefaultView { self.tableView.backgroundColor = spsb_FFFFFF_color(1.f); [self.tableView setDefaultFootViewWithError:nil image:jxh_getImage(empty_purchasers) height:jxh_screenHeight() - 90 action:^{ }]; } @end