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

#import "ClientViewController.h"
#import "InformationTableViewCell.h"
#import "ClientdetailsViewController.h"
#import "MyclientEntityModel.h"
#import "DeviceDirectionManager.h"
#import "PerfectInformationViewController.h"
15
#import "YXPickerManager.h"
16 17
#import "InviterViewController.h"
#import "InviterNavigationViewController.h"
曹云霄's avatar
曹云霄 committed
18

19
@interface ClientViewController ()<UITableViewDelegate,UITableViewDataSource,UITextFieldDelegate,UIImagePickerControllerDelegate,UINavigationControllerDelegate>
曹云霄's avatar
曹云霄 committed
20 21 22 23

/**
 *  客户数据源
 */
24
@property (nonatomic,strong) NSMutableArray *customerResultArray;
曹云霄's avatar
曹云霄 committed
25 26 27 28 29 30

/**
 *  查询条件
 */
@property (nonatomic,strong) ConsumerQueryCondition *condition;

31 32 33 34
/**
 邀请人entity
 */
@property (nonatomic,strong) VDesignerEntity *inviterEntity;
曹云霄's avatar
曹云霄 committed
35 36 37 38 39 40 41 42 43

@end

@implementation ClientViewController


/**
 *  初始化客户数据源
 */
44
- (NSMutableArray *)customerResultArray
曹云霄's avatar
曹云霄 committed
45
{
46
    if (_customerResultArray == nil) {
曹云霄's avatar
曹云霄 committed
47
        
48
        _customerResultArray = [NSMutableArray array];
曹云霄's avatar
曹云霄 committed
49
    }
50
    return _customerResultArray;
曹云霄's avatar
曹云霄 committed
51 52 53 54 55 56 57 58 59 60 61
}

- (ConsumerQueryCondition *)condition
{
    if (!_condition) {
        _condition = [[ConsumerQueryCondition alloc]init];
        _condition.resellerCodeEquals = [Shoppersmanager manager].shoppers.employee.currentDepart.orgCode;
        DataPage *page = [[DataPage alloc]init];
        page.sort = @"createDate";
        page.order = @"desc";
        page.rows = KROWS;
62
        _condition.page = page;
曹云霄's avatar
曹云霄 committed
63 64 65 66 67 68 69 70
    }
    return _condition;
}

- (void)viewDidLoad {
    [super viewDidLoad];
    
    [self uiConfigAction];
71 72
    [self addObserver];
    [self setupDefaultStoreAddress];
曹云霄's avatar
曹云霄 committed
73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92
}

- (void)viewDidAppear:(BOOL)animated
{
    [super viewDidAppear:animated];
    self.navigationController.fd_fullscreenPopGestureRecognizer.enabled = NO;
    if ([self.navigationController respondsToSelector:@selector(interactivePopGestureRecognizer)]) {
        self.navigationController.interactivePopGestureRecognizer.enabled = NO;
    }
}

- (void)viewWillDisappear:(BOOL)animated
{
    [super viewWillDisappear:animated];
    self.navigationController.fd_fullscreenPopGestureRecognizer.enabled = YES;
    if ([self.navigationController respondsToSelector:@selector(interactivePopGestureRecognizer)]) {
        self.navigationController.interactivePopGestureRecognizer.enabled = YES;
    }
}

93
#pragma mark -UI
曹云霄's avatar
曹云霄 committed
94
- (void)uiConfigAction
95 96 97 98 99 100
{
    [self.addpersonInformationButton horizontalCenterImageAndTitle:5];
}

#pragma mark -设置默认门店地址
- (void)setupDefaultStoreAddress
曹云霄's avatar
曹云霄 committed
101 102 103 104 105 106
{
    ToStoreAddress *storeAddress = [Shoppersmanager manager].storeAddress;
    [self.locationButton setTitle:[NSString stringWithFormat:@"%@-%@-%@",storeAddress.province,storeAddress.city,storeAddress.country] forState:UIControlStateNormal];
}

