ShoppingViewController.m 21.7 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
#import "AddressModel.h"
曹云霄's avatar
曹云霄 committed
15
#import "ProductDetailsViewController.h"
曹云霄's avatar
曹云霄 committed
16

17
@interface ShoppingViewController ()<UITableViewDelegate,UITableViewDataSource,ChangeGoodsNumberDelegate,DZNEmptyDataSetSource,UITextFieldDelegate>
曹云霄's avatar
曹云霄 committed
18 19

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

勾芒's avatar
勾芒 committed
21 22 23 24
/**
 *  购物车数据源
 */
@property (nonatomic,strong) NSMutableArray *shopResponseArray;
勾芒's avatar
勾芒 committed
25

26 27 28 29 30
/**
 *  折扣比例输入
 */
@property (nonatomic,strong) UITextField *textField;

勾芒's avatar
勾芒 committed
31

曹云霄's avatar
曹云霄 committed
32 33 34 35
@end

@implementation ShoppingViewController

曹云霄's avatar
曹云霄 committed
36 37 38 39

/**
 *  数据源
 */
勾芒's avatar
勾芒 committed
40
- (NSMutableArray *)shopResponseArray
曹云霄's avatar
曹云霄 committed
41
{
勾芒's avatar
勾芒 committed
42
    if (_shopResponseArray == nil) {
曹云霄's avatar
曹云霄 committed
43
        
勾芒's avatar
勾芒 committed
44
        _shopResponseArray = [NSMutableArray array];
曹云霄's avatar
曹云霄 committed
45
    }
勾芒's avatar
勾芒 committed
46
    return _shopResponseArray;
曹云霄's avatar
曹云霄 committed
47 48
}

曹云霄's avatar
曹云霄 committed
49 50
- (void)viewDidLoad {
    [super viewDidLoad];
曹云霄's avatar
曹云霄 committed
51
    
曹云霄's avatar
曹云霄 committed
52 53 54
    [self uiConfigAction];
}

勾芒's avatar
勾芒 committed
55
#pragma mark -渲染完成
56 57 58
- (void)viewDidAppear:(BOOL)animated
{
    [super viewDidAppear:animated];
曹云霄's avatar
曹云霄 committed
59
    self.navigationController.fd_fullscreenPopGestureRecognizer.enabled = NO;
60 61 62
    if ([self.navigationController respondsToSelector:@selector(interactivePopGestureRecognizer)]) {
        self.navigationController.interactivePopGestureRecognizer.enabled = NO;
    }
勾芒's avatar
勾芒 committed
63 64 65
    [self.shopResponseArray removeAllObjects];
    [self InitializeState];
    [self getShoppingCardata];
66
    [self QueryShoppingCarNumber];
67 68
}

勾芒's avatar
勾芒 committed
69
#pragma mark -视图即将消失
70 71 72
- (void)viewWillDisappear:(BOOL)animated
{
    [super viewWillDisappear:animated];
曹云霄's avatar
曹云霄 committed
73
    self.navigationController.fd_fullscreenPopGestureRecognizer.enabled = YES;
74 75 76
    if ([self.navigationController respondsToSelector:@selector(interactivePopGestureRecognizer)]) {
        self.navigationController.interactivePopGestureRecognizer.enabled = YES;
    }
77 78
}

勾芒's avatar
勾芒 committed
79 80 81 82 83 84
#pragma mark -初始化状态
- (void)InitializeState
{
    [self.settlementButton setTitle:@"去结算(0)" forState:UIControlStateNormal];
    self.allSelectedButton.selected = NO;
    self.totalpriceLabe.text = nil;
曹云霄's avatar
曹云霄 committed
85
    self.discountTextField.text = nil;
勾芒's avatar
勾芒 committed
86 87
}

