ClientViewController.m 29.1 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"
勾芒's avatar
勾芒 committed
14

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

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

勾芒's avatar
勾芒 committed
22 23 24 25 26
/**
 *  当前页数
 */
@property (nonatomic,assign) int indexPage;

勾芒's avatar
勾芒 committed
27 28 29 30 31
/**
 *  总页数
 */
@property (nonatomic,assign) int totalPages;

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

曹云霄's avatar
曹云霄 committed
37 38 39 40
@end

@implementation ClientViewController

曹云霄's avatar
曹云霄 committed
41 42 43 44 45 46 47 48 49 50 51 52 53

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

曹云霄's avatar
曹云霄 committed
54 55 56 57
- (ConsumerQueryCondition *)condition
{
    if (!_condition) {
        _condition = [[ConsumerQueryCondition alloc]init];
曹云霄's avatar
曹云霄 committed
58 59 60 61
        DataPage *page = [[DataPage alloc]init];
        page.sort = @"createDate";
        page.order = @"desc";
        self.condition.page = page;
曹云霄's avatar
曹云霄 committed
62 63 64 65
    }
    return _condition;
}

曹云霄's avatar
曹云霄 committed
66 67
- (void)viewDidLoad {
    [super viewDidLoad];
曹云霄's avatar
曹云霄 committed
68
    
曹云霄's avatar
曹云霄 committed
69 70 71
    [self uiConfigAction];
}

72 73 74
- (void)viewDidAppear:(BOOL)animated
{
    [super viewDidAppear:animated];
曹云霄's avatar
曹云霄 committed
75
    self.navigationController.fd_fullscreenPopGestureRecognizer.enabled = NO;
76 77 78
    if ([self.navigationController respondsToSelector:@selector(interactivePopGestureRecognizer)]) {
        self.navigationController.interactivePopGestureRecognizer.enabled = NO;
    }
79 80 81 82 83
}

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


曹云霄's avatar
曹云霄 committed
91 92 93
#pragma mark -布局
- (void)uiConfigAction
{
曹云霄's avatar
曹云霄 committed
94 95 96 97 98 99 100 101 102 103 104 105 106
    //圆角
    self.customerNameBack.layer.masksToBounds = YES;
    self.customerNameBack.layer.cornerRadius = kCornerRadius;
    self.phoneNumberBack.layer.masksToBounds = YES;
    self.phoneNumberBack.layer.cornerRadius = kCornerRadius;
    self.companyNameBack.layer.masksToBounds = YES;
    self.companyNameBack.layer.cornerRadius = kCornerRadius;
    self.customerAddressBack.layer.masksToBounds = YES;
    self.customerAddressBack.layer.cornerRadius = kCornerRadius;
    self.addpersonInformationButton.layer.masksToBounds = YES;
    self.addpersonInformationButton.layer.cornerRadius = kCornerRadius;
    self.changePersonInformationButton.layer.masksToBounds = YES;
    self.changePersonInformationButton.layer.cornerRadius = kCornerRadius;
勾芒's avatar
勾芒 committed
107
    self.indexPage = 1;
108 109
    self.customerHeader.userInteractionEnabled = YES;
    [self.customerHeader addGestureRecognizer:[[UITapGestureRecognizer alloc]initWithTarget:self action:@selector(customerHeaderClckAction:)]];
勾芒's avatar
勾芒 committed
110
    
曹云霄's avatar
曹云霄 committed
111 112 113 114
    //隐藏更改客户按钮
    self.changePersonInformationButton.hidden = YES;
    
    //代理
曹云霄's avatar
曹云霄 committed
115
    self.informationTableview.delegate = self;
曹云霄's avatar
曹云霄 committed
116
    self.informationTableview.dataSource = self;
勾芒's avatar
勾芒 committed
117
    self.informationTableview.tableFooterView = [UIView new];
曹云霄's avatar
曹云霄 committed
118
    self.searchPersonInformationField.delegate = self;
勾芒's avatar
勾芒 committed
119 120
    //监听键盘落下的通知
    [[NSNotificationCenter defaultCenter]addObserver:self selector:@selector(KeyboadrDismiss) name:UIKeyboardWillHideNotification object:nil];
勾芒's avatar
勾芒 committed
121 122
    //设置当前客户通知
    [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(SetupCurrentCustomer:) name:@"SETUPCURRENTCUSTOMER" object:nil];
123 124
    //清空当前客户数据
    [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(ChangeCustomerName:) name:@"EMPTYCUSTOMERNAME" object:nil];
勾芒's avatar
勾芒 committed
125
    
曹云霄's avatar
曹云霄 committed
126
    WS(weakSelf);
勾芒's avatar
勾芒 committed
127 128
    //下拉刷新
    MjRefreshHeaderCustom *headerRefresh = [MjRefreshHeaderCustom headerWithRefreshingBlock:^{
曹云霄's avatar
曹云霄 committed
129
        
130
        weakSelf.condition.resellerCodeEquals = [Shoppersmanager manager].Shoppers.employee.currentDepart.orgCode;
曹云霄's avatar
曹云霄 committed
131 132
        weakSelf.condition.page.page = 1;
        weakSelf.condition.page.rows = KROWS;
曹云霄's avatar
曹云霄 committed
133 134 135
        weakSelf.indexPage = 1;
        [weakSelf.informationTableview.mj_footer resetNoMoreData];
        [weakSelf getShoppersAssociatedCustomer:weakSelf.condition isRemoveArray:YES];
勾芒's avatar
勾芒 committed
136 137 138 139 140 141 142 143 144
    }];
    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
145 146
        if (++ weakSelf.indexPage > weakSelf.totalPages) {
            [weakSelf.informationTableview.mj_footer endRefreshingWithNoMoreData];
勾芒's avatar
勾芒 committed
147 148
        }else
        {
曹云霄's avatar
曹云霄 committed
149 150
            weakSelf.condition.page.page = weakSelf.indexPage;
            weakSelf.condition.page.rows = KROWS;
曹云霄's avatar
曹云霄 committed
151
            [weakSelf getShoppersAssociatedCustomer:weakSelf.condition isRemoveArray:NO];
勾芒's avatar
勾芒 committed
152 153
        }
    }];
