WFCUOrganizationCache.m 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284
  1. //
  2. // WFCUOrganizationCache.m
  3. // WFChatUIKit
  4. //
  5. // Created by Rain on 2022/12/25.
  6. // Copyright © 2022 WildfireChat. All rights reserved.
  7. //
  8. #import "WFCUOrganizationCache.h"
  9. #import "WFCUEmployee.h"
  10. #import "WFCUOrganization.h"
  11. #import "WFCUOrgRelationship.h"
  12. #import "WFCUOrganizationEx.h"
  13. #import "WFCUConfigManager.h"
  14. #import "WFCUEmployeeEx.h"
  15. #import <WFChatClient/WFCChatClient.h>
  16. NSString *kRootOrganizationUpdated = @"kRootOrganizationUpdated";
  17. NSString *kMyOrganizationUpdated = @"kMyOrganizationUpdated";
  18. NSString *kOrganizationUpdated = @"kOrganizationUpdated";
  19. NSString *kOrganizationExUpdated = @"kOrganizationExUpdated";
  20. NSString *kEmployeeUpdated = @"kEmployeeUpdated";
  21. NSString *kEmployeeExUpdated = @"kEmployeeExUpdated";
  22. NSString *kOrgRelationUpdated = @"kOrgRelationUpdated";
  23. static WFCUOrganizationCache *sharedSingleton = nil;
  24. @interface WFCUOrganizationCache ()
  25. @property(nonatomic, strong)NSMutableDictionary<NSString *, WFCUEmployee *> *employeeDict;
  26. @property(nonatomic, strong)NSMutableDictionary<NSString *, WFCUEmployeeEx *> *employeeExDict;
  27. @property(nonatomic, strong)NSMutableDictionary<NSNumber *, WFCUOrganization *> *organizationDict;
  28. @property(nonatomic, strong)NSMutableDictionary<NSNumber *, WFCUOrganizationEx *> *organizationExDict;
  29. @property(nonatomic, strong)NSMutableDictionary<NSString *, NSArray<WFCUOrgRelationship *> *> *relationshipDict;
  30. @end
  31. @implementation WFCUOrganizationCache
  32. + (WFCUOrganizationCache *)sharedCache {
  33. if (sharedSingleton == nil) {
  34. @synchronized (self) {
  35. if (sharedSingleton == nil) {
  36. sharedSingleton = [[WFCUOrganizationCache alloc] init];
  37. sharedSingleton.employeeDict = [[NSMutableDictionary alloc] init];
  38. sharedSingleton.employeeExDict = [[NSMutableDictionary alloc] init];
  39. sharedSingleton.organizationDict = [[NSMutableDictionary alloc] init];
  40. sharedSingleton.organizationExDict = [[NSMutableDictionary alloc] init];
  41. sharedSingleton.relationshipDict = [[NSMutableDictionary alloc] init];
  42. [sharedSingleton restoreMyOrganizationInfos];
  43. }
  44. }
  45. }
  46. return sharedSingleton;
  47. }
  48. - (void)restoreMyOrganizationInfos {
  49. //restore bottomOrganizationIds, rootOrganizationIds, rootOrganization
  50. NSMutableArray *arr = [[NSMutableArray alloc] init];
  51. NSArray *arrBottoms = [[NSUserDefaults standardUserDefaults] objectForKey:@"WFC_bottomOrganizationIds"];
  52. if([arrBottoms isKindOfClass:[NSArray class]]) {
  53. [arr addObjectsFromArray:arrBottoms];
  54. self.bottomOrganizationIds = arrBottoms;
  55. }
  56. NSArray *arrRoots = [[NSUserDefaults standardUserDefaults] objectForKey:@"WFC_rootOrganizationIds"];
  57. if([arrRoots isKindOfClass:[NSArray class]]) {
  58. [arr addObjectsFromArray:arrRoots];
  59. self.rootOrganizationIds = arrRoots;
  60. }
  61. [arr enumerateObjectsUsingBlock:^(NSNumber * _Nonnull obj, NSUInteger idx, BOOL * _Nonnull stop) {
  62. NSDictionary *dict = [[NSUserDefaults standardUserDefaults] objectForKey:[NSString stringWithFormat:@"WFC_organization_%ld", [obj integerValue]]];
  63. if([dict isKindOfClass:[NSDictionary class]]) {
  64. WFCUOrganization *org = [WFCUOrganization fromDict:dict];
  65. if(org.organizationId) {
  66. self.organizationDict[@(org.organizationId)] = org;
  67. }
  68. }
  69. }];
  70. }
  71. - (void)clearCaches {
  72. [[NSUserDefaults standardUserDefaults] removeObjectForKey:@"WFC_bottomOrganizationIds"];
  73. [[NSUserDefaults standardUserDefaults] removeObjectForKey:@"WFC_rootOrganizationIds"];
  74. self.bottomOrganizationIds = nil;
  75. self.rootOrganizationIds = nil;
  76. }
  77. - (void)loadMyOrganizationInfos {
  78. [[WFCUConfigManager globalManager].orgServiceProvider getRelationship:[WFCCNetworkService sharedInstance].userId success:^(NSArray<WFCUOrgRelationship *> * _Nonnull relationships) {
  79. self.relationshipDict[[WFCCNetworkService sharedInstance].userId] = relationships;
  80. NSMutableArray<NSNumber *> *bottomIds = [[NSMutableArray alloc] init];
  81. [relationships enumerateObjectsUsingBlock:^(WFCUOrgRelationship * _Nonnull obj, NSUInteger idx, BOOL * _Nonnull stop) {
  82. if(obj.bottom) [bottomIds addObject:@(obj.organizationId)];
  83. }];
  84. self.bottomOrganizationIds = bottomIds;
  85. [[NSUserDefaults standardUserDefaults] setObject:self.bottomOrganizationIds forKey:@"WFC_bottomOrganizationIds"];
  86. if(bottomIds.count) {
  87. [[WFCUConfigManager globalManager].orgServiceProvider getOrganizations:bottomIds success:^(NSArray<WFCUOrganization *> * _Nonnull organizations) {
  88. [organizations enumerateObjectsUsingBlock:^(WFCUOrganization * _Nonnull obj, NSUInteger idx, BOOL * _Nonnull stop) {
  89. self.organizationDict[@(obj.organizationId)] = obj;
  90. [[NSUserDefaults standardUserDefaults] setObject:[obj toDict] forKey:[NSString stringWithFormat:@"WFC_organization_%ld", obj.organizationId]];
  91. }];
  92. } error:^(int error_code) {
  93. }];
  94. [[NSNotificationCenter defaultCenter] postNotificationName:kMyOrganizationUpdated object:nil];
  95. }
  96. } error:^(int error_code) {
  97. }];
  98. [[WFCUConfigManager globalManager].orgServiceProvider getRootOrganization:^(NSArray<WFCUOrganization *> * _Nonnull organizations) {
  99. NSMutableArray<NSNumber *> *orgIds = [[NSMutableArray alloc] init];
  100. [organizations enumerateObjectsUsingBlock:^(WFCUOrganization * _Nonnull obj, NSUInteger idx, BOOL * _Nonnull stop) {
  101. [orgIds addObject:@(obj.organizationId)];
  102. self.organizationDict[@(obj.organizationId)] = obj;
  103. [[NSUserDefaults standardUserDefaults] setObject:[obj toDict] forKey:[NSString stringWithFormat:@"WFC_organization_%ld", obj.organizationId]];
  104. }];
  105. self.rootOrganizationIds = orgIds;
  106. [[NSUserDefaults standardUserDefaults] setObject:self.rootOrganizationIds forKey:@"WFC_rootOrganizationIds"];
  107. [[NSNotificationCenter defaultCenter] postNotificationName:kRootOrganizationUpdated object:nil];
  108. } error:^(int error_code) {
  109. }];
  110. }
  111. - (void)getRelationship:(NSString *)employeeId
  112. refresh:(BOOL)refresh
  113. success:(void(^)(NSString *employeeId, NSArray<WFCUOrgRelationship *> *rss))successBlock
  114. error:(void(^)(int error_code))errorBlock {
  115. NSArray<WFCUOrgRelationship *> * rs = self.relationshipDict[employeeId];
  116. if(!rs) {
  117. refresh = YES;
  118. } else {
  119. if(successBlock) successBlock(employeeId, rs);
  120. }
  121. if(refresh) {
  122. [[WFCUConfigManager globalManager].orgServiceProvider getRelationship:employeeId success:^(NSArray<WFCUOrgRelationship *> * _Nonnull arr) {
  123. self.relationshipDict[employeeId] = arr;
  124. [[NSNotificationCenter defaultCenter] postNotificationName:kOrgRelationUpdated object:employeeId userInfo:@{@"relationships":arr}];
  125. if(successBlock) successBlock(employeeId, arr);
  126. } error:^(int error_code) {
  127. if(errorBlock) errorBlock(error_code);
  128. }];
  129. }
  130. }
  131. - (NSArray<WFCUOrgRelationship *> *)getRelationship:(NSString *)employeeId refresh:(BOOL)refresh {
  132. NSArray<WFCUOrgRelationship *> * rs = self.relationshipDict[employeeId];
  133. if(!rs) {
  134. refresh = YES;
  135. }
  136. if(refresh) {
  137. [[WFCUConfigManager globalManager].orgServiceProvider getRelationship:employeeId success:^(NSArray<WFCUOrgRelationship *> * _Nonnull arr) {
  138. self.relationshipDict[employeeId] = arr;
  139. [[NSNotificationCenter defaultCenter] postNotificationName:kOrgRelationUpdated object:employeeId userInfo:@{@"relationships":arr}];
  140. } error:^(int error_code) {
  141. }];
  142. }
  143. return rs;
  144. }
  145. - (WFCUEmployee *)getEmployee:(NSString *)employeeId refresh:(BOOL)refresh {
  146. WFCUEmployee *employee = self.employeeDict[employeeId];
  147. if(!employee) {
  148. refresh = YES;
  149. }
  150. if(refresh) {
  151. [[WFCUConfigManager globalManager].orgServiceProvider getEmployee:employeeId success:^(WFCUEmployee * _Nonnull employee) {
  152. self.employeeDict[employeeId] = employee;
  153. [[NSNotificationCenter defaultCenter] postNotificationName:kEmployeeUpdated object:employeeId userInfo:@{@"employee":employee}];
  154. } error:^(int error_code) {
  155. }];
  156. }
  157. return employee;
  158. }
  159. - (WFCUEmployeeEx *)getEmployeeEx:(NSString *)employeeId refresh:(BOOL)refresh {
  160. WFCUEmployeeEx *ex = self.employeeExDict[employeeId];
  161. if(!ex) {
  162. refresh = YES;
  163. ex = [[WFCUEmployeeEx alloc] init];
  164. ex.employeeId = employeeId;
  165. ex.employee = self.employeeDict[employeeId];
  166. ex.relationships = self.relationshipDict[employeeId];
  167. }
  168. if(refresh) {
  169. [[WFCUConfigManager globalManager].orgServiceProvider getEmployeeEx:employeeId success:^(WFCUEmployeeEx * _Nonnull employeeEx) {
  170. self.employeeExDict[employeeId] = employeeEx;
  171. self.employeeDict[employeeId] = employeeEx.employee;
  172. self.relationshipDict[employeeId] = employeeEx.relationships;
  173. [[NSNotificationCenter defaultCenter] postNotificationName:kEmployeeExUpdated object:employeeId userInfo:@{@"employee":employeeEx.employee, @"relationships":employeeEx.relationships}];
  174. } error:^(int error_code) {
  175. }];
  176. }
  177. return ex;
  178. }
  179. - (WFCUOrganization *)getOrganization:(NSInteger)orgnaizationId refresh:(BOOL)refresh {
  180. WFCUOrganization *org = self.organizationDict[@(orgnaizationId)];
  181. if(!org) {
  182. refresh = YES;
  183. }
  184. if(refresh) {
  185. [[WFCUConfigManager globalManager].orgServiceProvider getOrganizations:@[@(orgnaizationId)] success:^(NSArray<WFCUOrganization *> * _Nonnull organizations) {
  186. [organizations enumerateObjectsUsingBlock:^(WFCUOrganization * _Nonnull obj, NSUInteger idx, BOOL * _Nonnull stop) {
  187. self.organizationDict[@(obj.organizationId)] = obj;
  188. }];
  189. [[NSNotificationCenter defaultCenter] postNotificationName:kOrganizationUpdated object:@(orgnaizationId) userInfo:@{@"organization":organizations.firstObject}];
  190. } error:^(int error_code) {
  191. }];
  192. }
  193. return org;
  194. }
  195. - (void)getOrganizationEx:(NSInteger)organizationId
  196. refresh:(BOOL)refresh
  197. success:(void(^)(NSInteger organizationId, WFCUOrganizationEx *ex))successBlock
  198. error:(void(^)(int error_code))errorBlock {
  199. WFCUOrganizationEx *ex = self.organizationExDict[@(organizationId)];
  200. if(!ex) {
  201. refresh = YES;
  202. } else {
  203. if(successBlock) successBlock(organizationId, ex);
  204. }
  205. if(refresh) {
  206. [[WFCUConfigManager globalManager].orgServiceProvider getOrganizationEx:organizationId success:^(WFCUOrganizationEx *newEx) {
  207. [newEx.subOrganizations enumerateObjectsUsingBlock:^(WFCUOrganization * _Nonnull obj, NSUInteger idx, BOOL * _Nonnull stop) {
  208. self.organizationDict[@(obj.organizationId)] = obj;
  209. }];
  210. [newEx.employees enumerateObjectsUsingBlock:^(WFCUEmployee * _Nonnull obj, NSUInteger idx, BOOL * _Nonnull stop) {
  211. self.employeeDict[obj.employeeId] = obj;
  212. }];
  213. self.organizationExDict[@(organizationId)] = newEx;
  214. [[NSNotificationCenter defaultCenter] postNotificationName:kOrganizationExUpdated object:@(organizationId)];
  215. if(successBlock) successBlock(organizationId, newEx);
  216. } error:^(int error_code) {
  217. if(errorBlock) errorBlock(error_code);
  218. }];
  219. }
  220. }
  221. - (WFCUOrganizationEx *)getOrganizationEx:(NSInteger)organizationId refresh:(BOOL)refresh {
  222. WFCUOrganizationEx *ex = self.organizationExDict[@(organizationId)];
  223. if(!ex) {
  224. ex = [[WFCUOrganizationEx alloc] init];
  225. ex.organizationId = organizationId;
  226. ex.organization = [self getOrganization:organizationId refresh:NO];
  227. refresh = YES;
  228. }
  229. if(refresh) {
  230. [[WFCUConfigManager globalManager].orgServiceProvider getOrganizationEx:organizationId success:^(WFCUOrganizationEx *newEx) {
  231. [newEx.subOrganizations enumerateObjectsUsingBlock:^(WFCUOrganization * _Nonnull obj, NSUInteger idx, BOOL * _Nonnull stop) {
  232. self.organizationDict[@(obj.organizationId)] = obj;
  233. }];
  234. [newEx.employees enumerateObjectsUsingBlock:^(WFCUEmployee * _Nonnull obj, NSUInteger idx, BOOL * _Nonnull stop) {
  235. self.employeeDict[obj.employeeId] = obj;
  236. }];
  237. self.organizationExDict[@(organizationId)] = newEx;
  238. [[NSNotificationCenter defaultCenter] postNotificationName:kOrganizationExUpdated object:@(organizationId)];
  239. } error:^(int error_code) {
  240. }];
  241. }
  242. return ex;
  243. }
  244. @end