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

#import "GenerateOrdersViewController.h"
#import "GenerateOrdersTableViewCell.h"
#import "GenerateOrdersModifyTableViewCell.h"
#import "PersonInformationTableViewCell.h"
#import "CommodityListTableViewCell.h"
#import "AllpriceTableViewCell.h"
#import "AddaddressViewController.h"
#import "ModifyShippingAddressView.h"
#import "AppDelegate.h"
#import "OrderdetailsViewController.h"
曹云霄's avatar
曹云霄 committed
19
#import "AddressViewController.h"
勾芒's avatar
勾芒 committed
20 21
#import "AddressModel.h"
@interface GenerateOrdersViewController ()<UITableViewDelegate,UITableViewDataSource,delecteDelegate>
曹云霄's avatar
曹云霄 committed
22 23 24 25 26 27

/**
 *  增加或者修改地址
 */
@property (nonatomic,strong) ModifyShippingAddressView *addressView;

曹云霄's avatar
曹云霄 committed
28

曹云霄's avatar
曹云霄 committed
29

勾芒's avatar
勾芒 committed
30 31 32 33 34 35
/**
 *  数据源
 */
@property (nonatomic,strong) NSMutableArray *datasArray;


曹云霄's avatar
曹云霄 committed
36 37 38 39 40
@property (nonatomic,strong) UIView *Tabbarview;
@end

@implementation GenerateOrdersViewController

勾芒's avatar
勾芒 committed
41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56
/**
 *  初始化数据源
 */
- (NSMutableArray *)datasArray
{
    if (_datasArray == nil) {
        
        _datasArray = [NSMutableArray array];
        for (int i=0; i<3; i++) {
            NSMutableArray *arr = [NSMutableArray array];
            [_datasArray addObject:arr];
        }
        //加入客户信息model
        Customermanager *customerModel = [Customermanager manager];
        [[_datasArray firstObject] addObject:customerModel];
        
勾芒's avatar
勾芒 committed
57 58
        AddressModel *model = [[AddressModel alloc]init];
        model.name = @"地址占位";
勾芒's avatar
勾芒 committed
59
        //地址占位
勾芒's avatar
勾芒 committed
60
        [[_datasArray objectAtIndex_opple:1] addObject:model];
勾芒's avatar
勾芒 committed
61 62 63 64 65 66 67
        
        //加入商品信息
        [_datasArray replaceObjectAtIndex:2 withObject:_settlementGoodsdatas];
    }
    
    return _datasArray;
}
曹云霄's avatar
曹云霄 committed
68 69 70



曹云霄's avatar
曹云霄 committed
71 72 73 74
- (void)viewDidLoad {
    [super viewDidLoad];
    
    [self uiConfigAction];
勾芒's avatar
勾芒 committed
75
    [self getAddressDatasRequest];
曹云霄's avatar
曹云霄 committed
76 77 78 79 80 81 82 83 84 85 86
}

#pragma mark -UI
- (void)uiConfigAction
{
    self.generateOrderTableview.dataSource = self;
    self.generateOrderTableview.delegate = self;
    self.generateOrderTableview.tableFooterView = [UIView new];
}


