// // OrderViewController.swift // MTP2_iOS // // Created by Handy_Cao on 2020/10/28. // Copyright © 2020 Muchinfo. All rights reserved. // import UIKit import JXSegmentedView import WHToast import SDWebImage import GTMRefresh import IBAnimatable /// 我的订单视图容器控制类 class OrderViewController: BaseViewController { // MARK: - 属性列表 /// 分段控制器 @IBOutlet weak var segmentedView: JXSegmentedView! { didSet { segmentedView.delegate = self segmentedView.dataSource = dataSource segmentedView.indicators = [self.indicator] } } /// segmentedDataSource let dataSource: JXSegmentedTitleDataSource = { var items: [String] = ["全部", "待成交", "待付款", "已完成"] $0.titles = items $0.titleNormalColor = UIColorFromHex(rgbValue: 0x333333) $0.titleNormalFont = .font_14 $0.titleSelectedColor = UIColorFromHex(rgbValue: 0x60a1e3) $0.titleSelectedFont = .font_14 $0.isTitleColorGradientEnabled = true return $0 } (JXSegmentedTitleDataSource()) /// 列表 @IBOutlet weak var tableView: UITableView! /// CellIdentifier let CellIdentifier = "Order_Cell" /// 单据数据信息 var orders: [MoHsbyMyBuyOrderDetails] = [] { didSet { /// 刷新列表数据 tableView.reloadData() /// 隐藏按钮 self.noDataButton.isHidden = !(orders.count == 0) } } // MARK: - 生命周期 override func viewDidLoad() { super.viewDidLoad() // Do any additional setup after loading the view. /// UI界面初始化 buildView() } override func viewWillAppear(_ animated: Bool) { super.viewWillAppear(animated) /// 隐藏导航栏 self.navigationController?.setNavigationBarHidden(false, animated: true) /// 数据初始化 initData() } // MARK: - 初始化 /// UI界面初始化 fileprivate func buildView() { /// loding...... self.addLoadingView() /// 添加下拉刷新控件 self.tableView.gtm_addRefreshHeaderView(refreshHeader: DefaultGTMRefreshHeader()) { /// 重置请求条件 self.reloadData(self.segmentedView.selectedIndex) } } /// 数据初始化 fileprivate func initData() { /// 请求数据 DispatchQueue.main.asyncAfter(deadline: DispatchTime.now()+0.2) { /// 默认滚动 self.segmentedView.defaultSelectedIndex = self.selectedIndex /// 刷新 self.segmentedView.reloadData() /// 刷新数据 self.reloadData(self.selectedIndex) } } // MARK: - 页面跳转 /// 详情页面跳转 /// - Parameter detail: detail fileprivate func goToGoodsDetailViewController(_ detail: MoGoodsInfo) { /// 商品详情 let goodsDetailController = UIStoryboard(name: "Quote", bundle: nil).instantiateViewController(withIdentifier: "GoodsDetail") as! GoodsDetailViewController goodsDetailController.model = detail self.navigationController?.pushViewController(goodsDetailController, animated: true) } // MARK: - 接口请求 /// 刷新数据 fileprivate func reloadData(_ segmentIndex: Int) { switch segmentIndex { case 1: /// 抢购中 self.requestQueryHsbyMyBuyOrderDetails(1) case 2: /// 查询待付款数据信息 self.requestQueryMyPayOrders() case 3: /// 查询已完成 self.requestQueryHsbyBuyMyTradeDetail() default: /// 查询全部 self.requestQueryHsbyMyBuyOrderDetails(nil) } } /// 查询我的单据数据信息 /// - Parameter status: 说明: 全部:一二级市场买委托;抢购中:一级市场买摘; 求购中:二级市场买挂 fileprivate func requestQueryHsbyMyBuyOrderDetails(_ status: Int?) { /// 异常 guard let orderManager = MTP2BusinessCore.shared.orderManager else { return } /// startAnimating _anim?.startAnimating() /// 发送请求 orderManager.requestQueryHsbyMyBuyOrderDetails(status) { (isComplete, error, orderDetails) in DispatchQueue.main.async { /// stopAnimating self._anim?.stopAnimating() /// endRefreshing self.tableView.endRefreshing(isSuccess: true) /// 数据赋值 if isComplete { if status == 1 { self.orders = orderDetails?.filter({$0.mybuystatus == .BuyIn || $0.mybuystatus == .LookingForIn}) ?? [] } else { self.orders = orderDetails ?? [] } } else { /// 查询失败 self.orders = [] WHToast.showError(withMessage: "数据查询失败,原因:\(error?.retMsg ?? "未知错误")", duration: 1.5, finishHandler: {}) } } } } /// 查询已完成订单数据信息 fileprivate func requestQueryHsbyBuyMyTradeDetail() { /// 异常 guard let orderManager = MTP2BusinessCore.shared.orderManager else { return } /// startAnimating _anim?.startAnimating() /// 发送请求 orderManager.requestQueryHsbyBuyMyTradeDetail(nil, nil) { (isComplete, error, objs) in DispatchQueue.main.async { /// stopAnimating self._anim?.stopAnimating() /// endRefreshing self.tableView.endRefreshing(isSuccess: true) /// 数据赋值 if isComplete { self.orders = objs ?? [] } else { /// 查询失败 self.orders = [] WHToast.showError(withMessage: "数据查询失败,原因:\(error?.retMsg ?? "未知错误")", duration: 1.5, finishHandler: {}) } } } } /// 查询待付款单据数据信息 fileprivate func requestQueryMyPayOrders() { /// 异常 guard let orderManager = MTP2BusinessCore.shared.orderManager else { return } /// startAnimating _anim?.startAnimating() /// 发送请求 orderManager.requestQueryMyPayOrders(nil, nil) { (isComplete, error, payOrders) in DispatchQueue.main.async { /// stopAnimating self._anim?.stopAnimating() /// endRefreshing self.tableView.endRefreshing(isSuccess: true) /// 数据赋值 if isComplete { self.orders = payOrders ?? [] } else { /// 查询失败 self.orders = [] WHToast.showError(withMessage: "数据查询失败,原因:\(error?.retMsg ?? "未知错误")", duration: 1.5, finishHandler: {}) } } } } /// 撤单 /// /// - Parameter orderId: 单号 fileprivate func cancelOrder(_ orderId: String?, _ accountId: Int64?, _ goodsID: String?, _ marketID: Int?) { /// 异常 guard let tradeManager = MTP2BusinessCore.shared.tradeManager else { return } /// alert showCommitAlertController(title: "提示", message: "确定要撤单吗?"){(_) in /// startAnimating self._anim?.startAnimating() /// 构建数据 let cancelOrderData = CancelOrderData(oldOrderId: orderId ?? "", accountId: accountId ?? 0, goodsID: goodsID ?? "", marketID: marketID ?? 0, operateType: .OPERATETYPE_ORDERCANCEL) /// 发送撤单请求 tradeManager.cancelOrder(cancelorderData: cancelOrderData, callback: { (isCompleted, errorInfo) in DispatchQueue.main.async { /// stopAnimating self._anim?.stopAnimating() if isCompleted { WHToast.showSuccess(withMessage: "撤销成功", duration: 1.5, finishHandler: { /// 刷新数据 self.reloadData(self.segmentedView.selectedIndex) }) } else { WHToast.showError(withMessage: "撤单失败,原因:\(ErrorUtils.getDesc(errorCode: errorInfo?.retCode ?? -60001) ?? "未知错误")", duration: 1.5, finishHandler: {}) } } }) } } /// 取消待付款订单 /// /// - Parameter orderId: 单号 fileprivate func cancelPayment(_ orderId: String?, _ accountId: Int64?, _ goodsID: String?, _ marketID: Int?) { /// 异常 guard let tradeManager = MTP2BusinessCore.shared.tradeManager else { return } /// alert showCommitAlertController(title: "提示", message: "确定要撤单吗?"){(_) in /// startAnimating self._anim?.startAnimating() /// 构建数据 let cancelOrderData = CancelOrderData(oldOrderId: orderId ?? "", accountId: accountId ?? 0, goodsID: goodsID ?? "", marketID: marketID ?? 0, operateType: .OPERATETYPE_ORDERCANCEL) /// 发送撤单请求 tradeManager.cancelPayment(cancelorderData: cancelOrderData, callback: { (isCompleted, errorInfo) in DispatchQueue.main.async { /// stopAnimating self._anim?.stopAnimating() if isCompleted { WHToast.showSuccess(withMessage: "撤销成功", duration: 1.5, finishHandler: { /// 刷新数据 self.reloadData(self.segmentedView.selectedIndex) }) } else { WHToast.showError(withMessage: "撤单失败,原因:\(ErrorUtils.getDesc(errorCode: errorInfo?.retCode ?? -60001) ?? "未知错误")", duration: 1.5, finishHandler: {}) } } }) } } /* // MARK: - Navigation // In a storyboard-based application, you will often want to do a little preparation before navigation override func prepare(for segue: UIStoryboardSegue, sender: Any?) { // Get the new view controller using segue.destination. // Pass the selected object to the new view controller. } */ } // MARK: - JXSegmentedViewDelegate extension OrderViewController: JXSegmentedViewDelegate { func segmentedView(_ segmentedView: JXSegmentedView, didSelectedItemAt index: Int) { /// 重置默认状态 self.selectedIndex = index /// 刷新数据 self.reloadData(index) } } // MARK: - UITableViewDelegate, UITableViewDataSource extension OrderViewController: UITableViewDelegate, UITableViewDataSource { func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int { return orders.count } func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell { let cell = tableView.dequeueReusableCell(withIdentifier: CellIdentifier, for: indexPath) as! OrderCell cell.model = orders[indexPath.row] cell.cancelBlock = { (_ orderId: String?, _ accountId: Int64?, _ goodsID: String?, _ marketID: Int?) in if self.segmentedView.selectedIndex == 2 { /// 撤销待付款单据 self.cancelPayment(orderId, accountId, goodsID, marketID) } else { /// 撤销 self.cancelOrder(orderId, accountId, goodsID, marketID) } } /// 去支付 cell.payBlock = { (_ orderId: String?, _ accountId: Int64?, _ goodsID: String?, _ marketID: Int?, _ amount: Double) in /// 异常 guard let orderManager = MTP2BusinessCore.shared.orderManager else { return } /// 查询查询单据数据 orderManager.requestQueryMyPayOrders(orderId, nil) { (isComplete, error, payOrders) in DispatchQueue.main.async { if isComplete { /// 异常 guard let paymentController = self.viewController(name: "Order", identifier: "Payment") as? PaymentViewController else { return } /// 待付款单据 paymentController.myPayOrder = self.orders[indexPath.row] as? MoMyPayOrders /// 页面跳转 self.navigationController?.pushViewController(paymentController, animated: true) } else { WHToast.showError(withMessage: "单据获取失败,请稍候重试!", duration: 1.5, finishHandler: {}) } } } } return cell } func tableView(_ tableView: UITableView, heightForRowAt indexPath: IndexPath) -> CGFloat { switch segmentedView.selectedIndex { case 3: return 170.0 case 2: return 200.0 default: return orders[indexPath.row].mybuystatus != .LookingForIn ? 175.0 : 200.0 } } func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) { /// 数据异常 guard let goodsManager = MTP2BusinessCore.shared.goodsManager else { return } if self.orders[indexPath.row].trademode == .TRADEMODE_LISTING_SELECT { /// 挂牌点选 /// 查询商品详情 goodsManager.requestQueryHsbyListingGoodsDetail(self.orders[indexPath.row].goodsid, callback: { (isComplete, error, goods) in DispatchQueue.main.async { if isComplete, let detail = goods { self.goToGoodsDetailViewController(detail) } else { WHToast.showError(withMessage: "该商品已退市", duration: 1.5, finishHandler: {}) } } }) } else if self.orders[indexPath.row].trademode == .TRADEMODE_TRADEMODE_HSBY_SHOP {} /// 商城 不做跳转 else { /// 查询商品详情 goodsManager.requestQueryHsbyPreGoodsDetail(self.orders[indexPath.row].goodsid, callback: { (isComplete, error, goods) in DispatchQueue.main.async { if isComplete, let detail = goods { self.goToGoodsDetailViewController(detail) } else { WHToast.showError(withMessage: "该商品已退市", duration: 1.5, finishHandler: {}) } } }) } } } // MARK: - OrderCell class OrderCell: BaseTableViewCell { /// 供应商名称 @IBOutlet weak var company: UILabel! /// 状态 @IBOutlet weak var status: UILabel! /// 单号 @IBOutlet weak var orderNo: UILabel! /// 商品图片 @IBOutlet weak var goodsImage: UIImageView! /// 价格 @IBOutlet weak var price: UILabel! /// 金额 @IBOutlet weak var amount: UILabel! /// 标题 @IBOutlet weak var title: UILabel! /// 数量 @IBOutlet weak var qty: UILabel! /// 撤销 @IBOutlet weak var cancel: AnimatableButton! /// 去支付 @IBOutlet weak var pay: AnimatableButton! /// 撤销 @IBOutlet weak var stackView: UIStackView! /// 数据 override var model: MoHsbyMyBuyOrderDetails? { didSet { /// 数据异常 guard let obj = model else { return } /// 单号 orderNo.text = obj.goodscode /// 商品名称 title.text = obj.goodsname /// 供应商名称 company.text = model?.trademode == .TRADEMODE_TRADEMODE_CPTRADE_LS ? obj.vendorname1 : obj.vendorname2 if let payOrder = obj as? MoMyPayOrders { /// 待付款 /// 状态 status.text = payOrder.payflag.description /// 成交价格 price.text = "\(obj.currencysign) \(payOrder.tradeprice)" /// 金额 amount.text = "总价:\(payOrder.tradeamount.toString(reserve: 2)), 优惠:\(payOrder.offamount.toString(reserve: 2)), 实付款:\(payOrder.payamount.toString(reserve: 2))" /// 数量 qty.text = "×\(payOrder.tradeqty)" /// 只有求购中的商品才可以撤销单据 pay.isHidden = !(payOrder.payflag == .UnPay) /// 只有求购中的商品才可以撤销单据 stackView.isHidden = false } else if let sellMyDetail = obj as? MoHsbySellMyDetail { /// 我的订单 (已完成) /// 价格 price.text = "\(sellMyDetail.currencysign) \(sellMyDetail.price)" /// 金额 amount.text = "金额:\(sellMyDetail.amount)" /// 数量 qty.text = "×\(sellMyDetail.qty)" /// 状态 status.text = "已完成" /// 只有求购中的商品才可以撤销单据 stackView.isHidden = true } else { /// 价格 price.text = "\(obj.currencysign) \(obj.orderprice)" /// 金额 amount.text = "金额:\(obj.orderamount)" /// 数量 qty.text = "×\(obj.orderqty)" /// 状态 status.text = obj.mybuystatus.description /// 只有求购中的商品才可以撤销单据 stackView.isHidden = !(obj.mybuystatus == .LookingForIn) /// 隐藏支付按钮 pay.isHidden = true } /// 商品图片 guard let url = StringUtils.getImageUrl(obj.trademode == .TRADEMODE_TRADEMODE_CPTRADE_LS ? obj.picurls1 : obj.picurls2) else { return } goodsImage.sd_setImage(with: url, placeholderImage: UIImage(named: "placeholder_image"), options: .queryDiskDataSync, context: nil) } } /// 撤销回调块 var cancelBlock: ((_ orderId: String?, _ accountId: Int64?, _ goodsID: String?, _ marketID: Int?) -> Void)? /// 支付回调块 var payBlock: ((_ orderId: String?, _ accountId: Int64?, _ goodsID: String?, _ marketID: Int?, _ amount: Double) -> Void)? /// 按钮点击响应事件 /// - Parameter sender: sender @IBAction private func onButtonPressed(_ sender: AnimatableButton) { switch sender { case cancel: /// 撤销 if let obj = model as? MoMyPayOrders { if let cancel = cancelBlock { cancel(obj.tradeid, Int64(obj.buyaccountid), model?.goodsid.description, model?.marketid) } } else { if let cancel = cancelBlock { cancel(model?.orderid, model?.accountid ?? 0, model?.goodsid.description, model?.marketid) } } case pay: if let pay = payBlock { pay((model as? MoMyPayOrders)?.tradeid, model?.accountid ?? 0, model?.goodsid.description, model?.marketid, (model as? MoMyPayOrders)?.tradeamount ?? 0.0) } default: break } } }