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

#import "ClientViewController.h"
曹云霄's avatar
曹云霄 committed
10
#import "InformationTableViewCell.h"
曹云霄's avatar
曹云霄 committed
11
#import "ClientdetailsViewController.h"
勾芒's avatar
勾芒 committed
12
#import "MyclientEntityModel.h"
13
#import "DeviceDirectionManager.h"
14
#import "PerfectInformationViewController.h"
15
#import "MOFSPickerManager.h"
勾芒's avatar
勾芒 committed
16

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

曹云霄's avatar
曹云霄 committed
19 20 21 22 23
/**
 *  客户数据源
 */
@property (nonatomic,strong) NSMutableArray *CustomerresultArray;

勾芒's avatar
勾芒 committed
24 25 26 27 28
/**
 *  总页数
 */
@property (nonatomic,assign) int totalPages;

曹云霄's avatar
曹云霄 committed
29 30 31 32 33
/**
 *  查询条件
 */
@property (nonatomic,strong) ConsumerQueryCondition *condition;

34 35 36 37 38 39

@property (nonatomic,copy) NSString *nameString;
@property (nonatomic,copy) NSString *phoneNumberString;
@property (nonatomic,copy) NSString *companyString;
@property (nonatomic,copy) NSString *addressString;

曹云霄's avatar
曹云霄 committed
40 41 42 43
@end

@implementation ClientViewController

曹云霄's avatar
曹云霄 committed
44 45 46 47 48 49 50 51 52 53 54 55 56

/**
 *  初始化客户数据源
 */
- (NSMutableArray *)CustomerresultArray
{
    if (_CustomerresultArray == nil) {
        
        _CustomerresultArray = [NSMutableArray array];
    }
    return _CustomerresultArray;
}

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

曹云霄's avatar
曹云霄 committed
71 72
- (void)viewDidLoad {
    [super viewDidLoad];
曹云霄's avatar
曹云霄 committed
73
    
曹云霄's avatar
曹云霄 committed
74
    [self uiConfigAction];
75
    [self addaddObserver];
曹云霄's avatar
曹云霄 committed
76
    [self setUpRefresh];
曹云霄's avatar
曹云霄 committed
77 78
}

79 80 81
- (void)viewDidAppear:(BOOL)animated
{
    [super viewDidAppear:animated];
曹云霄's avatar
曹云霄 committed
82
    self.navigationController.fd_fullscreenPopGestureRecognizer.enabled = NO;
83 84 85
    if ([self.navigationController respondsToSelector:@selector(interactivePopGestureRecognizer)]) {
        self.navigationController.interactivePopGestureRecognizer.enabled = NO;
    }
86 87 88 89 90
}

- (void)viewWillDisappear:(BOOL)animated
{
    [super viewWillDisappear:animated];
曹云霄's avatar
曹云霄 committed
91
    self.navigationController.fd_fullscreenPopGestureRecognizer.enabled = YES;
92 93 94
    if ([self.navigationController respondsToSelector:@selector(interactivePopGestureRecognizer)]) {
        self.navigationController.interactivePopGestureRecognizer.enabled = YES;
    }
95 96
}

曹云霄's avatar
曹云霄 committed
97 98 99
#pragma mark -布局
- (void)uiConfigAction
{
100 101
    ToStoreAddress *storeAddress = [Shoppersmanager manager].storeAddress;
    [self.locationButton setTitle:[NSString stringWithFormat:@"%@-%@-%@",storeAddress.province,storeAddress.city,storeAddress.country] forState:UIControlStateNormal];
曹云霄's avatar
曹云霄 committed
102
    self.changePersonInformationButton.hidden = YES;
勾芒's avatar
勾芒 committed
103
    self.informationTableview.tableFooterView = [UIView new];
104 105 106 107 108 109 110
}

#pragma mark -添加监听
- (void)addaddObserver
{
    //用户头像
    [self.customerHeader addGestureRecognizer:[[UITapGestureRecognizer alloc]initWithTarget:self action:@selector(customerHeaderClckAction:)]];
勾芒's avatar
勾芒 committed
111
    //监听键盘落下的通知
112
    [Notification addObserver:self selector:@selector(keyboadrDismiss) name:UIKeyboardWillHideNotification object:nil];
勾芒's avatar
勾芒 committed
113
    //设置当前客户通知
114
    [Notification addObserver:self selector:@selector(setupCurrentCustomer:) name:@"SETUPCURRENTCUSTOMER" object:nil];
115
    //清空当前客户数据
116 117 118 119 120 121 122 123 124
    [Notification addObserver:self selector:@selector(exitCurrentCustomer) name:@"EMPTYCUSTOMERNAME" object:nil];
    //监听输入变化
    [self.customerNameField addTarget:self action:@selector(nameChange:) forControlEvents:UIControlEventEditingChanged];
    [self.phoneNumberField addTarget:self action:@selector(phoneNumberChange:) forControlEvents:UIControlEventEditingChanged];
    [self.companyNameField addTarget:self action:@selector(companyChange:) forControlEvents:UIControlEventEditingChanged];
    [self.customerAddress addTarget:self action:@selector(addressChange:) forControlEvents:UIControlEventEditingChanged];
}


