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

#import "ShoppingBagViewController.h"
#import "ShoppingTableViewCell.h"
#import "TOGoodsEntityModel.h"
#import "ShopcarModel.h"
13
@interface ShoppingBagViewController ()<UITableViewDelegate,UITableViewDataSource,UIGestureRecognizerDelegate,DZNEmptyDataSetSource,ChangeGoodsNumberDelegate,UIImagePickerControllerDelegate>
曹云霄's avatar
曹云霄 committed
14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104

@end

@implementation ShoppingBagViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    
    [self uiConfigAction];
    [self setDelegateandDataSource];
}

#pragma mark -UI
- (void)uiConfigAction
{
    self.view.backgroundColor = [UIColor clearColor];
    UITapGestureRecognizer *tap = [[UITapGestureRecognizer alloc]initWithTarget:self action:@selector(dismissAction:)];
    tap.delegate = self;
    [self.view addGestureRecognizer:tap];
    self.sureButton.layer.masksToBounds = YES;
    self.sureButton.layer.cornerRadius = kCornerRadius;
}

#pragma mark -数据源
- (void)setDatasArray:(NSMutableArray *)datasArray
{
    NSMutableArray *array = [NSMutableArray array];
    for (TOGoodsEntityModel *model in datasArray) {
        ShopcarModel *shopModel = [[ShopcarModel alloc]init];
        VOResellerGoodsEntity *newModel = [[VOResellerGoodsEntity alloc]init];
        newModel.categoryName = model.categoryName;
        newModel.brandName = model.brandName;
        newModel.fid = model.fid;
        newModel.createDate = model.createDate;
        newModel.onlineTime = model.onlineTime;
        newModel.company = model.company;
        newModel.code = model.code;
        newModel.name = model.name;
        newModel.number = model.number;
        newModel.categoryId = model.categoryId;
        newModel.brandId = model.brandId;
        newModel.keyword = model.keyword;
        newModel.guidePrice = model.guidePrice;
        newModel.tagPrice = model.tagPrice;
        newModel.costPrice = model.costPrice;
        newModel.state = model.state;
        newModel.inv = model.inv;
        newModel.detailedIntro = model.detailedIntro;
        newModel.weight = model.weight;
        newModel.unit = model.unit;
        newModel.productProfile = model.productProfile;
        newModel.power = model.power;
        newModel.colorTemperature = model.colorTemperature;
        newModel.spec = model.spec;
        newModel.function = model.function;
        newModel.material = model.material;
        newModel.lightSource = model.lightSource;
        newModel.goodsStyle = model.goodsStyle;
        newModel.series = model.series;
        newModel.area = model.area;
        newModel.size = model.size;
        newModel.space = model.space;
        newModel.installMethod = model.installMethod;
        newModel.lightNumber = model.lightNumber;
        newModel.pictures = model.pictures;
        newModel.barcode = model.barcode;
        shopModel.goods = newModel;
        shopModel.costPrice = model.costPrice;
        shopModel.goodsNum = model.goodsNumber;
        [array addObject:shopModel];
    }
    _datasArray = [NSMutableArray arrayWithArray:array];
    [self.shoppingBagTableView reloadData];
}

#pragma mark -tableview
- (void)setDelegateandDataSource
{
    self.shoppingBagTableView.delegate = self;
    self.shoppingBagTableView.dataSource = self;
    self.shoppingBagTableView.emptyDataSetSource = self;
    self.shoppingBagTableView.tableFooterView = [UIView new];
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    ShoppingTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"Shopping" forIndexPath:indexPath];
    cell.selectionStyle = UITableViewCellSelectionStyleNone;
    cell.model = [self.datasArray objectAtIndex_opple:indexPath.row];
    cell.delegate = self;
    cell.cellindex = indexPath.row;
105 106
    cell.clinchTextfield.userInteractionEnabled = NO;
    cell.ClinchPriceBackView.backgroundColor = cell.backgroundColor;
