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

#import "AllCustomerViewController.h"
#import "AllCutomerTableViewCell.h"
勾芒's avatar
勾芒 committed
11 12
#import "DateSelectedViewController.h"
#import "MyclientEntityModel.h"
勾芒's avatar
勾芒 committed
13 14
#import "ClientdetailsViewController.h"

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

勾芒's avatar
勾芒 committed
17 18 19 20
/**
 *  数据源
 */
@property (nonatomic,strong) NSMutableArray *CustomerresultArray;
勾芒's avatar
勾芒 committed
21 22 23 24 25 26 27 28 29 30

/**
 *  当前页数
 */
@property (nonatomic,assign) int indexPage;

/**
 *  总页数
 */
@property (nonatomic,assign) int totalPages;
31

32

33

曹云霄's avatar
曹云霄 committed
34 35 36 37
@end

@implementation AllCustomerViewController

勾芒's avatar
勾芒 committed
38 39 40 41 42 43 44 45 46 47 48 49 50

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

51 52 53 54 55 56 57 58 59 60 61 62
/**
 *  初始化参数模型
 */
- (ConsumerQueryCondition *)conditionModel
{
    if (!_conditionModel) {
        
        _conditionModel = [[ConsumerQueryCondition alloc]init];
    }
    return _conditionModel;
}

勾芒's avatar
勾芒 committed
63

曹云霄's avatar
曹云霄 committed
64 65 66 67 68 69 70
- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    
    [self uiConfigAction];
}

勾芒's avatar
勾芒 committed
71

曹云霄's avatar
曹云霄 committed
72 73 74
#pragma mark -UI
- (void)uiConfigAction
{
75
    self.indexPage = ONE;
曹云霄's avatar
曹云霄 committed
76 77
    self.allCustomerTableview.dataSource = self;
    self.allCustomerTableview.delegate = self;
勾芒's avatar
勾芒 committed
78
    self.searchTextfield.delegate = self;
勾芒's avatar
勾芒 committed
79 80
    self.view.backgroundColor = kMainGrayColor;
    self.allCustomerTableview.backgroundColor = [UIColor clearColor];
勾芒's avatar
勾芒 committed
81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109
    self.searchTextfield.returnKeyType = UIReturnKeySearch;
    
    //调整布局
    [self.begindateButton mas_makeConstraints:^(MASConstraintMaker *make) {
        
        make.left.equalTo(self.RecentlydateLabe.mas_right).mas_offset(0);
        make.top.equalTo(self.RecentlydateLabe).mas_offset(-4);
        make.width.mas_offset(120);
    }];
    [self.redesignLabe mas_makeConstraints:^(MASConstraintMaker *make) {
        
        make.left.equalTo(self.begindateButton.mas_right).mas_offset(10);
        make.top.equalTo(self.RecentlydateLabe).mas_offset(2);
        make.width.mas_offset(20);
    }];
    [self.enddateButton mas_makeConstraints:^(MASConstraintMaker *make) {
        
        make.left.equalTo(self.redesignLabe.mas_right).mas_offset(10);
        make.top.equalTo(self.RecentlydateLabe).mas_offset(-4);
        make.width.mas_offset(120);
    }];
    
    //设置按钮时间
    [self.begindateButton setTitle:[self dateAsString:[NSDate date]] forState:UIControlStateNormal];
    [self.enddateButton setTitle:[self dateAsString:[NSDate date]] forState:UIControlStateNormal];
    self.begindateButton.layer.masksToBounds = YES;
    self.begindateButton.layer.cornerRadius = 10;
    self.enddateButton.layer.masksToBounds = YES;
    self.enddateButton.layer.cornerRadius = 10;
勾芒's avatar
勾芒 committed
110 111 112 113
    
    //下拉刷新
    MjRefreshHeaderCustom *headerRefresh = [MjRefreshHeaderCustom headerWithRefreshingBlock:^{
        DataPage *page = [[DataPage alloc]init];
114
        page.page = ONE;
勾芒's avatar
勾芒 committed
115
        page.rows = KROWS;
116
        self.conditionModel.page = page;
勾芒's avatar
勾芒 committed
117
        [self.allCustomerTableview.mj_footer resetNoMoreData];
曹云霄's avatar
曹云霄 committed
118 119
        //经销商代码
        self.conditionModel.resellerCodeEquals = [[Shoppersmanager manager].Shoppers.employee.currentDepart  orgCode];
120
        [self getShoppersAssociatedCustomer:self.conditionModel isRemove:YES];
勾芒's avatar
勾芒 committed
121 122 123 124 125 126 127 128
    }];
    headerRefresh.stateLabel.hidden = YES;
    headerRefresh.lastUpdatedTimeLabel.hidden = YES;
    self.allCustomerTableview.mj_header = headerRefresh;
    [self.allCustomerTableview.mj_header beginRefreshing];
    //上拉加载
    self.allCustomerTableview.mj_footer = [MJRefreshAutoNormalFooter footerWithRefreshingBlock:^{
        
勾芒's avatar
勾芒 committed
129
        if ( ++ self.indexPage > self.totalPages) {
勾芒's avatar
勾芒 committed
130 131 132
            [self.allCustomerTableview.mj_footer endRefreshingWithNoMoreData];
        }else
        {
133 134
            DataPage *page = [[DataPage alloc]init];
            page.rows = KROWS;
135
            page.page = self.indexPage;
136 137
            self.conditionModel.page = page;
            [self getShoppersAssociatedCustomer:self.conditionModel isRemove:NO];
勾芒's avatar
勾芒 committed
138 139
        }
    }];
曹云霄's avatar
曹云霄 committed
140
    self.allCustomerTableview.mj_footer.automaticallyHidden = YES;
曹云霄's avatar
曹云霄 committed
141 142 143
}