125 126 127 128 129 130 131 132 133 134 135
#pragma mark -省市区选择
- (IBAction)addressChooseButtonClickAction:(UIButton *)sender {
    
    [[MOFSPickerManager shareManger] showMOFSAddressPickerWithTitle:@"选择城市" cancelTitle:@"取消" commitTitle:@"完成" commitBlock:^(NSString *address, NSString *zipcode) {
        [sender setTitle:address forState:UIControlStateNormal];
        [self.changePersonInformationButton setTitle:@"保存" forState:UIControlStateNormal];
    } cancelBlock:^{
        
    }];
}

136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157
#pragma mark -名称变化时
- (void)nameChange:(UITextField *)textField
{
    self.nameString = textField.text;
}

#pragma mark -手机号码变化时
- (void)phoneNumberChange:(UITextField *)textField
{
    self.phoneNumberString = textField.text;
}

#pragma mark -公司名称变化时
- (void)companyChange:(UITextField *)textField
{
    self.companyString = textField.text;
}

#pragma mark -客户地址变化时
- (void)addressChange:(UITextField *)textField
{
    self.addressString= textField.text;
曹云霄's avatar
曹云霄 committed
158 159 160 161 162
}

#pragma mark -设置刷新
- (void)setUpRefresh
{
曹云霄's avatar
曹云霄 committed
163
    WS(weakSelf);
勾芒's avatar
勾芒 committed
164
    MjRefreshHeaderCustom *headerRefresh = [MjRefreshHeaderCustom headerWithRefreshingBlock:^{
曹云霄's avatar
曹云霄 committed
165
        
曹云霄's avatar
曹云霄 committed
166
        weakSelf.condition.page.page = ONE;
曹云霄's avatar
曹云霄 committed
167
        [weakSelf.informationTableview.mj_footer resetNoMoreData];
168
        [weakSelf getshoppersAssociatedCustomer:weakSelf.condition isRemoveArray:YES];
勾芒's avatar
勾芒 committed
169 170 171 172 173 174 175
    }];
    headerRefresh.stateLabel.hidden = YES;
    headerRefresh.lastUpdatedTimeLabel.hidden = YES;
    self.informationTableview.mj_header =headerRefresh;
    [self.informationTableview.mj_header beginRefreshing];
    self.informationTableview.mj_footer = [MJRefreshAutoNormalFooter footerWithRefreshingBlock:^{
        
曹云霄's avatar
曹云霄 committed
176
        if (++ weakSelf.condition.page.page > weakSelf.totalPages) {
曹云霄's avatar
曹云霄 committed
177
            [weakSelf.informationTableview.mj_footer endRefreshingWithNoMoreData];
勾芒's avatar
勾芒 committed
178 179
        }else
        {
180
            [weakSelf getshoppersAssociatedCustomer:weakSelf.condition isRemoveArray:NO];
勾芒's avatar
勾芒 committed
181 182
        }
    }];
曹云霄's avatar
曹云霄 committed
183
    self.informationTableview.mj_footer.automaticallyHidden = YES;
曹云霄's avatar
曹云霄 committed
184 185
}

