OrderViewController.swift 19 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479
  1. //
  2. // OrderViewController.swift
  3. // MTP2_iOS
  4. //
  5. // Created by Handy_Cao on 2020/10/28.
  6. // Copyright © 2020 Muchinfo. All rights reserved.
  7. //
  8. import UIKit
  9. import JXSegmentedView
  10. import WHToast
  11. import SDWebImage
  12. import GTMRefresh
  13. import IBAnimatable
  14. /// 我的订单视图容器控制类
  15. class OrderViewController: BaseViewController {
  16. // MARK: - 属性列表
  17. /// 分段控制器
  18. @IBOutlet weak var segmentedView: JXSegmentedView! {
  19. didSet {
  20. segmentedView.delegate = self
  21. segmentedView.dataSource = dataSource
  22. segmentedView.indicators = [self.indicator]
  23. }
  24. }
  25. /// segmentedDataSource
  26. let dataSource: JXSegmentedTitleDataSource = {
  27. var items: [String] = ["全部", "待成交", "待付款", "已完成"]
  28. $0.titles = items
  29. $0.titleNormalColor = UIColorFromHex(rgbValue: 0x333333)
  30. $0.titleNormalFont = .font_14
  31. $0.titleSelectedColor = UIColorFromHex(rgbValue: 0x60a1e3)
  32. $0.titleSelectedFont = .font_14
  33. $0.isTitleColorGradientEnabled = true
  34. return $0
  35. } (JXSegmentedTitleDataSource())
  36. /// 列表
  37. @IBOutlet weak var tableView: UITableView!
  38. /// CellIdentifier
  39. let CellIdentifier = "Order_Cell"
  40. /// 单据数据信息
  41. var orders: [MoHsbyMyBuyOrderDetails] = [] {
  42. didSet {
  43. /// 刷新列表数据
  44. tableView.reloadData()
  45. /// 隐藏按钮
  46. self.noDataButton.isHidden = !(orders.count == 0)
  47. }
  48. }
  49. // MARK: - 生命周期
  50. override func viewDidLoad() {
  51. super.viewDidLoad()
  52. // Do any additional setup after loading the view.
  53. /// UI界面初始化
  54. buildView()
  55. }
  56. override func viewWillAppear(_ animated: Bool) {
  57. super.viewWillAppear(animated)
  58. /// 隐藏导航栏
  59. self.navigationController?.setNavigationBarHidden(false, animated: true)
  60. /// 数据初始化
  61. initData()
  62. }
  63. // MARK: - 初始化
  64. /// UI界面初始化
  65. fileprivate func buildView() {
  66. /// loding......
  67. self.addLoadingView()
  68. /// 添加下拉刷新控件
  69. self.tableView.gtm_addRefreshHeaderView(refreshHeader: DefaultGTMRefreshHeader()) {
  70. /// 重置请求条件
  71. self.reloadData(self.segmentedView.selectedIndex)
  72. }
  73. }
  74. /// 数据初始化
  75. fileprivate func initData() {
  76. /// 请求数据
  77. DispatchQueue.main.asyncAfter(deadline: DispatchTime.now()+0.2) {
  78. /// 默认滚动
  79. self.segmentedView.defaultSelectedIndex = self.selectedIndex
  80. /// 刷新
  81. self.segmentedView.reloadData()
  82. /// 刷新数据
  83. self.reloadData(self.selectedIndex)
  84. }
  85. }
  86. // MARK: - 页面跳转
  87. /// 详情页面跳转
  88. /// - Parameter detail: detail
  89. fileprivate func goToGoodsDetailViewController(_ detail: MoGoodsInfo) {
  90. /// 商品详情
  91. let goodsDetailController = UIStoryboard(name: "Quote", bundle: nil).instantiateViewController(withIdentifier: "GoodsDetail") as! GoodsDetailViewController
  92. goodsDetailController.model = detail
  93. self.navigationController?.pushViewController(goodsDetailController, animated: true)
  94. }
  95. // MARK: - 接口请求
  96. /// 刷新数据
  97. fileprivate func reloadData(_ segmentIndex: Int) {
  98. switch segmentIndex {
  99. case 1: /// 抢购中
  100. self.requestQueryHsbyMyBuyOrderDetails(1)
  101. case 2: /// 查询待付款数据信息
  102. self.requestQueryMyPayOrders()
  103. case 3: /// 查询已完成
  104. self.requestQueryHsbyBuyMyTradeDetail()
  105. default: /// 查询全部
  106. self.requestQueryHsbyMyBuyOrderDetails(nil)
  107. }
  108. }
  109. /// 查询我的单据数据信息
  110. /// - Parameter status: 说明: 全部:一二级市场买委托;抢购中:一级市场买摘; 求购中:二级市场买挂
  111. fileprivate func requestQueryHsbyMyBuyOrderDetails(_ status: Int?) {
  112. /// 异常
  113. guard let orderManager = MTP2BusinessCore.shared.orderManager else { return }
  114. /// startAnimating
  115. _anim?.startAnimating()
  116. /// 发送请求
  117. orderManager.requestQueryHsbyMyBuyOrderDetails(status) { (isComplete, error, orderDetails) in
  118. DispatchQueue.main.async {
  119. /// stopAnimating
  120. self._anim?.stopAnimating()
  121. /// endRefreshing
  122. self.tableView.endRefreshing(isSuccess: true)
  123. /// 数据赋值
  124. if isComplete {
  125. if status == 1 {
  126. self.orders = orderDetails?.filter({$0.mybuystatus == .BuyIn || $0.mybuystatus == .LookingForIn}) ?? []
  127. } else {
  128. self.orders = orderDetails ?? []
  129. }
  130. } else { /// 查询失败
  131. self.orders = []
  132. WHToast.showError(withMessage: "数据查询失败,原因:\(error?.retMsg ?? "未知错误")", duration: 1.5, finishHandler: {})
  133. }
  134. }
  135. }
  136. }
  137. /// 查询已完成订单数据信息
  138. fileprivate func requestQueryHsbyBuyMyTradeDetail() {
  139. /// 异常
  140. guard let orderManager = MTP2BusinessCore.shared.orderManager else {
  141. return
  142. }
  143. /// startAnimating
  144. _anim?.startAnimating()
  145. /// 发送请求
  146. orderManager.requestQueryHsbyBuyMyTradeDetail(nil, nil) { (isComplete, error, objs) in
  147. DispatchQueue.main.async {
  148. /// stopAnimating
  149. self._anim?.stopAnimating()
  150. /// endRefreshing
  151. self.tableView.endRefreshing(isSuccess: true)
  152. /// 数据赋值
  153. if isComplete {
  154. self.orders = objs ?? []
  155. } else { /// 查询失败
  156. self.orders = []
  157. WHToast.showError(withMessage: "数据查询失败,原因:\(error?.retMsg ?? "未知错误")", duration: 1.5, finishHandler: {})
  158. }
  159. }
  160. }
  161. }
  162. /// 查询待付款单据数据信息
  163. fileprivate func requestQueryMyPayOrders() {
  164. /// 异常
  165. guard let orderManager = MTP2BusinessCore.shared.orderManager else {
  166. return
  167. }
  168. /// startAnimating
  169. _anim?.startAnimating()
  170. /// 发送请求
  171. orderManager.requestQueryMyPayOrders(nil, nil) { (isComplete, error, payOrders) in
  172. DispatchQueue.main.async {
  173. /// stopAnimating
  174. self._anim?.stopAnimating()
  175. /// endRefreshing
  176. self.tableView.endRefreshing(isSuccess: true)
  177. /// 数据赋值
  178. if isComplete {
  179. self.orders = payOrders ?? []
  180. } else { /// 查询失败
  181. self.orders = []
  182. WHToast.showError(withMessage: "数据查询失败,原因:\(error?.retMsg ?? "未知错误")", duration: 1.5, finishHandler: {})
  183. }
  184. }
  185. }
  186. }
  187. /// 撤单
  188. ///
  189. /// - Parameter orderId: 单号
  190. fileprivate func cancelOrder(_ orderId: String?, _ accountId: Int64?, _ goodsID: String?, _ marketID: Int?) {
  191. /// 异常
  192. guard let tradeManager = MTP2BusinessCore.shared.tradeManager else {
  193. return
  194. }
  195. /// alert
  196. showCommitAlertController(title: "提示", message: "确定要撤单吗?"){(_) in
  197. /// startAnimating
  198. self._anim?.startAnimating()
  199. /// 构建数据
  200. let cancelOrderData = CancelOrderData(oldOrderId: orderId ?? "", accountId: accountId ?? 0, goodsID: goodsID ?? "", marketID: marketID ?? 0, operateType: .OPERATETYPE_ORDERCANCEL)
  201. /// 发送撤单请求
  202. tradeManager.cancelOrder(cancelorderData: cancelOrderData, callback: { (isCompleted, errorInfo) in
  203. DispatchQueue.main.async {
  204. /// stopAnimating
  205. self._anim?.stopAnimating()
  206. if isCompleted {
  207. WHToast.showSuccess(withMessage: "撤销成功", duration: 1.5, finishHandler: {
  208. /// 刷新数据
  209. self.reloadData(self.segmentedView.selectedIndex)
  210. })
  211. } else {
  212. WHToast.showError(withMessage: "撤单失败,原因:\(ErrorUtils.getDesc(errorCode: errorInfo?.retCode ?? -60001) ?? "未知错误")", duration: 1.5, finishHandler: {})
  213. }
  214. }
  215. })
  216. }
  217. }
  218. /// 取消待付款订单
  219. ///
  220. /// - Parameter orderId: 单号
  221. fileprivate func cancelPayment(_ orderId: String?, _ accountId: Int64?, _ goodsID: String?, _ marketID: Int?) {
  222. /// 异常
  223. guard let tradeManager = MTP2BusinessCore.shared.tradeManager else {
  224. return
  225. }
  226. /// alert
  227. showCommitAlertController(title: "提示", message: "确定要撤单吗?"){(_) in
  228. /// startAnimating
  229. self._anim?.startAnimating()
  230. /// 构建数据
  231. let cancelOrderData = CancelOrderData(oldOrderId: orderId ?? "", accountId: accountId ?? 0, goodsID: goodsID ?? "", marketID: marketID ?? 0, operateType: .OPERATETYPE_ORDERCANCEL)
  232. /// 发送撤单请求
  233. tradeManager.cancelPayment(cancelorderData: cancelOrderData, callback: { (isCompleted, errorInfo) in
  234. DispatchQueue.main.async {
  235. /// stopAnimating
  236. self._anim?.stopAnimating()
  237. if isCompleted {
  238. WHToast.showSuccess(withMessage: "撤销成功", duration: 1.5, finishHandler: {
  239. /// 刷新数据
  240. self.reloadData(self.segmentedView.selectedIndex)
  241. })
  242. } else {
  243. WHToast.showError(withMessage: "撤单失败,原因:\(ErrorUtils.getDesc(errorCode: errorInfo?.retCode ?? -60001) ?? "未知错误")", duration: 1.5, finishHandler: {})
  244. }
  245. }
  246. })
  247. }
  248. }
  249. /*
  250. // MARK: - Navigation
  251. // In a storyboard-based application, you will often want to do a little preparation before navigation
  252. override func prepare(for segue: UIStoryboardSegue, sender: Any?) {
  253. // Get the new view controller using segue.destination.
  254. // Pass the selected object to the new view controller.
  255. }
  256. */
  257. }
  258. // MARK: - JXSegmentedViewDelegate
  259. extension OrderViewController: JXSegmentedViewDelegate {
  260. func segmentedView(_ segmentedView: JXSegmentedView, didSelectedItemAt index: Int) {
  261. /// 重置默认状态
  262. self.selectedIndex = index
  263. /// 刷新数据
  264. self.reloadData(index)
  265. }
  266. }
  267. // MARK: - UITableViewDelegate, UITableViewDataSource
  268. extension OrderViewController: UITableViewDelegate, UITableViewDataSource {
  269. func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
  270. return orders.count
  271. }
  272. func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
  273. let cell = tableView.dequeueReusableCell(withIdentifier: CellIdentifier, for: indexPath) as! OrderCell
  274. cell.model = orders[indexPath.row]
  275. cell.cancelBlock = { (_ orderId: String?, _ accountId: Int64?, _ goodsID: String?, _ marketID: Int?) in
  276. if self.segmentedView.selectedIndex == 2 {
  277. /// 撤销待付款单据
  278. self.cancelPayment(orderId, accountId, goodsID, marketID)
  279. } else {
  280. /// 撤销
  281. self.cancelOrder(orderId, accountId, goodsID, marketID)
  282. }
  283. }
  284. /// 去支付
  285. cell.payBlock = { (_ orderId: String?, _ accountId: Int64?, _ goodsID: String?, _ marketID: Int?, _ amount: Double) in
  286. /// 异常
  287. guard let orderManager = MTP2BusinessCore.shared.orderManager else {
  288. return
  289. }
  290. /// 查询查询单据数据
  291. orderManager.requestQueryMyPayOrders(orderId, nil) { (isComplete, error, payOrders) in
  292. DispatchQueue.main.async {
  293. if isComplete {
  294. /// 异常
  295. guard let paymentController = self.viewController(name: "Order", identifier: "Payment") as? PaymentViewController else { return }
  296. /// 待付款单据
  297. paymentController.myPayOrder = self.orders[indexPath.row] as? MoMyPayOrders
  298. /// 页面跳转
  299. self.navigationController?.pushViewController(paymentController, animated: true)
  300. } else {
  301. WHToast.showError(withMessage: "单据获取失败,请稍候重试!", duration: 1.5, finishHandler: {})
  302. }
  303. }
  304. }
  305. }
  306. return cell
  307. }
  308. func tableView(_ tableView: UITableView, heightForRowAt indexPath: IndexPath) -> CGFloat {
  309. switch segmentedView.selectedIndex {
  310. case 3:
  311. return 170.0
  312. case 2:
  313. return 200.0
  314. default:
  315. return orders[indexPath.row].mybuystatus != .LookingForIn ? 175.0 : 200.0
  316. }
  317. }
  318. func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
  319. /// 数据异常
  320. guard let goodsManager = MTP2BusinessCore.shared.goodsManager else { return }
  321. if self.orders[indexPath.row].trademode == .TRADEMODE_LISTING_SELECT { /// 挂牌点选
  322. /// 查询商品详情
  323. goodsManager.requestQueryHsbyListingGoodsDetail(self.orders[indexPath.row].goodsid, callback: { (isComplete, error, goods) in
  324. DispatchQueue.main.async {
  325. if isComplete,
  326. let detail = goods {
  327. self.goToGoodsDetailViewController(detail)
  328. } else {
  329. WHToast.showError(withMessage: "该商品已退市", duration: 1.5, finishHandler: {})
  330. }
  331. }
  332. })
  333. } else if self.orders[indexPath.row].trademode == .TRADEMODE_TRADEMODE_HSBY_SHOP {} /// 商城 不做跳转
  334. else {
  335. /// 查询商品详情
  336. goodsManager.requestQueryHsbyPreGoodsDetail(self.orders[indexPath.row].goodsid, callback: { (isComplete, error, goods) in
  337. DispatchQueue.main.async {
  338. if isComplete,
  339. let detail = goods {
  340. self.goToGoodsDetailViewController(detail)
  341. } else {
  342. WHToast.showError(withMessage: "该商品已退市", duration: 1.5, finishHandler: {})
  343. }
  344. }
  345. })
  346. }
  347. }
  348. }
  349. // MARK: - OrderCell
  350. class OrderCell: BaseTableViewCell<MoHsbyMyBuyOrderDetails> {
  351. /// 供应商名称
  352. @IBOutlet weak var company: UILabel!
  353. /// 状态
  354. @IBOutlet weak var status: UILabel!
  355. /// 单号
  356. @IBOutlet weak var orderNo: UILabel!
  357. /// 商品图片
  358. @IBOutlet weak var goodsImage: UIImageView!
  359. /// 价格
  360. @IBOutlet weak var price: UILabel!
  361. /// 金额
  362. @IBOutlet weak var amount: UILabel!
  363. /// 标题
  364. @IBOutlet weak var title: UILabel!
  365. /// 数量
  366. @IBOutlet weak var qty: UILabel!
  367. /// 撤销
  368. @IBOutlet weak var cancel: AnimatableButton!
  369. /// 去支付
  370. @IBOutlet weak var pay: AnimatableButton!
  371. /// 撤销
  372. @IBOutlet weak var stackView: UIStackView!
  373. /// 数据
  374. override var model: MoHsbyMyBuyOrderDetails? {
  375. didSet {
  376. /// 数据异常
  377. guard let obj = model else { return }
  378. /// 单号
  379. orderNo.text = obj.goodscode
  380. /// 商品名称
  381. title.text = obj.goodsname
  382. /// 供应商名称
  383. company.text = model?.trademode == .TRADEMODE_TRADEMODE_CPTRADE_LS ? obj.vendorname1 : obj.vendorname2
  384. if let payOrder = obj as? MoMyPayOrders { /// 待付款
  385. /// 状态
  386. status.text = payOrder.payflag.description
  387. /// 成交价格
  388. price.text = "\(obj.currencysign) \(payOrder.tradeprice)"
  389. /// 金额
  390. amount.text = "总价:\(payOrder.originalamount.toString(reserve: 2)), 优惠:\(payOrder.offamount.toString(reserve: 2)), 实付款:\(payOrder.payamount.toString(reserve: 2))"
  391. /// 数量
  392. qty.text = "×\(payOrder.tradeqty)"
  393. /// 只有求购中的商品才可以撤销单据
  394. pay.isHidden = !(payOrder.payflag == .UnPay)
  395. /// 只有求购中的商品才可以撤销单据
  396. stackView.isHidden = false
  397. } else if let sellMyDetail = obj as? MoHsbySellMyDetail { /// 我的订单 (已完成)
  398. /// 价格
  399. price.text = "\(sellMyDetail.currencysign) \(sellMyDetail.price)"
  400. /// 金额
  401. amount.text = "金额:\(sellMyDetail.amount)"
  402. /// 数量
  403. qty.text = "×\(sellMyDetail.qty)"
  404. /// 状态
  405. status.text = "已完成"
  406. /// 只有求购中的商品才可以撤销单据
  407. stackView.isHidden = true
  408. } else {
  409. /// 价格
  410. price.text = "\(obj.currencysign) \(obj.orderprice)"
  411. /// 金额
  412. amount.text = "金额:\(obj.orderamount)"
  413. /// 数量
  414. qty.text = "×\(obj.orderqty)"
  415. /// 状态
  416. status.text = obj.mybuystatus.description
  417. /// 只有求购中的商品才可以撤销单据
  418. stackView.isHidden = !(obj.mybuystatus == .LookingForIn)
  419. /// 隐藏支付按钮
  420. pay.isHidden = true
  421. }
  422. /// 商品图片
  423. guard let url = StringUtils.getImageUrl(obj.trademode == .TRADEMODE_TRADEMODE_CPTRADE_LS ? obj.picurls1 : obj.picurls2) else { return }
  424. goodsImage.sd_setImage(with: url, placeholderImage: UIImage(named: "placeholder_image"), options: .queryDiskDataSync, context: nil)
  425. }
  426. }
  427. /// 撤销回调块
  428. var cancelBlock: ((_ orderId: String?, _ accountId: Int64?, _ goodsID: String?, _ marketID: Int?) -> Void)?
  429. /// 支付回调块
  430. var payBlock: ((_ orderId: String?, _ accountId: Int64?, _ goodsID: String?, _ marketID: Int?, _ amount: Double) -> Void)?
  431. /// 按钮点击响应事件
  432. /// - Parameter sender: sender
  433. @IBAction private func onButtonPressed(_ sender: AnimatableButton) {
  434. switch sender {
  435. case cancel: /// 撤销
  436. if let obj = model as? MoMyPayOrders {
  437. if let cancel = cancelBlock { cancel(obj.tradeid, Int64(obj.buyaccountid), model?.goodsid.description, model?.marketid) }
  438. } else {
  439. if let cancel = cancelBlock { cancel(model?.orderid, model?.accountid ?? 0, model?.goodsid.description, model?.marketid) }
  440. }
  441. case pay:
  442. if let pay = payBlock { pay((model as? MoMyPayOrders)?.tradeid, model?.accountid ?? 0, model?.goodsid.description, model?.marketid, (model as? MoMyPayOrders)?.tradeamount ?? 0.0) }
  443. default: break
  444. }
  445. }
  446. }