#pragma mark -添加监听
107
- (void)addObserver
曹云霄's avatar
曹云霄 committed
108 109 110 111
{
    //用户头像
    [self.customerHeader addGestureRecognizer:[[UITapGestureRecognizer alloc]initWithTarget:self action:@selector(customerHeaderClckAction:)]];
    //设置当前客户通知
112
    [Notification addObserver:self selector:@selector(setupCurrentCustomer:) name:SETUPCURRENTCUSTOMER object:nil];
曹云霄's avatar
曹云霄 committed
113
    //清空当前客户数据
114
    [Notification addObserver:self selector:@selector(exitCurrentCustomer) name:EMPTYCUSTOMERNAME object:nil];
曹云霄's avatar
曹云霄 committed
115 116 117 118 119
}


#pragma mark -省市区选择
- (IBAction)addressChooseButtonClickAction:(UIButton *)sender {
120
    [self.view endEditing:YES];
121 122 123 124 125 126 127 128 129
    NSString *address = nil;
    ToStoreAddress *storeAddress = [Shoppersmanager manager].storeAddress;
    if (storeAddress) {
        address = [NSString stringWithFormat:@"%@-%@-%@",storeAddress.province,storeAddress.city,storeAddress.country];
    }
    if (![BaseViewController isBlankString:sender.currentTitle]) {
        address = sender.currentTitle;
    }
    [[YXPickerManager shareManager] showAddressPickerView:kMainBlueColor defaultAddress:address commitBlock:^(NSString *address, NSString *zipcode) {
曹云霄's avatar
曹云霄 committed
130 131 132 133 134 135 136
        [sender setTitle:address forState:UIControlStateNormal];
    } cancelBlock:^{
        
    }];
}


137 138
#pragma mark -邀请人选择
- (IBAction)inviterButtonClickAction:(UIButton *)sender {
139 140 141 142 143 144 145 146 147 148 149 150 151 152 153
    [self.view endEditing:YES];
    InviterNavigationViewController *chooseNav = [InviterNavigationViewController viewControllerWithStoryBoardType:STORYBOARD_TYPE_MAIN];
    InviterViewController *inviterVc = (InviterViewController *)chooseNav.visibleViewController;
    WS(weakSelf);
    [inviterVc setSelectedBlock:^(VDesignerEntity *entity) {
        weakSelf.inviterEntity = entity;
        [weakSelf.inviterButton setTitle:entity.userName forState:UIControlStateNormal];
        [weakSelf addCustomerInformation];
    }];
    chooseNav.preferredContentSize = CGSizeMake(ScreenHeight-300, ScreenHeight-300);
    chooseNav.modalPresentationStyle = UIModalPresentationFormSheet;
    UIPopoverPresentationController *pop = chooseNav.popoverPresentationController;
    pop.permittedArrowDirections = UIPopoverArrowDirectionAny;
    pop.sourceView = chooseNav.view;
    [self presentViewController:chooseNav animated:YES completion:nil];
曹云霄's avatar
曹云霄 committed
154 155 156
}


157 158
#pragma mark -下拉、上拉 加载数据
- (void)loadWebDataSource
曹云霄's avatar
曹云霄 committed
159 160
{
    WS(weakSelf);
161 162 163 164
    if (self.pullPageIndex == ONE) {
        [self.customerResultArray removeAllObjects];
    }
    self.condition.page.page = self.pullPageIndex;
165
    [self getshoppersAssociatedCustomer:self.condition completed:^(ConsumerDesignerPageResult *result) {
166 167 168 169
        if (weakSelf.pullPageIndex >= result.totalpages) {
            [weakSelf endRefresh:EndRefreshNotData];
        }else {
            [weakSelf endRefresh:EndRefreshDefault];
曹云霄's avatar
曹云霄 committed
170 171 172 173 174
        }
    }];
}

