123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613 |
- //
- // PdDetailViewModel.m
- // FirstLink
- //
- // Created by 王孝道 on 15/6/8.
- // Copyright (c) 2015年 FirstLink. All rights reserved.
- //
- #import "PindanDetailViewModel.h"
- #import "ProductShowItem.h"
- #import "PdCommentCell.h"
- #import "DetailTalkCell.h"
- #import "PdProductShowCell.h"
- #import "DetailMainImgCell.h"
- #import "DetailPriceExplainCell.h"
- #import "DetailTransitHeaderCell.h"
- #import "DetailTransitCommonCell.h"
- #import "DetailAuthorityCell.h"
- #import "DetailProductHeaderCell.h"
- #import "DetailFeatureCell.h"
- #import "DetailSourceCell.h"
- #import "DetailCommentHeadCell.h"
- #import "DetailNoCommentCell.h"
- #import "DetailMoreCommentCell.h"
- #import "FKDetailRequest.h"
- @implementation PindanDetailViewModel
- - (instancetype)init {
- self = [super init];
- if (self) {
- _showPriceExplain = NO;
- }
- return self;
- }
- #pragma mark - Make Request Info
- - (NSDictionary *)infoForRequestType:(kRequestType)type withInfo:(NSDictionary *)dict{
- switch (type) {
- case kRequestTypeDetail:
- case kRequestTypeRefreshComment:
- case kRequestTypeRefreshCommentScroll:
- case kRequestTypeAddCollect:
- case kRequestTypeRemoveCollect:
- case kRequestTypeAddLike:
- case kRequestTypeRemoveLike:
- case kRequestTypeShareUrlCircle:
- case kRequestTypeShareUrlFriend:
- return [self commonInfoForRequest];
- case kRequestTypeAddSelfComment:{
- NSMutableDictionary *dictM = [NSMutableDictionary dictionaryWithDictionary:[self commonInfoForRequest]];
- [dictM addEntriesFromDictionary:dict];
- return dictM;
- }
- case kRequestTypeAddOtherComment:{
- NSMutableDictionary *dictM = [NSMutableDictionary dictionaryWithDictionary:[self commonInfoForRequest]];
- [dictM addEntriesFromDictionary:dict];
- return dictM;
- }
- case kRequestTypeRemoveComment:
- return dict;
- default:
- return nil;
- break;
- }
- }
- - (NSDictionary *)commonInfoForRequest{
- if (self.postID.length > 0 && self.userID.length > 0) {
- return @{DETAIL_POSTID_KEY : self.postID, DETAIL_USERID_KEY : self.userID};
- }
- return nil;
- }
- #pragma mark - Store Data
- - (void)saveIntoDataSource:(PindanDetailItem *)item {
- _pindanDetailItem = item;
- }
- - (PindanDetailItem *)pindanDetailItem {
- return _pindanDetailItem;
- }
- - (CGFloat)cellHeightForIndexPath:(NSIndexPath *)indexPath
- {
- kDetailCellType cellType = [self cellTypeForIndexPath:indexPath];
- switch (cellType) {
- case kDetailCellTypeMainImg:
- return UISCREENWIDTH;
- case kDetailCellTypePriceExplain:
- if (self.showPriceExplain) return 107;
- return 77;
- case kDetailCellTypeAuthority:
- return 23;
- case kDetailCellTypeTalk:
- return [DetailTalkCell cellHeightForTitleText:[self pindanDetailItem].introduceText];
- case kDetailCellTypeProductHeader:
- return 55;
- case kDetailCellTypeProductShow:{
- ProductShowItem *showItem = [[self pindanDetailItem] showItemAtIndex:indexPath.row - 1];
- CGFloat imgHeight = 0;
- if (showItem) imgHeight = [showItem realImgSize].height;
- CGFloat reslut =[PdProductShowCell cellHeightForText:showItem.describString withImgHeight:imgHeight];
- return reslut;
- }
- case kDetailCellTypeFeature:
- return 140;
- case kDetailCellTypePriceDetailHeader:
- case kDetailCellTypeTransitHeader:
- return 45;
- case kDetailCellTypeNationalPrice:
- case kDetailCellTypeNationalPostage:
- case kDetailCellTypeCountryInPrice:
- case kDetailCellTypeTransitSource:
- case kDetailCellTypeTransitLink:
- case kDetailCellTypeTransitTime:
- case kDetailCellTypeSpecGuige:
- case kDetailCellTypeSpecComment:
- return 40;
- case kDetailCellTypeComment:{
- CommentItem *model = [self pindanDetailItem].commentModelArray[indexPath.row - 1];
- if (model){
- return [PdCommentCell cellHeightForCommentText:model.content];
- }
- return 40;
- }
- case kDetailCellTypeMoreComment:
- return 88;
- case kDetailCellTypeNoComment:
- return 200;
- default:
- break;
- }
- return 0;
- }
- - (NSInteger)numberOfRowsInSection:(NSInteger)section {
- if (section == 0) return [self showAuthority] ? 3 : 2;
- if (section == 1) return 1;
- if (section == 2) return ([self pindanDetailItem].productShowArray.count + 2); // 加个header 和 source
- if (section == 3) return 8;
- if (section == 4 && [self hasSpecs]) return 1;
- if (section == 5 - ([self hasSpecs] ? 0 : 1)) {
- if ([self pindanDetailItem].commentModelArray.count == 0) return 2;
- return [self pindanDetailItem].commentModelArray.count + ([[self pindanDetailItem] hasMoreComment] ? 2 : 1);
- }
- return 0;
- }
- - (NSString *)cellIdentifyForIndexPath:(NSIndexPath *)indexPath{
-
- kDetailCellType cellType = [self cellTypeForIndexPath:indexPath];
-
- NSString *identifyStr = nil;
-
- switch (cellType) {
- case kDetailCellTypeMainImg:
- identifyStr = NSStringFromClass([DetailMainImgCell class]);
- break;
- case kDetailCellTypePriceExplain:
- identifyStr = NSStringFromClass([DetailPriceExplainCell class]);
- break;
- case kDetailCellTypeAuthority:
- identifyStr = NSStringFromClass([DetailAuthorityCell class]);
- break;
- case kDetailCellTypeTalk:
- identifyStr = NSStringFromClass([DetailTalkCell class]);
- break;
- case kDetailCellTypeProductHeader:
- identifyStr = NSStringFromClass([DetailProductHeaderCell class]);
- break;
- case kDetailCellTypeProductShow:
- identifyStr = NSStringFromClass([PdProductShowCell class]);
- break;
- case kDetailCellTypeFeature:
- identifyStr = NSStringFromClass([DetailFeatureCell class]);
- break;
- case kDetailCellTypePriceDetailHeader:
- case kDetailCellTypeTransitHeader:
- identifyStr = NSStringFromClass([DetailTransitHeaderCell class]);
- break;
- case kDetailCellTypeTransitLink:
- case kDetailCellTypeSpecGuige:
- identifyStr = NSStringFromClass([DetailSourceCell class]);
- break;
- case kDetailCellTypeNationalPrice:
- case kDetailCellTypeNationalPostage:
- case kDetailCellTypeCountryInPrice:
- case kDetailCellTypeTransitSource:
- case kDetailCellTypeTransitTime:
- identifyStr = NSStringFromClass([DetailTransitCommonCell class]);
- break;
- case kDetailCellTypeSpecComment:
- identifyStr = NSStringFromClass([DetailCommentHeadCell class]);
- break;
- case kDetailCellTypeComment:
- identifyStr = NSStringFromClass([PdCommentCell class]);
- break;
- case kDetailCellTypeNoComment:
- identifyStr = NSStringFromClass([DetailNoCommentCell class]);
- break;
- case kDetailCellTypeMoreComment:
- identifyStr = NSStringFromClass([DetailMoreCommentCell class]);
- break;
- default:
- break;
- }
-
- return identifyStr;
- }
- - (kDetailCellType)cellTypeForIndexPath:(NSIndexPath *)indexPath {
- if (indexPath.section == 0) {
- if (indexPath.row == 0) return kDetailCellTypeMainImg;
- if (indexPath.row == 1 && [self showAuthority]) return kDetailCellTypeAuthority;
- return kDetailCellTypePriceExplain;
- } else if (indexPath.section == 1){
- return kDetailCellTypeTalk;
- } else if (indexPath.section == 2){
- if (indexPath.row == 0) return kDetailCellTypeProductHeader;
- if (indexPath.row == [self pindanDetailItem].productShowArray.count + 1) return kDetailCellTypeFeature; // 最后一行
- return kDetailCellTypeProductShow;
- } else if (indexPath.section == 3){
- if (indexPath.row == 0) return kDetailCellTypePriceDetailHeader;
- if (indexPath.row == 1) return kDetailCellTypeNationalPrice;
- if (indexPath.row == 2) return kDetailCellTypeNationalPostage;
- if (indexPath.row == 3) return kDetailCellTypeCountryInPrice;
-
- if (indexPath.row == 4) return kDetailCellTypeTransitHeader;
- if (indexPath.row == 5) return kDetailCellTypeTransitSource;
- if (indexPath.row == 6) return kDetailCellTypeTransitLink;
- if (indexPath.row == 7) return kDetailCellTypeTransitTime;
-
- } else if (indexPath.section == 4 && [self hasSpecs]){
- return kDetailCellTypeSpecGuige;
- } else if (indexPath.section == 5 - ([self hasSpecs] ? 0 : 1)){
- NSInteger rowCount = [self numberOfRowsInSection:indexPath.section];
- if (indexPath.row == 0) return kDetailCellTypeSpecComment;
- if (indexPath.row == rowCount - 1 && [self pindanDetailItem].commentModelArray.count == 0) return kDetailCellTypeNoComment; // 没有评论提示
- if (indexPath.row == rowCount - 1 && [[self pindanDetailItem] hasMoreComment]) return kDetailCellTypeMoreComment;
- if (indexPath.row > 0) return kDetailCellTypeComment;
- }
- return kDetailCellTypeUnknown;
- }
- - (CGFloat)translateImgSizeToHeight:(CGSize)size
- {
- if (size.width == 0 || size.height == 0) return UISCREENWIDTH * 7 / 9;
- return UISCREENWIDTH * size.height / size.width;
- }
- - (BOOL)isCommentBySelfForRow:(NSInteger)row
- {
- CommentItem *commentItem = [self commentModelForIndex:row];
- if (commentItem) {
- if ([commentItem.buyerID isEqualToString:[FKUserManager sharedManager].user.userID]) return YES;
- }
- return NO;
- }
- - (NSString *)getCommentPlaceHolderWithIndex:(NSInteger)index
- {
- if (index < 0) return @"";
- if (![self isCommentBySelfForRow:index]){
- CommentItem *commentItem = [self commentModelForIndex:index];
- return [NSString stringWithFormat:@"回复 %@", commentItem.buyerNickName];
- }
- return @"";
- }
- - (CommentItem *)commentModelForIndex:(NSInteger)index
- {
- if (index >= [self pindanDetailItem].commentModelArray.count || index < 0) {
- return nil;
- }
- return [self pindanDetailItem].commentModelArray[index];
- }
- - (BOOL)showAuthority {
- return ([self pindanDetailItem].authorityItem.onlyVIP
- || [self pindanDetailItem].authorityItem.onlyNew
- || [self pindanDetailItem].isPostageFree);
- }
- - (BOOL)hasSpecs {
- // 是否有规格
- if ([self pindanDetailItem].specsArray
- && [self pindanDetailItem].specsArray.count > 0) {
- return YES;
- }
- return NO;
- }
- - (CommentItem *)commentItemForIndex:(NSInteger)index{
- if (index >= 0 && index < [self pindanDetailItem].commentModelArray.count) {
- return [self pindanDetailItem].commentModelArray[index];
- }
- return nil;
- }
- - (BOOL)isProductStateEnd {
-
- BOOL timeOut = [self pindanDetailItem].timeInterval <= 0;
- BOOL isBegin = [self pindanDetailItem].isPindanBegin;
- BOOL isNormal = [self pindanDetailItem].productState == kProductStateNormal;
-
- if ((timeOut && isBegin) || !isNormal){
- return YES;
- }
- return NO;
- }
- - (BOOL)isProductStateSaleDone{
-
- NSString *stock = [self pindanDetailItem].stock;
- BOOL stockEmpty = NO; // 库存是否为0
- if ([stock isKindOfClass:[NSString class]] && stock.integerValue == 0) stockEmpty = YES;
- if (stockEmpty && [self pindanDetailItem].productState == kProductStateNormal) return YES;
- return NO;
- }
- - (NSInteger)numberOfSectionsInTableView {
- if (![self pindanDetailItem].priceDetailItem) return 0; // 还未加载数据
- NSInteger section = 6;
- if (![self hasSpecs]) section = 5;
- return section;
- }
- - (NSIndexPath *)lastIndexPath
- {
- NSInteger rows = [self numberOfRowsInSection:([self numberOfSectionsInTableView] - 1)];
- if (rows >= 1) return [NSIndexPath indexPathForRow:rows - 1 inSection:([self numberOfSectionsInTableView] - 1)];
- return [NSIndexPath indexPathForRow:0 inSection:([self numberOfSectionsInTableView] - 1)];
- }
- #pragma mark - Purchase Cache
- - (FKPurchaseCache *)saveDataIntoCache:(FKPurchaseCache *)cache orderType:(kOrderType)orderType {
- cache.postID = self.postID;
- cache.userID = self.userID;
- cache.descString = self.title;
- cache.limiteAmount = self.limiteNum;
- cache.postageState = [self pindanDetailItem].postageFreeState;
- cache.unitWeight = [self pindanDetailItem].priceDetailItem.singleWeight;
- cache.internationalPostage = [self pindanDetailItem].priceDetailItem.international_postage;
-
- cache.orderType = orderType;
- cache.userLevel = self.userLevel;
- cache.isPindanNeedIDCard = [self isNeedIDCard];
-
- // 解析规格
- NSMutableArray *selectedSpecArray = [NSMutableArray array];
- if (self.specsArray && self.specsArray.count > 0) {
- for (PindanSpecItem *item in self.specsArray) {
- if (item.selectedAmount > 0) {
- [selectedSpecArray addObject:item];
- }
- }
- } else {
- // 没有子类规格
- PindanSpecItem *noSpeItem = [[PindanSpecItem alloc]init];
- noSpeItem.postID = self.postID;
- noSpeItem.userID = self.userID;
-
- if (kOrderTypeGroup == orderType) {
- noSpeItem.price = [self pindanDetailItem].priceDetailItem.group_price;
- } else {
- noSpeItem.price = [self pindanDetailItem].priceDetailItem.estimate_price;
- if (self.userLevel == kUserLevelVIP
- && [self pindanDetailItem].priceDetailItem.vip_price.length > 0) {
- noSpeItem.vipPrice = [self pindanDetailItem].priceDetailItem.vip_price;
- }
- }
- noSpeItem.imgUrl = self.firstPic;
- noSpeItem.selectedAmount = 1;
-
- [selectedSpecArray addObject:noSpeItem];
- }
- cache.specArray = selectedSpecArray;
-
- return cache;
- }
- #pragma mark Setter & Getter
- - (void)setIsFavorite:(BOOL)favorite {
- [self pindanDetailItem].isFavorite = favorite;
- }
- - (BOOL)isFavorite {
- return [self pindanDetailItem].isFavorite;
- }
- - (void)setShareURL:(NSString *)shareURL {
- [self pindanDetailItem].shareUrl = shareURL;
- }
- - (NSString *)shareURL {
- return [self pindanDetailItem].shareUrl;
- }
- - (void)setCommentArray:(NSArray *)commentArray {
- [self pindanDetailItem].commentModelArray = commentArray;
- }
- - (NSArray *)commentArray {
- return [self pindanDetailItem].commentModelArray;
- }
- - (void)setCommentCount:(NSString *)count {
- [self pindanDetailItem].commentCount = count;
- }
- - (NSString *)commentCount {
- return [self pindanDetailItem].commentCount;
- }
- - (NSInteger)orderCount {
- return [self pindanDetailItem].orderCount;
- }
- - (NSInteger)userOrderCount {
- return [self pindanDetailItem].userOrderCount;
- }
- - (NSString *)title {
- return [self pindanDetailItem].title;
- }
- - (NSString *)firstPic {
- return [self pindanDetailItem].firstPic;
- }
- - (kUserLevel)userLevel {
- return [self pindanDetailItem].userLevel;
- }
- - (NSString *)webURL {
- return [self pindanDetailItem].webURL;
- }
- - (NSAttributedString *)totalPriceAttributedString {
- return [self pindanDetailItem].priceDetailItem.totalPriceAttString;
- }
- - (NSString *)totalPrice {
- return [self pindanDetailItem].priceDetailItem.totalPrice;
- }
- - (NSString *)referPrice {
- return [self pindanDetailItem].priceDetailItem.refer_price;
- }
- - (NSString *)vipPrice {
- return [self pindanDetailItem].priceDetailItem.vipPrice;
- }
- - (NSString *)estimatePrice {
- return [self pindanDetailItem].priceDetailItem.estimate_price;
- }
- - (NSString *)internationalPostage {
- NSString *postage = [self pindanDetailItem].priceDetailItem.international_postage;
- if ([postage isKindOfClass:[NSString class]]) {
- return postage;
- }
- return @"-.--";
- }
- - (NSTimeInterval)timeInterval {
- return [self pindanDetailItem].timeInterval;
- }
- - (void)setTimeInterval:(NSTimeInterval)time {
- [self pindanDetailItem].timeInterval = time;
- }
- - (BOOL)isPostageFree {
- return [self pindanDetailItem].isPostageFree;
- }
- - (BOOL)isOnlyVIP {
- return [self pindanDetailItem].authorityItem.onlyVIP;
- }
- - (BOOL)isOnlyNew {
- return [self pindanDetailItem].authorityItem.onlyNew;
- }
- - (NSString *)nickname {
- return [self pindanDetailItem].nickname;
- }
- - (NSString *)introduceText {
- return [self pindanDetailItem].introduceText;
- }
- - (NSString *)headURL {
- return [self pindanDetailItem].headURL;
- }
- - (NSString *)serviceUserID {
- return [self pindanDetailItem].serveUserItem.userId;
- }
- - (NSString *)serviceNickname {
- return [self pindanDetailItem].serveUserItem.nickName;
- }
- - (NSString *)serviceHeadURL {
- return [self pindanDetailItem].serveUserItem.headerPic;
- }
- - (NSArray *)specsArray {
- return [self pindanDetailItem].specsArray;
- }
- - (NSInteger)limiteNum {
- return [self pindanDetailItem].limiteNum;
- }
- - (NSString *)firstSpecImgURL {
- return [[self pindanDetailItem] getFirstImgUrl];
- }
- - (NSArray *)productShowArray {
- return [self pindanDetailItem].productShowArray;
- }
- - (NSArray *)allProductIMGURLArray {
- return [self pindanDetailItem].allProductImgUrlArray;
- }
- - (ProductShowItem *)showItemAtIndex:(int)index {
- return [[self pindanDetailItem] showItemAtIndex:index];
- }
- - (NSString *)buyChannel {
- return [self pindanDetailItem].priceDetailItem.buyChannel;
- }
- - (NSString *)source {
- return [self pindanDetailItem].priceDetailItem.source;
- }
- - (NSString *)arrivalTime {
- return [self pindanDetailItem].priceDetailItem.arrival_time;
- }
- - (BOOL)isRemind {
- if ([[self pindanDetailItem].isLike isKindOfClass:[NSString class]]) {
- return [self pindanDetailItem].isLike.boolValue;
- }
- return NO;
- }
- - (BOOL)isVIP {
- if ([self pindanDetailItem].userLevel == kUserLevelVIP) {
- return YES;
- }
- return NO;
- }
- - (BOOL)isNeedIDCard {
- return [self pindanDetailItem].isNeedIDCard;
- }
- - (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];
- }
- - (kPindanState)pindanState {
- kPindanState pindanState = kPindanStateNormal;
- if (!self.pindanDetailItem.isPindanBegin) {
- pindanState = kPindanStateWaitBegin;
- }else if ([self isProductStateEnd] || [self isProductStateSaleDone]){
- pindanState = [self isRemind] ? kPindanStateCancelRemind : kPindanStateRemind;
- } else {
- if ([self pindanDetailItem].authorityItem.onlyNew == YES && [self pindanDetailItem].userOrderCount > 0) {
- pindanState = kPindanStateOnlyNew;
- }
- if ([self pindanDetailItem].authorityItem.onlyVIP == YES && [self pindanDetailItem].userLevel != kUserLevelVIP) {
- pindanState = kPindanStateOnlyVIP;
- }
- }
- return pindanState;
- }
- - (ProductState)productState {
- ProductState state = ProductStateNormal;
- if ([self isProductStateEnd]) {
- state = ProductStateOver;
- } else if ([self isProductStateSaleDone]){
- state = ProductStateSaleDone;
- }
- return state;
- }
- //- (SubmitOrderItem *)submitModelWithSpecArray:(NSArray *)specArray orderType:(kOrderType)orderType {
- // return [[self pindanDetailItem] submitModelWithSpecArray:specArray orderType:orderType];
- //}
- @end
|