曹云霄's avatar
曹云霄 committed
88 89 90
#pragma mark - UI
- (void)uiConfigAction
{
勾芒's avatar
勾芒 committed
91 92
    self.settlementButton.layer.masksToBounds = YES;
    self.settlementButton.layer.cornerRadius = kCornerRadius;
勾芒's avatar
勾芒 committed
93
    self.view.backgroundColor = kTCColor(238, 238, 238);
曹云霄's avatar
曹云霄 committed
94 95
    self.shoppingTableview.dataSource = self;
    self.shoppingTableview.delegate = self;
勾芒's avatar
勾芒 committed
96
    self.shoppingTableview.backgroundColor = [UIColor clearColor];
曹云霄's avatar
曹云霄 committed
97
    self.shoppingTableview.tableFooterView = [UIView new];
98
    self.discountTextField.delegate = self;
曹云霄's avatar
曹云霄 committed
99 100
}

101 102 103
#pragma mark -<UITextFieldDelegate>
- (BOOL)textFieldShouldBeginEditing:(UITextField *)textField
{
104 105 106
    if (![textField isEqual:self.discountTextField]) {
        return YES;
    }
107
    if (self.shopResponseArray.count == 0) {
曹云霄's avatar
曹云霄 committed
108
        [XBLoadingView showHUDViewWithText:@"请先添加商品到购物车"];return NO;
109 110 111 112 113 114 115 116
    }
    NSMutableArray *array = [NSMutableArray array];
    for (ShopcarModel *model in self.shopResponseArray) {
        if (model.isSelected) {
            [array addObject:model];
        }
    }
    if (array.count == 0) {
曹云霄's avatar
曹云霄 committed
117
        [XBLoadingView showHUDViewWithText:@"没有选中任何商品"];return NO;
118
    }
119 120
    [self inputDiscountNumber];
    return NO;
121 122
}

123 124
#pragma mark - 输入折扣金额
- (void)inputDiscountNumber
125
{
126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150
    WS(weakSelf);
    UIAlertController *alertControl = [UIAlertController alertControllerWithTitle:@"提示" message:@"请输入折扣比例(%)" preferredStyle:UIAlertControllerStyleAlert];
    [alertControl addAction:[UIAlertAction actionWithTitle:@"取消" style:UIAlertActionStyleCancel handler:^(UIAlertAction * _Nonnull action) {
        weakSelf.textField.text = nil;
         [weakSelf textFieldEndEditing];
    }]];
    [alertControl addTextFieldWithConfigurationHandler:^(UITextField * _Nonnull textField) {
        
        weakSelf.textField = textField;
        textField.placeholder = @"请输入";
        textField.keyboardType = UIKeyboardTypeNumberPad;
        textField.delegate = self;
    }];
    [alertControl addAction:[UIAlertAction actionWithTitle:@"确认" style:UIAlertActionStyleDestructive handler:^(UIAlertAction * _Nonnull action) {
        [weakSelf textFieldEndEditing];
    }]];
    [self presentViewController:alertControl animated:YES completion:nil];
}

#pragma mark - 结束编辑
- (void)textFieldEndEditing
{
    if ([BaseViewController isBlankString:self.textField.text]) {
        return;
    }
151
    Shoppersmanager *user = [Shoppersmanager manager];
152
    if ([self.textField.text integerValue] < [user.Shoppers.lowestDiscount integerValue] && ![[self class] isBlankString:self.textField.text])
153
    {
曹云霄's avatar
曹云霄 committed
154
        [XBLoadingView showHUDViewWithText:@"当前价格低于你的价格权限!"];
155 156 157 158
        self.textField.text = nil;
    }else {
        self.discountTextField.text = self.textField.text;
        [self unifiedChangeDiscount:self.textField.text];
159 160
    }
}
勾芒's avatar
勾芒 committed
161

162 163 164 165 166 167 168 169 170 171 172 173 174
#pragma mark - 输入折扣比例
- (BOOL)textField:(UITextField *)textField shouldChangeCharactersInRange:(NSRange)range replacementString:(NSString *)string
{
    return [self validateNumber:string];
}

