ClientViewController.m 24 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"
曹云霄's avatar
曹云霄 committed
16

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

/**
 *  客户数据源
 */
22
@property (nonatomic,strong) NSMutableArray *customerResultArray;
曹云霄's avatar
曹云霄 committed
23 24 25 26 27 28 29 30 31 32 33 34 35 36

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

@end

@implementation ClientViewController


/**
 *  初始化客户数据源
 */
37
- (NSMutableArray *)customerResultArray
曹云霄's avatar
曹云霄 committed
38
{
39
    if (_customerResultArray == nil) {
曹云霄's avatar
曹云霄 committed
40
        
41
        _customerResultArray = [NSMutableArray array];
曹云霄's avatar
曹云霄 committed
42
    }
43
    return _customerResultArray;
曹云霄's avatar
曹云霄 committed
44 45 46 47 48 49 50 51 52 53 54
}

- (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;
55
        _condition.page = page;
曹云霄's avatar
曹云霄 committed
56 57 58 59 60 61 62
    }
    return _condition;
}

- (void)viewDidLoad {
    [super viewDidLoad];
    
63
    [self uiConfigAction];
64 65
    [self addObserver];
    [self setupDefaultStoreAddress];
曹云霄's avatar
曹云霄 committed
66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85
}

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

86
#pragma mark -UI
曹云霄's avatar
曹云霄 committed
87
- (void)uiConfigAction
88 89 90 91 92 93
{
    [self.addpersonInformationButton horizontalCenterImageAndTitle:5];
}

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

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

#pragma mark -省市区选择
- (IBAction)addressChooseButtonClickAction:(UIButton *)sender {
112 113 114 115 116 117 118 119 120
    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
121 122 123 124 125 126 127
        [sender setTitle:address forState:UIControlStateNormal];
    } cancelBlock:^{
        
    }];
}


128 129 130
#pragma mark -邀请人选择
- (IBAction)inviterButtonClickAction:(UIButton *)sender {
    
曹云霄's avatar
曹云霄 committed
131 132 133
}


134 135
#pragma mark -下拉、上拉 加载数据
- (void)loadWebDataSource
曹云霄's avatar
曹云霄 committed
136 137
{
    WS(weakSelf);
138 139 140 141 142 143 144 145 146
    if (self.pullPageIndex == ONE) {
        [self.customerResultArray removeAllObjects];
    }
    self.condition.page.page = self.pullPageIndex;
    [self getshoppersAssociatedCustomer:self.condition completed:^(ConsumerPageResult *result) {
        if (weakSelf.pullPageIndex >= result.totalpages) {
            [weakSelf endRefresh:EndRefreshNotData];
        }else {
            [weakSelf endRefresh:EndRefreshDefault];
曹云霄's avatar
曹云霄 committed
147 148 149 150 151
        }
    }];
}