#pragma mark -获取导购关联客户
175
- (void)getshoppersAssociatedCustomer:(ConsumerQueryCondition *)condition completed:(void(^)(ConsumerDesignerPageResult *result))completed
曹云霄's avatar
曹云霄 committed
176 177 178 179
{
    WS(weakSelf);
    [HTTP networkRequestWithURL:SERVERREQUESTURL(GETshoppersCONSUMER)  withRequestType:ZERO withParameter:condition withReturnValueBlock:^(id returnValue) {
        if (RESULT(returnValue)) {
180
            ConsumerDesignerPageResult *customerResult = [[ConsumerDesignerPageResult alloc]initWithDictionary:RESPONSE(returnValue) error:nil];
181 182
            completed(customerResult);
            for (TOConsumerEntity *objc in customerResult.results) {
曹云霄's avatar
曹云霄 committed
183
                 MyclientEntityModel *myclientModel = [[MyclientEntityModel alloc]initWithDictionary:[objc toDictionary] error:nil];
184
                [weakSelf.customerResultArray addObject:myclientModel];
曹云霄's avatar
曹云霄 committed
185 186
            }
            //刷新后添加选中状态
187 188
            for (int i=0; i<weakSelf.customerResultArray.count; i++) {
                MyclientEntityModel *myclientModel = [weakSelf.customerResultArray objectAtIndex_opple:i];
189
                if ([[Customermanager manager].model.consumer.fid isEqualToString:myclientModel.consumer.fid]) {
190
                    InformationTableViewCell *cell = [weakSelf.tableView cellForRowAtIndexPath:[NSIndexPath indexPathForRow:i inSection:0]];
曹云霄's avatar
曹云霄 committed
191 192 193 194 195 196 197
                    myclientModel.selectedState = YES;
                    cell.setCurrentCustomer.selected = YES;
                }
            }
        }
        else
        {
198
            [weakSelf endRefresh:EndRefreshDefault];
曹云霄's avatar
曹云霄 committed
199 200
            [XBLoadingView showHUDViewWithText:MESSAGE(returnValue)];
        }
201 202
        [weakSelf.tableView reloadData];

曹云霄's avatar
曹云霄 committed
203
    }withFailureBlock:^(NSError *error) {
204
        [weakSelf endRefresh:EndRefreshDefault];
曹云霄's avatar
曹云霄 committed
205 206 207 208 209 210 211 212 213 214
        [XBLoadingView showHUDViewWithText:error.localizedDescription];
    }];
}


- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    InformationTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"information" forIndexPath:indexPath];
    cell.selectionStyle = UITableViewCellSelectionStyleNone;
    cell.indexNumber = indexPath.row;
215
    cell.model = [self.customerResultArray objectAtIndex_opple:indexPath.row];
曹云霄's avatar
曹云霄 committed
216 217 218 219 220 221 222 223 224 225
    //清空其他选中状态
    WS(weakSelf);
    [cell setBlockSeletced:^(NSInteger index) {
        [weakSelf setCustomerButtonClick:index];
    }];
    return cell;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
226
    return self.customerResultArray.count;
曹云霄's avatar
曹云霄 committed
227 228 229 230 231 232 233 234 235
}

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

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
236
    ClientdetailsViewController *clientdetails = [ClientdetailsViewController viewControllerWithStoryBoardType:STORYBOARD_TYPE_MAIN];
237
    clientdetails.model = [self.customerResultArray objectAtIndex_opple:indexPath.row];
曹云霄's avatar
曹云霄 committed
238 239 240 241 242
    clientdetails.cellindex = indexPath.row;
    //设置当前客户
    WS(weakSelf);
    [clientdetails setCurrentUserBlock:^(NSInteger index,NSString *title) {
        if ([title isEqualToString:@"设为当前客户"]) {
243
            InformationTableViewCell *cell = [weakSelf.tableView cellForRowAtIndexPath:[NSIndexPath indexPathForRow:index inSection:0]];
曹云霄's avatar
曹云霄 committed
244 245 246 247 248 249 250 251
            [weakSelf setCustomerButtonClick:index];
            cell.setCurrentCustomer.selected = YES;
        }else if ([title isEqualToString:@"退出当前客户"])
        {
            [weakSelf exitCurrentCustomer];
            [weakSelf.addpersonInformationButton setTitle:@"添加客户信息" forState:UIControlStateNormal];
        }
    }];
252
    [self pushViewController:clientdetails animated:YES];
曹云霄's avatar
曹云霄 committed
253 254 255 256 257 258 259
}


