// // PreTradeViewController.swift // MTP2_iOS // // Created by Handy_Cao on 2020/11/12. // Copyright © 2020 Muchinfo. All rights reserved. // import UIKit import WHToast import NVActivityIndicatorView /// 一级预售市场下单视图容器控制类 class PreTradeViewController: BaseViewController { // MARK: - 属性列表 /// 购买价格 @IBOutlet weak var orderPrice: IBLabel! /// 购买数量 @IBOutlet weak var orderQty: IBNumberEditText! { didSet { orderQty.currentIntNumbers = 0 orderQty.minIntNum = 0 orderQty.delegate = self } } /// 最大可购买数量 @IBOutlet weak var maxQty: UILabel! /// 购买金额 @IBOutlet weak var orderAmount: IBLabel! /// 余额 @IBOutlet weak var blance: UILabel! /// 立即购买按钮 @IBOutlet weak var trade: UIButton! /// 关闭按钮 @IBOutlet weak var closed: IBButton! /// 商品数据 override var takeInfo: Any? { didSet { /// 数据异常 guard let obj = takeInfo as? MoGoodsInfo, let accountManager = MTP2BusinessCore.shared.accountManager, let taAccountInfo = accountManager.getCurrentTAAccountInfo() else { return } DispatchQueue.main.asyncAfter(deadline: DispatchTime.now()+0.5) { /// 委托价格 self.orderPrice.text = obj.refprice.description /// 下单递增数量 self.orderQty.incrementIntNum = obj.lotsize /// 期末余额 let blance = taAccountInfo.currentBalance-(taAccountInfo.freezeMargin+taAccountInfo.otherFreezeMargin+taAccountInfo.freezeCharge) if obj.refprice == 0.0 || obj.agreeUnit == 0 { self.max = 0 } else { /// 0为则不限制 if obj.buymaxqty == 0 { self.max = 1000 } else { self.max = obj.buymaxqty } } /// 余额 self.blance.text = "可用资金:\(blance.toDownString(reserve: 2))" } } } /// 最大可购买数量 var max: Int = 0 { didSet { /// 最大可购买数量 orderQty.maxIntNum = max maxQty.text = "最大可购买数量:\(max)" } } /// 回调块 var disBlock: ((_ orderId: String?, _ amount: Double, _ payOrder: MoMyPayOrders?) -> Void)? // MARK: - 生命周期 override func viewDidLoad() { super.viewDidLoad() // Do any additional setup after loading the view. } // MARK: - 初始化 /// UI界面初始化 fileprivate func buildView() { /// addLoadingView self.addLoadingView() } // MARK: - 交互相关 /// onButtonPressed /// - Parameter sender: sender @IBAction fileprivate func onButtonPressed(_ sender: IBButton) { switch sender { case closed: /// 关闭按钮 self.dismiss(animated: true, completion: {}) default: /// 立即购买 doDelistingTrade() } } /// onTapGestureRecognizer /// - Parameter sender: sender @IBAction fileprivate func onTapGestureRecognizer(_ sender: UITapGestureRecognizer) { self.dismiss(animated: true, completion: {}) } // MARK: - 接口请求 /// 预售下单请求 fileprivate func doDelistingTrade() { /// 数据异常 guard check(), let obj = takeInfo as? MoGoodsInfo, let tradeManager = MTP2BusinessCore.shared.tradeManager, let orderManager = MTP2BusinessCore.shared.orderManager else { return } /// 构建交易参数 let tradeParam = MoTradeParam() /// 买卖方向 tradeParam.buyOrSell = .ORDERDIRECTION_BUY /// 数量 tradeParam.orderQty = Double(orderQty.currentIntNumbers) /// 挂牌价格 tradeParam.orderPrice = Double(orderPrice.text ?? "0.0") ?? 0.0 /// 点选成交 tradeParam.listingSelectType = .LISTINGSELECTTYPE_DELISTING /// 有效类型 tradeParam.validType = .VALIDTYPE_DR /// 下单类型 tradeParam.buildType = .BUILDTYPE_OPEN /// 时间有效类型 tradeParam.timevalidType = .VALIDTYPE_YZ /// 市场编号 tradeParam.marketID = obj.marketid /// 商品ID tradeParam.goodsID = obj.goodsid /// 开启Loading NVActivityIndicatorPresenter.sharedInstance.startAnimating(ActivityData(message: "订单生成中......".localized, type: .ballClipRotate, color: UIColor.loding(), backgroundColor: .clear), NVActivityIndicatorView.DEFAULT_FADE_IN_ANIMATION) tradeManager.tradeCommision(tradeParam: tradeParam) { [weak self] (isSuccess, error, orderNo) in DispatchQueue.main.async { /// dismisss loding..... NVActivityIndicatorPresenter.sharedInstance.stopAnimating(NVActivityIndicatorView.DEFAULT_FADE_OUT_ANIMATION) if isSuccess { WHToast.showSuccess(withMessage: "订单生成成功", duration: 1.5, finishHandler: { orderManager.requestQueryMyPayOrders(orderNo, nil) { (isComplete, error, payOrders) in DispatchQueue.main.async { if isComplete { self?.dismiss(animated: true, completion: { if let block = self?.disBlock { block(orderNo, Double(self?.orderAmount.text ?? "0.0") ?? 0.0, payOrders?.first) } }) } else { WHToast.showError(withMessage: "待成交单据查询失败,请前往单据->待付款查看", duration: 1.5, finishHandler: {}) } } } }) } else { WHToast.showError(withMessage: "请求失败,\(ErrorUtils.getDesc(errorCode: error?.retCode ?? -99999) ?? "未知错误")", duration: 1.5, finishHandler: {}) } } } } /// 合规性校验 /// - Returns: 是否合规 fileprivate func check() -> Bool { /// 请输入委托数量 if orderQty.currentIntNumbers == 0 { WHToast.showError(withMessage: "请输入委托数量", duration: 1.5, finishHandler: {}) return false } return true } /* // 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. } */ } extension PreTradeViewController: IBNumberEditTextDelegate { func numberInputViewDidEndEditing(_ numberInputView: IBNumberEditText) { } func IBNumberEditText(textField: UITextField) { } func numberInputView(_ numberInputView: IBNumberEditText, _ buttonType: NumberInputViewButtonType) { guard let obj = takeInfo as? MoGoodsInfo else { return } /// 购买金额 orderAmount.text = "\(obj.agreeUnit*Double(numberInputView.currentIntNumbers)*obj.refprice)" } }