勾芒's avatar
勾芒 committed
87 88 89
#pragma mark -获得地址信息
- (void)getAddressDatasRequest
{
勾芒's avatar
勾芒 committed
90
    [self CreateMBProgressHUDLoding];
勾芒's avatar
勾芒 committed
91 92
    [[NetworkRequestClassManager Manager] NetworkWithDictionaryRequestWithURL:[NSString stringWithFormat:@"%@%@%@",ServerAddress,@"/shippingAddress/listAddress/",[Customermanager manager].customerID] WithRequestType:1 WithParameter:nil WithReturnValueBlock:^(id returnValue) {
        
勾芒's avatar
勾芒 committed
93
        [self RemoveMBProgressHUDLoding];
勾芒's avatar
勾芒 committed
94 95 96
        if ([returnValue[@"code"] isEqualToNumber:@0]) {
         
            RsShippingAddrEntity *address = [[RsShippingAddrEntity alloc]initWithDictionary:returnValue[@"data"] error:nil];
勾芒's avatar
勾芒 committed
97
            
勾芒's avatar
勾芒 committed
98 99 100
            for (NSInteger i=address.list.count-1; i>=0; i--) {
                
                TOShippingAddrEntity *model = [address.list objectAtIndex_opple:i];
勾芒's avatar
勾芒 committed
101 102 103 104 105 106 107 108 109 110 111 112 113 114
                //自定义model
                AddressModel *Newmodel = [[AddressModel alloc]init];
                Newmodel.fid = model.fid;
                Newmodel.createDate = model.createDate;
                Newmodel.sysOrgCode = model.sysOrgCode;
                Newmodel.name = model.name;
                Newmodel.miblephone = model.miblephone;
                Newmodel.province = model.province;
                Newmodel.city = model.city;
                Newmodel.country = model.country;
                Newmodel.consumerId = model.consumerId;
                Newmodel.address = model.address;
                
                [[self.datasArray objectAtIndex_opple:1]insertObject:Newmodel atIndex:0];
勾芒's avatar
勾芒 committed
115 116 117 118 119 120 121 122 123 124 125 126 127 128
            }
            
            [self.generateOrderTableview reloadData];
        }
        else
        {
            [self ErrorMBProgressView:returnValue[@"message"]];
        }
        
    } WithErrorCodeBlock:^(id errorCodeValue) {
        
        
    } WithFailureBlock:^(id error) {
        NSLog(@"%@",error);
勾芒's avatar
勾芒 committed
129
        [self RemoveMBProgressHUDLoding];
勾芒's avatar
勾芒 committed
130 131 132
    }];
}

曹云霄's avatar
曹云霄 committed
133 134 135 136 137 138 139 140


- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    
    if (indexPath.section == 0 && indexPath.row == 0) {
        
        PersonInformationTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"secondcell" forIndexPath:indexPath];
勾芒's avatar
勾芒 committed
141
        cell.selectionStyle = UITableViewCellSelectionStyleNone;
曹云霄's avatar
曹云霄 committed
142 143 144 145
        return cell;
    }
    else if (indexPath.section == 1)
    {
勾芒's avatar
勾芒 committed
146 147 148
        NSArray *Addressarr = [self.datasArray objectAtIndex_opple:indexPath.section];
        if (indexPath.row == Addressarr.count-1) {
            
勾芒's avatar
勾芒 committed
149 150 151 152
            GenerateOrdersModifyTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"Modifycell" forIndexPath:indexPath];
            //增加地址或者修改
            [cell.addAddressButton addTarget:self action:@selector(AddAddressButtonClick) forControlEvents:UIControlEventTouchUpInside];
            [cell.changeButton addTarget:self action:@selector(ChangeButtonClick) forControlEvents:UIControlEventTouchUpInside];
勾芒's avatar
勾芒 committed
153
            cell.selectionStyle = UITableViewCellSelectionStyleNone;
勾芒's avatar
勾芒 committed
154
            cell.backgroundColor = kTCColor(242, 242, 242);
曹云霄's avatar
曹云霄 committed
155 156
            return cell;
        }
勾芒's avatar
勾芒 committed
157
        GenerateOrdersTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"consigneecell" forIndexPath:indexPath];
勾芒's avatar
勾芒 committed
158 159
        cell.selectionStyle = UITableViewCellSelectionStyleNone;
        cell.backgroundColor = kTCColor(242, 242, 242);
勾芒's avatar
勾芒 committed
160 161 162 163 164 165 166
        cell.indexNumber = indexPath.row;
        //回调
        [cell setBlockSeletced:^(NSInteger index) {
            
            [self SetCustomerButtonClick:index];
        }];
        cell.NewModel = [self.datasArray objectAtIndex_opple:indexPath.section][indexPath.row];
曹云霄's avatar
曹云霄 committed
167 168 169 170 171
        return cell;
    }
    else if (indexPath.section == 2)
    {

勾芒's avatar
勾芒 committed
172 173
        NSArray *Addressarr = [self.datasArray objectAtIndex_opple:indexPath.section];
        if (indexPath.row == Addressarr.count-1) {
曹云霄's avatar
曹云霄 committed
174 175
            
            AllpriceTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"sixthcell" forIndexPath:indexPath];
勾芒's avatar
勾芒 committed
176
            cell.selectionStyle = UITableViewCellSelectionStyleNone;
勾芒's avatar
勾芒 committed
177
            cell.goodsAllprice = [self.datasArray objectAtIndex_opple:indexPath.section];
曹云霄's avatar
曹云霄 committed
178 179 180
            return cell;
        }
        CommodityListTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"fourthcell" forIndexPath:indexPath];