勾芒's avatar
勾芒 committed
144 145

#pragma mark -获取导购关联客户
勾芒's avatar
勾芒 committed
146
- (void)getShoppersAssociatedCustomer:(ConsumerQueryCondition *)condition isRemove:(BOOL)remove
勾芒's avatar
勾芒 committed
147
{
曹云霄's avatar
曹云霄 committed
148
    [XBLoadingView showHUDViewWithDefault];
曹云霄's avatar
曹云霄 committed
149
    WS(weakSelf);
150
    [[NetworkRequestClassManager Manager] NetworkRequestWithURL:SERVERREQUESTURL(GETSHOPPERSCONSUMER)  WithRequestType:ZERO WithParameter:condition WithReturnValueBlock:^(id returnValue) {
勾芒's avatar
勾芒 committed
151
        
曹云霄's avatar
曹云霄 committed
152 153
        weakSelf.allCustomerTableview.emptyDataSetSource = weakSelf;
        weakSelf.allCustomerTableview.emptyDataSetDelegate = weakSelf;
曹云霄's avatar
曹云霄 committed
154
        [XBLoadingView hideHUDViewWithDefault];
曹云霄's avatar
曹云霄 committed
155
        [weakSelf endRefreshingForTableView:self.allCustomerTableview];
勾芒's avatar
勾芒 committed
156
        if ([returnValue[@"code"] isEqualToNumber:@0]) {
勾芒's avatar
勾芒 committed
157
            if (remove) {
曹云霄's avatar
曹云霄 committed
158
                [weakSelf.CustomerresultArray removeAllObjects];
勾芒's avatar
勾芒 committed
159
            }
勾芒's avatar
勾芒 committed
160
            ConsumerPageResult *Customerresult = [[ConsumerPageResult alloc]initWithDictionary:returnValue[@"data"] error:nil];
曹云霄's avatar
曹云霄 committed
161
            weakSelf.totalPages = [returnValue[@"data"][@"totalpages"] intValue];
勾芒's avatar
勾芒 committed
162 163 164 165 166 167 168 169 170 171 172 173 174 175 176
            for (TOConsumerEntity *objc in Customerresult.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;
勾芒's avatar
勾芒 committed
177
                myclientModel.company = objc.company;
勾芒's avatar
勾芒 committed
178 179
                myclientModel.lastVisitedTime = objc.lastVisitedTime;
                
曹云霄's avatar
曹云霄 committed
180
                [weakSelf.CustomerresultArray addObject:myclientModel];
勾芒's avatar
勾芒 committed
181 182
            }
            
曹云霄's avatar
曹云霄 committed
183
            [weakSelf.allCustomerTableview reloadData];
勾芒's avatar
勾芒 committed
184 185 186
        }
        else
        {
曹云霄's avatar
曹云霄 committed
187
            [XBLoadingView showHUDViewWithText:returnValue[@"message"]];
勾芒's avatar
勾芒 committed
188
        }
189
    }WithFailureBlock:^(id error) {
勾芒's avatar
勾芒 committed
190
        
曹云霄's avatar
曹云霄 committed
191
        [weakSelf endRefreshingForTableView:weakSelf.allCustomerTableview];
曹云霄's avatar
曹云霄 committed
192
        [XBLoadingView hideHUDViewWithDefault];
勾芒's avatar
勾芒 committed
193 194 195 196 197
    }];
}