#pragma mark - 判断输入是否有效<两位整数、第一位不能为0>
- (BOOL)validateNumber:(NSString*)number
{
    BOOL res = YES;
    if ([number isEqualToString:@""]) {
        return YES;
    }
175
    if (self.textField.text.length == 2) {
176 177
        return NO;
    }
178
    if([[self class] isBlankString:self.textField.text] && [number isEqualToString:@"0"])
179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201
    {
        if([number isEqualToString:@"0"]){
            return NO;
        }
    }
    NSCharacterSet* tmpSet = [NSCharacterSet characterSetWithCharactersInString:@"0123456789"];
    int i = 0;
    while (i < number.length) {
        NSString * string = [number substringWithRange:NSMakeRange(i, 1)];
        NSRange range = [string rangeOfCharacterFromSet:tmpSet];
        if (range.length == 0) {
            res = NO;
            break;
        }
        i++;
    }
    return res;
}

#pragma mark - 统一修改折扣率
- (void)unifiedChangeDiscount:(NSString *)discount
{
    WS(weakSelf);
202
    NSLog(@"----->%@",discount);
203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223
    NSDecimalNumber *discountNumber = [NSDecimalNumber decimalNumberWithString:discount];
    NSDecimalNumber *number = [discountNumber decimalNumberByDividingBy:[NSDecimalNumber decimalNumberWithString:@"100"]];
    for (ShopcarModel *model in self.shopResponseArray) {
        if (model.isSelected) {
            NSNumber *constPrice = model.goods.tagPrice;
            model.costPrice = [number decimalNumberByMultiplyingBy:[NSDecimalNumber decimalNumberWithString:[constPrice stringValue]]];
        }
    }
    //** boolValue为false表示修改失败 */
    [self modifyMultipleGoodsNnumberOrPrice:self.shopResponseArray completeModify:^(BOOL boolValue) {
        if (!boolValue) {
            for (ShopcarModel *model in weakSelf.shopResponseArray) {
                if (model.isSelected) {
                    model.costPrice = model.goods.tagPrice;
                    model.goodsNum = 1;
                }
            }
        }
        [weakSelf.shoppingTableview reloadData];
    }];
}
勾芒's avatar
勾芒 committed
224

勾芒's avatar
勾芒 committed
225 226 227 228 229 230 231 232
#pragma mark -获取购物车商品
- (void)getShoppingCardata
{
    //判断是否需要请求数据-通过当前客户ID
    if (![Shoppersmanager manager].currentCustomer) {
        return;
    }
    ShopCartFilter *shopcarNumber = [[ShopCartFilter alloc]init];
勾芒's avatar
勾芒 committed
233
    shopcarNumber.consumerId = [Customermanager manager].model.fid;
勾芒's avatar
勾芒 committed
234
    DataPage *Newpage = [[DataPage alloc]init];
勾芒's avatar
勾芒 committed
235 236
    Newpage.page = 1;
    Newpage.rows = 99999;
勾芒's avatar
勾芒 committed
237
    shopcarNumber.dp = Newpage;
曹云霄's avatar
曹云霄 committed
238
    [XBLoadingView showHUDViewWithDefault];
曹云霄's avatar
曹云霄 committed
239
    WS(weakSelf);
240
    [[NetworkRequestClassManager Manager] NetworkRequestWithURL:SERVERREQUESTURL(SHOPPINGBAG) WithRequestType:ZERO WithParameter:shopcarNumber WithReturnValueBlock:^(id returnValue) {
勾芒's avatar
勾芒 committed
241
        
曹云霄's avatar
曹云霄 committed
242 243
        weakSelf.shoppingTableview.emptyDataSetSource = weakSelf;
        [weakSelf endRefreshingForTableView:weakSelf.shoppingTableview];
曹云霄's avatar
曹云霄 committed
244
        [XBLoadingView hideHUDViewWithDefault];
勾芒's avatar
勾芒 committed
245
        if ([returnValue[@"code"] isEqualToNumber:@0]) {
勾芒's avatar
勾芒 committed
246
  
勾芒's avatar
勾芒 committed
247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262
            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;
勾芒's avatar
勾芒 committed
263
                model.costPrice = objc.costPrice;
曹云霄's avatar
曹云霄 committed
264
                [weakSelf.shopResponseArray addObject:model];
勾芒's avatar
勾芒 committed
265
            }
曹云霄's avatar
曹云霄 committed
266
            [weakSelf.shoppingTableview reloadData];
勾芒's avatar
勾芒 committed
267 268
        }else
        {
曹云霄's avatar
曹云霄 committed
269
            [XBLoadingView showHUDViewWithText:returnValue[@"message"]];
勾芒's avatar
勾芒 committed
270
        }
271
    } WithFailureBlock:^(NSError *error) {
272
        [XBLoadingView hideHUDViewWithDefault];
曹云霄's avatar
曹云霄 committed
273
        [weakSelf endRefreshingForTableView:weakSelf.shoppingTableview];
曹云霄's avatar
曹云霄 committed
274
        [XBLoadingView showHUDViewWithText:error.localizedDescription];
勾芒's avatar
勾芒 committed
275 276 277 278
    }];
}


