123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201 |
- //
- // 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<FKPackageLogisticsItem *> *)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<FKPackageLogisticsItem *> *)selectPackageArray {
- if (self.cardSegmentIndex == kCardSegmentIndexLeft) {
- return self.inPackageArray;
- }
- return self.outPackageArray;
- }
- - (NSInteger)numberOfSectionsWithSegmentIndex:(NSInteger)index {
- NSArray<FKPackageLogisticsItem *> *array = [self selectPackageArray];
-
- if (index < array.count) {
- return 2;
- }
- return 0;
- }
- - (NSInteger)numberOfRowsInSection:(NSInteger)section withSegmentIndex:(NSInteger)index {
- NSArray<FKPackageLogisticsItem *> *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<FKPackageLogisticsItem *> *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<FKPackageLogisticsItem *> *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<FKPackageLogisticsItem *> *array = [self selectPackageArray];
- FKPackageLogisticsItem *package = array[index];
- if (indexPath.row < package.logisticsArray.count) {
- return package.logisticsArray[indexPath.row];
- }
- return nil;
- }
- - (NSArray *)sectionTitleArray {
- NSArray<FKPackageLogisticsItem *> *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<FKPackageLogisticsItem *> *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
|