WareHouseViewController.swift 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306
  1. //
  2. // WareHouseViewController.swift
  3. // MTP2_iOS
  4. //
  5. // Created by Muchinfo on 2021/2/26.
  6. // Copyright © 2021 Muchinfo. All rights reserved.
  7. //
  8. import UIKit
  9. import JXSegmentedView
  10. import IBAnimatable
  11. import WHToast
  12. import GTMRefresh
  13. import SwiftyAttributes
  14. /// 仓库信息视图容器控制类
  15. class WareHouseViewController: BaseViewController {
  16. // MARK: - 属性列表
  17. /// 列表
  18. @IBOutlet weak var tableView: UITableView!
  19. /// 类目
  20. @IBOutlet weak var segmentedView: JXSegmentedView! {
  21. didSet {
  22. segmentedView.dataSource = dataSource
  23. segmentedView.indicators = [indicator]
  24. segmentedView.delegate = self
  25. }
  26. }
  27. /// CellIdentifier
  28. let CellIdentifier = "WareHouse_Cell"
  29. /// 选中的位置
  30. var selectedIndex: (row: Int, isExpland: Bool)? {
  31. didSet {
  32. if let index = selectedIndex {
  33. /// 刷新某一行
  34. tableView.reloadRows(at: [IndexPath(row: index.row, section: 0)], with: .automatic)
  35. } else {
  36. if let cell = tableView.visibleCells.first(where: { (obj) -> Bool in
  37. return (obj as? WareHouseCell)?.isExpland ?? false
  38. }) as? BusinessManagerCell {
  39. cell.isExpland = false
  40. }
  41. }
  42. if let old = oldValue,
  43. let cell = (tableView.cellForRow(at: IndexPath(row: old.row, section: 0)) as? WareHouseCell) {
  44. cell.isExpland = false
  45. }
  46. }
  47. }
  48. /// 仓库信息
  49. var wareHouses: [MoWarehouse] = [] {
  50. didSet {
  51. /// 刷新列表数据
  52. tableView.reloadData()
  53. /// noDataButton
  54. noDataButton.isHidden = wareHouses.count != 0
  55. }
  56. }
  57. /// 权限功能
  58. var roleTitles: [String] {
  59. get {
  60. var titles: [String] = []
  61. /// 权限控制
  62. guard let commonManager = MTP2BusinessCore.shared.commonManager else {
  63. return titles
  64. }
  65. if commonManager.isContainTraderMenu(key: "client_warehouse_normal") {
  66. titles.append("正常")
  67. }
  68. if commonManager.isContainTraderMenu(key: "client_warehouse_stop") {
  69. titles.append("停用")
  70. }
  71. return titles
  72. }
  73. }
  74. // MARK: - 生命周期
  75. override func viewDidLoad() {
  76. super.viewDidLoad()
  77. // Do any additional setup after loading the view.
  78. }
  79. override func loadView() {
  80. super.loadView()
  81. /// titles
  82. dataSource.titles = roleTitles
  83. segmentedView.reloadData()
  84. /// 判断其权限控制
  85. guard let commonManager = MTP2BusinessCore.shared.commonManager else { return }
  86. /// 没有新增仓库权限
  87. if !commonManager.isContainTraderMenu(key: "client_warehouse_add") {
  88. self.navigationItem.rightBarButtonItem?.image = nil
  89. self.navigationItem.rightBarButtonItem?.isEnabled = false
  90. } else {
  91. self.navigationItem.rightBarButtonItem?.image = UIImage(named: "new_contract")
  92. self.navigationItem.rightBarButtonItem?.isEnabled = true
  93. }
  94. }
  95. override func buildView() {
  96. super.buildView()
  97. /// loding......
  98. addLoadingView()
  99. if roleTitles.count != 0 {
  100. /// 添加下拉刷新控件
  101. tableView.gtm_addRefreshHeaderView(refreshHeader: DefaultGTMRefreshHeader()) {
  102. switch self.roleTitles[self.segmentedView.selectedIndex] {
  103. case "正常": /// 正常
  104. self.queryWareHouses("1")
  105. default: /// 停用
  106. self.queryWareHouses("2")
  107. }
  108. self.selectedIndex = nil
  109. }
  110. /// 默认选中第一个
  111. segmentedView.defaultSelectedIndex = 0
  112. segmentedView.delegate?.segmentedView(segmentedView, didSelectedItemAt: 0)
  113. }
  114. }
  115. // MARK: - 接口请求
  116. /// 查询套保计划
  117. /// - Parameter status: 1:正常 2:注销 3:待审核 4:审核拒绝
  118. fileprivate func queryWareHouses(_ status: String) {
  119. /// 异常
  120. guard let wareHouseManager = MTP2BusinessCore.shared.wareHouseManager else {
  121. return
  122. }
  123. /// startAnimating
  124. _anim?.startAnimating()
  125. /// 发送请求
  126. wareHouseManager.queryWarehouseInfo(status: status) { (isComplete, er, objs) in
  127. DispatchQueue.main.async {
  128. /// stopAnimating
  129. self._anim?.stopAnimating()
  130. /// endRefreshing
  131. self.tableView.endRefreshing()
  132. /// 查询失败
  133. if !isComplete {
  134. self.wareHouses = []
  135. WHToast.showMessage("数据查询失败,原因:\(er?.retMsg ?? "请求超时")", duration: 1.5, finishHandler: {})
  136. return
  137. }
  138. /// 数据赋值
  139. self.wareHouses = objs ?? []
  140. }
  141. }
  142. }
  143. // MARK: - Navigation
  144. // In a storyboard-based application, you will often want to do a little preparation before navigation
  145. override func prepare(for segue: UIStoryboardSegue, sender: Any?) {
  146. // Get the new view controller using segue.destination.
  147. // Pass the selected object to the new view controller.
  148. if segue.identifier == "ShowNewWareHouse", let button = sender as? UIButton, button.currentTitle == "修改", let new = segue.destination as? NewWareHouseViewController, let row = selectedIndex?.row {
  149. /// 修改仓库
  150. new.operatorType = .update
  151. new.moWareHouse = wareHouses[row]
  152. new.popBlock = { (_ takeInfo: Any?) in
  153. if let _ = takeInfo as? OperatorType {
  154. /// 跳转到待审核
  155. self.segmentedView.selectItemAt(index: 0)
  156. /// didSelectedItemAt
  157. self.tableView.triggerRefreshing()
  158. }
  159. }
  160. } else if segue.identifier == "ShowNewWareHouse", let new = segue.destination as? NewWareHouseViewController {
  161. /// 新增仓库
  162. new.operatorType = .add
  163. new.popBlock = { (_ takeInfo: Any?) in
  164. if let _ = takeInfo as? OperatorType {
  165. /// 跳转到待审核
  166. self.segmentedView.selectItemAt(index: 0)
  167. /// didSelectedItemAt
  168. self.tableView.triggerRefreshing()
  169. }
  170. }
  171. } else if segue.identifier == "ShowWareHouseDetail", let detail = segue.destination as? WareHouseDetailViewController, let button = sender as? UIButton, let row = selectedIndex?.row {
  172. /// 导航栏标题
  173. detail.title = button.currentTitle
  174. detail.operatorType = button.currentTitle == "停用" ? .stop : .reused
  175. detail.moWareHouse = wareHouses[row]
  176. detail.popBlock = { (_ takeInfo: Any?) in
  177. if let type = takeInfo as? OperatorType {
  178. /// 跳转到待审核
  179. self.segmentedView.selectItemAt(index: type == .stop ? 1 : 0)
  180. /// didSelectedItemAt
  181. self.tableView.triggerRefreshing()
  182. }
  183. }
  184. }
  185. }
  186. }
  187. // MARK: - JXSegmentedViewDelegate
  188. extension WareHouseViewController: JXSegmentedViewDelegate {
  189. /// didSelectedItemAt
  190. /// - Parameters:
  191. /// - segmentedView: segmentedView
  192. /// - index: index
  193. func segmentedView(_ segmentedView: JXSegmentedView, didSelectedItemAt index: Int) {
  194. switch roleTitles[index] {
  195. case "正常":
  196. /// 正常
  197. queryWareHouses("1")
  198. default:
  199. /// 停用
  200. queryWareHouses("2")
  201. }
  202. selectedIndex = nil
  203. }
  204. }
  205. extension WareHouseViewController: UITableViewDelegate, UITableViewDataSource {
  206. func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
  207. return wareHouses.count
  208. }
  209. func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
  210. let cell = tableView.dequeueReusableCell(withIdentifier: CellIdentifier, for: indexPath) as! WareHouseCell
  211. cell.model = wareHouses[indexPath.row]
  212. return cell
  213. }
  214. func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
  215. if indexPath.row == (selectedIndex?.row ?? 0) {
  216. selectedIndex = (row: indexPath.row, !(selectedIndex?.isExpland ?? false))
  217. } else {
  218. selectedIndex = (row: indexPath.row, true)
  219. }
  220. (tableView.cellForRow(at: indexPath) as! WareHouseCell).isExpland = selectedIndex?.isExpland ?? false
  221. }
  222. func tableView(_ tableView: UITableView, heightForRowAt indexPath: IndexPath) -> CGFloat {
  223. return (selectedIndex?.row == indexPath.row && selectedIndex?.isExpland == true) ? 90.0 : 55.0
  224. }
  225. }
  226. class WareHouseCell: BaseTableViewCell<MoWarehouse> {
  227. /// 状态
  228. @IBOutlet weak var status: UILabel!
  229. /// 仓库名称
  230. @IBOutlet weak var wareHouseName: UILabel!
  231. /// 仓库简称
  232. @IBOutlet weak var wareHouseShortName: UILabel!
  233. /// 仓库类型
  234. @IBOutlet weak var type: UILabel!
  235. /// 箭头
  236. @IBOutlet weak var arrow: AnimatableImageView!
  237. /// 恢复
  238. @IBOutlet weak var restore: UIButton!
  239. /// 修改
  240. @IBOutlet weak var update: UIButton!
  241. /// 停用
  242. @IBOutlet weak var stop: UIButton!
  243. /// 详情
  244. @IBOutlet weak var detail: UIButton!
  245. /// 是否拓展
  246. override var isExpland: Bool {
  247. didSet {
  248. arrow.image = UIImage(named: isExpland ? "arrow_up" : "arrow_down")
  249. }
  250. }
  251. /// 数据模型
  252. override var model: MoWarehouse? {
  253. didSet {
  254. /// 数据异常
  255. guard let commonManager = MTP2BusinessCore.shared.commonManager,
  256. let obj = model else {
  257. return
  258. }
  259. /// 状态
  260. status.text = obj.warehousestatus.description
  261. /// 仓库名称
  262. wareHouseName.text = obj.warehousename.isBlank()
  263. /// 仓库简称
  264. wareHouseShortName.text = obj.warehousecode.isBlank()
  265. /// 仓库类型
  266. type.text = obj.warehousetype.description
  267. /// 判断其权限控制
  268. switch obj.warehousestatus {
  269. case .normal: /// 正常
  270. update.isHidden = !commonManager.isContainTraderMenu(key: "client_warehouse_update")
  271. stop.isHidden = !commonManager.isContainTraderMenu(key: "client_warehouse_stop")
  272. restore.isHidden = true
  273. default: /// 其他状态
  274. update.isHidden = true
  275. stop.isHidden = true
  276. restore.isHidden = !commonManager.isContainTraderMenu(key: "client_warehouse_restore")
  277. }
  278. }
  279. }
  280. }