曹云霄's avatar
曹云霄 committed
186
#pragma mark -获取导购关联客户
187
- (void)getshoppersAssociatedCustomer:(ConsumerQueryCondition *)condition isRemoveArray:(BOOL)remove
曹云霄's avatar
曹云霄 committed
188
{
189
    WS(weakSelf);
曹云霄's avatar
曹云霄 committed
190
    [XBLoadingView showHUDViewWithDefault];
191
    [HTTP networkRequestWithURL:SERVERREQUESTURL(GETshoppersCONSUMER)  withRequestType:ZERO withParameter:condition withReturnValueBlock:^(id returnValue) {
曹云霄's avatar
曹云霄 committed
192
        
曹云霄's avatar
曹云霄 committed
193 194
        weakSelf.informationTableview.emptyDataSetSource = weakSelf;
        weakSelf.informationTableview.emptyDataSetDelegate = weakSelf;
曹云霄's avatar
曹云霄 committed
195
        [XBLoadingView hideHUDViewWithDefault];
曹云霄's avatar
曹云霄 committed
196
        [weakSelf endRefreshingForTableView:weakSelf.informationTableview];
曹云霄's avatar
曹云霄 committed
197
        if (RESULT(returnValue)) {
曹云霄's avatar
曹云霄 committed
198
            
勾芒's avatar
勾芒 committed
199
            if (remove) {
曹云霄's avatar
曹云霄 committed
200
                [weakSelf.CustomerresultArray removeAllObjects];
勾芒's avatar
勾芒 committed
201
            }
曹云霄's avatar
曹云霄 committed
202
            NSDictionary *datas = RESPONSE(returnValue);
勾芒's avatar
勾芒 committed
203
            ConsumerPageResult *Customerresult = [[ConsumerPageResult alloc]initWithDictionary:datas error:nil];
曹云霄's avatar
曹云霄 committed
204
            weakSelf.totalPages = [datas[@"totalpages"] intValue];
勾芒's avatar
勾芒 committed
205
            for (TOConsumerEntity *objc in Customerresult.results) {
曹云霄's avatar
曹云霄 committed
206
                
207
                 MyclientEntityModel *myclientModel = [[MyclientEntityModel alloc]initWithDictionary:[objc toDictionary] error:nil];
曹云霄's avatar
曹云霄 committed
208
                [weakSelf.CustomerresultArray addObject:myclientModel];
曹云霄's avatar
曹云霄 committed
209
            }
勾芒's avatar
勾芒 committed
210
            //刷新后添加选中状态
曹云霄's avatar
曹云霄 committed
211 212
            for (int i=0; i<weakSelf.CustomerresultArray.count; i++) {
                MyclientEntityModel *myclientModel = [weakSelf.CustomerresultArray objectAtIndex_opple:i];
勾芒's avatar
勾芒 committed
213
                if ([[Customermanager manager].model.fid isEqualToString:myclientModel.fid]) {
曹云霄's avatar
曹云霄 committed
214
                    InformationTableViewCell *cell = [weakSelf.informationTableview cellForRowAtIndexPath:[NSIndexPath indexPathForRow:i inSection:0]];
勾芒's avatar
勾芒 committed
215 216 217 218
                    myclientModel.selectedState = YES;
                    cell.setCurrentCustomer.selected = YES;
                }
            }
曹云霄's avatar
曹云霄 committed
219
            [weakSelf.informationTableview reloadData];
曹云霄's avatar
曹云霄 committed
220 221 222
        }
        else
        {
223
            [XBLoadingView showHUDViewWithText:MESSAGE(returnValue)];
曹云霄's avatar
曹云霄 committed
224
        }
225
    }withFailureBlock:^(NSError *error) {
曹云霄's avatar
曹云霄 committed
226
        [weakSelf endRefreshingForTableView:weakSelf.informationTableview];
曹云霄's avatar
曹云霄 committed
227
        [XBLoadingView showHUDViewWithText:error.localizedDescription];
曹云霄's avatar
曹云霄 committed
228 229 230 231
    }];
}


曹云霄's avatar
曹云霄 committed
232 233 234
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    InformationTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"information" forIndexPath:indexPath];
曹云霄's avatar
曹云霄 committed
235
    cell.selectionStyle = UITableViewCellSelectionStyleNone;
勾芒's avatar
勾芒 committed
236
    cell.indexNumber = indexPath.row;
曹云霄's avatar
曹云霄 committed
237
    cell.model = [self.CustomerresultArray objectAtIndex_opple:indexPath.row];
勾芒's avatar
勾芒 committed
238
    //清空其他选中状态
239
    WS(weakSelf);
勾芒's avatar
勾芒 committed
240
    [cell setBlockSeletced:^(NSInteger index) {
241
        [weakSelf setCustomerButtonClick:index];
勾芒's avatar
勾芒 committed
242
    }];
曹云霄's avatar
曹云霄 committed
243
    return cell;
曹云霄's avatar
曹云霄 committed
244 245
}

曹云霄's avatar
曹云霄 committed
246 247
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
曹云霄's avatar
曹云霄 committed
248
    return self.CustomerresultArray.count;
曹云霄's avatar
曹云霄 committed
249
}
曹云霄's avatar
曹云霄 committed
250

曹云霄's avatar
曹云霄 committed
251
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
曹云霄's avatar
曹云霄 committed
252
{
曹云霄's avatar
曹云霄 committed
253
    return 100;
曹云霄's avatar
曹云霄 committed
254 255
}

曹云霄's avatar
曹云霄 committed
256

曹云霄's avatar
曹云霄 committed
257 258
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
曹云霄's avatar
曹云霄 committed
259
    ClientdetailsViewController *clientdetails = [[[self class] getMainStoryboardClass] instantiateViewControllerWithIdentifier:@"clientdetails"];
勾芒's avatar
勾芒 committed
260
    clientdetails.model = [self.CustomerresultArray objectAtIndex_opple:indexPath.row];
勾芒's avatar
勾芒 committed
261 262
    clientdetails.cellindex = indexPath.row;
    //设置当前客户
263
    WS(weakSelf);
