FuturesViewController.swift 9.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264
  1. //
  2. // FuturesViewController.swift
  3. // MTP2_iOS
  4. //
  5. // Created by Muchinfo on 2021/1/18.
  6. // Copyright © 2021 Muchinfo. All rights reserved.
  7. //
  8. import UIKit
  9. import JXSegmentedView
  10. /// 期货视图容器控制类视图
  11. class FuturesViewController: BaseViewController {
  12. // MARK: - 属性列表
  13. /// 类目
  14. @IBOutlet weak var segmentedView: JXSegmentedView! {
  15. didSet {
  16. segmentedView.dataSource = dataSource
  17. segmentedView.indicators = [indicator]
  18. segmentedView.delegate = self
  19. }
  20. }
  21. /// 列头视图
  22. @IBOutlet weak var titleView: UIView!
  23. /// 标题
  24. @IBOutlet weak var titleButton: UIButton! {
  25. didSet {
  26. /// 异常
  27. guard let taAccountInfo = MTP2BusinessCore.shared.accountManager?.moTaAccountInfo else { return }
  28. /// 切换账户
  29. titleButton.setAttributedTitle("\(taAccountInfo.accountname) (\(taAccountInfo.accountid))".withFont(.font_10).withTextColor(.white), for: .normal)
  30. }
  31. }
  32. /// 标题
  33. @IBOutlet weak var arrow: UIButton!
  34. /// 内容装载视图
  35. @IBOutlet weak var contentView: UIStackView!
  36. /// 行情视图
  37. lazy var quote: QuoteViewController = {
  38. $0.view.frame = CGRect(origin: CGPoint.zero, size: contentView.frame.size)
  39. self.addChild($0)
  40. $0.didMove(toParent: self)
  41. return $0
  42. } (self.viewController("Futures", "Quote") as! QuoteViewController)
  43. /// 持仓
  44. lazy var position: PositionDetailViewController = {
  45. $0.view.frame = CGRect(origin: CGPoint.zero, size: contentView.frame.size)
  46. $0.showType = .fromOrder
  47. self.addChild($0)
  48. $0.didMove(toParent: self)
  49. return $0
  50. } (self.viewController("Futures", "Position") as! PositionDetailViewController)
  51. /// 委托
  52. lazy var order: InnerOrderDetailViewController = {
  53. $0.showType = .fromOrder
  54. $0.view.frame = CGRect(origin: CGPoint.zero, size: contentView.frame.size)
  55. self.addChild($0)
  56. $0.didMove(toParent: self)
  57. return $0
  58. } (self.viewController("Futures", "Order") as! InnerOrderDetailViewController)
  59. /// 成交
  60. lazy var trade: TradeDetailViewController = {
  61. $0.view.frame = CGRect(origin: CGPoint.zero, size: contentView.frame.size)
  62. self.addChild($0)
  63. $0.showType = .fromOrder
  64. $0.didMove(toParent: self)
  65. return $0
  66. } (self.viewController("Futures", "Trade") as! TradeDetailViewController)
  67. /// 资金
  68. lazy var fund: FundInfoViewController = {
  69. $0.view.frame = CGRect(origin: CGPoint.zero, size: contentView.frame.size)
  70. self.addChild($0)
  71. $0.didMove(toParent: self)
  72. return $0
  73. } (self.viewController("Futures", "FundInfo") as! FundInfoViewController)
  74. /// 权限菜单
  75. var roleTitles: [String] {
  76. get {
  77. var titles: [String] = []
  78. /// 权限控制
  79. guard let commonManager = MTP2BusinessCore.shared.commonManager else {
  80. return titles
  81. }
  82. if commonManager.containMenu(key: "client_futures_quote") {
  83. titles.append("行情")
  84. }
  85. if commonManager.containMenu(key: "client_futures_position") {
  86. titles.append("持仓")
  87. }
  88. if commonManager.containMenu(key: "client_futures_order") {
  89. titles.append("委托")
  90. }
  91. if commonManager.containMenu(key: "client_futures_trade") {
  92. titles.append("成交")
  93. }
  94. if commonManager.containMenu(key: "client_futures_fund") {
  95. titles.append("资金")
  96. }
  97. return titles
  98. }
  99. }
  100. // MARK: - 生命周期
  101. override func viewDidLoad() {
  102. super.viewDidLoad()
  103. // Do any additional setup after loading the view.
  104. /// 不需要返回按钮
  105. addBackBarButtonItem(true)
  106. }
  107. override func viewWillAppear(_ animated: Bool) {
  108. super.viewWillAppear(animated)
  109. let value = UIInterfaceOrientation.portrait.rawValue
  110. UIDevice.current.setValue(value, forKey: "orientation")
  111. }
  112. override func loadView() {
  113. super.loadView()
  114. /// 标题视图
  115. self.navigationItem.titleView = titleView
  116. /// titles
  117. dataSource.titles = roleTitles
  118. segmentedView.reloadData()
  119. /// 默认选中第一个
  120. segmentedView.defaultSelectedIndex = 0
  121. segmentedView.delegate?.segmentedView(segmentedView, didSelectedItemAt: 0)
  122. }
  123. /// onTitleItemPressed
  124. /// - Parameter sender: sender
  125. @IBAction func onTitleItemPressed(_ sender: UIButton) {
  126. /// 行情不做任何处理
  127. if roleTitles[segmentedView.selectedIndex] == "行情" { return }
  128. /// 账户切换
  129. let se = viewController("Main", "SelectTaAccountInfo") as! SelectTaAccountInfoViewController
  130. se.modalTransitionStyle = .crossDissolve
  131. se.modalPresentationStyle = .overFullScreen
  132. se.popBlock = { (_ takeInfo: Any?) in
  133. if let taAccountInfo = takeInfo as? MoTaAccountInfo {
  134. /// 切换账户
  135. self.titleButton.setAttributedTitle("\(taAccountInfo.accountname) (\(taAccountInfo.accountid))".withFont(.font_10).withTextColor(.white), for: .normal)
  136. switch self.roleTitles[self.segmentedView.selectedIndex] {
  137. case "持仓": /// 持仓
  138. self.position.tableView.triggerRefreshing()
  139. case "委托": /// 委托
  140. self.order.tableView.triggerRefreshing()
  141. case "成交": /// 成交
  142. self.trade.tableView.triggerRefreshing()
  143. case "资金": /// 资金
  144. if self.fund.isFundInfo {
  145. /// 显示资金信息
  146. self.refreshOrders()
  147. } else {
  148. /// 查询资金流水
  149. self.fund.queryAmountLog()
  150. }
  151. default: break
  152. }
  153. }
  154. }
  155. self.present(se, animated: true, completion: {})
  156. }
  157. /// 切换资金账户后重新去请求单据数据
  158. fileprivate func refreshOrders() {
  159. /// 异常
  160. guard let orderManager = MTP2BusinessCore.shared.orderManager else {
  161. return
  162. }
  163. /// group
  164. let group = DispatchGroup()
  165. group.enter()
  166. orderManager.queryTradePosition(tradeMode: nil) { isSuccess, er, objs in
  167. group.leave()
  168. }
  169. group.enter()
  170. orderManager.queryTradeOrderDetail(tradeMode: nil, orderStatus: nil, orderID: nil) { isSuccess, er, objs in
  171. group.leave()
  172. }
  173. group.enter()
  174. orderManager.queryTradeDetail(tradeMode: nil, tradeID: nil, orderID: nil, buildType: nil, tradeType: nil, goodsID: nil) { isSuccess, er, objs in
  175. group.leave()
  176. }
  177. group.enter()
  178. orderManager.queryErmcpTradePositions(marketID: nil) { isSuccess, er, objs in
  179. group.leave()
  180. }
  181. group.enter()
  182. orderManager.queryErmcpOrderDetails { isSuccess, er, objs in
  183. group.leave()
  184. }
  185. group.enter()
  186. orderManager.queryErmcpTradeDetails(goodsid: nil, buyOrSell: nil, orderID: nil) { isSuccess, er, objs in
  187. group.leave()
  188. }
  189. group.notify(queue: .main) {
  190. /// 显示资金信息
  191. self.fund.showTaAccountInfo()
  192. }
  193. }
  194. }
  195. // MARK: - JXSegmentedViewDelegate
  196. extension FuturesViewController: JXSegmentedViewDelegate {
  197. /// didSelectedItemAt
  198. /// - Parameters:
  199. /// - segmentedView: segmentedView
  200. /// - index: index
  201. func segmentedView(_ segmentedView: JXSegmentedView, didSelectedItemAt index: Int) {
  202. /// 移出所有视图
  203. if contentView.arrangedSubviews.count != 0 {
  204. contentView.arrangedSubviews.forEach { (view) in
  205. contentView.removeArrangedSubview(view)
  206. view.isHidden = true
  207. (view as? PageChangeDelegate)?.didHide()
  208. /// 回调父类方法
  209. view.superController()?.viewDidDisappear(true)
  210. }
  211. }
  212. switch roleTitles[index] {
  213. case "行情": /// 行情
  214. contentView.insertArrangedSubview(quote.view, at: 0)
  215. quote.view.isHidden = false
  216. case "持仓": /// 头寸
  217. contentView.insertArrangedSubview(position.view, at: 0)
  218. position.view.isHidden = false
  219. position.didShow()
  220. case "委托": /// 委托
  221. contentView.insertArrangedSubview(order.view, at: 0)
  222. order.view.isHidden = false
  223. order.didShow()
  224. case "成交": /// 成交
  225. contentView.insertArrangedSubview(trade.view, at: 0)
  226. trade.view.isHidden = false
  227. trade.didShow()
  228. default: /// 资金
  229. contentView.insertArrangedSubview(fund.view, at: 0)
  230. fund.view.isHidden = false
  231. }
  232. /// 隐藏
  233. arrow.isHidden = roleTitles[index] == "行情"
  234. titleButton.isHidden = roleTitles[index] == "行情"
  235. }
  236. }