// // FuturesViewController.swift // MTP2_iOS // // Created by Muchinfo on 2021/1/18. // Copyright © 2021 Muchinfo. All rights reserved. // import UIKit import JXSegmentedView /// 期货视图容器控制类视图 class FuturesViewController: BaseViewController { // MARK: - 属性列表 /// 类目 @IBOutlet weak var segmentedView: JXSegmentedView! { didSet { segmentedView.dataSource = dataSource segmentedView.indicators = [indicator] segmentedView.delegate = self } } /// 列头视图 @IBOutlet weak var titleView: UIView! /// 标题 @IBOutlet weak var titleButton: UIButton! { didSet { /// 异常 guard let taAccountInfo = MTP2BusinessCore.shared.accountManager?.moTaAccountInfo else { return } /// 切换账户 titleButton.setAttributedTitle("\(taAccountInfo.accountname) (\(taAccountInfo.accountid))".withFont(.font_10).withTextColor(.white), for: .normal) } } /// 标题 @IBOutlet weak var arrow: UIButton! /// 内容装载视图 @IBOutlet weak var contentView: UIStackView! /// 行情视图 lazy var quote: QuoteViewController = { $0.view.frame = CGRect(origin: CGPoint.zero, size: contentView.frame.size) self.addChild($0) $0.didMove(toParent: self) return $0 } (self.viewController("Futures", "Quote") as! QuoteViewController) /// 持仓 lazy var position: PositionDetailViewController = { $0.view.frame = CGRect(origin: CGPoint.zero, size: contentView.frame.size) $0.showType = .fromOrder self.addChild($0) $0.didMove(toParent: self) return $0 } (self.viewController("Futures", "Position") as! PositionDetailViewController) /// 委托 lazy var order: InnerOrderDetailViewController = { $0.showType = .fromOrder $0.view.frame = CGRect(origin: CGPoint.zero, size: contentView.frame.size) self.addChild($0) $0.didMove(toParent: self) return $0 } (self.viewController("Futures", "Order") as! InnerOrderDetailViewController) /// 成交 lazy var trade: TradeDetailViewController = { $0.view.frame = CGRect(origin: CGPoint.zero, size: contentView.frame.size) self.addChild($0) $0.showType = .fromOrder $0.didMove(toParent: self) return $0 } (self.viewController("Futures", "Trade") as! TradeDetailViewController) /// 资金 lazy var fund: FundInfoViewController = { $0.view.frame = CGRect(origin: CGPoint.zero, size: contentView.frame.size) self.addChild($0) $0.didMove(toParent: self) return $0 } (self.viewController("Futures", "FundInfo") as! FundInfoViewController) /// 权限菜单 var roleTitles: [String] { get { var titles: [String] = [] /// 权限控制 guard let commonManager = MTP2BusinessCore.shared.commonManager else { return titles } if commonManager.containMenu(key: "client_futures_quote") { titles.append("行情") } if commonManager.containMenu(key: "client_futures_position") { titles.append("持仓") } if commonManager.containMenu(key: "client_futures_order") { titles.append("委托") } if commonManager.containMenu(key: "client_futures_trade") { titles.append("成交") } if commonManager.containMenu(key: "client_futures_fund") { titles.append("资金") } return titles } } // MARK: - 生命周期 override func viewDidLoad() { super.viewDidLoad() // Do any additional setup after loading the view. /// 不需要返回按钮 addBackBarButtonItem(true) } override func viewWillAppear(_ animated: Bool) { super.viewWillAppear(animated) let value = UIInterfaceOrientation.portrait.rawValue UIDevice.current.setValue(value, forKey: "orientation") } override func loadView() { super.loadView() /// 标题视图 self.navigationItem.titleView = titleView /// titles dataSource.titles = roleTitles segmentedView.reloadData() /// 默认选中第一个 segmentedView.defaultSelectedIndex = 0 segmentedView.delegate?.segmentedView(segmentedView, didSelectedItemAt: 0) } /// onTitleItemPressed /// - Parameter sender: sender @IBAction func onTitleItemPressed(_ sender: UIButton) { /// 行情不做任何处理 if roleTitles[segmentedView.selectedIndex] == "行情" { return } /// 账户切换 let se = viewController("Main", "SelectTaAccountInfo") as! SelectTaAccountInfoViewController se.modalTransitionStyle = .crossDissolve se.modalPresentationStyle = .overFullScreen se.popBlock = { (_ takeInfo: Any?) in if let taAccountInfo = takeInfo as? MoTaAccountInfo { /// 切换账户 self.titleButton.setAttributedTitle("\(taAccountInfo.accountname) (\(taAccountInfo.accountid))".withFont(.font_10).withTextColor(.white), for: .normal) switch self.roleTitles[self.segmentedView.selectedIndex] { case "持仓": /// 持仓 self.position.tableView.triggerRefreshing() case "委托": /// 委托 self.order.tableView.triggerRefreshing() case "成交": /// 成交 self.trade.tableView.triggerRefreshing() case "资金": /// 资金 if self.fund.isFundInfo { /// 显示资金信息 self.refreshOrders() } else { /// 查询资金流水 self.fund.queryAmountLog() } default: break } } } self.present(se, animated: true, completion: {}) } /// 切换资金账户后重新去请求单据数据 fileprivate func refreshOrders() { /// 异常 guard let orderManager = MTP2BusinessCore.shared.orderManager else { return } /// group let group = DispatchGroup() group.enter() orderManager.queryTradePosition(tradeMode: nil) { isSuccess, er, objs in group.leave() } group.enter() orderManager.queryTradeOrderDetail(tradeMode: nil, orderStatus: nil, orderID: nil) { isSuccess, er, objs in group.leave() } group.enter() orderManager.queryTradeDetail(tradeMode: nil, tradeID: nil, orderID: nil, buildType: nil, tradeType: nil, goodsID: nil) { isSuccess, er, objs in group.leave() } group.enter() orderManager.queryErmcpTradePositions(marketID: nil) { isSuccess, er, objs in group.leave() } group.enter() orderManager.queryErmcpOrderDetails { isSuccess, er, objs in group.leave() } group.enter() orderManager.queryErmcpTradeDetails(goodsid: nil, buyOrSell: nil, orderID: nil) { isSuccess, er, objs in group.leave() } group.notify(queue: .main) { /// 显示资金信息 self.fund.showTaAccountInfo() } } } // MARK: - JXSegmentedViewDelegate extension FuturesViewController: JXSegmentedViewDelegate { /// didSelectedItemAt /// - Parameters: /// - segmentedView: segmentedView /// - index: index func segmentedView(_ segmentedView: JXSegmentedView, didSelectedItemAt index: Int) { /// 移出所有视图 if contentView.arrangedSubviews.count != 0 { contentView.arrangedSubviews.forEach { (view) in contentView.removeArrangedSubview(view) view.isHidden = true (view as? PageChangeDelegate)?.didHide() /// 回调父类方法 view.superController()?.viewDidDisappear(true) } } switch roleTitles[index] { case "行情": /// 行情 contentView.insertArrangedSubview(quote.view, at: 0) quote.view.isHidden = false case "持仓": /// 头寸 contentView.insertArrangedSubview(position.view, at: 0) position.view.isHidden = false position.didShow() case "委托": /// 委托 contentView.insertArrangedSubview(order.view, at: 0) order.view.isHidden = false order.didShow() case "成交": /// 成交 contentView.insertArrangedSubview(trade.view, at: 0) trade.view.isHidden = false trade.didShow() default: /// 资金 contentView.insertArrangedSubview(fund.view, at: 0) fund.view.isHidden = false } /// 隐藏 arrow.isHidden = roleTitles[index] == "行情" titleButton.isHidden = roleTitles[index] == "行情" } }