#pragma mark -所有客户界面 设置当前客户通知回调
- (void)setupCurrentCustomer:(NSNotification *)not
{
    NSDictionary *dict = not.object;
260
    for (InformationTableViewCell *cell in self.tableView.visibleCells) {
曹云霄's avatar
曹云霄 committed
261 262
        cell.setCurrentCustomer.selected = NO;
    }
263
    for (MyclientEntityModel *model in self.customerResultArray) {
264
        if ([model.consumer.fid isEqualToString:dict[@"customerid"]]) {
曹云霄's avatar
曹云霄 committed
265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280
            model.selectedState = YES;
        }else
        {
           model.selectedState = NO;
        }
    }
    if ([dict[@"title"] isEqualToString:@"设为当前客户"]) {
        MyclientEntityModel *model = dict[@"model"];
        [Shoppersmanager manager].currentCustomer = YES;
        [Customermanager manager].model = model;
        [self setUpCurrentCustomerAssignment:model];
        [self.addpersonInformationButton setTitle:@"退出当前客户" forState:UIControlStateNormal];
    }else if ([dict[@"title"] isEqualToString:@"退出当前客户"])
    {
        [self exitCurrentCustomer];
    }
281
    [self changeCustomerName];
曹云霄's avatar
曹云霄 committed
282 283 284 285 286
}

#pragma mark -设置为当前客户回调2
- (void)setCustomerButtonClick:(NSInteger)index;
{
287
    for (InformationTableViewCell *cell in self.tableView.visibleCells) {
曹云霄's avatar
曹云霄 committed
288 289
        cell.setCurrentCustomer.selected = NO;
    }
290
    for (MyclientEntityModel *model in self.customerResultArray) {
曹云霄's avatar
曹云霄 committed
291 292
        model.selectedState = NO;
    }
293
    MyclientEntityModel *model = [self.customerResultArray objectAtIndex_opple:index];
曹云霄's avatar
曹云霄 committed
294
    //判断省市区是否为空
295
    if ([[self class] isBlankString:model.consumer.country]) {
曹云霄's avatar
曹云霄 committed
296
        WS(weakSelf);
297 298 299
        ToStoreAddress *storeAddress = [Shoppersmanager manager].storeAddress;
        NSString *address = [NSString stringWithFormat:@"%@-%@-%@",storeAddress.province,storeAddress.city,storeAddress.country];
        [[YXPickerManager shareManager] showAddressPickerView:kMainBlueColor defaultAddress:address commitBlock:^(NSString *address, NSString *zipcode) {
曹云霄's avatar
曹云霄 committed
300 301
            //省市区
            NSArray *addressArray = [address componentsSeparatedByString:@"-"];
302 303 304
            model.consumer.province = addressArray[0];
            model.consumer.city = addressArray[1];
            model.consumer.country = addressArray[2];
曹云霄's avatar
曹云霄 committed
305 306 307 308 309
            model.selectedState = YES;
            //保存客户信息
            [Shoppersmanager manager].currentCustomer = YES;
            [Customermanager manager].model = model;
            [self setUpCurrentCustomerAssignment:model];
310
            [weakSelf addCustomerInformation];
曹云霄's avatar
曹云霄 committed
311 312
            [weakSelf.addpersonInformationButton setTitle:@"退出当前客户" forState:UIControlStateNormal];
        } cancelBlock:nil];
313
        
曹云霄's avatar
曹云霄 committed
314 315 316 317 318 319 320 321 322
    }else {
        model.selectedState = YES;
        //保存客户信息
        [Shoppersmanager manager].currentCustomer = YES;
        [Customermanager manager].model = model;
        [self setUpCurrentCustomerAssignment:model];
        [self.addpersonInformationButton setTitle:@"退出当前客户" forState:UIControlStateNormal];
    }
    [self changeCustomerName];
323
    [self.tableView reloadData];
曹云霄's avatar
曹云霄 committed
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
}


