| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644 |
- //
- // GoodsKindViewController.m
- // IBOSS
- //
- // Created by guan hong hou on 2017/9/8.
- // Copyright © 2017年 elongtian. All rights reserved.
- //
- // 功能描述:产品种类树型控制器
- #import "GoodsKindViewController.h"
- #import "BusinessDepartmentTreeTableViewCell.h"
- @interface GoodsKindViewController (){
- /**
- 产品种类数据源
- */
- NSMutableArray *dataSource;
- }
- @end
- @implementation GoodsKindViewController
- #pragma 公共函数
- /**
- 视图加载完成函数
- */
- - (void)viewDidLoad {
- [super viewDidLoad];
- dataSource = [NSMutableArray new];
- [self initUI];
- [self initData];
- }
- #pragma 委托函数
- /**
- 数据成功回调
-
- @param sender sender description
- */
- - (void)onRequestSuccess:(ASIDownManager *)sender {
- // 取消进度条
- [self cancel];
- // 服务器返回数据
- RequestResultModel *resultModel = [RequestResultModel dk_modelWithJSON:sender.mWebStr];
- // 服务器返回数据状态值
- int iStatus = resultModel.status;
- // 服务器返回数据消息
- NSString *message = resultModel.message;
- // 服务器返回数据状态值正确
- if (iStatus == 0)
- {
- // 服务器返回数据结果
- NSArray *approvArr = (NSArray *)resultModel.result;
- if(approvArr != nil&&approvArr.count>0)
- {
- _arrSearch=[[NSMutableArray alloc]init];
- _treeArray=[[NSMutableArray alloc]init];
- for(int i=0;i<approvArr.count;i++){
- NSDictionary *dic=approvArr[i];
- NSInteger kindId= [[dic objectForKey:@"KindID"]integerValue];
- NSString *kindCode=[dic objectForKey:@"KindCode"];
- NSString *kindName=[dic objectForKey:@"KindName"];
- AchiementDepartmentInfoModel *info = [AchiementDepartmentInfoModel new];
- [info setOrganizationId:[NSString stringWithFormat:@"%ld",(long)kindId]];
- [info setOrganizationCode:kindCode];
- [info setOrganizationName:kindName];
- [_arrSearch addObject:info];
-
- }
- NSInteger minLength= [Util findDepartmentsRootNodeLength:_arrSearch];
- NSMutableArray *parentArray=[Util findAllParents:_arrSearch minLength:minLength];
- for(int i=0;i<parentArray.count;i++){
- AchiementDepartmentInfoModel *infoModel=parentArray[i];
- BusinessDepartmentFrame *frame=[BusinessDepartmentFrame new];
- NSInteger minLength= [Util findDepartmentsRootNodeLength:_arrSearch];
- NSInteger n=infoModel.organizationCode.length/minLength;
- [infoModel setIsExpand:NO];
- [infoModel setLevelType:[NSString stringWithFormat:@"Level_Type_%ld",(long)n-1]];
- [frame setDepartmentFrame:infoModel];
- [_treeArray addObject:frame];
- AchiementDepartmentInfoModel *model= frame.departmentModel;
- [Util bindChildByParent:model allArray:_arrSearch ];
- }
-
- //设置数据源
- for (int i=0; i<_treeArray.count; i++) {
- BusinessDepartmentFrame *frm_1 = [_treeArray objectAtIndex:i];
- NSMutableArray *array = [NSMutableArray new];
- [array addObject:frm_1];
- [dataSource addObject:array];
-
- }
- [self.kindTableView reloadData];
- [self autoExpandTree];
-
-
-
- }
- }
- // 服务器返回数据状态值异常
- else if(iStatus == ActionResultStatusAuthError
- ||iStatus == ActionResultStatusNoLogin
- ||iStatus == ActionResultStatusLogined||iStatus==ActionResultSessionOverdue)
- {
- [self showReLoginDialog:message];
- }
- // 其他情况
- else
- {
- [self showAlertViewText:message];
- }
- }
- /**
- 数据异常回调
-
- @param sender <#sender description#>
- */
- - (void)onRequestFail:(ASIDownManager *)sender {
- [self cancel];
- [self showAlertViewText:@"网络异常"];
- }
- /**
- tableview的分区数
- @param tableView <#tableView description#>
- @return <#return value description#>
- */
- - (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
-
- return dataSource.count;
- }
- /**
- tableview的行数
- @param tableView <#tableView description#>
- @param section <#section description#>
- @return <#return value description#>
- */
- - (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
-
- NSMutableArray *models = [dataSource objectAtIndex:section];
- return models.count;
- }
- /**
- 获取tableview的cell
- @param tableView <#tableView description#>
- @param indexPath <#indexPath description#>
- @return <#return value description#>
- */
- - (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
-
- static NSString *cellID = @"CELLID";
- BusinessDepartmentTreeTableViewCell *cell = [[BusinessDepartmentTreeTableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:cellID];
- NSMutableArray *arr = [dataSource objectAtIndex:indexPath.section];
- BusinessDepartmentFrame *testFrame = [arr objectAtIndex:indexPath.row];
- [cell setUIWithModel:testFrame];
- [cell.checkBT addTarget:self action:@selector(selectBTClick:event:) forControlEvents:UIControlEventTouchUpInside];
- return cell;
- }
- /**
- tableview的cell点击事件
- @param tableView <#tableView description#>
- @param indexPath <#indexPath description#>
- */
- - (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
-
- [tableView deselectRowAtIndexPath:indexPath animated:YES];
- NSInteger section= indexPath.section;
- NSMutableArray *array = [dataSource objectAtIndex:section];
- NSInteger row= indexPath.row;
- BusinessDepartmentFrame *tes = [array objectAtIndex:row];
- if(tes!=nil){
- if(tes.departmentModel!=nil){
- if (tes.departmentModel.childArray!=nil&&tes.departmentModel.childArray.count>0) {
- tes.departmentModel.isExpand = !tes.departmentModel.isExpand;
- [self reloadSection:section withMyTestFrame:tes withExpand:tes.departmentModel.isExpand];
- }
- }
- }
- }
- /**
- tableview的header高度
- @param tableView <#tableView description#>
- @param section <#section description#>
- @return <#return value description#>
- */
- - (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section{
-
- return 0.01;
- }
- /**
- tableview的footer高度
- @param tableView <#tableView description#>
- @param section <#section description#>
- @return <#return value description#>
- */
- - (CGFloat)tableView:(UITableView *)tableView heightForFooterInSection:(NSInteger)section{
- return 0.01;
- }
- #pragma 私有函数
- /**
- 初始化UI
- */
- -(void)initUI{
- // 是否是dismissViewController
- if (_isPresentViewFlag)
- {
- UIView *vTitle = [[UIView alloc]init];
- vTitle.frame = CGRectMake(0, 0, Screen_Width, 44 + rectStatusHeight);
- vTitle.backgroundColor = NavigationBarTintColor;
- [self.view addSubview:vTitle];
-
- UIButton *button = [UIButton buttonWithType:UIButtonTypeCustom];
- [button setImage:[UIImage imageNamed:@"icon_back"]
- forState:UIControlStateNormal];
- [button addTarget:self action:@selector(goBack)
- forControlEvents:UIControlEventTouchUpInside];
- button.frame = CGRectMake(20, rectStatusHeight + 13, 9, 15);
- [vTitle addSubview:button];
- UILabel *lblTitle = [[UILabel alloc]init];
- lblTitle.frame = CGRectMake(9, rectStatusHeight + 13, Screen_Width-2*9 - 20, 15);
- lblTitle.textColor = [UIColor whiteColor];
- lblTitle.text = @"请选择";
- lblTitle.textAlignment = NSTextAlignmentCenter;
- [vTitle addSubview:lblTitle];
- UIButton *btnSure=[UIButton buttonWithType:UIButtonTypeCustom];
- [btnSure setTitle:@"确定" forState:UIControlStateNormal];
- [btnSure addTarget:self action:@selector(submit)
- forControlEvents:UIControlEventTouchUpInside];
- btnSure.titleLabel.font= [UIFont systemFontOfSize: 15.0];
- btnSure.frame = CGRectMake(CGRectGetWidth(vTitle.frame)-60, rectStatusHeight + 13,40, 15);
- [vTitle addSubview:btnSure];
- }
-
- else
- {
- self.navigationItem.title = @"请选择";
- UIButton *button = [UIButton buttonWithType:UIButtonTypeCustom];
- [button setImage:[UIImage imageNamed:@"icon_back"]
- forState:UIControlStateNormal];
- [button addTarget:self action:@selector(goBack)
- forControlEvents:UIControlEventTouchUpInside];
- button.frame = CGRectMake(0, 0,45,22);
-
- UIBarButtonItem *menuButton = [[UIBarButtonItem alloc] initWithCustomView:button];
- self.navigationItem.leftBarButtonItem = menuButton;
- [self.navigationController setNavigationBarHidden:NO];
-
- }
- _kindTableView = [[UITableView alloc] initWithFrame:CGRectZero];
-
- if (_isPresentViewFlag) {
- self.kindTableView.frame = CGRectMake(0, 44 + rectStatusHeight, self.view.bounds.size.width, self.view.bounds.size.height - 44 - rectStatusHeight - 1) ;
- }
- else{
- self.kindTableView.frame = CGRectMake(0, 0, self.view.bounds.size.width, self.view.bounds.size.height-rectNavHeight - rectStatusHeight - 1) ;
- }
-
- _kindTableView.autoresizingMask = UIViewAutoresizingFlexibleHeight;
- _kindTableView.separatorStyle=UITableViewCellSeparatorStyleNone;
- _kindTableView.delegate = self;
- _kindTableView.dataSource=self;
- [self.view addSubview: _kindTableView];
-
- }
- /**
- 提交函数
- */
- -(void)submit{
- _kindIdResultArray=[[NSMutableArray alloc]init];
- _kindNameResultArray=[[NSMutableArray alloc]init];
- if(dataSource!=nil&&dataSource.count>0){
- for (NSMutableArray *arr0 in dataSource) {
- [self getAllSelectedTreeArray:arr0];
- }
-
- NSString *kindIdResultStr=[_kindIdResultArray componentsJoinedByString:@","];
- NSString *kindNameResultStr=[_kindNameResultArray componentsJoinedByString:@","];
- if ([self.cDelegate respondsToSelector:@selector(kindDataTreeResult:name:)]){
- [self.cDelegate kindDataTreeResult:kindIdResultStr name:kindNameResultStr];
- }
- //是否是dismissViewController
- if (_isPresentViewFlag) {
- [self dismissViewControllerAnimated:YES completion:nil];
- }else{
- [self.navigationController popViewControllerAnimated:YES];
- }
-
- }
- }
- /**
- 获取所有的选中的列表
- @param treeArray <#treeArray description#>
- */
- -(void)getAllSelectedTreeArray:(NSMutableArray*) treeArray{
- for(BusinessDepartmentFrame *frm in treeArray){
- if(frm.departmentModel.isSelect){
- NSString *kindId=frm.departmentModel.organizationId;
- NSString *kindName=frm.departmentModel.organizationName;
- if(![_kindIdResultArray containsObject:kindId]){
- [_kindIdResultArray addObject:kindId];
- }
- if(![_kindNameResultArray containsObject:kindName]){
- [_kindNameResultArray addObject:kindName];
- }
- if(frm.departmentModel.childArray!=nil&&frm.departmentModel.childArray.count>0){
- [self getAllSelectedTreeArray:frm.departmentModel.childArray];
- }
- }
-
- }
- }
- /**
- 收回列表
- @param arry <#arry description#>
- @param model <#model description#>
- */
- -(void)removeChild:(NSMutableArray*)arry frame:(BusinessDepartmentFrame *)frm{
- if(frm.departmentModel.childArray!=nil&&frm.departmentModel.childArray.count>0){
- for(BusinessDepartmentFrame *childFrame in frm.departmentModel.childArray){
- if([arry containsObject:childFrame]){
- [arry removeObject:childFrame];
- }
-
- if(childFrame.departmentModel.childArray!=nil&&childFrame.departmentModel.childArray.count>0){
-
- [self removeChild:arry frame:childFrame];
-
- }
- }
- }
- }
- /**
- check box点击事件
- @param sender <#sender description#>
- */
- - (void)selectBTClick:(UIButton*)sender event:(id)event{
-
- UITableView *cell = (UITableView *)sender.superview.superview;
-
- NSSet *touches =[event allTouches];
- UITouch *touch =[touches anyObject];
- CGPoint currentTouchPosition = [touch locationInView:self.kindTableView];
- NSIndexPath *indexPath= [self.kindTableView indexPathForRowAtPoint:currentTouchPosition];
-
- BusinessDepartmentTreeTableViewCell *businessCell= [self.kindTableView cellForRowAtIndexPath:indexPath];
- businessCell.achievementModel.isSelect = !businessCell.achievementModel.isSelect;
-
-
- [businessCell.checkBT setImage:businessCell.achievementModel.isSelect ? [UIImage imageNamed:@"order_checked"]:[UIImage imageNamed:@"order_unchecked"] forState:UIControlStateNormal];
-
- if(businessCell.achievementModel.childArray!=nil&&businessCell.achievementModel.childArray.count>0)
- {
-
- [self selectAllChildNode:businessCell.achievementModel.childArray achievementCell:&businessCell];
- }
-
- if(dataSource!=nil&&dataSource.count>0){
- for (NSMutableArray *arr0 in dataSource) {
- [self selectParentNode:arr0 achievementFrame:businessCell.achievementFrame];
- }
-
- for (NSMutableArray *arr0 in dataSource) {
- [self deSelectParentNode:arr0 achievementFrame:businessCell.achievementFrame];
- }
- }
-
-
-
- [self.kindTableView reloadSections:[NSIndexSet indexSetWithIndex:indexPath.section] withRowAnimation:UITableViewRowAnimationNone];
-
- }
- /**
- 初始化数据
- */
- -(void)initData{
- self.downManager = [[ASIDownManager alloc] init];
- self.downManager.delegate=self;
- self.downManager.onRequestSuccess=@selector(onRequestSuccess:);
- self.downManager.onRequestFail=@selector(onRequestFail:);
- [self startLoading];
- NSString *urlStr = ServerURL;
- NSMutableDictionary *dict = [NSMutableDictionary new];
- [dict setObject:@"GetDataSource" forKey:@"Action"];
- [dict setObject:kkAccountCode forKey:@"AccountCode"];
- [dict setObject:kkUserCode forKey:@"UserCode"];
- [dict setObject:kkUserPwd forKey:@"UserPassword"];
- [dict setObject:kkSessionKey forKey:@"SessionKey"];
- [dict setObject:@"T_MST_Goods_Kind" forKey:@"DataSourceCode"];
- [dict setObject:@"" forKeyedSubscript:@"IsShowTwoLevelCode"];
- [self.downManager postHttpRequest:urlStr dic:dict path:nil fileName:nil];
- }
- /**
- 进度条隐藏
- */
- - (void)cancel {
- [self stopLoading];
- }
- /**
- 重新加载section
- @param section <#section description#>
- @param model <#model description#>
- @param isExpand <#isExpand description#>
- */
- - (void)reloadSection:(NSInteger)section withMyTestFrame:(BusinessDepartmentFrame *)frm withExpand:(BOOL)isExpand{
-
- NSMutableArray *arry1 = [dataSource objectAtIndex:section];
- NSInteger index = [arry1 indexOfObject:frm];
-
- NSIndexSet *indexSet = [NSIndexSet indexSetWithIndexesInRange:NSMakeRange(index+1, frm.departmentModel.childArray.count)];
-
- if (isExpand){
- if(frm!=nil){
- if(frm.departmentModel!=nil){
- AchiementDepartmentInfoModel *model=frm.departmentModel;
- if(model.childArray!=nil&&model.childArray.count>0){
- for (int i =0; i<model.childArray.count; i++) {
- BusinessDepartmentFrame *frame = [model.childArray objectAtIndex:i];
- if(frame!=nil){
- if(frame.departmentModel!=nil){
- frame.departmentModel.isExpand = NO;
- }
- }
-
- }
-
- [arry1 insertObjects:frm.departmentModel.childArray atIndexes:indexSet];
- }
- }
- }
-
- }
- else{
- if(frm!=nil){
- if(frm.departmentModel!=nil){
- if((frm.departmentModel.childArray!=nil&&frm.departmentModel.childArray.count>0)&&(arry1!=nil&&arry1.count>0)){
-
- [self removeChild:arry1 frame:frm];
- }
- }
- }
- }
-
- NSIndexSet *reloadSet = [NSIndexSet indexSetWithIndex:section];
- [self.kindTableView reloadSections:reloadSet withRowAnimation:UITableViewRowAnimationAutomatic];
-
- }
- /**
- 自动展开列表
- */
- -(void)autoExpandTree{
- NSInteger section= 0;
- NSMutableArray *array = [dataSource objectAtIndex:section];
- NSInteger row= 0;
- BusinessDepartmentFrame *frm = [array objectAtIndex:row];
- if (frm.departmentModel.childArray!=nil&&frm.departmentModel.childArray.count>0) {
- frm.departmentModel.isExpand = !frm.departmentModel.isExpand;
- [self reloadSection:section withMyTestFrame:frm withExpand:frm.departmentModel.isExpand];
- }
-
- }
-
- /**
- 树的checkbox选中和反选事件
- @param sender <#sender description#>
- */
- - (void)selectBTClick:(UIButton *)sender{
-
- BusinessDepartmentTreeTableViewCell *cell = (BusinessDepartmentTreeTableViewCell *)sender.superview.superview;
-
- cell.achievementModel.isSelect = !cell.achievementModel.isSelect;
- NSIndexPath *indexpath = [self.kindTableView indexPathForCell:cell];
- [cell.checkBT setImage:cell.achievementModel.isSelect ? [UIImage imageNamed:@"order_checked"]:[UIImage imageNamed:@"order_unchecked"] forState:UIControlStateNormal];
-
- if(cell.achievementModel.childArray!=nil&&cell.achievementModel.childArray.count>0)
- {
-
- [self selectAllChildNode:cell.achievementModel.childArray achievementCell:&cell];
- }
-
- if(dataSource!=nil&&dataSource.count>0){
- for (NSMutableArray *arr0 in dataSource) {
- [self selectParentNode:arr0 achievementFrame:cell.achievementFrame];
-
- for (NSMutableArray *arr0 in dataSource) {
- [self deSelectParentNode:arr0 achievementFrame:cell.achievementFrame];
- }
- }
- }
-
-
- [self.kindTableView reloadSections:[NSIndexSet indexSetWithIndex:indexpath.section] withRowAnimation:UITableViewRowAnimationNone];
- }
- /**
- 选中和反选所有的子节点
- @param childArray <#childArray description#>
- @param cell <#cell description#>
- */
- -(void)selectAllChildNode:(NSMutableArray*)childArray achievementCell:(BusinessDepartmentTreeTableViewCell*__autoreleasing *)cell{
- for(BusinessDepartmentFrame *frm in childArray){
- if(frm!=nil){
- BusinessDepartmentTreeTableViewCell* achievementCell=*cell;
- frm.departmentModel.isSelect=achievementCell.achievementModel.isSelect;
- if(frm.departmentModel.childArray!=nil&&frm.departmentModel.childArray>0){
- [self selectAllChildNode:frm.departmentModel.childArray achievementCell:&achievementCell];
- }
- }
- }
- }
- /**
- 选中子节点的父节点
- @param array <#array description#>
- @param achievementModel <#achievementModel description#>
- */
- -(void)selectParentNode:(NSMutableArray*)array achievementFrame:(BusinessDepartmentFrame*)achievementFrame{
-
- for(BusinessDepartmentFrame *frame in array){
- if(frame!=nil){
- if(frame.departmentModel!=nil){
- if(frame.departmentModel.childArray!=nil&&frame.departmentModel.childArray.count>0){
- if([frame.departmentModel.childArray containsObject:achievementFrame]){
- if(achievementFrame.departmentModel.isSelect){
- if([self isSelectAllChild:frame.departmentModel]){
- frame.departmentModel.isSelect=YES;
- }
- if(dataSource!=nil&&dataSource.count>0){
- for (NSMutableArray *arr0 in dataSource) {
- [self selectParentNode:arr0 achievementFrame:frame];
- }
- }
- }
- }
- for(BusinessDepartmentFrame *mo in frame.departmentModel.childArray){
- if(mo!=nil){
- if(mo.departmentModel!=nil){
- if(mo.departmentModel.childArray!=nil&&mo.departmentModel.childArray.count>0){
- [self selectParentNode:mo.departmentModel.childArray achievementFrame:achievementFrame];
- }
- }
- }
- }
- }
- }
- }
-
- }
- }
- /**
- 是否选中所有的子节点
- @param model <#model description#>
- @return <#return value description#>
- */
- -(Boolean)isSelectAllChild:(AchiementDepartmentInfoModel*)model{
- int i=0;
- Boolean flag = NO;
- if(model.childArray!=nil&&model.childArray.count>0){
- for(BusinessDepartmentFrame *frame in model.childArray){
- if(frame.departmentModel.isSelect){
- i++;
- }
- }
-
- if(model.childArray.count==i){
- flag=YES;
- }
- else{
- flag=NO;
- }
- }
- return flag;
-
- }
- /**
- 反选子节点的父节点
- @param array <#array description#>
- @param achievementModel <#achievementModel description#>
- */
- -(void)deSelectParentNode:(NSMutableArray*)array achievementFrame:(BusinessDepartmentFrame*)achievementFrame{
-
- for(BusinessDepartmentFrame *frame in array){
- if(frame.departmentModel.childArray!=nil&&frame.departmentModel.childArray.count>0){
- if(frame!=nil){
- if(frame.departmentModel!=nil){
- if([frame.departmentModel.childArray containsObject:achievementFrame]){
- if(!achievementFrame.departmentModel.isSelect){
- frame.departmentModel.isSelect=NO;
- if(dataSource!=nil&&dataSource.count>0){
- for (NSMutableArray *arr0 in dataSource) {
- [self deSelectParentNode:arr0 achievementFrame:frame];
- }
- }
- }
- }
- for(BusinessDepartmentFrame *mo in frame.departmentModel.childArray){
- if(mo!=nil){
- if(mo.departmentModel!=nil){
- if(mo.departmentModel.childArray!=nil&&mo.departmentModel.childArray.count>0){
- [self deSelectParentNode:mo.departmentModel.childArray achievementFrame:achievementFrame];
- }
- }
- }
- }
- }
- }
- }
- }
- }
- @end
|