ClientViewController.m 13.6 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
@end

@implementation ClientViewController

曹云霄's avatar
曹云霄 committed
25 26 27 28 29 30 31 32 33 34 35 36 37 38

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


曹云霄's avatar
曹云霄 committed
39 40 41
- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
曹云霄's avatar
曹云霄 committed
42
    
曹云霄's avatar
曹云霄 committed
43
    [self uiConfigAction];
曹云霄's avatar
曹云霄 committed
44
    [self getShoppersAssociatedCustomer];
曹云霄's avatar
曹云霄 committed
45 46
}

47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68

- (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
69 70 71
#pragma mark -布局
- (void)uiConfigAction
{
曹云霄's avatar
曹云霄 committed
72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89
    //圆角
    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;
    
    //隐藏更改客户按钮
    self.changePersonInformationButton.hidden = YES;
    
    //代理
曹云霄's avatar
曹云霄 committed
90
    self.informationTableview.delegate = self;
曹云霄's avatar
曹云霄 committed
91
    self.informationTableview.dataSource = self;
勾芒's avatar
勾芒 committed
92
    self.informationTableview.tableFooterView = [UIView new];
曹云霄's avatar
曹云霄 committed
93
    self.searchPersonInformationField.delegate = self;
勾芒's avatar
勾芒 committed
94 95 96
    //监听键盘落下的通知
    [[NSNotificationCenter defaultCenter]addObserver:self selector:@selector(KeyboadrDismiss) name:UIKeyboardWillHideNotification object:nil];
    
曹云霄's avatar
曹云霄 committed
97 98
}

曹云霄's avatar
曹云霄 committed
99

曹云霄's avatar
曹云霄 committed
100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115

#pragma mark -获取导购关联客户
- (void)getShoppersAssociatedCustomer
{
    
    [self CreateMBProgressHUDLoding];
    ConsumerQueryCondition *condition = [[ConsumerQueryCondition alloc]init];
    DataPage *page = [[DataPage alloc]init];
    page.page = 0;
    condition.page = page;
    [[NetworkRequestClassManager Manager] NetworkRequestWithURL:[NSString stringWithFormat:@"%@%@",ServerAddress,@"/consumer/query"] WithRequestType:0 WithParameter:condition WithReturnValueBlock:^(id returnValue) {
        
        [self RemoveMBProgressHUDLoding];
        if ([returnValue[@"code"] isEqualToNumber:@0]) {
            
            ConsumerPageResult *Customerresult = [[ConsumerPageResult alloc]initWithDictionary:returnValue[@"data"] error:nil];
勾芒's avatar
勾芒 committed
116
            for (TOConsumerEntity *objc in Customerresult.results) {
曹云霄's avatar
曹云霄 committed
117
                
勾芒's avatar
勾芒 committed
118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133
                 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
134
            }
勾芒's avatar
勾芒 committed
135
            
曹云霄's avatar
曹云霄 committed
136 137 138 139 140 141 142 143 144 145 146 147
            [self.informationTableview reloadData];
        }
        else
        {
            [self ErrorMBProgressView:returnValue[@"message"]];
        }
        
    } WithErrorCodeBlock:^(id errorCodeValue) {
        
        
    } WithFailureBlock:^(id error) {
        
勾芒's avatar
勾芒 committed
148
        [self RemoveMBProgressHUDLoding];
曹云霄's avatar
曹云霄 committed
149 150 151 152
    }];
}


曹云霄's avatar
曹云霄 committed
153 154 155
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    InformationTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"information" forIndexPath:indexPath];
曹云霄's avatar
曹云霄 committed
156
    cell.selectionStyle = UITableViewCellSelectionStyleNone;
勾芒's avatar
勾芒 committed
157
    cell.indexNumber = indexPath.row;
曹云霄's avatar
曹云霄 committed
158
    cell.model = [self.CustomerresultArray objectAtIndex_opple:indexPath.row];
勾芒's avatar
勾芒 committed
159 160 161 162 163
    //清空其他选中状态
    [cell setBlockSeletced:^(NSInteger index) {
        
        [self SetCustomerButtonClick:index];
    }];
曹云霄's avatar
曹云霄 committed
164
    return cell;
曹云霄's avatar
曹云霄 committed
165 166
}

曹云霄's avatar
曹云霄 committed
167 168
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
曹云霄's avatar
曹云霄 committed
169
    return self.CustomerresultArray.count;
曹云霄's avatar
曹云霄 committed
170
}
曹云霄's avatar
曹云霄 committed
171

