DepartObject.m 7.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212
  1. //
  2. // DepartObject.m
  3. // shiku_im
  4. //
  5. // Created by 1 on 17/5/11.
  6. // Copyright © 2017年 Reese. All rights reserved.
  7. //
  8. #import "DepartObject.h"
  9. #import "EmployeObject.h"
  10. @interface DepartObject()
  11. {
  12. NSMutableArray * _allDict;
  13. }
  14. @end
  15. @implementation DepartObject
  16. -(instancetype)initDepart:(NSDictionary *)nodeDict allData:(NSMutableArray *)allDict{
  17. self = [super init];
  18. if (self) {
  19. if (nodeDict[@"departName"] != nil)
  20. self.departName = nodeDict[@"departName"];
  21. if (nodeDict[@"id"] != nil)
  22. self.departId = nodeDict[@"id"];
  23. if (nodeDict[@"companyId"] != nil)
  24. self.companyId = nodeDict[@"companyId"];
  25. if (nodeDict[@"createUserId"] != nil)
  26. self.createUserId = [NSString stringWithFormat:@"%@",nodeDict[@"createUserId"]];
  27. if (nodeDict[@"empNum"] != nil)
  28. self.empNum = [nodeDict[@"empNum"] integerValue];
  29. if (nodeDict[@"employees"] != nil)
  30. self.employees = nodeDict[@"employees"];
  31. if (nodeDict[@"parentId"] != nil) {
  32. self.parentId = nodeDict[@"parentId"];
  33. }
  34. if (nodeDict[@"noticeContent"] != nil)
  35. self.noticeContent = nodeDict[@"noticeContent"];
  36. if (allDict) {
  37. _allDict = allDict;
  38. NSMutableArray * departArr = [[NSMutableArray alloc] init];
  39. for (NSDictionary * data in allDict) {
  40. // NSDictionary * data = [allDict objectForKey:key];
  41. if (data[@"parentId"] != nil && [data[@"parentId"] isEqualToString:self.departId]) {
  42. [departArr addObject:data];
  43. }
  44. }
  45. if (departArr.count > 0) {
  46. self.departes = departArr;
  47. }else{
  48. self.departes = [NSArray array];
  49. }
  50. NSMutableArray * childArr = [NSMutableArray array];
  51. if (self.departes.count > 0) {
  52. [childArr addObjectsFromArray:self.departes];
  53. }
  54. if (self.employees.count > 0) {
  55. [childArr addObjectsFromArray:self.employees];
  56. }
  57. if (childArr.count > 0) {
  58. self.children = childArr;
  59. }
  60. }
  61. if (!_children)
  62. _children = [NSArray array];
  63. if (!_employees)
  64. _employees = [NSArray array];
  65. if (!_departes)
  66. _departes = [NSArray array];
  67. _allDict = nil;
  68. // {
  69. // "companyId": "591e5ea35da45bc34f99d940",
  70. // "createTime": 1495162531,
  71. // "createUserId": 10007882,
  72. // "departName": "跳跳糖",
  73. // "empNum": 1,
  74. // "employees": [
  75. // {
  76. // "companyId": "591e5ea35da45bc34f99d940",
  77. // "departmentId": "591e5ea35da45bc34f99d941",
  78. // "id": "591e5ea35da45bc34f99d942",
  79. // "role": 3,
  80. // "userId": 10007882
  81. // }
  82. // ],
  83. // "id": "591e5ea35da45bc34f99d941"
  84. // }
  85. }
  86. return self;
  87. }
  88. -(void)setDepartes:(NSArray *)departes{
  89. if (departes != nil && departes.count > 0) {
  90. if ([departes[0] isKindOfClass:[DepartObject class]]) {
  91. _departes = departes;
  92. NSMutableArray * childArr = [NSMutableArray array];
  93. [childArr addObjectsFromArray:_departes];
  94. [childArr addObjectsFromArray:_employees];
  95. _children = childArr;
  96. return;
  97. }
  98. NSMutableArray *childArray = [NSMutableArray array];
  99. for (NSDictionary * childDict in departes) {
  100. DepartObject * departObj = [DepartObject departmentObjectWith:childDict allData:_allDict];
  101. [childArray addObject:departObj];
  102. }
  103. _departes = [NSArray arrayWithArray:childArray];
  104. }else{
  105. _departes = [NSArray array];
  106. }
  107. }
  108. /** 创建员工节点 */
  109. -(void)setEmployees:(NSArray *)employees{
  110. if (employees != nil && employees.count > 0) {
  111. if ([employees[0] isKindOfClass:[EmployeObject class]]) {
  112. _employees = employees;
  113. _empNum = _employees.count;
  114. NSMutableArray * childArr = [NSMutableArray array];
  115. [childArr addObjectsFromArray:_departes];
  116. [childArr addObjectsFromArray:_employees];
  117. _children = childArr;
  118. return;
  119. }
  120. NSMutableArray *childArray = [NSMutableArray array];
  121. for (NSDictionary * childDict in employees) {
  122. EmployeObject * employeObj = [EmployeObject employWithDict:childDict];
  123. [childArray addObject:employeObj];
  124. }
  125. _employees = [NSArray arrayWithArray:childArray];
  126. }else{
  127. _employees = [NSArray array];
  128. }
  129. }
  130. -(void)setChildren:(NSArray *)children{
  131. if ([children[0] isKindOfClass:[DepartObject class]] || [children[0] isKindOfClass:[EmployeObject class]]) {
  132. _children = children;
  133. return;
  134. }
  135. // NSMutableArray *childArray = [NSMutableArray array];
  136. // for (NSDictionary * childDict in children) {
  137. // if (!childDict[@"userId"]) {
  138. //// EmployeObject * employ = [EmployeObject employ:childDict];
  139. //// [childArray addObject:];
  140. // }else if () {
  141. //// DepartObject * depart = [DepartObject de];
  142. //// [childArray addObject:organiz];
  143. // }
  144. //
  145. // }
  146. // _children = [NSArray arrayWithArray:childArray];
  147. }
  148. - (void)addChild:(id)child
  149. {
  150. NSMutableArray *children = [self.children mutableCopy];
  151. if ([child isMemberOfClass:[DepartObject class]]) {
  152. NSMutableArray *departs = [self.departes mutableCopy];
  153. [departs insertObject:child atIndex:0];
  154. _departes = [departs copy];
  155. [children insertObject:child atIndex:0];
  156. }else if ([child isMemberOfClass:[EmployeObject class]]) {
  157. NSMutableArray *employee = [self.employees mutableCopy];
  158. [employee insertObject:child atIndex:0];
  159. _employees = [employee copy];
  160. NSUInteger insert = _departes.count ? (_departes.count -1) : 0;
  161. [children insertObject:child atIndex:(insert)];
  162. _empNum = _employees.count;
  163. }
  164. _children = [children copy];
  165. }
  166. - (void)removeChild:(id)child
  167. {
  168. if ([child isMemberOfClass:[DepartObject class]]) {
  169. NSMutableArray *departs = [self.departes mutableCopy];
  170. [departs removeObject:child];
  171. _departes = [departs copy];
  172. }else if ([child isMemberOfClass:[EmployeObject class]]) {
  173. NSMutableArray *employee = [self.children mutableCopy];
  174. [employee removeObject:child];
  175. _employees = [employee copy];
  176. _empNum = _employees.count;
  177. }
  178. NSMutableArray *children = [self.children mutableCopy];
  179. [children removeObject:child];
  180. _children = [children copy];
  181. }
  182. /** 创建部门节点 */
  183. +(instancetype)departmentObjectWith:(NSDictionary *)nodeDict allData:(NSMutableArray *)allDict{
  184. return [[self alloc] initDepart:nodeDict allData:allDict];
  185. }
  186. @end