SPSBPurchasersListTableViewController.m 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310
  1. //
  2. // SPSBPurchasersListTableViewController.m
  3. // 我的社保
  4. //
  5. // Created by shanp on 2021/5/14.
  6. //
  7. #import "SPSBPurchasersListTableViewController.h"
  8. #import "SPSBUIGeneralHeader.h"
  9. #import "SPSBBusinessManager.h"
  10. #import "SPSBPurchasersModel.h"
  11. #import "UITableView+SPSBDefault.h"
  12. #import "SPSBPurchasersListTableViewCell.h"
  13. #import "SPSBConfirmAlertViewController.h"
  14. #import "UIViewController+SPSBNetworkManager.h"
  15. #import "SPSBSQLProfile.h"
  16. #import "SPSBMinePurchasersDetailsViewController.h"
  17. #import "SPSBCreatePurchaserViewController.h"
  18. @interface SPSBPurchasersListTableViewController () {
  19. NSMutableArray<SPSBPurchasersModel *> *_dataArray;
  20. bool _firstLoad;
  21. NSUInteger _chose;
  22. UIView *_headerView;
  23. UIView *_footerView;
  24. }
  25. @end
  26. @implementation SPSBPurchasersListTableViewController
  27. - (instancetype)init {
  28. self = [super initWithStyle:UITableViewStyleGrouped backgroundColor:SPSBTableViewBackgroundColorGray];
  29. if (!self) return nil;
  30. return self;
  31. }
  32. - (void)viewDidLoad {
  33. [super viewDidLoad];
  34. [self initData];
  35. [self setupTableView];
  36. }
  37. - (void)viewWillAppear:(BOOL)animated {
  38. [super viewWillAppear:animated];
  39. if (_firstLoad) {
  40. _firstLoad = true;
  41. } else {
  42. _dataArray = [spsb_purchasersArray() mutableCopy];
  43. [self.tableView reloadData];
  44. if (_dataArray.count == 0) {
  45. [self setDefaultView];
  46. } else {
  47. self.tableView.backgroundColor = spsb_F5F5F5_color();
  48. [self.tableView removeDefaultFootView];
  49. }
  50. }
  51. }
  52. - (void)initData {
  53. _firstLoad = true;
  54. _dataArray = [spsb_purchasersArray() mutableCopy];
  55. if (_spsb_type != SPSBPurchasersListTypeManage) {
  56. _chose = 0;
  57. }
  58. }
  59. #pragma mark - Action
  60. - (void)addPurchasersAction {
  61. SPSBCreatePurchaserViewController *vc = SPSBCreatePurchaserViewController.new;
  62. vc.spsb_type = _spsb_bussessType;
  63. [self.navigationController pushViewController:vc animated:true];
  64. }
  65. - (void)editPurchasers:(SPSBPurchasersListTableViewCell *)cell {
  66. NSIndexPath *indexPath = [self.tableView indexPathForCell:cell];
  67. if (!indexPath || indexPath.row >= _dataArray.count) return;
  68. SPSBMinePurchasersDetailsViewController *vc = SPSBMinePurchasersDetailsViewController.new;
  69. vc.spsb_purchasers = _dataArray[indexPath.row];
  70. vc.spsb_isEdit = true;
  71. @weakify(self)
  72. vc.spsb_hadChnaged = ^{
  73. @strongify(self)
  74. self->_dataArray = [spsb_purchasersArray() mutableCopy];
  75. [self.tableView reloadData];
  76. };
  77. [self.navigationController pushViewController:vc animated:true];
  78. }
  79. - (void)deletePurchasers:(SPSBPurchasersListTableViewCell *)cell {
  80. NSIndexPath *indexPath = [self.tableView indexPathForCell:cell];
  81. if (!indexPath || indexPath.row >= _dataArray.count) return;
  82. SPSBPurchasersModel *model = _dataArray[indexPath.row];
  83. SPSBConfirmAlertViewController *vc = [SPSBConfirmAlertViewController alertWithTitle:@"删除后将无法恢复" content:@"是否删除TA"];
  84. [vc setCancelButtonTitle:nil titleColor:nil action:^{
  85. }];
  86. @weakify(self)
  87. [vc setConfirmButtonTitle:@"删除" titleColor:nil action:^{
  88. @strongify(self)
  89. [self->_dataArray removeObjectAtIndex:indexPath.row];
  90. [self.tableView reloadData];
  91. [self deletePurchasersWithId:model.spsb_id];
  92. [self handelCacheWithId:model.spsb_id];
  93. }];
  94. [self presentViewController:vc animated:false completion:nil];
  95. }
  96. - (void)handelCacheWithId:(NSString *)purchasersId {
  97. dispatch_async(dispatch_get_global_queue(0, 0), ^{
  98. @synchronized(spsb_purchasersArray()) {
  99. NSMutableArray *arr;
  100. NSArray *cacheArray = [JXHFMDBManager searchWithPath:SPSBShanpSocialInsuranceLocationSQL key:SPSBmyPurchasersSQLKey condition:^id(FMDatabase *database) {
  101. return [database executeQuery:@"select * from cache_data where key = ?", SPSBmyPurchasersSQLKey];
  102. }];
  103. if (cacheArray) {
  104. arr = [NSMutableArray arrayWithArray:cacheArray];
  105. for (NSInteger i = 0; i < arr.count; i ++) {
  106. NSDictionary *dic = arr[i];
  107. if ([[NSString stringWithFormat:@"%@", dic[@"id"]] isEqualToString:purchasersId]) {
  108. [arr removeObject:dic];
  109. break;
  110. }
  111. }
  112. [JXHFMDBManager updateWithPath:SPSBShanpSocialInsuranceLocationSQL condition:^id(FMDatabase *database) {
  113. return @([database executeUpdate:@"delete from cache_data where key = ?", SPSBmyPurchasersSQLKey]);
  114. }];
  115. spsb_setPurchasersArray([SPSBPurchasersModel getModelListWithArray:arr]);
  116. [JXHFMDBManager updateWithPath:SPSBShanpSocialInsuranceLocationSQL condition:^id(FMDatabase *database) {
  117. return @([database executeUpdate:@"insert into cache_data values(?, ?)", SPSBmyPurchasersSQLKey, jxh_archiver(arr, SPSBmyPurchasersSQLKey)]);
  118. }];
  119. }
  120. }
  121. });
  122. }
  123. - (SPSBPurchasersModel *)spsb_chosePurchasers {
  124. if (_chose >= _dataArray.count) return nil;
  125. return _dataArray[_chose];
  126. }
  127. #pragma mark - Network Action
  128. - (void)deletePurchasersWithId:(NSString *)purchasersId {
  129. [self networkUseMethod:SPSBNetworkMethodPOST loadingTips:nil isLogin:true url:spsb_appUrl(SPSBUrlDeletePurchasers) filtrationKey:purchasersId urlParameters:nil parameters:^NSDictionary * _Nonnull{
  130. return @{@"id": purchasersId};
  131. } success:nil failure:nil];
  132. }
  133. #pragma mark - Delegate & DataSource
  134. static NSString * const reuseIdentifier = @"SPSBPurchasersListTableViewCell";
  135. - (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
  136. SPSBPurchasersListTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:reuseIdentifier];
  137. if (!cell) {
  138. cell = [[SPSBPurchasersListTableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:reuseIdentifier type:_spsb_type];
  139. @weakify(self)
  140. cell.spsb_action = ^(SPSBPurchasersListTableViewCell * _Nonnull cell, bool isEdit) {
  141. @strongify(self)
  142. if (isEdit) {
  143. [self editPurchasers:cell];
  144. } else {
  145. [self deletePurchasers:cell];
  146. }
  147. };
  148. }
  149. [cell reloadData:_dataArray[indexPath.row] isFirstRow:indexPath.row == 0 isChose:indexPath.row == _chose];
  150. return cell;
  151. }
  152. - (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
  153. return _dataArray.count;
  154. }
  155. - (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
  156. [tableView deselectRowAtIndexPath:indexPath animated:false];
  157. if (_spsb_type == SPSBPurchasersListTypeManage) {
  158. SPSBMinePurchasersDetailsViewController *vc = SPSBMinePurchasersDetailsViewController.new;
  159. vc.spsb_purchasers = _dataArray[indexPath.row];
  160. vc.spsb_isEdit = false;
  161. @weakify(self)
  162. vc.spsb_hadChnaged = ^{
  163. @strongify(self)
  164. self->_dataArray = [spsb_purchasersArray() mutableCopy];
  165. [self.tableView reloadData];
  166. };
  167. [self.navigationController pushViewController:vc animated:true];
  168. } else {
  169. _chose = indexPath.row;
  170. [self.tableView reloadData];
  171. }
  172. }
  173. - (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section {
  174. return 90;
  175. }
  176. - (UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section {
  177. return [self headerView];
  178. }
  179. - (CGFloat)tableView:(UITableView *)tableView heightForFooterInSection:(NSInteger)section {
  180. if (_spsb_type == SPSBPurchasersListTypePurchase) {
  181. return 112;
  182. }
  183. return jxh_onePixe();
  184. }
  185. - (UIView *)tableView:(UITableView *)tableView viewForFooterInSection:(NSInteger)section {
  186. if (_spsb_type == SPSBPurchasersListTypePurchase) {
  187. return [self footerView];
  188. }
  189. return UIView.new;
  190. }
  191. #pragma mark - UI
  192. - (void)setupTableView {
  193. self.tableView.rowHeight = 112.f;
  194. if (_dataArray.count == 0) {
  195. [self setDefaultView];
  196. }
  197. }
  198. - (UIView *)footerView {
  199. if (!_footerView) {
  200. _footerView = UIView.new;
  201. _footerView.backgroundColor = spsb_F5F5F5_color();
  202. UIView *bg = UIView.new;
  203. bg.backgroundColor = spsb_FFFFFF_color(1.f);
  204. [bg setLayerCornerRadius:4 clipToBounds:false];
  205. [_footerView addSubview:bg];
  206. [bg makeConstraints:^(JXHConstraintMaker *make) {
  207. make.top.equalTo(0);
  208. make.leading.equalTo(12);
  209. make.trailing.equalTo(-12);
  210. make.height.equalTo(100);
  211. }];
  212. UIView *contentView = UIView.new;
  213. contentView.backgroundColor = spsb_FFFFFF_color(1.f);
  214. [bg addSubview: contentView];
  215. [contentView makeConstraints:^(JXHConstraintMaker *make) {
  216. make.center.equalTo(bg);
  217. }];
  218. UIImageView *icon = [[UIImageView alloc] initWithImage:jxh_getImage(purchasers_add)];
  219. [contentView addSubview:icon];
  220. [icon makeConstraints:^(JXHConstraintMaker *make) {
  221. make.leading.equalTo(0);
  222. make.top.greaterThanOrEqualTo(0);
  223. make.bottom.lessThanOrEqualTo(0);
  224. make.centerY.equalTo(contentView);
  225. }];
  226. UILabel *label = [UILabel convenienceWithFont:spsb_font(15) text:@"新增人员" textColor:spsb_3296FB_color()];
  227. [contentView addSubview:label];
  228. [label makeConstraints:^(JXHConstraintMaker *make) {
  229. make.trailing.equalTo(0);
  230. make.leading.equalTo(icon.trailing).offset(8);
  231. make.top.greaterThanOrEqualTo(0);
  232. make.bottom.lessThanOrEqualTo(0);
  233. make.centerY.equalTo(contentView);
  234. }];
  235. UITapGestureRecognizer *tap = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(addPurchasersAction)];
  236. [_footerView addGestureRecognizer:tap];
  237. }
  238. return _footerView;
  239. }
  240. - (UIView *)headerView {
  241. if (!_headerView) {
  242. _headerView = [[UIView alloc] init];
  243. _headerView.backgroundColor = spsb_28AFF0_color();
  244. UIImageView *icon = [[UIImageView alloc] initWithImage:jxh_getImage(pei_c)];
  245. [_headerView addSubview:icon];
  246. [icon makeConstraints:^(JXHConstraintMaker *make) {
  247. make.trailing.equalTo(-22);
  248. make.bottom.equalTo(0);
  249. }];
  250. UILabel *firstLabel = [UILabel convenienceWithFont:spsb_font(18) text:_spsb_type == SPSBPurchasersListTypeManage ? @"人员信息管理" : @"选择人员" textColor:spsb_FFFFFF_color(1.f)];
  251. [_headerView addSubview:firstLabel];
  252. [firstLabel makeConstraints:^(JXHConstraintMaker *make) {
  253. make.leading.equalTo(15);
  254. make.top.equalTo(20);
  255. }];
  256. UILabel *secondLabel = [UILabel convenienceWithFont:spsb_font(12) text:_spsb_type == SPSBPurchasersListTypeManage ? @"可对人员信息进行编辑、查看、删除" : @"系统会默认上次最后购买的人" textColor:spsb_FFFFFF_color(1.f)];
  257. [_headerView addSubview:secondLabel];
  258. [secondLabel makeConstraints:^(JXHConstraintMaker *make) {
  259. make.leading.equalTo(15);
  260. make.top.equalTo(firstLabel.bottom).offset(5);
  261. }];
  262. }
  263. return _headerView;
  264. }
  265. - (void)setDefaultView {
  266. self.tableView.backgroundColor = spsb_FFFFFF_color(1.f);
  267. [self.tableView setDefaultFootViewWithError:nil image:jxh_getImage(empty_purchasers) height:jxh_screenHeight() - 90 action:^{
  268. }];
  269. }
  270. @end