SelectStoreViewController.m 7.76 KB
Newer Older
admin's avatar
admin committed
1 2 3 4 5 6 7 8 9
//
//  SelectStoreViewController.m
//  redstar
//
//  Created by admin on 15/12/13.
//  Copyright © 2015年 ZWF. All rights reserved.
//

#import "SelectStoreViewController.h"
admin's avatar
admin committed
10
#import "SelectStoreHeadView.h"
admin's avatar
admin committed
11
#import "CheckBoxButton.h"
admin's avatar
admin committed
12

13
#import "HttpClient.h"
admin's avatar
admin committed
14 15 16 17
#import <MBProgressHUD.h>

#import "TreeView.h"
#import "TreeNodeModel.h"
18

admin's avatar
admin committed
19
@interface SelectStoreViewController () <TreeDelegate>
admin's avatar
admin committed
20
@property (nonatomic, strong) SelectStoreHeadView *headerView;
admin's avatar
admin committed
21 22 23 24 25 26
@property (nonatomic, strong) TreeView *treeView;
@property (nonatomic, strong) UIButton *sureButton;

@property (nonatomic, strong) NSMutableArray *allTreeArray;

@property (nonatomic, strong) NSMutableArray *selectTreeArray;
admin's avatar
admin committed
27

admin's avatar
admin committed
28 29 30 31 32 33 34 35
@end

@implementation SelectStoreViewController

- (void)viewDidLoad
{
    [super viewDidLoad];
    
admin's avatar
admin committed
36 37 38 39 40 41 42 43 44
    if ([[[UIDevice currentDevice] systemVersion] floatValue] >= 7.0) {
        self.edgesForExtendedLayout = UIRectEdgeNone;
        self.extendedLayoutIncludesOpaqueBars = NO;
        self.modalPresentationCapturesStatusBarAppearance = NO;
        self.navigationController.navigationBar.translucent = NO;
    }
    
    
    self.view.backgroundColor = kFootViewBackGroundColor;
admin's avatar
admin committed
45 46 47 48 49 50 51
    
    UILabel *customLab = [[UILabel alloc] initWithFrame:CGRectMake(0, 0, 40, 30)];
    [customLab setTextColor:[UIColor whiteColor]];
    [customLab setText:@"选择商场"];
    customLab.font = [UIFont boldSystemFontOfSize:19];
    self.navigationItem.titleView = customLab;
    
admin's avatar
admin committed
52 53 54
    self.allTreeArray = [NSMutableArray array];
    self.selectTreeArray = [NSMutableArray array];

admin's avatar
admin committed
55 56 57 58 59 60 61 62 63 64 65
    
    UIButton *backBtn = [UIButton buttonWithType:UIButtonTypeCustom];
    backBtn.frame = CGRectMake(0, 0, 30, 44);
    
    [backBtn setImage:[UIImage imageNamed:@"back_btn"] forState:UIControlStateNormal];
    [backBtn addTarget:self action:@selector(doBack:) forControlEvents:UIControlEventTouchUpInside];
    
    UIBarButtonItem *backItem = [[UIBarButtonItem alloc] initWithCustomView:backBtn];
    self.navigationItem.leftBarButtonItem = backItem;
    
    
admin's avatar
admin committed
66
    [self requestQueryLowerOrgs];
admin's avatar
admin committed
67
    
admin's avatar
admin committed
68
    [self.sureButton addTarget:self action:@selector(sureClick:) forControlEvents:UIControlEventTouchUpInside];
69
    
admin's avatar
admin committed
70 71 72 73 74 75 76 77 78
}

- (void)sureClick:(UIButton *)sender
{
    if (_delegate && [_delegate respondsToSelector:@selector(deliverWithArray:)]) {
        [_delegate deliverWithArray:self.treeView.selectTreeArray];
    }
    [self.navigationController popViewControllerAnimated:YES];

admin's avatar
admin committed
79 80 81 82 83 84 85
}

- (void)doBack:(id)sender
{
    [self.navigationController popViewControllerAnimated:YES];
}

