ClientViewController.m 23.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 13
#import "MyclientEntityModel.h"

曹云霄's avatar
曹云霄 committed
14
@interface ClientViewController ()<UITableViewDelegate,UITableViewDataSource,UITextFieldDelegate>
曹云霄's avatar
曹云霄 committed
15

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

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

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

曹云霄's avatar
曹云霄 committed
31 32 33 34
@end

@implementation ClientViewController

曹云霄's avatar
曹云霄 committed
35 36 37 38 39 40 41 42 43 44 45 46 47 48

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


曹云霄's avatar
曹云霄 committed
49 50 51
- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
曹云霄's avatar
曹云霄 committed
52
    
曹云霄's avatar
曹云霄 committed
53
    [self uiConfigAction];
勾芒's avatar
勾芒 committed
54
    
曹云霄's avatar
曹云霄 committed
55 56
}

57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78

- (void)viewDidAppear:(BOOL)animated
{
    [super viewDidAppear:animated];
    
    // 禁用 iOS7 返回手势
    if ([self.navigationController respondsToSelector:@selector(interactivePopGestureRecognizer)]) {
        self.navigationController.interactivePopGestureRecognizer.enabled = NO;
    }
}

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


曹云霄's avatar
曹云霄 committed
79 80 81
#pragma mark -布局
- (void)uiConfigAction
{
曹云霄's avatar
曹云霄 committed
82 83 84 85 86 87 88 89 90 91 92 93 94
    //圆角
    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
95
    self.indexPage = 1;
勾芒's avatar
勾芒 committed
96
    
曹云霄's avatar
曹云霄 committed
97 98 99 100
    //隐藏更改客户按钮
    self.changePersonInformationButton.hidden = YES;
    
    //代理
曹云霄's avatar
曹云霄 committed
101
    self.informationTableview.delegate = self;
曹云霄's avatar
曹云霄 committed
102
    self.informationTableview.dataSource = self;
勾芒's avatar
勾芒 committed
103
    self.informationTableview.tableFooterView = [UIView new];
曹云霄's avatar
曹云霄 committed
104
    self.searchPersonInformationField.delegate = self;
勾芒's avatar
勾芒 committed
105 106
    //监听键盘落下的通知
    [[NSNotificationCenter defaultCenter]addObserver:self selector:@selector(KeyboadrDismiss) name:UIKeyboardWillHideNotification object:nil];
勾芒's avatar
勾芒 committed
107 108 109
    //设置当前客户通知
    [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(SetupCurrentCustomer:) name:@"SETUPCURRENTCUSTOMER" object:nil];
    
勾芒's avatar
勾芒 committed
110 111 112 113 114 115 116 117
    
    //下拉刷新
    MjRefreshHeaderCustom *headerRefresh = [MjRefreshHeaderCustom headerWithRefreshingBlock:^{
        ConsumerQueryCondition *condition = [[ConsumerQueryCondition alloc]init];
        condition.resellerCodeEquals = [[Shoppersmanager manager].Shoppers.employee.currentDepart  orgCode];
        DataPage *page = [[DataPage alloc]init];
        page.page = 1;
        condition.page = page;
勾芒's avatar
勾芒 committed
118
        page.rows = KROWS;
勾芒's avatar
勾芒 committed
119 120 121 122 123 124 125 126 127 128 129 130 131
        self.indexPage = 1;
        [self.informationTableview.mj_footer resetNoMoreData];
        [self getShoppersAssociatedCustomer:condition isRemoveArray:YES];
    }];
    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
132
        if (++ self.indexPage > self.totalPages) {
勾芒's avatar
勾芒 committed
133 134 135 136 137 138
            [self.informationTableview.mj_footer endRefreshingWithNoMoreData];
        }else
        {
            ConsumerQueryCondition *condition = [[ConsumerQueryCondition alloc]init];
            DataPage *page = [[DataPage alloc]init];
            page.page = self.indexPage ++;
勾芒's avatar
勾芒 committed
139
            page.rows = KROWS;
勾芒's avatar
勾芒 committed
140 141 142 143
            condition.page = page;
            [self getShoppersAssociatedCustomer:condition isRemoveArray:NO];
        }
    }];