曹云霄's avatar
曹云霄 committed
154
    self.informationTableview.mj_footer.automaticallyHidden = YES;
曹云霄's avatar
曹云霄 committed
155 156
}

曹云霄's avatar
曹云霄 committed
157
#pragma mark -获取导购关联客户
勾芒's avatar
勾芒 committed
158
- (void)getShoppersAssociatedCustomer:(ConsumerQueryCondition *)condition isRemoveArray:(BOOL)remove
曹云霄's avatar
曹云霄 committed
159
{
曹云霄's avatar
曹云霄 committed
160
    __weak typeof(self) weakSelf = self;
曹云霄's avatar
曹云霄 committed
161
    [self CreateMBProgressHUDLoding];
162
    [[NetworkRequestClassManager Manager] NetworkRequestWithURL:SERVERREQUESTURL(GETSHOPPERSCONSUMER) WithRequestType:0 WithParameter:condition WithReturnValueBlock:^(id returnValue) {
曹云霄's avatar
曹云霄 committed
163
        
曹云霄's avatar
曹云霄 committed
164 165 166 167
        weakSelf.informationTableview.emptyDataSetSource = weakSelf;
        weakSelf.informationTableview.emptyDataSetDelegate = weakSelf;
        [weakSelf RemoveMBProgressHUDLoding];
        [weakSelf endRefreshingForTableView:weakSelf.informationTableview];
曹云霄's avatar
曹云霄 committed
168 169
        if ([returnValue[@"code"] isEqualToNumber:@0]) {
            
勾芒's avatar
勾芒 committed
170
            if (remove) {
曹云霄's avatar
曹云霄 committed
171
                [weakSelf.CustomerresultArray removeAllObjects];
勾芒's avatar
勾芒 committed
172
            }
勾芒's avatar
勾芒 committed
173
            NSDictionary *datas = returnValue[@"data"];
勾芒's avatar
勾芒 committed
174
            
勾芒's avatar
勾芒 committed
175
            ConsumerPageResult *Customerresult = [[ConsumerPageResult alloc]initWithDictionary:datas error:nil];
曹云霄's avatar
曹云霄 committed
176
            weakSelf.totalPages = [datas[@"totalpages"] intValue];
勾芒's avatar
勾芒 committed
177
            for (TOConsumerEntity *objc in Customerresult.results) {
曹云霄's avatar
曹云霄 committed
178
                
勾芒's avatar
勾芒 committed
179 180 181 182 183 184 185 186 187 188 189 190 191 192
                 MyclientEntityModel *myclientModel = [[MyclientEntityModel alloc]init];
                 myclientModel.fid = objc.fid;
                 myclientModel.createName = objc.createName;
                 myclientModel.createBy = objc.createBy;
                 myclientModel.createDate = objc.createDate;
                 myclientModel.sysOrgCode = objc.sysOrgCode;
                 myclientModel.name = objc.name;
                 myclientModel.mobile = objc.mobile;
                 myclientModel.province = objc.province;
                 myclientModel.city = objc.city;
                 myclientModel.country = objc.country;
                 myclientModel.address = objc.address;
                 myclientModel.picture = objc.picture;
                 myclientModel.lastVisitedTime = objc.lastVisitedTime;
勾芒's avatar
勾芒 committed
193
                 myclientModel.company = objc.company;
勾芒's avatar
勾芒 committed
194
                
曹云霄's avatar
曹云霄 committed
195
                [weakSelf.CustomerresultArray addObject:myclientModel];
曹云霄's avatar
曹云霄 committed
196
            }
勾芒's avatar
勾芒 committed
197
            //刷新后添加选中状态
曹云霄's avatar
曹云霄 committed
198 199
            for (int i=0; i<weakSelf.CustomerresultArray.count; i++) {
                MyclientEntityModel *myclientModel = [weakSelf.CustomerresultArray objectAtIndex_opple:i];
勾芒's avatar
勾芒 committed
200
                if ([[Customermanager manager].model.fid isEqualToString:myclientModel.fid]) {
曹云霄's avatar
曹云霄 committed
201
                    InformationTableViewCell *cell = [weakSelf.informationTableview cellForRowAtIndexPath:[NSIndexPath indexPathForRow:i inSection:0]];
勾芒's avatar
勾芒 committed
202 203 204 205
                    myclientModel.selectedState = YES;
                    cell.setCurrentCustomer.selected = YES;
                }
            }
曹云霄's avatar
曹云霄 committed
206
            [weakSelf.informationTableview reloadData];
曹云霄's avatar
曹云霄 committed
207 208 209
        }
        else
        {
曹云霄's avatar
曹云霄 committed
210
            [weakSelf ErrorMBProgressView:returnValue[@"message"]];
曹云霄's avatar
曹云霄 committed
211 212 213
        }
        
    } WithErrorCodeBlock:^(id errorCodeValue) {
曹云霄's avatar
曹云霄 committed
214 215
        [weakSelf RemoveMBProgressHUDLoding];
        [weakSelf promptCustomerWithString:@"网络连接已断开"];
曹云霄's avatar
曹云霄 committed
216 217 218
        
    } WithFailureBlock:^(id error) {
        
曹云霄's avatar
曹云霄 committed
219 220
        [weakSelf RemoveMBProgressHUDLoding];
        [weakSelf endRefreshingForTableView:weakSelf.informationTableview];
曹云霄's avatar
曹云霄 committed
221 222 223 224
    }];
}


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