曹云霄's avatar
曹云霄 committed
279 280 281
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    ShoppingTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"Shopping" forIndexPath:indexPath];
勾芒's avatar
勾芒 committed
282 283
    cell.selectionStyle = UITableViewCellSelectionStyleNone;
    cell.model = [self.shopResponseArray objectAtIndex_opple:indexPath.row];
勾芒's avatar
勾芒 committed
284
    cell.delegate = self;
勾芒's avatar
勾芒 committed
285 286
    cell.cellindex = indexPath.row;
    //cell选中回调
287 288
    __weak typeof(cell) weakCell = cell;
    WS(weakSelf);
勾芒's avatar
勾芒 committed
289
    [cell setReturnCellblock:^(NSInteger index) {
勾芒's avatar
勾芒 committed
290
        [weakSelf setSelectedButton:index];
勾芒's avatar
勾芒 committed
291
    }];
勾芒's avatar
勾芒 committed
292 293
    //提示框回调
    [cell setPromptStringBlock:^(NSString *string) {
曹云霄's avatar
曹云霄 committed
294 295
        [XBLoadingView showHUDViewWithText:string];
        [weakCell.clinchTextfield becomeFirstResponder];
勾芒's avatar
勾芒 committed
296
    }];
曹云霄's avatar
曹云霄 committed
297 298 299 300 301
    return  cell;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
勾芒's avatar
勾芒 committed
302
    return self.shopResponseArray.count;
曹云霄's avatar
曹云霄 committed
303 304 305 306 307 308 309 310
}


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

曹云霄's avatar
曹云霄 committed
311 312
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
曹云霄's avatar
曹云霄 committed
313
    ProductDetailsViewController *productDetails = [[[self class] getMainStoryboardClass] instantiateViewControllerWithIdentifier:@"productdetails"];
曹云霄's avatar
曹云霄 committed
314 315 316
    productDetails.goodsID = [[self.shopResponseArray objectAtIndex_opple:indexPath.row] goodsId];
    [self.navigationController pushViewController:productDetails animated:YES];
}
曹云霄's avatar
曹云霄 committed
317 318 319 320 321 322

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

勾芒's avatar
勾芒 committed
323
#pragma mark -选中商品回调
勾芒's avatar
勾芒 committed
324 325 326
- (void)setSelectedButton:(NSInteger)index;
{
    ShopcarModel *model = [self.shopResponseArray objectAtIndex_opple:index];
勾芒's avatar
勾芒 committed
327
    model.isSelected = !model.isSelected;
勾芒's avatar
勾芒 committed
328 329 330 331 332 333
    
    NSInteger goodsNumber = 0;
    for (ShopcarModel *model in self.shopResponseArray) {
        if (model.isSelected) {
            goodsNumber ++;
        }
勾芒's avatar
勾芒 committed
334 335 336 337 338 339
    }
    if (goodsNumber == self.shopResponseArray.count) {
        self.allSelectedButton.selected = YES;
    }else
    {
        self.allSelectedButton.selected = NO;
勾芒's avatar
勾芒 committed
340
    }
勾芒's avatar
勾芒 committed
341
    [self CalculateSelectedGoodsAllprice];
勾芒's avatar
勾芒 committed
342
    [self.settlementButton setTitle:[NSString stringWithFormat:@"去结算(%ld)",goodsNumber] forState:UIControlStateNormal];
勾芒's avatar
勾芒 committed
343 344 345
}


