ContactPickerViewModel.swift 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291
  1. //
  2. // ContactPickerViewModel.swift
  3. // O2Platform
  4. //
  5. // Created by FancyLou on 2019/8/13.
  6. // Copyright © 2019 zoneland. All rights reserved.
  7. //
  8. import Promises
  9. class ContactPickerViewModel: NSObject {
  10. override init() {
  11. super.init()
  12. }
  13. private let orgControlAPI = OOMoyaProvider<OOContactAPI>()
  14. private let orgExpressAPI = OOMoyaProvider<OOContactExpressAPI>()
  15. }
  16. extension ContactPickerViewModel {
  17. func getPersonInfo(dn: String) -> Promise<OOPersonModel> {
  18. return Promise { fulfill, reject in
  19. self.orgControlAPI.request(.getPerson(dn)) { (result) in
  20. let myResult = OOResult<BaseModelClass<OOPersonModel>>(result)
  21. if myResult.isResultSuccess() {
  22. if let data = myResult.model?.data {
  23. fulfill(data)
  24. }else {
  25. reject(OOAppError.jsonMapping(message: "返回数据为空!!", statusCode: 1024, data: nil))
  26. }
  27. } else {
  28. reject(myResult.error!)
  29. }
  30. }
  31. }
  32. }
  33. //
  34. // 组织查询
  35. // @param parent 上级组织id -1就是顶层
  36. // @param topList 顶级组织id 列表
  37. // @param unitType 组织类型
  38. //
  39. func loadUnitList(parent: String, topList: [String] = [], unitType: String = "") -> Promise<[OOUnitModel]> {
  40. if parent == "-1" { //顶层
  41. if unitType.isEmpty {
  42. if topList.isEmpty {
  43. return self.unitListTop()
  44. }else {
  45. return self.unitListByIds(distinguishedNameList: topList)
  46. }
  47. }else {
  48. if topList.isEmpty {
  49. return self.unitListByType(type: unitType, parentUnit: nil)
  50. }else {//这里没有查询 toplist过滤 组织类型的接口
  51. return self.unitListByIds(distinguishedNameList: topList)
  52. }
  53. }
  54. }else {
  55. if unitType.isEmpty {
  56. return self.unitSubList(parent: parent)
  57. }else {
  58. return self.unitListByType(type: unitType, parentUnit: parent)
  59. }
  60. }
  61. }
  62. //
  63. // 身份查询
  64. // @param unit 上级组织id
  65. // @param dutyList 过滤的职责
  66. //
  67. func loadIdentityList(dutyList:[String], unit:String) -> Promise<[OOIdentityModel]> {
  68. if dutyList.isEmpty {
  69. return self.identityListByUnit(unit: unit)
  70. }else {
  71. return self.identityListByUnitFilterDutyList(unit: unit, dutyList: dutyList)
  72. }
  73. }
  74. //
  75. // 分页查询群组 每页20条
  76. // @param lastId
  77. //
  78. func loadGroupList(lastId: String) -> Promise<[OOGroupModel]> {
  79. return Promise{ fulfill, reject in
  80. self.orgControlAPI.request(.groupListNext(lastId, 20), completion: { (result) in
  81. let response = OOResult<BaseModelClass<[OOGroupModel]>>(result)
  82. if response.isResultSuccess() {
  83. if let data = response.model?.data {
  84. fulfill(data)
  85. }else {
  86. reject(OOAppError.jsonMapping(message: "返回数据为空!!", statusCode: 1024, data: nil))
  87. }
  88. }else {
  89. reject(response.error!)
  90. }
  91. })
  92. }
  93. }
  94. //
  95. // 搜索查询人员
  96. //
  97. func searchPersonList(searchText: String) -> Promise<[OOPersonModel]> {
  98. return Promise{ fulfill, reject in
  99. self.orgControlAPI.request(.personLike(searchText), completion: { (result) in
  100. let response = OOResult<BaseModelClass<[OOPersonModel]>>(result)
  101. if response.isResultSuccess() {
  102. if let data = response.model?.data {
  103. fulfill(data)
  104. }else {
  105. reject(OOAppError.jsonMapping(message: "返回数据为空!!", statusCode: 1024, data: nil))
  106. }
  107. }else {
  108. reject(response.error!)
  109. }
  110. })
  111. }
  112. }
  113. //获取顶级组织列表
  114. private func unitListTop() -> Promise<[OOUnitModel]> {
  115. return Promise { fulfill, reject in
  116. self.orgControlAPI.request(.listTop, completion: { (result) in
  117. let response = OOResult<BaseModelClass<[OOUnitModel]>>(result)
  118. if response.isResultSuccess() {
  119. if let data = response.model?.data {
  120. fulfill(data)
  121. }else {
  122. reject(OOAppError.jsonMapping(message: "返回数据为空!!", statusCode: 1024, data: nil))
  123. }
  124. }else {
  125. reject(response.error!)
  126. }
  127. })
  128. }
  129. }
  130. //根据父获取子组织列表
  131. private func unitSubList(parent: String) -> Promise<[OOUnitModel]> {
  132. return Promise { fulfill, reject in
  133. self.orgControlAPI.request(.listSubDirect(parent), completion: { (result) in
  134. let response = OOResult<BaseModelClass<[OOUnitModel]>>(result)
  135. if response.isResultSuccess() {
  136. if let data = response.model?.data {
  137. fulfill(data)
  138. }else {
  139. reject(OOAppError.jsonMapping(message: "返回数据为空!!", statusCode: 1024, data: nil))
  140. }
  141. }else {
  142. reject(response.error!)
  143. }
  144. })
  145. }
  146. }
  147. //根据id列表获取组织对象列表
  148. private func unitListByIds(distinguishedNameList: [String]) -> Promise<[OOUnitModel]> {
  149. return Promise { fulfill, reject in
  150. self.orgControlAPI.request(.unitList(distinguishedNameList), completion: { (result) in
  151. let response = OOResult<BaseModelClass<[OOUnitModel]>>(result)
  152. if response.isResultSuccess() {
  153. if let data = response.model?.data {
  154. fulfill(data)
  155. }else {
  156. reject(OOAppError.jsonMapping(message: "返回数据为空!!", statusCode: 1024, data: nil))
  157. }
  158. }else {
  159. reject(response.error!)
  160. }
  161. })
  162. }
  163. }
  164. //根据组织类型返回组织列表,parentUnit为空就是返回顶级组织列表
  165. private func unitListByType(type: String, parentUnit: String?) -> Promise<[OOUnitModel]> {
  166. var unitList: [String] = []
  167. if parentUnit != nil && parentUnit?.isEmpty != true {
  168. unitList = [parentUnit!]
  169. }
  170. return Promise { fulfill, reject in
  171. self.orgControlAPI.request(.unitListByType(type, unitList), completion: { (result) in
  172. let response = OOResult<BaseModelClass<[OOUnitModel]>>(result)
  173. if response.isResultSuccess() {
  174. if let data = response.model?.data {
  175. fulfill(data)
  176. }else {
  177. reject(OOAppError.jsonMapping(message: "返回数据为空!!", statusCode: 1024, data: nil))
  178. }
  179. }else {
  180. reject(response.error!)
  181. }
  182. })
  183. }
  184. }
  185. //e根据组织查询身份列表
  186. private func identityListByUnit(unit: String) -> Promise<[OOIdentityModel]> {
  187. return Promise{ fulfill, reject in
  188. self.orgControlAPI.request(.identityListByUnit(unit), completion: { (result) in
  189. let response = OOResult<BaseModelClass<[OOIdentityModel]>>(result)
  190. if response.isResultSuccess() {
  191. if let data = response.model?.data {
  192. //person id 换 DN 人员选择的时候需要
  193. let idList = data.map({ (oo) -> String in
  194. return oo.person ?? ""
  195. })
  196. if idList.isEmpty {
  197. fulfill(data)
  198. }else {
  199. self.orgExpressAPI.request(.personListDN(idList), completion: { (dnResult) in
  200. let dnResponse = OOResult<BaseModelClass<OOPersonDNModel>>(dnResult)
  201. if dnResponse.isResultSuccess() {
  202. if let dnList = dnResponse.model?.data?.personList {
  203. var index = 0
  204. let newData = data.map({ (ooId) -> OOIdentityModel in
  205. ooId.person = dnList[index]
  206. index += 1
  207. return ooId
  208. })
  209. fulfill(newData)
  210. }else {
  211. fulfill(data)
  212. }
  213. }else {
  214. fulfill(data)
  215. }
  216. })
  217. }
  218. }else {
  219. reject(OOAppError.jsonMapping(message: "返回数据为空!!", statusCode: 1024, data: nil))
  220. }
  221. }else {
  222. reject(response.error!)
  223. }
  224. })
  225. }
  226. }
  227. private func identityListByUnitFilterDutyList(unit: String, dutyList:[String]) -> Promise<[OOIdentityModel]> {
  228. return Promise{ fulfill, reject in
  229. self.orgExpressAPI.request(.identityListByUnitAndDuty(dutyList, unit), completion: { (result) in
  230. let response = OOResult<BaseModelClass<[OOIdentityModel]>>(result)
  231. if response.isResultSuccess() {
  232. if let data = response.model?.data {
  233. //person id 换 DN 人员选择的时候需要
  234. let idList = data.map({ (oo) -> String in
  235. return oo.person ?? ""
  236. })
  237. if idList.isEmpty {
  238. fulfill(data)
  239. }else {
  240. self.orgExpressAPI.request(.personListDN(idList), completion: { (dnResult) in
  241. let dnResponse = OOResult<BaseModelClass<OOPersonDNModel>>(dnResult)
  242. if dnResponse.isResultSuccess() {
  243. if let dnList = dnResponse.model?.data?.personList {
  244. var index = 0
  245. let newData = data.map({ (ooId) -> OOIdentityModel in
  246. ooId.person = dnList[index]
  247. index += 1
  248. return ooId
  249. })
  250. fulfill(newData)
  251. }else {
  252. fulfill(data)
  253. }
  254. }else {
  255. fulfill(data)
  256. }
  257. })
  258. }
  259. }else {
  260. reject(OOAppError.jsonMapping(message: "返回数据为空!!", statusCode: 1024, data: nil))
  261. }
  262. }else {
  263. reject(response.error!)
  264. }
  265. })
  266. }
  267. }
  268. }