曹云霄's avatar
曹云霄 committed
239 240
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
曹云霄's avatar
曹云霄 committed
241
    return self.CustomerresultArray.count;
曹云霄's avatar
曹云霄 committed
242
}
曹云霄's avatar
曹云霄 committed
243

曹云霄's avatar
曹云霄 committed
244
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
曹云霄's avatar
曹云霄 committed
245
{
曹云霄's avatar
曹云霄 committed
246
    return 100;
曹云霄's avatar
曹云霄 committed
247 248
}

曹云霄's avatar
曹云霄 committed
249

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


勾芒's avatar
勾芒 committed
274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298
#pragma mark -所有客户界面 设置当前客户通知回调
- (void)SetupCurrentCustomer:(NSNotification *)not
{
    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"];
        self.customerNameField.text = model.name;
        self.phoneNumberField.text = model.mobile;
        self.customerAddress.text = model.address;
        [self.customerHeader sd_setImageWithURL:[NSURL URLWithString:model.picture] placeholderImage:TCImage(@"now")];
        //保存客户信息
        [Shoppersmanager manager].currentCustomer = YES;
勾芒's avatar
勾芒 committed
299
        [Customermanager manager].model = model;
勾芒's avatar
勾芒 committed
300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316
        [self.addpersonInformationButton setTitle:@"退出当前客户" forState:UIControlStateNormal];
        self.changePersonInformationButton.hidden = NO;
        [self SetupUserShoppingCarNumberRequest];
        [self SetupUserRequest];
        [self ChangeCustomerName];
        
        self.customerNameField.enabled = NO;
        self.customerAddress.enabled = NO;
        self.phoneNumberField.enabled = NO;
        self.companyNameField.enabled = NO;
    }else if ([dict[@"title"] isEqualToString:@"退出当前客户"])
    {
        [self ExitCurrentCustomer];
    }
}

