ShoppingViewController.m 9.33 KB
Newer Older
曹云霄's avatar
曹云霄 committed
1 2 3 4 5 6 7 8 9
//
//  ShoppingViewController.m
//  Lighting
//
//  Created by 曹云霄 on 16/4/27.
//  Copyright © 2016年 上海勾芒科技有限公司. All rights reserved.
//

#import "ShoppingViewController.h"
曹云霄's avatar
曹云霄 committed
10 11
#import "ShoppingTableViewCell.h"
#import "AppDelegate.h"
曹云霄's avatar
曹云霄 committed
12
#import "GenerateOrdersViewController.h"
勾芒's avatar
勾芒 committed
13
#import "ShopcarModel.h"
曹云霄's avatar
曹云霄 committed
14

曹云霄's avatar
曹云霄 committed
15
@interface ShoppingViewController ()<UITableViewDelegate,UITableViewDataSource>
曹云霄's avatar
曹云霄 committed
16

曹云霄's avatar
曹云霄 committed
17 18

@property (weak, nonatomic) IBOutlet UITableView *shoppingTableview;
曹云霄's avatar
曹云霄 committed
19

勾芒's avatar
勾芒 committed
20 21 22 23 24 25 26



/**
 *  购物车数据源
 */
@property (nonatomic,strong) NSMutableArray *shopResponseArray;
曹云霄's avatar
曹云霄 committed
27 28 29 30
@end

@implementation ShoppingViewController

曹云霄's avatar
曹云霄 committed
31 32 33 34

/**
 *  数据源
 */
勾芒's avatar
勾芒 committed
35
- (NSMutableArray *)shopResponseArray
曹云霄's avatar
曹云霄 committed
36
{
勾芒's avatar
勾芒 committed
37
    if (_shopResponseArray == nil) {
曹云霄's avatar
曹云霄 committed
38
        
勾芒's avatar
勾芒 committed
39
        _shopResponseArray = [NSMutableArray array];
曹云霄's avatar
曹云霄 committed
40
    }
勾芒's avatar
勾芒 committed
41
    return _shopResponseArray;
曹云霄's avatar
曹云霄 committed
42 43 44
}


曹云霄's avatar
曹云霄 committed
45 46 47
- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
曹云霄's avatar
曹云霄 committed
48
    
曹云霄's avatar
曹云霄 committed
49
    [self uiConfigAction];
勾芒's avatar
勾芒 committed
50
    [self getShoppingCardata];
曹云霄's avatar
曹云霄 committed
51 52 53
}


54 55 56 57 58 59 60 61 62 63 64 65 66
- (void)viewDidAppear:(BOOL)animated
{
    [super viewDidAppear:animated];
    
    // 禁用 iOS7 返回手势
    if ([self.navigationController respondsToSelector:@selector(interactivePopGestureRecognizer)]) {
        self.navigationController.interactivePopGestureRecognizer.enabled = NO;
    }
}

- (void)viewWillDisappear:(BOOL)animated
{
    [super viewWillDisappear:animated];
勾芒's avatar
勾芒 committed
67 68
    
    [self.shopResponseArray removeAllObjects];
勾芒's avatar
勾芒 committed
69
    [self getShoppingCardata];
70 71 72 73 74 75
    // 开启
    if ([self.navigationController respondsToSelector:@selector(interactivePopGestureRecognizer)]) {
        self.navigationController.interactivePopGestureRecognizer.enabled = YES;
    }
}

曹云霄's avatar
曹云霄 committed
76

曹云霄's avatar
曹云霄 committed
77 78 79
#pragma mark - UI
- (void)uiConfigAction
{
勾芒's avatar
勾芒 committed
80
    self.view.backgroundColor = kTCColor(238, 238, 238);
曹云霄's avatar
曹云霄 committed
81 82
    self.shoppingTableview.dataSource = self;
    self.shoppingTableview.delegate = self;
勾芒's avatar
勾芒 committed
83
    self.shoppingTableview.backgroundColor = [UIColor clearColor];
曹云霄's avatar
曹云霄 committed
84 85 86
    self.shoppingTableview.tableFooterView = [UIView new];
}