曹云霄's avatar
曹云霄 committed
198 199 200 201 202 203
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    AllCutomerTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"allcustomercell" forIndexPath:indexPath];
    cell.contentBackView.layer.masksToBounds = YES;
    cell.contentBackView.layer.cornerRadius = kCornerRadius;
    cell.backgroundColor = [UIColor clearColor];
勾芒's avatar
勾芒 committed
204 205
    cell.Model = [self.CustomerresultArray objectAtIndex_opple:indexPath.row];
    
曹云霄's avatar
曹云霄 committed
206 207 208 209 210
    return cell;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
勾芒's avatar
勾芒 committed
211
    return self.CustomerresultArray.count;
曹云霄's avatar
曹云霄 committed
212 213 214
}
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
勾芒's avatar
勾芒 committed
215
    return 125;
曹云霄's avatar
曹云霄 committed
216 217
}

勾芒's avatar
勾芒 committed
218 219
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
曹云霄's avatar
曹云霄 committed
220
    ClientdetailsViewController *clientdetails = [[[self class] getMainStoryboardClass] instantiateViewControllerWithIdentifier:@"clientdetails"];
勾芒's avatar
勾芒 committed
221 222
    clientdetails.model = [self.CustomerresultArray objectAtIndex_opple:indexPath.row];
    clientdetails.cellindex = indexPath.row;
勾芒's avatar
勾芒 committed
223
    //设置当前客户
曹云霄's avatar
曹云霄 committed
224
    WS(weakSelf);
勾芒's avatar
勾芒 committed
225 226 227 228 229 230
    [clientdetails setCurrentUserBlock:^(NSInteger index,NSString *title) {
        
        NSString *customerID = [[weakSelf.CustomerresultArray objectAtIndex_opple:indexPath.row] fid];
        NSDictionary *dict = @{@"customerid":customerID,@"title":title,@"model":[weakSelf.CustomerresultArray objectAtIndex_opple:indexPath.row]};
        [[NSNotificationCenter defaultCenter] postNotificationName:@"SETUPCURRENTCUSTOMER" object:dict];
    }];
勾芒's avatar
勾芒 committed
231 232 233
    [self.navigationController pushViewController:clientdetails animated:YES];
}

曹云霄's avatar
曹云霄 committed
234 235


勾芒's avatar
勾芒 committed
236
#pragma mark -开始筛选时间
237
- (IBAction)StartScreeningButtonClick:(UIButton *)sender {
勾芒's avatar
勾芒 committed
238 239 240 241 242 243 244 245 246 247 248 249 250 251
    
    DateSelectedViewController *datevc = [[DateSelectedViewController alloc]init];
    //选中时间回调
    [datevc setSelectedDateBlock:^(NSDate *selectedDate) {
        
        [self.begindateButton setTitle:[self dateAsString:selectedDate] forState:UIControlStateNormal];
        [self CalltimeSearch];
    }];
    
    datevc.preferredContentSize = CGSizeMake(300, 250);
    datevc.modalPresentationStyle = UIModalPresentationFormSheet;
    UIPopoverPresentationController *pop = datevc.popoverPresentationController;
    pop.permittedArrowDirections = UIPopoverArrowDirectionAny;
    pop.sourceView = datevc.view;
曹云霄's avatar
曹云霄 committed
252 253 254
    dispatch_async(dispatch_get_main_queue(), ^{
        [self presentViewController:datevc animated:YES completion:nil];
    });
勾芒's avatar
勾芒 committed
255 256 257 258 259 260 261 262 263 264 265 266 267 268
}


#pragma mark -时间转换NSDate转NSString
- (NSString*)dateAsString:(NSDate*)date {
    
    NSDateFormatter *formatter=[[NSDateFormatter alloc] init];
    [formatter setDateFormat:@"yyyy-MM-dd"];
    NSString * timeString = [formatter stringFromDate:date];
    return timeString;
    
}

#pragma mark -结束筛选时间
269
- (IBAction)EndScreeningButtonClick:(UIButton *)sender {
勾芒's avatar
勾芒 committed
270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294
    
    
    DateSelectedViewController *datevc = [[DateSelectedViewController alloc]init];
    //选中时间回调
    [datevc setSelectedDateBlock:^(NSDate *selectedDate) {
        
        [self.enddateButton setTitle:[self dateAsString:selectedDate] forState:UIControlStateNormal];
        [self CalltimeSearch];
        
    }];
    datevc.preferredContentSize = CGSizeMake(300, 250);
    datevc.modalPresentationStyle = UIModalPresentationFormSheet;
    UIPopoverPresentationController *pop = datevc.popoverPresentationController;
    pop.permittedArrowDirections = UIPopoverArrowDirectionAny;
    pop.sourceView = datevc.view;
    [self presentViewController:datevc animated:YES completion:nil];
}