曹云霄's avatar
曹云霄 committed
172
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
曹云霄's avatar
曹云霄 committed
173
{
曹云霄's avatar
曹云霄 committed
174
    return 100;
曹云霄's avatar
曹云霄 committed
175 176
}

曹云霄's avatar
曹云霄 committed
177

曹云霄's avatar
曹云霄 committed
178 179 180 181
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    UIStoryboard *storyboard = [UIStoryboard storyboardWithName:@"StoryboardwithCYX" bundle:nil];
    ClientdetailsViewController *clientdetails = [storyboard instantiateViewControllerWithIdentifier:@"clientdetails"];
勾芒's avatar
勾芒 committed
182
    clientdetails.model = [self.CustomerresultArray objectAtIndex_opple:indexPath.row];
曹云霄's avatar
曹云霄 committed
183 184
    [self.navigationController pushViewController:clientdetails animated:YES];
}
曹云霄's avatar
曹云霄 committed
185 186


勾芒's avatar
勾芒 committed
187 188 189

#pragma mark -设置为当前客户回调
- (void)SetCustomerButtonClick:(NSInteger)index;
曹云霄's avatar
曹云霄 committed
190
{
勾芒's avatar
勾芒 committed
191
    
曹云霄's avatar
曹云霄 committed
192 193 194 195
    for (InformationTableViewCell *cell in self.informationTableview.visibleCells) {
        
        cell.setCurrentCustomer.selected = NO;
    }
勾芒's avatar
勾芒 committed
196 197 198 199 200 201
    for (MyclientEntityModel *model in self.CustomerresultArray) {
        
        model.selectedState = NO;
    }
    MyclientEntityModel *model = [self.CustomerresultArray objectAtIndex_opple:index];
    model.selectedState = YES;
曹云霄's avatar
曹云霄 committed
202
    
勾芒's avatar
勾芒 committed
203 204 205
    self.customerNameField.text = model.name;
    self.phoneNumberField.text = model.mobile;
    self.customerAddress.text = model.address;
勾芒's avatar
勾芒 committed
206
    //保存客户信息
勾芒's avatar
勾芒 committed
207 208
    [Shoppersmanager manager].currentCustomer = YES;
    [Customermanager manager].customerID = model.fid;
勾芒's avatar
勾芒 committed
209 210 211 212
    [Customermanager manager].customerName = model.name;
    [Customermanager manager].customerPhoneNumber = model.mobile;
    [Customermanager manager].companyAddress = model.address;
    [Customermanager manager].cutomerAddress = model.address;
勾芒's avatar
勾芒 committed
213 214
    [self.addpersonInformationButton setTitle:@"退出当前客户" forState:UIControlStateNormal];
    self.changePersonInformationButton.hidden = NO;
曹云霄's avatar
曹云霄 committed
215
}
曹云霄's avatar
曹云霄 committed
216

曹云霄's avatar
曹云霄 committed
217

勾芒's avatar
勾芒 committed
218 219


曹云霄's avatar
曹云霄 committed
220
#pragma mark -添加客户信息
曹云霄's avatar
曹云霄 committed
221 222 223 224 225 226 227 228
- (IBAction)addCustomerInformationORExit:(UIButton *)sender {
    
    if ([sender.currentTitle isEqualToString:@"退出当前客户"]) {
      
        self.customerNameField.text = nil;
        self.customerAddress.text = nil;
        self.phoneNumberField.text = nil;
        self.companyNameField.text = nil;
勾芒's avatar
勾芒 committed
229 230 231 232
        self.customerNameField.enabled = YES;
        self.customerAddress.enabled = YES;
        self.phoneNumberField.enabled = YES;
        self.companyNameField.enabled = YES;
曹云霄's avatar
曹云霄 committed
233
        self.changePersonInformationButton.hidden = YES;
勾芒's avatar
勾芒 committed
234 235
        [Shoppersmanager manager].currentCustomer = NO;
        [Customermanager manager].customerID = nil;
勾芒's avatar
勾芒 committed
236 237 238 239 240 241 242 243 244 245
        
        //全部为为选中状态
        for (InformationTableViewCell *cell in self.informationTableview.visibleCells) {
            
            cell.setCurrentCustomer.selected = NO;
        }
        for (MyclientEntityModel *model in self.CustomerresultArray) {
            
            model.selectedState = NO;
        }
曹云霄's avatar
曹云霄 committed
246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 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 287
        [sender setTitle:@"添加客户信息" forState:UIControlStateNormal];
        return;
    }
    [self addCustomerInformation];
}