勾芒's avatar
勾芒 committed
264 265
    [clientdetails setCurrentUserBlock:^(NSInteger index,NSString *title) {
        if ([title isEqualToString:@"设为当前客户"]) {
曹云霄's avatar
曹云霄 committed
266
            InformationTableViewCell *cell = [weakSelf.informationTableview cellForRowAtIndexPath:[NSIndexPath indexPathForRow:index inSection:0]];
267
            [weakSelf setCustomerButtonClick:index];
勾芒's avatar
勾芒 committed
268 269 270
            cell.setCurrentCustomer.selected = YES;
        }else if ([title isEqualToString:@"退出当前客户"])
        {
271
            [weakSelf exitCurrentCustomer];
曹云霄's avatar
曹云霄 committed
272
            [weakSelf.addpersonInformationButton setTitle:@"添加客户信息" forState:UIControlStateNormal];
勾芒's avatar
勾芒 committed
273
        }
勾芒's avatar
勾芒 committed
274
    }];
曹云霄's avatar
曹云霄 committed
275 276
    [self.navigationController pushViewController:clientdetails animated:YES];
}
曹云霄's avatar
曹云霄 committed
277 278


勾芒's avatar
勾芒 committed
279
#pragma mark -所有客户界面 设置当前客户通知回调
280
- (void)setupCurrentCustomer:(NSNotification *)not
勾芒's avatar
勾芒 committed
281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297
{
    NSDictionary *dict = not.object;
    for (InformationTableViewCell *cell in self.informationTableview.visibleCells) {
        cell.setCurrentCustomer.selected = NO;
    }
    for (MyclientEntityModel *model in self.CustomerresultArray) {
        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;
勾芒's avatar
勾芒 committed
298
        [Customermanager manager].model = model;
299
        [self setUpCurrentCustomerAssignment:model];
勾芒's avatar
勾芒 committed
300
        [self.addpersonInformationButton setTitle:@"退出当前客户" forState:UIControlStateNormal];
301
        [self notEditing];
勾芒's avatar
勾芒 committed
302 303
    }else if ([dict[@"title"] isEqualToString:@"退出当前客户"])
    {
304
        [self exitCurrentCustomer];
勾芒's avatar
勾芒 committed
305 306 307 308
    }
}

#pragma mark -设置为当前客户回调2
309
- (void)setCustomerButtonClick:(NSInteger)index;
曹云霄's avatar
曹云霄 committed
310 311 312 313
{
    for (InformationTableViewCell *cell in self.informationTableview.visibleCells) {
        cell.setCurrentCustomer.selected = NO;
    }
勾芒's avatar
勾芒 committed
314 315 316 317
    for (MyclientEntityModel *model in self.CustomerresultArray) {
        model.selectedState = NO;
    }
    MyclientEntityModel *model = [self.CustomerresultArray objectAtIndex_opple:index];
318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335
    //判断省市区是否为空
    if ([[self class] isBlankString:model.country]) {
        WS(weakSelf);
        [[MOFSPickerManager shareManger] showMOFSAddressPickerWithTitle:@"选择城市" cancelTitle:@"取消" commitTitle:@"完成" commitBlock:^(NSString *address, NSString *zipcode) {
            //省市区
            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];
            [weakSelf notEditing];
        } cancelBlock:nil];
336

337 338 339 340 341 342 343 344 345 346
    }else {
        model.selectedState = YES;
        //保存客户信息
        [Shoppersmanager manager].currentCustomer = YES;
        [Customermanager manager].model = model;
        [self setUpCurrentCustomerAssignment:model];
        [self.addpersonInformationButton setTitle:@"退出当前客户" forState:UIControlStateNormal];
        [self notEditing];
    }
}
347 348 349 350

#pragma mark -不可编辑状态
- (void)notEditing {
    
勾芒's avatar
勾芒 committed
351
    self.changePersonInformationButton.hidden = NO;
352 353 354
    [self setupUserShoppingCarNumberRequest];
    [self setupUserRequest];
    [self changeCustomerName];
勾芒's avatar
勾芒 committed
355 356 357 358
    self.customerNameField.enabled = NO;
    self.customerAddress.enabled = NO;
    self.phoneNumberField.enabled = NO;
    self.companyNameField.enabled = NO;
勾芒's avatar
勾芒 committed
359 360
}

361 362 363 364
#pragma mark -更改客户头像
- (void)customerHeaderClckAction:(UITapGestureRecognizer *)tap
{
    if (![Shoppersmanager manager].currentCustomer) {
曹云霄's avatar
曹云霄 committed
365
        [XBLoadingView showHUDViewWithText:@"请先设置当前客户"];
366 367
        return;
    }
368
    if([BaseViewController determineCameraPermissions]){
369 370 371
        UIImagePickerController *PcCamera = [[UIImagePickerController alloc]init];
        PcCamera.delegate = self;
        [DeviceDirectionManager instance].isHorizontal=YES;
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
        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;
404 405 406
            }
        });
    }
407 408 409 410 411 412 413 414 415 416 417 418 419 420
}

