OrderViewController.swift 20 KB

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