勾芒's avatar
勾芒 committed
87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123
#pragma mark -获取购物车商品
- (void)getShoppingCardata
{
    //判断是否需要请求数据-通过当前客户ID
    if (![Shoppersmanager manager].currentCustomer) {
        
        return;
    }
    ShopCartFilter *shopcarNumber = [[ShopCartFilter alloc]init];
    shopcarNumber.consumerId = [Customermanager manager].customerID;
    DataPage *Newpage = [[DataPage alloc]init];
    Newpage.page = 0;
    shopcarNumber.dp = Newpage;
    [self CreateMBProgressHUDLoding];
    [[NetworkRequestClassManager Manager] NetworkRequestWithURL:[NSString stringWithFormat:@"%@%@",ServerAddress,@"/shopcart/query"] WithRequestType:0 WithParameter:shopcarNumber WithReturnValueBlock:^(id returnValue) {
        
        [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;
                [self.shopResponseArray addObject:model];
            }
勾芒's avatar
勾芒 committed
124
            [self.shoppingTableview reloadData];
勾芒's avatar
勾芒 committed
125 126 127 128 129 130 131 132 133 134 135 136 137
        }else
        {
            [self ErrorMBProgressView:returnValue[@"message"]];
        }
    } WithErrorCodeBlock:^(id errorCodeValue) {
        
    } WithFailureBlock:^(id error) {
        
        [self RemoveMBProgressHUDLoding];
    }];
}


曹云霄's avatar
曹云霄 committed
138 139 140 141

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    ShoppingTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"Shopping" forIndexPath:indexPath];
勾芒's avatar
勾芒 committed
142 143
    cell.selectionStyle = UITableViewCellSelectionStyleNone;
    cell.model = [self.shopResponseArray objectAtIndex_opple:indexPath.row];
勾芒's avatar
勾芒 committed
144 145 146 147 148 149
    cell.cellindex = indexPath.row;
    //cell选中回调
    [cell setReturnCellblock:^(NSInteger index) {
        
        [self setSelectedButton:index];
    }];
曹云霄's avatar
曹云霄 committed
150 151 152 153 154
    return  cell;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
勾芒's avatar
勾芒 committed
155
    return self.shopResponseArray.count;
曹云霄's avatar
曹云霄 committed
156 157 158 159 160 161 162 163 164 165 166 167 168 169 170
}


- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
    return 80;
}


- (void)tableView:(UITableView *)tableView didDeselectRowAtIndexPath:(NSIndexPath *)indexPath
{
    [self.shoppingTableview deselectRowAtIndexPath:indexPath animated:YES];
}


勾芒's avatar
勾芒 committed
171
#pragma mark -选中商品回调
勾芒's avatar
勾芒 committed
172 173 174
- (void)setSelectedButton:(NSInteger)index;
{
    ShopcarModel *model = [self.shopResponseArray objectAtIndex_opple:index];
勾芒's avatar
勾芒 committed
175
    model.isSelected = !model.isSelected;
勾芒's avatar
勾芒 committed
176 177 178 179 180 181 182 183
    
    NSInteger goodsNumber = 0;
    for (ShopcarModel *model in self.shopResponseArray) {
        if (model.isSelected) {
            goodsNumber ++;
        }
    }
    [self.settlementButton setTitle:[NSString stringWithFormat:@"去结算(%ld)",goodsNumber] forState:UIControlStateNormal];
勾芒's avatar
勾芒 committed
184 185 186
}


曹云霄's avatar
曹云霄 committed
187 188 189
#pragma mark -结算
- (IBAction)settlementButtonClick:(UIButton *)sender {
    
勾芒's avatar
勾芒 committed
190 191 192 193 194 195 196 197 198 199 200
    NSMutableArray *array = [NSMutableArray array];
    for (ShopcarModel *model in self.shopResponseArray) {
        if (model.isSelected) {
            [array addObject:model];
        }
    }
    if (array.count == 0) {
        
        [self ErrorMBProgressView:@"没有选中任何商品"];
        return;
    }
勾芒's avatar
勾芒 committed
201 202 203
    //占位
    ShopcarModel *ZhanweiModel = [[ShopcarModel alloc]init];
    [array addObject:ZhanweiModel];
曹云霄's avatar
曹云霄 committed
204
    GenerateOrdersViewController *generateOrder = [[self getStoryboardWithName] instantiateViewControllerWithIdentifier:@"generateorders"];
勾芒's avatar
勾芒 committed
205
    generateOrder.settlementGoodsdatas = array;
曹云霄's avatar
曹云霄 committed
206
    [self.navigationController pushViewController:generateOrder animated:YES];
曹云霄's avatar
曹云霄 committed
207
}
曹云霄's avatar
曹云霄 committed
208

