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

#import "AllCustomerViewController.h"
#import "AllCutomerTableViewCell.h"
#import "DateSelectedViewController.h"
#import "MyclientEntityModel.h"
#import "ClientdetailsViewController.h"

@interface AllCustomerViewController ()<UITableViewDelegate,UITableViewDataSource,UITextFieldDelegate,DZNEmptyDataSetSource,DZNEmptyDataSetDelegate>

/**
 *  数据源
 */
20
@property (nonatomic,strong) NSMutableArray *customerresultArray;
曹云霄's avatar
曹云霄 committed
21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37


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



@end

@implementation AllCustomerViewController


/**
 *  初始化客户数据源
 */
38
- (NSMutableArray *)customerresultArray
曹云霄's avatar
曹云霄 committed
39
{
40
    if (_customerresultArray == nil) {
曹云霄's avatar
曹云霄 committed
41
        
42
        _customerresultArray = [NSMutableArray array];
曹云霄's avatar
曹云霄 committed
43
    }
44
    return _customerresultArray;
曹云霄'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 69 70 71 72 73 74 75 76 77 78 79 80 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 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149
}

/**
 *  初始化参数模型
 */
- (ConsumerQueryCondition *)conditionModel
{
    if (!_conditionModel) {
        _conditionModel = [[ConsumerQueryCondition alloc]init];
        DataPage *page = [[DataPage alloc]init];
        page.page = ONE;
        page.rows = KROWS;
        _conditionModel.page = page;
        _conditionModel.resellerCodeEquals = [[Shoppersmanager manager].shoppers.employee.currentDepart  orgCode];
    }
    return _conditionModel;
}

#pragma mark -渲染完成
- (void)viewDidAppear:(BOOL)animated
{
    [super viewDidAppear:animated];
    self.navigationController.fd_fullscreenPopGestureRecognizer.enabled = NO;
    if ([self.navigationController respondsToSelector:@selector(interactivePopGestureRecognizer)]) {
        self.navigationController.interactivePopGestureRecognizer.enabled = NO;
    }
}

#pragma mark -视图即将消失
- (void)viewWillDisappear:(BOOL)animated
{
    [super viewWillDisappear:animated];
    // 开启
    self.navigationController.fd_fullscreenPopGestureRecognizer.enabled = YES;
    if ([self.navigationController respondsToSelector:@selector(interactivePopGestureRecognizer)]) {
        self.navigationController.interactivePopGestureRecognizer.enabled = YES;
    }
}

- (void)viewDidLoad {
    [super viewDidLoad];
    
    [self uiConfigAction];
    [self setUpRefreshAction];
}

#pragma mark -UI
- (void)uiConfigAction
{
    self.allCustomerTableview.dataSource = self;
    self.allCustomerTableview.delegate = self;
    self.searchTextfield.delegate = self;
    self.view.backgroundColor = kMainGrayColor;
    self.allCustomerTableview.backgroundColor = [UIColor clearColor];
    self.searchTextfield.returnKeyType = UIReturnKeySearch;
    
    //设置按钮时间
    [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;
}

#pragma mark -设置刷新
- (void)setUpRefreshAction
{
    WS(weakSelf);
    //下拉刷新
    MjRefreshHeaderCustom *headerRefresh = [MjRefreshHeaderCustom headerWithRefreshingBlock:^{
        
        weakSelf.conditionModel.page.page = ONE;
        [weakSelf.allCustomerTableview.mj_footer resetNoMoreData];
        [weakSelf getshoppersAssociatedCustomer:weakSelf.conditionModel isRemove:YES];
    }];
    headerRefresh.stateLabel.hidden = YES;
    headerRefresh.lastUpdatedTimeLabel.hidden = YES;
    self.allCustomerTableview.mj_header = headerRefresh;
    [self.allCustomerTableview.mj_header beginRefreshing];
    self.allCustomerTableview.mj_footer = [MJRefreshAutoNormalFooter footerWithRefreshingBlock:^{
        
        if ( ++ weakSelf.conditionModel.page.page > weakSelf.totalPages) {
            [weakSelf.allCustomerTableview.mj_footer endRefreshingWithNoMoreData];
        }else
        {
            [weakSelf getshoppersAssociatedCustomer:weakSelf.conditionModel isRemove:NO];
        }
    }];
    self.allCustomerTableview.mj_footer.automaticallyHidden = YES;
}

#pragma mark -获取导购关联客户
- (void)getshoppersAssociatedCustomer:(ConsumerQueryCondition *)condition isRemove:(BOOL)remove
{
    [XBLoadingView showHUDViewWithDefault];
    WS(weakSelf);
    [HTTP networkRequestWithURL:SERVERREQUESTURL(GETshoppersCONSUMER)  withRequestType:ZERO withParameter:condition withReturnValueBlock:^(id returnValue) {
        
        weakSelf.allCustomerTableview.emptyDataSetSource = weakSelf;
        weakSelf.allCustomerTableview.emptyDataSetDelegate = weakSelf;
        [XBLoadingView hideHUDViewWithDefault];
        [weakSelf endRefreshingForTableView:weakSelf.allCustomerTableview];
        if (RESULT(returnValue)) {
            if (remove) {
150
                [weakSelf.customerresultArray removeAllObjects];
曹云霄's avatar
曹云霄 committed
151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170
            }
            ConsumerPageResult *Customerresult = [[ConsumerPageResult alloc]initWithDictionary:RESPONSE(returnValue) error:nil];
            weakSelf.totalPages = [RESPONSE(returnValue)[@"totalpages"] intValue];
            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;
                myclientModel.company = objc.company;
                myclientModel.lastVisitedTime = objc.lastVisitedTime;
171
                [weakSelf.customerresultArray addObject:myclientModel];
曹云霄's avatar
曹云霄 committed
172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191
            }
            [weakSelf.allCustomerTableview reloadData];
        }
        else
        {
            [XBLoadingView showHUDViewWithText:MESSAGE(returnValue)];
        }
    }withFailureBlock:^(id error) {
        
        [weakSelf endRefreshingForTableView:weakSelf.allCustomerTableview];
        [XBLoadingView hideHUDViewWithDefault];
    }];
}

