// // OrdeDetailController.m // FirstLink // // Created by jack on 15/6/22. // Copyright (c) 2015年 FirstLink. All rights reserved. // #import "OrderDetailController.h" #import "LogisticsItem.h" #import "FKOrderGoodItem.h" #import "SubmitPaymentItem.h" #import "FKOrderSupplierItem.h" #import "FKSupplierDetailItem.h" #import "FKOrderAddressCell.h" #import "FKOrderSupplierStatusCell.h" #import "FKOrderDetailLogisticsCell.h" #import "FKOrderDetailProductCell.h" #import "FKOrderDetailActionCell.h" #import "PindanCommonCell.h" #import "FKOrderDetailTotalPriceCell.h" #import "FKOrderDetailSummaryCell.h" #import "FKOrderDetailPriceView.h" #import "FKOrderDetailRequest.h" #import "FKOrderDetailReform.h" #import "OrderDetailBottomVIew.h" #import "FKSignDoneView.h" #import "FLThirdPayHelper.h" #import "UIExtendAlertView.h" #import "ShakeRedPacketController.h" #import "ChatViewController.h" #import "OrderPayController.h" #import "PindanDetailController.h" #import "FKProDetailController.h" #import "FKGroupOrderController.h" #import "FKGroupDetailController.h" #import "LogisticsViewController.h" #import "FKRefundDetailController.h" #import "FKWeakTimerTarget.h" @interface OrderDetailController () @property (nonatomic, strong) NSString *orderID; @property (nonatomic, strong) FKBaseOrderViewModel *viewModel; @property (nonatomic, strong) UITableView *tableView; @property (nonatomic, strong) OrderDetailBottomVIew *bottomView; @property (nonatomic, strong) FKOrderDetailPriceView *detailPriceView; @property (nonatomic, assign) NSTimeInterval timeInterval; @end @implementation OrderDetailController - (instancetype)initWithOrderID:(NSString *)orderID { self = [super init]; if (self) { _orderID = orderID; } return self; } - (void)loadView { [super loadView]; [self addAllSubviews]; } - (void)viewDidLoad { [super viewDidLoad]; self.view.backgroundColor = [UIColor colorWithRed:235/255.0f green:235/255.0f blue:241/255.0f alpha:1.0]; if (self.openURLPara) { _orderID = self.openURLPara[@"order_id"]; } [self configTableViewCell]; } - (void)dealloc { } - (void)viewWillAppear:(BOOL)animated { [super viewWillAppear:animated]; [self.navigationController setNavigationBarHidden:NO animated:YES]; self.navigationItem.title = @"订单详情"; [self requestOrderDetail]; } - (void)viewDidAppear:(BOOL)animated { [super viewDidAppear:animated]; } - (void)viewWillDisappear:(BOOL)animated { [super viewWillDisappear:animated]; } #pragma mark - Request - (void)requestOrderDetail { if (_orderID.length > 0) { [self.hudView show:YES]; [FKOrderDetailRequest requestOrderDetail:FKOrderRequestDetail orderID:_orderID delegate:self]; } } #pragma mark - Response - (void)networkDidReceiveError:(NSError*)error identify:(int)identify header:(MSGHeader*)header { [self.hudView hide:NO]; [FLProgressHUDHelper showText:header.msg inView:self.view]; } - (void)networkDidSuccessResponse:(NSDictionary*)response identify:(int)identify header:(MSGHeader*)header{ [self.hudView hide:NO]; if (header.code.intValue == RESPONSE_MSG_NORMAL) { if (identify == FKOrderRequestDetail) { OrderDetailItem *orderDetailItem = [FKOrderDetailReform parserOrderDetailItem:response]; self.viewModel = [[FKNormalOrderViewModel alloc] initWithOrderID:orderDetailItem.orderID]; self.viewModel.orderDetailItem = orderDetailItem; // 获取BottomView高度 if ([self.viewModel respondsToSelector:@selector(bottomViewHeight)]) { CGFloat height = [self.viewModel bottomViewHeight]; [self remakeBottomViewHeight:height]; // 判断是否需要开启失效倒计时 if (height > 0) { NSTimeInterval interval = [self.viewModel countDownTimeInterval]; if (interval > 0) { // 防止多次启用倒计时 if (self.timeInterval == 0) { self.timeInterval = interval; self.bottomView.textLabel.text = [NSString stringWithFormat:@"失效倒计时:%@", [self.viewModel countDownTimeString:self.timeInterval]]; [FKWeakTimerTarget scheduledTimerWithTimeInterval:1.0 target:self selector:@selector(timeCountDownCallback) userInfo:nil repeats:YES]; } } else { self.bottomView.textLabel.text = @""; } } } // 普通订单和团订单设置BottomView标题 if ([self.viewModel respondsToSelector:@selector(bottomViewTitle)]) { NSString *title = [self.viewModel bottomViewTitle]; [self.bottomView.paymentButton setTitle:title forState:UIControlStateNormal]; } [self.tableView reloadData]; } else if (identify == FKOrderRequestConfirm) { NSString *score = [NSString stringWithFormat:@"%@", response[@"data"][@"score"]]; [FKSignDoneView showInView:self.view.window withNextDay:nil score:score bgImage:[UIImage imageNamed:@"sign_bg2"] completion:nil]; [self requestOrderDetail]; } } else { [FLProgressHUDHelper showText:header.msg inView:self.view]; } } #pragma mark - tableView dataSource & delegate - (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView { if ([self.viewModel respondsToSelector:@selector(numberOfSectionsInTableView)]) { return [self.viewModel numberOfSectionsInTableView]; } return 0; } - (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section { if ([self.viewModel respondsToSelector:@selector(numberOfRowsInSection:)]) { return [self.viewModel numberOfRowsInSection:section]; } return 0; } - (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section { return 10; } - (CGFloat)tableView:(UITableView *)tableView heightForFooterInSection:(NSInteger)section { return CGFLOAT_MIN; } - (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath { kOrderCellType cellType = kOrderCellTypeNone; if ([self.viewModel respondsToSelector:@selector(orderCellTypeAtIndexPath:)]) { cellType = [self.viewModel orderCellTypeAtIndexPath:indexPath]; } switch (cellType) { case kOrderCellTypeAddress: return [FKOrderAddressCell heightWithAddress:[self.viewModel.orderDetailItem.address detailAddressWithNoTip] idNum:[self.viewModel idCardNum]]; case kOrderCellTypeStatus: return 60; case kOrderCellTypeLogistics: return 60; case kOrderCellTypeSpecs: return (95 + 2); case kOrderCellTypeAction: return (50 - 2); case kOrderCellTypeRefundChargeClear: case kOrderCellTypeChargeClear: return 44; case kOrderCellTypeMessage: return [PindanBaseCell cellHeightForMessage:[self.viewModel priceDetailTitle:cellType].string]; case kOrderCellTypeTotalPrice: return (10 + 40); case kOrderCellTypeRefundTotalPrice: return (10 + 30); case kOrderCellTypeSummary: return 110; default: return 28; break; } } - (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath { id cell; if ([self.viewModel respondsToSelector:@selector(orderCellTypeAtIndexPath:)]) { kOrderCellType cellType = [self.viewModel orderCellTypeAtIndexPath:indexPath]; NSString *identify = [self.viewModel orderCellIdentifyWith:cellType]; cell = [self.tableView dequeueReusableCellWithIdentifier:identify]; switch (cellType) { case kOrderCellTypeStatus: case kOrderCellTypeLogistics: case kOrderCellTypeAddress: case kOrderCellTypeSpecs: case kOrderCellTypeTotalPrice: case kOrderCellTypeRefundTotalPrice: case kOrderCellTypeSummary: case kOrderCellTypeAction: { if ([cell respondsToSelector:@selector(configOrderDetailCell:viewModel:)]) { [cell configOrderDetailCell:indexPath viewModel:self.viewModel]; } if ([cell isKindOfClass:[FKOrderDetailSummaryCell class]]) { FKOrderDetailSummaryCell *headerCell = (FKOrderDetailSummaryCell *)cell; [headerCell.serviceButton addTarget:self action:@selector(clickConnectServiceAction) forControlEvents:UIControlEventTouchUpInside]; } if ([cell isKindOfClass:[FKOrderDetailActionCell class]]) { FKOrderDetailActionCell *actionCell = (FKOrderDetailActionCell *)cell; // actionCell.logisticsDetailButton.tag = indexPath.section; actionCell.priceDetailButton.tag = indexPath.section; actionCell.refundDetailButton.tag = indexPath.section; actionCell.confirmReceiptButton.tag = indexPath.section; // [actionCell.logisticsDetailButton addTarget:self action:@selector(clickLogisticsAction:) forControlEvents:UIControlEventTouchUpInside]; [actionCell.priceDetailButton addTarget:self action:@selector(clickPriceDetailAction:) forControlEvents:UIControlEventTouchUpInside]; [actionCell.refundDetailButton addTarget:self action:@selector(clickRefundPriceDetailAction:) forControlEvents:UIControlEventTouchUpInside]; [actionCell.confirmReceiptButton addTarget:self action:@selector(clickConfirmReceiptAction:) forControlEvents:UIControlEventTouchUpInside]; } break; } case kOrderCellTypeChargeClear: case kOrderCellTypeRefundChargeClear: { if ([cell respondsToSelector:@selector(configOrderDetailPriceCell:text:)]) { [cell configOrderDetailPriceCell:[self.viewModel chargeClearTitleWith:cellType] text:nil]; } if ([cell isKindOfClass:[PindanCommonCell class]]) { PindanCommonCell *commonCell = (PindanCommonCell *)cell; commonCell.bottomLine.hidden = NO; } break; } case kOrderCellTypeProductPrice: case kOrderCellTypeRefundProductPrice: case kOrderCellTypeOfficialCarriage: case kOrderCellTypeRefundOfficialCarriage: case kOrderCellTypeInternationCarriage: case kOrderCellTypeRefundInternationCarriage: case kOrderCellTypeTaxFee: case kOrderCellTypeRefundTaxFee: case kOrderCellTypeServiceFee: case kOrderCellTypeDerateFee: case kOrderCellTypeRefundDerateFee: case kOrderCellTypeMessage: { if ([cell respondsToSelector:@selector(configOrderDetailPriceCell:text:)]) { [cell configOrderDetailPriceCell:[self.viewModel priceDetailTitle:cellType] text:[self.viewModel priceDetailText:cellType]]; } if ([cell isKindOfClass:[PindanBaseCell class]]) { PindanBaseCell *commonCell = (PindanBaseCell *)cell; commonCell.bottomLine.hidden = YES; } break; } default: break; } } if (!cell) { cell = (id)[[UITableViewCell alloc] init]; } return (UITableViewCell*)cell; } - (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath { UITableViewCell *cell = [tableView cellForRowAtIndexPath:indexPath]; if ([cell isKindOfClass:[FKOrderDetailProductCell class]]){ FKOrderGoodItem *goodItem = [self.viewModel goodItemAtIndexPath:indexPath]; if((kOrderType)goodItem.objectType.intValue == kOrderTypeProduct) { FKProDetailController *controller = [[FKProDetailController alloc] initWithProductID:goodItem.objectID selectSpecId:goodItem.goodsID]; controller.hidesBottomBarWhenPushed = YES; [self.navigationController pushViewController:controller animated:YES]; } else { PindanDetailController *controller = [[PindanDetailController alloc] initWithUserID:goodItem.userID postID:goodItem.objectID]; controller.hidesBottomBarWhenPushed = YES; [self.navigationController pushViewController:controller animated:YES]; } } else if ([cell isKindOfClass:[FKOrderDetailLogisticsCell class]]) { [self pushLogisticsViewController:indexPath.section]; } } #pragma mark - Action - (IBAction)clickLogisticsAction:(UIButton *)sender { [self pushLogisticsViewController:sender.tag]; } - (void)pushLogisticsViewController:(NSInteger)section { FKOrderSupplierItem *item = [self.viewModel supplierItemAtIndex:(section - 1)]; FKSupplierDetailItem *detailItem = item.supplierDetailItem; LogisticsItem *logisticsItem = item.logisticsItem; if (detailItem.waybillID.length == 0) { [FLProgressHUDHelper showText:@"暂未查询到物流信息" inView:self.view]; } else { LogisticsViewController *controller = [LogisticsViewController new]; controller.waybillID = detailItem.waybillID; controller.logisticsID = logisticsItem.itemID; controller.hidesBottomBarWhenPushed = YES; [self.navigationController pushViewController:controller animated:YES]; } } - (IBAction)clickRefundPriceDetailAction:(UIButton *)sender { FKOrderSupplierItem *item = [self.viewModel supplierItemAtIndex:(sender.tag - 1)]; FKSupplierDetailItem *detailItem = item.supplierDetailItem; FKRefundDetailController *controller = [[FKRefundDetailController alloc] initWithPaymentID:detailItem.itemID]; controller.hidesBottomBarWhenPushed = YES; [self.navigationController pushViewController:controller animated:YES]; } - (IBAction)clickPriceDetailAction:(UIButton *)sender { FKOrderSupplierItem *item = [self.viewModel supplierItemAtIndex:(sender.tag - 1)]; FKSupplierDetailItem *supplierDetailItem = item.supplierDetailItem; self.detailPriceView.totalPriceLabel.text = [self convertFenStringToYuan:supplierDetailItem.totalPrice]; self.detailPriceView.officialPostageLabel.text = [self convertFenStringToYuan:supplierDetailItem.officialPostage]; self.detailPriceView.internationalPostageLabel.text = [self convertFenStringToYuan:supplierDetailItem.internationalPostage]; self.detailPriceView.taxFeeLabel.text = [self convertFenStringToYuan:supplierDetailItem.taxFee]; self.detailPriceView.serviceFeeLabel.text = [self convertFenStringToYuan:supplierDetailItem.serviceFee]; self.detailPriceView.derateLabel.text = [NSString stringWithFormat:@"-%@", [self convertFenStringToYuan:supplierDetailItem.derateFee]]; [self.view.window addSubview:self.detailPriceView]; } - (IBAction)clickConfirmReceiptAction:(UIButton *)sender { UIExtendAlertView *alert = [[UIExtendAlertView alloc] initWithTitle:nil message:@"是否确认收货?" delegate:self cancelButtonTitle:@"否" otherButtonTitles:@"是", nil]; alert.delegate = self; // alert.userInfo = @{@"type": FKPersonOrderCancelAction}; alert.tag = (sender.tag - 1); [alert show]; } - (void)clickBottomButtonAction { if ([self.viewModel getOrderStatus] == FKOrderStatusUnpay) { SubmitPaymentItem *paymentItem = [[SubmitPaymentItem alloc]init]; paymentItem.orderID = self.viewModel.orderDetailItem.orderID; paymentItem.successController = FKOrderPaymentSuccessControllerRedpacket; OrderPayController *orderPayController = [[OrderPayController alloc] initWithPaymentItem:paymentItem]; NSMutableArray *viewControllers = [NSMutableArray arrayWithArray:self.navigationController.viewControllers]; for (UIViewController *controller in viewControllers) { if ([controller isKindOfClass:[OrderPayController class]]){ [viewControllers removeObject:controller]; break; } } [viewControllers addObject:orderPayController]; [self.navigationController setViewControllers:viewControllers animated:YES]; } } - (void)clickConnectServiceAction { OrderDetailItem *item = self.viewModel.orderDetailItem; ChatViewController *chatController = [[ChatViewController alloc] initWithChatter:item.serveUserItem.userId isGroup:NO]; chatController.naviTitle = self.viewModel.orderDetailItem.serveUserItem.nickName; chatController.receiverHeadURL = item.serveUserItem.headerPic; chatController.msgExtData = [self connectCustomerExtData:item]; [self.navigationController pushViewController:chatController animated:YES]; // 行为统计 NSString *jsonStr = [FLRequestHelper JSONStringWithKeys:@[@"order_id"] values:@[self.viewModel.orderDetailItem.orderID]]; [BehaviorTrackManger requestBehaviorType:kBehaviorOrderServe jsonString:jsonStr]; } #pragma mark - Alert View Delegate - (void)alertView:(UIAlertView *)alertView clickedButtonAtIndex:(NSInteger)buttonIndex { if (buttonIndex == 1) { if ([alertView isKindOfClass:[UIExtendAlertView class]]) { FKOrderSupplierItem *item = [self.viewModel supplierItemAtIndex:alertView.tag]; FKSupplierDetailItem *detailItem = item.supplierDetailItem; if (detailItem.itemID.length > 0) { [FKOrderDetailRequest requestConfirmReceive:FKOrderRequestConfirm orderID:detailItem.itemID deleagate:self]; } } } } #pragma mark - Method - (void)timeCountDownCallback { self.timeInterval = (self.timeInterval - 1); self.bottomView.textLabel.text = [NSString stringWithFormat:@"失效倒计时:%@", [self.viewModel countDownTimeString:self.timeInterval]]; if (self.timeInterval <= 0) { [self remakeBottomViewHeight:0]; [self.viewModel setOrderAsInvalid]; [self.tableView reloadData]; } } - (NSString *)convertFenStringToYuan:(NSString *)string { return [NSString stringWithFormat:@"¥%.2f", string.floatValue/100.0]; } #pragma mark - Private - (void)remakeBottomViewHeight:(CGFloat)height { [self.bottomView mas_remakeConstraints:^(MASConstraintMaker *make) { make.left.right.bottom.equalTo(self.view); make.height.equalTo(@(height)); }]; } - (NSDictionary *)connectCustomerExtData:(OrderDetailItem *)item { return @{EXT_MSG_ORDER_TITLE: [NSString stringWithFormat:@"订单号:%@", item.paymentNO], EXT_MSG_GOODS_ID_V2: self.viewModel.orderDetailItem.orderID, EXT_QUEUE_NAME : QueueNameShouhou}; } - (void)configTableViewCell { [self.tableView registerClass:[FKOrderAddressCell class] forCellReuseIdentifier:FKOrderDetailReceiverAddressCellIdentify]; [self.tableView registerClass:[FKOrderSupplierStatusCell class] forCellReuseIdentifier:FKOrderSupplierStatusCellIdentify]; [self.tableView registerClass:[FKOrderDetailLogisticsCell class] forCellReuseIdentifier:FKOrderDetailLogisticsCellIdentify]; [self.tableView registerClass:[FKOrderDetailProductCell class] forCellReuseIdentifier:FKOrderDetailProductDetailCellIdentify]; [self.tableView registerClass:[FKOrderDetailActionCell class] forCellReuseIdentifier:FKOrderDetailActionCellIdentify]; [self.tableView registerClass:[FKOrderDetailTotalPriceCell class] forCellReuseIdentifier:FKOrderDetailTotalPriceCellIdentify]; [self.tableView registerClass:[PindanBaseCell class] forCellReuseIdentifier:FKOrderDetailPindanBaseCellIdentify]; [self.tableView registerClass:[PindanCommonCell class] forCellReuseIdentifier:FKOrderDetailPindanCommonCellIdentify]; [self.tableView registerClass:[FKOrderDetailSummaryCell class] forCellReuseIdentifier:FKOrderDetailSummaryCellIdentify]; } #pragma mark - Layout - (void)addAllSubviews { [self.view addSubview:self.bottomView]; [self.bottomView mas_remakeConstraints:^(MASConstraintMaker *make) { make.left.right.bottom.equalTo(self.view); make.height.equalTo(@(0)); }]; [self.view addSubview:self.tableView]; [self.tableView mas_makeConstraints:^(MASConstraintMaker *make) { make.left.top.right.equalTo(self.view); make.bottom.equalTo(self.bottomView.mas_top); }]; } #pragma mark - Property - (UITableView *)tableView { if (_tableView == nil) { _tableView = [[UITableView alloc]initWithFrame:CGRectZero style:UITableViewStyleGrouped]; _tableView.dataSource = self; _tableView.delegate = self; _tableView.separatorStyle = UITableViewCellSeparatorStyleNone; _tableView.sectionFooterHeight = 0; _tableView.backgroundColor = COMMON_TABLEVIEW_BACKGROUND_COLOR; if (@available(iOS 11.0, *)) { _tableView.contentInsetAdjustmentBehavior = UIScrollViewContentInsetAdjustmentNever; } } return _tableView; } - (OrderDetailBottomVIew *)bottomView { if (_bottomView == nil) { _bottomView = [[OrderDetailBottomVIew alloc]init]; [_bottomView.paymentButton addTarget:self action:@selector(clickBottomButtonAction) forControlEvents:UIControlEventTouchUpInside]; } return _bottomView; } - (FKOrderDetailPriceView *)detailPriceView { if (!_detailPriceView) { _detailPriceView = [[FKOrderDetailPriceView alloc] init]; _detailPriceView.frame = CGRectMake(0, 0, UISCREENWIDTH, UISCREENHEIGH); _detailPriceView.backgroundColor = [UIColor colorWithRed:0 green:0 blue:0 alpha:0.6]; } return _detailPriceView; } @end