// // LogisticsViewModel.m // FirstLink // // Created by ascii on 15/6/13. // Copyright (c) 2015年 FirstLink. All rights reserved. // #import "LogisticsViewModel.h" #import "FKPackageLogisticsItem.h" @implementation LogisticsViewModel - (void)requestLogistics:(NSString *)waybillID logisticsID:(NSString *)logisticsID success:(void (^)(MSGHeader *, NSDictionary *))success failure:(void (^)(MSGHeader *, NSError *))failure { if (!waybillID || waybillID.length == 0) { return; } NSString *itemJSONString = [FLRequestHelper JSONStringWithKeys:@[@"waybill_id", @"id"] values:@[waybillID, [FLStringHelper replaceNilWithEmpty:logisticsID]]]; NSMutableDictionary *para = [NSMutableDictionary dictionary]; [para setValue:itemJSONString forKey:@"logistics_json"]; [para addEntriesFromDictionary:[FLRequestHelper commonParamater]]; NSString *URLString = [NSString stringWithFormat:@"%@/link-site/api/logistics/find_logisticss_for_order_new.json", [[FKServerUtil sharedInstance] apiServer]]; FLDataCenter *dataCenter = [FLDataCenter sharedDataCenter]; [dataCenter POST:URLString parameters:para success:^(MSGHeader *header, id responseObject) { if ([header.code intValue] == RESPONSE_MSG_NORMAL) { success(header, responseObject); } else { success(header, responseObject); } } failure:^(MSGHeader *header, NSError *error) { failure(header, error); }]; } + (NSMutableArray *)parsePackages:(NSArray *)json { if ([json isKindOfClass:[NSArray class]]) { FKPackageLogisticsItem *packageItem; NSMutableArray *mutableArray = [NSMutableArray array]; NSMutableArray *goodArray; NSMutableArray *logisticsArray; FKOrderGoodItem *goodItem; LogisticsItem *logisticsItem; for (NSDictionary *dict in json) { packageItem = [[FKPackageLogisticsItem alloc] init]; goodArray = [NSMutableArray array]; for (NSDictionary *goodDict in dict[@"goods_list"]) { goodItem = [[FKOrderGoodItem alloc] init]; [goodItem mts_setValuesForKeysWithDictionary:goodDict]; [goodArray addObject:goodItem]; } packageItem.goodArray = goodArray; logisticsArray = [NSMutableArray array]; for (NSDictionary *logisticDict in dict[@"logistics_list"]) { logisticsItem = [[LogisticsItem alloc] init]; [logisticsItem mts_setValuesForKeysWithDictionary:logisticDict]; [logisticsArray addObject:logisticsItem]; } packageItem.logisticsArray = logisticsArray; if (dict[@"selected"] != [NSNull null]) { packageItem.selected = [NSString stringWithFormat:@"%@", dict[@"selected"]]; } [mutableArray addObject:packageItem]; } return mutableArray; } return nil; } + (NSString *)parseLogisticsStatus:(NSDictionary *)json { if (json[@"data"][@"bind_status"] != [NSNull null]) { return [NSString stringWithFormat:@"%@", json[@"data"][@"bind_status"]]; } return nil; } #pragma mark - Method - (NSArray *)selectPackageArray { if (self.cardSegmentIndex == kCardSegmentIndexLeft) { return self.inPackageArray; } return self.outPackageArray; } - (NSInteger)numberOfSectionsWithSegmentIndex:(NSInteger)index { NSArray *array = [self selectPackageArray]; if (index < array.count) { return 2; } return 0; } - (NSInteger)numberOfRowsInSection:(NSInteger)section withSegmentIndex:(NSInteger)index { NSArray *array = [self selectPackageArray]; if (index < array.count) { FKPackageLogisticsItem *package = array[index]; if (section == 0) { return (package.goodArray.count + 1); } return (package.logisticsArray.count + 1); } return 0; } - (kLogisticsCellType)cellTypeAtIndexPath:(NSIndexPath *)indexPath withSegmentIndex:(NSInteger)index { NSArray *array = [self selectPackageArray]; if (index < array.count) { FKPackageLogisticsItem *package = array[index]; if (indexPath.section == 0) { if (indexPath.row < package.goodArray.count) { return kLogisticsCellTypeProduct; } return kLogisticsCellTypeTimeOut; } else { NSInteger rowCount = (package.logisticsArray.count + 1); if (indexPath.row == 0) { return kLogisticsCellTypeSuccess; } else if (indexPath.row == (rowCount - 1)) { return kLogisticsCellTypeQuestionAnswer; } else { return kLogisticsCellTypeProgress; } } } return kLogisticsCellTypeProgress; } - (FKOrderGoodItem *)orderGoodItemAtIndexPath:(NSIndexPath *)indexPath withSegmentIndex:(NSInteger)index { NSArray *array = [self selectPackageArray]; FKPackageLogisticsItem *package = array[index]; if (indexPath.row < package.goodArray.count) { return package.goodArray[indexPath.row]; } return nil; } - (LogisticsItem *)logisticsItemAtIndexPath:(NSIndexPath *)indexPath withSegmentIndex:(NSInteger)index { NSArray *array = [self selectPackageArray]; FKPackageLogisticsItem *package = array[index]; if (indexPath.row < package.logisticsArray.count) { return package.logisticsArray[indexPath.row]; } return nil; } - (NSArray *)sectionTitleArray { NSArray *array = [self selectPackageArray]; if (array.count == 0) { return nil; } NSMutableArray *titleArray = [NSMutableArray array]; for (int index = 0; index < array.count; index++) { [titleArray addObject:[NSString stringWithFormat:@"包裹%d", (index+1)]]; } return titleArray; } - (NSInteger)selectedIndex { NSArray *array = [self selectPackageArray]; __block NSInteger selectedIndex = 0; [array enumerateObjectsUsingBlock:^(FKPackageLogisticsItem *obj, NSUInteger idx, BOOL * _Nonnull stop) { if ([obj.selected isEqualToString:@"1"]) { selectedIndex = idx; *stop = YES; } }]; return selectedIndex; } - (BOOL)isPackageCompleted { if (self.logisticsStatus && [self.logisticsStatus intValue] > 1) { return NO; } return YES; } @end