// // FKProDetailViewModel.m // FirstLink // // Created by jack on 16/8/15. // Copyright © 2016年 FirstLink. All rights reserved. // #import "FKProDetailViewModel.h" #import "UserDefaultManager.h" @implementation FKProDetailViewModel - (instancetype)init { self = [super init]; if (self) { self.showWeightDetail = [self isNeedShowWeightDetailDefault]; self.isBrandDescClose = TRUE; self.hasSelectedSKU = NO; } return self; } - (NSInteger)numberOfSectionsInTableType:(kProTableType)tableType{ if (tableType == kProTableTypeUp) { NSInteger count = 4; if (self.brandRecItem.brandInfo) count++; if (self.allLookItem.productList.count) count++; return count; } else if (tableType == kProTableTypeDown) { if (self.showWarn) { // 展示温馨提示 return 3; } NSInteger count = 2; if ([self.dataItem hasRecommend]) count++; if (self.relativeProArray.count) count++; return count; } return 0; } - (NSInteger)numberOfRowsInSection:(NSInteger)section tableType:(kProTableType)tableType{ if (tableType == kProTableTypeUp) { NSInteger addForBrand = (self.brandRecItem.brandInfo ? 1 : 0); if (section == 0) { NSInteger count = 5; if (self.dataItem.isExistActivityBanner) count++; if ([self.dataItem.productInfo isHasAmazonScore]) count++; if (self.tagArray.count) count++; if (self.showWeightDetail) count++; if (self.dataItem.productInfo.isBCChannel) count++; return count; }else if (section == 1) { return self.activityArray.count + 1; }else if (section == 2) { // 官网 return 1; }else if (section == 3) { if ([self.dataItem.lastQA hasAskAndAnswer]) return 2; return 1; }else if (section == 4 && addForBrand) { if (self.brandRecItem.brandRecList.count) return 2; return 1; }else if (section == 4 + addForBrand) { return 1; } } else if (tableType == kProTableTypeDown) { if (self.showWarn) { if (section == 0) { return self.tipsItem.beforeBuyCount + 1; }else if (section == 1) { return self.tipsItem.buyAttentionCount + 1; }else if (section == 2) { return 2; } } else{ NSInteger addRecommend = ([self.dataItem hasRecommend] ? 1 : 0); if (section == 0 && addRecommend) { return 1; }else if (section == 0 + addRecommend) { return 1; }else if (section == 1 + addRecommend) { return self.dataItem.productShowArray.count; }else if (section == 2 + addRecommend) { return ceil(self.relativeProArray.count / 2.0f); } } } return 0; } - (kProCellType)cellTypeForIndexPath:(NSIndexPath *)indexPath tableType:(kProTableType)tableType{ if (tableType == kProTableTypeUp) { NSInteger addForBrand = (self.brandRecItem.brandInfo ? 1 : 0); if (indexPath.section == 0) { if (indexPath.row == 0) return kProCellTypeTopImgList; NSInteger startIndex = 0; if (self.dataItem.isExistActivityBanner) startIndex ++; if (indexPath.row == startIndex) return kProCellTypeActivityBanner; startIndex ++; if (indexPath.row == startIndex) return kProCellTypeTitle; if ([self.dataItem.productInfo isHasAmazonScore]) startIndex ++; if (indexPath.row == startIndex) return kProCellTypeAmazonScore; // if (self.tagArray.count) startIndex ++; // if (indexPath.row == startIndex) return kProCellTypeTagList; startIndex++; if (indexPath.row == startIndex) return kProCellTypePriceDetail; startIndex++; if (indexPath.row == startIndex) return kProCellTypeWeight; if (self.showWeightDetail) startIndex++; if (indexPath.row == startIndex) return kProCellTypeWeightError; startIndex++; if (indexPath.row == startIndex) return kProCellTypeSelectSpec; if (self.dataItem.productInfo.isBCChannel) startIndex++; if (indexPath.row == startIndex) return kProCellTypeBCChannelFee; } else if (indexPath.section == 1) { if (indexPath.row < self.activityArray.count) return kProCellTypeActivityScroll; return kProCellTypeFeature; } else if (indexPath.section == 2) { return kProCellTypeSource; } else if (indexPath.section == 3) { if (indexPath.row == 0) return kProCellTypeSeek; if (indexPath.row == 1) return kProCellTypeAskAnswer; } else if (indexPath.section == 4 && addForBrand) { if (indexPath.row == 0) return kProCellTypeBrand; if (indexPath.row == 1) return kProCellTypeBrandList; } else if (indexPath.section == 4 + addForBrand) { return kProCellTypeAllLook; } } else if (tableType == kProTableTypeDown) { if (self.showWarn) { if (indexPath.row == 0) return kProCellTypeWarnHeader; if (indexPath.section == 2) { return kProCellTypeProcess; } return kProCellTypeWarnTitle; } else { NSInteger addRecommend = ([self.dataItem hasRecommend] ? 1 : 0); if (indexPath.section == 0 && addRecommend) { return kProCellTypeRecommend; } else if (indexPath.section == 0 + addRecommend) { return kProCellTypeInfo; } else if (indexPath.section == 1 + addRecommend) { return kProCellTypeProudctShow; } else if (indexPath.section == 2 + addRecommend) { return kProCellTypeRelative; } } } return kProCellTypeNone; } - (CGFloat)cellHeightForIndexPath:(NSIndexPath *)indexPath tableType:(kProTableType)tableType{ kProCellType cellType = [self cellTypeForIndexPath:indexPath tableType:tableType]; switch (cellType) { case kProCellTypeTopImgList: return [FKProBannerCell height]; case kProCellTypeActivityBanner: return [FKProActivityBaseCell height]; case kProCellTypeTitle: return [FKProTitleCell cellHeightForTitle:self.dataItem.productInfo.name]; case kProCellTypeAmazonScore: return [FKProAmazonScoreCell height]; case kProCellTypeTagList: return [FKProTagListCell cellHeightForTitleArray:self.tagArray]; case kProCellTypePriceDetail: return [FKProPriceDetailCell cellHeight:[self priceDetailCellType]]; case kProCellTypeWeight: return [FKProductWeightCell cellHeight]; case kProCellTypeWeightError: return [FKProWeightErrorCell cellHeight]; case kProCellTypeSelectSpec: return [FKProSpecResCell cellHeight]; case kProCellTypeBCChannelFee: return [FKProBCChannelFeeCell cellHeight]; case kProCellTypeActivityScroll: return [FKProActivityCell cellHeight]; case kProCellTypeFeature: return [FKProFeatureCell cellHeight]; case kProCellTypeSource: if (self.dataItem.supplier.postageRule.length > 0) { return 60; } return 44; case kProCellTypeSeek: return [FKProSeekCell cellHeight]; case kProCellTypeAskAnswer: return [FKProAskAnswerCell cellHeight]; case kProCellTypeBrand: return self.isBrandDescClose ? (77 + 35) : [FKProBrandCell cellHeightForContent:self.brandRecItem.brandInfo.desc]; case kProCellTypeBrandList: case kProCellTypeAllLook: return [FKProRecommendListCell cellHeight]; case kProCellTypeRecommend: return [FKProductRecommendCell cellHeightForTitleText:self.dataItem.recommendInfo.recommendTitle]; case kProCellTypeInfo: return [FKProInfoCell heightForUpText:self.dataItem.productInfo.descChinese downText:self.dataItem.productInfo.descOrginal showTranslate:self.showTranslate]; case kProCellTypeProudctShow:{ ProductShowItem *showItem = [self.dataItem productShowItemForIndex:indexPath.row]; return [PdProductShowCell cellHeightForText:showItem.describString withImgHeight:[showItem realImgSize].height]; } case kProCellTypeRelative: return [FKProductRelativeCell cellHeight]; case kProCellTypeWarnTitle:{ NSString *title = [self.tipsItem buyWarnTitleForIndexPath:indexPath]; BOOL isLast = [self.tipsItem isLastTitleForIndexPath:indexPath]; return [FKProWarnTitleCell cellHeightForTitle:title lastRow:isLast]; } case kProCellTypeWarnHeader: return [FKProWarnHeaderCell cellHeight]; case kProCellTypeProcess: return [FKProProcessCell cellHeight]; default: break; } return 0; } - (NSString *)cellIdentifyForIndexPath:(NSIndexPath *)indexPath tableType:(kProTableType)tableType{ kProCellType cellType = [self cellTypeForIndexPath:indexPath tableType:tableType]; switch (cellType) { case kProCellTypeTopImgList: return NSStringFromClass([FKProBannerCell class]); case kProCellTypeActivityBanner:{ switch ([self.dataItem activityBannerType]) { case kActivityBannerTypeInProgress: { return NSStringFromClass([FKProActivityInProgressCell class]); break; } case kActivityBannerTypeForeshow: { return NSStringFromClass([FKProActivityForeshowCell class]); break; } case kActivityBannerTypeTheme: { return NSStringFromClass([FKProActivityThemeCell class]); break; } default: break; } } case kProCellTypeTitle: return NSStringFromClass([FKProTitleCell class]); case kProCellTypeAmazonScore: return NSStringFromClass([FKProAmazonScoreCell class]); case kProCellTypeTagList: return NSStringFromClass([FKProTagListCell class]); case kProCellTypePriceDetail: return NSStringFromClass([FKProPriceDetailCell class]); case kProCellTypeWeight: return NSStringFromClass([FKProductWeightCell class]); case kProCellTypeWeightError: return NSStringFromClass([FKProWeightErrorCell class]); case kProCellTypeSelectSpec: return NSStringFromClass([FKProSpecResCell class]); case kProCellTypeBCChannelFee: return NSStringFromClass([FKProBCChannelFeeCell class]); case kProCellTypeActivityScroll: return NSStringFromClass([FKProActivityCell class]); case kProCellTypeFeature: return NSStringFromClass([FKProFeatureCell class]); case kProCellTypeSource: return NSStringFromClass([FKProSourceCell class]); case kProCellTypeSeek: return NSStringFromClass([FKProSeekCell class]); case kProCellTypeAskAnswer: return NSStringFromClass([FKProAskAnswerCell class]); case kProCellTypeBrand: return NSStringFromClass([FKProBrandCell class]); case kProCellTypeBrandList: return NSStringFromClass([FKProRecommendListCell class]); case kProCellTypeAllLook: return NSStringFromClass([FKProRecommendListCell class]); case kProCellTypeRecommend: return NSStringFromClass([FKProductRecommendCell class]); case kProCellTypeInfo: return NSStringFromClass([FKProInfoCell class]); case kProCellTypeProudctShow: return NSStringFromClass([PdProductShowCell class]); case kProCellTypeRelative: return NSStringFromClass([FKProductRelativeCell class]); case kProCellTypeWarnTitle: return NSStringFromClass([FKProWarnTitleCell class]); case kProCellTypeWarnHeader: return NSStringFromClass([FKProWarnHeaderCell class]); case kProCellTypeProcess: return NSStringFromClass([FKProProcessCell class]); default: break; } return @""; } - (NSDictionary *)infoForRequestType:(kProductReqType)type{ if (!self.productID.length) return nil; switch (type) { case kProductReqTypeDetail: case kProductReqTypeRemoveCollect: case kProductReqTypeAllLookList: return @{PRODUCT_ID_KEY : self.productID}; case kProductReqTypeAddCollect: return @{PRODUCT_ID_KEY : self.productID, PRODUCT_COLLECT_TYPE_KEY : @"10"}; case kProductReqTypeBrandRecommend: return @{BRAND_ID_KEY : [FLStringHelper replaceNilWithEmpty:self.brandRecItem.brandInfo.itemID]}; default: break; } return nil; } - (FKSubmitUnitItem *)createSubmitUnitItemWithProductItem:(FKProductItem *)productItem{ FKSubmitUnitItem *unitItem = [[FKSubmitUnitItem alloc]init]; unitItem.supplierID = self.dataItem.supplier.supplyID; unitItem.shipChannelId = self.dataItem.supplier.channelID; unitItem.postageRule = self.dataItem.supplier.postageRule; unitItem.countryInfoItem = self.dataItem.countryInfoItem; unitItem.shippingType = self.dataItem.supplier.shipType; unitItem.supplyName = self.dataItem.supplier.name; unitItem.type = self.dataItem.supplier.type; FKSubmitProductItem *submitProItem = [self convertProductItemToSubmit:productItem]; submitProItem.payTaxFee = self.dataItem.productInfo.isBCChannel; unitItem.productItemArray = @[submitProItem]; return unitItem; } - (FKSubmitProductItem *)convertProductItemToSubmit:(FKProductItem *)productItem{ FKSubmitProductItem *submitProItem = [[FKSubmitProductItem alloc]init]; submitProItem.goodsID = productItem.goodsID; submitProItem.title = self.dataItem.productInfo.name; submitProItem.picUrl = productItem.picUrl; submitProItem.price = productItem.price; // submitProItem.specName = productItem.fullSpecName; submitProItem.specName = productItem.title; // title是规格名称 submitProItem.quality = productItem.selectedAmount; submitProItem.sellSide = productItem.sellSide; return submitProItem; } - (NSArray *)specImgArrayForProductArray:(NSArray *)productItemArray{ if (productItemArray.count <= 0) return nil; NSMutableArray *arrayM = [NSMutableArray arrayWithCapacity:productItemArray.count]; for (FKProductItem *item in productItemArray) { if ([FLStringHelper isValidString:item.picUrl]) { [arrayM addObject:item.picUrl]; } } NSSet *set = [NSSet setWithArray:arrayM]; NSArray *filters = @[[[NSSortDescriptor alloc] initWithKey:nil ascending:YES]]; NSArray *filtedArray = [set sortedArrayUsingDescriptors:filters]; if (filtedArray.count) { if (filtedArray.count > 5) return [filtedArray subarrayWithRange:NSMakeRange(0, 5)]; return filtedArray; } return nil; } - (void)setSelectProductItemWithProductArray:(NSArray *)productItemArray{ NSMutableArray *validArray = [NSMutableArray arrayWithCapacity:productItemArray.count]; for (FKProductItem *item in productItemArray) { if ([item.stock isKindOfClass:[NSString class]] && item.stock.integerValue <= 0) continue; if (!item.isOnline) continue; [validArray addObject:item]; } if (!self.selectBuyItem) { self.selectBuyItem = [self getProductItemForSelectId:validArray]; self.selectBuyItem.selectedAmount = 1; } if (!self.selectBuyItem) { self.selectBuyItem = [self getCheapestItemWithProductArray:validArray]; self.selectBuyItem.selectedAmount = 1; } // 降价提醒不用过滤库存和下线 if (!self.selectPriceWarnItem) { self.selectPriceWarnItem = [self getProductItemForSelectId:productItemArray]; } if (!self.selectPriceWarnItem) { self.selectPriceWarnItem = [self getCheapestItemWithProductArray:productItemArray]; } } - (FKProductItem *)getProductItemForSelectId:(NSArray *)productItemArray{ if (productItemArray.count && self.selectSpecId) { for (FKProductItem *item in productItemArray) { if ([item.goodsID isEqualToString:self.selectSpecId]) { return item; } } } return nil; } - (FKProductItem *)getCheapestItemWithProductArray:(NSArray *)productItemArray{ FKProductItem *targetItem = nil; for (FKProductItem *productItem in productItemArray) { if (!targetItem) { targetItem = productItem; } else{ if (targetItem.price.integerValue > productItem.price.integerValue) { targetItem = productItem; } } } return targetItem; } - (void)checkSpecImgArray{ // NSString *firstPic = self.dataItem.productInfo.firstPicUrl; // if (!self.specPicArray.count && firstPic) { // // 规格图为空,用主图 // self.specPicArray = @[firstPic]; // } } - (BOOL)isHasShowIDCardAlert { NSString *ret = [[UserDefaultManager sharedManager] getUserDefaultObject:ID_CARD_ALERT_SHOW_KEY]; if (ret && [ret isEqualToString:@"1"]) { return YES; } return NO; } - (void)setHasShowIDCardAlert { [[UserDefaultManager sharedManager] setUserDefaultObject:@"1" key:ID_CARD_ALERT_SHOW_KEY]; } - (void)appendMoreRelativePro:(NSArray *)array{ if (array.count == 0) return; NSMutableArray *arrayM = [NSMutableArray arrayWithArray:self.relativeProArray]; [arrayM addObjectsFromArray:array]; self.relativeProArray = arrayM; } - (FKProductRelativeItem *)relativeItemAtIndex:(NSUInteger)index{ if (index < self.relativeProArray.count){ return self.relativeProArray[index]; } return nil; } - (void)configWithSizeImgView:(UIImageView *)sizeImgView{ ProductShowItem *sizeItem = [self.dataItem getSizePicItem]; sizeImgView.backgroundColor = [UIColor whiteColor]; sizeImgView.contentMode = UIViewContentModeScaleAspectFit; sizeImgView.frame = CGRectMake(0, 0, UISCREENWIDTH, [sizeItem realImgHeightForRealWidth:[sizeItem realImgSize].width]); [sizeImgView setImageWithURL:sizeItem.imageUrl cdnWidth:UISCREENWIDTH]; } - (FKPromotionActivityItem *)activityItemAtIndex:(NSInteger)index{ if (index >= 0 && index < self.activityArray.count) { return self.activityArray[index]; } return nil; } - (NSString *)warnHeaderTitleForSection:(NSInteger)section{ if (section == 0) return @"买前必看"; if (section == 1) return @"注意事项"; if (section == 2) return @"购物流程"; return nil; } - (FKProRecommendItem *)allLookItemAtIndex:(NSInteger)index{ if (index >= 0 && index < self.allLookItem.productList.count) { return self.allLookItem.productList[index]; } return nil; } #pragma mark - Method - (FKProPriceDetailCellType)priceDetailCellType { NSString *desc = self.selectBuyItem.activityDesc; // NSString *price = self.selectBuyItem.activityPrice; if (desc.length > 0 /*&& price.length > 0*/) { if ([self.dataItem activityBannerType] == kActivityBannerTypeForeshow) { return FKProPriceDetailCellTypeForeshow; } if ([self.dataItem activityBannerType] == kActivityBannerTypeInProgress) { return FKProPriceDetailCellTypeInProgress; } } return FKProPriceDetailCellTypeNone; } #pragma mark - Brand Cell - (FKProRecommendItem *)brandRecItemAtIndex:(NSInteger)index{ if (index >= 0 && index < self.brandRecItem.brandRecList.count) { return self.brandRecItem.brandRecList[index]; } return nil; } - (BOOL)isNeedShowWeightDetailDefault { NSString *hasShown = [[UserDefaultManager sharedManager] getUserDefaultObject:SHOW_WEIGHT_DETAIL_KEY]; if (hasShown && [hasShown isEqualToString:@"1"]) { return NO; } return YES; } - (void)setNotShowWeightDetailDefault { [[UserDefaultManager sharedManager] setUserDefaultObject:@"1" key:SHOW_WEIGHT_DETAIL_KEY]; } @end