#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;
曹云霄's avatar
曹云霄 committed
421 422
        for (int i=0;i<weskSelf.CustomerresultArray.count;i++) {
            MyclientEntityModel *customerModel = (MyclientEntityModel *)[weskSelf.CustomerresultArray objectAtIndex_opple:i];
423 424 425
            if ([customerModel.fid isEqualToString:[Customermanager manager].model.fid]) {
                customerModel.picture = string;
                NSIndexPath *indexPath = [NSIndexPath indexPathForRow:i inSection:0];
曹云霄's avatar
曹云霄 committed
426
                [weskSelf.informationTableview reloadRowsAtIndexPaths:@[indexPath] withRowAnimation:UITableViewRowAnimationBottom];
427 428 429
                break;
            }
        }
430 431 432 433 434 435
    }];
}

#pragma mark -保存选中头像、并上传
- (void)uploadUserHeader:(UIImage *)image completeBlock:(void(^)(NSString *string))complete
{
曹云霄's avatar
曹云霄 committed
436
    WS(weakSelf);
437
    NSData *data = UIImageJPEGRepresentation(image, 0.5);
曹云霄's avatar
曹云霄 committed
438
    XBLoadingView *progressView = [XBLoadingView showHUDViewProgressLabel:@"上传头像中"];
曹云霄's avatar
曹云霄 committed
439
    [HTTP uploadImageWithURL:SERVERREQUESTURL(UPLOADHEADER) withRequestType:ONE withImageDatas:data withParameter:nil withReturnValueBlock:^(id returnValue) {
440
        
曹云霄's avatar
曹云霄 committed
441
        if (RESULT(returnValue)) {
曹云霄's avatar
曹云霄 committed
442 443
            [weakSelf modifyshoppersInformation:RESPONSE(returnValue) complete:^{
                complete(RESPONSE(returnValue));
444 445 446
            }];
        }else
        {
447
            [XBLoadingView showHUDViewWithText:MESSAGE(returnValue)];
448
        }
449
    } withprogressBlock:^(double progress) {
曹云霄's avatar
曹云霄 committed
450
        
451
        if (progress >= 1) {
452 453 454 455
            dispatch_async(dispatch_get_main_queue(), ^{
                progressView.labelText = @"上传完成";
                [progressView hide:YES afterDelay:1];
            });
曹云霄's avatar
曹云霄 committed
456 457
        }else{
            progressView.progress = progress;
458
        }
459
    } withFailureBlock:^(NSError *error) {
曹云霄's avatar
曹云霄 committed
460
        [XBLoadingView showHUDViewWithText:error.localizedDescription];
461 462 463 464 465 466 467
    }];
}


#pragma mark -上传完成修改导购个人信息
- (void)modifyshoppersInformation:(NSString *)headerurl complete:(void(^)())block
{
曹云霄's avatar
曹云霄 committed
468
    [XBLoadingView showHUDViewWithDefault];
469
    MyclientEntityModel *customerEntity = nil;
470 471
    customerEntity = [Customermanager manager].model;
    customerEntity.picture = headerurl;
曹云霄's avatar
曹云霄 committed
472
    [HTTP networkRequestWithURL:SERVERREQUESTURL(ADDCONSUMER)  withRequestType:ZERO withParameter:customerEntity withReturnValueBlock:^(id returnValue) {
473
        
曹云霄's avatar
曹云霄 committed
474
        [XBLoadingView hideHUDViewWithDefault];
曹云霄's avatar
曹云霄 committed
475
        if (RESULT(returnValue)) {
476 477 478
            [XBLoadingView showHUDViewWithSuccessText:@"修改成功" completeBlock:^{
                block();
            }];
479 480
        }else
        {
481
            [XBLoadingView showHUDViewWithText:MESSAGE(returnValue)];
482
        }
483
    }withFailureBlock:^(NSError *error) {
曹云霄's avatar
曹云霄 committed
484
        [XBLoadingView showHUDViewWithText:error.localizedDescription];
485 486 487 488
    }];
}


勾芒's avatar
勾芒 committed
489
#pragma mark -更新用户名字
490
- (void)changeCustomerName
勾芒's avatar
勾芒 committed
491
{
勾芒's avatar
勾芒 committed
492
    MyclientEntityModel *model = [Customermanager manager].model;
493
    [Notification postNotificationName:CHANGECUSTOMERNAME object:model.name];
曹云霄's avatar
曹云霄 committed
494
}
曹云霄's avatar
曹云霄 committed
495

曹云霄's avatar
曹云霄 committed
496

勾芒's avatar
勾芒 committed
497
#pragma mark -设置为当前客户、查询购物车数量
498
- (void)setupUserShoppingCarNumberRequest
勾芒's avatar
勾芒 committed
499
{
500
    [self queryShoppingCarNumber];
勾芒's avatar
勾芒 committed
501 502
}

