123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571 |
- //
- // 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
|