曹云霄's avatar
曹云霄 committed
346 347 348
#pragma mark -结算
- (IBAction)settlementButtonClick:(UIButton *)sender {
    
勾芒's avatar
勾芒 committed
349 350 351 352 353 354 355 356
    NSMutableArray *array = [NSMutableArray array];
    for (ShopcarModel *model in self.shopResponseArray) {
        if (model.isSelected) {
            [array addObject:model];
        }
    }
    if (array.count == 0) {
        
曹云霄's avatar
曹云霄 committed
357
        [XBLoadingView showHUDViewWithText:@"没有选中任何商品"];
勾芒's avatar
勾芒 committed
358 359
        return;
    }
勾芒's avatar
勾芒 committed
360 361
    //商品总信息占位
     ShopcarModel*ZhanweiModel = [[ShopcarModel alloc]init];
勾芒's avatar
勾芒 committed
362
    [array addObject:ZhanweiModel];
曹云霄's avatar
曹云霄 committed
363
    GenerateOrdersViewController *generateOrder = [[[self class] getMainStoryboardClass] instantiateViewControllerWithIdentifier:@"generateorders"];
勾芒's avatar
勾芒 committed
364
    generateOrder.settlementGoodsdatas = array;
勾芒's avatar
勾芒 committed
365 366 367 368 369
    //清除已经生成订单的商品
    [generateOrder setDelecteSelectedGoods:^(NSArray *goodsCode) {
        
        DeleteCartRequest *delecteGoods = [[DeleteCartRequest alloc]init];
        delecteGoods.cartIds = goodsCode;
曹云霄's avatar
曹云霄 committed
370
        WS(weakSelf);
371
        [[NetworkRequestClassManager Manager] NetworkRequestWithURL:SERVERREQUESTURL(REMOVESHOPPINGBAG)  WithRequestType:ZERO WithParameter:delecteGoods WithReturnValueBlock:^(id returnValue) {
勾芒's avatar
勾芒 committed
372
            
曹云霄's avatar
曹云霄 committed
373
            [XBLoadingView hideHUDViewWithDefault];
勾芒's avatar
勾芒 committed
374
            if ([returnValue[@"code"] isEqualToNumber:@0]) {
曹云霄's avatar
曹云霄 committed
375
                [weakSelf QueryShoppingCarNumber];
勾芒's avatar
勾芒 committed
376 377 378
                //商品cell
                NSMutableArray *cellArray = [NSMutableArray array];
                //删除商品
曹云霄's avatar
曹云霄 committed
379 380
                for (int i=0; i<weakSelf.shopResponseArray.count; i++) {
                    ShopcarModel *model = [weakSelf.shopResponseArray objectAtIndex_opple:i];
勾芒's avatar
勾芒 committed
381
                    if ([goodsCode containsObject:model.fid]) {
曹云霄's avatar
曹云霄 committed
382
                        [weakSelf.shopResponseArray removeObject:model];
勾芒's avatar
勾芒 committed
383 384 385 386
                        NSIndexPath *indexpath = [NSIndexPath indexPathForRow:i inSection:0];
                        [cellArray addObject:indexpath];
                    }
                }
曹云霄's avatar
曹云霄 committed
387
                [weakSelf.shoppingTableview deleteRowsAtIndexPaths:cellArray withRowAnimation:UITableViewRowAnimationTop];
勾芒's avatar
勾芒 committed
388 389
            }else
            {
曹云霄's avatar
曹云霄 committed
390
                [XBLoadingView showHUDViewWithText:returnValue[@"message"]];
勾芒's avatar
勾芒 committed
391
            }
392
        } WithFailureBlock:^(NSError *error) {
曹云霄's avatar
曹云霄 committed
393 394
            [XBLoadingView hideHUDViewWithDefault];
            [XBLoadingView showHUDViewWithText:error.localizedDescription];
勾芒's avatar
勾芒 committed
395 396
        }];
    }];
曹云霄's avatar
曹云霄 committed
397
    [self.navigationController pushViewController:generateOrder animated:YES];
曹云霄's avatar
曹云霄 committed
398
}
曹云霄's avatar
曹云霄 committed
399