勾芒's avatar
勾芒 committed
181 182
        cell.selectionStyle = UITableViewCellSelectionStyleNone;
        cell.model = [self.datasArray objectAtIndex_opple:indexPath.section][indexPath.row];
曹云霄's avatar
曹云霄 committed
183 184 185 186 187 188 189 190
        return cell;
    }
    return nil;
}


- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
勾芒's avatar
勾芒 committed
191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214
//    return [[self.datasArray objectAtIndex_opple:section] count];
    NSArray *arr = [self.datasArray objectAtIndex_opple:section];
    return arr.count;
//    switch (section) {
//        case 0:
//        {
//            return 1;
//        }
//            break;
//        case 1:
//        {
//            return 2;
//        }
//            break;
//        case 2:
//        {
//            return 2;
//        }
//            break;
//            
//        default:
//            break;
//    }
//    return 0;
曹云霄's avatar
曹云霄 committed
215 216 217 218 219 220 221 222
}


- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
    switch (indexPath.section) {
        case 0:
        {
勾芒's avatar
勾芒 committed
223
            return 140;
曹云霄's avatar
曹云霄 committed
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
        }
            break;
        case 1:
        {
            return 56;
        }
            break;
        case 2:
        {
            if (indexPath.row == 1) {
                
                return 50;
            }
            return 80;
        }
            break;
            
        default:
            break;
    }
    return 0;
}

