Bez popisu

LogisticsViewModel.m 6.7KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201
  1. //
  2. // LogisticsViewModel.m
  3. // FirstLink
  4. //
  5. // Created by ascii on 15/6/13.
  6. // Copyright (c) 2015年 FirstLink. All rights reserved.
  7. //
  8. #import "LogisticsViewModel.h"
  9. #import "FKPackageLogisticsItem.h"
  10. @implementation LogisticsViewModel
  11. - (void)requestLogistics:(NSString *)waybillID
  12. logisticsID:(NSString *)logisticsID
  13. success:(void (^)(MSGHeader *, NSDictionary *))success
  14. failure:(void (^)(MSGHeader *, NSError *))failure {
  15. if (!waybillID || waybillID.length == 0) {
  16. return;
  17. }
  18. NSString *itemJSONString = [FLRequestHelper JSONStringWithKeys:@[@"waybill_id", @"id"]
  19. values:@[waybillID, [FLStringHelper replaceNilWithEmpty:logisticsID]]];
  20. NSMutableDictionary *para = [NSMutableDictionary dictionary];
  21. [para setValue:itemJSONString forKey:@"logistics_json"];
  22. [para addEntriesFromDictionary:[FLRequestHelper commonParamater]];
  23. NSString *URLString = [NSString stringWithFormat:@"%@/link-site/api/logistics/find_logisticss_for_order_new.json", [[FKServerUtil sharedInstance] apiServer]];
  24. FLDataCenter *dataCenter = [FLDataCenter sharedDataCenter];
  25. [dataCenter POST:URLString
  26. parameters:para
  27. success:^(MSGHeader *header, id responseObject)
  28. {
  29. if ([header.code intValue] == RESPONSE_MSG_NORMAL) {
  30. success(header, responseObject);
  31. } else {
  32. success(header, responseObject);
  33. }
  34. } failure:^(MSGHeader *header, NSError *error) {
  35. failure(header, error);
  36. }];
  37. }
  38. + (NSMutableArray<FKPackageLogisticsItem *> *)parsePackages:(NSArray *)json {
  39. if ([json isKindOfClass:[NSArray class]]) {
  40. FKPackageLogisticsItem *packageItem;
  41. NSMutableArray *mutableArray = [NSMutableArray array];
  42. NSMutableArray *goodArray;
  43. NSMutableArray *logisticsArray;
  44. FKOrderGoodItem *goodItem;
  45. LogisticsItem *logisticsItem;
  46. for (NSDictionary *dict in json) {
  47. packageItem = [[FKPackageLogisticsItem alloc] init];
  48. goodArray = [NSMutableArray array];
  49. for (NSDictionary *goodDict in dict[@"goods_list"]) {
  50. goodItem = [[FKOrderGoodItem alloc] init];
  51. [goodItem mts_setValuesForKeysWithDictionary:goodDict];
  52. [goodArray addObject:goodItem];
  53. }
  54. packageItem.goodArray = goodArray;
  55. logisticsArray = [NSMutableArray array];
  56. for (NSDictionary *logisticDict in dict[@"logistics_list"]) {
  57. logisticsItem = [[LogisticsItem alloc] init];
  58. [logisticsItem mts_setValuesForKeysWithDictionary:logisticDict];
  59. [logisticsArray addObject:logisticsItem];
  60. }
  61. packageItem.logisticsArray = logisticsArray;
  62. if (dict[@"selected"] != [NSNull null]) {
  63. packageItem.selected = [NSString stringWithFormat:@"%@", dict[@"selected"]];
  64. }
  65. [mutableArray addObject:packageItem];
  66. }
  67. return mutableArray;
  68. }
  69. return nil;
  70. }
  71. + (NSString *)parseLogisticsStatus:(NSDictionary *)json {
  72. if (json[@"data"][@"bind_status"] != [NSNull null]) {
  73. return [NSString stringWithFormat:@"%@", json[@"data"][@"bind_status"]];
  74. }
  75. return nil;
  76. }
  77. #pragma mark - Method
  78. - (NSArray<FKPackageLogisticsItem *> *)selectPackageArray {
  79. if (self.cardSegmentIndex == kCardSegmentIndexLeft) {
  80. return self.inPackageArray;
  81. }
  82. return self.outPackageArray;
  83. }
  84. - (NSInteger)numberOfSectionsWithSegmentIndex:(NSInteger)index {
  85. NSArray<FKPackageLogisticsItem *> *array = [self selectPackageArray];
  86. if (index < array.count) {
  87. return 2;
  88. }
  89. return 0;
  90. }
  91. - (NSInteger)numberOfRowsInSection:(NSInteger)section withSegmentIndex:(NSInteger)index {
  92. NSArray<FKPackageLogisticsItem *> *array = [self selectPackageArray];
  93. if (index < array.count) {
  94. FKPackageLogisticsItem *package = array[index];
  95. if (section == 0) {
  96. return (package.goodArray.count + 1);
  97. }
  98. return (package.logisticsArray.count + 1);
  99. }
  100. return 0;
  101. }
  102. - (kLogisticsCellType)cellTypeAtIndexPath:(NSIndexPath *)indexPath withSegmentIndex:(NSInteger)index {
  103. NSArray<FKPackageLogisticsItem *> *array = [self selectPackageArray];
  104. if (index < array.count) {
  105. FKPackageLogisticsItem *package = array[index];
  106. if (indexPath.section == 0) {
  107. if (indexPath.row < package.goodArray.count) {
  108. return kLogisticsCellTypeProduct;
  109. }
  110. return kLogisticsCellTypeTimeOut;
  111. } else {
  112. NSInteger rowCount = (package.logisticsArray.count + 1);
  113. if (indexPath.row == 0) {
  114. return kLogisticsCellTypeSuccess;
  115. } else if (indexPath.row == (rowCount - 1)) {
  116. return kLogisticsCellTypeQuestionAnswer;
  117. } else {
  118. return kLogisticsCellTypeProgress;
  119. }
  120. }
  121. }
  122. return kLogisticsCellTypeProgress;
  123. }
  124. - (FKOrderGoodItem *)orderGoodItemAtIndexPath:(NSIndexPath *)indexPath withSegmentIndex:(NSInteger)index {
  125. NSArray<FKPackageLogisticsItem *> *array = [self selectPackageArray];
  126. FKPackageLogisticsItem *package = array[index];
  127. if (indexPath.row < package.goodArray.count) {
  128. return package.goodArray[indexPath.row];
  129. }
  130. return nil;
  131. }
  132. - (LogisticsItem *)logisticsItemAtIndexPath:(NSIndexPath *)indexPath withSegmentIndex:(NSInteger)index {
  133. NSArray<FKPackageLogisticsItem *> *array = [self selectPackageArray];
  134. FKPackageLogisticsItem *package = array[index];
  135. if (indexPath.row < package.logisticsArray.count) {
  136. return package.logisticsArray[indexPath.row];
  137. }
  138. return nil;
  139. }
  140. - (NSArray *)sectionTitleArray {
  141. NSArray<FKPackageLogisticsItem *> *array = [self selectPackageArray];
  142. if (array.count == 0) {
  143. return nil;
  144. }
  145. NSMutableArray *titleArray = [NSMutableArray array];
  146. for (int index = 0; index < array.count; index++) {
  147. [titleArray addObject:[NSString stringWithFormat:@"包裹%d", (index+1)]];
  148. }
  149. return titleArray;
  150. }
  151. - (NSInteger)selectedIndex {
  152. NSArray<FKPackageLogisticsItem *> *array = [self selectPackageArray];
  153. __block NSInteger selectedIndex = 0;
  154. [array enumerateObjectsUsingBlock:^(FKPackageLogisticsItem *obj, NSUInteger idx, BOOL * _Nonnull stop) {
  155. if ([obj.selected isEqualToString:@"1"]) {
  156. selectedIndex = idx;
  157. *stop = YES;
  158. }
  159. }];
  160. return selectedIndex;
  161. }
  162. - (BOOL)isPackageCompleted {
  163. if (self.logisticsStatus && [self.logisticsStatus intValue] > 1) {
  164. return NO;
  165. }
  166. return YES;
  167. }
  168. @end