勾芒's avatar
勾芒 committed
503
#pragma mark -设置为当前用户请求、写入访问时间
504
- (void)setupUserRequest
勾芒's avatar
勾芒 committed
505
{
勾芒's avatar
勾芒 committed
506
    MyclientEntityModel *model = [Customermanager manager].model;
曹云霄's avatar
曹云霄 committed
507
    [HTTP networkWithDictionaryRequestWithURL:[NSString stringWithFormat:@"%@%@",SERVERREQUESTURL(SAVEVISITEDTIME),model.fid]  withRequestType:ONE withParameter:nil withReturnValueBlock:^(id returnValue) {
勾芒's avatar
勾芒 committed
508
        
曹云霄's avatar
曹云霄 committed
509
        if (RESULT(returnValue)) {
勾芒's avatar
勾芒 committed
510 511
            NSLog(@"写入客户访问时间成功");
        }
512
    } withFailureBlock:^(id error) {
勾芒's avatar
勾芒 committed
513 514 515 516 517 518
        
        NSLog(@"写入客户访问时间失败");
    }];
}


勾芒's avatar
勾芒 committed
519
#pragma mark -添加客户信息、或者退出当前客户
曹云霄's avatar
曹云霄 committed
520 521 522
- (IBAction)addCustomerInformationORExit:(UIButton *)sender {
    
    if ([sender.currentTitle isEqualToString:@"退出当前客户"]) {
523
        [self exitCurrentCustomer];
曹云霄's avatar
曹云霄 committed
524 525 526
        [sender setTitle:@"添加客户信息" forState:UIControlStateNormal];
        return;
    }
勾芒's avatar
勾芒 committed
527
    [self addCustomerInformation:NO];
勾芒's avatar
勾芒 committed
528
    
曹云霄's avatar
曹云霄 committed
529 530
}

勾芒's avatar
勾芒 committed
531
#pragma mark -退出当前客户
532
- (void)exitCurrentCustomer
勾芒's avatar
勾芒 committed
533 534 535 536 537
{
    self.customerNameField.text = nil;
    self.customerAddress.text = nil;
    self.phoneNumberField.text = nil;
    self.companyNameField.text = nil;
538
    [self.locationButton setTitle:nil forState:UIControlStateNormal];
勾芒's avatar
勾芒 committed
539 540 541 542 543 544
    self.customerNameField.enabled = YES;
    self.customerAddress.enabled = YES;
    self.phoneNumberField.enabled = YES;
    self.companyNameField.enabled = YES;
    self.changePersonInformationButton.hidden = YES;
    [Shoppersmanager manager].currentCustomer = NO;
勾芒's avatar
勾芒 committed
545
    [Customermanager manager].model = nil;
勾芒's avatar
勾芒 committed
546
    self.customerHeader.image = TCImage(@"now");
勾芒's avatar
勾芒 committed
547
    [self.changePersonInformationButton setTitle:@"更改客户信息" forState:UIControlStateNormal];
548 549
    [self changeCustomerName];
    [self queryShoppingCarNumber];
550
    //全部为未选中状态
勾芒's avatar
勾芒 committed
551 552 553 554 555 556 557
    for (InformationTableViewCell *cell in self.informationTableview.visibleCells) {
        cell.setCurrentCustomer.selected = NO;
    }
    for (MyclientEntityModel *model in self.CustomerresultArray) {
        model.selectedState = NO;
    }
}
曹云霄's avatar
曹云霄 committed
558