#pragma mark -更改客户头像
- (void)customerHeaderClckAction:(UITapGestureRecognizer *)tap
{
    if (![Shoppersmanager manager].currentCustomer) {
        [XBLoadingView showHUDViewWithText:@"请先设置当前客户"];
        return;
    }
    if([BaseViewController determineCameraPermissions]){
        UIImagePickerController *PcCamera = [[UIImagePickerController alloc]init];
        PcCamera.delegate = self;
        [DeviceDirectionManager instance].isHorizontal=YES;
        WS(weakSelf);
        ShowAlertView(@"请选择", nil, @[@"拍照",@"从相册选择",@"取消"], UIAlertControllerStyleAlert, ^(NSInteger index) {
            if (index == ZERO) {
                //拍照
                if ([UIImagePickerController isSourceTypeAvailable:UIImagePickerControllerSourceTypeCamera]) {
                    [PcCamera setSourceType:UIImagePickerControllerSourceTypeCamera];
                    PcCamera.allowsEditing = YES;
                    dispatch_async(dispatch_get_main_queue(), ^{
                        [weakSelf presentViewController:PcCamera animated:YES completion:nil];
                    });
                }
                else
                {
                    [DeviceDirectionManager instance].isHorizontal=NO;
                    [XBLoadingView showHUDViewWithText:@"相机无法使用"];
                }
            }else if (index == ONE) {
                //从相册中选择
                if ([UIImagePickerController isSourceTypeAvailable:UIImagePickerControllerSourceTypePhotoLibrary]) {
                    [PcCamera setSourceType:UIImagePickerControllerSourceTypePhotoLibrary];
                    PcCamera.allowsEditing = YES;
                    dispatch_async(dispatch_get_main_queue(), ^{
                        [weakSelf presentViewController:PcCamera animated:YES completion:nil];
                    });
                }
                else
                {
                    [DeviceDirectionManager instance].isHorizontal=NO;
                    [XBLoadingView showHUDViewWithText:@"相册无法打开"];
                }
            }else {
                [DeviceDirectionManager instance].isHorizontal=NO;
            }
        });
    }
}

#pragma mark -拍照、从相册选择
#pragma -mark -UIImagePickerControllerDelegate
- (void)imagePickerController:(UIImagePickerController *)picker didFinishPickingMediaWithInfo:(NSDictionary<NSString *,id> *)info
{
    [DeviceDirectionManager instance].isHorizontal=NO;
    [self dismissViewControllerAnimated:YES completion:nil];
    UIImage *Headimage = [info objectForKey:UIImagePickerControllerOriginalImage];
    __weak typeof(self) weskSelf = self;
    //修改并保存头像
    [self uploadUserHeader:Headimage completeBlock:^(NSString *string) {
        weskSelf.customerHeader.image = Headimage;
386
        [Customermanager manager].model.consumer.picture = string;
387 388
        for (int i=0;i<weskSelf.customerResultArray.count;i++) {
            MyclientEntityModel *customerModel = (MyclientEntityModel *)[weskSelf.customerResultArray objectAtIndex_opple:i];
389 390
            if ([customerModel.consumer.fid isEqualToString:[Customermanager manager].model.consumer.fid]) {
                customerModel.consumer.picture = string;
曹云霄's avatar
曹云霄 committed
391
                NSIndexPath *indexPath = [NSIndexPath indexPathForRow:i inSection:0];
392
                [weskSelf.tableView reloadRowsAtIndexPaths:@[indexPath] withRowAnimation:UITableViewRowAnimationBottom];
曹云霄's avatar
曹云霄 committed
393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410
                break;
            }
        }
    }];
}

#pragma mark -保存选中头像、并上传
- (void)uploadUserHeader:(UIImage *)image completeBlock:(void(^)(NSString *string))complete
{
    WS(weakSelf);
    NSData *data = UIImageJPEGRepresentation(image, 0.5);
    XBLoadingView *progressView = [XBLoadingView showHUDViewProgressLabel:@"上传头像中"];
    [HTTP uploadImageWithURL:SERVERREQUESTURL(UPLOADHEADER) withRequestType:ONE withImageDatas:data withParameter:nil withReturnValueBlock:^(id returnValue) {
        
        if (RESULT(returnValue)) {
            [weakSelf modifyshoppersInformation:RESPONSE(returnValue) complete:^{
                complete(RESPONSE(returnValue));
            }];
411
        }else{
曹云霄's avatar
曹云霄 committed
412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435
            [XBLoadingView showHUDViewWithText:MESSAGE(returnValue)];
        }
    } withprogressBlock:^(double progress) {
        
        if (progress >= 1) {
            dispatch_async(dispatch_get_main_queue(), ^{
                progressView.labelText = @"上传完成";
                [progressView hide:YES afterDelay:1];
            });
        }else{
            progressView.progress = progress;
        }
    } withFailureBlock:^(NSError *error) {
        [XBLoadingView showHUDViewWithText:error.localizedDescription];
    }];
}


