// // ShoppingViewController.m // Lighting // // Created by 曹云霄 on 16/4/27. // Copyright © 2016年 上海勾芒科技有限公司. All rights reserved. // #import "ShoppingViewController.h" #import "ShoppingTableViewCell.h" #import "AppDelegate.h" #import "GenerateOrdersViewController.h" #import "ShopcarModel.h" #import "AddressModel.h" #import "ProductDetailsViewController.h" @interface ShoppingViewController () @property (weak, nonatomic) IBOutlet UITableView *shoppingTableview; /** * 购物车数据源 */ @property (nonatomic,strong) NSMutableArray *shopResponseArray; @end @implementation ShoppingViewController /** * 数据源 */ - (NSMutableArray *)shopResponseArray { if (_shopResponseArray == nil) { _shopResponseArray = [NSMutableArray array]; } return _shopResponseArray; } - (void)viewDidLoad { [super viewDidLoad]; // Do any additional setup after loading the view. [self uiConfigAction]; } #pragma mark -渲染完成 - (void)viewDidAppear:(BOOL)animated { [super viewDidAppear:animated]; self.navigationController.fd_fullscreenPopGestureRecognizer.enabled = NO; [self.shopResponseArray removeAllObjects]; [self InitializeState]; [self getShoppingCardata]; } #pragma mark -视图即将消失 - (void)viewWillDisappear:(BOOL)animated { [super viewWillDisappear:animated]; self.navigationController.fd_fullscreenPopGestureRecognizer.enabled = YES; } #pragma mark -初始化状态 - (void)InitializeState { [self.settlementButton setTitle:@"去结算(0)" forState:UIControlStateNormal]; self.allSelectedButton.selected = NO; self.totalpriceLabe.text = nil; } #pragma mark - UI - (void)uiConfigAction { self.settlementButton.layer.masksToBounds = YES; self.settlementButton.layer.cornerRadius = kCornerRadius; self.view.backgroundColor = kTCColor(238, 238, 238); self.shoppingTableview.dataSource = self; self.shoppingTableview.delegate = self; self.shoppingTableview.backgroundColor = [UIColor clearColor]; self.shoppingTableview.tableFooterView = [UIView new]; } #pragma mark -获取购物车商品 - (void)getShoppingCardata { //判断是否需要请求数据-通过当前客户ID if (![Shoppersmanager manager].currentCustomer) { return; } ShopCartFilter *shopcarNumber = [[ShopCartFilter alloc]init]; shopcarNumber.consumerId = [Customermanager manager].model.fid; DataPage *Newpage = [[DataPage alloc]init]; Newpage.page = 1; Newpage.rows = 99999; shopcarNumber.dp = Newpage; [self CreateMBProgressHUDLoding]; [[NetworkRequestClassManager Manager] NetworkRequestWithURL:SERVERREQUESTURL(SHOPPINGBAG) WithRequestType:0 WithParameter:shopcarNumber WithReturnValueBlock:^(id returnValue) { self.shoppingTableview.emptyDataSetSource = self; [self endRefreshingForTableView:self.shoppingTableview]; [self RemoveMBProgressHUDLoding]; if ([returnValue[@"code"] isEqualToNumber:@0]) { ShopCartResponse *shopcar = [[ShopCartResponse alloc]initWithDictionary:returnValue[@"data"] error:nil]; //自定义属性 for (TOShopcartEntity *objc in shopcar.shopcart) { ShopcarModel *model = [[ShopcarModel alloc]init]; model.goods = objc.goods; model.fid = objc.fid; model.createName = objc.createName; model.createBy = objc.createBy; model.createDate = objc.createDate; model.updateName = objc.updateName; model.updateBy = objc.updateBy; model.updateDate = objc.updateDate; model.goodsId = objc.goodsId; model.goodsNum = objc.goodsNum; model.consumerId = objc.consumerId; model.costPrice = objc.costPrice; [self.shopResponseArray addObject:model]; } [self.shoppingTableview reloadData]; }else { [self ErrorMBProgressView:returnValue[@"message"]]; } } WithErrorCodeBlock:^(id errorCodeValue) { [self RemoveMBProgressHUDLoding]; [self ErrorMBProgressView:@"无网络"]; } WithFailureBlock:^(id error) { [self RemoveMBProgressHUDLoding]; [self endRefreshingForTableView:self.shoppingTableview]; }]; } - (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath { ShoppingTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"Shopping" forIndexPath:indexPath]; cell.selectionStyle = UITableViewCellSelectionStyleNone; cell.model = [self.shopResponseArray objectAtIndex_opple:indexPath.row]; cell.delegate = self; cell.cellindex = indexPath.row; //cell选中回调 __weak typeof(self) weakSelf = self; [cell setReturnCellblock:^(NSInteger index) { [weakSelf setSelectedButton:index]; }]; //提示框回调 [cell setPromptStringBlock:^(NSString *string) { [weakSelf promptCustomerWithString:string]; }]; return cell; } - (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section { return self.shopResponseArray.count; } - (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath { return 80; } - (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath { ProductDetailsViewController *productDetails = [[self getStoryboardWithName] instantiateViewControllerWithIdentifier:@"productdetails"]; productDetails.goodsID = [[self.shopResponseArray objectAtIndex_opple:indexPath.row] goodsId]; [self.navigationController pushViewController:productDetails animated:YES]; } - (void)tableView:(UITableView *)tableView didDeselectRowAtIndexPath:(NSIndexPath *)indexPath { [self.shoppingTableview deselectRowAtIndexPath:indexPath animated:YES]; } #pragma mark -选中商品回调 - (void)setSelectedButton:(NSInteger)index; { ShopcarModel *model = [self.shopResponseArray objectAtIndex_opple:index]; model.isSelected = !model.isSelected; NSInteger goodsNumber = 0; for (ShopcarModel *model in self.shopResponseArray) { if (model.isSelected) { goodsNumber ++; } } if (goodsNumber == self.shopResponseArray.count) { self.allSelectedButton.selected = YES; }else { self.allSelectedButton.selected = NO; } [self CalculateSelectedGoodsAllprice]; [self.settlementButton setTitle:[NSString stringWithFormat:@"去结算(%ld)",goodsNumber] forState:UIControlStateNormal]; } #pragma mark -结算 - (IBAction)settlementButtonClick:(UIButton *)sender { NSMutableArray *array = [NSMutableArray array]; for (ShopcarModel *model in self.shopResponseArray) { if (model.isSelected) { [array addObject:model]; } } if (array.count == 0) { [self ErrorMBProgressView:@"没有选中任何商品"]; return; } //商品总信息占位 ShopcarModel*ZhanweiModel = [[ShopcarModel alloc]init]; [array addObject:ZhanweiModel]; GenerateOrdersViewController *generateOrder = [[self getStoryboardWithName] instantiateViewControllerWithIdentifier:@"generateorders"]; generateOrder.settlementGoodsdatas = array; //清除已经生成订单的商品 [generateOrder setDelecteSelectedGoods:^(NSArray *goodsCode) { DeleteCartRequest *delecteGoods = [[DeleteCartRequest alloc]init]; delecteGoods.cartIds = goodsCode; [[NetworkRequestClassManager Manager] NetworkRequestWithURL:SERVERREQUESTURL(REMOVESHOPPINGBAG) WithRequestType:0 WithParameter:delecteGoods WithReturnValueBlock:^(id returnValue) { [self RemoveMBProgressHUDLoding]; if ([returnValue[@"code"] isEqualToNumber:@0]) { [self QueryShoppingCarNumber]; //商品cell NSMutableArray *cellArray = [NSMutableArray array]; //删除商品 for (int i=0; i