OOMeetingCreateViewModel.swift 5.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173
  1. //
  2. // OOMeetingCreateViewModel.swift
  3. // o2app
  4. //
  5. // Created by 刘振兴 on 2018/1/26.
  6. // Copyright © 2018年 zone. All rights reserved.
  7. //
  8. import UIKit
  9. class OOMeetingCreateViewModel: NSObject {
  10. //Meeting API
  11. private let ooMeetingAPI = OOMoyaProvider<O2MeetingAPI>()
  12. //Contact API
  13. private let ooContactAPI = OOMoyaProvider<OOContactAPI>()
  14. private var persons:[OOPersonModel] = []
  15. var selectedPersons:[OOPersonModel] = []
  16. typealias CallDefine = (_ msg:String?) -> Void
  17. //人员列表回调
  18. var contactCallBlock:CallDefine?
  19. //选择人员列表回调
  20. var selectedContactCallBlock:CallDefine?
  21. override init() {
  22. super.init()
  23. }
  24. }
  25. extension OOMeetingCreateViewModel{
  26. func getLastPerson() -> OOPersonModel? {
  27. return self.persons.last ?? nil
  28. }
  29. //所有用户列表
  30. func getAllPerson(_ next:String?){
  31. if let nextId = next {
  32. ooContactAPI.request(.personListNext(nextId, 20)) { (result) in
  33. let myResult = OOResult<BaseModelClass<[OOPersonModel]>>(result)
  34. if myResult.isResultSuccess() {
  35. if let model = myResult.model?.data {
  36. model.forEach({ (item) in
  37. self.persons.append(item)
  38. })
  39. }
  40. }
  41. guard let block = self.contactCallBlock else {
  42. return
  43. }
  44. if myResult.isResultSuccess() {
  45. block(nil)
  46. }else{
  47. block(myResult.error?.errorDescription)
  48. }
  49. }
  50. }else{
  51. self.persons.removeAll()
  52. ooContactAPI.request(.personListNext("(0)", 20)) { (result) in
  53. let myResult = OOResult<BaseModelClass<[OOPersonModel]>>(result)
  54. if myResult.isResultSuccess() {
  55. if let model = myResult.model?.data {
  56. model.forEach({ (item) in
  57. self.persons.append(item)
  58. })
  59. }
  60. }
  61. guard let block = self.contactCallBlock else {
  62. return
  63. }
  64. if myResult.isResultSuccess() {
  65. block(nil)
  66. }else{
  67. block(myResult.error?.errorDescription)
  68. }
  69. }
  70. }
  71. }
  72. // MARK: - 获取icon
  73. func getIconOfPerson(_ person:OOPersonModel,compeletionBlock:@escaping (_ image:UIImage?,_ errMsg:String?) -> Void) {
  74. ooContactAPI.request(.iconByPerson(person.distinguishedName!)) { (result) in
  75. if let err = result.error {
  76. compeletionBlock(#imageLiteral(resourceName: "icon_?"),err.errorDescription)
  77. }else{
  78. let data = result.value?.data
  79. guard let image = UIImage(data: data!) else {
  80. compeletionBlock(#imageLiteral(resourceName: "icon_?"),"image transform error")
  81. return
  82. }
  83. compeletionBlock(image,nil)
  84. }
  85. }
  86. }
  87. //创建会议
  88. func createMeetingAction(_ meeting:OOMeetingFormBean,completedBlock:@escaping (_ returnMessage:String?) -> Void){
  89. ooMeetingAPI.request(.meetingItemByCreate(meeting)) { (result) in
  90. let myResult = OOResult<BaseModelClass<[OOCommonModel]>>(result)
  91. if myResult.isResultSuccess() {
  92. completedBlock(nil)
  93. }else{
  94. completedBlock(myResult.error?.errorDescription)
  95. }
  96. }
  97. }
  98. //表单模型
  99. func getFormModels() -> [OOFormBaseModel] {
  100. let titleModel = OOFormBaseModel(titleName: "会议主题", key: "subject", componentType: .textItem, itemStatus: .edit)
  101. let dateModel = OOFormBaseModel(titleName: "会议日期", key: "date", componentType: .dateItem, itemStatus: .edit)
  102. let dateIntervalModel = OOFormDateIntervalModel(titleName: "会议时间", key: "dateInterval", componentType: .dateIntervalItem, itemStatus: .edit)
  103. let segueModel = OOFormSegueItemModel(titleName: "会议室", key: "room", componentType: .segueItem, itemStatus: .edit)
  104. segueModel.segueIdentifier = "OOMeetingMeetingRoomManageController"
  105. return [titleModel,dateModel,dateIntervalModel,segueModel]
  106. }
  107. }
  108. // MARK:- 选择的人员列表
  109. extension OOMeetingCreateViewModel{
  110. func addSelectPerson(_ p:OOPersonModel){
  111. self.selectedPersons.append(p)
  112. }
  113. func removeSelectPerson(_ p:OOPersonModel){
  114. if let i = self.selectedPersons.firstIndex(of: p) {
  115. self.selectedPersons.remove(at:i)
  116. }
  117. }
  118. func refreshData(){
  119. guard let block = self.selectedContactCallBlock else {
  120. return
  121. }
  122. block(nil)
  123. }
  124. func collectionViewNumberOfSections() -> Int {
  125. return 1
  126. }
  127. func collectionViewNumberOfRowsInSection(_ section: Int) -> Int {
  128. return selectedPersons.count + 1
  129. }
  130. func collectionViewNodeForIndexPath(_ indexPath:IndexPath) -> OOPersonModel? {
  131. if indexPath.row < selectedPersons.count {
  132. return selectedPersons[indexPath.row]
  133. }else{
  134. return nil
  135. }
  136. }
  137. }
  138. // MARK:- 人员列表
  139. extension OOMeetingCreateViewModel {
  140. func tableViewNumberOfSections() -> Int {
  141. return 1
  142. }
  143. func tableViewNumberOfRowsInSection(_ section: Int) -> Int {
  144. return persons.count
  145. }
  146. func tableViewNodeForIndexPath(_ indexPath:IndexPath) -> OOPersonModel? {
  147. return persons[indexPath.row]
  148. }
  149. }