暫無描述

FKProDetailViewModel.m 21KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571
  1. //
  2. // FKProDetailViewModel.m
  3. // FirstLink
  4. //
  5. // Created by jack on 16/8/15.
  6. // Copyright © 2016年 FirstLink. All rights reserved.
  7. //
  8. #import "FKProDetailViewModel.h"
  9. #import "UserDefaultManager.h"
  10. @implementation FKProDetailViewModel
  11. - (instancetype)init {
  12. self = [super init];
  13. if (self) {
  14. self.showWeightDetail = [self isNeedShowWeightDetailDefault];
  15. self.isBrandDescClose = TRUE;
  16. self.hasSelectedSKU = NO;
  17. }
  18. return self;
  19. }
  20. - (NSInteger)numberOfSectionsInTableType:(kProTableType)tableType{
  21. if (tableType == kProTableTypeUp) {
  22. NSInteger count = 4;
  23. if (self.brandRecItem.brandInfo) count++;
  24. if (self.allLookItem.productList.count) count++;
  25. return count;
  26. } else if (tableType == kProTableTypeDown) {
  27. if (self.showWarn) {
  28. // 展示温馨提示
  29. return 3;
  30. }
  31. NSInteger count = 2;
  32. if ([self.dataItem hasRecommend]) count++;
  33. if (self.relativeProArray.count) count++;
  34. return count;
  35. }
  36. return 0;
  37. }
  38. - (NSInteger)numberOfRowsInSection:(NSInteger)section tableType:(kProTableType)tableType{
  39. if (tableType == kProTableTypeUp) {
  40. NSInteger addForBrand = (self.brandRecItem.brandInfo ? 1 : 0);
  41. if (section == 0) {
  42. NSInteger count = 5;
  43. if (self.dataItem.isExistActivityBanner) count++;
  44. if ([self.dataItem.productInfo isHasAmazonScore]) count++;
  45. if (self.tagArray.count) count++;
  46. if (self.showWeightDetail) count++;
  47. if (self.dataItem.productInfo.isBCChannel) count++;
  48. return count;
  49. }else if (section == 1) {
  50. return self.activityArray.count + 1;
  51. }else if (section == 2) {
  52. // 官网
  53. return 1;
  54. }else if (section == 3) {
  55. if ([self.dataItem.lastQA hasAskAndAnswer]) return 2;
  56. return 1;
  57. }else if (section == 4 && addForBrand) {
  58. if (self.brandRecItem.brandRecList.count) return 2;
  59. return 1;
  60. }else if (section == 4 + addForBrand) {
  61. return 1;
  62. }
  63. } else if (tableType == kProTableTypeDown) {
  64. if (self.showWarn) {
  65. if (section == 0) {
  66. return self.tipsItem.beforeBuyCount + 1;
  67. }else if (section == 1) {
  68. return self.tipsItem.buyAttentionCount + 1;
  69. }else if (section == 2) {
  70. return 2;
  71. }
  72. } else{
  73. NSInteger addRecommend = ([self.dataItem hasRecommend] ? 1 : 0);
  74. if (section == 0 && addRecommend) {
  75. return 1;
  76. }else if (section == 0 + addRecommend) {
  77. return 1;
  78. }else if (section == 1 + addRecommend) {
  79. return self.dataItem.productShowArray.count;
  80. }else if (section == 2 + addRecommend) {
  81. return ceil(self.relativeProArray.count / 2.0f);
  82. }
  83. }
  84. }
  85. return 0;
  86. }
  87. - (kProCellType)cellTypeForIndexPath:(NSIndexPath *)indexPath tableType:(kProTableType)tableType{
  88. if (tableType == kProTableTypeUp) {
  89. NSInteger addForBrand = (self.brandRecItem.brandInfo ? 1 : 0);
  90. if (indexPath.section == 0) {
  91. if (indexPath.row == 0) return kProCellTypeTopImgList;
  92. NSInteger startIndex = 0;
  93. if (self.dataItem.isExistActivityBanner) startIndex ++;
  94. if (indexPath.row == startIndex) return kProCellTypeActivityBanner;
  95. startIndex ++;
  96. if (indexPath.row == startIndex) return kProCellTypeTitle;
  97. if ([self.dataItem.productInfo isHasAmazonScore]) startIndex ++;
  98. if (indexPath.row == startIndex) return kProCellTypeAmazonScore;
  99. // if (self.tagArray.count) startIndex ++;
  100. // if (indexPath.row == startIndex) return kProCellTypeTagList;
  101. startIndex++;
  102. if (indexPath.row == startIndex) return kProCellTypePriceDetail;
  103. startIndex++;
  104. if (indexPath.row == startIndex) return kProCellTypeWeight;
  105. if (self.showWeightDetail) startIndex++;
  106. if (indexPath.row == startIndex) return kProCellTypeWeightError;
  107. startIndex++;
  108. if (indexPath.row == startIndex) return kProCellTypeSelectSpec;
  109. if (self.dataItem.productInfo.isBCChannel) startIndex++;
  110. if (indexPath.row == startIndex) return kProCellTypeBCChannelFee;
  111. } else if (indexPath.section == 1) {
  112. if (indexPath.row < self.activityArray.count) return kProCellTypeActivityScroll;
  113. return kProCellTypeFeature;
  114. } else if (indexPath.section == 2) {
  115. return kProCellTypeSource;
  116. } else if (indexPath.section == 3) {
  117. if (indexPath.row == 0) return kProCellTypeSeek;
  118. if (indexPath.row == 1) return kProCellTypeAskAnswer;
  119. } else if (indexPath.section == 4 && addForBrand) {
  120. if (indexPath.row == 0) return kProCellTypeBrand;
  121. if (indexPath.row == 1) return kProCellTypeBrandList;
  122. } else if (indexPath.section == 4 + addForBrand) {
  123. return kProCellTypeAllLook;
  124. }
  125. } else if (tableType == kProTableTypeDown) {
  126. if (self.showWarn) {
  127. if (indexPath.row == 0) return kProCellTypeWarnHeader;
  128. if (indexPath.section == 2) {
  129. return kProCellTypeProcess;
  130. }
  131. return kProCellTypeWarnTitle;
  132. } else {
  133. NSInteger addRecommend = ([self.dataItem hasRecommend] ? 1 : 0);
  134. if (indexPath.section == 0 && addRecommend) {
  135. return kProCellTypeRecommend;
  136. } else if (indexPath.section == 0 + addRecommend) {
  137. return kProCellTypeInfo;
  138. } else if (indexPath.section == 1 + addRecommend) {
  139. return kProCellTypeProudctShow;
  140. } else if (indexPath.section == 2 + addRecommend) {
  141. return kProCellTypeRelative;
  142. }
  143. }
  144. }
  145. return kProCellTypeNone;
  146. }
  147. - (CGFloat)cellHeightForIndexPath:(NSIndexPath *)indexPath tableType:(kProTableType)tableType{
  148. kProCellType cellType = [self cellTypeForIndexPath:indexPath tableType:tableType];
  149. switch (cellType) {
  150. case kProCellTypeTopImgList:
  151. return [FKProBannerCell height];
  152. case kProCellTypeActivityBanner:
  153. return [FKProActivityBaseCell height];
  154. case kProCellTypeTitle:
  155. return [FKProTitleCell cellHeightForTitle:self.dataItem.productInfo.name];
  156. case kProCellTypeAmazonScore:
  157. return [FKProAmazonScoreCell height];
  158. case kProCellTypeTagList:
  159. return [FKProTagListCell cellHeightForTitleArray:self.tagArray];
  160. case kProCellTypePriceDetail:
  161. return [FKProPriceDetailCell cellHeight:[self priceDetailCellType]];
  162. case kProCellTypeWeight:
  163. return [FKProductWeightCell cellHeight];
  164. case kProCellTypeWeightError:
  165. return [FKProWeightErrorCell cellHeight];
  166. case kProCellTypeSelectSpec:
  167. return [FKProSpecResCell cellHeight];
  168. case kProCellTypeBCChannelFee:
  169. return [FKProBCChannelFeeCell cellHeight];
  170. case kProCellTypeActivityScroll:
  171. return [FKProActivityCell cellHeight];
  172. case kProCellTypeFeature:
  173. return [FKProFeatureCell cellHeight];
  174. case kProCellTypeSource:
  175. if (self.dataItem.supplier.postageRule.length > 0) {
  176. return 60;
  177. }
  178. return 44;
  179. case kProCellTypeSeek:
  180. return [FKProSeekCell cellHeight];
  181. case kProCellTypeAskAnswer:
  182. return [FKProAskAnswerCell cellHeight];
  183. case kProCellTypeBrand:
  184. return self.isBrandDescClose ? (77 + 35) : [FKProBrandCell cellHeightForContent:self.brandRecItem.brandInfo.desc];
  185. case kProCellTypeBrandList:
  186. case kProCellTypeAllLook:
  187. return [FKProRecommendListCell cellHeight];
  188. case kProCellTypeRecommend:
  189. return [FKProductRecommendCell cellHeightForTitleText:self.dataItem.recommendInfo.recommendTitle];
  190. case kProCellTypeInfo:
  191. return [FKProInfoCell heightForUpText:self.dataItem.productInfo.descChinese downText:self.dataItem.productInfo.descOrginal showTranslate:self.showTranslate];
  192. case kProCellTypeProudctShow:{
  193. ProductShowItem *showItem = [self.dataItem productShowItemForIndex:indexPath.row];
  194. return [PdProductShowCell cellHeightForText:showItem.describString withImgHeight:[showItem realImgSize].height];
  195. }
  196. case kProCellTypeRelative:
  197. return [FKProductRelativeCell cellHeight];
  198. case kProCellTypeWarnTitle:{
  199. NSString *title = [self.tipsItem buyWarnTitleForIndexPath:indexPath];
  200. BOOL isLast = [self.tipsItem isLastTitleForIndexPath:indexPath];
  201. return [FKProWarnTitleCell cellHeightForTitle:title lastRow:isLast];
  202. }
  203. case kProCellTypeWarnHeader:
  204. return [FKProWarnHeaderCell cellHeight];
  205. case kProCellTypeProcess:
  206. return [FKProProcessCell cellHeight];
  207. default:
  208. break;
  209. }
  210. return 0;
  211. }
  212. - (NSString *)cellIdentifyForIndexPath:(NSIndexPath *)indexPath tableType:(kProTableType)tableType{
  213. kProCellType cellType = [self cellTypeForIndexPath:indexPath tableType:tableType];
  214. switch (cellType) {
  215. case kProCellTypeTopImgList:
  216. return NSStringFromClass([FKProBannerCell class]);
  217. case kProCellTypeActivityBanner:{
  218. switch ([self.dataItem activityBannerType]) {
  219. case kActivityBannerTypeInProgress: {
  220. return NSStringFromClass([FKProActivityInProgressCell class]);
  221. break;
  222. }
  223. case kActivityBannerTypeForeshow: {
  224. return NSStringFromClass([FKProActivityForeshowCell class]);
  225. break;
  226. }
  227. case kActivityBannerTypeTheme: {
  228. return NSStringFromClass([FKProActivityThemeCell class]);
  229. break;
  230. }
  231. default:
  232. break;
  233. }
  234. }
  235. case kProCellTypeTitle:
  236. return NSStringFromClass([FKProTitleCell class]);
  237. case kProCellTypeAmazonScore:
  238. return NSStringFromClass([FKProAmazonScoreCell class]);
  239. case kProCellTypeTagList:
  240. return NSStringFromClass([FKProTagListCell class]);
  241. case kProCellTypePriceDetail:
  242. return NSStringFromClass([FKProPriceDetailCell class]);
  243. case kProCellTypeWeight:
  244. return NSStringFromClass([FKProductWeightCell class]);
  245. case kProCellTypeWeightError:
  246. return NSStringFromClass([FKProWeightErrorCell class]);
  247. case kProCellTypeSelectSpec:
  248. return NSStringFromClass([FKProSpecResCell class]);
  249. case kProCellTypeBCChannelFee:
  250. return NSStringFromClass([FKProBCChannelFeeCell class]);
  251. case kProCellTypeActivityScroll:
  252. return NSStringFromClass([FKProActivityCell class]);
  253. case kProCellTypeFeature:
  254. return NSStringFromClass([FKProFeatureCell class]);
  255. case kProCellTypeSource:
  256. return NSStringFromClass([FKProSourceCell class]);
  257. case kProCellTypeSeek:
  258. return NSStringFromClass([FKProSeekCell class]);
  259. case kProCellTypeAskAnswer:
  260. return NSStringFromClass([FKProAskAnswerCell class]);
  261. case kProCellTypeBrand:
  262. return NSStringFromClass([FKProBrandCell class]);
  263. case kProCellTypeBrandList:
  264. return NSStringFromClass([FKProRecommendListCell class]);
  265. case kProCellTypeAllLook:
  266. return NSStringFromClass([FKProRecommendListCell class]);
  267. case kProCellTypeRecommend:
  268. return NSStringFromClass([FKProductRecommendCell class]);
  269. case kProCellTypeInfo:
  270. return NSStringFromClass([FKProInfoCell class]);
  271. case kProCellTypeProudctShow:
  272. return NSStringFromClass([PdProductShowCell class]);
  273. case kProCellTypeRelative:
  274. return NSStringFromClass([FKProductRelativeCell class]);
  275. case kProCellTypeWarnTitle:
  276. return NSStringFromClass([FKProWarnTitleCell class]);
  277. case kProCellTypeWarnHeader:
  278. return NSStringFromClass([FKProWarnHeaderCell class]);
  279. case kProCellTypeProcess:
  280. return NSStringFromClass([FKProProcessCell class]);
  281. default:
  282. break;
  283. }
  284. return @"";
  285. }
  286. - (NSDictionary *)infoForRequestType:(kProductReqType)type{
  287. if (!self.productID.length) return nil;
  288. switch (type) {
  289. case kProductReqTypeDetail:
  290. case kProductReqTypeRemoveCollect:
  291. case kProductReqTypeAllLookList:
  292. return @{PRODUCT_ID_KEY : self.productID};
  293. case kProductReqTypeAddCollect:
  294. return @{PRODUCT_ID_KEY : self.productID, PRODUCT_COLLECT_TYPE_KEY : @"10"};
  295. case kProductReqTypeBrandRecommend:
  296. return @{BRAND_ID_KEY : [FLStringHelper replaceNilWithEmpty:self.brandRecItem.brandInfo.itemID]};
  297. default:
  298. break;
  299. }
  300. return nil;
  301. }
  302. - (FKSubmitUnitItem *)createSubmitUnitItemWithProductItem:(FKProductItem *)productItem{
  303. FKSubmitUnitItem *unitItem = [[FKSubmitUnitItem alloc]init];
  304. unitItem.supplierID = self.dataItem.supplier.supplyID;
  305. unitItem.shipChannelId = self.dataItem.supplier.channelID;
  306. unitItem.postageRule = self.dataItem.supplier.postageRule;
  307. unitItem.countryInfoItem = self.dataItem.countryInfoItem;
  308. unitItem.shippingType = self.dataItem.supplier.shipType;
  309. unitItem.supplyName = self.dataItem.supplier.name;
  310. unitItem.type = self.dataItem.supplier.type;
  311. FKSubmitProductItem *submitProItem = [self convertProductItemToSubmit:productItem];
  312. submitProItem.payTaxFee = self.dataItem.productInfo.isBCChannel;
  313. unitItem.productItemArray = @[submitProItem];
  314. return unitItem;
  315. }
  316. - (FKSubmitProductItem *)convertProductItemToSubmit:(FKProductItem *)productItem{
  317. FKSubmitProductItem *submitProItem = [[FKSubmitProductItem alloc]init];
  318. submitProItem.goodsID = productItem.goodsID;
  319. submitProItem.title = self.dataItem.productInfo.name;
  320. submitProItem.picUrl = productItem.picUrl;
  321. submitProItem.price = productItem.price;
  322. // submitProItem.specName = productItem.fullSpecName;
  323. submitProItem.specName = productItem.title; // title是规格名称
  324. submitProItem.quality = productItem.selectedAmount;
  325. submitProItem.sellSide = productItem.sellSide;
  326. return submitProItem;
  327. }
  328. - (NSArray *)specImgArrayForProductArray:(NSArray *)productItemArray{
  329. if (productItemArray.count <= 0) return nil;
  330. NSMutableArray *arrayM = [NSMutableArray arrayWithCapacity:productItemArray.count];
  331. for (FKProductItem *item in productItemArray) {
  332. if ([FLStringHelper isValidString:item.picUrl]) {
  333. [arrayM addObject:item.picUrl];
  334. }
  335. }
  336. NSSet *set = [NSSet setWithArray:arrayM];
  337. NSArray *filters = @[[[NSSortDescriptor alloc] initWithKey:nil ascending:YES]];
  338. NSArray *filtedArray = [set sortedArrayUsingDescriptors:filters];
  339. if (filtedArray.count) {
  340. if (filtedArray.count > 5) return [filtedArray subarrayWithRange:NSMakeRange(0, 5)];
  341. return filtedArray;
  342. }
  343. return nil;
  344. }
  345. - (void)setSelectProductItemWithProductArray:(NSArray *)productItemArray{
  346. NSMutableArray *validArray = [NSMutableArray arrayWithCapacity:productItemArray.count];
  347. for (FKProductItem *item in productItemArray) {
  348. if ([item.stock isKindOfClass:[NSString class]] && item.stock.integerValue <= 0) continue;
  349. if (!item.isOnline) continue;
  350. [validArray addObject:item];
  351. }
  352. if (!self.selectBuyItem) {
  353. self.selectBuyItem = [self getProductItemForSelectId:validArray];
  354. self.selectBuyItem.selectedAmount = 1;
  355. }
  356. if (!self.selectBuyItem) {
  357. self.selectBuyItem = [self getCheapestItemWithProductArray:validArray];
  358. self.selectBuyItem.selectedAmount = 1;
  359. }
  360. // 降价提醒不用过滤库存和下线
  361. if (!self.selectPriceWarnItem) {
  362. self.selectPriceWarnItem = [self getProductItemForSelectId:productItemArray];
  363. }
  364. if (!self.selectPriceWarnItem) {
  365. self.selectPriceWarnItem = [self getCheapestItemWithProductArray:productItemArray];
  366. }
  367. }
  368. - (FKProductItem *)getProductItemForSelectId:(NSArray *)productItemArray{
  369. if (productItemArray.count && self.selectSpecId) {
  370. for (FKProductItem *item in productItemArray) {
  371. if ([item.goodsID isEqualToString:self.selectSpecId]) {
  372. return item;
  373. }
  374. }
  375. }
  376. return nil;
  377. }
  378. - (FKProductItem *)getCheapestItemWithProductArray:(NSArray *)productItemArray{
  379. FKProductItem *targetItem = nil;
  380. for (FKProductItem *productItem in productItemArray) {
  381. if (!targetItem) {
  382. targetItem = productItem;
  383. } else{
  384. if (targetItem.price.integerValue > productItem.price.integerValue) {
  385. targetItem = productItem;
  386. }
  387. }
  388. }
  389. return targetItem;
  390. }
  391. - (void)checkSpecImgArray{
  392. // NSString *firstPic = self.dataItem.productInfo.firstPicUrl;
  393. // if (!self.specPicArray.count && firstPic) {
  394. // // 规格图为空,用主图
  395. // self.specPicArray = @[firstPic];
  396. // }
  397. }
  398. - (BOOL)isHasShowIDCardAlert {
  399. NSString *ret = [[UserDefaultManager sharedManager] getUserDefaultObject:ID_CARD_ALERT_SHOW_KEY];
  400. if (ret && [ret isEqualToString:@"1"]) {
  401. return YES;
  402. }
  403. return NO;
  404. }
  405. - (void)setHasShowIDCardAlert {
  406. [[UserDefaultManager sharedManager] setUserDefaultObject:@"1" key:ID_CARD_ALERT_SHOW_KEY];
  407. }
  408. - (void)appendMoreRelativePro:(NSArray *)array{
  409. if (array.count == 0) return;
  410. NSMutableArray *arrayM = [NSMutableArray arrayWithArray:self.relativeProArray];
  411. [arrayM addObjectsFromArray:array];
  412. self.relativeProArray = arrayM;
  413. }
  414. - (FKProductRelativeItem *)relativeItemAtIndex:(NSUInteger)index{
  415. if (index < self.relativeProArray.count){
  416. return self.relativeProArray[index];
  417. }
  418. return nil;
  419. }
  420. - (void)configWithSizeImgView:(UIImageView *)sizeImgView{
  421. ProductShowItem *sizeItem = [self.dataItem getSizePicItem];
  422. sizeImgView.backgroundColor = [UIColor whiteColor];
  423. sizeImgView.contentMode = UIViewContentModeScaleAspectFit;
  424. sizeImgView.frame = CGRectMake(0, 0, UISCREENWIDTH, [sizeItem realImgHeightForRealWidth:[sizeItem realImgSize].width]);
  425. [sizeImgView setImageWithURL:sizeItem.imageUrl cdnWidth:UISCREENWIDTH];
  426. }
  427. - (FKPromotionActivityItem *)activityItemAtIndex:(NSInteger)index{
  428. if (index >= 0 && index < self.activityArray.count) {
  429. return self.activityArray[index];
  430. }
  431. return nil;
  432. }
  433. - (NSString *)warnHeaderTitleForSection:(NSInteger)section{
  434. if (section == 0) return @"买前必看";
  435. if (section == 1) return @"注意事项";
  436. if (section == 2) return @"购物流程";
  437. return nil;
  438. }
  439. - (FKProRecommendItem *)allLookItemAtIndex:(NSInteger)index{
  440. if (index >= 0 && index < self.allLookItem.productList.count) {
  441. return self.allLookItem.productList[index];
  442. }
  443. return nil;
  444. }
  445. #pragma mark - Method
  446. - (FKProPriceDetailCellType)priceDetailCellType {
  447. NSString *desc = self.selectBuyItem.activityDesc;
  448. // NSString *price = self.selectBuyItem.activityPrice;
  449. if (desc.length > 0 /*&& price.length > 0*/) {
  450. if ([self.dataItem activityBannerType] == kActivityBannerTypeForeshow) {
  451. return FKProPriceDetailCellTypeForeshow;
  452. }
  453. if ([self.dataItem activityBannerType] == kActivityBannerTypeInProgress) {
  454. return FKProPriceDetailCellTypeInProgress;
  455. }
  456. }
  457. return FKProPriceDetailCellTypeNone;
  458. }
  459. #pragma mark - Brand Cell
  460. - (FKProRecommendItem *)brandRecItemAtIndex:(NSInteger)index{
  461. if (index >= 0 && index < self.brandRecItem.brandRecList.count) {
  462. return self.brandRecItem.brandRecList[index];
  463. }
  464. return nil;
  465. }
  466. - (BOOL)isNeedShowWeightDetailDefault {
  467. NSString *hasShown = [[UserDefaultManager sharedManager] getUserDefaultObject:SHOW_WEIGHT_DETAIL_KEY];
  468. if (hasShown && [hasShown isEqualToString:@"1"]) {
  469. return NO;
  470. }
  471. return YES;
  472. }
  473. - (void)setNotShowWeightDetailDefault {
  474. [[UserDefaultManager sharedManager] setUserDefaultObject:@"1" key:SHOW_WEIGHT_DETAIL_KEY];
  475. }
  476. @end