曹云霄's avatar
曹云霄 committed
144 145
}

曹云霄's avatar
曹云霄 committed
146

曹云霄's avatar
曹云霄 committed
147

勾芒's avatar
勾芒 committed
148

曹云霄's avatar
曹云霄 committed
149
#pragma mark -获取导购关联客户
勾芒's avatar
勾芒 committed
150
- (void)getShoppersAssociatedCustomer:(ConsumerQueryCondition *)condition isRemoveArray:(BOOL)remove
曹云霄's avatar
曹云霄 committed
151 152 153 154 155 156
{
    
    [self CreateMBProgressHUDLoding];
    [[NetworkRequestClassManager Manager] NetworkRequestWithURL:[NSString stringWithFormat:@"%@%@",ServerAddress,@"/consumer/query"] WithRequestType:0 WithParameter:condition WithReturnValueBlock:^(id returnValue) {
        
        [self RemoveMBProgressHUDLoding];
勾芒's avatar
勾芒 committed
157
        [self endRefreshingForTableView:self.informationTableview];
曹云霄's avatar
曹云霄 committed
158 159
        if ([returnValue[@"code"] isEqualToNumber:@0]) {
            
勾芒's avatar
勾芒 committed
160 161 162
            if (remove) {
                [self.CustomerresultArray removeAllObjects];
            }
勾芒's avatar
勾芒 committed
163
            NSDictionary *datas = returnValue[@"data"];
勾芒's avatar
勾芒 committed
164
            
勾芒's avatar
勾芒 committed
165 166
            ConsumerPageResult *Customerresult = [[ConsumerPageResult alloc]initWithDictionary:datas error:nil];
            self.totalPages = [datas[@"totalpages"] intValue];
勾芒's avatar
勾芒 committed
167
            for (TOConsumerEntity *objc in Customerresult.results) {
曹云霄's avatar
曹云霄 committed
168
                
勾芒's avatar
勾芒 committed
169 170 171 172 173 174 175 176 177 178 179 180 181 182
                 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
183
                 myclientModel.company = objc.company;
勾芒's avatar
勾芒 committed
184 185
                
                [self.CustomerresultArray addObject:myclientModel];
曹云霄's avatar
曹云霄 committed
186
            }
勾芒's avatar
勾芒 committed
187 188 189
            //刷新后添加选中状态
            for (int i=0; i<self.CustomerresultArray.count; i++) {
                MyclientEntityModel *myclientModel = [self.CustomerresultArray objectAtIndex_opple:i];
勾芒's avatar
勾芒 committed
190
                if ([[Customermanager manager].model.fid isEqualToString:myclientModel.fid]) {
勾芒's avatar
勾芒 committed
191 192 193 194 195
                    InformationTableViewCell *cell = [self.informationTableview cellForRowAtIndexPath:[NSIndexPath indexPathForRow:i inSection:0]];
                    myclientModel.selectedState = YES;
                    cell.setCurrentCustomer.selected = YES;
                }
            }
曹云霄's avatar
曹云霄 committed
196 197 198 199 200 201 202 203
            [self.informationTableview reloadData];
        }
        else
        {
            [self ErrorMBProgressView:returnValue[@"message"]];
        }
        
    } WithErrorCodeBlock:^(id errorCodeValue) {
勾芒's avatar
勾芒 committed
204 205
        [self RemoveMBProgressHUDLoding];
        [self promptCustomerWithString:@"网络连接已断开"];
曹云霄's avatar
曹云霄 committed
206 207 208
        
    } WithFailureBlock:^(id error) {
        
勾芒's avatar
勾芒 committed
209
        [self RemoveMBProgressHUDLoding];
勾芒's avatar
勾芒 committed
210
        [self endRefreshingForTableView:self.informationTableview];
曹云霄's avatar
曹云霄 committed
211 212 213 214
    }];
}


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

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

曹云霄's avatar
曹云霄 committed
233
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
曹云霄's avatar
曹云霄 committed
234
{
曹云霄's avatar
曹云霄 committed
235
    return 100;
曹云霄's avatar
曹云霄 committed
236 237
}