#pragma mark -添加客户信息请求
- (void)addCustomerInformation
{
    [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;
    }
    
    //客户信息类
    TOConsumerEntity *customerEntity = [[TOConsumerEntity alloc]init];
    customerEntity.createName = self.customerNameField.text;
    customerEntity.mobile = self.phoneNumberField.text;
    customerEntity.address = self.customerAddress.text;
    customerEntity.name = self.companyNameField.text;
    
    [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
288
            //保存客户信息
曹云霄's avatar
曹云霄 committed
289
            [Customermanager manager].customerID = returnValue[@"data"];
勾芒's avatar
勾芒 committed
290 291 292 293 294
            [Customermanager manager].customerName = customerEntity.name;
            [Customermanager manager].customerPhoneNumber = customerEntity.mobile;
            [Customermanager manager].companyAddress = customerEntity.address;
            [Customermanager manager].cutomerAddress = customerEntity.address;
            
勾芒's avatar
勾芒 committed
295
            [Shoppersmanager manager].currentCustomer = YES;
曹云霄's avatar
曹云霄 committed
296
            [self SuccessMBProgressView:returnValue[@"新增成功"]];
曹云霄's avatar
曹云霄 committed
297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332
            [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;
        }
        else
        {
            [self ErrorMBProgressView:returnValue[@"message"]];
        }
        
    } WithErrorCodeBlock:^(id errorCodeValue) {
        
        
        
    } WithFailureBlock:^(id error) {
        
        [self RemoveMBProgressHUDLoding];
        
    }];
}


#pragma mark -更改客户信息
- (IBAction)changeCustomerInformation:(UIButton *)sender {
    
    
    self.customerNameField.enabled = YES;
    self.customerAddress.enabled = YES;
    self.phoneNumberField.enabled = YES;
    self.companyNameField.enabled = YES;
    [self.customerNameField becomeFirstResponder];
}


勾芒's avatar
勾芒 committed
333

曹云霄's avatar
曹云霄 committed
334 335 336
#pragma mark -UITextFieldDelegate代理 ----搜索
- (BOOL)textFieldShouldReturn:(UITextField *)textField
{
曹云霄's avatar
曹云霄 committed
337
    [textField resignFirstResponder];
曹云霄's avatar
曹云霄 committed
338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353
    [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
354 355 356
    DataPage *page = [[DataPage alloc]init];
    page.page = 0;
    searchCustomer.page = page;
曹云霄's avatar
曹云霄 committed
357 358 359 360 361 362 363 364 365 366 367 368 369
    searchCustomer.nameEquals = self.searchPersonInformationField.text;
    searchCustomer.mobileEquals = self.searchPersonInformationField.text;
    //request
    [[NetworkRequestClassManager Manager] NetworkRequestWithURL:[NSString stringWithFormat:@"%@%@",ServerAddress,@"/consumer/query"] WithRequestType:0 WithParameter:searchCustomer WithReturnValueBlock:^(id returnValue) {
        
        [self RemoveMBProgressHUDLoding];
        if ([returnValue[@"code"] isEqualToNumber:@0]) {
            
            ConsumerPageResult *result = [[ConsumerPageResult alloc]initWithDictionary:returnValue error:nil];
            NSLog(@"%@",result);
        }
        else
        {
勾芒's avatar
勾芒 committed
370
            [self ErrorMBProgressView:returnValue[@"message"]];
曹云霄's avatar
曹云霄 committed
371 372 373 374 375 376
        }
        
    } WithErrorCodeBlock:^(id errorCodeValue) {
        
    } WithFailureBlock:^(id error) {
        
勾芒's avatar
勾芒 committed
377
        [self RemoveMBProgressHUDLoding];
曹云霄's avatar
曹云霄 committed
378
    }];
勾芒's avatar
勾芒 committed
379 380 381 382 383 384 385 386 387 388 389 390 391
}


#pragma mark -键盘落下,判断更改信息按钮是否可见,可见则取消输入框的响应
- (void)KeyboadrDismiss
{
    if (!self.changePersonInformationButton.selected) {
     
        self.customerNameField.enabled = NO;
        self.customerAddress.enabled = NO;
        self.phoneNumberField.enabled = NO;
        self.companyNameField.enabled = NO;
    }
曹云霄's avatar
曹云霄 committed
392
}
曹云霄's avatar
曹云霄 committed
393 394


曹云霄's avatar
曹云霄 committed
395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410
- (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