#pragma mark -设置为当前客户回调2
勾芒's avatar
勾芒 committed
317
- (void)SetCustomerButtonClick:(NSInteger)index;
曹云霄's avatar
曹云霄 committed
318
{
勾芒's avatar
勾芒 committed
319
    
曹云霄's avatar
曹云霄 committed
320 321 322 323
    for (InformationTableViewCell *cell in self.informationTableview.visibleCells) {
        
        cell.setCurrentCustomer.selected = NO;
    }
勾芒's avatar
勾芒 committed
324 325 326 327 328 329
    for (MyclientEntityModel *model in self.CustomerresultArray) {
        
        model.selectedState = NO;
    }
    MyclientEntityModel *model = [self.CustomerresultArray objectAtIndex_opple:index];
    model.selectedState = YES;
曹云霄's avatar
曹云霄 committed
330
    
勾芒's avatar
勾芒 committed
331 332 333
    self.customerNameField.text = model.name;
    self.phoneNumberField.text = model.mobile;
    self.customerAddress.text = model.address;
勾芒's avatar
勾芒 committed
334
    self.companyNameField.text = model.company;
335

勾芒's avatar
勾芒 committed
336
    [self.customerHeader sd_setImageWithURL:[NSURL URLWithString:model.picture] placeholderImage:TCImage(@"now")];
勾芒's avatar
勾芒 committed
337
    //保存客户信息
勾芒's avatar
勾芒 committed
338
    [Shoppersmanager manager].currentCustomer = YES;
勾芒's avatar
勾芒 committed
339
    [Customermanager manager].model = model;
勾芒's avatar
勾芒 committed
340 341
    [self.addpersonInformationButton setTitle:@"退出当前客户" forState:UIControlStateNormal];
    self.changePersonInformationButton.hidden = NO;
勾芒's avatar
勾芒 committed
342
    [self SetupUserShoppingCarNumberRequest];
勾芒's avatar
勾芒 committed
343
    [self SetupUserRequest];
勾芒's avatar
勾芒 committed
344
    [self ChangeCustomerName];
勾芒's avatar
勾芒 committed
345 346 347 348 349
    
    self.customerNameField.enabled = NO;
    self.customerAddress.enabled = NO;
    self.phoneNumberField.enabled = NO;
    self.companyNameField.enabled = NO;
勾芒's avatar
勾芒 committed
350 351 352
}


