MyUnusedViewController.swift 34 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856
  1. //
  2. // MyUnusedViewController.swift
  3. // MTP2_iOS
  4. //
  5. // Created by Handy_Cao on 2020/10/30.
  6. // Copyright © 2020 Muchinfo. All rights reserved.
  7. //
  8. import UIKit
  9. import JXSegmentedView
  10. import WHToast
  11. import NVActivityIndicatorView
  12. import SwiftDate
  13. /// 我的闲置视图容器控制类
  14. class MyUnusedViewController: BaseViewController {
  15. // MARK: - 属性列表
  16. /// 商品切换按钮
  17. @IBOutlet weak var goods: UIButton!
  18. /// 发布价格
  19. @IBOutlet weak var orderPrice: IBNumberEditText! {
  20. didSet {
  21. orderPrice.valueType = 2
  22. orderPrice.currentIntNumbers = 0
  23. orderPrice.delegate = self
  24. }
  25. }
  26. /// 价格范围
  27. @IBOutlet weak var priceRange: UILabel!
  28. /// 发布数量
  29. @IBOutlet weak var orderQty: IBNumberEditText! {
  30. didSet {
  31. orderQty.valueType = 1
  32. orderQty.delegate = self
  33. }
  34. }
  35. /// 可用数量
  36. @IBOutlet weak var enableQty: UILabel!
  37. /// 提货按钮
  38. @IBOutlet weak var submit: UIButton!
  39. /// 刷新按钮
  40. @IBOutlet weak var refresh: UIButton!
  41. /// 有效期按钮
  42. @IBOutlet weak var valid: UIButton! {
  43. didSet {
  44. valid.isUserInteractionEnabled = false
  45. }
  46. }
  47. /// 有效期
  48. @IBOutlet weak var validStackView: UIStackView!
  49. /// 分段控制器
  50. @IBOutlet weak var segmentedView: JXSegmentedView! {
  51. didSet {
  52. segmentedView.delegate = self
  53. segmentedView.dataSource = dataSource
  54. segmentedView.indicators = [self.indicator]
  55. }
  56. }
  57. /// segmentedDataSource
  58. let dataSource: JXSegmentedTitleDataSource = {
  59. var items: [String] = ["待成交", "待收款", "已完成"]
  60. $0.titles = items
  61. $0.titleNormalColor = UIColorFromHex(rgbValue: 0x333333)
  62. $0.titleNormalFont = .font_14
  63. $0.titleSelectedColor = UIColorFromHex(rgbValue: 0x60a1e3)
  64. $0.titleSelectedFont = .font_14
  65. $0.isTitleColorGradientEnabled = true
  66. return $0
  67. } (JXSegmentedTitleDataSource())
  68. /// 买家委托单列表
  69. @IBOutlet weak var orderTableView: UITableView! {
  70. didSet {
  71. orderDelegate = OrderDelegate(self)
  72. orderTableView.delegate = orderDelegate
  73. orderTableView.dataSource = orderDelegate
  74. }
  75. }
  76. /// 单据视图
  77. @IBOutlet weak var orderView: UIView!
  78. /// orderTableViewHeightConstraint
  79. @IBOutlet weak var orderTableViewHeightConstraint: NSLayoutConstraint! {
  80. didSet {
  81. orderTableViewHeightConstraint.constant = 0.0
  82. }
  83. }
  84. /// 列表
  85. @IBOutlet weak var tableView: UITableView! {
  86. didSet {
  87. myUnusedDelegate = MyUnusedDelegate(self)
  88. tableView.delegate = myUnusedDelegate
  89. tableView.dataSource = myUnusedDelegate
  90. }
  91. }
  92. /// tableViewHeightConstraint
  93. @IBOutlet weak var tableViewHeightConstraint: NSLayoutConstraint! {
  94. didSet {
  95. tableViewHeightConstraint.constant = 0.0
  96. }
  97. }
  98. /// 提示信息
  99. @IBOutlet weak var tips: UILabel!
  100. /// OrderCellIdentifier
  101. let OrderCellIdentifier = "Trade_Cell"
  102. /// CellIdentifier
  103. let CellIdentifier = "SellMyDetail_Cell"
  104. /// orderDelegate
  105. fileprivate var orderDelegate: OrderDelegate?
  106. /// myUnusedDelegate
  107. fileprivate var myUnusedDelegate: MyUnusedDelegate?
  108. /// 我的商品数据
  109. var myGoods: MoHsbyMyGoods? {
  110. didSet {
  111. /// 异常
  112. guard let obj = myGoods,
  113. let goodsManager = MTP2BusinessCore.shared.goodsManager,
  114. let last = goodsManager.goodsInfos.first(where: {$0.goodsid == obj.goodsid})?.getLast() else { return }
  115. /// 显示商品信息
  116. showGoodsInfo(obj)
  117. /// 行情订阅
  118. self.subscriptQuote()
  119. /// 挂牌点选
  120. if obj.trademode == .TRADEMODE_LISTING_SELECT {
  121. /// 侦听行情推送广播
  122. MTP2BusinessCore.shared.broadcastManager?.addBroadcastListener(owner: self, action: .ReceiveTradeQuote, block: { [unowned self] (notify) in
  123. DispatchQueue.main.async {
  124. /// 异常情况
  125. guard let goodsQuoteInfo = notify.object as? [(goodsHqCode: String, exchHqCode: String)],
  126. goodsQuoteInfo.count != 0 else { return }
  127. /// 更新商品行情信息
  128. goodsQuoteInfo.forEach {
  129. if $0.goodsHqCode == obj.goodscode {
  130. if last != self.orderPrice.currentDoubleNumbers {
  131. /// 查询数据
  132. self.requestQueryHsbyGoodsOrderDetails(self.orderPrice.currentDoubleNumbers.toString(reserve: 2))
  133. }
  134. }
  135. }
  136. }
  137. })
  138. }
  139. /// 隐藏
  140. orderView.isHidden = obj.trademode == .TRADEMODE_TRADEMODE_HSBY_SHOP
  141. /// 挂牌点选默认当日有效
  142. valid.isUserInteractionEnabled = obj.trademode != .TRADEMODE_LISTING_SELECT
  143. /// 小数位
  144. orderPrice.decimalPlace = obj.decimalplace
  145. }
  146. }
  147. /// 委托单数据
  148. var orderDetails: [MoHsbyGoodsOrderDetail] = [] {
  149. didSet {
  150. /// 刷新列表数据
  151. orderTableView.reloadData()
  152. /// 重新设置高度约束
  153. orderTableViewHeightConstraint.constant = CGFloat(orderDetails.count)*50.0
  154. /// 是否隐藏提示
  155. tips.isHidden = orderDetails.count != 0
  156. }
  157. }
  158. /// 我的闲置单据信息
  159. var sellMyDetails: [MoHsbySellMyDetail] = [] {
  160. didSet {
  161. /// 刷新列表数据
  162. tableView.reloadData()
  163. switch self.segmentedView.selectedIndex {
  164. case 0:
  165. /// 重新设置高度约束
  166. tableViewHeightConstraint.constant = CGFloat(sellMyDetails.count)*200.0
  167. default:
  168. /// 重新设置高度约束
  169. tableViewHeightConstraint.constant = CGFloat(sellMyDetails.count)*180.0
  170. }
  171. }
  172. }
  173. /// 委托数量
  174. var qty: Int = 0 {
  175. didSet { orderQty.currentIntNumbers = qty }
  176. }
  177. /// 有效期
  178. var validType: EValidType = .VALIDTYPE_DR {
  179. didSet {
  180. switch validType {
  181. case .VALIDTYPE_DR:
  182. self.valid.setTitle("当日有效", for: .normal)
  183. case .VALIDTYPE_YZ:
  184. self.valid.setTitle("一直有效", for: .normal)
  185. default:
  186. self.valid.setTitle("3天有效", for: .normal)
  187. }
  188. }
  189. }
  190. /// 交易日查询
  191. var run: MoMarketRun? {
  192. get {
  193. /// 结算计划
  194. return MTP2BusinessCore.shared.goodsManager?.marketRun
  195. }
  196. }
  197. // MARK: - 生命周期
  198. override func viewDidLoad() {
  199. super.viewDidLoad()
  200. // Do any additional setup after loading the view.e
  201. /// 数据初始化
  202. initData()
  203. }
  204. override func viewWillAppear(_ animated: Bool) {
  205. super.viewWillAppear(animated)
  206. /// 隐藏导航栏
  207. self.navigationController?.setNavigationBarHidden(false, animated: true)
  208. }
  209. deinit {
  210. /// 移出行情广播
  211. MTP2BusinessCore.shared.broadcastManager?.removeBroadcastListener(owner: self, forName: .ReceiveTradeQuote)
  212. }
  213. // MARK: - 初始化
  214. /// 数据初始化
  215. fileprivate func initData() {
  216. /// 默认查询我的闲置
  217. self.requestQueryHsbySellMyDetails(0)
  218. }
  219. // MARK: - 交互相关
  220. /// 按钮点击响应事件
  221. /// - Parameter sender: sender
  222. @IBAction fileprivate func onButtonPressed(_ sender: UIControl) {
  223. switch sender {
  224. case goods: /// 商品切换
  225. self.performSegue(withIdentifier: "ShowMyGoods", sender: nil)
  226. case submit:
  227. let isListing = !orderTableView.visibleCells.contains { (cell) -> Bool in
  228. return (cell as? TradeCell)?.isCheck ?? false
  229. }
  230. /// 有对手委托单得时候必须选一个
  231. if isListing, orderDetails.count != 0 {
  232. WHToast.showError(withMessage: "请选择买家", duration: 1.5, finishHandler: {})
  233. return
  234. }
  235. if isListing {
  236. /// 买挂牌
  237. doListingTrade()
  238. } else {
  239. /// 买摘牌
  240. doDeListingTrade()
  241. }
  242. case refresh: /// 刷新按钮
  243. let orderPrice = self.orderPrice.currentDoubleNumbers
  244. /// 更新单据
  245. self.requestQueryHsbyGoodsOrderDetails(orderPrice.toString(reserve: 2))
  246. case valid: /// 有效期
  247. let titles: [String] = ["当日有效", "3天有效", "一直有效"]
  248. self.showActionSheetController(title: "有效期", message: nil, cancelTitle: "取消", cancelBlock: {}, actionSheetTitles: titles) { (index, action) in
  249. switch index {
  250. case 0: /// 档期有效
  251. self.validType = EValidType.VALIDTYPE_DR
  252. case 1: /// 3天有效
  253. self.validType = EValidType.VALIDTYPE_ZDRQ
  254. if self.run == nil {
  255. /// 查询市场结算计划
  256. self.queryMarketRun()
  257. }
  258. default: /// 一直有效
  259. self.validType = EValidType.VALIDTYPE_YZ
  260. }
  261. }
  262. default:
  263. break
  264. }
  265. }
  266. // MARK: - 数据显示
  267. /// 显示商品数据信息
  268. /// - Parameter obj: obj
  269. func showGoodsInfo(_ myGoods: MoHsbyMyGoods?) {
  270. /// 异常
  271. guard let obj = myGoods else { return }
  272. if obj.trademode == .TRADEMODE_LISTING_SELECT {
  273. /// 获取商品
  274. guard let goodsInfo = MTP2BusinessCore.shared.goodsManager?.goodsInfos.first(where: {obj.goodsid == $0.goodsid}) else { return }
  275. /// 默认为一直有效
  276. self.validType = .VALIDTYPE_DR
  277. /// 步进值
  278. self.orderPrice.incrementDoubleNum = obj.stepvalue
  279. /// 小数位
  280. self.orderPrice.decimalPlace = obj.decimalplace
  281. /// 最小值
  282. self.orderPrice.minDoubleNum = obj.limitdown
  283. /// 最大值
  284. self.orderPrice.maxDoubleNum = obj.limitup
  285. /// 委托价格
  286. self.orderPrice.currentDoubleNumbers = goodsInfo.getLast()
  287. /// 最大可发布数量
  288. self.orderQty.maxIntNum = obj.buymaxqty == 0 ? obj.enableqty : min(obj.enableqty, obj.buymaxqty)
  289. /// 最小数量
  290. self.orderQty.minIntNum = obj.lotsize
  291. /// 当前数量
  292. self.orderQty.currentIntNumbers = obj.lotsize
  293. /// 下单递增数量
  294. self.orderQty.incrementIntNum = obj.lotsize
  295. /// 价格范围
  296. self.priceRange.text = "\(obj.limitdown) <= 价格范围 <= \(obj.limitup)"
  297. /// 可发不数量
  298. self.enableQty.text = "可发布数量 \(obj.enableqty)"
  299. /// 不允许操作
  300. self.orderPrice.isUserInteractionEnabled = true
  301. } else {
  302. /// 最大可发布数量
  303. self.orderQty.maxIntNum = obj.enableqty
  304. /// 不允许操作
  305. self.orderPrice.isUserInteractionEnabled = false
  306. /// 最小数量
  307. self.orderQty.minIntNum = obj.lotsize
  308. /// 当前数量
  309. self.orderQty.currentIntNumbers = obj.lotsize
  310. /// 下单递增数量
  311. self.orderQty.incrementIntNum = obj.lotsize
  312. /// 默认为一直有效
  313. self.validType = .VALIDTYPE_YZ
  314. /// 小数位
  315. self.orderPrice.decimalPlace = obj.decimalplace
  316. /// 步进值
  317. self.orderPrice.incrementDoubleNum = obj.stepvalue
  318. ///  设置大小范围
  319. self.orderPrice.minDoubleNum = Double.zero
  320. /// 最大值
  321. self.orderPrice.maxDoubleNum = 10000000000.00
  322. /// 委托价格
  323. self.orderPrice.currentDoubleNumbers = obj.goodsprice
  324. }
  325. /// 商品名称
  326. self.goods.setTitle("\(obj.goodscode) \(obj.goodsname) ▸", for: .normal)
  327. /// 可发不数量
  328. self.enableQty.text = "可发布数量 \(obj.enableqty)"
  329. /// 隐藏
  330. self.priceRange.isHidden = obj.trademode != .TRADEMODE_LISTING_SELECT
  331. }
  332. // MARK: - 接口请求
  333. /// 买挂牌提交请求方法
  334. fileprivate func doListingTrade() {
  335. /// 异常
  336. guard let obj = myGoods,
  337. let tradeManager = MTP2BusinessCore.shared.tradeManager,
  338. check() else { return }
  339. /// 构建交易参数
  340. let tradeParam = MoTradeParam()
  341. /// 买卖方向
  342. tradeParam.buyOrSell = .ORDERDIRECTION_SELL
  343. /// 数量
  344. tradeParam.orderQty = Double(orderQty.currentIntNumbers)
  345. /// 挂牌价格
  346. tradeParam.orderPrice = orderPrice.currentDoubleNumbers
  347. /// 挂牌点选类型
  348. tradeParam.delistingType = .DELISTINGTYPE_SELECTED
  349. /// 点选成交
  350. tradeParam.listingSelectType = .LISTINGSELECTTYPE_LISTING
  351. /// 有效类型
  352. tradeParam.validType = self.validType
  353. /// 下单类型
  354. tradeParam.buildType = .BUILDTYPE_CLOSE
  355. /// 时间有效类型
  356. tradeParam.timevalidType = self.validType
  357. /// 市场编号
  358. tradeParam.marketID = obj.marketid
  359. /// 商品ID
  360. tradeParam.goodsID = obj.goodsid
  361. /// 指定时间
  362. if tradeParam.validType == .VALIDTYPE_ZDRQ,
  363. let tradeDate = self.run?.tradedate {
  364. tradeParam.validTime = ((tradeDate.toDate(formatter: "yyyyMMdd") ?? Date())+3.days).getString(formatter: "yyyy-MM-dd 00:00:00")
  365. }
  366. /// 开启Loading
  367. NVActivityIndicatorPresenter.sharedInstance.startAnimating(ActivityData(message: "发布中......", type: .ballClipRotate, color: UIColor.loding(), backgroundColor: .clear), NVActivityIndicatorView.DEFAULT_FADE_IN_ANIMATION)
  368. /// 发送请求
  369. tradeManager.tradeCommision(tradeParam: tradeParam) { [weak self] (isSuccess, error, orderNo) in
  370. DispatchQueue.main.async {
  371. /// dismisss loding.....
  372. NVActivityIndicatorPresenter.sharedInstance.stopAnimating(NVActivityIndicatorView.DEFAULT_FADE_OUT_ANIMATION)
  373. if isSuccess {
  374. /// 返回上次视图
  375. WHToast.showSuccess(withMessage: "发布请求成功", duration: 1.5) {
  376. self?.navigationController?.popViewController(animated: true)
  377. }
  378. } else {
  379. WHToast.showError(withMessage: "发布请求失败,\(ErrorUtils.getDesc(errorCode: error?.retCode ?? -99999) ?? "未知错误")", duration: 1.5, finishHandler: {})
  380. }
  381. }
  382. }
  383. }
  384. /// 买摘牌提交请求方法
  385. fileprivate func doDeListingTrade() {
  386. /// 异常
  387. guard let obj = myGoods,
  388. let tradeManager = MTP2BusinessCore.shared.tradeManager,
  389. check() else { return }
  390. /// 构建交易参数
  391. let tradeParam = MoTradeParam()
  392. /// 买卖方向
  393. tradeParam.buyOrSell = .ORDERDIRECTION_SELL
  394. /// 挂牌点选类型
  395. tradeParam.delistingType = .DELISTINGTYPE_SELECTED
  396. /// 点选成交
  397. tradeParam.listingSelectType = .LISTINGSELECTTYPE_DELISTING
  398. /// 有效类型
  399. tradeParam.validType = self.validType
  400. /// 下单类型
  401. tradeParam.buildType = .BUILDTYPE_CLOSE
  402. /// 时间有效类型
  403. tradeParam.timevalidType = self.validType
  404. /// 市场编号
  405. tradeParam.marketID = obj.marketid
  406. /// 商品ID
  407. tradeParam.goodsID = obj.goodsid
  408. /// 指定时间
  409. if tradeParam.validType == .VALIDTYPE_ZDRQ,
  410. let tradeDate = self.run?.tradedate {
  411. tradeParam.validTime = ((tradeDate.toDate(formatter: "yyyyMMdd") ?? Date())+3.days).getString(formatter: "yyyy-MM-dd 00:00:00")
  412. }
  413. /// 开启Loading
  414. NVActivityIndicatorPresenter.sharedInstance.startAnimating(ActivityData(message: "发布中......", type: .ballClipRotate, color: UIColor.loding(), backgroundColor: .clear), NVActivityIndicatorView.DEFAULT_FADE_IN_ANIMATION)
  415. let group = DispatchGroup()
  416. /// 数据构建
  417. for cell in orderTableView.visibleCells {
  418. if let cell = cell as? TradeCell, cell.isCheck {
  419. /// 挂牌价格
  420. tradeParam.orderPrice = cell.model?.orderprice ?? 0.0
  421. /// 关联单号
  422. tradeParam.relatedID = UInt64(cell.model?.orderid ?? "0") ?? 0
  423. /// 数量
  424. tradeParam.orderQty = Double(cell.qtyField.currentIntNumbers)
  425. group.enter()
  426. tradeManager.tradeCommision(tradeParam: tradeParam) { (isSuccess, error, orderNo) in
  427. group.leave()
  428. }
  429. }
  430. }
  431. group.notify(queue: DispatchQueue.main) {
  432. /// dismisss loding.....
  433. NVActivityIndicatorPresenter.sharedInstance.stopAnimating(NVActivityIndicatorView.DEFAULT_FADE_OUT_ANIMATION)
  434. WHToast.showSuccess(withMessage: "委托成功,请刷新后查看!", duration: 1.5, finishHandler: {
  435. self.navigationController?.popViewController(animated: true)
  436. })
  437. }
  438. }
  439. /// 查询商品对应委托单的数据信息
  440. /// - Parameter price: 价格
  441. fileprivate func requestQueryHsbyGoodsOrderDetails(_ price: String?) {
  442. /// 异常
  443. guard let orderManager = MTP2BusinessCore.shared.orderManager,
  444. let obj = myGoods else { return }
  445. /// startAnimating
  446. self._anim?.startAnimating()
  447. /// 发送请求
  448. orderManager.requestQueryHsbyGoodsOrderDetails(obj.goodsid,
  449. buyOrSell: .ORDERDIRECTION_BUY,
  450. price,
  451. 3) { (isComplete, error, orderDetails) in
  452. DispatchQueue.main.async {
  453. /// stopAnimating
  454. self._anim?.stopAnimating()
  455. if isComplete {
  456. /// 更新数据
  457. self.orderDetails = orderDetails ?? []
  458. } else {
  459. self.orderDetails = []
  460. WHToast.showError(withMessage: "数据更新失败,原因:\(error?.retMsg ?? "未知错误")", duration: 1.5, finishHandler: {})
  461. }
  462. }
  463. }
  464. }
  465. /// 查询交易日时间
  466. fileprivate func queryMarketRun() {
  467. /// 异常
  468. guard let goodsManger = MTP2BusinessCore.shared.goodsManager,
  469. let obj = myGoods else { return }
  470. /// startAnimating
  471. self._anim?.startAnimating()
  472. /// 获取交易日时间
  473. goodsManger.requestQueryMarketRun(obj.marketid) { (isComplete, error, marketRun) in }
  474. }
  475. /// 查询"我的闲置"单据信息
  476. /// - Parameter orderType: 单据类型:0 - 已发布(默认), 1 - 已完成
  477. fileprivate func requestQueryHsbySellMyDetails(_ orderType: Int) {
  478. /// 异常
  479. guard let orderManager = MTP2BusinessCore.shared.orderManager else { return }
  480. /// startAnimating
  481. self._anim?.startAnimating()
  482. /// 发送请求
  483. orderManager.requestQueryHsbySellMyDetails(orderType, callback: { (isComplete, error, sellMyDetails) in
  484. DispatchQueue.main.async {
  485. /// stopAnimating
  486. self._anim?.stopAnimating()
  487. if isComplete {
  488. /// 更新数据
  489. self.sellMyDetails = sellMyDetails ?? []
  490. } else {
  491. self.sellMyDetails = []
  492. WHToast.showError(withMessage: "数据更新失败,原因:\(error?.retMsg ?? "未知错误")", duration: 1.5, finishHandler: {})
  493. }
  494. }
  495. })
  496. }
  497. /// 获取待收款单据数据信息
  498. fileprivate func requestQueryMyCollectionOrders() {
  499. /// 数据异常
  500. guard let orderManager = MTP2BusinessCore.shared.orderManager else { return }
  501. /// startAnimating
  502. _anim?.startAnimating()
  503. /// 发送请求
  504. orderManager.requestQueryMyCollectionOrders(callback: { (isComplete, error, collectionOrders) in
  505. DispatchQueue.main.async {
  506. /// stopAnimating
  507. self._anim?.stopAnimating()
  508. /// 单据数据信息
  509. self.sellMyDetails = collectionOrders ?? []
  510. if !isComplete {
  511. WHToast.showError(withMessage: "待付款单据数据信息查询失败,请稍候再试!", duration: 1.5, finishHandler: {})
  512. }
  513. }
  514. })
  515. }
  516. /// 撤单
  517. ///
  518. /// - Parameter orderId: 单号
  519. /// - Parameter accountId: accountId
  520. /// - Parameter goodsID: goodsID
  521. /// - Parameter marketID: marketID
  522. fileprivate func cancelOrder(_ orderId: String?, _ accountId: Int64?, _ goodsID: String?, _ marketID: Int?) {
  523. /// 数据异常
  524. guard let tradeManager = MTP2BusinessCore.shared.tradeManager else { return }
  525. /// alert
  526. showCommitAlertController(title: "提示", message: "确定要撤单吗?"){(_) in
  527. /// startAnimating
  528. self._anim?.startAnimating()
  529. /// 构建数据
  530. let cancelOrderData = CancelOrderData(oldOrderId: orderId ?? "", accountId: accountId ?? 0, goodsID: goodsID ?? "", marketID: marketID ?? 0, operateType: .OPERATETYPE_ORDERCANCEL)
  531. /// 发送撤单请求
  532. tradeManager.cancelOrder(cancelorderData: cancelOrderData, callback: { (isCompleted, errorInfo) in
  533. DispatchQueue.main.async {
  534. /// stopAnimating
  535. self._anim?.stopAnimating()
  536. if isCompleted {
  537. WHToast.showSuccess(withMessage: "撤销成功", duration: 1.5, finishHandler: {
  538. if self.segmentedView.selectedIndex == 1 {
  539. /// 待付款
  540. self.requestQueryMyCollectionOrders()
  541. } else {
  542. /// 查询数据信息
  543. self.requestQueryHsbySellMyDetails(self.segmentedView.selectedIndex)
  544. }
  545. })
  546. } else {
  547. WHToast.showError(withMessage: "撤单失败,原因:\(ErrorUtils.getDesc(errorCode: errorInfo?.retCode ?? -60001) ?? "未知错误")", duration: 1.5, finishHandler: {})
  548. }
  549. }
  550. })
  551. }
  552. }
  553. /// 合规性校验
  554. /// - Returns: Bool
  555. fileprivate func check() -> Bool {
  556. /// 请选择商品
  557. guard let obj = myGoods else {
  558. WHToast.showError(withMessage: "请选择商品", duration: 1.5, finishHandler: {})
  559. return false
  560. }
  561. /// 请输入委托价格
  562. guard orderPrice.currentDoubleNumbers != 0.0 else {
  563. WHToast.showError(withMessage: "请输入委托价格", duration: 1.5, finishHandler: {})
  564. return false
  565. }
  566. /// 挂牌点选
  567. if obj.trademode == .TRADEMODE_LISTING_SELECT {
  568. /// 委托价格不在范围内
  569. if orderPrice.currentDoubleNumbers < (obj.limitdown) || (orderPrice.currentDoubleNumbers > obj.limitup) {
  570. WHToast.showError(withMessage: "\(obj.limitdown)<=委托价格<=\(obj.limitup)", duration: 1.5, finishHandler: {})
  571. return false
  572. }
  573. }
  574. /// 请输入交易数量
  575. if orderQty.currentIntNumbers == 0 {
  576. WHToast.showError(withMessage: "请输入交易数量", duration: 1.5, finishHandler: {})
  577. return false
  578. }
  579. return true
  580. }
  581. // MARK: - 订阅行情相关
  582. var uuid: String = UUID().uuidString
  583. /// 行情订阅
  584. private func subscriptQuote() {
  585. var goodsCodes = Set<String>()
  586. if let goods = self.myGoods {
  587. goodsCodes = [goods.goodscode]
  588. }
  589. guard let quoteSubscriptManager = MTP2BusinessCore.shared.quoteSubscriptManager else { return }
  590. quoteSubscriptManager.updateQuoteSubcriptGoods(uuid: uuid, goodsCode: goodsCodes)
  591. }
  592. // MARK: - Navigation
  593. // In a storyboard-based application, you will often want to do a little preparation before navigation
  594. override func prepare(for segue: UIStoryboardSegue, sender: Any?) {
  595. // Get the new view controller using segue.destination.
  596. // Pass the selected object to the new view controller.
  597. if segue.identifier == "ShowMyGoods" {
  598. (segue.destination as? MyGoodsViewController)?.block = { (_ model: MoHsbyMyGoods?) in
  599. /// 执行回调后
  600. self.myGoods = model
  601. }
  602. } else if segue.identifier == "ShowMyUnusedDetail" { /// 我的闲置详情
  603. (segue.destination as? MyUnusedDetailViewController)?.takeInfo = (sender as? OrderCell)?.model
  604. }
  605. }
  606. }
  607. // MARK: - JXSegmentedViewDelegate
  608. extension MyUnusedViewController: JXSegmentedViewDelegate {
  609. func segmentedView(_ segmentedView: JXSegmentedView, didSelectedItemAt index: Int) {
  610. if index == 1 {
  611. /// 待付款
  612. self.requestQueryMyCollectionOrders()
  613. } else {
  614. /// 查询我的闲置单据数据
  615. self.requestQueryHsbySellMyDetails(index)
  616. }
  617. }
  618. }
  619. // MARK: - IBNumberEditTextDelegate
  620. extension MyUnusedViewController: IBNumberEditTextDelegate {
  621. func numberInputViewDidEndEditing(_ numberInputView: IBNumberEditText) {
  622. if numberInputView == orderPrice {
  623. let orderPrice = numberInputView.currentDoubleNumbers
  624. /// 更新单据
  625. self.requestQueryHsbyGoodsOrderDetails(orderPrice.toString(reserve: 2))
  626. } else {
  627. /// 如果大于最大数量 默认设置最大数量
  628. if numberInputView.currentIntNumbers > numberInputView.maxIntNum {
  629. numberInputView.currentIntNumbers = numberInputView.maxIntNum
  630. }
  631. }
  632. }
  633. func numberInputView(_ numberInputView: IBNumberEditText, _ buttonType: NumberInputViewButtonType) {
  634. if numberInputView == orderPrice {
  635. let orderPrice = numberInputView.currentDoubleNumbers
  636. /// 更新单据
  637. self.requestQueryHsbyGoodsOrderDetails(orderPrice.toString(reserve: 2))
  638. } else {
  639. /// 如果大于最大数量 默认设置最大数量
  640. if numberInputView.currentIntNumbers > numberInputView.maxIntNum {
  641. numberInputView.currentIntNumbers = numberInputView.maxIntNum
  642. }
  643. }
  644. }
  645. func IBNumberEditText(textField: UITextField) {}
  646. }
  647. class OrderDelegate: NSCoder, UITableViewDelegate, UITableViewDataSource {
  648. let owner: MyUnusedViewController
  649. init(_ owner: MyUnusedViewController) {
  650. self.owner = owner
  651. }
  652. func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
  653. return self.owner.orderDetails.count
  654. }
  655. func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
  656. let cell = tableView.dequeueReusableCell(withIdentifier: owner.OrderCellIdentifier, for: indexPath) as! TradeCell
  657. cell.model = self.owner.orderDetails[indexPath.row]
  658. cell.isCheck = false
  659. cell.block = { (_ model: MoHsbyGoodsOrderDetail?, _ isCheck: Bool, _ qty: Int) in
  660. /// 设置数量
  661. DispatchQueue.main.asyncAfter(deadline: DispatchTime.now()+0.2) {
  662. var qty: Int = 0
  663. self.owner.orderTableView.visibleCells.forEach { (cell) in
  664. if (cell as! TradeCell).isCheck {
  665. qty += (cell as! TradeCell).qtyField.currentIntNumbers
  666. }
  667. }
  668. self.owner.qty = qty
  669. /// 输入框是否可用
  670. self.owner.orderQty.isUserInteractionEnabled = !self.owner.orderTableView.visibleCells.contains(where: { (cell) -> Bool in
  671. return (cell as! TradeCell).isCheck
  672. })
  673. }
  674. }
  675. return cell
  676. }
  677. func tableView(_ tableView: UITableView, heightForRowAt indexPath: IndexPath) -> CGFloat {
  678. return 50.0
  679. }
  680. }
  681. // MARK: - UICollectionViewDelegate, UICollectionViewDataSource, UICollectionViewDelegateFlowLayout
  682. class MyUnusedDelegate: NSCoder, UITableViewDelegate, UITableViewDataSource {
  683. let owner: MyUnusedViewController
  684. init(_ owner: MyUnusedViewController) {
  685. self.owner = owner
  686. }
  687. func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
  688. return owner.sellMyDetails.count
  689. }
  690. func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
  691. let cell = tableView.dequeueReusableCell(withIdentifier: owner.CellIdentifier, for: indexPath) as! SellMyDetailCell
  692. cell.model = owner.sellMyDetails[indexPath.row]
  693. cell.cancelBlock = { (_ orderId: String?, _ accountId: Int64?, _ goodsID: String?, _ marketID: Int?) in
  694. /// 撤销
  695. self.owner.cancelOrder(orderId, accountId, goodsID, marketID)
  696. }
  697. return cell
  698. }
  699. func tableView(_ tableView: UITableView, heightForRowAt indexPath: IndexPath) -> CGFloat {
  700. /// 待付款
  701. if self.owner.segmentedView.selectedIndex == 1 {
  702. return 180.0
  703. } else {
  704. return owner.sellMyDetails[indexPath.row].ordertype == 1 ? 180.0 : 200.0
  705. }
  706. }
  707. }
  708. // MARK: - OrderCell
  709. class SellMyDetailCell: BaseTableViewCell<MoHsbySellMyDetail> {
  710. /// 供应商名称
  711. @IBOutlet weak var company: UILabel!
  712. /// 状态
  713. @IBOutlet weak var status: UILabel!
  714. /// 单号
  715. @IBOutlet weak var orderNo: UILabel!
  716. /// 商品图片
  717. @IBOutlet weak var goodsImage: UIImageView!
  718. /// 价格
  719. @IBOutlet weak var price: UILabel!
  720. /// 标题
  721. @IBOutlet weak var title: UILabel!
  722. /// 数量
  723. @IBOutlet weak var qty: UILabel!
  724. /// 金额
  725. @IBOutlet weak var amount: UILabel!
  726. /// 待付款金额
  727. @IBOutlet weak var payamount: UILabel!
  728. /// 撤销
  729. @IBOutlet weak var cancel: IBButton!
  730. /// 撤销
  731. @IBOutlet weak var stackView: UIStackView!
  732. /// 数据
  733. override var model: MoHsbySellMyDetail? {
  734. didSet {
  735. /// 数据异常
  736. guard let obj = model else { return }
  737. if let collectionOrder = model as? MoMyCollectionOrders {
  738. /// 隐藏付款金额
  739. payamount.isHidden = false
  740. /// 隐藏金额
  741. amount.isHidden = true
  742. /// 金额详情
  743. payamount.text = "总价:\(collectionOrder.tradeamount.toDownString(reserve: obj.decimalplace)) 优惠:\(collectionOrder.offamount.toDownString(reserve: obj.decimalplace)) 实付款:\(collectionOrder.payamount.toDownString(reserve: obj.decimalplace))"
  744. /// 数量
  745. qty.text = obj.tradeqty.description
  746. /// 等待对方付款
  747. status.text = "等待对方付款"
  748. /// 只有已发布的商品才可以撤销单据
  749. stackView.isHidden = true
  750. } else {
  751. /// 隐藏付款金额
  752. payamount.isHidden = true
  753. /// 待付款数量
  754. amount.text = "金额:\(obj.currencysign) \((Double(obj.qty-obj.cancelqty-obj.tradeqty)*obj.price*obj.agreeunit).toDownString(reserve: obj.decimalplace))"
  755. /// 状态
  756. if obj.buyorsell == EOrderDirection.ORDERDIRECTION_BUY {
  757. /// 已完成
  758. status.text = "已完成"
  759. /// 只有已发布的商品才可以撤销单据
  760. stackView.isHidden = true
  761. } else {
  762. status.text = obj.ordertype == 0 ? "已发布" : "已完成"
  763. /// 只有已发布的商品才可以撤销单据
  764. stackView.isHidden = !(obj.ordertype == 0)
  765. }
  766. /// 数量
  767. qty.text = obj.qty.description
  768. }
  769. /// 单号
  770. orderNo.text = obj.goodscode
  771. /// 商品名称
  772. title.text = obj.goodsname
  773. /// 价格
  774. price.text = "\(obj.currencysign) \(obj.price.toDownString(reserve: obj.decimalplace))"
  775. /// 商品图片
  776. guard let url = StringUtils.getImageUrl(obj.picurls) else { return }
  777. goodsImage.sd_setImage(with: url, placeholderImage: UIImage(named: "placeholder_image"), options: .queryDiskDataSync, context: nil)
  778. /// 我的闲置显示的是委托时间
  779. company.text = DateUtils.getTDateString(obj.time, "yyyy-MM-dd HH:mm:ss")
  780. }
  781. }
  782. /// 撤销回调块
  783. var cancelBlock: ((_ orderId: String?, _ accountId: Int64?, _ goodsID: String?, _ marketID: Int?) -> Void)?
  784. /// 支付回调块
  785. var payBlock: ((_ orderId: String?, _ accountId: Int64?, _ goodsID: String?, _ marketID: Int?, _ amount: Double) -> Void)?
  786. /// 按钮点击响应事件
  787. /// - Parameter sender: sender
  788. @IBAction private func onButtonPressed(_ sender: IBButton) {
  789. switch sender {
  790. case cancel: /// 撤销
  791. if let cancel = cancelBlock { cancel(model?.orderid, model?.accountid ?? 0, model?.goodsid.description, model?.marketid) }
  792. default: break
  793. }
  794. }
  795. }