ChooseWarehouseViewController.m 5.62 KB
Newer Older
n22's avatar
n22 committed
1 2 3 4
//
//  ChooseStoreViewController.m
//  XFFruit
//
n22's avatar
n22 committed
5
//  Created by 陈俊俊 on 15/8/26.
n22's avatar
n22 committed
6 7 8 9 10 11
//  Copyright (c) 2015年 Xummer. All rights reserved.
//

#import "ChooseWarehouseViewController.h"
#define TopMargin 50
#define TableHeight 50
陈俊俊's avatar
陈俊俊 committed
12
@interface ChooseWarehouseViewController ()
n22's avatar
n22 committed
13 14 15 16 17 18 19 20 21 22
{
    NSIndexPath *_currentIndexPath;
    
}
@end

@implementation ChooseWarehouseViewController

- (void)viewDidLoad {
    [super viewDidLoad];
陈俊俊's avatar
陈俊俊 committed
23 24 25 26
    //记录更新时间
    self.tableView.header.lastUpdatedTimeKey = ChooseWarehouseUpdateDate;
    self.title = @"选择仓库";
    [self fetchWarehouseList:@""];
n22's avatar
n22 committed
27
}
陈俊俊's avatar
陈俊俊 committed
28 29

- (void)getBaseDataFromServer{
n22's avatar
n22 committed
30 31 32 33 34
    __weak typeof(self)weakSelf = self;
    void(^succ)(id) = ^(id data) {
        [IBTLoadingView hideHUDWithText:nil];
        if (data) {
            __strong __typeof(weakSelf)strongSelf = weakSelf;
陈俊俊's avatar
陈俊俊 committed
35 36
            [self endRefreshing];
            [strongSelf fetchWarehouseList:@""];
n22's avatar
n22 committed
37 38 39 40 41 42 43 44 45 46 47 48 49 50
        }else{
            [IBTLoadingView showTips:data];
        }
    };
    void(^fail)(id) = ^(id data) {
        [IBTLoadingView hideHUDWithText:nil];
        [IBTLoadingView showTips:data];
        
    };
    [IBTLoadingView showProgressLabel:@"正在加载..."];
    [[ICRHTTPController sharedController] getWarehouseWithPage_number:0 page_size:100 success:succ failure:fail];
}

#pragma mark -成功
陈俊俊's avatar
陈俊俊 committed
51
- (void)fetchWarehouseList:(NSString *)titleStr{
n22's avatar
n22 committed
52
    ICRDatabaseFetchBlock fetchBlk = ^FMResultSet *(FMDatabase *db) {
陈俊俊's avatar
陈俊俊 committed
53 54 55 56 57 58
        NSString * sql = @"";
        if (titleStr.length > 0) {
            sql = [NSString stringWithFormat:@"SELECT * FROM %@ WHERE NAME LIKE '%%%@%%' ORDER BY %@", [Warehouse TableName],titleStr, @"uuid"];
        }else{
            sql = [NSString stringWithFormat:@"SELECT * FROM %@ ORDER BY %@", [Warehouse TableName], @"uuid"];
        }
n22's avatar
n22 committed
59 60 61 62 63 64
        return [db executeQuery:sql];
    };
    
    __weak typeof(self)weakSelf = self;
    ICRDatabaseFetchResultsBlock fetchResultsBlk = ^(NSArray *fetchedObjects) {
        __strong __typeof(weakSelf)strongSelf = weakSelf;
陈俊俊's avatar
陈俊俊 committed
65 66 67 68 69 70 71 72 73 74 75
        if (![GXF_NSUSERFEFTAULTS objectForKey:ChooseWarehouseUpdateDate]) {
            [GXF_NSUSERFEFTAULTS setObject:[NSDate date] forKey:ChooseWarehouseUpdateDate];
            [GXF_NSUSERFEFTAULTS synchronize];
            [self getBaseDataFromServer];
        }else{
            [strongSelf.dataArr removeAllObjects];
            [strongSelf.dataArr addObject:self.tableView.header.lastUpdatedTime];
            [strongSelf.dataArr addObjectsFromArray:fetchedObjects];
            [self tableViewRefresh];
        }

n22's avatar
n22 committed
76 77 78 79 80 81 82 83 84 85
    };
    
    ICRDataBaseController *dbCtrl = [ICRDataBaseController sharedController];
    [dbCtrl runFetchForClass:[Warehouse class]
                  fetchBlock:fetchBlk
           fetchResultsBlock:fetchResultsBlk];
}
- (void)sureClick{
    
    if (self.dataArr.count > 0) {
n22's avatar
n22 committed
86 87 88 89
        if (_currentIndexPath) {
            Warehouse *warehouse = self.dataArr[_currentIndexPath.row];
            self.choseWarehouse(warehouse);
        }
n22's avatar
n22 committed
90 91 92 93 94
    }
    [self PopViewControllerAnimated:YES];
}