559
#pragma mark -添加客户信息请求 -新增、或者修改 ischange为真是修改
勾芒's avatar
勾芒 committed
560
- (void)addCustomerInformation:(BOOL)isChange
曹云霄's avatar
曹云霄 committed
561 562
{
    [self.view endEditing:YES];
563
    if ([BaseViewController isBlankString:self.customerNameField.text]) {
曹云霄's avatar
曹云霄 committed
564
        [XBLoadingView showHUDViewWithText:@"客户名不能为空"];
曹云霄's avatar
曹云霄 committed
565 566
        return;
    }
567
    if ([BaseViewController isBlankString:self.phoneNumberField.text]) {
曹云霄's avatar
曹云霄 committed
568
        [XBLoadingView showHUDViewWithText:@"手机号码不能为空"];
曹云霄's avatar
曹云霄 committed
569 570 571 572
        return;
    }
    NSString *phoneNumber = self.phoneNumberField.text;
    if (![HENLENSONG isValidateMobile:phoneNumber]) {
曹云霄's avatar
曹云霄 committed
573
        [XBLoadingView showHUDViewWithText:@"手机号码格式不正确"];
曹云霄's avatar
曹云霄 committed
574 575
        return;
    }
576
    if ([BaseViewController isBlankString:self.customerAddress.text]) {
曹云霄's avatar
曹云霄 committed
577
        [XBLoadingView showHUDViewWithText:@"地址不能为空"];
曹云霄's avatar
曹云霄 committed
578 579
        return;
    }
580 581 582 583
    if ([BaseViewController isBlankString:[self.locationButton currentTitle]]) {
        [XBLoadingView showHUDViewWithText:@"省市区不能为空"];
        return;
    }
曹云霄's avatar
曹云霄 committed
584
    //客户信息类
勾芒's avatar
勾芒 committed
585
    MyclientEntityModel *customerEntity = [[MyclientEntityModel alloc]init];
586
    customerEntity.createName = [Shoppersmanager manager].shoppers.employee.realName;
587 588 589 590
    customerEntity.mobile = self.phoneNumberString;
    customerEntity.address = self.addressString;
    customerEntity.name = self.nameString;
    customerEntity.company = self.companyString;
勾芒's avatar
勾芒 committed
591
    customerEntity.picture = [Customermanager manager].model.picture;
勾芒's avatar
勾芒 committed
592 593
    //修改
    if (isChange) {
勾芒's avatar
勾芒 committed
594
       customerEntity.fid = [Customermanager manager].model.fid;
勾芒's avatar
勾芒 committed
595
    }
596 597 598 599 600
    //省市区
    NSArray *addressArray = [[self.locationButton currentTitle] componentsSeparatedByString:@"-"];
    customerEntity.province = addressArray[0];
    customerEntity.city = addressArray[1];
    customerEntity.country = addressArray[2];
601
    WS(weakSelf);
曹云霄's avatar
曹云霄 committed
602
    [XBLoadingView showHUDViewWithDefault];
曹云霄's avatar
曹云霄 committed
603
    [HTTP networkRequestWithURL:SERVERREQUESTURL(ADDCONSUMER)  withRequestType:ZERO withParameter:customerEntity withReturnValueBlock:^(id returnValue) {
曹云霄's avatar
曹云霄 committed
604
        
曹云霄's avatar
曹云霄 committed
605
        [XBLoadingView hideHUDViewWithDefault];
曹云霄's avatar
曹云霄 committed
606
        if (RESULT(returnValue)) {
607
            [weakSelf.informationTableview.mj_header beginRefreshing];
勾芒's avatar
勾芒 committed
608 609
            //新增保存ID
            if (!isChange) {
曹云霄's avatar
曹云霄 committed
610
                customerEntity.fid = RESPONSE(returnValue);
曹云霄's avatar
曹云霄 committed
611
                [Customermanager manager].model = customerEntity;
612
                [XBLoadingView showHUDViewWithSuccessText:@"新增成功" completeBlock:nil];
613
                [weakSelf addAddressInformationRequest];
勾芒's avatar
勾芒 committed
614 615
            }else
            {
616
                [XBLoadingView showHUDViewWithSuccessText:@"修改成功" completeBlock:nil];
617
                [weakSelf.changePersonInformationButton setTitle:@"更改客户信息" forState:UIControlStateNormal];
勾芒's avatar
勾芒 committed
618
            }
勾芒's avatar
勾芒 committed
619
            //保存客户信息
勾芒's avatar
勾芒 committed
620
            [Customermanager manager].model = customerEntity;
勾芒's avatar
勾芒 committed
621
            [Shoppersmanager manager].currentCustomer = YES;
622
            [weakSelf.addpersonInformationButton setTitle:@"退出当前客户" forState:UIControlStateNormal];
623
            [weakSelf notEditing];
624
            [weakSelf setUpCurrentCustomerAssignment:customerEntity];
曹云霄's avatar
曹云霄 committed
625 626 627
        }
        else
        {
628
            [XBLoadingView showHUDViewWithText:MESSAGE(returnValue)];
曹云霄's avatar
曹云霄 committed
629
        }
630
    }withFailureBlock:^(id error) {
曹云霄's avatar
曹云霄 committed
631
        [XBLoadingView hideHUDViewWithDefault];
曹云霄's avatar
曹云霄 committed
632 633 634 635
    }];
}


勾芒's avatar
勾芒 committed
636 637 638 639
#pragma mark -新增客户成功写入默认地址信息
- (void)addAddressInformationRequest
{
    TOShippingAddrEntity *address = [[TOShippingAddrEntity alloc]init];
曹云霄's avatar
曹云霄 committed
640 641
    MyclientEntityModel *model = [Customermanager manager].model;
    address.consumerId = model.fid;
勾芒's avatar
勾芒 committed
642 643 644
    address.name = self.customerNameField.text;
    address.miblephone = self.phoneNumberField.text;
    address.address = self.customerAddress.text;
645 646 647 648 649
    //省市区
    NSArray *addressArray = [[self.locationButton currentTitle] componentsSeparatedByString:@"-"];
    address.province = addressArray[0];
    address.city = addressArray[1];
    address.country = addressArray[2];
曹云霄's avatar
曹云霄 committed
650
    [HTTP networkRequestWithURL:SERVERREQUESTURL(ADDADDRESS)  withRequestType:ZERO withParameter:address withReturnValueBlock:^(id returnValue) {
曹云霄's avatar
曹云霄 committed
651
        if (RESULT(returnValue)) {
勾芒's avatar
勾芒 committed
652 653 654 655
            NSLog(@"写入默认收货地址成功");
        }
        else
        {
656
            [XBLoadingView showHUDViewWithText:MESSAGE(returnValue)];
勾芒's avatar
勾芒 committed
657
        }
658
    }withFailureBlock:^(id error) {
勾芒's avatar
勾芒 committed
659 660 661 662 663
        NSLog(@"写入默认收货地址失败");
    }];
}