曹云霄's avatar
曹云霄 committed
107
    //cell选中回调
108
    WS(weakSelf);
曹云霄's avatar
曹云霄 committed
109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 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
    [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.datasArray.count;
}

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


#pragma mark -选中商品回调
- (void)setSelectedButton:(NSInteger)index;
{
    ShopcarModel *model = [self.datasArray objectAtIndex_opple:index];
    model.isSelected = !model.isSelected;
    
    NSInteger goodsNumber = 0;
    for (ShopcarModel *model in self.datasArray) {
        if (model.isSelected) {
            goodsNumber ++;
        }
    }
    if (goodsNumber == self.datasArray.count) {
        self.totalSelectedButton.selected = YES;
    }else
    {
        self.totalSelectedButton.selected = NO;
    }
    [self CalculateSelectedGoodsAllprice];
曹云霄's avatar
曹云霄 committed
150
    [self.sureButton setTitle:[NSString stringWithFormat:@"确认添加(%ld)",goodsNumber] forState:UIControlStateNormal];
曹云霄's avatar
曹云霄 committed
151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212
}

#pragma mark -计算选中后的商品总金额
- (void)CalculateSelectedGoodsAllprice
{
    CGFloat allPrice = 0;
    for (ShopcarModel *model in self.datasArray) {
        if (model.isSelected) {
            allPrice += ([model.costPrice floatValue]?[model.costPrice floatValue]:[model.goods.tagPrice floatValue]) * model.goodsNum;
        }
    }
    self.totalPriceLabe.text = [NSString stringWithFormat:@"¥%.2f",allPrice];
}


#pragma mark -全选
- (IBAction)totalButtonSelectedAction:(UIButton *)sender {
    
    sender.selected = !sender.selected;
    if (sender.selected) {
        
        //全部选中
        for (ShoppingTableViewCell *cell in self.shoppingBagTableView.visibleCells) {
            
            cell.selectedButton.selected = YES;
        }
        for (ShopcarModel *model in self.datasArray) {
            
            model.isSelected = YES;
        }
        [self.sureButton setTitle:[NSString stringWithFormat:@"确认添加(%ld)",self.datasArray.count] forState:UIControlStateNormal];
    }else
    {
        //取消全部选中
        for (ShoppingTableViewCell *cell in self.shoppingBagTableView.visibleCells) {
            
            cell.selectedButton.selected = NO;
        }
        for (ShopcarModel *model in self.datasArray) {
            
            model.isSelected = NO;
        }
        [self.sureButton setTitle:@"确认添加(0)" forState:UIControlStateNormal];
    }
    //计算总金额
    [self CalculateSelectedGoodsAllprice];
}

#pragma mark -删除
- (IBAction)delectedButtonSelectedAction:(UIButton *)sender {
    
    NSMutableArray *indexpathArray = [NSMutableArray array];
    NSMutableArray *delectedModelArray = [NSMutableArray array];
    for (int i=0; i < self.datasArray.count; i++) {
        ShopcarModel *model = [self.datasArray objectAtIndex_opple:i];
        if (model.isSelected) {
            NSIndexPath *indexPath = [NSIndexPath indexPathForRow:i inSection:0];
            [indexpathArray addObject:indexPath];
            [delectedModelArray addObject:model];
        }
    }
    if (!indexpathArray.count) {
曹云霄's avatar
曹云霄 committed
213
        [XBLoadingView showHUDViewWithText:@"未选中任何商品"];
曹云霄's avatar
曹云霄 committed
214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239
    }else
    {
        for (ShopcarModel *model in delectedModelArray) {
            [self.datasArray removeObject:model];
        }
        [self.sureButton setTitle:@"确认添加(0)" forState:UIControlStateNormal];
        self.totalSelectedButton.selected = NO;
        self.totalPriceLabe.text = nil;
       [self.shoppingBagTableView deleteRowsAtIndexPaths:indexpathArray withRowAnimation:UITableViewRowAnimationLeft];
        dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(1 * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{
            [self.shoppingBagTableView reloadData];
        });
    }
}