#pragma mark -获取导购关联客户
152
- (void)getshoppersAssociatedCustomer:(ConsumerQueryCondition *)condition completed:(void(^)(ConsumerPageResult *result))completed
曹云霄's avatar
曹云霄 committed
153 154 155 156
{
    WS(weakSelf);
    [HTTP networkRequestWithURL:SERVERREQUESTURL(GETshoppersCONSUMER)  withRequestType:ZERO withParameter:condition withReturnValueBlock:^(id returnValue) {
        if (RESULT(returnValue)) {
157 158 159
            ConsumerPageResult *customerResult = [[ConsumerPageResult alloc]initWithDictionary:RESPONSE(returnValue) error:nil];
            completed(customerResult);
            for (TOConsumerEntity *objc in customerResult.results) {
曹云霄's avatar
曹云霄 committed
160
                 MyclientEntityModel *myclientModel = [[MyclientEntityModel alloc]initWithDictionary:[objc toDictionary] error:nil];
161
                [weakSelf.customerResultArray addObject:myclientModel];
曹云霄's avatar
曹云霄 committed
162 163
            }
            //刷新后添加选中状态
164 165
            for (int i=0; i<weakSelf.customerResultArray.count; i++) {
                MyclientEntityModel *myclientModel = [weakSelf.customerResultArray objectAtIndex_opple:i];
曹云霄's avatar
曹云霄 committed
166
                if ([[Customermanager manager].model.fid isEqualToString:myclientModel.fid]) {
167
                    InformationTableViewCell *cell = [weakSelf.tableView cellForRowAtIndexPath:[NSIndexPath indexPathForRow:i inSection:0]];
曹云霄's avatar
曹云霄 committed
168 169 170 171
                    myclientModel.selectedState = YES;
                    cell.setCurrentCustomer.selected = YES;
                }
            }
172
            [weakSelf.tableView reloadData];
曹云霄's avatar
曹云霄 committed
173 174 175 176 177 178
        }
        else
        {
            [XBLoadingView showHUDViewWithText:MESSAGE(returnValue)];
        }
    }withFailureBlock:^(NSError *error) {
179
        [weakSelf endRefresh:EndRefreshDefault];
曹云霄's avatar
曹云霄 committed
180 181 182 183 184 185 186 187 188 189
        [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;
190
    cell.model = [self.customerResultArray objectAtIndex_opple:indexPath.row];
曹云霄's avatar
曹云霄 committed
191 192 193 194 195 196 197 198 199 200
    //清空其他选中状态
    WS(weakSelf);
    [cell setBlockSeletced:^(NSInteger index) {
        [weakSelf setCustomerButtonClick:index];
    }];
    return cell;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
201
    return self.customerResultArray.count;
曹云霄's avatar
曹云霄 committed
202 203 204 205 206 207 208 209 210 211
}

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

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    ClientdetailsViewController *clientdetails = [[[self class] getMainStoryboardClass] instantiateViewControllerWithIdentifier:@"clientdetails"];
212
    clientdetails.model = [self.customerResultArray objectAtIndex_opple:indexPath.row];
曹云霄's avatar
曹云霄 committed
213 214 215 216 217
    clientdetails.cellindex = indexPath.row;
    //设置当前客户
    WS(weakSelf);
    [clientdetails setCurrentUserBlock:^(NSInteger index,NSString *title) {
        if ([title isEqualToString:@"设为当前客户"]) {
218
            InformationTableViewCell *cell = [weakSelf.tableView cellForRowAtIndexPath:[NSIndexPath indexPathForRow:index inSection:0]];
曹云霄's avatar
曹云霄 committed
219 220 221 222 223 224 225 226
            [weakSelf setCustomerButtonClick:index];
            cell.setCurrentCustomer.selected = YES;
        }else if ([title isEqualToString:@"退出当前客户"])
        {
            [weakSelf exitCurrentCustomer];
            [weakSelf.addpersonInformationButton setTitle:@"添加客户信息" forState:UIControlStateNormal];
        }
    }];
227
    [self pushViewController:clientdetails animated:YES];
曹云霄's avatar
曹云霄 committed
228 229 230 231 232 233 234
}


#pragma mark -所有客户界面 设置当前客户通知回调
- (void)setupCurrentCustomer:(NSNotification *)not
{
    NSDictionary *dict = not.object;
235
    for (InformationTableViewCell *cell in self.tableView.visibleCells) {
曹云霄's avatar
曹云霄 committed
236 237
        cell.setCurrentCustomer.selected = NO;
    }
238
    for (MyclientEntityModel *model in self.customerResultArray) {
曹云霄's avatar
曹云霄 committed
239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261
        if ([model.fid isEqualToString:dict[@"customerid"]]) {
            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];
    }
}

#pragma mark -设置为当前客户回调2
- (void)setCustomerButtonClick:(NSInteger)index;
{
262
    for (InformationTableViewCell *cell in self.tableView.visibleCells) {
曹云霄's avatar
曹云霄 committed
263 264
        cell.setCurrentCustomer.selected = NO;
    }
265
    for (MyclientEntityModel *model in self.customerResultArray) {
曹云霄's avatar
曹云霄 committed
266 267
        model.selectedState = NO;
    }
268
    MyclientEntityModel *model = [self.customerResultArray objectAtIndex_opple:index];
曹云霄's avatar
曹云霄 committed
269 270 271
    //判断省市区是否为空
    if ([[self class] isBlankString:model.country]) {
        WS(weakSelf);
272 273 274
        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
275 276 277 278 279 280 281 282 283 284 285 286 287
            //省市区
            NSArray *addressArray = [address componentsSeparatedByString:@"-"];
            model.province = addressArray[0];
            model.city = addressArray[1];
            model.country = addressArray[2];
            model.selectedState = YES;
            //保存客户信息
            [Shoppersmanager manager].currentCustomer = YES;
            [Customermanager manager].model = model;
            [self setUpCurrentCustomerAssignment:model];
            [weakSelf addCustomerInformation:YES];
            [weakSelf.addpersonInformationButton setTitle:@"退出当前客户" forState:UIControlStateNormal];
        } cancelBlock:nil];
288
        
曹云霄's avatar
曹云霄 committed
289 290 291 292 293 294 295 296
    }else {
        model.selectedState = YES;
        //保存客户信息
        [Shoppersmanager manager].currentCustomer = YES;
        [Customermanager manager].model = model;
        [self setUpCurrentCustomerAssignment:model];
        [self.addpersonInformationButton setTitle:@"退出当前客户" forState:UIControlStateNormal];
    }
297
    [self.tableView reloadData];
曹云霄's avatar
曹云霄 committed
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
}