曹云霄's avatar
曹云霄 committed
209
#pragma mark -全选
勾芒's avatar
勾芒 committed
210
- (IBAction)allSelectedButtonClick:(UIButton *)sender {
曹云霄's avatar
曹云霄 committed
211
    
曹云霄's avatar
曹云霄 committed
212
    
勾芒's avatar
勾芒 committed
213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236
    sender.selected = !sender.selected;
    if (sender.selected) {
        
        //全部选中
        for (ShoppingTableViewCell *cell in self.shoppingTableview.visibleCells) {
            
            cell.selectedButton.selected = YES;
        }
        for (ShopcarModel *model in self.shopResponseArray) {
            
            model.isSelected = YES;
        }
    }else
    {
        //取消全部选中
        for (ShoppingTableViewCell *cell in self.shoppingTableview.visibleCells) {
            
            cell.selectedButton.selected = NO;
        }
        for (ShopcarModel *model in self.shopResponseArray) {
            
            model.isSelected = NO;
        }
    }
曹云霄's avatar
曹云霄 committed
237 238
}

曹云霄's avatar
曹云霄 committed
239

勾芒's avatar
勾芒 committed
240 241 242
#pragma mark -删除选中商品
- (IBAction)delecteSelectedGoods:(UIButton *)sender {

勾芒's avatar
勾芒 committed
243 244 245 246 247 248
    
    [self CreateMBProgressHUDLoding];
    DeleteCartRequest *delecteGoods = [[DeleteCartRequest alloc]init];
    //code数组
    NSMutableArray *codeArr = [NSMutableArray array];
    //需要删除的cell数组indexpath
勾芒's avatar
勾芒 committed
249
    NSMutableArray *delecteArray = [NSMutableArray array];
勾芒's avatar
勾芒 committed
250 251
    //模型数组
    NSMutableArray *delectemodel = [NSMutableArray array];
勾芒's avatar
勾芒 committed
252 253 254 255
    for (int i=0; i<self.shopResponseArray.count; i++) {
        
        ShopcarModel *model = [self.shopResponseArray objectAtIndex_opple:i];
        if (model.isSelected) {
勾芒's avatar
勾芒 committed
256 257 258
            
            [codeArr addObject:model.fid];
            [delectemodel addObject:model];
勾芒's avatar
勾芒 committed
259 260 261 262
            NSIndexPath *indexpath = [NSIndexPath indexPathForRow:i inSection:0];
            [delecteArray addObject:indexpath];
        }
    }
勾芒's avatar
勾芒 committed
263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295
    //没有选中任何商品
    if (codeArr.count == 0) {
        [self RemoveMBProgressHUDLoding];
        return;
    }
    delecteGoods.cartIds = codeArr;
    [[NetworkRequestClassManager Manager] NetworkRequestWithURL:[NSString stringWithFormat:@"%@%@",ServerAddress,@"/shopcart/delete"] WithRequestType:0 WithParameter:delecteGoods WithReturnValueBlock:^(id returnValue) {
        
        [self RemoveMBProgressHUDLoding];
        if ([returnValue[@"code"] isEqualToNumber:@0]) {
            
            //删除商品
            for (ShopcarModel *model in delectemodel) {
                [self.shopResponseArray removeObject:model];
            }
            [self.shoppingTableview deleteRowsAtIndexPaths:delecteArray withRowAnimation:UITableViewRowAnimationLeft];
            [self SuccessMBProgressView:@"删除成功"];
            
        }else
        {
            [self ErrorMBProgressView:returnValue[@"message"]];
        }
        
    } WithErrorCodeBlock:^(id errorCodeValue) {
        
        
    } WithFailureBlock:^(id error) {
        
        [self RemoveMBProgressHUDLoding];
        
    }];
    
    
勾芒's avatar
勾芒 committed
296
}
曹云霄's avatar
曹云霄 committed
297 298 299 300 301





曹云霄's avatar
曹云霄 committed
302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317
- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#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