OrganizeSelectVC.m 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469
  1. //
  2. // OrganizeSelectVC.m
  3. // shiku_im
  4. //
  5. // Created by IMAC on 2019/8/14.
  6. // Copyright © 2019 Reese. All rights reserved.
  7. //
  8. #import "OrganizeSelectVC.h"
  9. #import "DepartObject.h"
  10. #import "EmployeObject.h"
  11. #import "RATreeView.h"
  12. #import "OrganizTableViewCell.h"
  13. #import "EmployeeTableViewCell.h"
  14. #import "JX_DownListView.h"
  15. #import "JXAddDepartViewController.h"
  16. #import "JXSelFriendVC.h"
  17. #import "JXSelectFriendsVC.h"
  18. #import "JXUserInfoVC.h"
  19. #import "JXSelDepartViewController.h"
  20. #import "QCheckBox.h"
  21. @interface OrganizeSelectVC ()<RATreeViewDelegate, RATreeViewDataSource,SelDepartDelegate,UIAlertViewDelegate,QCheckBoxDelegate>
  22. @property (nonatomic, strong) NSMutableArray<DepartObject *> * dataArray;
  23. @property (nonatomic, weak) RATreeView * treeView;
  24. @property (nonatomic, strong) UIButton * moreButton;
  25. @property (atomic, strong) id currentOrgObj;
  26. @property (nonatomic, assign) BOOL afterDelCompany;
  27. @property (nonatomic, strong) UIControl * control;
  28. @property (nonatomic, strong) NSMutableDictionary * allDataDict;
  29. @property (nonatomic, strong) NSMutableDictionary * employeesDict;
  30. @property (nonatomic, strong) NSMutableDictionary * companyDict;
  31. @property (nonatomic, copy) NSString * companyId;
  32. @property (nonatomic, copy) NSString * companyName;
  33. @property (nonatomic, copy) void (^rowActionAfterRequestBlock)(id sender);
  34. @property (nonatomic, strong) UIView * noCompanyView;
  35. @property (atomic, strong) id item;
  36. @property (nonatomic, assign) BOOL isNotDele;
  37. @property (nonatomic,strong)NSMutableDictionary *colleagueDic;
  38. @property (nonatomic,strong)NSMutableDictionary *checkBoxDic;
  39. @end
  40. @implementation OrganizeSelectVC
  41. - (id)init
  42. {
  43. self = [super init];
  44. if (self) {
  45. self.heightHeader = JX_SCREEN_TOP;
  46. self.heightFooter = 0;
  47. self.title = Localized(@"OrganizVC_Organiz");
  48. self.tableBody.backgroundColor = THEMEBACKCOLOR;
  49. self.isFreeOnClose = YES;
  50. self.isGotoBack = YES;
  51. _dataArray = [NSMutableArray new];
  52. _allDataDict = [NSMutableDictionary new];
  53. _employeesDict = [NSMutableDictionary new];
  54. _companyDict = [NSMutableDictionary new];
  55. _colleagueDic = [NSMutableDictionary dictionary];
  56. [self createTreeView];
  57. [g_server getCompanyAuto:self];
  58. _checkBoxDic = [NSMutableDictionary dictionary];
  59. }
  60. return self;
  61. }
  62. - (void)viewDidLoad {
  63. [super viewDidLoad];
  64. [self createHeadAndFoot];
  65. UIButton *allSelect = [UIButton buttonWithType:UIButtonTypeSystem];
  66. [allSelect setTitle:Localized(@"JX_Confirm") forState:UIControlStateNormal];
  67. if (THESIMPLESTYLE) {
  68. [allSelect setTitleColor:[UIColor blackColor] forState:UIControlStateNormal];
  69. }else{
  70. [allSelect setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
  71. }
  72. allSelect.tintColor = [UIColor clearColor];
  73. allSelect.frame = CGRectMake(JX_SCREEN_WIDTH - 70, JX_SCREEN_TOP - 34, 60, 24);
  74. [allSelect addTarget:self action:@selector(confirmBtnAction:) forControlEvents:UIControlEventTouchUpInside];
  75. [self.tableHeader addSubview:allSelect];
  76. }
  77. - (void)viewWillAppear:(BOOL)animated {
  78. [super viewWillAppear:animated];
  79. [_treeView reloadRows];
  80. }
  81. -(void)createTreeView{
  82. RATreeView *treeView = [[RATreeView alloc] initWithFrame:self.view.bounds style:RATreeViewStylePlain];
  83. treeView.delegate = self;
  84. treeView.dataSource = self;
  85. treeView.treeFooterView = [UIView new];
  86. treeView.separatorStyle = RATreeViewCellSeparatorStyleSingleLine;
  87. treeView.estimatedRowHeight = 0;
  88. treeView.estimatedSectionHeaderHeight = 0;
  89. treeView.estimatedSectionFooterHeight = 0;
  90. UIRefreshControl *refreshControl = [UIRefreshControl new];
  91. [refreshControl addTarget:self action:@selector(refreshControlChanged:) forControlEvents:UIControlEventValueChanged];
  92. [treeView.scrollView addSubview:refreshControl];
  93. [treeView reloadData];
  94. [treeView setBackgroundColor:[UIColor colorWithWhite:0.97 alpha:1.0]];
  95. self.treeView = treeView;
  96. treeView.frame = self.tableBody.bounds;
  97. treeView.autoresizingMask = UIViewAutoresizingFlexibleWidth | UIViewAutoresizingFlexibleHeight;
  98. [self.tableBody addSubview:treeView];
  99. [treeView registerClass:[OrganizTableViewCell class] forCellReuseIdentifier:NSStringFromClass([OrganizTableViewCell class])];
  100. [treeView registerClass:[EmployeeTableViewCell class] forCellReuseIdentifier:NSStringFromClass([EmployeeTableViewCell class])];
  101. }
  102. - (void)confirmBtnAction:(UIButton *)btn{
  103. if ([self.delegate respondsToSelector:@selector(selectOrganizeVC:selectArray:)]) {
  104. [self.delegate selectOrganizeVC:self selectArray:_seletedArray];
  105. }
  106. [self actionQuit];
  107. }
  108. #pragma mark TreeView Delegate methods
  109. - (CGFloat)treeView:(RATreeView *)treeView heightForRowForItem:(id)item
  110. {
  111. if ([item isMemberOfClass:[EmployeObject class]]) {
  112. return 60;
  113. }
  114. return 44;
  115. }
  116. - (void)treeView:(RATreeView *)treeView willExpandRowForItem:(id)item
  117. {
  118. if ([item isMemberOfClass:[DepartObject class]]) {
  119. OrganizTableViewCell * cell = (OrganizTableViewCell *)[self.treeView cellForItem:item];
  120. cell.arrowExpand = YES;
  121. }
  122. }
  123. - (void)treeView:(RATreeView *)treeView willCollapseRowForItem:(id)item
  124. {
  125. if ([item isMemberOfClass:[DepartObject class]]) {
  126. OrganizTableViewCell * cell = (OrganizTableViewCell *)[self.treeView cellForItem:item];
  127. cell.arrowExpand = NO;
  128. }
  129. }
  130. #pragma mark 左划手势 -删除
  131. - (BOOL)treeView:(RATreeView *)treeView canEditRowForItem:(id)item
  132. {
  133. return NO;
  134. }
  135. -(UITableViewCellEditingStyle)treeView:(RATreeView *)treeView editingStyleForRowForItem:(id)item{
  136. if (treeView.editing)
  137. return UITableViewCellEditingStyleNone;
  138. else
  139. return UITableViewCellEditingStyleDelete;
  140. }
  141. - (void)treeView:(RATreeView *)treeView commitEditingStyle:(UITableViewCellEditingStyle)editingStyle forRowForItem:(id)item
  142. {
  143. if (editingStyle != UITableViewCellEditingStyleDelete) {
  144. return;
  145. }
  146. }
  147. -(void)treeView:(RATreeView *)treeView didSelectRowForItem:(id)item{
  148. if ([item isMemberOfClass:[EmployeObject class]]){
  149. QCheckBox *checkBox = nil;
  150. NSArray *array = [_checkBoxDic allKeys];
  151. NSInteger user = [((EmployeObject *)item).userId intValue];
  152. for (NSString *userId in array) {
  153. QCheckBox *btn = [_checkBoxDic objectForKey:userId];
  154. if (btn.tag == user + 10086) {
  155. checkBox = btn;
  156. break;
  157. }
  158. }
  159. checkBox.selected = !checkBox.selected;
  160. [self didSelectedCheckBox:checkBox checked:checkBox.selected];
  161. }else{
  162. DepartObject * depart = item;
  163. if (depart.children.count == 0)
  164. [g_server showMsg:Localized(@"OrgaVC_DepartNoChild") delay:1.8];
  165. }
  166. }
  167. - (void)didSelectedCheckBox:(QCheckBox *)checkbox checked:(BOOL)checked{
  168. NSString *userId = [NSString stringWithFormat:@"%ld",checkbox.tag - 10086];
  169. EmployeObject *employe = [_colleagueDic objectForKey:userId];
  170. if(checked){
  171. for (NSInteger i = 0; i < _seletedArray.count; i++) {
  172. EmployeObject *employ1 = _seletedArray[i];
  173. if ([employ1.userId isEqualToString:employe.userId]) {
  174. [_seletedArray removeObjectAtIndex:i];
  175. i = i - 1;
  176. }
  177. }
  178. [_seletedArray addObject:employe];
  179. }else{
  180. for (NSInteger i = 0; i < _seletedArray.count; i++) {
  181. EmployeObject *employer = _seletedArray[i];
  182. if ([employer isEqual:employe] || [employer.userId isEqualToString:employe.userId]) {
  183. [_seletedArray removeObjectAtIndex:i];
  184. i = i - 1;
  185. }
  186. }
  187. }
  188. }
  189. #pragma mark TreeView Data Source
  190. - (UITableViewCell *)treeView:(RATreeView *)treeView cellForItem:(id)item
  191. {
  192. NSInteger level = [self.treeView levelForCellForItem:item];
  193. if ([item isMemberOfClass:[DepartObject class]]) {
  194. DepartObject * dataObject = item;
  195. BOOL expanded = [self.treeView isCellForItemExpanded:item];
  196. // OrganizTableViewCell * cell = [self.treeView dequeueReusableCellWithIdentifier:NSStringFromClass([OrganizTableViewCell class])];
  197. OrganizTableViewCell *cell = [[OrganizTableViewCell alloc] init];
  198. cell.selectionStyle = UITableViewCellSelectionStyleNone;
  199. [cell setupWithData:dataObject level:level expand:expanded];
  200. cell.additionButton.hidden = YES;
  201. return cell;
  202. }else if ([item isMemberOfClass:[EmployeObject class]]) {
  203. EmployeObject * dataObject = item;
  204. EmployeeTableViewCell *cell = [[EmployeeTableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:@"myCheckBoxCell"];
  205. cell.selectionStyle = UITableViewCellSelectionStyleNone;
  206. BOOL flag = NO;
  207. for (EmployeObject *employe in _seletedArray) {
  208. if ([employe.userId isEqualToString:dataObject.userId] && [employe.nickName isEqualToString:dataObject.nickName]) {
  209. flag = YES;
  210. break;
  211. }
  212. }
  213. [_colleagueDic setObject:dataObject forKey:dataObject.userId];
  214. QCheckBox* btn = [[QCheckBox alloc] initWithDelegate:self];
  215. btn.tag = [dataObject.userId intValue] + 10086;
  216. btn.checked = flag;
  217. btn.frame = CGRectMake(JX_SCREEN_WIDTH-40, 17.5, 25, 25);
  218. [cell addSubview:btn];
  219. [_checkBoxDic setObject:btn forKey:dataObject.userId];
  220. [cell setupWithData:dataObject level:level];
  221. return cell;
  222. }
  223. return nil;
  224. }
  225. - (NSInteger)treeView:(RATreeView *)treeView numberOfChildrenOfItem:(id)item
  226. {
  227. if (item == nil) {
  228. return [self.dataArray count];
  229. }
  230. if ([item isMemberOfClass:[EmployeObject class]]) {
  231. return 0;
  232. }else{
  233. DepartObject * dataObject = item;
  234. return [dataObject.children count];
  235. }
  236. }
  237. - (id)treeView:(RATreeView *)treeView child:(NSInteger)index ofItem:(id)item
  238. {
  239. if (item == nil) {
  240. return [self.dataArray objectAtIndex:index];
  241. }
  242. if ([item isMemberOfClass:[EmployeObject class]]) {
  243. return nil;
  244. }else{
  245. DepartObject * dataObject = item;
  246. return dataObject.children[index];
  247. }
  248. }
  249. - (void)didReceiveMemoryWarning {
  250. [super didReceiveMemoryWarning];
  251. }
  252. -(void)dealloc{
  253. self.rowActionAfterRequestBlock = nil;
  254. }
  255. #pragma mark - Actions
  256. - (void)refreshControlChanged:(UIRefreshControl *)refreshControl
  257. {
  258. dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(0.5 * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{
  259. [refreshControl endRefreshing];
  260. });
  261. }
  262. #pragma mark alertViewDelegate
  263. -(void)alertView:(UIAlertView *)alertView clickedButtonAtIndex:(NSInteger)buttonIndex{
  264. if (alertView.tag == 1001) {
  265. if (buttonIndex == 1) {
  266. DepartObject * orgObject = _currentOrgObj;
  267. [g_server quitCompany:orgObject.companyId toView:self];
  268. }else{
  269. self.rowActionAfterRequestBlock = nil;
  270. }
  271. }else if (alertView.tag == 2001) {
  272. }
  273. }
  274. #pragma mark - selDepart Delegate
  275. -(void)selNewDepartmentWith:(DepartObject *)newDepart{
  276. EmployeObject * employeeOBJ = _currentOrgObj;
  277. [g_server modifyDpart:employeeOBJ.userId companyId:employeeOBJ.companyId newDepartmentId:newDepart.departId toView:self];
  278. OrganizTableViewCell * cell = (OrganizTableViewCell *)[self.treeView cellForItem:newDepart];
  279. if (!cell.arrowExpand) {
  280. cell.arrowExpand = YES;
  281. [self.treeView expandRowForItem:newDepart expandChildren:NO withRowAnimation:RATreeViewRowAnimationNone];
  282. }
  283. __weak typeof(self) weakSelf = self;
  284. self.rowActionAfterRequestBlock = ^(id sender) {
  285. DepartObject * oldDepart = [weakSelf.treeView parentForItem:employeeOBJ];
  286. NSInteger index = [oldDepart.children indexOfObject:employeeOBJ];
  287. [newDepart addChild:employeeOBJ];
  288. [oldDepart removeChild:employeeOBJ];
  289. [weakSelf.treeView insertItemsAtIndexes:[NSIndexSet indexSetWithIndex:0] inParent:newDepart withAnimation:RATreeViewRowAnimationNone];
  290. if (index < 0 || index > (oldDepart.children.count + 1)) {
  291. [weakSelf.treeView reloadData];
  292. }else {
  293. [weakSelf.treeView deleteItemsAtIndexes:[NSIndexSet indexSetWithIndex:index] inParent:oldDepart withAnimation:RATreeViewRowAnimationNone];
  294. }
  295. };
  296. }
  297. -(void)expandAllRows{
  298. for (DepartObject * depart in _dataArray) {
  299. if (!depart.parentId.length) {
  300. [_treeView expandRowForItem:depart expandChildren:NO withRowAnimation:RATreeViewRowAnimationAutomatic];
  301. }
  302. }
  303. [_treeView reloadRows];
  304. }
  305. #pragma mark 获取数据后处理及刷新
  306. /** 自动获取公司成树后reload */
  307. -(void)autoConstructTreeView:(NSArray *)originalArray{
  308. dispatch_async(dispatch_get_global_queue(0, 0), ^{
  309. NSArray * array = [self getRootArray:originalArray];
  310. dispatch_async(dispatch_get_main_queue(), ^{
  311. _dataArray = [NSMutableArray arrayWithArray:array];
  312. if (_dataArray.count > 0){
  313. [_treeView reloadData];
  314. [self performSelector:@selector(expandAllRows) withObject:nil afterDelay:0.1f];
  315. }
  316. });
  317. });
  318. }
  319. #pragma mark 数据成树
  320. /** 所有公司数据 */
  321. -(NSArray <DepartObject *>*) getRootArray:(NSArray *)originalArray{
  322. NSMutableArray * rootArr = [[NSMutableArray alloc] init];
  323. for (NSDictionary * companyDict in originalArray) {
  324. [_companyDict setObject:companyDict forKey:companyDict[@"id"]];
  325. NSArray * compRootDepartArr = [self constructCompanyObject:companyDict];
  326. [rootArr addObjectsFromArray:compRootDepartArr];
  327. }
  328. return rootArr;
  329. }
  330. /** 公司实体数据 - 返回根部门数组 */
  331. -(NSArray <DepartObject *>*) constructCompanyObject:(NSDictionary *)companyDict{
  332. NSArray *departDictArr = companyDict[@"departments"];
  333. // NSArray * rootDpartArr = companyDict[@"rootDpartId"];
  334. return [self constructDepartObject:departDictArr];
  335. }
  336. /** 部门列表 */
  337. -(NSArray <DepartObject *>*) constructDepartObject:(NSArray *)departArray{
  338. NSMutableArray * rootArr = [[NSMutableArray alloc] init];
  339. NSMutableDictionary * allDataDict = [NSMutableDictionary new];
  340. NSMutableArray *allDataArr = [NSMutableArray array];
  341. for (NSDictionary * departData in departArray) {
  342. if (!departData[@"parentId"]) {
  343. [rootArr addObject:departData];
  344. if (![_employeesDict objectForKey:departData[@"companyId"]])
  345. [_employeesDict setObject:[NSMutableSet set] forKey:departData[@"companyId"]];
  346. }
  347. [allDataDict setObject:departData forKey:departData[@"id"]];
  348. [allDataArr addObject:departData];
  349. }
  350. //
  351. for (NSDictionary *departData in departArray) {
  352. if (departData[@"employees"]) {
  353. NSMutableSet * emplySet = [_employeesDict objectForKey:departData[@"companyId"]];
  354. NSArray * emplArr = departData[@"employees"];
  355. for (NSDictionary * emp in emplArr) {
  356. if (emp[@"departmentId"] != nil && emp[@"userId"] != nil)
  357. [emplySet addObject:[NSString stringWithFormat:@"%@",emp[@"userId"]]];
  358. }
  359. }
  360. }
  361. NSMutableArray * departArr = [[NSMutableArray alloc] init];
  362. for (NSDictionary * rootData in rootArr) {
  363. DepartObject * departObj = [DepartObject departmentObjectWith:rootData allData:allDataArr];
  364. [departArr addObject:departObj];
  365. }
  366. [_allDataDict addEntriesFromDictionary:allDataDict];
  367. return departArr;
  368. }
  369. #pragma mark - **数据请求**
  370. -(void) didServerResultSucces:(JXConnection*)aDownload dict:(NSDictionary*)dict array:(NSArray*)array1{
  371. [_wait stop];
  372. if([aDownload.action isEqualToString:act_getCompany]){//自动查找公司
  373. if (!array1) {
  374. }else{
  375. [self autoConstructTreeView:array1];
  376. }
  377. }
  378. }
  379. #pragma mark -
  380. -(int) didServerResultFailed:(JXConnection*)aDownload dict:(NSDictionary*)dict{
  381. [_wait stop];
  382. self.rowActionAfterRequestBlock = nil;
  383. return show_error;
  384. }
  385. -(int) didServerConnectError:(JXConnection*)aDownload error:(NSError *)error{//error为空时,代表超时
  386. [_wait stop];
  387. self.rowActionAfterRequestBlock = nil;
  388. return show_error;
  389. }
  390. -(void) didServerConnectStart:(JXConnection*)aDownload{
  391. dispatch_async(dispatch_get_main_queue(), ^{
  392. [_wait start];
  393. });
  394. }
  395. @end