ClientViewController.m 29.5 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
{
曹云霄's avatar
曹云霄 committed
100
    self.changePersonInformationButton.hidden = YES;
勾芒's avatar
勾芒 committed
101
    self.informationTableview.tableFooterView = [UIView new];
102 103 104 105 106 107 108
}

#pragma mark -添加监听
- (void)addaddObserver
{
    //用户头像
    [self.customerHeader addGestureRecognizer:[[UITapGestureRecognizer alloc]initWithTarget:self action:@selector(customerHeaderClckAction:)]];
勾芒's avatar
勾芒 committed
109
    //监听键盘落下的通知
110
    [Notification addObserver:self selector:@selector(keyboadrDismiss) name:UIKeyboardWillHideNotification object:nil];
勾芒's avatar
勾芒 committed
111
    //设置当前客户通知
112
    [Notification addObserver:self selector:@selector(setupCurrentCustomer:) name:@"SETUPCURRENTCUSTOMER" object:nil];
113
    //清空当前客户数据
114 115 116 117 118 119 120 121 122
    [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];
}


123 124 125 126 127 128 129 130 131 132 133
#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:^{
        
    }];
}

134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155
#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
156 157 158 159 160
}

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

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


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

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

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

曹云霄's avatar
曹云霄 committed
254

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


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

#pragma mark -设置为当前客户回调2
307
- (void)setCustomerButtonClick:(NSInteger)index;
曹云霄's avatar
曹云霄 committed
308 309 310 311
{
    for (InformationTableViewCell *cell in self.informationTableview.visibleCells) {
        cell.setCurrentCustomer.selected = NO;
    }
勾芒's avatar
勾芒 committed
312 313 314 315
    for (MyclientEntityModel *model in self.CustomerresultArray) {
        model.selectedState = NO;
    }
    MyclientEntityModel *model = [self.CustomerresultArray objectAtIndex_opple:index];
316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333
    //判断省市区是否为空
    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];
334

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

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

359 360 361 362
#pragma mark -更改客户头像
- (void)customerHeaderClckAction:(UITapGestureRecognizer *)tap
{
    if (![Shoppersmanager manager].currentCustomer) {
曹云霄's avatar
曹云霄 committed
363
        [XBLoadingView showHUDViewWithText:@"请先设置当前客户"];
364 365
        return;
    }
366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388
    // 判断应用是否有使用相机的权限
    if(![BaseViewController determineCameraPermissions]){
        UIAlertController *alertVC = [UIAlertController alertControllerWithTitle:@"提示" message:@"请在iPad的“设置-隐私-相机”选项中,允许欧立方访问你的相机" preferredStyle:UIAlertControllerStyleAlert];
        [alertVC addAction:[UIAlertAction actionWithTitle:@"知道了" style:UIAlertActionStyleCancel handler:nil]];
        [self presentViewController:alertVC animated:YES completion:nil];
        
    }else {
        UIAlertController *alertView = [UIAlertController alertControllerWithTitle:nil message:nil preferredStyle:UIAlertControllerStyleAlert];
        UIImagePickerController *PcCamera = [[UIImagePickerController alloc]init];
        PcCamera.delegate = self;
        [DeviceDirectionManager instance].isHorizontal=YES;
        __weak typeof(self) weakSelf = self;
        [alertView addAction:[UIAlertAction actionWithTitle:@"拍照" style:UIAlertActionStyleDefault handler:^(UIAlertAction * _Nonnull action) {
            //拍照
            if ([UIImagePickerController isSourceTypeAvailable:UIImagePickerControllerSourceTypeCamera]) {
                [PcCamera setSourceType:UIImagePickerControllerSourceTypeCamera];
                PcCamera.allowsEditing = YES;
                dispatch_async(dispatch_get_main_queue(), ^{
                    [weakSelf presentViewController:PcCamera animated:YES completion:nil];
                });
            }
            else
            {
曹云霄's avatar
曹云霄 committed
389
                [XBLoadingView showHUDViewWithText:@"相机无法使用"];
390 391 392 393 394 395 396 397 398 399 400 401 402
            }
        }]];
        [alertView addAction:[UIAlertAction actionWithTitle:@"从相册选择" style:UIAlertActionStyleDefault handler:^(UIAlertAction * _Nonnull action) {
            //从相册中选择
            if ([UIImagePickerController isSourceTypeAvailable:UIImagePickerControllerSourceTypePhotoLibrary]) {
                [PcCamera setSourceType:UIImagePickerControllerSourceTypePhotoLibrary];
                PcCamera.allowsEditing = YES;
                dispatch_async(dispatch_get_main_queue(), ^{
                    [weakSelf presentViewController:PcCamera animated:YES completion:nil];
                });
            }
            else
            {
曹云霄's avatar
曹云霄 committed
403
                [XBLoadingView showHUDViewWithText:@"相册无法打开"];
404 405 406 407 408 409 410 411 412
            }
        }]];
        [alertView addAction:[UIAlertAction actionWithTitle:@"取消" style:UIAlertActionStyleCancel handler:^(UIAlertAction * _Nonnull action) {
            [alertView dismissViewControllerAnimated:YES completion:nil];
        }]];
        dispatch_async(dispatch_get_main_queue(), ^{
            [weakSelf presentViewController:alertView animated:YES completion:nil];
        });
    }
413 414 415 416 417 418 419 420 421 422 423 424 425 426
}

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

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


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


勾芒's avatar
勾芒 committed
495
#pragma mark -更新用户名字
496
- (void)changeCustomerName
勾芒's avatar
勾芒 committed
497
{
勾芒's avatar
勾芒 committed
498
    MyclientEntityModel *model = [Customermanager manager].model;
499
    [Notification postNotificationName:CHANGECUSTOMERNAME object:model.name];
曹云霄's avatar
曹云霄 committed
500
}
曹云霄's avatar
曹云霄 committed
501

曹云霄's avatar
曹云霄 committed
502

勾芒's avatar
勾芒 committed
503
#pragma mark -设置为当前客户、查询购物车数量
504
- (void)setupUserShoppingCarNumberRequest
勾芒's avatar
勾芒 committed
505
{
506
    [self queryShoppingCarNumber];
勾芒's avatar
勾芒 committed
507 508
}

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


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

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

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


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


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


#pragma mark -UITextFieldDelegate代理 ----搜索
- (BOOL)textFieldShouldReturn:(UITextField *)textField
{
曹云霄's avatar
曹云霄 committed
698
    [textField resignFirstResponder];
曹云霄's avatar
曹云霄 committed
699 700 701 702 703 704 705 706
    [self searchCustomerInforMation];
    return YES;
}


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


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

730 731
#pragma mark -设置为当前客户赋值
- (void)setUpCurrentCustomerAssignment:(MyclientEntityModel *)model
732
{
733 734 735 736 737 738 739 740 741
    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];
742
    [self.customerHeader sd_setImageWithURL:[NSURL URLWithString:model.picture] placeholderImage:ReplaceImage];
743 744
}

曹云霄's avatar
曹云霄 committed
745 746 747 748 749 750 751 752 753 754 755 756 757 758 759 760
#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
761 762 763 764 765
- (CGFloat)verticalOffsetForEmptyDataSet:(UIScrollView *)scrollView
{
    return 100;
}

曹云霄's avatar
曹云霄 committed
766 767 768


@end