HisExposuresViewController.swift 6.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177
  1. //
  2. // HisExposuresViewController.swift
  3. // MTP2_iOS
  4. //
  5. // Created by Muchinfo on 2021/2/7.
  6. // Copyright © 2021 Muchinfo. All rights reserved.
  7. //
  8. import UIKit
  9. import WHToast
  10. import AAInfographics
  11. /// 历史敞口数据信息视图容器控制类
  12. class HisExposuresViewController: BaseViewController {
  13. // MARK: - 属性列表
  14. /// 属性列表
  15. @IBOutlet weak var tableView: UITableView!
  16. /// 历史敞口数据
  17. var hisExposures: [MoHisExposure] = [] {
  18. didSet {
  19. /// 刷新列表数据
  20. tableView.reloadData()
  21. /// 无数据按钮
  22. noDataButton.isHidden = hisExposures.count != 0
  23. }
  24. }
  25. /// CellIdentifier
  26. let CellIdentifier = "HisExposures_Cell"
  27. // MARK: - 生命周期
  28. override func viewDidLoad() {
  29. super.viewDidLoad()
  30. // Do any additional setup after loading the view.
  31. /// loding
  32. addLoadingView()
  33. }
  34. override func viewWillAppear(_ animated: Bool) {
  35. super.viewWillAppear(animated)
  36. let value = UIInterfaceOrientation.portrait.rawValue
  37. UIDevice.current.setValue(value, forKey: "orientation")
  38. }
  39. override func didMove(toParent parent: UIViewController?) {
  40. super.didMove(toParent: parent)
  41. if parent != nil {
  42. /// 获取数据
  43. requestHisExposures()
  44. }
  45. }
  46. // MARK: - 接口请求
  47. fileprivate func requestHisExposures() {
  48. /// 异常
  49. guard let spotManager = MTP2BusinessCore.shared.spotManager else { return }
  50. /// startAnimating
  51. _anim?.startAnimating()
  52. /// 查询历史敞口数据信息
  53. spotManager.queryHisExposure(lastNum: -1) { (isSuccess, er, objs) in
  54. DispatchQueue.main.async {
  55. /// stopAnimating
  56. self._anim?.stopAnimating()
  57. /// 查询失败
  58. if !isSuccess {
  59. self.hisExposures = []
  60. WHToast.showMessage("数据查询失败,原因:\(er?.retMsg ?? "查询超时")", duration: ToastTimer, finishHandler: {})
  61. return
  62. }
  63. self.hisExposures = objs ?? []
  64. }
  65. }
  66. }
  67. }
  68. // MARK: - UITableViewDelegate, UITableViewDataSource
  69. extension HisExposuresViewController: UITableViewDelegate, UITableViewDataSource {
  70. func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
  71. return hisExposures.count
  72. }
  73. func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
  74. let cell = tableView.dequeueReusableCell(withIdentifier: CellIdentifier, for: indexPath) as! HisExposuresCell
  75. cell.model = hisExposures[indexPath.row]
  76. return cell
  77. }
  78. func tableView(_ tableView: UITableView, heightForRowAt indexPath: IndexPath) -> CGFloat {
  79. return 220.0
  80. }
  81. }
  82. // MARK: - HisExposuresCell
  83. class HisExposuresCell: BaseTableViewCell<MoHisExposure> {
  84. /// 套保品种
  85. @IBOutlet weak var middlegoodsname: UILabel!
  86. /// 图表数据
  87. @IBOutlet weak var lineChartView: UIView! {
  88. didSet {
  89. lineChartView.addSubview(chart)
  90. }
  91. }
  92. /// 折现图
  93. lazy var chart: AAChartView = {
  94. $0.isSeriesHidden = true
  95. $0.scrollEnabled = true
  96. $0.frame = CGRect(origin: CGPoint.zero, size: lineChartView.frame.size)
  97. return $0
  98. }(AAChartView())
  99. override var model: MoHisExposure? {
  100. didSet {
  101. /// 异常
  102. guard let obj = model else { return }
  103. /// 套保名称和单位
  104. middlegoodsname.text = "\(obj.middlegoodsname) (\(obj.enumdicname))"
  105. let dates: [String] = obj.data.map { (data) -> String in
  106. return data.reckondate.count > 7 ? (data.reckondate.substing(range: 2..<8) ?? "--") : data.reckondate
  107. }
  108. /// 总敞口
  109. let totalexposures: [Double] = obj.data.map { (data) -> Double in
  110. return (Double(data.totalexposure.toUpString()) ?? 0.0)
  111. }
  112. /// 应套保敞口
  113. let needhedgeexposoures: [Double] = obj.data.map { (data) -> Double in
  114. return (Double(data.needhedgeexposoure.toUpString()) ?? 0.0)
  115. }
  116. /// totalMax
  117. let totalMax = totalexposures.sorted { a, b in
  118. return a>b
  119. }.first ?? 0.0
  120. /// totalMin
  121. let totalMin = totalexposures.sorted { a, b in
  122. return a<b
  123. }.first ?? 0.0
  124. /// needMax
  125. let needMax = needhedgeexposoures.sorted { a, b in
  126. return a>b
  127. }.first ?? 0.0
  128. /// needMin
  129. let needMin = needhedgeexposoures.sorted { a, b in
  130. return a<b
  131. }.first ?? 0.0
  132. let chartModel = AAChartModel().chartType(.spline) /// 图表类型
  133. .animationType(.bounce) /// 动画类型
  134. .zoomType(.xy) /// 手势缩放
  135. .dataLabelsEnabled(false)
  136. .touchEventEnabled(false)
  137. .markerRadius(0.5)
  138. .yAxisMin(Swift.min(totalMin, needMin))
  139. .yAxisMax(Swift.max(totalMax, needMax))
  140. .xAxisGridLineWidth(1.0)
  141. .yAxisGridLineWidth(1.0)
  142. .legendEnabled(false) /// 是否启用图表的图例(图表底部的可点击的小圆点)
  143. .animationDuration(2) /// 动画时间
  144. .inverted(false) /// 是否翻转图形
  145. .tooltipValueSuffix("\(obj.enumdicname)") /// 浮动提示框单位后缀
  146. .colorsTheme(["#20CCF1","#FF6E47"]) /// 主题颜色数组
  147. .categories(dates)
  148. .markerSymbol(.circle)
  149. .markerSymbolStyle(.normal)
  150. .series([AASeriesElement().name("总敞口:").lineWidth(1.0).data(totalexposures),
  151. AASeriesElement().name("应套保敞口:").lineWidth(1.0).data(needhedgeexposoures)])
  152. chart.aa_drawChartWithChartModel(chartModel)
  153. }
  154. }
  155. }