#pragma mark -上传完成修改导购个人信息
- (void)modifyshoppersInformation:(NSString *)headerurl complete:(void(^)())block
{
    [XBLoadingView showHUDViewWithDefault];
    MyclientEntityModel *customerEntity = nil;
    customerEntity = [Customermanager manager].model;
436
    customerEntity.consumer.picture = headerurl;
曹云霄's avatar
曹云霄 committed
437 438 439 440 441 442
    [HTTP networkRequestWithURL:SERVERREQUESTURL(ADDCONSUMER)  withRequestType:ZERO withParameter:customerEntity withReturnValueBlock:^(id returnValue) {
        [XBLoadingView hideHUDViewWithDefault];
        if (RESULT(returnValue)) {
            [XBLoadingView showHUDViewWithSuccessText:@"修改成功" completeBlock:^{
                block();
            }];
443
        }else{
曹云霄's avatar
曹云霄 committed
444 445 446 447 448 449 450 451 452 453 454
            [XBLoadingView showHUDViewWithText:MESSAGE(returnValue)];
        }
    }withFailureBlock:^(NSError *error) {
        [XBLoadingView showHUDViewWithText:error.localizedDescription];
    }];
}

#pragma mark -更新用户名字
- (void)changeCustomerName
{
    MyclientEntityModel *model = [Customermanager manager].model;
455
    [Notification postNotificationName:CHANGECUSTOMERNAME object:model.consumer.name];
曹云霄's avatar
曹云霄 committed
456 457 458 459 460 461 462 463 464 465 466 467
}

#pragma mark -设置为当前客户、查询购物车数量
- (void)setupUserShoppingCarNumberRequest
{
    [self queryShoppingCarNumber];
}

#pragma mark -设置为当前用户请求、写入访问时间
- (void)setupUserRequest
{
    MyclientEntityModel *model = [Customermanager manager].model;
468
    [HTTP networkWithDictionaryRequestWithURL:[NSString stringWithFormat:@"%@%@",SERVERREQUESTURL(SAVEVISITEDTIME),model.consumer.fid]  withRequestType:ONE withParameter:nil withReturnValueBlock:^(id returnValue) {
曹云霄's avatar
曹云霄 committed
469 470 471 472 473 474 475 476 477 478
        if (RESULT(returnValue)) {
            NSLog(@"写入客户访问时间成功");
        }
    } withFailureBlock:^(id error) {
        NSLog(@"写入客户访问时间失败");
    }];
}


#pragma mark -添加客户信息、或者退出当前客户
479
- (IBAction)addCustomerInformationOrExit:(UIButton *)sender {
曹云霄's avatar
曹云霄 committed
480 481 482 483 484
    if ([sender.currentTitle isEqualToString:@"退出当前客户"]) {
        [self exitCurrentCustomer];
        [sender setTitle:@"添加客户信息" forState:UIControlStateNormal];
        return;
    }
485
    [self addCustomerInformation];
曹云霄's avatar
曹云霄 committed
486 487 488 489 490 491 492 493 494
}

#pragma mark -退出当前客户
- (void)exitCurrentCustomer
{
    self.customerNameField.text = nil;
    self.customerAddress.text = nil;
    self.phoneNumberField.text = nil;
    self.companyNameField.text = nil;
495
    [self.inviterButton setTitle:@"" forState:UIControlStateNormal];
496
    [self setupDefaultStoreAddress];
曹云霄's avatar
曹云霄 committed
497 498
    [Shoppersmanager manager].currentCustomer = NO;
    [Customermanager manager].model = nil;
499
    self.customerHeader.image = TCImage(@"customer");
曹云霄's avatar
曹云霄 committed
500 501
    [self changeCustomerName];
    [self queryShoppingCarNumber];
502
    for (InformationTableViewCell *cell in self.tableView.visibleCells) {
曹云霄's avatar
曹云霄 committed
503 504
        cell.setCurrentCustomer.selected = NO;
    }
505
    for (MyclientEntityModel *model in self.customerResultArray) {
曹云霄's avatar
曹云霄 committed
506 507 508 509
        model.selectedState = NO;
    }
}