86 87 88 89 90 91
- (void)requestQueryLowerOrgs
{
    NSString *url = [NSString stringWithFormat:@"%@%@", kRedStarURL, kQueryLowerOrgsURL];
    HttpClient *httpClient = [[HttpClient alloc] initWithUrl:url];
    NSDictionary *parameters = @{@"path":@"",
                                 @"pageNumber":@(0),
admin's avatar
admin committed
92
                                 @"pageSize":@(999)
93
                                 };
94
    NSLog(@"url = %@", url);
admin's avatar
admin committed
95
    [MBProgressHUD showHUDAddedTo:self.view animated:YES];
96 97
    [httpClient queryLowerOrgsWithParameters:parameters completion:^(id response, NSError *error) {
        NSLog(@"查询下级组织 reponse = %@,error = %@", response, error);
admin's avatar
admin committed
98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117
        NSDictionary *data = response[@"data"];
        NSArray *dataArray = data[@"records"];
        NSMutableArray *oneTree = [NSMutableArray array];
        NSMutableArray *twoTree = [NSMutableArray array];
        NSMutableArray *threeTree = [NSMutableArray array];
        NSMutableArray *fourTree = [NSMutableArray array];
        for (NSDictionary *dict in dataArray) {
            TreeNodeModel *tree = [[TreeNodeModel alloc] init];
            [tree setValuesForKeysWithDictionary:dict];
            [_allTreeArray addObject:tree];

            if (tree.level == 1) {
                [oneTree addObject:tree];
            } else if (tree.level == 2) {
                [twoTree addObject:tree];
            } else if (tree.level == 3){
                [threeTree addObject:tree];
            } else {
                [fourTree addObject:tree];
            }
admin's avatar
admin committed
118
        }
119
        [MBProgressHUD hideHUDForView:self.view animated:YES];
admin's avatar
admin committed
120
        
121 122 123
        if (oneTree.count == 0) {
            return;
        }
admin's avatar
admin committed
124 125 126 127 128 129 130
        
        TreeNodeModel *tree1 = oneTree[0];
        for (int k = 0; k < twoTree.count; k++) {
            TreeNodeModel *tree2 = twoTree[k];
            if ([tree2.upper isEqualToString:tree1.code]) {
                [tree1 insertChild:tree2];
            }
admin's avatar
admin committed
131
        }
admin's avatar
admin committed
132 133 134 135 136 137 138 139 140
        
        for (int i = 0; i < twoTree.count; i++) {
            TreeNodeModel *tree2 = twoTree[i];
            for (int k = 0; k < threeTree.count; k++) {
                TreeNodeModel *tree3 = threeTree[k];
                if ([tree3.upper isEqualToString:tree2.code]) {
                    [tree2 insertChild:tree3];
                }
            }
admin's avatar
admin committed
141

142
        }
admin's avatar
admin committed
143 144 145 146 147 148 149 150 151 152
        
        for (int i = 0; i < threeTree.count; i++) {
            TreeNodeModel *tree3 = threeTree[i];
            for (int k = 0; k < fourTree.count; k++) {
                TreeNodeModel *tree4 = fourTree[k];
                if ([tree4.upper isEqualToString:tree3.code]) {
                    [tree3 insertChild:tree4];
                }
            }
            
153
        }
admin's avatar
admin committed
154
        self.treeView.nodeData = tree1;
admin's avatar
admin committed
155

admin's avatar
admin committed
156
    }];
157
}
admin's avatar
admin committed
158

admin's avatar
admin committed
159 160
#pragma mark - TreeDelegate
- (void)tree:(TreeView *)tree didClickTitleAtNode:(TreeNodeCell *)node
admin's avatar
admin committed
161
{
admin's avatar
admin committed
162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177
    node.titleButton.selected = !node.titleButton.selected;
    node.nodeData.isChoose = node.titleButton.selected;
    NSString *path = node.nodeData.path;
    
    // 对子类做处理
    if (node.nodeData.isChoose) {
        for (TreeNodeModel *model in _allTreeArray) {
            if (model.path.length < path.length) {
                continue;
            }

            NSString *modelPath = [model.path substringToIndex:path.length];
            if ([path isEqualToString:modelPath]) {
                model.isChoose = YES;
            }
        }
admin's avatar
admin committed
178
    } else {
admin's avatar
admin committed
179 180 181 182 183 184 185 186 187
        for (TreeNodeModel *model in _allTreeArray) {
            if (model.path.length < path.length) {
                continue;
            }
            NSString *modelPath = [model.path substringToIndex:path.length];
            if ([path isEqualToString:modelPath]) {
                model.isChoose = NO;
            }
        }
admin's avatar
admin committed
188
    }
admin's avatar
admin committed
189 190 191 192 193 194
    
    NSMutableArray *chooseArray = [NSMutableArray array];
    for (TreeNodeModel *model in _allTreeArray) {
        if (model.isChoose == YES) {
            [chooseArray addObject:model];
        }
admin's avatar
admin committed
195
    }
admin's avatar
admin committed
196 197 198 199 200 201 202 203 204
    
    NSMutableArray *repeatArray = [NSMutableArray array];
    for (int i = 0; i < chooseArray.count; i++) {
        TreeNodeModel *tree1 = chooseArray[i];
        for (int j = 0; j < chooseArray.count; j++) {
            TreeNodeModel *tree2 = chooseArray[j];
            if ([tree1.code isEqualToString:tree2.upper]) {
                [repeatArray addObject:tree2];
            }
admin's avatar
admin committed
205
        }
admin's avatar
admin committed
206
    }
admin's avatar
admin committed
207 208 209 210 211
    
    [chooseArray removeObjectsInArray:repeatArray];
    
    self.treeView.selectTreeArray = chooseArray;
    [self.treeView reloadData];
admin's avatar
admin committed
212 213
}

admin's avatar
admin committed
214
- (TreeView *)treeView
admin's avatar
admin committed
215 216
{
    if (!_treeView) {
admin's avatar
admin committed
217 218 219
        _treeView = [[TreeView alloc] initWithFrame:CGRectMake(0, 0, kScreenWidth, kScreenHeight - 64 - 70)];
        _treeView.treeDelegate = self;
        _treeView.tableHeaderView = self.headerView;
admin's avatar
admin committed
220 221 222 223 224
        [self.view addSubview:_treeView];
    }
    return _treeView;
}

admin's avatar
admin committed
225 226 227 228 229 230 231 232
- (SelectStoreHeadView *)headerView
{
    if (!_headerView) {
        _headerView = [[SelectStoreHeadView alloc] initWithFrame:CGRectMake(0, 0, kScreenWidth, 50)];
    }
    return _headerView;
}

admin's avatar
admin committed
233

admin's avatar
admin committed
234 235 236 237 238 239 240 241 242
- (UIButton *)sureButton
{
    if (!_sureButton) {
        _sureButton = [[UIButton alloc] initWithFrame:CGRectMake(20, kScreenHeight - 64 - 55, kScreenWidth - 40, 40)];
        _sureButton.backgroundColor = kNavigationBarColor;
        [_sureButton setTitle:@"确定" forState:UIControlStateNormal];
        [self.view addSubview:_sureButton];
    }
    return _sureButton;
admin's avatar
admin committed
243
}
admin's avatar
admin committed
244

admin's avatar
admin committed
245 246

@end