353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369
#pragma mark -更改客户头像
- (void)customerHeaderClckAction:(UITapGestureRecognizer *)tap
{
    if (![Shoppersmanager manager].currentCustomer) {
        [self ErrorMBProgressView:@"请先设置当前客户"];
        return;
    }
    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;
曹云霄's avatar
曹云霄 committed
370 371 372
            dispatch_async(dispatch_get_main_queue(), ^{
                [weakSelf presentViewController:PcCamera animated:YES completion:nil];
            });
373 374 375 376 377 378 379 380 381 382 383
        }
        else
        {
            [weakSelf ErrorMBProgressView:@"相机无法使用"];
        }
    }]];
    [alertView addAction:[UIAlertAction actionWithTitle:@"从相册选择" style:UIAlertActionStyleDefault handler:^(UIAlertAction * _Nonnull action) {
        //从相册中选择
        if ([UIImagePickerController isSourceTypeAvailable:UIImagePickerControllerSourceTypePhotoLibrary]) {
            [PcCamera setSourceType:UIImagePickerControllerSourceTypePhotoLibrary];
            PcCamera.allowsEditing = YES;
曹云霄's avatar
曹云霄 committed
384 385 386
            dispatch_async(dispatch_get_main_queue(), ^{
                [weakSelf presentViewController:PcCamera animated:YES completion:nil];
            });
387 388 389 390 391 392 393 394 395
        }
        else
        {
            [weakSelf ErrorMBProgressView:@"相册无法打开"];
        }
    }]];
    [alertView addAction:[UIAlertAction actionWithTitle:@"取消" style:UIAlertActionStyleCancel handler:^(UIAlertAction * _Nonnull action) {
        [alertView dismissViewControllerAnimated:YES completion:nil];
    }]];
曹云霄's avatar
曹云霄 committed
396 397 398
    dispatch_async(dispatch_get_main_queue(), ^{
        [weakSelf presentViewController:alertView animated:YES completion:nil];
    });
399 400 401 402 403 404 405 406 407 408 409 410 411 412
}

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

#pragma mark -保存选中头像、并上传
- (void)uploadUserHeader:(UIImage *)image completeBlock:(void(^)(NSString *string))complete
{
    NSData *data = UIImageJPEGRepresentation(image, 0.5);
     __weak typeof(self) weskSelf = self;
430
    [[NetworkRequestClassManager Manager] UploadImageWithURL:SERVERREQUESTURL(UPLOADHEADER) WithRequestType:1 WithImageDatas:data WithParameter:nil WithReturnValueBlock:^(id returnValue) {
431 432 433 434 435 436 437 438 439 440 441 442
        
        if ([returnValue[@"code"] isEqualToNumber:@0]) {
            [weskSelf modifyshoppersInformation:returnValue[@"data"] complete:^{
                complete(returnValue[@"data"]);
            }];
        }else
        {
            [weskSelf ErrorMBProgressView:returnValue[@"message"]];
        }
        
    } WithprogressBlock:^(double progress) {
        if (progress >= 1) {
443 444
            weskSelf.TCHud.labelText = @"上传完成";
            [weskSelf RemoveMBProgressHUDLoding];
445 446
        }else
        {
447
            [weskSelf ShowProgressView:progress];
448 449
        }
    } WithErrorCodeBlock:^(id errorCodeValue) {
450 451
        weskSelf.TCHud.labelText = @"上传失败";
        [weskSelf RemoveMBProgressHUDLoding];
452
    } WithFailureBlock:^(NSError *error) {
453 454
        weskSelf.TCHud.labelText = @"上传失败";
        [weskSelf RemoveMBProgressHUDLoding];
455 456 457 458 459 460 461 462 463 464 465
    }];
}


#pragma mark -上传完成修改导购个人信息
- (void)modifyshoppersInformation:(NSString *)headerurl complete:(void(^)())block
{
    [self CreateMBProgressHUDLoding];
    MyclientEntityModel *customerEntity = [[MyclientEntityModel alloc]init];
    customerEntity = [Customermanager manager].model;
    customerEntity.picture = headerurl;
曹云霄's avatar
曹云霄 committed
466
    __weak typeof(self) weskSelf = self;
467
    [[NetworkRequestClassManager Manager] NetworkRequestWithURL:SERVERREQUESTURL(ADDCONSUMER) WithRequestType:0 WithParameter:customerEntity WithReturnValueBlock:^(id returnValue) {
468
        
曹云霄's avatar
曹云霄 committed
469
        [weskSelf RemoveMBProgressHUDLoding];
470 471
        if ([returnValue[@"code"] isEqualToNumber:@0]) {
            block();
曹云霄's avatar
曹云霄 committed
472
            [weskSelf SuccessMBProgressView:@"修改成功"];
473 474
        }else
        {
曹云霄's avatar
曹云霄 committed
475
            [weskSelf ErrorMBProgressView:returnValue[@"message"]];
476 477 478
        }
        
    } WithErrorCodeBlock:^(id errorCodeValue) {
曹云霄's avatar
曹云霄 committed
479
        [weskSelf RemoveMBProgressHUDLoding];
480
    } WithFailureBlock:^(NSError *error) {
曹云霄's avatar
曹云霄 committed
481
        [weskSelf RemoveMBProgressHUDLoding];
482 483 484 485 486
    }];
}



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