- (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];
192
    cell.model = [self.customerresultArray objectAtIndex_opple:indexPath.row];
曹云霄's avatar
曹云霄 committed
193 194 195 196 197
    return cell;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
198
    return self.customerresultArray.count;
曹云霄's avatar
曹云霄 committed
199 200 201 202 203 204 205 206 207
}
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
    return 125;
}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    ClientdetailsViewController *clientdetails = [[[self class] getMainStoryboardClass] instantiateViewControllerWithIdentifier:@"clientdetails"];
208
    clientdetails.model = [self.customerresultArray objectAtIndex_opple:indexPath.row];
曹云霄's avatar
曹云霄 committed
209 210 211 212 213
    clientdetails.cellindex = indexPath.row;
    //设置当前客户
    WS(weakSelf);
    [clientdetails setCurrentUserBlock:^(NSInteger index,NSString *title) {
        
214 215
        NSString *customerID = [[weakSelf.customerresultArray objectAtIndex_opple:indexPath.row] fid];
        NSDictionary *dict = @{@"customerid":customerID,@"title":title,@"model":[weakSelf.customerresultArray objectAtIndex_opple:indexPath.row]};
曹云霄's avatar
曹云霄 committed
216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 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 288 289 290 291 292 293 294 295 296 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 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355
        [Notification postNotificationName:@"SETUPCURRENTCUSTOMER" object:dict];
    }];
    [self.navigationController pushViewController:clientdetails animated:YES];
}


#pragma mark -开始筛选时间
- (IBAction)StartScreeningButtonClick:(UIButton *)sender {
    
    DateSelectedViewController *datevc = [[DateSelectedViewController alloc]init];
    //选中时间回调
    WS(weakSelf);
    [datevc setSelectedDateBlock:^(NSDate *selectedDate) {
        [weakSelf.begindateButton setTitle:[weakSelf dateAsString:selectedDate] forState:UIControlStateNormal];
        [weakSelf CalltimeSearch];
    }];
    
    datevc.preferredContentSize = CGSizeMake(300, 250);
    datevc.modalPresentationStyle = UIModalPresentationFormSheet;
    UIPopoverPresentationController *pop = datevc.popoverPresentationController;
    pop.permittedArrowDirections = UIPopoverArrowDirectionAny;
    pop.sourceView = datevc.view;
    dispatch_async(dispatch_get_main_queue(), ^{
        [self presentViewController:datevc animated:YES completion:nil];
    });
}


#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 -结束筛选时间
- (IBAction)EndScreeningButtonClick:(UIButton *)sender {
    
    WS(weakSelf);
    DateSelectedViewController *datevc = [[DateSelectedViewController alloc]init];
    [datevc setSelectedDateBlock:^(NSDate *selectedDate) {
        [weakSelf.enddateButton setTitle:[weakSelf dateAsString:selectedDate] forState:UIControlStateNormal];
        [weakSelf 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];
    NSDateFormatter* endformater = [[NSDateFormatter alloc] init];
    [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
        {
            [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];
        }
            break;
        case 0://start == end
        {
            [formater setDateFormat:@"yyyy-MM-dd HH:mm:ss"];
            self.conditionModel.createTimeBegin = nil;
            self.conditionModel.createTimeEnd = nil;
        }
            break;
        case 1://start > end
        {
            [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];
        }
            break;
            
        default:
            break;
    }
    self.conditionModel.nameEquals = nil;
    self.conditionModel.mobileEquals = nil;
    self.searchTextfield.text = nil;
    [self.allCustomerTableview.mj_header beginRefreshing];
}

#pragma mark -Return键检测
- (BOOL)textFieldShouldReturn:(UITextField *)textField
{
    [textField resignFirstResponder];
    self.conditionModel.nameEquals = self.searchTextfield.text;
    self.conditionModel.mobileEquals = self.searchTextfield.text;
    if ([BaseViewController isBlankString:self.searchTextfield.text]) {
        self.conditionModel.nameEquals = nil;
        self.conditionModel.mobileEquals = nil;
    }
    [self.allCustomerTableview.mj_header beginRefreshing];
    return YES;
}


#pragma mark -友好界面
- (UIImage *)imageForEmptyDataSet:(UIScrollView *)scrollView
{
    return kNoDataImage;
}

- (NSAttributedString *)titleForEmptyDataSet:(UIScrollView *)scrollView
{
    return [[NSAttributedString alloc]initWithString:@"暂无数据" attributes:nil];
}

- (BOOL)emptyDataSetShouldAllowScroll:(UIScrollView *)scrollView
{
    return YES;
}




@end