曹云霄's avatar
曹云霄 committed
238

曹云霄's avatar
曹云霄 committed
239 240 241 242
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    UIStoryboard *storyboard = [UIStoryboard storyboardWithName:@"StoryboardwithCYX" bundle:nil];
    ClientdetailsViewController *clientdetails = [storyboard instantiateViewControllerWithIdentifier:@"clientdetails"];
勾芒's avatar
勾芒 committed
243
    clientdetails.model = [self.CustomerresultArray objectAtIndex_opple:indexPath.row];
勾芒's avatar
勾芒 committed
244 245
    clientdetails.cellindex = indexPath.row;
    //设置当前客户
勾芒's avatar
勾芒 committed
246 247 248 249 250 251 252 253 254 255 256
    [clientdetails setCurrentUserBlock:^(NSInteger index,NSString *title) {
        if ([title isEqualToString:@"设为当前客户"]) {
            InformationTableViewCell *cell = [self.informationTableview cellForRowAtIndexPath:[NSIndexPath indexPathForRow:index inSection:0]];
            [self SetCustomerButtonClick:index];
            cell.setCurrentCustomer.selected = YES;
            
        }else if ([title isEqualToString:@"退出当前客户"])
        {
            [self ExitCurrentCustomer];
            [self.addpersonInformationButton setTitle:@"添加客户信息" forState:UIControlStateNormal];
        }
勾芒's avatar
勾芒 committed
257
    }];
曹云霄's avatar
曹云霄 committed
258 259
    [self.navigationController pushViewController:clientdetails animated:YES];
}
曹云霄's avatar
曹云霄 committed
260 261


勾芒's avatar
勾芒 committed
262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286
#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
287
        [Customermanager manager].model = model;