曹云霄's avatar
曹云霄 committed
400
#pragma mark -全选
勾芒's avatar
勾芒 committed
401 402 403 404 405 406 407 408 409 410
- (IBAction)allSelectedButtonClick:(UIButton *)sender {
    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;
        }
勾芒's avatar
勾芒 committed
411
        [self.settlementButton setTitle:[NSString stringWithFormat:@"去结算(%ld)",self.shopResponseArray.count] forState:UIControlStateNormal];
勾芒's avatar
勾芒 committed
412 413 414 415 416 417 418 419 420
    }else
    {
        //取消全部选中
        for (ShoppingTableViewCell *cell in self.shoppingTableview.visibleCells) {
            cell.selectedButton.selected = NO;
        }
        for (ShopcarModel *model in self.shopResponseArray) {
            model.isSelected = NO;
        }
勾芒's avatar
勾芒 committed
421
        [self.settlementButton setTitle:@"去结算(0)" forState:UIControlStateNormal];
勾芒's avatar
勾芒 committed
422
    }
勾芒's avatar
勾芒 committed
423 424
    //计算总金额
    [self CalculateSelectedGoodsAllprice];
曹云霄's avatar
曹云霄 committed
425 426
}

勾芒's avatar
勾芒 committed
427 428 429
#pragma mark -计算选中后的商品总金额
- (void)CalculateSelectedGoodsAllprice
{
勾芒's avatar
勾芒 committed
430
    CGFloat allPrice = 0;
勾芒's avatar
勾芒 committed
431 432
    for (ShopcarModel *model in self.shopResponseArray) {
        if (model.isSelected) {
勾芒's avatar
勾芒 committed
433
            allPrice += ([model.costPrice floatValue]?[model.costPrice floatValue]:[model.goods.tagPrice floatValue]) * model.goodsNum;
勾芒's avatar
勾芒 committed
434 435
        }
    }
勾芒's avatar
勾芒 committed
436
    self.totalpriceLabe.text = [NSString stringWithFormat:@"¥%.2f",allPrice];
勾芒's avatar
勾芒 committed
437 438
}

曹云霄's avatar
曹云霄 committed
439

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

曹云霄's avatar
曹云霄 committed
443
    [XBLoadingView showHUDViewWithDefault];
勾芒's avatar
勾芒 committed
444 445 446 447
    DeleteCartRequest *delecteGoods = [[DeleteCartRequest alloc]init];
    //code数组
    NSMutableArray *codeArr = [NSMutableArray array];
    //需要删除的cell数组indexpath
勾芒's avatar
勾芒 committed
448
    NSMutableArray *delecteArray = [NSMutableArray array];
勾芒's avatar
勾芒 committed
449 450
    //模型数组
    NSMutableArray *delectemodel = [NSMutableArray array];
勾芒's avatar
勾芒 committed
451 452 453
    for (int i=0; i<self.shopResponseArray.count; i++) {
        ShopcarModel *model = [self.shopResponseArray objectAtIndex_opple:i];
        if (model.isSelected) {
勾芒's avatar
勾芒 committed
454 455
            [codeArr addObject:model.fid];
            [delectemodel addObject:model];
勾芒's avatar
勾芒 committed
456 457 458 459
            NSIndexPath *indexpath = [NSIndexPath indexPathForRow:i inSection:0];
            [delecteArray addObject:indexpath];
        }
    }
勾芒's avatar
勾芒 committed
460 461
    //没有选中任何商品
    if (codeArr.count == 0) {
曹云霄's avatar
曹云霄 committed
462 463
        [XBLoadingView hideHUDViewWithDefault];
        [XBLoadingView showHUDViewWithText:@"未选中商品"];
勾芒's avatar
勾芒 committed
464 465 466
        return;
    }
    delecteGoods.cartIds = codeArr;
曹云霄's avatar
曹云霄 committed
467
    WS(weakSelf);
