LookHistoryVC.m 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434
  1. //
  2. // LookHistoryVC.m
  3. // yunbaolive
  4. //
  5. // Created by ybRRR on 2020/1/18.
  6. // Copyright © 2020 cat. All rights reserved.
  7. //
  8. #import "LookHistoryVC.h"
  9. #import "LookHistoryCell.h"
  10. #import "LookHistoryModel.h"
  11. #import "CommodityDetailVC.h"
  12. #import "HistoryListModel.h"
  13. #import "NSObject+MJKeyValue.h"
  14. #import "OutsideGoodsDetailVC.h"
  15. @interface LookHistoryVC ()<UITableViewDataSource, UITableViewDelegate>
  16. {
  17. BOOL manageSel;
  18. BOOL isAllSel;
  19. BOOL sectionSel;
  20. UIView *bottomView;
  21. int pageIndex;
  22. //已选的商品集合
  23. NSMutableArray *selectHistory;
  24. UIButton *allSelBtn;
  25. UIButton *sectionBtn;
  26. NSInteger sectionIndex;
  27. NSMutableArray *sectionArr;
  28. UIButton *deleteBtn;
  29. }
  30. @property (nonatomic, strong)UITableView *historyTable;
  31. @property (nonatomic, strong)UIView *headerView;
  32. @property (nonatomic, strong)NSMutableArray *infos;
  33. @property (nonatomic,strong)NSMutableDictionary *dic;
  34. @property (nonatomic, strong)NSMutableArray *listArr;
  35. @end
  36. @implementation LookHistoryVC
  37. /// 懒加载indexpath字典
  38. - (NSMutableDictionary *)dic {
  39. if (_dic == nil) {
  40. _dic = [NSMutableDictionary dictionary];
  41. }
  42. return _dic;
  43. }
  44. #pragma mark-------请求数据---------
  45. -(void)requstData{
  46. NSString *url = [purl stringByAppendingFormat:@"?service=Buyer.getGoodsVisitRecord"];
  47. NSDictionary *dic = @{
  48. @"uid":[Config getOwnID],
  49. @"token":[Config getOwnToken],
  50. @"p":@(pageIndex)
  51. };
  52. [YBNetworking postWithUrl:@"Buyer.getGoodsVisitRecord" Dic:dic Suc:^(int code, id info, NSString *msg) {
  53. [self.historyTable.mj_header endRefreshing];
  54. [self.historyTable.mj_footer endRefreshing];
  55. if (code == 0) {
  56. NSArray *infoArr = info;
  57. if (pageIndex == 1) {
  58. [self.infos removeAllObjects];
  59. [sectionArr removeAllObjects];
  60. [selectHistory removeAllObjects];
  61. [_listArr removeAllObjects];
  62. NSMutableArray *array = [NSMutableArray array];
  63. for (NSDictionary *dic in infoArr) {
  64. HistoryListModel *model = [HistoryListModel mj_objectWithKeyValues:dic];
  65. [array addObject:model];
  66. }
  67. [_listArr addObjectsFromArray:array];
  68. if (infoArr.count < 1) {
  69. self.rightBtn.hidden = YES;
  70. [PublicView showImgNoData:self.historyTable name:@"shop_无数据" text:YZMsg(@"近期暂无浏览记录")];
  71. }else{
  72. self.rightBtn.hidden = NO;
  73. [PublicView hiddenImgNoData:self.historyTable];
  74. }
  75. }else{
  76. self.rightBtn.hidden = NO;
  77. NSMutableArray *array = [NSMutableArray array];
  78. for (NSDictionary *dic in infoArr) {
  79. HistoryListModel *model = [HistoryListModel mj_objectWithKeyValues:dic];
  80. [array addObject:model];
  81. }
  82. [_listArr addObjectsFromArray:array];
  83. }
  84. [self.infos addObjectsFromArray:infoArr];
  85. [self.historyTable reloadData];
  86. }else{
  87. [MBProgressHUD showError:msg];
  88. }
  89. } Fail:^(id fail) {
  90. [self.historyTable.mj_header endRefreshing];
  91. [self.historyTable.mj_footer endRefreshing];
  92. }];
  93. }
  94. -(void)initData{
  95. pageIndex = 1;
  96. self.infos = [NSMutableArray array];
  97. selectHistory = [NSMutableArray array];
  98. sectionArr = [NSMutableArray array];
  99. _listArr = [NSMutableArray array];
  100. }
  101. - (void)viewDidLoad {
  102. [super viewDidLoad];
  103. self.titleL.text = YZMsg(@"浏览记录");
  104. self.rightBtn.hidden = NO;
  105. [self.rightBtn setTitle:YZMsg(@"管理") forState:0];
  106. [self.rightBtn setTitleColor:[UIColor blackColor] forState:0];
  107. [self initData];
  108. [self.view addSubview:self.historyTable];
  109. [self creatBottomView];
  110. [self requstData];
  111. }
  112. -(void)rightBtnClick
  113. {
  114. manageSel = !manageSel;
  115. if (manageSel) {
  116. [self.rightBtn setTitle:YZMsg(@"取消") forState:0];
  117. bottomView.hidden = NO;
  118. self.historyTable.frame = CGRectMake(0, 64+statusbarHeight, _window_width, _window_height-64-statusbarHeight-50);
  119. }else{
  120. [self.rightBtn setTitle:YZMsg(@"管理") forState:0];
  121. bottomView.hidden = YES;
  122. self.historyTable.frame = CGRectMake(0, 64+statusbarHeight, _window_width, _window_height-64-statusbarHeight);
  123. }
  124. [self.historyTable reloadData];
  125. }
  126. -(void)creatBottomView{
  127. bottomView = [[UIView alloc]initWithFrame:CGRectMake(0, _window_height-50, _window_width, 50)];
  128. [self.view addSubview:bottomView];
  129. allSelBtn = [UIButton buttonWithType:0];
  130. allSelBtn.frame = CGRectMake(0, 10, 80, 40);
  131. [allSelBtn setImage:[UIImage imageNamed:@"记录未选"] forState:0];
  132. [allSelBtn setImage:[UIImage imageNamed:@"记录选中"] forState:UIControlStateSelected];
  133. [allSelBtn setTitle:YZMsg(@"全选") forState:0];
  134. allSelBtn.titleLabel.font = [UIFont systemFontOfSize:14];
  135. [allSelBtn setTitleColor:[UIColor blackColor] forState:0];
  136. [allSelBtn setTitleEdgeInsets:UIEdgeInsetsMake(0, 10, 0, 0)];
  137. [allSelBtn addTarget:self action:@selector(allSelClick:) forControlEvents:UIControlEventTouchUpInside];
  138. [bottomView addSubview:allSelBtn];
  139. deleteBtn = [UIButton buttonWithType:0];
  140. deleteBtn.frame = CGRectMake(_window_width-90, 10, 90, 40);
  141. [deleteBtn setBackgroundColor:Normal_Color];
  142. [deleteBtn setTitle:YZMsg(@"删除") forState:0];
  143. [deleteBtn setTitleColor:[UIColor whiteColor] forState:0];
  144. deleteBtn.titleLabel.font = [UIFont systemFontOfSize:14];
  145. [deleteBtn addTarget:self action:@selector(deleteClick) forControlEvents:UIControlEventTouchUpInside];
  146. [bottomView addSubview:deleteBtn];
  147. bottomView.hidden = YES;
  148. }
  149. -(UITableView *)historyTable{
  150. if (!_historyTable) {
  151. _historyTable = [[UITableView alloc]initWithFrame:CGRectMake(0, 64+statusbarHeight, _window_width, _window_height-64-statusbarHeight) style:UITableViewStylePlain];
  152. _historyTable.delegate = self;
  153. _historyTable.dataSource = self;
  154. _historyTable.separatorStyle = UITableViewCellSeparatorStyleNone;
  155. _historyTable.mj_header = [MJRefreshHeader headerWithRefreshingBlock:^{
  156. pageIndex = 1;
  157. [self requstData];
  158. }];
  159. _historyTable.mj_footer = [MJRefreshBackFooter footerWithRefreshingBlock:^{
  160. pageIndex ++;
  161. [self requstData];
  162. }];
  163. }
  164. return _historyTable;
  165. }
  166. -(NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
  167. {
  168. return self.listArr.count;
  169. }
  170. -(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
  171. {
  172. HistoryListModel * tempModle = (HistoryListModel*)self.listArr[section];
  173. return tempModle.list.count;
  174. }
  175. -(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
  176. {
  177. return 70;
  178. }
  179. -(CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section
  180. {
  181. return 40;
  182. }
  183. -(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
  184. {
  185. LookHistoryCell *cell = [LookHistoryCell cellWithTab:tableView andIndexPath:indexPath];
  186. if (manageSel) {
  187. cell.selBtn.hidden =NO;
  188. [cell.selBtn mas_makeConstraints:^(MASConstraintMaker *make) {
  189. make.width.mas_equalTo(30);
  190. }];
  191. }else{
  192. cell.selBtn.hidden = YES;
  193. [cell.selBtn mas_makeConstraints:^(MASConstraintMaker *make) {
  194. make.width.mas_equalTo(0);
  195. }];
  196. }
  197. LookHistoryModel *models =((HistoryListModel*)self.listArr[indexPath.section]).list[indexPath.row];
  198. cell.model = models;
  199. // 给cell做标记
  200. cell.tag = (long)indexPath.section *100 + (long)indexPath.row;
  201. NSString * cellTag = [NSString stringWithFormat:@"%zd",cell.tag];
  202. NSDictionary* _tempDic = @{
  203. cellTag:indexPath
  204. };
  205. [self.dic addEntriesFromDictionary:_tempDic];
  206. [cell layoutSubviews];
  207. return cell;
  208. }
  209. -(UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section
  210. {
  211. HistoryListModel*listModel = self.listArr[section];
  212. _headerView = [tableView dequeueReusableHeaderFooterViewWithIdentifier:@"ShopHistoryHeader"];
  213. if (_headerView == nil) {
  214. _headerView = [[UIView alloc]initWithFrame:CGRectMake(0, 0, _window_width, 40)];
  215. _headerView.backgroundColor = [UIColor whiteColor];
  216. }
  217. _headerView.tag = section;
  218. UILabel *dateLb = [[UILabel alloc]init];
  219. dateLb.font = [UIFont boldSystemFontOfSize:16];
  220. dateLb.textColor = [UIColor blackColor];
  221. [_headerView addSubview:dateLb];
  222. dateLb.text = [self.infos[section] valueForKey:@"date"];
  223. sectionBtn = [UIButton buttonWithType:0];
  224. sectionBtn.frame = CGRectMake(15, 8, 22, 22);
  225. sectionBtn.tag = section;
  226. [sectionBtn setImage:[UIImage imageNamed:@"记录未选"] forState:UIControlStateNormal];
  227. [sectionBtn setImage:[UIImage imageNamed:@"记录选中"] forState:UIControlStateSelected];
  228. [sectionBtn addTarget:self action:@selector(sectionBtnClick:) forControlEvents:UIControlEventTouchUpInside];
  229. [self.headerView addSubview:sectionBtn];
  230. sectionBtn.hidden = YES;
  231. if (manageSel) {
  232. sectionBtn.hidden = NO;
  233. dateLb.frame = CGRectMake(sectionBtn.right+10, 8, _window_width, 22);
  234. }else{
  235. sectionBtn.hidden = YES;
  236. dateLb.frame = CGRectMake(15, 8, _window_width, 22);
  237. }
  238. sectionBtn.selected = listModel.groupSelected;
  239. if (listModel.list.count == 0) {
  240. return nil;
  241. }
  242. return self.headerView;
  243. }
  244. -(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
  245. {
  246. [tableView deselectRowAtIndexPath:indexPath animated:YES];
  247. LookHistoryModel *models =((HistoryListModel*)self.listArr[indexPath.section]).list[indexPath.row];
  248. if (manageSel) {
  249. LookHistoryCell *cell = [tableView cellForRowAtIndexPath:indexPath];
  250. models.isSelected = !models.isSelected;
  251. cell.selBtn.selected = !cell.selBtn.selected;
  252. //判断组的是否选中状态是否修改
  253. NSString * cellTagStr = [NSString stringWithFormat:@"%zd",cell.tag];
  254. NSIndexPath *indexPath = self.dic[cellTagStr];
  255. HistoryListModel * listModel = (HistoryListModel*)self.listArr[indexPath.section];
  256. //0.便利当前组cell上选中按钮的个数
  257. NSInteger seletedNum =0;
  258. for (LookHistoryModel* goodsModel in listModel.list) {
  259. if (goodsModel.isSelected) {
  260. seletedNum += 1;
  261. }
  262. // 1.当前组的cell的个数 是否等于 勾选的总数
  263. if (((HistoryListModel*)self.listArr[indexPath.section]).list.count == seletedNum) {
  264. listModel.groupSelected = YES; //cell改变组头变为选中
  265. //判断 //cell改变组头 //组头改变全选
  266. NSInteger selectedNum = 0 ;
  267. for (HistoryListModel * tempListModel in self.listArr) {//遍历所有组
  268. if (tempListModel.groupSelected) {//如果组头是选中的
  269. selectedNum += 1;
  270. }
  271. if (selectedNum == self.listArr.count) {
  272. allSelBtn.selected = YES;
  273. }
  274. }
  275. } else {
  276. listModel.groupSelected = NO;
  277. allSelBtn.selected = NO;
  278. }
  279. [_historyTable reloadData];
  280. }
  281. }else{
  282. if (![models.goods_status isEqual:@"1"]) {
  283. [MBProgressHUD showError:YZMsg(@"商品不存在~")];
  284. return;
  285. }
  286. if ([models.type isEqual:@"1"]) {
  287. OutsideGoodsDetailVC *detail = [[OutsideGoodsDetailVC alloc]init];
  288. detail.goodsID = models.goodsid;
  289. detail.liveUid = models.uid;
  290. [[YBBaseAppDelegate sharedAppDelegate] pushViewController:detail animated:YES];
  291. }else{
  292. CommodityDetailVC *detail = [[CommodityDetailVC alloc]init];
  293. detail.goodsID = models.goodsid;
  294. detail.liveUid = models.uid;
  295. [[YBBaseAppDelegate sharedAppDelegate] pushViewController:detail animated:YES];
  296. }
  297. }
  298. }
  299. -(void)sectionBtnClick:(UIButton *)sender{
  300. sender.selected = !sender.selected;
  301. [self headerSelectedBtnClick:sender.tag];
  302. }
  303. #pragma mark-----全部选择--------
  304. -(void)allSelClick: (UIButton*)allSelectedBtn{
  305. allSelectedBtn.selected = !allSelectedBtn.selected; // 修改全选按钮的状态
  306. if (allSelectedBtn.selected) { // 如果全选按钮改变了为选中
  307. for (int i = 0; i <self.listArr.count; i ++) {
  308. HistoryListModel * listModel = self.listArr[i];
  309. if (!listModel.groupSelected) {//遍历如果组不是选中状态
  310. [self headerSelectedBtnClick:i]; //模拟组头点击了一次
  311. }
  312. }
  313. }else{
  314. for (int i = 0; i < self.listArr.count; i ++) { // 遍历所有的组头点击
  315. [self headerSelectedBtnClick:i];
  316. }
  317. }
  318. }
  319. - (void)headerSelectedBtnClick:(NSInteger)section {
  320. HistoryListModel*listModel = self.listArr[section];
  321. listModel.groupSelected = !listModel.groupSelected;
  322. // 判断如果点击 | header选中
  323. if (listModel.groupSelected) {
  324. // /// 判断组头的点击改变全选按钮
  325. NSInteger tempGroupSelectNum = 0;
  326. for (HistoryListModel *model in self.listArr) {
  327. if (model.groupSelected) {
  328. tempGroupSelectNum ++;
  329. }
  330. if (tempGroupSelectNum == self.listArr.count) {
  331. allSelBtn.selected = YES;
  332. }
  333. }
  334. for (LookHistoryModel* goodsModel in listModel.list) {
  335. if (!goodsModel.isSelected) { //下面不是选中状态的cell
  336. goodsModel.isSelected = YES;
  337. }
  338. }
  339. } else { // 取消header选中 所有都取消
  340. //全选按钮变为不选中
  341. allSelBtn.selected = NO;
  342. for (LookHistoryModel* goodsModel in listModel.list) {
  343. goodsModel.isSelected = NO;
  344. }
  345. }
  346. [_historyTable reloadData];
  347. }
  348. #pragma mark-----删除操作--------
  349. -(void)deleteClick{
  350. [selectHistory removeAllObjects];
  351. for (int i = 0; i < self.listArr.count; i ++) {
  352. HistoryListModel*listModel = self.listArr[i];
  353. for (LookHistoryModel* goodsModel in listModel.list) {
  354. if (goodsModel.isSelected) {
  355. [selectHistory addObject:goodsModel];
  356. }
  357. }
  358. }
  359. if (selectHistory.count < 1) {
  360. [MBProgressHUD showError:YZMsg(@"请先选择要删除内容")];
  361. return;;
  362. }
  363. NSLog(@"------------sel:%@ \n ====:%ld",selectHistory, selectHistory.count);
  364. NSString *delId = @"";
  365. for (LookHistoryModel *model in selectHistory) {
  366. delId = [delId stringByAppendingFormat:@"%@,",model.idStr];
  367. }
  368. NSRange range = NSMakeRange(delId.length-1, 1);
  369. delId = [delId stringByReplacingCharactersInRange:range withString:@""];
  370. NSString *url = [purl stringByAppendingFormat:@"?service=Buyer.delGoodsVisitRecord"];
  371. NSDictionary *signdic = @{@"uid":[Config getOwnID],@"token":[Config getOwnToken], @"time":[NSNumber numberWithLong: (long)[[NSDate date] timeIntervalSince1970]]};
  372. NSString *sign = [PublicObj sortString:signdic];
  373. NSDictionary *dic = @{
  374. @"uid":[Config getOwnID],
  375. @"token":[Config getOwnToken],
  376. @"time":[NSNumber numberWithLong: (long)[[NSDate date] timeIntervalSince1970]],
  377. @"recordids":delId,
  378. @"sign":sign
  379. };
  380. NSLog(@"-=-=-=-=:%@", dic);
  381. [YBNetworking postWithUrl:@"Buyer.delGoodsVisitRecord" Dic:dic Suc:^(int code, id info, NSString *msg) {
  382. if (code ==0) {
  383. [MBProgressHUD showError:msg];
  384. [self requstData];
  385. [self rightBtnClick];
  386. }else{
  387. [MBProgressHUD showError:msg];
  388. }
  389. } Fail:^(id fail) {
  390. }];
  391. }
  392. @end