ContactIdentityPickerViewController.swift 7.9 KB

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