1
2
3
4
5
6
7
8
9
10
11
12
13
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
105
106
107
108
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
150
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
213
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
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
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
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
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
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
//
// 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 ()<UITableViewDelegate,UITableViewDataSource,ChangeGoodsNumberDelegate,DZNEmptyDataSetSource>
@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;
if ([self.navigationController respondsToSelector:@selector(interactivePopGestureRecognizer)]) {
self.navigationController.interactivePopGestureRecognizer.enabled = NO;
}
[self.shopResponseArray removeAllObjects];
[self InitializeState];
[self getShoppingCardata];
}
#pragma mark -视图即将消失
- (void)viewWillDisappear:(BOOL)animated
{
[super viewWillDisappear:animated];
self.navigationController.fd_fullscreenPopGestureRecognizer.enabled = YES;
if ([self.navigationController respondsToSelector:@selector(interactivePopGestureRecognizer)]) {
self.navigationController.interactivePopGestureRecognizer.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];
WS(weakSelf);
[[NetworkRequestClassManager Manager] NetworkRequestWithURL:SERVERREQUESTURL(SHOPPINGBAG) WithRequestType:0 WithParameter:shopcarNumber WithReturnValueBlock:^(id returnValue) {
weakSelf.shoppingTableview.emptyDataSetSource = weakSelf;
[weakSelf endRefreshingForTableView:weakSelf.shoppingTableview];
[weakSelf 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;
[weakSelf.shopResponseArray addObject:model];
}
[weakSelf.shoppingTableview reloadData];
}else
{
[weakSelf ErrorMBProgressView:returnValue[@"message"]];
}
} WithErrorCodeBlock:^(id errorCodeValue) {
[weakSelf RemoveMBProgressHUDLoding];
[weakSelf ErrorMBProgressView:@"无网络"];
} WithFailureBlock:^(id error) {
[weakSelf RemoveMBProgressHUDLoding];
[weakSelf endRefreshingForTableView:weakSelf.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 ErrorMBProgressView: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;
WS(weakSelf);
[[NetworkRequestClassManager Manager] NetworkRequestWithURL:SERVERREQUESTURL(REMOVESHOPPINGBAG) WithRequestType:0 WithParameter:delecteGoods WithReturnValueBlock:^(id returnValue) {
[weakSelf RemoveMBProgressHUDLoding];
if ([returnValue[@"code"] isEqualToNumber:@0]) {
[weakSelf QueryShoppingCarNumber];
//商品cell
NSMutableArray *cellArray = [NSMutableArray array];
//删除商品
for (int i=0; i<weakSelf.shopResponseArray.count; i++) {
ShopcarModel *model = [weakSelf.shopResponseArray objectAtIndex_opple:i];
if ([goodsCode containsObject:model.fid]) {
[weakSelf.shopResponseArray removeObject:model];
NSIndexPath *indexpath = [NSIndexPath indexPathForRow:i inSection:0];
[cellArray addObject:indexpath];
}
}
//删除cell
[weakSelf.shoppingTableview deleteRowsAtIndexPaths:cellArray withRowAnimation:UITableViewRowAnimationTop];
}else
{
[weakSelf ErrorMBProgressView:returnValue[@"message"]];
}
} WithErrorCodeBlock:^(id errorCodeValue) {
[weakSelf RemoveMBProgressHUDLoding];
[weakSelf ErrorMBProgressView:@"无网络"];
} WithFailureBlock:^(id error) {
[weakSelf RemoveMBProgressHUDLoding];
}];
}];
[self.navigationController pushViewController:generateOrder animated:YES];
}
#pragma mark -全选
- (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;
}
[self.settlementButton setTitle:[NSString stringWithFormat:@"去结算(%ld)",self.shopResponseArray.count] forState:UIControlStateNormal];
}else
{
//取消全部选中
for (ShoppingTableViewCell *cell in self.shoppingTableview.visibleCells) {
cell.selectedButton.selected = NO;
}
for (ShopcarModel *model in self.shopResponseArray) {
model.isSelected = NO;
}
[self.settlementButton setTitle:@"去结算(0)" forState:UIControlStateNormal];
}
//计算总金额
[self CalculateSelectedGoodsAllprice];
}
#pragma mark -计算选中后的商品总金额
- (void)CalculateSelectedGoodsAllprice
{
CGFloat allPrice = 0;
for (ShopcarModel *model in self.shopResponseArray) {
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)delecteSelectedGoods:(UIButton *)sender {
[self CreateMBProgressHUDLoding];
DeleteCartRequest *delecteGoods = [[DeleteCartRequest alloc]init];
//code数组
NSMutableArray *codeArr = [NSMutableArray array];
//需要删除的cell数组indexpath
NSMutableArray *delecteArray = [NSMutableArray array];
//模型数组
NSMutableArray *delectemodel = [NSMutableArray array];
for (int i=0; i<self.shopResponseArray.count; i++) {
ShopcarModel *model = [self.shopResponseArray objectAtIndex_opple:i];
if (model.isSelected) {
[codeArr addObject:model.fid];
[delectemodel addObject:model];
NSIndexPath *indexpath = [NSIndexPath indexPathForRow:i inSection:0];
[delecteArray addObject:indexpath];
}
}
//没有选中任何商品
if (codeArr.count == 0) {
[self RemoveMBProgressHUDLoding];
return;
}
delecteGoods.cartIds = codeArr;
WS(weakSelf);
[[NetworkRequestClassManager Manager] NetworkRequestWithURL:SERVERREQUESTURL(REMOVESHOPPINGBAG) WithRequestType:0 WithParameter:delecteGoods WithReturnValueBlock:^(id returnValue) {
[weakSelf RemoveMBProgressHUDLoding];
if ([returnValue[@"code"] isEqualToNumber:@0]) {
[weakSelf QueryShoppingCarNumber];
//删除商品
for (ShopcarModel *model in delectemodel) {
[weakSelf.shopResponseArray removeObject:model];
}
[weakSelf.shoppingTableview deleteRowsAtIndexPaths:delecteArray withRowAnimation:UITableViewRowAnimationLeft];
dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(1 * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{
[weakSelf.shoppingTableview reloadData];
});
[weakSelf SuccessMBProgressView:@"删除成功"];
[weakSelf CalculateSelectedGoodsAllprice];
weakSelf.allSelectedButton.selected = NO;
[weakSelf.settlementButton setTitle:@"去结算(0)" forState:UIControlStateNormal];
}else
{
[weakSelf ErrorMBProgressView:returnValue[@"message"]];
}
} WithErrorCodeBlock:^(id errorCodeValue) {
[weakSelf RemoveMBProgressHUDLoding];
[weakSelf ErrorMBProgressView:@"无网络"];
} WithFailureBlock:^(id error) {
[weakSelf RemoveMBProgressHUDLoding];
}];
}
#pragma mark -改变商品数量
- (void)ChangeGoodsNumber:(NSInteger)goodsNumber WithcostPrice:(CGFloat)costprice Withcellindex:(NSInteger)cellindex returnValue:(void (^)(id))result
{
[self CreateMBProgressHUDLoding];
//保存商品数量
ShopcarModel *model = [self.shopResponseArray objectAtIndex_opple:cellindex];
model.goodsNum = goodsNumber;
// [self.shopResponseArray replaceObjectAtIndex:cellindex withObject:model];
//保存成交价格
ShopcarModel *Newmodel = [self.shopResponseArray objectAtIndex_opple:cellindex];
Newmodel.costPrice = [NSNumber numberWithFloat:costprice];
// [self.shopResponseArray replaceObjectAtIndex:cellindex withObject:Newmodel];
//在服务器保存数量、成交价
//购物车ID
NSString *carid = [[self.shopResponseArray objectAtIndex_opple:cellindex] fid];
//商品id
NSString *goodsis = [[[self.shopResponseArray objectAtIndex_opple:cellindex] goods] fid];
//成交价
NSString *costpriceString = [NSString stringWithFormat:@"%.2f",costprice];
//商品数量
NSString *goodsNumberString = [NSString stringWithFormat:@"%ld",(long)goodsNumber];
WS(weakSelf);
[[NetworkRequestClassManager Manager] NetworkWithDictionaryRequestWithURL:[NSString stringWithFormat:@"%@%@/%@/%@/%@",SERVERREQUESTURL(CHANGESHOPPINGBAGNUMBERPRICE),carid,goodsis,costpriceString,goodsNumberString] WithRequestType:1 WithParameter:nil WithReturnValueBlock:^(id returnValue) {
[weakSelf RemoveMBProgressHUDLoding];
result(returnValue);//提供是否支持修改的参数
if ([returnValue[@"code"] isEqualToNumber:@0]) {
NSLog(@"写入服务器成功");
[weakSelf CalculateSelectedGoodsAllprice];
[weakSelf QueryShoppingCarNumber];
}else{
[weakSelf ErrorMBProgressView:returnValue[@"message"]];
}
} WithErrorCodeBlock:^(id errorCodeValue) {
[weakSelf RemoveMBProgressHUDLoding];
[weakSelf ErrorMBProgressView:@"无网络"];
} WithFailureBlock:^(id error) {
NSLog(@"写入服务器失败");
[weakSelf RemoveMBProgressHUDLoding];
}];
}
#pragma mark -友好界面
- (UIImage *)imageForEmptyDataSet:(UIScrollView *)scrollView
{
return kNoDataImage;
}
- (NSAttributedString *)titleForEmptyDataSet:(UIScrollView *)scrollView
{
return [[NSAttributedString alloc]initWithString:@"暂无数据" attributes:nil];
}
@end