曹云霄's avatar
曹云霄 committed
664 665
#pragma mark -更改客户信息
- (IBAction)changeCustomerInformation:(UIButton *)sender {
勾芒's avatar
勾芒 committed
666 667 668 669 670 671
    if ([sender.currentTitle isEqualToString:@"保存"]) {
        [self.customerNameField resignFirstResponder];
        [sender setTitle:@"更改客户信息" forState:UIControlStateNormal];
         [self addCustomerInformation:YES];
    }else
    {
672 673 674 675
        self.nameString = self.customerNameField.text;
        self.phoneNumberString = self.phoneNumberField.text;
        self.companyString = self.companyNameField.text;
        self.addressString = self.customerAddress.text;
勾芒's avatar
勾芒 committed
676 677 678 679 680
        dispatch_async(dispatch_get_main_queue(), ^{
            [self.customerNameField becomeFirstResponder];
        });
        [sender setTitle:@"保存" forState:UIControlStateNormal];
    }
曹云霄's avatar
曹云霄 committed
681 682 683 684
    self.customerNameField.enabled = YES;
    self.customerAddress.enabled = YES;
    self.phoneNumberField.enabled = YES;
    self.companyNameField.enabled = YES;
685
    self.locationButton.enabled = YES;
曹云霄's avatar
曹云霄 committed
686 687 688 689 690 691
}


#pragma mark -UITextFieldDelegate代理 ----搜索
- (BOOL)textFieldShouldReturn:(UITextField *)textField
{
曹云霄's avatar
曹云霄 committed
692
    [textField resignFirstResponder];
曹云霄's avatar
曹云霄 committed
693 694 695 696 697 698 699 700
    [self searchCustomerInforMation];
    return YES;
}


#pragma amrk -搜索客户信息
- (void)searchCustomerInforMation
{
701
    if ([BaseViewController isBlankString:self.searchPersonInformationField.text]) {
曹云霄's avatar
曹云霄 committed
702 703 704 705 706
        self.condition.nameEquals = nil;
        self.condition.mobileEquals = nil;
    }else {
        self.condition.nameEquals = self.searchPersonInformationField.text;
        self.condition.mobileEquals = self.searchPersonInformationField.text;
曹云霄's avatar
曹云霄 committed
707
    }
曹云霄's avatar
曹云霄 committed
708
    [self.informationTableview.mj_header beginRefreshing];
勾芒's avatar
勾芒 committed
709 710 711 712
}


#pragma mark -键盘落下,判断更改信息按钮是否可见,可见则取消输入框的响应
713
- (void)keyboadrDismiss
勾芒's avatar
勾芒 committed
714
{
勾芒's avatar
勾芒 committed
715
    if (!self.changePersonInformationButton.hidden) {
勾芒's avatar
勾芒 committed
716 717 718 719
        self.customerNameField.enabled = NO;
        self.customerAddress.enabled = NO;
        self.phoneNumberField.enabled = NO;
        self.companyNameField.enabled = NO;
720
        [self.changePersonInformationButton setTitle:@"更改客户信息" forState:UIControlStateNormal];
勾芒's avatar
勾芒 committed
721
    }
曹云霄's avatar
曹云霄 committed
722
}
曹云霄's avatar
曹云霄 committed
723

724 725
#pragma mark -设置为当前客户赋值
- (void)setUpCurrentCustomerAssignment:(MyclientEntityModel *)model
726
{
727 728 729 730 731 732 733 734 735
    self.customerAddress.text = model.address;
    self.phoneNumberField.text = model.mobile;
    self.companyNameField.text = model.company;
    self.customerNameField.text = model.name;
    self.phoneNumberString = model.mobile;
    self.addressString = model.address;
    self.nameString = model.name;
    self.companyString = model.company;
    [self.locationButton setTitle:[NSString stringWithFormat:@"%@-%@-%@",model.province,model.city,model.country] forState:UIControlStateNormal];
736
    [self.customerHeader sd_setImageWithURL:[NSURL URLWithString:model.picture] placeholderImage:ReplaceImage];
737 738
}

曹云霄's avatar
曹云霄 committed
739 740 741 742 743 744 745 746 747 748 749 750 751 752 753 754
#pragma mark -友好界面
- (UIImage *)imageForEmptyDataSet:(UIScrollView *)scrollView
{
    return kNoDataImage;
}

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

- (BOOL)emptyDataSetShouldAllowScroll:(UIScrollView *)scrollView
{
    return YES;
}

曹云霄's avatar
曹云霄 committed
755 756 757 758 759
- (CGFloat)verticalOffsetForEmptyDataSet:(UIScrollView *)scrollView
{
    return 100;
}

曹云霄's avatar
曹云霄 committed
760 761 762


@end