PreTradeViewController.swift 7.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214
  1. //
  2. // PreTradeViewController.swift
  3. // MTP2_iOS
  4. //
  5. // Created by Handy_Cao on 2020/11/12.
  6. // Copyright © 2020 Muchinfo. All rights reserved.
  7. //
  8. import UIKit
  9. import WHToast
  10. import NVActivityIndicatorView
  11. /// 一级预售市场下单视图容器控制类
  12. class PreTradeViewController: BaseViewController {
  13. // MARK: - 属性列表
  14. /// 购买价格
  15. @IBOutlet weak var orderPrice: IBLabel!
  16. /// 购买数量
  17. @IBOutlet weak var orderQty: IBNumberEditText! {
  18. didSet {
  19. orderQty.currentIntNumbers = 0
  20. orderQty.minIntNum = 0
  21. orderQty.delegate = self
  22. }
  23. }
  24. /// 最大可购买数量
  25. @IBOutlet weak var maxQty: UILabel!
  26. /// 购买金额
  27. @IBOutlet weak var orderAmount: IBLabel!
  28. /// 余额
  29. @IBOutlet weak var blance: UILabel!
  30. /// 立即购买按钮
  31. @IBOutlet weak var trade: UIButton!
  32. /// 关闭按钮
  33. @IBOutlet weak var closed: IBButton!
  34. /// 商品数据
  35. override var takeInfo: Any? {
  36. didSet {
  37. /// 数据异常
  38. guard let obj = takeInfo as? MoGoodsInfo,
  39. let accountManager = MTP2BusinessCore.shared.accountManager,
  40. let taAccountInfo = accountManager.getCurrentTAAccountInfo() else {
  41. return
  42. }
  43. DispatchQueue.main.asyncAfter(deadline: DispatchTime.now()+0.5) {
  44. /// 委托价格
  45. self.orderPrice.text = obj.refprice.description
  46. /// 下单递增数量
  47. self.orderQty.incrementIntNum = obj.lotsize
  48. /// 期末余额
  49. let blance = taAccountInfo.currentBalance-(taAccountInfo.freezeMargin+taAccountInfo.otherFreezeMargin+taAccountInfo.freezeCharge)
  50. if obj.refprice == 0.0 || obj.agreeUnit == 0 {
  51. self.max = 0
  52. } else {
  53. /// 0为则不限制
  54. if obj.buymaxqty == 0 {
  55. self.max = 1000
  56. } else {
  57. self.max = obj.buymaxqty
  58. }
  59. }
  60. /// 余额
  61. self.blance.text = "可用资金:\(blance.toDownString(reserve: 2))"
  62. }
  63. }
  64. }
  65. /// 最大可购买数量
  66. var max: Int = 0 {
  67. didSet {
  68. /// 最大可购买数量
  69. orderQty.maxIntNum = max
  70. maxQty.text = "最大可购买数量:\(max)"
  71. }
  72. }
  73. /// 回调块
  74. var disBlock: ((_ orderId: String?, _ amount: Double, _ payOrder: MoMyPayOrders?) -> Void)?
  75. // MARK: - 生命周期
  76. override func viewDidLoad() {
  77. super.viewDidLoad()
  78. // Do any additional setup after loading the view.
  79. }
  80. // MARK: - 初始化
  81. /// UI界面初始化
  82. fileprivate func buildView() {
  83. /// addLoadingView
  84. self.addLoadingView()
  85. }
  86. // MARK: - 交互相关
  87. /// onButtonPressed
  88. /// - Parameter sender: sender
  89. @IBAction fileprivate func onButtonPressed(_ sender: IBButton) {
  90. switch sender {
  91. case closed: /// 关闭按钮
  92. self.dismiss(animated: true, completion: {})
  93. default: /// 立即购买
  94. doDelistingTrade()
  95. }
  96. }
  97. /// onTapGestureRecognizer
  98. /// - Parameter sender: sender
  99. @IBAction fileprivate func onTapGestureRecognizer(_ sender: UITapGestureRecognizer) {
  100. self.dismiss(animated: true, completion: {})
  101. }
  102. // MARK: - 接口请求
  103. /// 预售下单请求
  104. fileprivate func doDelistingTrade() {
  105. /// 数据异常
  106. guard check(),
  107. let obj = takeInfo as? MoGoodsInfo,
  108. let tradeManager = MTP2BusinessCore.shared.tradeManager,
  109. let orderManager = MTP2BusinessCore.shared.orderManager else {
  110. return
  111. }
  112. /// 构建交易参数
  113. let tradeParam = MoTradeParam()
  114. /// 买卖方向
  115. tradeParam.buyOrSell = .ORDERDIRECTION_BUY
  116. /// 数量
  117. tradeParam.orderQty = Double(orderQty.currentIntNumbers)
  118. /// 挂牌价格
  119. tradeParam.orderPrice = Double(orderPrice.text ?? "0.0") ?? 0.0
  120. /// 点选成交
  121. tradeParam.listingSelectType = .LISTINGSELECTTYPE_DELISTING
  122. /// 有效类型
  123. tradeParam.validType = .VALIDTYPE_DR
  124. /// 下单类型
  125. tradeParam.buildType = .BUILDTYPE_OPEN
  126. /// 时间有效类型
  127. tradeParam.timevalidType = .VALIDTYPE_YZ
  128. /// 市场编号
  129. tradeParam.marketID = obj.marketid
  130. /// 商品ID
  131. tradeParam.goodsID = obj.goodsid
  132. /// 开启Loading
  133. NVActivityIndicatorPresenter.sharedInstance.startAnimating(ActivityData(message: "订单生成中......", type: .ballClipRotate, color: UIColor.loding(), backgroundColor: .clear), NVActivityIndicatorView.DEFAULT_FADE_IN_ANIMATION)
  134. tradeManager.tradeCommision(tradeParam: tradeParam) { [weak self] (isSuccess, error, orderNo) in
  135. DispatchQueue.main.async {
  136. /// dismisss loding.....
  137. NVActivityIndicatorPresenter.sharedInstance.stopAnimating(NVActivityIndicatorView.DEFAULT_FADE_OUT_ANIMATION)
  138. if isSuccess {
  139. WHToast.showSuccess(withMessage: "订单生成成功", duration: 1.5, finishHandler: {
  140. orderManager.requestQueryMyPayOrders(orderNo, nil) { (isComplete, error, payOrders) in
  141. DispatchQueue.main.async {
  142. if isComplete {
  143. self?.dismiss(animated: true, completion: {
  144. if let block = self?.disBlock {
  145. block(orderNo, Double(self?.orderAmount.text ?? "0.0") ?? 0.0, payOrders?.first)
  146. }
  147. })
  148. } else {
  149. WHToast.showError(withMessage: "待成交单据查询失败,请前往单据->待付款查看", duration: 1.5, finishHandler: {})
  150. }
  151. }
  152. }
  153. })
  154. } else {
  155. WHToast.showError(withMessage: "请求失败,\(ErrorUtils.getDesc(errorCode: error?.retCode ?? -99999) ?? "未知错误")", duration: 1.5, finishHandler: {})
  156. }
  157. }
  158. }
  159. }
  160. /// 合规性校验
  161. /// - Returns: 是否合规
  162. fileprivate func check() -> Bool {
  163. /// 请输入委托数量
  164. if orderQty.currentIntNumbers == 0 {
  165. WHToast.showError(withMessage: "请输入委托数量", duration: 1.5, finishHandler: {})
  166. return false
  167. }
  168. return true
  169. }
  170. /*
  171. // MARK: - Navigation
  172. // In a storyboard-based application, you will often want to do a little preparation before navigation
  173. override func prepare(for segue: UIStoryboardSegue, sender: Any?) {
  174. // Get the new view controller using segue.destination.
  175. // Pass the selected object to the new view controller.
  176. }
  177. */
  178. }
  179. extension PreTradeViewController: IBNumberEditTextDelegate {
  180. func numberInputViewDidEndEditing(_ numberInputView: IBNumberEditText) {
  181. }
  182. func IBNumberEditText(textField: UITextField) {
  183. }
  184. func numberInputView(_ numberInputView: IBNumberEditText, _ buttonType: NumberInputViewButtonType) {
  185. guard let obj = takeInfo as? MoGoodsInfo else { return }
  186. /// 购买金额
  187. orderAmount.text = "\(obj.agreeUnit*Double(numberInputView.currentIntNumbers)*obj.refprice)"
  188. }
  189. }