468
    [[NetworkRequestClassManager Manager] NetworkRequestWithURL:SERVERREQUESTURL(REMOVESHOPPINGBAG)  WithRequestType:ZERO WithParameter:delecteGoods WithReturnValueBlock:^(id returnValue) {
勾芒's avatar
勾芒 committed
469
        
曹云霄's avatar
曹云霄 committed
470
        [XBLoadingView hideHUDViewWithDefault];
勾芒's avatar
勾芒 committed
471 472
        if ([returnValue[@"code"] isEqualToNumber:@0]) {
            
曹云霄's avatar
曹云霄 committed
473
            [weakSelf QueryShoppingCarNumber];
勾芒's avatar
勾芒 committed
474 475
            //删除商品
            for (ShopcarModel *model in delectemodel) {
曹云霄's avatar
曹云霄 committed
476
                [weakSelf.shopResponseArray removeObject:model];
勾芒's avatar
勾芒 committed
477
            }
曹云霄's avatar
曹云霄 committed
478
            [weakSelf.shoppingTableview deleteRowsAtIndexPaths:delecteArray withRowAnimation:UITableViewRowAnimationLeft];
曹云霄's avatar
曹云霄 committed
479
            dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(1 * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{
曹云霄's avatar
曹云霄 committed
480
                [weakSelf.shoppingTableview reloadData];
曹云霄's avatar
曹云霄 committed
481
            });
482
            [XBLoadingView showHUDViewWithSuccessText:@"删除成功" completeBlock:nil];
曹云霄's avatar
曹云霄 committed
483 484 485
            [weakSelf CalculateSelectedGoodsAllprice];
            weakSelf.allSelectedButton.selected = NO;
            [weakSelf.settlementButton setTitle:@"去结算(0)" forState:UIControlStateNormal];
勾芒's avatar
勾芒 committed
486 487
        }else
        {
曹云霄's avatar
曹云霄 committed
488
            [XBLoadingView showHUDViewWithText:returnValue[@"message"]];
勾芒's avatar
勾芒 committed
489
        }
490
    } WithFailureBlock:^(NSError *error) {
491
        [XBLoadingView hideHUDViewWithDefault];
曹云霄's avatar
曹云霄 committed
492
        [XBLoadingView showHUDViewWithText:error.localizedDescription];
勾芒's avatar
勾芒 committed
493
    }];
勾芒's avatar
勾芒 committed
494
}
曹云霄's avatar
曹云霄 committed
495

496 497 498 499 500 501
#pragma mark - 修改多个商品数量、价格
- (void)modifyMultipleGoodsNnumberOrPrice:(NSArray<ShopcarModel *> *)goodsArray completeModify:(void(^)(BOOL boolValue))finish
{
    WS(weakSelf);
    dispatch_group_t group = dispatch_group_create();
    static NSInteger number = ZERO;
曹云霄's avatar
曹云霄 committed
502
    [XBLoadingView showHUDViewWithDefault];
503
    [goodsArray enumerateObjectsUsingBlock:^(ShopcarModel * _Nonnull model, NSUInteger idx, BOOL * _Nonnull stop) {
曹云霄's avatar
曹云霄 committed
504

505 506
        dispatch_group_enter(group);
        NSLog(@"%@",[NSString stringWithFormat:@"%@%@/%@/%@/%ld",SERVERREQUESTURL(CHANGESHOPPINGBAGNUMBERPRICE),model.fid,model.goodsId,[model.costPrice stringValue],model.goodsNum]);
507
        [[NetworkRequestClassManager Manager] NetworkWithDictionaryRequestWithURL:[NSString stringWithFormat:@"%@%@/%@/%@/%ld",SERVERREQUESTURL(CHANGESHOPPINGBAGNUMBERPRICE),model.fid,model.goodsId,[model.costPrice stringValue],model.goodsNum]  WithRequestType:ONE WithParameter:nil WithReturnValueBlock:^(id returnValue) {
508 509 510 511 512 513 514 515 516 517 518 519
            
            dispatch_group_leave(group);
            if ([returnValue[@"code"] isEqualToNumber:@0]) {
                number ++;
            }
        } WithFailureBlock:^(NSError *error) {
            dispatch_group_leave(group);
        }];
    }];
    
    dispatch_group_notify(group, dispatch_get_main_queue(), ^{
        
曹云霄's avatar
曹云霄 committed
520
        [XBLoadingView hideHUDViewWithDefault];
521 522 523 524 525
        if (number == goodsArray.count) {
            [weakSelf CalculateSelectedGoodsAllprice];
            [weakSelf QueryShoppingCarNumber];
            finish(YES);
        }else {
曹云霄's avatar
曹云霄 committed
526 527
            [XBLoadingView showHUDViewWithText:@"修改失败"];
            finish(NO);
528 529 530 531
        }
        number = ZERO;
    });
}
勾芒's avatar
勾芒 committed
532