勾芒's avatar
勾芒 committed
288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304
        [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
305
- (void)SetCustomerButtonClick:(NSInteger)index;
曹云霄's avatar
曹云霄 committed
306
{
勾芒's avatar
勾芒 committed
307
    
曹云霄'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 316 317
    for (MyclientEntityModel *model in self.CustomerresultArray) {
        
        model.selectedState = NO;
    }
    MyclientEntityModel *model = [self.CustomerresultArray objectAtIndex_opple:index];
    model.selectedState = YES;
曹云霄's avatar
曹云霄 committed
318
    
勾芒's avatar
勾芒 committed
319 320 321
    self.customerNameField.text = model.name;
    self.phoneNumberField.text = model.mobile;
    self.customerAddress.text = model.address;
勾芒's avatar
勾芒 committed
322
    self.companyNameField.text = model.company;
勾芒's avatar
勾芒 committed
323
    [self.customerHeader sd_setImageWithURL:[NSURL URLWithString:model.picture] placeholderImage:TCImage(@"now")];
勾芒's avatar
勾芒 committed
324
    //保存客户信息
勾芒's avatar
勾芒 committed
325
    [Shoppersmanager manager].currentCustomer = YES;
勾芒's avatar
勾芒 committed
326
    [Customermanager manager].model = model;
勾芒's avatar
勾芒 committed
327 328
    [self.addpersonInformationButton setTitle:@"退出当前客户" forState:UIControlStateNormal];
    self.changePersonInformationButton.hidden = NO;
勾芒's avatar
勾芒 committed
329
    [self SetupUserShoppingCarNumberRequest];
勾芒's avatar
勾芒 committed
330
    [self SetupUserRequest];
勾芒's avatar
勾芒 committed
331
    [self ChangeCustomerName];
勾芒's avatar
勾芒 committed
332 333 334 335 336
    
    self.customerNameField.enabled = NO;
    self.customerAddress.enabled = NO;
    self.phoneNumberField.enabled = NO;
    self.companyNameField.enabled = NO;
勾芒's avatar
勾芒 committed
337 338 339 340 341 342
}


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

曹云霄's avatar
曹云霄 committed
347

勾芒's avatar
勾芒 committed
348 349 350
#pragma mark -设置为当前客户、查询购物车数量
- (void)SetupUserShoppingCarNumberRequest
{
勾芒's avatar
勾芒 committed
351
    [self QueryShoppingCarNumber];
勾芒's avatar
勾芒 committed
352 353 354
}


勾芒's avatar
勾芒 committed
355

勾芒's avatar
勾芒 committed
356 357 358
#pragma mark -设置为当前用户请求、写入访问时间
- (void)SetupUserRequest
{
勾芒's avatar
勾芒 committed
359 360
    MyclientEntityModel *model = [Customermanager manager].model;
    [[NetworkRequestClassManager Manager] NetworkWithDictionaryRequestWithURL:[NSString stringWithFormat:@"%@%@%@",ServerAddress,@"/consumer/saveLastVisitedTime?consumerId=",model.fid] WithRequestType:1 WithParameter:nil WithReturnValueBlock:^(id returnValue) {
勾芒's avatar
勾芒 committed
361 362 363 364 365 366 367
        
        if ([returnValue[@"code"] isEqualToNumber:@0]) {
            NSLog(@"写入客户访问时间成功");
        }
        
    } WithErrorCodeBlock:^(id errorCodeValue) {
        
勾芒's avatar
勾芒 committed
368 369
        [self promptCustomerWithString:@"网络连接已断开"];
        
勾芒's avatar
勾芒 committed
370 371 372 373 374 375 376
    } WithFailureBlock:^(id error) {
        
        NSLog(@"写入客户访问时间失败");
    }];
}


勾芒's avatar
勾芒 committed
377
#pragma mark -添加客户信息、或者退出当前客户
曹云霄's avatar
曹云霄 committed
378 379 380
- (IBAction)addCustomerInformationORExit:(UIButton *)sender {
    
    if ([sender.currentTitle isEqualToString:@"退出当前客户"]) {
勾芒's avatar
勾芒 committed
381
       
勾芒's avatar
勾芒 committed
382
        [self ExitCurrentCustomer];
曹云霄's avatar
曹云霄 committed
383 384 385
        [sender setTitle:@"添加客户信息" forState:UIControlStateNormal];
        return;
    }
勾芒's avatar
勾芒 committed
386
    [self addCustomerInformation:NO];
勾芒's avatar
勾芒 committed
387
    
曹云霄's avatar
曹云霄 committed
388 389
}

勾芒's avatar
勾芒 committed
390 391 392 393 394 395 396 397 398 399 400 401 402
#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
403
    [Customermanager manager].model = nil;
勾芒's avatar
勾芒 committed
404
    self.customerHeader.image = TCImage(@"now");
勾芒's avatar
勾芒 committed
405 406 407 408 409 410 411 412 413 414 415 416 417
    [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
418

勾芒's avatar
勾芒 committed
419
#pragma mark -添加客户信息请求 -新增、或者修改 ischange为真是新增
勾芒's avatar
勾芒 committed
420
- (void)addCustomerInformation:(BOOL)isChange
曹云霄's avatar
曹云霄 committed
421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440
{
    [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
441
    MyclientEntityModel *customerEntity = [[MyclientEntityModel alloc]init];
442
    customerEntity.createName = [Shoppersmanager manager].Shoppers.employee.realName;
曹云霄's avatar
曹云霄 committed
443 444
    customerEntity.mobile = self.phoneNumberField.text;
    customerEntity.address = self.customerAddress.text;
445
    customerEntity.name = self.customerNameField.text;
446
    customerEntity.company = self.companyNameField.text;
勾芒's avatar
勾芒 committed
447
    customerEntity.picture = [Customermanager manager].model.picture;
勾芒's avatar
勾芒 committed
448 449
    //修改
    if (isChange) {
勾芒's avatar
勾芒 committed
450
       customerEntity.fid = [Customermanager manager].model.fid;
勾芒's avatar
勾芒 committed
451
    }
曹云霄's avatar
曹云霄 committed
452 453 454 455 456 457 458
    
    [self CreateMBProgressHUDLoding];
    [[NetworkRequestClassManager Manager] NetworkRequestWithURL:[NSString stringWithFormat:@"%@%@",ServerAddress,@"/consumer/save"] WithRequestType:0 WithParameter:customerEntity WithReturnValueBlock:^(id returnValue) {
        
        [self RemoveMBProgressHUDLoding];
        if ([returnValue[@"code"] isEqualToNumber:@0]) {
            
勾芒's avatar
勾芒 committed
459 460
            //进入刷新状态
            [self.informationTableview.mj_header beginRefreshing];
勾芒's avatar
勾芒 committed
461 462
            //新增保存ID
            if (!isChange) {
勾芒's avatar
勾芒 committed
463
                [Customermanager manager].model.fid = returnValue[@"data"];
勾芒's avatar
勾芒 committed
464 465 466 467 468 469
                [self SuccessMBProgressView:@"新增成功"];
            }else
            {
                [self SuccessMBProgressView:@"修改成功"];
                [self.changePersonInformationButton setTitle:@"更改客户信息" forState:UIControlStateNormal];
            }
勾芒's avatar
勾芒 committed
470
            //保存客户信息
勾芒's avatar
勾芒 committed
471
            [Customermanager manager].model = customerEntity;
勾芒's avatar
勾芒 committed
472
            [Shoppersmanager manager].currentCustomer = YES;
曹云霄's avatar
曹云霄 committed
473 474 475 476 477 478
            [self.addpersonInformationButton setTitle:@"退出当前客户" forState:UIControlStateNormal];
            self.changePersonInformationButton.hidden = NO;
            self.customerNameField.enabled = NO;
            self.customerAddress.enabled = NO;
            self.phoneNumberField.enabled = NO;
            self.companyNameField.enabled = NO;
勾芒's avatar
勾芒 committed
479 480 481 482
            
            [self SetupUserShoppingCarNumberRequest];
            [self SetupUserRequest];
            [self ChangeCustomerName];
勾芒's avatar
勾芒 committed
483
            [self addAddressInformationRequest];
曹云霄's avatar
曹云霄 committed
484 485 486 487 488 489 490 491
        }
        else
        {
            [self ErrorMBProgressView:returnValue[@"message"]];
        }
        
    } WithErrorCodeBlock:^(id errorCodeValue) {
        
勾芒's avatar
勾芒 committed
492 493
        [self RemoveMBProgressHUDLoding];
        [self promptCustomerWithString:@"网络连接已断开"];
曹云霄's avatar
曹云霄 committed
494 495 496 497 498 499 500 501 502
        
    } WithFailureBlock:^(id error) {
        
        [self RemoveMBProgressHUDLoding];
        
    }];
}


勾芒's avatar
勾芒 committed
503 504 505 506
#pragma mark -新增客户成功写入默认地址信息
- (void)addAddressInformationRequest
{
    TOShippingAddrEntity *address = [[TOShippingAddrEntity alloc]init];
勾芒's avatar
勾芒 committed
507
    address.consumerId = [Customermanager manager].model.fid;
勾芒's avatar
勾芒 committed
508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533
    address.name = self.customerNameField.text;
    address.miblephone = self.phoneNumberField.text;
    address.address = self.customerAddress.text;
    
    [[NetworkRequestClassManager Manager] NetworkRequestWithURL:[NSString stringWithFormat:@"%@%@",ServerAddress,@"/shippingAddress/save"] WithRequestType:0 WithParameter:address WithReturnValueBlock:^(id returnValue) {
        
        if ([returnValue[@"code"] isEqualToNumber:@0]) {
            
            NSLog(@"写入默认收货地址成功");
        }
        else
        {
            [self ErrorMBProgressView:returnValue[@"message"]];
        }
        
    } WithErrorCodeBlock:^(id errorCodeValue) {
        
        [self promptCustomerWithString:@"网络连接已断开"];
        
    } WithFailureBlock:^(id error) {
        
        NSLog(@"写入默认收货地址失败");
    }];
}


曹云霄's avatar
曹云霄 committed
534 535 536 537
#pragma mark -更改客户信息
- (IBAction)changeCustomerInformation:(UIButton *)sender {
    
    
勾芒's avatar
勾芒 committed
538 539 540 541 542 543 544 545 546 547 548 549
    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
550 551 552 553 554 555 556
    self.customerNameField.enabled = YES;
    self.customerAddress.enabled = YES;
    self.phoneNumberField.enabled = YES;
    self.companyNameField.enabled = YES;
}


勾芒's avatar
勾芒 committed
557

曹云霄's avatar
曹云霄 committed
558 559 560
#pragma mark -UITextFieldDelegate代理 ----搜索
- (BOOL)textFieldShouldReturn:(UITextField *)textField
{
曹云霄's avatar
曹云霄 committed
561
    [textField resignFirstResponder];
曹云霄's avatar
曹云霄 committed
562 563 564 565 566 567 568 569 570 571 572 573 574 575 576
    [self searchCustomerInforMation];
    return YES;
}


#pragma amrk -搜索客户信息
- (void)searchCustomerInforMation
{
    if (self.searchPersonInformationField.text.length == 0) {
        
        [self ErrorMBProgressView:@"搜索信息不能为空"];
        return;
    }
    [self CreateMBProgressHUDLoding];
    ConsumerQueryCondition *searchCustomer = [[ConsumerQueryCondition alloc]init];
曹云霄's avatar
曹云霄 committed
577
    DataPage *page = [[DataPage alloc]init];
勾芒's avatar
勾芒 committed
578 579
    page.page = 1;
    page.rows = 10;
曹云霄's avatar
曹云霄 committed
580
    searchCustomer.page = page;
曹云霄's avatar
曹云霄 committed
581 582
    searchCustomer.nameEquals = self.searchPersonInformationField.text;
    searchCustomer.mobileEquals = self.searchPersonInformationField.text;
勾芒's avatar
勾芒 committed
583
    
曹云霄's avatar
曹云霄 committed
584 585 586 587 588 589
    //request
    [[NetworkRequestClassManager Manager] NetworkRequestWithURL:[NSString stringWithFormat:@"%@%@",ServerAddress,@"/consumer/query"] WithRequestType:0 WithParameter:searchCustomer WithReturnValueBlock:^(id returnValue) {
        
        [self RemoveMBProgressHUDLoding];
        if ([returnValue[@"code"] isEqualToNumber:@0]) {
            
勾芒's avatar
勾芒 committed
590
            ConsumerPageResult *result = [[ConsumerPageResult alloc]initWithDictionary:returnValue[@"data"] error:nil];
勾芒's avatar
勾芒 committed
591
            [self.CustomerresultArray removeAllObjects];
勾芒's avatar
勾芒 committed
592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609
            for (TOConsumerEntity *objc in result.results) {
                
                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;
                
                [self.CustomerresultArray addObject:myclientModel];
勾芒's avatar
勾芒 committed
610 611
            }
            [self.informationTableview reloadData];
曹云霄's avatar
曹云霄 committed
612 613 614
        }
        else
        {
勾芒's avatar
勾芒 committed
615
            [self ErrorMBProgressView:returnValue[@"message"]];
曹云霄's avatar
曹云霄 committed
616 617 618 619
        }
        
    } WithErrorCodeBlock:^(id errorCodeValue) {
        
勾芒's avatar
勾芒 committed
620 621 622
        [self RemoveMBProgressHUDLoding];
        [self promptCustomerWithString:@"网络连接已断开"];
        
曹云霄's avatar
曹云霄 committed
623 624
    } WithFailureBlock:^(id error) {
        
勾芒's avatar
勾芒 committed
625
        [self RemoveMBProgressHUDLoding];
曹云霄's avatar
曹云霄 committed
626
    }];
勾芒's avatar
勾芒 committed
627 628 629 630 631 632
}


#pragma mark -键盘落下,判断更改信息按钮是否可见,可见则取消输入框的响应
- (void)KeyboadrDismiss
{
勾芒's avatar
勾芒 committed
633
    if (!self.changePersonInformationButton.hidden) {
勾芒's avatar
勾芒 committed
634 635 636 637 638 639
     
        self.customerNameField.enabled = NO;
        self.customerAddress.enabled = NO;
        self.phoneNumberField.enabled = NO;
        self.companyNameField.enabled = NO;
    }
曹云霄's avatar
曹云霄 committed
640
}
曹云霄's avatar
曹云霄 committed
641 642


曹云霄's avatar
曹云霄 committed
643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658
- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end