曹云霄's avatar
曹云霄 committed
494

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


勾芒's avatar
勾芒 committed
502

勾芒's avatar
勾芒 committed
503 504 505
#pragma mark -设置为当前用户请求、写入访问时间
- (void)SetupUserRequest
{
曹云霄's avatar
曹云霄 committed
506
    __weak typeof(self) weskSelf = self;
勾芒's avatar
勾芒 committed
507
    MyclientEntityModel *model = [Customermanager manager].model;
508
    [[NetworkRequestClassManager Manager] NetworkWithDictionaryRequestWithURL:[NSString stringWithFormat:@"%@%@",SERVERREQUESTURL(SAVEVISITEDTIME),model.fid] WithRequestType:1 WithParameter:nil WithReturnValueBlock:^(id returnValue) {
勾芒's avatar
勾芒 committed
509 510 511 512 513 514 515
        
        if ([returnValue[@"code"] isEqualToNumber:@0]) {
            NSLog(@"写入客户访问时间成功");
        }
        
    } WithErrorCodeBlock:^(id errorCodeValue) {
        
曹云霄's avatar
曹云霄 committed
516
        [weskSelf promptCustomerWithString:@"网络连接已断开"];
勾芒's avatar
勾芒 committed
517
        
勾芒's avatar
勾芒 committed
518 519 520 521 522 523 524
    } WithFailureBlock:^(id error) {
        
        NSLog(@"写入客户访问时间失败");
    }];
}


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

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