#pragma mark -添加至购物车
- (IBAction)addShoppingBagButtonClickAction:(UIButton *)sender {
    
    NSMutableArray *array = [NSMutableArray array];
    for (int i=0; i<self.datasArray.count; i++) {
        ShopcarModel *model = [self.datasArray objectAtIndex_opple:i];
        if (model.isSelected) {
            [array addObject:model];
        }
    }
    if (!array.count) {
曹云霄's avatar
曹云霄 committed
240
        [XBLoadingView showHUDViewWithText:@"请先勾选商品"];
曹云霄's avatar
曹云霄 committed
241 242 243
        return;
    }
    
曹云霄's avatar
曹云霄 committed
244
    [XBLoadingView showHUDViewWithDefault];
曹云霄's avatar
曹云霄 committed
245 246 247 248 249
    NSMutableString *goodsIds = [[NSMutableString alloc]init];
    NSMutableString *goodsCounts = [[NSMutableString alloc]init];
    for (ShopcarModel *model in self.datasArray) {
        if (model.isSelected) {
            [goodsIds appendFormat:@"%@,",model.goods.fid];
250
            [goodsCounts appendFormat:@"%ld,",(long)model.goodsNum];
曹云霄's avatar
曹云霄 committed
251 252 253 254 255 256
        }
    }
    SaveShoppingCartRequest *shopCar = [[SaveShoppingCartRequest alloc]init];
    shopCar.consumerId = [Customermanager manager].model.fid;
    shopCar.goodsId = [goodsIds substringToIndex:goodsIds.length-1];
    shopCar.count = [goodsCounts substringToIndex:goodsCounts.length-1];
曹云霄's avatar
曹云霄 committed
257
    WS(weakSelf);
258
    [[NetworkRequestClassManager Manager] NetworkRequestWithURL:SERVERREQUESTURL(ADDSHOPPINGBAG)  WithRequestType:ZERO WithParameter:shopCar WithReturnValueBlock:^(id returnValue) {
曹云霄's avatar
曹云霄 committed
259
        
曹云霄's avatar
曹云霄 committed
260
        [XBLoadingView hideHUDViewWithDefault];
曹云霄's avatar
曹云霄 committed
261
        if ([returnValue[@"code"] isEqualToNumber:@0]) {
曹云霄's avatar
曹云霄 committed
262 263
            if (weakSelf.dismissBlock) {
                weakSelf.dismissBlock();
264
            }
曹云霄's avatar
曹云霄 committed
265 266
            [weakSelf dismissViewControllerAnimated:YES completion:nil];
            [weakSelf QueryShoppingCarNumber];
267
            [XBLoadingView showHUDViewWithSuccessText:@"加入购物车成功" completeBlock:nil];
曹云霄's avatar
曹云霄 committed
268 269
        }else
        {
曹云霄's avatar
曹云霄 committed
270
            [XBLoadingView showHUDViewWithText:returnValue[@"message"]];
曹云霄's avatar
曹云霄 committed
271
        }
272
    }WithFailureBlock:^(NSError *error) {
曹云霄's avatar
曹云霄 committed
273
        [XBLoadingView showHUDViewWithText:error.localizedDescription];
曹云霄's avatar
曹云霄 committed
274 275 276 277
    }];
}


278 279 280 281 282 283 284 285 286
#pragma mark -系统提示框
- (void)promptCustomerWithString:(NSString *)message
{
    UIAlertController *alertVC = [UIAlertController alertControllerWithTitle:@"提示" message:message preferredStyle:UIAlertControllerStyleAlert];
    [alertVC addAction:[UIAlertAction actionWithTitle:@"我知道了" style:UIAlertActionStyleDefault handler:^(UIAlertAction * _Nonnull action) {
        [alertVC dismissViewControllerAnimated:YES completion:nil];
    }]];
    [self presentViewController:alertVC animated:YES completion:nil];
}
287

