// // WareHouseViewController.swift // MTP2_iOS // // Created by Muchinfo on 2021/2/26. // Copyright © 2021 Muchinfo. All rights reserved. // import UIKit import JXSegmentedView import IBAnimatable import WHToast import GTMRefresh import SwiftyAttributes /// 仓库信息视图容器控制类 class WareHouseViewController: BaseViewController { // MARK: - 属性列表 /// 列表 @IBOutlet weak var tableView: UITableView! /// 类目 @IBOutlet weak var segmentedView: JXSegmentedView! { didSet { segmentedView.dataSource = dataSource segmentedView.indicators = [indicator] segmentedView.delegate = self } } /// CellIdentifier let CellIdentifier = "WareHouse_Cell" /// 选中的位置 var selectedIndex: (row: Int, isExpland: Bool)? { didSet { if let index = selectedIndex { /// 刷新某一行 tableView.reloadRows(at: [IndexPath(row: index.row, section: 0)], with: .automatic) } else { if let cell = tableView.visibleCells.first(where: { (obj) -> Bool in return (obj as? WareHouseCell)?.isExpland ?? false }) as? BusinessManagerCell { cell.isExpland = false } } if let old = oldValue, let cell = (tableView.cellForRow(at: IndexPath(row: old.row, section: 0)) as? WareHouseCell) { cell.isExpland = false } } } /// 仓库信息 var wareHouses: [MoWarehouse] = [] { didSet { /// 刷新列表数据 tableView.reloadData() /// noDataButton noDataButton.isHidden = wareHouses.count != 0 } } /// 权限功能 var roleTitles: [String] { get { var titles: [String] = [] /// 权限控制 guard let commonManager = MTP2BusinessCore.shared.commonManager else { return titles } if commonManager.isContainTraderMenu(key: "client_warehouse_normal") { titles.append("正常") } if commonManager.isContainTraderMenu(key: "client_warehouse_stop") { titles.append("停用") } return titles } } // MARK: - 生命周期 override func viewDidLoad() { super.viewDidLoad() // Do any additional setup after loading the view. } override func loadView() { super.loadView() /// titles dataSource.titles = roleTitles segmentedView.reloadData() /// 判断其权限控制 guard let commonManager = MTP2BusinessCore.shared.commonManager else { return } /// 没有新增仓库权限 if !commonManager.isContainTraderMenu(key: "client_warehouse_add") { self.navigationItem.rightBarButtonItem?.image = nil self.navigationItem.rightBarButtonItem?.isEnabled = false } else { self.navigationItem.rightBarButtonItem?.image = UIImage(named: "new_contract") self.navigationItem.rightBarButtonItem?.isEnabled = true } } override func buildView() { super.buildView() /// loding...... addLoadingView() if roleTitles.count != 0 { /// 添加下拉刷新控件 tableView.gtm_addRefreshHeaderView(refreshHeader: DefaultGTMRefreshHeader()) { switch self.roleTitles[self.segmentedView.selectedIndex] { case "正常": /// 正常 self.queryWareHouses("1") default: /// 停用 self.queryWareHouses("2") } self.selectedIndex = nil } /// 默认选中第一个 segmentedView.defaultSelectedIndex = 0 segmentedView.delegate?.segmentedView(segmentedView, didSelectedItemAt: 0) } } // MARK: - 接口请求 /// 查询套保计划 /// - Parameter status: 1:正常 2:注销 3:待审核 4:审核拒绝 fileprivate func queryWareHouses(_ status: String) { /// 异常 guard let wareHouseManager = MTP2BusinessCore.shared.wareHouseManager else { return } /// startAnimating _anim?.startAnimating() /// 发送请求 wareHouseManager.queryWarehouseInfo(status: status) { (isComplete, er, objs) in DispatchQueue.main.async { /// stopAnimating self._anim?.stopAnimating() /// endRefreshing self.tableView.endRefreshing() /// 查询失败 if !isComplete { self.wareHouses = [] WHToast.showMessage("数据查询失败,原因:\(er?.retMsg ?? "请求超时")", duration: 1.5, finishHandler: {}) return } /// 数据赋值 self.wareHouses = objs ?? [] } } } // 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 == "ShowNewWareHouse", let button = sender as? UIButton, button.currentTitle == "修改", let new = segue.destination as? NewWareHouseViewController, let row = selectedIndex?.row { /// 修改仓库 new.operatorType = .update new.moWareHouse = wareHouses[row] new.popBlock = { (_ takeInfo: Any?) in if let _ = takeInfo as? OperatorType { /// 跳转到待审核 self.segmentedView.selectItemAt(index: 0) /// didSelectedItemAt self.tableView.triggerRefreshing() } } } else if segue.identifier == "ShowNewWareHouse", let new = segue.destination as? NewWareHouseViewController { /// 新增仓库 new.operatorType = .add new.popBlock = { (_ takeInfo: Any?) in if let _ = takeInfo as? OperatorType { /// 跳转到待审核 self.segmentedView.selectItemAt(index: 0) /// didSelectedItemAt self.tableView.triggerRefreshing() } } } else if segue.identifier == "ShowWareHouseDetail", let detail = segue.destination as? WareHouseDetailViewController, let button = sender as? UIButton, let row = selectedIndex?.row { /// 导航栏标题 detail.title = button.currentTitle detail.operatorType = button.currentTitle == "停用" ? .stop : .reused detail.moWareHouse = wareHouses[row] detail.popBlock = { (_ takeInfo: Any?) in if let type = takeInfo as? OperatorType { /// 跳转到待审核 self.segmentedView.selectItemAt(index: type == .stop ? 1 : 0) /// didSelectedItemAt self.tableView.triggerRefreshing() } } } } } // MARK: - JXSegmentedViewDelegate extension WareHouseViewController: JXSegmentedViewDelegate { /// didSelectedItemAt /// - Parameters: /// - segmentedView: segmentedView /// - index: index func segmentedView(_ segmentedView: JXSegmentedView, didSelectedItemAt index: Int) { switch roleTitles[index] { case "正常": /// 正常 queryWareHouses("1") default: /// 停用 queryWareHouses("2") } selectedIndex = nil } } extension WareHouseViewController: UITableViewDelegate, UITableViewDataSource { func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int { return wareHouses.count } func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell { let cell = tableView.dequeueReusableCell(withIdentifier: CellIdentifier, for: indexPath) as! WareHouseCell cell.model = wareHouses[indexPath.row] return cell } func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) { if indexPath.row == (selectedIndex?.row ?? 0) { selectedIndex = (row: indexPath.row, !(selectedIndex?.isExpland ?? false)) } else { selectedIndex = (row: indexPath.row, true) } (tableView.cellForRow(at: indexPath) as! WareHouseCell).isExpland = selectedIndex?.isExpland ?? false } func tableView(_ tableView: UITableView, heightForRowAt indexPath: IndexPath) -> CGFloat { return (selectedIndex?.row == indexPath.row && selectedIndex?.isExpland == true) ? 90.0 : 55.0 } } class WareHouseCell: BaseTableViewCell { /// 状态 @IBOutlet weak var status: UILabel! /// 仓库名称 @IBOutlet weak var wareHouseName: UILabel! /// 仓库简称 @IBOutlet weak var wareHouseShortName: UILabel! /// 仓库类型 @IBOutlet weak var type: UILabel! /// 箭头 @IBOutlet weak var arrow: AnimatableImageView! /// 恢复 @IBOutlet weak var restore: UIButton! /// 修改 @IBOutlet weak var update: UIButton! /// 停用 @IBOutlet weak var stop: UIButton! /// 详情 @IBOutlet weak var detail: UIButton! /// 是否拓展 override var isExpland: Bool { didSet { arrow.image = UIImage(named: isExpland ? "arrow_up" : "arrow_down") } } /// 数据模型 override var model: MoWarehouse? { didSet { /// 数据异常 guard let commonManager = MTP2BusinessCore.shared.commonManager, let obj = model else { return } /// 状态 status.text = obj.warehousestatus.description /// 仓库名称 wareHouseName.text = obj.warehousename.isBlank() /// 仓库简称 wareHouseShortName.text = obj.warehousecode.isBlank() /// 仓库类型 type.text = obj.warehousetype.description /// 判断其权限控制 switch obj.warehousestatus { case .normal: /// 正常 update.isHidden = !commonManager.isContainTraderMenu(key: "client_warehouse_update") stop.isHidden = !commonManager.isContainTraderMenu(key: "client_warehouse_stop") restore.isHidden = true default: /// 其他状态 update.isHidden = true stop.isHidden = true restore.isHidden = !commonManager.isContainTraderMenu(key: "client_warehouse_restore") } } } }