勾芒's avatar
勾芒 committed
567
#pragma mark -添加客户信息请求 -新增、或者修改 ischange为真是新增
勾芒's avatar
勾芒 committed
568
- (void)addCustomerInformation:(BOOL)isChange
曹云霄's avatar
曹云霄 committed
569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586
{
    [self.view endEditing:YES];
    if (self.customerNameField.text.length == 0) {
        
        [self ErrorMBProgressView:@"客户名不能为空"];
        return;
    }
    if (self.phoneNumberField.text.length == 0) {
        
        [self ErrorMBProgressView:@"手机号码不能为空"];
        return;
    }
    NSString *phoneNumber = self.phoneNumberField.text;
    if (![HENLENSONG isValidateMobile:phoneNumber]) {
        
        [self ErrorMBProgressView:@"手机号码格式不正确"];
        return;
    }
曹云霄's avatar
曹云霄 committed
587 588 589 590 591
    if (self.customerAddress.text.length == 0) {
        
        [self ErrorMBProgressView:@"地址不能为空"];
        return;
    }
曹云霄's avatar
曹云霄 committed
592
    //客户信息类
勾芒's avatar
勾芒 committed
593
    MyclientEntityModel *customerEntity = [[MyclientEntityModel alloc]init];
594
    customerEntity.createName = [Shoppersmanager manager].Shoppers.employee.realName;
曹云霄's avatar
曹云霄 committed
595 596
    customerEntity.mobile = self.phoneNumberField.text;
    customerEntity.address = self.customerAddress.text;
597
    customerEntity.name = self.customerNameField.text;
598
    customerEntity.company = self.companyNameField.text;
勾芒's avatar
勾芒 committed
599
    customerEntity.picture = [Customermanager manager].model.picture;
勾芒's avatar
勾芒 committed
600 601
    //修改
    if (isChange) {
勾芒's avatar
勾芒 committed
602
       customerEntity.fid = [Customermanager manager].model.fid;
勾芒's avatar
勾芒 committed
603
    }
604
    WS(weakSelf);
曹云霄's avatar
曹云霄 committed
605
    [self CreateMBProgressHUDLoding];
606
    [[NetworkRequestClassManager Manager] NetworkRequestWithURL:SERVERREQUESTURL(ADDCONSUMER) WithRequestType:0 WithParameter:customerEntity WithReturnValueBlock:^(id returnValue) {
曹云霄's avatar
曹云霄 committed
607
        
608
        [weakSelf RemoveMBProgressHUDLoding];
曹云霄's avatar
曹云霄 committed
609 610
        if ([returnValue[@"code"] isEqualToNumber:@0]) {
            
勾芒's avatar
勾芒 committed
611
            //进入刷新状态
612
            [weakSelf.informationTableview.mj_header beginRefreshing];
勾芒's avatar
勾芒 committed
613 614
            //新增保存ID
            if (!isChange) {
曹云霄's avatar
曹云霄 committed
615 616
                customerEntity.fid = returnValue[@"data"];
                [Customermanager manager].model = customerEntity;
617 618
                [weakSelf SuccessMBProgressView:@"新增成功"];
                [weakSelf addAddressInformationRequest];
勾芒's avatar
勾芒 committed
619 620
            }else
            {
621 622
                [weakSelf SuccessMBProgressView:@"修改成功"];
                [weakSelf.changePersonInformationButton setTitle:@"更改客户信息" forState:UIControlStateNormal];
勾芒's avatar
勾芒 committed
623
            }
勾芒's avatar
勾芒 committed
624
            //保存客户信息
勾芒's avatar
勾芒 committed
625
            [Customermanager manager].model = customerEntity;
勾芒's avatar
勾芒 committed
626
            [Shoppersmanager manager].currentCustomer = YES;
627 628 629 630 631 632 633 634 635
            [weakSelf.addpersonInformationButton setTitle:@"退出当前客户" forState:UIControlStateNormal];
            weakSelf.changePersonInformationButton.hidden = NO;
            weakSelf.customerNameField.enabled = NO;
            weakSelf.customerAddress.enabled = NO;
            weakSelf.phoneNumberField.enabled = NO;
            weakSelf.companyNameField.enabled = NO;
            [weakSelf SetupUserShoppingCarNumberRequest];
            [weakSelf SetupUserRequest];
            [weakSelf ChangeCustomerName];
曹云霄's avatar
曹云霄 committed
636 637 638
        }
        else
        {
639
            [weakSelf ErrorMBProgressView:returnValue[@"message"]];
曹云霄's avatar
曹云霄 committed
640 641 642 643
        }
        
    } WithErrorCodeBlock:^(id errorCodeValue) {
        
644 645
        [weakSelf RemoveMBProgressHUDLoding];
        [weakSelf promptCustomerWithString:@"网络连接已断开"];
曹云霄's avatar
曹云霄 committed
646 647
        
    } WithFailureBlock:^(id error) {
648
        [weakSelf RemoveMBProgressHUDLoding];
曹云霄's avatar
曹云霄 committed
649 650 651 652
    }];
}


勾芒's avatar
勾芒 committed
653 654 655 656
#pragma mark -新增客户成功写入默认地址信息
- (void)addAddressInformationRequest
{
    TOShippingAddrEntity *address = [[TOShippingAddrEntity alloc]init];
曹云霄's avatar
曹云霄 committed
657 658
    MyclientEntityModel *model = [Customermanager manager].model;
    address.consumerId = model.fid;
勾芒's avatar
勾芒 committed
659 660 661
    address.name = self.customerNameField.text;
    address.miblephone = self.phoneNumberField.text;
    address.address = self.customerAddress.text;
曹云霄's avatar
曹云霄 committed
662
    __weak typeof(self) weskSelf = self;
663
    [[NetworkRequestClassManager Manager] NetworkRequestWithURL:SERVERREQUESTURL(ADDADDRESS) WithRequestType:0 WithParameter:address WithReturnValueBlock:^(id returnValue) {
勾芒's avatar
勾芒 committed
664 665 666 667 668 669 670
        
        if ([returnValue[@"code"] isEqualToNumber:@0]) {
            
            NSLog(@"写入默认收货地址成功");
        }
        else
        {
曹云霄's avatar
曹云霄 committed
671
            [weskSelf ErrorMBProgressView:returnValue[@"message"]];
勾芒's avatar
勾芒 committed
672 673 674 675
        }
        
    } WithErrorCodeBlock:^(id errorCodeValue) {
        
曹云霄's avatar
曹云霄 committed
676
        [weskSelf promptCustomerWithString:@"网络连接已断开"];
勾芒's avatar
勾芒 committed
677 678 679 680 681 682 683 684
        
    } WithFailureBlock:^(id error) {
        
        NSLog(@"写入默认收货地址失败");
    }];
}


