// // ChoiceGoodsViewController.swift // MTP2_iOS // // Created by Muchinfo on 2019/6/1. // Copyright © 2019 Muchinfo. All rights reserved. // import UIKit /// 商品检索视图控制器 class ChoiceGoodsViewController: BaseViewController { // MARK: - 属性列表 /// 搜索框 第一响应者 @IBOutlet weak var searchBar: UISearchBar! { didSet { searchBar.becomeFirstResponder() } } /// 数据展示列表 @IBOutlet weak var tableView: UITableView! /// 是否是交易 var isTrade: Bool = false /// 商品数据信息 var goodsInfo: [MoGoodsInfo] = [] /// 执行回调 var block: ((_ goodsInfo: MoGoodsInfo?) -> Void)? // MARK: - 生命周期相关 override func viewDidLoad() { super.viewDidLoad() /// 获取上次搜索的商品数据信息 getResearchGoodsInfo() /// returnKeyType searchBar.returnKeyType = .done } // MARK: - 交互相关 /// 获取上次之前搜索的商品数据信息 fileprivate func getResearchGoodsInfo() { /// 现清除数据 if goodsInfo.count != 0 { goodsInfo.removeAll() } /// 获取数据 let goodsIds = UserDefaultsUtils.getResearchGoodsInfo() /// 商品信息 if goodsIds.count != 0, let infos = MTP2BusinessCore.shared.goodsManager?.goodsInfos, infos.count != 0 { for id in goodsIds { if let goods = infos.first(where: {id == "\($0.goodsid)"}) { goodsInfo.append(goods) } } } /// 刷新列表数据 tableView.reloadData() } /// 商品切换的方法 /// - Parameter goodsId: goodsId func didSelectGoods(_ goods: MoGoodsInfo?) { /// 获取商品数据信息 guard let info = goods, let bk = block else { return } /// 执行回调 DispatchQueue.main.asyncAfter(deadline: .now()+0.2) { bk(info) } } // 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. if segue.identifier == "ShowChart", let c = segue.destination as? ChartViewController { /// 图表 block = { (_ info: MoGoodsInfo?) in /// 期货合约商品 c.moGoodsInfo = info /// 显示分时图 c.showTSChart() } } } override func shouldPerformSegue(withIdentifier identifier: String, sender: Any?) -> Bool { return !isTrade } } // MARK: - UISearchBarDelegate extension ChoiceGoodsViewController: UISearchBarDelegate { /// searchBarCancelButtonClicked func searchBarCancelButtonClicked(_ searchBar: UISearchBar) { /// 回到上层视图 self.navigationController?.popViewController(animated: true) } func searchBar(_ searchBar: UISearchBar, textDidChange searchText: String) { /// 如果数据为空 if searchText == "" { getResearchGoodsInfo() } else { /// 商品信息不存在 guard let goods = try? DatabaseHelper.getGoodsInfo(ByCondition: searchText) else { return } /// 刷新数据信息 goodsInfo = goods /// 刷新数据 tableView.reloadData() } } func searchBarSearchButtonClicked(_ searchBar: UISearchBar) { /// 失去第一响应 searchBar.resignFirstResponder() } } // MARK: - UITableViewDelegate, UITableViewDataSource extension ChoiceGoodsViewController: UITableViewDelegate, UITableViewDataSource { func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) { /// 记录已查询的商品数据 UserDefaultsUtils.setResearchGoodsInfo("\(goodsInfo[indexPath.row].goodsid)") /// 商品切换回掉 self.didSelectGoods(goodsInfo[indexPath.row]) } func tableView(_ tableView: UITableView, heightForRowAt indexPath: IndexPath) -> CGFloat { return 50.0 } func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int { return goodsInfo.count } func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell { let cell = tableView.dequeueReusableCell(withIdentifier: "tableView_cell", for: indexPath) as! ChoiceGoodsTableCell cell.model = goodsInfo[indexPath.row] return cell } } // MARK: - UIScrollViewDelegate extension ChoiceGoodsViewController: UIScrollViewDelegate { func scrollViewDidScroll(_ scrollView: UIScrollView) { /// 失去第一响应 searchBar.resignFirstResponder() } } // MARK: - ChoiceGoodsTableCell class ChoiceGoodsTableCell: BaseTableViewCell { /// 商品名称 @IBOutlet weak var goodsName: UILabel! /// 商品代码 @IBOutlet weak var goodsCode: UILabel! /// 标志 @IBOutlet weak var flag: UILabel! { didSet { flag.layer.cornerRadius = 5.0 flag.layer.borderWidth = 1.0 flag.layer.borderColor = UIColor.white.cgColor flag.layer.masksToBounds = true flag.textAlignment = .center } } /// 自选按钮 @IBOutlet weak var selfButton: UIButton! /// 标志 @IBOutlet weak var flagWidthLayoutConstraint: NSLayoutConstraint! /// 是否为自选 var isSelfGoods = false { didSet { /// 设置 selfButton.setImage(UIImage(named: isSelfGoods ? "self" : "unself"), for: .normal) } } override var model: MoGoodsInfo? { didSet { /// 异常 guard let info = model else { return } /// 商品名称和代码 goodsName.text = info.goodsname goodsCode.text = info.goodscode /// 获取标志 guard let goodsManager = MTP2BusinessCore.shared.goodsManager, let group = goodsManager.goodsGroups?.first(where: {$0.goodsgroupid == info.goodsgroupid}), let exchanges = goodsManager.exchanges?.first(where: {$0.autoid == group.exexchangeid}) else { return } /// 计算宽度 let size = exchanges.exexchangecode.getExchangeCodeVerb().getTextSize(font: .font_14, size: CGSize(width: kScreenWidth, height: 35.0)) flagWidthLayoutConstraint.constant = CGFloat(size.width) + 10.0 flag.text = exchanges.exexchangecode.getExchangeCodeVerb() /// 自选商品 isSelfGoods = goodsManager.favGoodsIds?.contains(where: {$0 == info.goodsid}) ?? false } } // MARK: - 交互相关 /// onButtonPressed /// - Parameter sender: sender @IBAction func onButtonPressed(_ sender: UIControl) { /// 异常 guard let info = model, let goodsManager = MTP2BusinessCore.shared.goodsManager else { return } /// 删自选 if self.isSelfGoods { goodsManager.requestRemoveUserFavoriteGoods(goodsID: info.goodsid, callback: { (isSuccess, _) in DispatchQueue.main.async { self.isSelfGoods = !isSuccess } }) } else { /// 加自选 goodsManager.requestAddUserFavoriteGoods(goodsID: info.goodsid, callback: { (isSuccess, _) in DispatchQueue.main.async { self.isSelfGoods = isSuccess } }) } } }