510 511
#pragma mark -添加客户信息请求
- (void)addCustomerInformation
曹云霄's avatar
曹云霄 committed
512 513 514 515 516 517 518 519 520 521
{
    [self.view endEditing:YES];
    if ([BaseViewController isBlankString:self.customerNameField.text]) {
        [XBLoadingView showHUDViewWithText:@"客户名不能为空"];
        return;
    }
    if ([BaseViewController isBlankString:self.phoneNumberField.text]) {
        [XBLoadingView showHUDViewWithText:@"手机号码不能为空"];
        return;
    }
曹云霄's avatar
曹云霄 committed
522 523 524 525 526
    NSString *phoneNumber = self.phoneNumberField.text;
    if (![phoneNumber isTelephone]) {
        [XBLoadingView showHUDViewWithText:@"手机号码格式不正确"];
        return;
    }
曹云霄's avatar
曹云霄 committed
527 528 529 530 531 532 533 534 535 536
    if ([BaseViewController isBlankString:self.customerAddress.text]) {
        [XBLoadingView showHUDViewWithText:@"地址不能为空"];
        return;
    }
    if ([BaseViewController isBlankString:[self.locationButton currentTitle]]) {
        [XBLoadingView showHUDViewWithText:@"省市区不能为空"];
        return;
    }
    //客户信息类
    MyclientEntityModel *customerEntity = [[MyclientEntityModel alloc]init];
537 538 539 540 541 542 543
    TOConsumerEntity *consumer = [[TOConsumerEntity alloc] init];
    consumer.createName = [Shoppersmanager manager].shoppers.employee.realName;
    consumer.mobile = self.phoneNumberField.text;
    consumer.address = self.customerAddress.text;
    consumer.name = self.customerNameField.text;
    consumer.company = self.companyNameField.text;
    consumer.picture = [Customermanager manager].model.consumer.picture;
曹云霄's avatar
曹云霄 committed
544 545
    //省市区
    NSArray *addressArray = [[self.locationButton currentTitle] componentsSeparatedByString:@"-"];
546 547 548 549 550 551 552 553
    consumer.province = addressArray[0];
    consumer.city = addressArray[1];
    consumer.country = addressArray[2];
    customerEntity.consumer = consumer;
    //修改/还是新增
    if ([Customermanager manager].model) {
       consumer.fid = [Customermanager manager].model.consumer.fid;
    }
曹云霄's avatar
曹云霄 committed
554 555
    WS(weakSelf);
    [XBLoadingView showHUDViewWithDefault];
556 557 558 559 560 561 562 563 564
    NSString *inviterId = @"";
    if (self.inviterEntity) {
        inviterId = self.inviterEntity.fid;
        TODesignerCustomerEntity *entity = [[TODesignerCustomerEntity alloc] init];
        entity.designerCode = self.inviterEntity.userName;
        customerEntity.designerCustomer = entity;
    }
    NSString *urlsString = [NSString stringWithFormat:SERVERREQUESTURL(ADDCONSUMER),inviterId];
    [HTTP networkRequestWithURL:urlsString withRequestType:ZERO withParameter:consumer withReturnValueBlock:^(id returnValue) {
曹云霄's avatar
曹云霄 committed
565 566 567
        
        [XBLoadingView hideHUDViewWithDefault];
        if (RESULT(returnValue)) {
568
            [weakSelf.tableView.mj_header beginRefreshing];
曹云霄's avatar
曹云霄 committed
569
            //新增保存ID
570 571
            if (![Customermanager manager].model) {
                customerEntity.consumer.fid = RESPONSE(returnValue);
曹云霄's avatar
曹云霄 committed
572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599
                [Customermanager manager].model = customerEntity;
                [XBLoadingView showHUDViewWithSuccessText:@"新增成功" completeBlock:nil];
                [weakSelf addAddressInformationRequest];
            }else
            {
                [XBLoadingView showHUDViewWithSuccessText:@"修改成功" completeBlock:nil];
            }
            //保存客户信息
            [Customermanager manager].model = customerEntity;
            [Shoppersmanager manager].currentCustomer = YES;
            [weakSelf.addpersonInformationButton setTitle:@"退出当前客户" forState:UIControlStateNormal];
            [weakSelf setUpCurrentCustomerAssignment:customerEntity];
        }
        else
        {
            [XBLoadingView showHUDViewWithText:MESSAGE(returnValue)];
        }
    }withFailureBlock:^(id error) {
        [XBLoadingView hideHUDViewWithDefault];
    }];
}