#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;
        [Customermanager manager].model.picture = string;
361 362
        for (int i=0;i<weskSelf.customerResultArray.count;i++) {
            MyclientEntityModel *customerModel = (MyclientEntityModel *)[weskSelf.customerResultArray objectAtIndex_opple:i];
曹云霄's avatar
曹云霄 committed
363 364 365
            if ([customerModel.fid isEqualToString:[Customermanager manager].model.fid]) {
                customerModel.picture = string;
                NSIndexPath *indexPath = [NSIndexPath indexPathForRow:i inSection:0];
366
                [weskSelf.tableView reloadRowsAtIndexPaths:@[indexPath] withRowAnimation:UITableViewRowAnimationBottom];
曹云霄's avatar
曹云霄 committed
367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384
                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));
            }];
385
        }else{
曹云霄's avatar
曹云霄 committed
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
            [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;
    customerEntity.picture = headerurl;
    [HTTP networkRequestWithURL:SERVERREQUESTURL(ADDCONSUMER)  withRequestType:ZERO withParameter:customerEntity withReturnValueBlock:^(id returnValue) {
        [XBLoadingView hideHUDViewWithDefault];
        if (RESULT(returnValue)) {
            [XBLoadingView showHUDViewWithSuccessText:@"修改成功" completeBlock:^{
                block();
            }];
417
        }else{
曹云霄's avatar
曹云霄 committed
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
            [XBLoadingView showHUDViewWithText:MESSAGE(returnValue)];
        }
    }withFailureBlock:^(NSError *error) {
        [XBLoadingView showHUDViewWithText:error.localizedDescription];
    }];
}

#pragma mark -更新用户名字
- (void)changeCustomerName
{
    MyclientEntityModel *model = [Customermanager manager].model;
    [Notification postNotificationName:CHANGECUSTOMERNAME object:model.name];
}

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

#pragma mark -设置为当前用户请求、写入访问时间
- (void)setupUserRequest
{
    MyclientEntityModel *model = [Customermanager manager].model;
    [HTTP networkWithDictionaryRequestWithURL:[NSString stringWithFormat:@"%@%@",SERVERREQUESTURL(SAVEVISITEDTIME),model.fid]  withRequestType:ONE withParameter:nil withReturnValueBlock:^(id returnValue) {
        if (RESULT(returnValue)) {
            NSLog(@"写入客户访问时间成功");
        }
    } withFailureBlock:^(id error) {
        NSLog(@"写入客户访问时间失败");
    }];
}


#pragma mark -添加客户信息、或者退出当前客户
453
- (IBAction)addCustomerInformationOrExit:(UIButton *)sender {
曹云霄's avatar
曹云霄 committed
454 455 456 457 458 459 460 461 462 463 464 465 466 467 468
    if ([sender.currentTitle isEqualToString:@"退出当前客户"]) {
        [self exitCurrentCustomer];
        [sender setTitle:@"添加客户信息" forState:UIControlStateNormal];
        return;
    }
    [self addCustomerInformation:NO];
}

#pragma mark -退出当前客户
- (void)exitCurrentCustomer
{
    self.customerNameField.text = nil;
    self.customerAddress.text = nil;
    self.phoneNumberField.text = nil;
    self.companyNameField.text = nil;
469
    [self setupDefaultStoreAddress];
曹云霄's avatar
曹云霄 committed
470 471
    [Shoppersmanager manager].currentCustomer = NO;
    [Customermanager manager].model = nil;
472
    self.customerHeader.image = TCImage(@"customer");
曹云霄's avatar
曹云霄 committed
473 474
    [self changeCustomerName];
    [self queryShoppingCarNumber];
475
    for (InformationTableViewCell *cell in self.tableView.visibleCells) {
曹云霄's avatar
曹云霄 committed
476 477
        cell.setCurrentCustomer.selected = NO;
    }
478
    for (MyclientEntityModel *model in self.customerResultArray) {
曹云霄's avatar
曹云霄 committed
479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494
        model.selectedState = NO;
    }
}

#pragma mark -添加客户信息请求 -新增、或者修改 ischange为真是修改
- (void)addCustomerInformation:(BOOL)isChange
{
    [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
495 496 497 498 499
    NSString *phoneNumber = self.phoneNumberField.text;
    if (![phoneNumber isTelephone]) {
        [XBLoadingView showHUDViewWithText:@"手机号码格式不正确"];
        return;
    }
曹云霄's avatar
曹云霄 committed
500 501 502 503 504 505 506 507 508 509 510
    if ([BaseViewController isBlankString:self.customerAddress.text]) {
        [XBLoadingView showHUDViewWithText:@"地址不能为空"];
        return;
    }
    if ([BaseViewController isBlankString:[self.locationButton currentTitle]]) {
        [XBLoadingView showHUDViewWithText:@"省市区不能为空"];
        return;
    }
    //客户信息类
    MyclientEntityModel *customerEntity = [[MyclientEntityModel alloc]init];
    customerEntity.createName = [Shoppersmanager manager].shoppers.employee.realName;
511 512 513 514
    customerEntity.mobile = self.phoneNumberField.text;
    customerEntity.address = self.customerAddress.text;
    customerEntity.name = self.customerNameField.text;
    customerEntity.company = self.companyNameField.text;
曹云霄's avatar
曹云霄 committed
515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530
    customerEntity.picture = [Customermanager manager].model.picture;
    //修改
    if (isChange) {
       customerEntity.fid = [Customermanager manager].model.fid;
    }
    //省市区
    NSArray *addressArray = [[self.locationButton currentTitle] componentsSeparatedByString:@"-"];
    customerEntity.province = addressArray[0];
    customerEntity.city = addressArray[1];
    customerEntity.country = addressArray[2];
    WS(weakSelf);
    [XBLoadingView showHUDViewWithDefault];
    [HTTP networkRequestWithURL:SERVERREQUESTURL(ADDCONSUMER)  withRequestType:ZERO withParameter:customerEntity withReturnValueBlock:^(id returnValue) {
        
        [XBLoadingView hideHUDViewWithDefault];
        if (RESULT(returnValue)) {
531
            [weakSelf.tableView.mj_header beginRefreshing];
曹云霄's avatar
曹云霄 committed
532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575
            //新增保存ID
            if (!isChange) {
                customerEntity.fid = RESPONSE(returnValue);
                [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;
    address.consumerId = model.fid;
    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(@"写入默认收货地址成功");
        }
576
        else{
曹云霄's avatar
曹云霄 committed
577 578 579 580 581 582 583
            [XBLoadingView showHUDViewWithText:MESSAGE(returnValue)];
        }
    }withFailureBlock:^(id error) {
        NSLog(@"写入默认收货地址失败");
    }];
}

584
#pragma mark -UITextFieldDelegate代理 ----搜索、完成
曹云霄's avatar
曹云霄 committed
585 586 587
- (BOOL)textFieldShouldReturn:(UITextField *)textField
{
    [textField resignFirstResponder];
588 589 590 591
    if (textField.returnKeyType == UIReturnKeyDone) {
        [self addCustomerInformation:YES];
        return YES;
    }
曹云霄's avatar
曹云霄 committed
592 593 594 595 596 597 598 599 600 601 602 603 604 605 606
    [self searchCustomerInforMation];
    return YES;
}


#pragma amrk -搜索客户信息
- (void)searchCustomerInforMation
{
    if ([BaseViewController isBlankString:self.searchPersonInformationField.text]) {
        self.condition.nameEquals = nil;
        self.condition.mobileEquals = nil;
    }else {
        self.condition.nameEquals = self.searchPersonInformationField.text;
        self.condition.mobileEquals = self.searchPersonInformationField.text;
    }
607
    [self.tableView.mj_header beginRefreshing];
曹云霄's avatar
曹云霄 committed
608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625
}


#pragma mark -设置为当前客户赋值
- (void)setUpCurrentCustomerAssignment:(MyclientEntityModel *)model
{
    self.customerAddress.text = model.address;
    self.phoneNumberField.text = model.mobile;
    self.companyNameField.text = model.company;
    self.customerNameField.text = model.name;
    [self.locationButton setTitle:[NSString stringWithFormat:@"%@-%@-%@",model.province,model.city,model.country] forState:UIControlStateNormal];
    [self.customerHeader sd_setImageWithURL:[NSURL URLWithString:model.picture] placeholderImage:ReplaceImage];
}




@end