曹云霄's avatar
曹云霄 committed
685 686
#pragma mark -更改客户信息
- (IBAction)changeCustomerInformation:(UIButton *)sender {
勾芒's avatar
勾芒 committed
687 688 689 690 691 692 693 694 695 696 697
    if ([sender.currentTitle isEqualToString:@"保存"]) {
        [self.customerNameField resignFirstResponder];
        [sender setTitle:@"更改客户信息" forState:UIControlStateNormal];
         [self addCustomerInformation:YES];
    }else
    {
        dispatch_async(dispatch_get_main_queue(), ^{
            [self.customerNameField becomeFirstResponder];
        });
        [sender setTitle:@"保存" forState:UIControlStateNormal];
    }
曹云霄's avatar
曹云霄 committed
698 699 700 701 702 703 704
    self.customerNameField.enabled = YES;
    self.customerAddress.enabled = YES;
    self.phoneNumberField.enabled = YES;
    self.companyNameField.enabled = YES;
}


勾芒's avatar
勾芒 committed
705

曹云霄's avatar
曹云霄 committed
706 707 708
#pragma mark -UITextFieldDelegate代理 ----搜索
- (BOOL)textFieldShouldReturn:(UITextField *)textField
{
曹云霄's avatar
曹云霄 committed
709
    [textField resignFirstResponder];
曹云霄's avatar
曹云霄 committed
710 711 712 713 714 715 716 717 718
    [self searchCustomerInforMation];
    return YES;
}


#pragma amrk -搜索客户信息
- (void)searchCustomerInforMation
{
    if (self.searchPersonInformationField.text.length == 0) {
曹云霄's avatar
曹云霄 committed
719 720 721 722 723
        self.condition.nameEquals = nil;
        self.condition.mobileEquals = nil;
    }else {
        self.condition.nameEquals = self.searchPersonInformationField.text;
        self.condition.mobileEquals = self.searchPersonInformationField.text;
曹云霄's avatar
曹云霄 committed
724
    }
曹云霄's avatar
曹云霄 committed
725
    [self.informationTableview.mj_header beginRefreshing];
勾芒's avatar
勾芒 committed
726 727 728 729 730 731
}


#pragma mark -键盘落下,判断更改信息按钮是否可见,可见则取消输入框的响应
- (void)KeyboadrDismiss
{
勾芒's avatar
勾芒 committed
732
    if (!self.changePersonInformationButton.hidden) {
勾芒's avatar
勾芒 committed
733 734 735 736 737
     
        self.customerNameField.enabled = NO;
        self.customerAddress.enabled = NO;
        self.phoneNumberField.enabled = NO;
        self.companyNameField.enabled = NO;
738 739 740 741
        self.customerNameField.text = [Customermanager manager].model.name;
        self.customerAddress.text = [Customermanager manager].model.address;
        self.phoneNumberField.text = [Customermanager manager].model.mobile;
        self.companyNameField.text = [Customermanager manager].model.company;
742
        [self.changePersonInformationButton setTitle:@"更改客户信息" forState:UIControlStateNormal];
勾芒's avatar
勾芒 committed
743
    }
曹云霄's avatar
曹云霄 committed
744
}
曹云霄's avatar
曹云霄 committed
745

746 747 748 749 750 751 752
#pragma mark -清空当前客户的数据
- (void)ChangeCustomerName:(NSNotification *)not
{
    [self.informationTableview.mj_header beginRefreshing];
    [self ExitCurrentCustomer];
}

曹云霄's avatar
曹云霄 committed
753

曹云霄's avatar
曹云霄 committed
754 755 756 757 758 759 760 761 762 763 764 765 766 767 768 769
#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
770 771 772 773 774
- (CGFloat)verticalOffsetForEmptyDataSet:(UIScrollView *)scrollView
{
    return 100;
}

曹云霄's avatar
曹云霄 committed
775 776 777


@end