OOMeetingCreateViewModel.swift 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309
  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. switch result {
  76. case .success(let res):
  77. guard let image = UIImage(data: res.data) else {
  78. compeletionBlock(#imageLiteral(resourceName: "icon_?"),"image transform error")
  79. return
  80. }
  81. compeletionBlock(image,nil)
  82. break
  83. case .failure(let err):
  84. compeletionBlock(#imageLiteral(resourceName: "icon_?"),err.errorDescription)
  85. break
  86. }
  87. }
  88. }
  89. //创建会议
  90. func createMeetingAction(_ meeting:OOMeetingFormBean,completedBlock:@escaping (_ returnMessage:String?) -> Void){
  91. ooMeetingAPI.request(.meetingItemByCreate(meeting)) { (result) in
  92. let myResult = OOResult<BaseModelClass<OOCommonIdModel>>(result)
  93. if myResult.isResultSuccess() {
  94. completedBlock(nil)
  95. }else{
  96. completedBlock(myResult.error?.errorDescription)
  97. }
  98. }
  99. }
  100. /// 创建会议
  101. func createMeetingActionNew(_ meeting:OOMeetingInfo, completedBlock:@escaping (_ returnMessage:String?, _ meetingId: String?) -> Void){
  102. ooMeetingAPI.request(.meetingCreate(meeting)) { (result) in
  103. let myResult = OOResult<BaseModelClass<OOCommonIdModel>>(result)
  104. if myResult.isResultSuccess() {
  105. completedBlock(nil, myResult.model?.data?.id)
  106. }else{
  107. completedBlock(myResult.error?.errorDescription, nil)
  108. }
  109. }
  110. }
  111. /// 更新会议
  112. func updateMeetingAction(meeting: OOMeetingInfo, completedBlock: @escaping (_ returnMessage:String?) -> Void) {
  113. ooMeetingAPI.request(.meetingItemUpdate(meeting)) { (result) in
  114. let myResult = OOResult<BaseModelClass<OOCommonIdModel>>(result)
  115. if myResult.isResultSuccess() {
  116. completedBlock(nil)
  117. }else{
  118. completedBlock(myResult.error?.errorDescription)
  119. }
  120. }
  121. }
  122. /// 上传会议材料 返回当前会议的材料列表
  123. func uploadMeetingFile(meetingId: String, fileName: String, file: Data, completedBlock:@escaping (_ returnMessage:String?, _ list: [OOMeetingAttachmentList]?) -> Void) {
  124. ooMeetingAPI.request(.meetingUploadFile(meetingId, fileName, file)) { result in
  125. let myResult = OOResult<BaseModelClass<OOCommonIdModel>>(result)
  126. if myResult.isResultSuccess() {
  127. self.ooMeetingAPI.request(.meetingAttachmentList(meetingId)) { attListResult in
  128. let attList = OOResult<BaseModelClass<[OOMeetingAttachmentList]>>(attListResult)
  129. if attList.isResultSuccess() {
  130. completedBlock(nil, attList.model?.data)
  131. }else {
  132. completedBlock(attList.error?.errorDescription, nil)
  133. }
  134. }
  135. }else{
  136. completedBlock(myResult.error?.errorDescription, nil)
  137. }
  138. }
  139. }
  140. /// 删除一个会议材料 返回当前会议的材料列表
  141. func deleteMeetingFile(meetingId: String, fileId: String, completedBlock:@escaping (_ returnMessage:String?, _ list: [OOMeetingAttachmentList]?) -> Void) {
  142. ooMeetingAPI.request(.meetingAttachmentDelete(fileId)) { deleteResult in
  143. let myDeleteResult = OOResult<BaseModelClass<OOCommonIdModel>>(deleteResult)
  144. if myDeleteResult.isResultSuccess() {
  145. self.ooMeetingAPI.request(.meetingAttachmentList(meetingId)) { attListResult in
  146. let myAttListResult = OOResult<BaseModelClass<[OOMeetingAttachmentList]>>(attListResult)
  147. if myAttListResult.isResultSuccess() {
  148. completedBlock(nil, myAttListResult.model?.data)
  149. }else {
  150. completedBlock(myAttListResult.error?.errorDescription, nil)
  151. }
  152. }
  153. }else {
  154. completedBlock(myDeleteResult.error?.errorDescription, nil)
  155. }
  156. }
  157. }
  158. ///接受会议邀请
  159. func acceptMeeting(meetingId: String, completedBlock: @escaping (_ returnMessage:String?) -> Void) {
  160. ooMeetingAPI.request(.meetingItemAcceptById(meetingId)) { result in
  161. let myResult = OOResult<BaseModelClass<OOCommonIdModel>>(result)
  162. if myResult.isResultSuccess() {
  163. completedBlock(nil)
  164. } else {
  165. completedBlock(myResult.error?.errorDescription)
  166. }
  167. }
  168. }
  169. ///拒绝会议邀请
  170. func rejectMeeting(meetingId: String, completedBlock: @escaping (_ returnMessage:String?) -> Void) {
  171. ooMeetingAPI.request(.meetingItemRejectById(meetingId)) { result in
  172. let myResult = OOResult<BaseModelClass<OOCommonIdModel>>(result)
  173. if myResult.isResultSuccess() {
  174. completedBlock(nil)
  175. } else {
  176. completedBlock(myResult.error?.errorDescription)
  177. }
  178. }
  179. }
  180. ///取消会议
  181. func deleteMeeting(meetingId: String, completedBlock: @escaping (_ returnMessage:String?) -> Void) {
  182. ooMeetingAPI.request(.meetingItemDelete(meetingId)) { result in
  183. let myResult = OOResult<BaseModelClass<OOCommonIdModel>>(result)
  184. if myResult.isResultSuccess() {
  185. completedBlock(nil)
  186. } else {
  187. completedBlock(myResult.error?.errorDescription)
  188. }
  189. }
  190. }
  191. /// 下载附件到本地 返回本地url 可以前端打开
  192. func downloadMeetingFile(file: OOMeetingAttachmentList, completedBlock: @escaping (_ error: String?, _ url: URL?) -> Void) {
  193. self.ooMeetingAPI.request(.meetingAttachmentDownload(file)) { result in
  194. switch result {
  195. case .success(_):
  196. completedBlock(nil, self.meetingFilePath(file: file))
  197. case .failure(let err):
  198. completedBlock(err.errorDescription, nil)
  199. }
  200. }
  201. }
  202. private func meetingFilePath(file: OOMeetingAttachmentList) -> URL {
  203. let fileName = "\(file.name ?? "untitle").\(file.extension ?? "png")"
  204. return O2.meetingFileLocalFolder().appendingPathComponent(fileName)
  205. }
  206. //表单模型
  207. func getFormModels() -> [OOFormBaseModel] {
  208. let titleModel = OOFormBaseModel(titleName: "会议主题", key: "subject", componentType: .textItem, itemStatus: .edit)
  209. let dateModel = OOFormBaseModel(titleName: "会议日期", key: "date", componentType: .dateItem, itemStatus: .edit)
  210. let dateIntervalModel = OOFormDateIntervalModel(titleName: "会议时间", key: "dateInterval", componentType: .dateIntervalItem, itemStatus: .edit)
  211. let segueModel = OOFormSegueItemModel(titleName: "会议室", key: "room", componentType: .segueItem, itemStatus: .edit)
  212. segueModel.segueIdentifier = "OOMeetingMeetingRoomManageController"
  213. return [titleModel,dateModel,dateIntervalModel,segueModel]
  214. }
  215. func getFormModelsUpdate(meeting: OOMeetingInfo) -> [OOFormBaseModel] {
  216. let titleModel = OOFormBaseModel(titleName: "会议主题", key: "subject", componentType: .textItem, itemStatus: .edit)
  217. titleModel.callbackValue = meeting.subject
  218. let dateModel = OOFormBaseModel(titleName: "会议日期", key: "date", componentType: .dateItem, itemStatus: .edit)
  219. if let startTime = meeting.startTime {
  220. dateModel.callbackValue = Date.date(startTime, formatter: "yyyy-MM-dd HH:mm:ss")
  221. }
  222. let dateIntervalModel = OOFormDateIntervalModel(titleName: "会议时间", key: "dateInterval", componentType: .dateIntervalItem, itemStatus: .edit)
  223. if let startTime = meeting.startTime {
  224. dateIntervalModel.value1 = Date.date(startTime, formatter: "yyyy-MM-dd HH:mm:ss")
  225. }
  226. if let endTime = meeting.completedTime {
  227. dateIntervalModel.value2 = Date.date(endTime, formatter: "yyyy-MM-dd HH:mm:ss")
  228. }
  229. let segueModel = OOFormSegueItemModel(titleName: "会议室", key: "room", componentType: .segueItem, itemStatus: .edit)
  230. segueModel.segueIdentifier = "OOMeetingMeetingRoomManageController"
  231. segueModel.callbackValue = meeting.woRoom
  232. return [titleModel,dateModel,dateIntervalModel,segueModel]
  233. }
  234. }
  235. // MARK:- 选择的人员列表
  236. extension OOMeetingCreateViewModel{
  237. func addSelectPerson(_ p:OOPersonModel){
  238. self.selectedPersons.append(p)
  239. }
  240. func removeSelectPerson(_ p:OOPersonModel){
  241. if let i = self.selectedPersons.firstIndex(of: p) {
  242. self.selectedPersons.remove(at:i)
  243. }
  244. }
  245. func refreshData(){
  246. guard let block = self.selectedContactCallBlock else {
  247. return
  248. }
  249. block(nil)
  250. }
  251. func collectionViewNumberOfSections() -> Int {
  252. return 1
  253. }
  254. func collectionViewNumberOfRowsInSection(_ section: Int) -> Int {
  255. return selectedPersons.count + 1
  256. }
  257. func collectionViewNodeForIndexPath(_ indexPath:IndexPath) -> OOPersonModel? {
  258. if indexPath.row < selectedPersons.count {
  259. return selectedPersons[indexPath.row]
  260. }else{
  261. return nil
  262. }
  263. }
  264. }
  265. // MARK:- 人员列表
  266. extension OOMeetingCreateViewModel {
  267. func tableViewNumberOfSections() -> Int {
  268. return 1
  269. }
  270. func tableViewNumberOfRowsInSection(_ section: Int) -> Int {
  271. return persons.count
  272. }
  273. func tableViewNodeForIndexPath(_ indexPath:IndexPath) -> OOPersonModel? {
  274. return persons[indexPath.row]
  275. }
  276. }