533 534
#pragma mark -修改单个商品数量、价格
- (void)changeGoodsNumber:(NSInteger)goodsNumber WithcostPrice:(CGFloat)costprice Withcellindex:(NSInteger)cellindex returnValue:(void (^)(id))result
勾芒's avatar
勾芒 committed
535
{
曹云霄's avatar
曹云霄 committed
536
    [XBLoadingView showHUDViewWithDefault];
勾芒's avatar
勾芒 committed
537 538 539 540
    //保存商品数量
    ShopcarModel *model = [self.shopResponseArray objectAtIndex_opple:cellindex];
    model.goodsNum = goodsNumber;
    //保存成交价格
541
    model.costPrice = [NSNumber numberWithFloat:costprice];
曹云霄's avatar
曹云霄 committed
542
    //在服务器保存数量、成交价
勾芒's avatar
勾芒 committed
543
    //购物车ID
544
    NSString *carid = model.fid;
勾芒's avatar
勾芒 committed
545
    //商品id
546
    NSString *goodsis = model.goodsId;
勾芒's avatar
勾芒 committed
547
    //成交价
勾芒's avatar
勾芒 committed
548
    NSString *costpriceString = [NSString stringWithFormat:@"%.2f",costprice];
勾芒's avatar
勾芒 committed
549
    //商品数量
曹云霄's avatar
曹云霄 committed
550 551
    NSString *goodsNumberString = [NSString stringWithFormat:@"%ld",(long)goodsNumber];
    WS(weakSelf);
552
    [[NetworkRequestClassManager Manager] NetworkWithDictionaryRequestWithURL:[NSString stringWithFormat:@"%@%@/%@/%@/%@",SERVERREQUESTURL(CHANGESHOPPINGBAGNUMBERPRICE),carid,goodsis,costpriceString,goodsNumberString]  WithRequestType:ONE WithParameter:nil WithReturnValueBlock:^(id returnValue) {
勾芒's avatar
勾芒 committed
553
        
曹云霄's avatar
曹云霄 committed
554
        [XBLoadingView hideHUDViewWithDefault];
曹云霄's avatar
曹云霄 committed
555
        result(returnValue);//提供是否支持修改的参数
勾芒's avatar
勾芒 committed
556
        if ([returnValue[@"code"] isEqualToNumber:@0]) {
曹云霄's avatar
曹云霄 committed
557 558 559 560
            [weakSelf CalculateSelectedGoodsAllprice];
            [weakSelf QueryShoppingCarNumber];
        }else{
            
曹云霄's avatar
曹云霄 committed
561
            [XBLoadingView showHUDViewWithText:returnValue[@"message"]];
勾芒's avatar
勾芒 committed
562
        }
563
    }WithFailureBlock:^(NSError *error) {
564
        [XBLoadingView hideHUDViewWithDefault];
曹云霄's avatar
曹云霄 committed
565
        [XBLoadingView showHUDViewWithText:error.localizedDescription];
勾芒's avatar
勾芒 committed
566
    }];
勾芒's avatar
勾芒 committed
567
}
曹云霄's avatar
曹云霄 committed
568

曹云霄's avatar
曹云霄 committed
569 570 571 572 573 574 575 576 577 578 579
#pragma mark -友好界面
- (UIImage *)imageForEmptyDataSet:(UIScrollView *)scrollView
{
    return kNoDataImage;
}

- (NSAttributedString *)titleForEmptyDataSet:(UIScrollView *)scrollView
{
    return [[NSAttributedString alloc]initWithString:@"暂无数据" attributes:nil];
}

勾芒's avatar
勾芒 committed
580

581

曹云霄's avatar
曹云霄 committed
582
@end