ContactUnitPickerViewController.swift 6.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206
  1. //
  2. // ContactUnitPickerViewController.swift
  3. // O2Platform
  4. //
  5. // Created by FancyLou on 2019/8/12.
  6. // Copyright © 2019 zoneland. All rights reserved.
  7. //
  8. import CocoaLumberjack
  9. import UIKit
  10. class ContactUnitPickerViewController: UITableViewController {
  11. // MARK: - 需要传入的参数
  12. var topUnitList: [String] = [] //顶级组织
  13. var unitType: String = "" //组织类型 查询组织用的
  14. // MARK: - 私有属性
  15. private var dataList:[OOUnitModel] = []
  16. private var breadcrumbList: [ContactBreadcrumbBean] = []
  17. private var unitParent: String = "-1"
  18. private var unitParentName: String = "通讯录"
  19. private let viewModel: ContactPickerViewModel = {
  20. return ContactPickerViewModel()
  21. }()
  22. override func viewDidLoad() {
  23. super.viewDidLoad()
  24. self.loadData()
  25. }
  26. // MARK: - Table view data source
  27. override func numberOfSections(in tableView: UITableView) -> Int {
  28. return 2
  29. }
  30. override func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
  31. if section == 0 {
  32. return 1
  33. }
  34. return self.dataList.count
  35. }
  36. override func tableView(_ tableView: UITableView, heightForHeaderInSection section: Int) -> CGFloat {
  37. return 10
  38. }
  39. override func tableView(_ tableView: UITableView, heightForFooterInSection section: Int) -> CGFloat {
  40. return 3
  41. }
  42. override func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
  43. if indexPath.section == 0 {
  44. let cell = tableView.dequeueReusableCell(withIdentifier: "breadcrumbViewCell", for: indexPath) as! UnitBreadcrumbViewCell
  45. cell.refreshBreadcrumb(breadcrumbList: self.breadcrumbList)
  46. cell.delegate = self
  47. return cell
  48. }else {
  49. let cell = tableView.dequeueReusableCell(withIdentifier: "unitPickerViewCell", for: indexPath) as! UnitPickerTableViewCell
  50. let unit = dataList[indexPath.row]
  51. cell.loadUnitInfo(info: unit, checked: self.isSelected(value: unit.distinguishedName!))
  52. cell.delegate = self
  53. return cell
  54. }
  55. }
  56. override func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
  57. if indexPath.section == 1 {
  58. let value = dataList[indexPath.row].distinguishedName!
  59. if self.isSelected(value: value) {
  60. self.removeSelected(value: value)
  61. }else {
  62. self.addSelected(dept: dataList[indexPath.row])
  63. }
  64. tableView.reloadRows(at: [indexPath], with: .automatic)
  65. }
  66. self.tableView.deselectRow(at: indexPath, animated: false)
  67. }
  68. //
  69. // // MARK: - UITextViewDelegate
  70. // func textView(_ textView: UITextView, shouldInteractWith URL: URL, in characterRange: NSRange) -> Bool {
  71. // DDLogDebug("url: "+URL.description)
  72. // if let scheme = URL.scheme {
  73. // switch scheme {
  74. // case "reloadto" :
  75. // let id = (URL.description as NSString).substring(from: 9)
  76. // for unit in self.breadcrumbList {
  77. // if id == unit.key {
  78. // self.clickBreadcrumb(bean: unit)
  79. // break;
  80. // }
  81. // }
  82. // default:
  83. // break
  84. // }
  85. // }
  86. // return true
  87. // }
  88. /*
  89. // MARK: - Navigation
  90. // In a storyboard-based application, you will often want to do a little preparation before navigation
  91. override func prepare(for segue: UIStoryboardSegue, sender: Any?) {
  92. // Get the new view controller using segue.destination.
  93. // Pass the selected object to the new view controller.
  94. }
  95. */
  96. // MARK: - private method
  97. //获取组织数据 必须先操作面包屑导航数据
  98. private func loadData() {
  99. self.showLoading()
  100. viewModel.loadUnitList(parent: unitParent, topList: topUnitList, unitType: unitType)
  101. .then { (list) in
  102. DDLogDebug("loadUnitList 结果: \(list.count)")
  103. self.dataList = list
  104. var bean = ContactBreadcrumbBean()
  105. bean.key = self.unitParent
  106. bean.name = self.unitParentName
  107. bean.level = self.breadcrumbList.count
  108. self.breadcrumbList.append(bean)
  109. self.tableView.reloadData()
  110. self.hideLoading()
  111. }.catch { (error) in
  112. DDLogError(error.localizedDescription)
  113. self.hideLoading()
  114. }
  115. }
  116. private func isSelected(value: String) -> Bool {
  117. if let vc = self.parent as? ContactPickerViewController {
  118. return vc.isSelectedValue(type: .unit, value: value)
  119. }
  120. return false
  121. }
  122. private func removeSelected(value: String) {
  123. if let vc = self.parent as? ContactPickerViewController {
  124. vc.removeSelectedValue(type: .unit, value: value)
  125. }
  126. }
  127. private func addSelected(dept: OOUnitModel) {
  128. if let vc = self.parent as? ContactPickerViewController {
  129. vc.addSelectedDept(dept: dept)
  130. }
  131. }
  132. //点击面包屑导航上的组织按钮
  133. // private func clickBreadcrumb(bean: ContactBreadcrumbBean) {
  134. // //清空后面的导航按钮
  135. // for (index,unit) in self.breadcrumbList.enumerated() {
  136. // if unit.key == bean.key {
  137. // let n = self.breadcrumbList.count - index
  138. // self.breadcrumbList.removeLast(n)
  139. // break
  140. // }
  141. // }
  142. // self.unitParentName = bean.name
  143. // self.unitParent = bean.key
  144. // self.loadData()
  145. // }
  146. }
  147. // MARK: - extension delegate
  148. extension ContactUnitPickerViewController : UnitPickerNextBtnDelegate {
  149. //进入下级组织
  150. func next(unitName: String?, unitDistinguishedName: String?) {
  151. DDLogDebug("name: \(String(describing: unitName)) dis:\(String(describing: unitDistinguishedName))")
  152. if unitName == nil || unitDistinguishedName == nil {
  153. DDLogError("参数为空。。。。。")
  154. }else {
  155. self.unitParentName = unitName!
  156. self.unitParent = unitDistinguishedName!
  157. self.loadData()
  158. }
  159. }
  160. }
  161. extension ContactUnitPickerViewController: UnitPickerBreadcrumbClickDelegate {
  162. //点击面包屑导航上的组织按钮
  163. func breadcrumbTap(name: String, distinguished: String) {
  164. //清空后面的导航按钮
  165. for (index,unit) in self.breadcrumbList.enumerated() {
  166. if unit.key == distinguished {
  167. let n = self.breadcrumbList.count - index
  168. self.breadcrumbList.removeLast(n)
  169. break
  170. }
  171. }
  172. self.unitParentName = name
  173. self.unitParent = distinguished
  174. self.loadData()
  175. }
  176. }