#pragma mark -新增客户成功写入默认地址信息
- (void)addAddressInformationRequest
{
    TOShippingAddrEntity *address = [[TOShippingAddrEntity alloc]init];
    MyclientEntityModel *model = [Customermanager manager].model;
600
    address.consumerId = model.consumer.fid;
曹云霄's avatar
曹云霄 committed
601 602 603 604 605 606 607 608 609 610 611 612
    address.name = self.customerNameField.text;
    address.miblephone = self.phoneNumberField.text;
    address.address = self.customerAddress.text;
    //省市区
    NSArray *addressArray = [[self.locationButton currentTitle] componentsSeparatedByString:@"-"];
    address.province = addressArray[0];
    address.city = addressArray[1];
    address.country = addressArray[2];
    [HTTP networkRequestWithURL:SERVERREQUESTURL(ADDADDRESS)  withRequestType:ZERO withParameter:address withReturnValueBlock:^(id returnValue) {
        if (RESULT(returnValue)) {
            NSLog(@"写入默认收货地址成功");
        }
613
        else{
曹云霄's avatar
曹云霄 committed
614 615 616 617 618 619 620
            [XBLoadingView showHUDViewWithText:MESSAGE(returnValue)];
        }
    }withFailureBlock:^(id error) {
        NSLog(@"写入默认收货地址失败");
    }];
}

621
#pragma mark -UITextFieldDelegate代理 ----搜索、完成
曹云霄's avatar
曹云霄 committed
622 623 624
- (BOOL)textFieldShouldReturn:(UITextField *)textField
{
    [textField resignFirstResponder];
625
    if (textField.returnKeyType == UIReturnKeyDone) {
626
        [self addCustomerInformation];
627 628
        return YES;
    }
曹云霄's avatar
曹云霄 committed
629 630 631 632 633 634 635 636 637 638 639
    [self searchCustomerInforMation];
    return YES;
}


#pragma amrk -搜索客户信息
- (void)searchCustomerInforMation
{
    if ([BaseViewController isBlankString:self.searchPersonInformationField.text]) {
        self.condition.nameEquals = nil;
        self.condition.mobileEquals = nil;
640
        self.condition.resellerCodeEquals = [Shoppersmanager manager].shoppers.employee.currentDepart.orgCode;
曹云霄's avatar
曹云霄 committed
641
    }else {
642
        self.condition.resellerCodeEquals = nil;
曹云霄's avatar
曹云霄 committed
643 644
        self.condition.nameEquals = self.searchPersonInformationField.text;
        self.condition.mobileEquals = self.searchPersonInformationField.text;
645
        self.condition.resellerCodeEquals = nil;
曹云霄's avatar
曹云霄 committed
646
    }
647
    [self.tableView.mj_header beginRefreshing];
曹云霄's avatar
曹云霄 committed
648 649 650 651 652 653
}


#pragma mark -设置为当前客户赋值
- (void)setUpCurrentCustomerAssignment:(MyclientEntityModel *)model
{
654 655 656 657 658 659 660
    self.customerAddress.text = model.consumer.address;
    self.phoneNumberField.text = model.consumer.mobile;
    self.companyNameField.text = model.consumer.company;
    self.customerNameField.text = model.consumer.name;
    [self.inviterButton setTitle:model.designerCustomer.designerCode forState:UIControlStateNormal];
    [self.locationButton setTitle:[NSString stringWithFormat:@"%@-%@-%@",model.consumer.province,model.consumer.city,model.consumer.country] forState:UIControlStateNormal];
    [self.customerHeader sd_setImageWithURL:[NSURL URLWithString:model.consumer.picture] placeholderImage:ReplaceImage];
曹云霄's avatar
曹云霄 committed
661 662 663 664 665 666
}




@end