- (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section
{
    switch (section) {
        case 0:
        {
            return 0;
        }
            break;
        case 1:
        {
            return 10;
        }
            break;
        case 2:
        {
            return 44;
        }
            break;
            
        default:
            break;
    }
    return 0;
}

- (UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section
{
勾芒's avatar
勾芒 committed
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
    switch (section) {
        case 1:
        {
            UIImageView *imageView = [[UIImageView alloc]initWithFrame:CGRectMake(0, 0, ScreenWidth, 10)];
            imageView.image = TCImage(@"lineNew");
            UIView *view = [[UIView alloc]initWithFrame:CGRectMake(0, 0, ScreenWidth, 44)];
            [view addSubview:imageView];
            return view;
        }
            break;
        case 2:
        {
            UILabel *titleLabe = [[UILabel alloc]initWithFrame:CGRectMake(50, 0, 100, 44)];
            titleLabe.text = @"购物袋清单";
            UIView *view = [[UIView alloc]initWithFrame:CGRectMake(0, 0, ScreenWidth, 44)];
            [view addSubview:titleLabe];
            
            //横线
            UIView *lineView = [[UIView alloc]initWithFrame:CGRectMake(0, 43, ScreenWidth, 1)];
            lineView.backgroundColor = kTCColor(242, 242, 242);
            [view addSubview:lineView];
            return view;
        }
            break;
            
        default:
            break;
    }
    return nil;
曹云霄's avatar
曹云霄 committed
303 304 305 306 307 308
}



- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
勾芒's avatar
勾芒 committed
309
    return self.datasArray.count;
曹云霄's avatar
曹云霄 committed
310 311
}

勾芒's avatar
勾芒 committed
312

曹云霄's avatar
曹云霄 committed
313 314 315


#pragma mark -新增或者修改收货地址
勾芒's avatar
勾芒 committed
316
- (void)CreateModifyShippingView:(AddressModel *)model
曹云霄's avatar
曹云霄 committed
317 318
{
    
曹云霄's avatar
曹云霄 committed
319
    AddressViewController *address = [[AddressViewController alloc]init];
勾芒's avatar
勾芒 committed
320 321 322 323 324 325
    address.delegate = self;
    if (model != nil) {
        
        address.model = model;
        address.isChange = YES;
    }
曹云霄's avatar
曹云霄 committed
326 327 328 329 330 331
    address.preferredContentSize = CGSizeMake(315, 320);
    address.modalPresentationStyle = UIModalPresentationFormSheet;
    UIPopoverPresentationController *pop = address.popoverPresentationController;
    pop.permittedArrowDirections = UIPopoverArrowDirectionAny;
    pop.sourceView = address.view;
    [self presentViewController:address animated:YES completion:nil];
曹云霄's avatar
曹云霄 committed
332 333 334 335 336 337 338 339
}




#pragma mark -生成订单
- (IBAction)CreateOrderButtonClick:(UIButton *)sender {
    
勾芒's avatar
勾芒 committed
340
    
勾芒's avatar
勾芒 committed
341
    
勾芒's avatar
勾芒 committed
342
    OrderBill *order = [[OrderBill alloc]init];
勾芒's avatar
勾芒 committed
343 344
    
    //分页
勾芒's avatar
勾芒 committed
345 346
    DataPage *page = [[DataPage alloc]init];
    page.page = 0;
勾芒's avatar
勾芒 committed
347
    order.datapage = page;
勾芒's avatar
勾芒 committed
348
    
勾芒's avatar
勾芒 committed
349
    
勾芒's avatar
勾芒 committed
350
    //收货地址
勾芒's avatar
勾芒 committed
351
    TOOrderEntity *orderReceiver = [[TOOrderEntity alloc]init];
勾芒's avatar
勾芒 committed
352 353 354 355 356 357 358 359
    NSArray *Adressarr = [self.datasArray objectAtIndex_opple:1];
    for (int i=0; i<Adressarr.count; i++) {
        
        if ([[Adressarr objectAtIndex_opple:i] isKindOfClass:[AddressModel class]]) {
            
            AddressModel *model = [Adressarr objectAtIndex_opple:i];
            if (model.isSelected) {
                
勾芒's avatar
勾芒 committed
360
                //收货人信息
勾芒's avatar
勾芒 committed
361 362 363 364
                orderReceiver.receiverName = model.name;
                orderReceiver.receiverMobile = model.miblephone;
                orderReceiver.receiverAddress = model.address;
                
勾芒's avatar
勾芒 committed
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
                order.order = orderReceiver;
                orderReceiver.guideId = [[Shoppersmanager manager] Shoppers].employee.fid;
                orderReceiver.consumerId = [[Customermanager manager] customerID];
                //商品ID
                NSArray *goodArray = [self.datasArray lastObject];
                NSMutableArray *goodidArr = [NSMutableArray array];
                for (int i=0; i<goodArray.count-1; i++) {
                    ShopcarModel *model = [goodArray objectAtIndex_opple:i];
                    TOOrderdetailEntity *orderGoods = [[TOOrderdetailEntity alloc]init];
                    orderGoods.goodsId = model.fid;
                    orderGoods.goodsCover = model.goods.pictures;
                    orderGoods.goodsCode = model.goods.code;
                    orderGoods.goodsSpec = model.goods.spec;
                    orderGoods.goodsName = model.goods.name;
                    orderGoods.goodsBrand = model.goods.brandId;
                    orderGoods.goodsNum = model.goods.number;
                    orderGoods.goodsPrice = model.goods.costPrice;
//                    orderGoods.goodsUnit = model.goods.unit;
                    
//                    @synthesize goodsId;
//                    @synthesize goodsCode;
//                    @synthesize goodsName;
//                    @synthesize goodsCover;
//                    @synthesize goodsSpec;
//                    @synthesize goodsBrand;
//                    @synthesize goodsNum;
//                    @synthesize goodsPrice;
//                    @synthesize goodsTotalPrice;
                    [goodidArr addObject:orderGoods];
                }
                
                order.orderdetailList = goodidArr;
                [self CreateMBProgressHUDLoding];
                [[NetworkRequestClassManager Manager] NetworkRequestWithURL:[NSString stringWithFormat:@"%@%@",ServerAddress,@"/order/save"] WithRequestType:0 WithParameter:order WithReturnValueBlock:^(id returnValue) {
                    [self RemoveMBProgressHUDLoding];
                    if ([returnValue[@"code"] isEqualToNumber:@0]) {
                        OrderdetailsViewController *orderDetails = [[self getStoryboardWithName] instantiateViewControllerWithIdentifier:@"orderdetails"];
                        orderDetails.orderCode = returnValue[@"data"];
                        orderDetails.sectionTitle = @[@"订单信息",@"客户信息",@"收货信息",@"商品清单"];
                        orderDetails.isShowattachment = NO;
                        [self.navigationController pushViewController:orderDetails animated:YES];
                    }else
                    {
                        [self ErrorMBProgressView:returnValue[@"message"]];
                    }
                } WithErrorCodeBlock:^(id errorCodeValue) {
                    
                } WithFailureBlock:^(id error) {
                    [self RemoveMBProgressHUDLoding];
                }];
勾芒's avatar
勾芒 committed
415 416
                return;
            }
勾芒's avatar
勾芒 committed
417 418 419 420
            else if (i == Adressarr.count -1)
            {
               [self ErrorMBProgressView:@"未选中地址"];
            }
勾芒's avatar
勾芒 committed
421 422
        }
    }
曹云霄's avatar
曹云霄 committed
423 424 425 426 427 428 429 430 431 432 433
}


#pragma mark -返回购物车

- (IBAction)BackShoppingBagsButtonClick:(UIButton *)sender {
    
    [self.navigationController popViewControllerAnimated:YES];
}


勾芒's avatar
勾芒 committed
434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457
#pragma mark -设置为当前地址回调
- (void)SetCustomerButtonClick:(NSInteger)index;
{
    
    for (UITableViewCell *cell in self.generateOrderTableview.visibleCells) {
        
        if ([cell isKindOfClass:[GenerateOrdersTableViewCell class]]) {
            
           GenerateOrdersTableViewCell *Newcell = (GenerateOrdersTableViewCell *)cell;
           Newcell.isSelectedButton.selected = NO;
        }
    }
    for (id objc in [self.datasArray objectAtIndex_opple:1]) {
        
        if ([objc isKindOfClass:[AddressModel class]]) {
            
            AddressModel *model = (AddressModel *)objc;
            model.isSelected = NO;
        }
    }
    AddressModel *model = [[self.datasArray objectAtIndex_opple:1] objectAtIndex_opple:index];
    model.isSelected = YES;
}

曹云霄's avatar
曹云霄 committed
458 459


勾芒's avatar
勾芒 committed
460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476
#pragma mark -修改地址
- (void)ChangeButtonClick
{
    NSArray *arr = [self.datasArray objectAtIndex_opple:1];
    for (int i=0; i<arr.count; i++) {
        
        if ([[arr objectAtIndex_opple:i] isKindOfClass:[AddressModel class]]) {
            
            AddressModel *model = [arr objectAtIndex_opple:i];
            //判断是否为选中
            if (model.isSelected) {
                
                [self CreateModifyShippingView:[arr objectAtIndex_opple:i]];
            }
        }
    }
}
曹云霄's avatar
曹云霄 committed
477 478


勾芒's avatar
勾芒 committed
479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514
#pragma mark -新增地址
- (void)AddAddressButtonClick
{
    [self CreateModifyShippingView:nil];
}


#pragma mark -新增地址回调
- (void)addAddressCell:(AddressModel *)model
{
    [self dismissViewControllerAnimated:YES completion:nil];
    [[self.datasArray objectAtIndex_opple:1]insertObject:model atIndex:0];
    NSIndexPath *indexpath = [NSIndexPath indexPathForRow:0 inSection:1];
    [self.generateOrderTableview insertRowsAtIndexPaths:@[indexpath] withRowAnimation:UITableViewRowAnimationLeft];
}

#pragma mark -删除地址
- (void)delecteCell:(NSString *)addressid
{
    NSArray *arr = [self.datasArray objectAtIndex_opple:1];
    for (int i=0; i<arr.count; i++) {
        
        if ([[arr objectAtIndex_opple:i] isKindOfClass:[AddressModel class]]) {
            
            AddressModel *model = [arr objectAtIndex_opple:i];
            //判断是否为选中
            if ([model.fid isEqualToString:addressid]) {
                
                [[self.datasArray objectAtIndex_opple:1] removeObjectAtIndex:i];
                NSIndexPath *indexpath = [NSIndexPath indexPathForRow:i inSection:1];
                [self.generateOrderTableview deleteRowsAtIndexPaths:@[indexpath] withRowAnimation:UITableViewRowAnimationLeft];
                [self dismissViewControllerAnimated:YES completion:nil];
            }
        }
    }
}
曹云霄's avatar
曹云霄 committed
515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531

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