#pragma mark -调用时间段搜索
- (void)CalltimeSearch
{
    NSString *startString = self.begindateButton.currentTitle;
    NSString *endString = self.enddateButton.currentTitle;
    NSDateFormatter* formater = [[NSDateFormatter alloc] init];
295
    NSDateFormatter* endformater = [[NSDateFormatter alloc] init];
勾芒's avatar
勾芒 committed
296 297 298 299 300 301 302 303 304 305 306
    [formater setDateFormat:@"yyyy-MM-dd"];
    NSDate* date = [formater dateFromString:startString];
    [formater setDateFormat:@"yyyy-MM-dd"];
    NSDate* date1 = [formater dateFromString:endString];
    
    //比较结果
    NSInteger result = [self compareOneDay:date withAnotherDay:date1];
    //比较两个NSDate的大小
    switch (result) {
        case -1://start < end
        {
307 308 309 310
            [formater setDateFormat:@"yyyy-MM-dd 00:00:01"];
            [endformater setDateFormat:@"yyyy-MM-dd 23:59:59"];
            self.conditionModel.createTimeBegin = [formater stringFromDate:date];
            self.conditionModel.createTimeEnd = [endformater stringFromDate:date1];
勾芒's avatar
勾芒 committed
311 312 313 314 315
        }
            break;
        case 0://start == end
        {
            [formater setDateFormat:@"yyyy-MM-dd HH:dd:ss"];
316 317
            self.conditionModel.createTimeBegin = nil;
            self.conditionModel.createTimeEnd = nil;
勾芒's avatar
勾芒 committed
318 319 320 321
        }
            break;
        case 1://start > end
        {
322 323 324 325
            [formater setDateFormat:@"yyyy-MM-dd 00:00:01"];
            [endformater setDateFormat:@"yyyy-MM-dd 23:59:59"];
            self.conditionModel.createTimeBegin = [formater stringFromDate:date1];
            self.conditionModel.createTimeEnd = [endformater stringFromDate:date];
勾芒's avatar
勾芒 committed
326 327 328 329 330 331
        }
            break;
            
        default:
            break;
    }
332 333 334 335
    self.conditionModel.nameEquals = nil;
    self.conditionModel.mobileEquals = nil;
    self.searchTextfield.text = nil;
    [self.allCustomerTableview.mj_header beginRefreshing];
勾芒's avatar
勾芒 committed
336 337
}

曹云霄's avatar
曹云霄 committed
338

勾芒's avatar
勾芒 committed
339 340


勾芒's avatar
勾芒 committed
341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368
#pragma mark -比较两个NSDate的大小
- (int)compareOneDay:(NSDate *)oneDay withAnotherDay:(NSDate *)anotherDay
{
    NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];
    [dateFormatter setDateFormat:@"yyyy-MM-dd"];
    NSString *oneDayStr = [dateFormatter stringFromDate:oneDay];
    NSString *anotherDayStr = [dateFormatter stringFromDate:anotherDay];
    NSDate *dateA = [dateFormatter dateFromString:oneDayStr];
    NSDate *dateB = [dateFormatter dateFromString:anotherDayStr];
    NSComparisonResult result = [dateA compare:dateB];
    NSLog(@"date1 : %@, date2 : %@", oneDay, anotherDay);
    if (result == NSOrderedDescending) {
        //NSLog(@"Date1  is in the future");
        return 1;
    }
    else if (result == NSOrderedAscending){
        //NSLog(@"Date1 is in the past");
        return -1;
    }
    //NSLog(@"Both dates are the same");
    return 0;
    
}

#pragma mark -Return键检测
- (BOOL)textFieldShouldReturn:(UITextField *)textField
{
    [textField resignFirstResponder];
369 370
    self.conditionModel.nameEquals = self.searchTextfield.text;
    self.conditionModel.mobileEquals = self.searchTextfield.text;
371
    if ([BaseViewController isBlankString:self.searchTextfield.text]) {
372 373
        self.conditionModel.nameEquals = nil;
        self.conditionModel.mobileEquals = nil;
勾芒's avatar
勾芒 committed
374
    }
375
    [self.allCustomerTableview.mj_header beginRefreshing];
勾芒's avatar
勾芒 committed
376 377 378
    return YES;
}

曹云霄's avatar
曹云霄 committed
379

曹云霄's avatar
曹云霄 committed
380 381 382 383 384 385 386 387 388 389 390 391 392 393 394
#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
395 396 397 398 399




@end