陈俊俊's avatar
陈俊俊 committed
95 96 97
- (void)deleteTextFieldStr {
    [super deleteTextFieldStr];
    
n22's avatar
n22 committed
98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120
}

#pragma mark - 协议方法
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    static NSString *cellID = @"MaskCell";
    MaskCell *cell = [tableView dequeueReusableCellWithIdentifier:cellID];
    if (cell == nil) {
        cell = [[MaskCell alloc]initWithStyle:UITableViewCellStyleDefault reuseIdentifier:cellID totalWidth:ScreenSize.width totalHeight:TableHeight];
        tableView.separatorStyle = UITableViewCellSeparatorStyleNone;
        cell.selectionStyle = UITableViewCellSelectionStyleNone;
        cell.Commitbtn.hidden = YES;
    }
    
    if (_currentIndexPath) {
        if (indexPath.row == _currentIndexPath.row) {
            cell.Commitbtn.hidden = NO;
        }else{
            cell.Commitbtn.hidden = YES;
        }
    }else{
        cell.Commitbtn.hidden = YES;
    }
    if (self.dataArr.count > 0) {
陈俊俊's avatar
陈俊俊 committed
121 122 123 124 125 126 127 128 129 130
        if (indexPath.row == 0) {
            NSString *dateStr = [self.dataArr[indexPath.row] httpParameterString];
            cell.titleLabel.textColor = GXF_CONTENT_COLOR;
            [cell setTitleStr:[NSString stringWithFormat:@"上次更新时间:%@",dateStr]];
        }else{
            Warehouse *warehouse = self.dataArr[indexPath.row];
            NSString *proStr = [NSString stringWithFormat:@"%@[%@]",warehouse.name,warehouse.code];
            
            [cell setTitleStr:proStr];
        }
n22's avatar
n22 committed
131 132 133 134 135 136 137 138 139 140 141 142 143 144
    }
    return cell;
}


- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
    
    MaskCell *currentCell = (MaskCell *)[tableView cellForRowAtIndexPath:_currentIndexPath];
    currentCell.Commitbtn.hidden = YES;
    MaskCell *cell = (MaskCell *)[tableView cellForRowAtIndexPath:indexPath];
    cell.Commitbtn.hidden = NO;
    _currentIndexPath = indexPath;
}

陈俊俊's avatar
陈俊俊 committed
145 146 147 148 149 150 151 152 153 154 155
#pragma mark - 按钮事件
- (void)boxValueChanged:(NSNotification *)fication{
    NSString *tilkeStr = [IBTCommon trimmingCharacters:self.selectTextFiled.text];
    [self fetchWarehouseList:tilkeStr];
}
- (void)tableViewRefresh{
    _currentIndexPath = nil;
    if (self.indexArr.count > 0) {
        [self.indexArr removeAllObjects];
    }
    [self.tableView reloadData];
n22's avatar
n22 committed
156
}
陈俊俊's avatar
陈俊俊 committed
157

n22's avatar
n22 committed
158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173
- (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