288 289
#pragma mark -修改单个商品数量、价格
- (void)changeGoodsNumber:(NSInteger)goodsNumber WithcostPrice:(CGFloat)costprice Withcellindex:(NSInteger)cellindex returnValue:(void (^)(id))result
曹云霄's avatar
曹云霄 committed
290
{
曹云霄's avatar
曹云霄 committed
291
    [XBLoadingView showHUDViewWithDefault];
292
    //保存商品数量
曹云霄's avatar
曹云霄 committed
293
    ShopcarModel *model = [self.datasArray objectAtIndex_opple:cellindex];
曹云霄's avatar
曹云霄 committed
294 295
    model.goodsNum = goodsNumber;
    //保存成交价格
296
    model.costPrice = [NSNumber numberWithFloat:costprice];
曹云霄's avatar
曹云霄 committed
297 298
    //在服务器保存数量、成交价
    //购物车ID
299
    NSString *carid = model.fid;
曹云霄's avatar
曹云霄 committed
300
    //商品id
301
    NSString *goodsis = model.goodsId;
曹云霄's avatar
曹云霄 committed
302 303 304 305 306
    //成交价
    NSString *costpriceString = [NSString stringWithFormat:@"%.2f",costprice];
    //商品数量
    NSString *goodsNumberString = [NSString stringWithFormat:@"%ld",(long)goodsNumber];
    WS(weakSelf);
307
    [[NetworkRequestClassManager Manager] NetworkWithDictionaryRequestWithURL:[NSString stringWithFormat:@"%@%@/%@/%@/%@",SERVERREQUESTURL(CHANGESHOPPINGBAGNUMBERPRICE),carid,goodsis,costpriceString,goodsNumberString]  WithRequestType:ONE WithParameter:nil WithReturnValueBlock:^(id returnValue) {
曹云霄's avatar
曹云霄 committed
308
        
曹云霄's avatar
曹云霄 committed
309
        [XBLoadingView hideHUDViewWithDefault];
曹云霄's avatar
曹云霄 committed
310 311 312 313 314 315
        result(returnValue);//提供是否支持修改的参数
        if ([returnValue[@"code"] isEqualToNumber:@0]) {
            [weakSelf CalculateSelectedGoodsAllprice];
            [weakSelf QueryShoppingCarNumber];
        }else{
            
曹云霄's avatar
曹云霄 committed
316
            [XBLoadingView showHUDViewWithText:returnValue[@"message"]];
曹云霄's avatar
曹云霄 committed
317
        }
318
    }WithFailureBlock:^(NSError *error) {
曹云霄's avatar
曹云霄 committed
319
        [XBLoadingView showHUDViewWithText:error.localizedDescription];
曹云霄's avatar
曹云霄 committed
320
    }];
曹云霄's avatar
曹云霄 committed
321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345
}

#pragma mark -销毁
- (void)dismissAction:(UITapGestureRecognizer *)tap
{
    if (self.dismissBlock) {
        self.dismissBlock();
    }
    [self dismissViewControllerAnimated:YES completion:nil];
}

#pragma mark -UIGestureRecognizerDelegate代理
- (BOOL)gestureRecognizer:(UIGestureRecognizer *)gestureRecognizer shouldReceiveTouch:(UITouch *)touch
{
    //取消子视图的的时间穿透,否则子视图的点击无效,会被传递到父视图响应
    if (CGRectContainsPoint(self.shoppingBagBackView.frame, [touch locationInView:self.view.window])) {
        
        return NO;
    }
    return YES;
}

#pragma mark -友好界面
- (UIImage *)imageForEmptyDataSet:(UIScrollView *)scrollView
{
曹云霄's avatar
曹云霄 committed
346
    return kNoDataImage;
曹云霄's avatar
曹云霄 committed
347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370
}

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


- (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