ChoiceGoodsViewController.swift 7.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221
  1. //
  2. // ChoiceGoodsViewController.swift
  3. // MTP2_iOS
  4. //
  5. // Created by Muchinfo on 2019/6/1.
  6. // Copyright © 2019 Muchinfo. All rights reserved.
  7. //
  8. import UIKit
  9. /// 商品检索视图控制器
  10. class ChoiceGoodsViewController: BaseViewController {
  11. // MARK: - 属性列表
  12. /// 搜索框 第一响应者
  13. @IBOutlet weak var searchBar: UISearchBar! {
  14. didSet {
  15. searchBar.becomeFirstResponder()
  16. }
  17. }
  18. /// 数据展示列表
  19. @IBOutlet weak var tableView: UITableView!
  20. /// 是否是交易
  21. var isTrade: Bool = false
  22. /// 商品数据信息
  23. var goodsInfo: [MoGoodsInfo] = []
  24. /// 执行回调
  25. var block: ((_ goodsInfo: MoGoodsInfo?) -> Void)?
  26. // MARK: - 生命周期相关
  27. override func viewDidLoad() {
  28. super.viewDidLoad()
  29. /// 获取上次搜索的商品数据信息
  30. getResearchGoodsInfo()
  31. /// returnKeyType
  32. searchBar.returnKeyType = .done
  33. }
  34. // MARK: - 交互相关
  35. /// 获取上次之前搜索的商品数据信息
  36. fileprivate func getResearchGoodsInfo() {
  37. /// 现清除数据
  38. if goodsInfo.count != 0 { goodsInfo.removeAll() }
  39. /// 获取数据
  40. let goodsIds = UserDefaultsUtils.getResearchGoodsInfo()
  41. /// 商品信息
  42. if goodsIds.count != 0, let infos = MTP2BusinessCore.shared.goodsManager?.goodsInfos, infos.count != 0 {
  43. for id in goodsIds {
  44. if let goods = infos.first(where: {id == "\($0.goodsid)"}) {
  45. goodsInfo.append(goods)
  46. }
  47. }
  48. }
  49. /// 刷新列表数据
  50. tableView.reloadData()
  51. }
  52. /// 商品切换的方法
  53. /// - Parameter goodsId: goodsId
  54. func didSelectGoods(_ goods: MoGoodsInfo?) {
  55. /// 获取商品数据信息
  56. guard let info = goods, let bk = block else { return }
  57. /// 执行回调
  58. DispatchQueue.main.asyncAfter(deadline: .now()+0.2) {
  59. bk(info)
  60. }
  61. }
  62. // MARK: - Navigation
  63. // In a storyboard-based application, you will often want to do a little preparation before navigation
  64. override func prepare(for segue: UIStoryboardSegue, sender: Any?) {
  65. // Get the new view controller using segue.destination.
  66. // Pass the selected object to the new view controller.
  67. if segue.identifier == "ShowChart", let c = segue.destination as? ChartViewController {
  68. /// 图表
  69. block = { (_ info: MoGoodsInfo?) in
  70. /// 期货合约商品
  71. c.moGoodsInfo = info
  72. /// 显示分时图
  73. c.showTSChart()
  74. }
  75. }
  76. }
  77. override func shouldPerformSegue(withIdentifier identifier: String, sender: Any?) -> Bool {
  78. return !isTrade
  79. }
  80. }
  81. // MARK: - UISearchBarDelegate
  82. extension ChoiceGoodsViewController: UISearchBarDelegate {
  83. /// searchBarCancelButtonClicked
  84. func searchBarCancelButtonClicked(_ searchBar: UISearchBar) {
  85. /// 回到上层视图
  86. self.navigationController?.popViewController(animated: true)
  87. }
  88. func searchBar(_ searchBar: UISearchBar, textDidChange searchText: String) {
  89. /// 如果数据为空
  90. if searchText == "" {
  91. getResearchGoodsInfo()
  92. } else {
  93. /// 商品信息不存在
  94. guard let goods = try? DatabaseHelper.getGoodsInfo(ByCondition: searchText) else { return }
  95. /// 刷新数据信息
  96. goodsInfo = goods
  97. /// 刷新数据
  98. tableView.reloadData()
  99. }
  100. }
  101. func searchBarSearchButtonClicked(_ searchBar: UISearchBar) {
  102. /// 失去第一响应
  103. searchBar.resignFirstResponder()
  104. }
  105. }
  106. // MARK: - UITableViewDelegate, UITableViewDataSource
  107. extension ChoiceGoodsViewController: UITableViewDelegate, UITableViewDataSource {
  108. func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
  109. /// 记录已查询的商品数据
  110. UserDefaultsUtils.setResearchGoodsInfo("\(goodsInfo[indexPath.row].goodsid)")
  111. /// 商品切换回掉
  112. self.didSelectGoods(goodsInfo[indexPath.row])
  113. }
  114. func tableView(_ tableView: UITableView, heightForRowAt indexPath: IndexPath) -> CGFloat {
  115. return 50.0
  116. }
  117. func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
  118. return goodsInfo.count
  119. }
  120. func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
  121. let cell = tableView.dequeueReusableCell(withIdentifier: "tableView_cell", for: indexPath) as! ChoiceGoodsTableCell
  122. cell.model = goodsInfo[indexPath.row]
  123. return cell
  124. }
  125. }
  126. // MARK: - UIScrollViewDelegate
  127. extension ChoiceGoodsViewController: UIScrollViewDelegate {
  128. func scrollViewDidScroll(_ scrollView: UIScrollView) {
  129. /// 失去第一响应
  130. searchBar.resignFirstResponder()
  131. }
  132. }
  133. // MARK: - ChoiceGoodsTableCell
  134. class ChoiceGoodsTableCell: BaseTableViewCell <MoGoodsInfo> {
  135. /// 商品名称
  136. @IBOutlet weak var goodsName: UILabel!
  137. /// 商品代码
  138. @IBOutlet weak var goodsCode: UILabel!
  139. /// 标志
  140. @IBOutlet weak var flag: UILabel! {
  141. didSet {
  142. flag.layer.cornerRadius = 5.0
  143. flag.layer.borderWidth = 1.0
  144. flag.layer.borderColor = UIColor.white.cgColor
  145. flag.layer.masksToBounds = true
  146. flag.textAlignment = .center
  147. }
  148. }
  149. /// 自选按钮
  150. @IBOutlet weak var selfButton: UIButton!
  151. /// 标志
  152. @IBOutlet weak var flagWidthLayoutConstraint: NSLayoutConstraint!
  153. /// 是否为自选
  154. var isSelfGoods = false {
  155. didSet {
  156. /// 设置
  157. selfButton.setImage(UIImage(named: isSelfGoods ? "self" : "unself"), for: .normal)
  158. }
  159. }
  160. override var model: MoGoodsInfo? {
  161. didSet {
  162. /// 异常
  163. guard let info = model else { return }
  164. /// 商品名称和代码
  165. goodsName.text = info.goodsname
  166. goodsCode.text = info.goodscode
  167. /// 获取标志
  168. guard let goodsManager = MTP2BusinessCore.shared.goodsManager,
  169. let group = goodsManager.goodsGroups?.first(where: {$0.goodsgroupid == info.goodsgroupid}),
  170. let exchanges = goodsManager.exchanges?.first(where: {$0.autoid == group.exexchangeid}) else { return }
  171. /// 计算宽度
  172. let size = exchanges.exexchangecode.getExchangeCodeVerb().getTextSize(font: .font_14, size: CGSize(width: kScreenWidth, height: 35.0))
  173. flagWidthLayoutConstraint.constant = CGFloat(size.width) + 10.0
  174. flag.text = exchanges.exexchangecode.getExchangeCodeVerb()
  175. /// 自选商品
  176. isSelfGoods = goodsManager.favGoodsIds?.contains(where: {$0 == info.goodsid}) ?? false
  177. }
  178. }
  179. // MARK: - 交互相关
  180. /// onButtonPressed
  181. /// - Parameter sender: sender
  182. @IBAction func onButtonPressed(_ sender: UIControl) {
  183. /// 异常
  184. guard let info = model,
  185. let goodsManager = MTP2BusinessCore.shared.goodsManager else { return }
  186. /// 删自选
  187. if self.isSelfGoods {
  188. goodsManager.requestRemoveUserFavoriteGoods(goodsID: info.goodsid, callback: { (isSuccess, _) in
  189. DispatchQueue.main.async { self.isSelfGoods = !isSuccess }
  190. })
  191. } else {
  192. /// 加自选
  193. goodsManager.requestAddUserFavoriteGoods(goodsID: info.goodsid, callback: { (isSuccess, _) in
  194. DispatchQueue.main.async { self.isSelfGoods = isSuccess }
  195. })
  196. }
  197. }
  198. }