OOMeetingMainViewModel.swift 9.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267
  1. //
  2. // OOMeetingMainViewModel.swift
  3. // o2app
  4. //
  5. // Created by 刘振兴 on 2018/1/17.
  6. // Copyright © 2018年 zone. All rights reserved.
  7. //
  8. import UIKit
  9. import Promises
  10. class OOMeetingMainViewModel: NSObject {
  11. //HTTP API
  12. private let o2MeetingAPI = OOMoyaProvider<O2MeetingAPI>()
  13. private let o2PersonalAPI = OOMoyaProvider<PersonalAPI>()
  14. private let o2ProcessAPI = OOMoyaProvider<OOApplicationAPI>()
  15. //所有本月所有会议
  16. private var meetingsByMonth:[OOMeetingInfo] = []
  17. //本月所有会议按日期生成key,value
  18. private var meetingsByMonthForDict:[String:[OOMeetingInfo]?] = [:]
  19. //指定日期的所有会议
  20. var theMeetingsByDay:[OOMeetingInfo] = []
  21. //回调块类型定义
  22. typealias CallbackBlockDefine = (_ msg:String?) -> Void
  23. //回调块定义
  24. var callbackExecutor:CallbackBlockDefine?
  25. override init() {
  26. super.init()
  27. }
  28. private func formatData() {
  29. self.meetingsByMonthForDict.removeAll()
  30. meetingsByMonth.forEach { (item) in
  31. let startDate = String((item.startTime?.split(separator: " ").first)!)
  32. if var meetings = meetingsByMonthForDict[startDate] {
  33. meetings?.append(item)
  34. }else{
  35. let meetings:[OOMeetingInfo] = [item]
  36. meetingsByMonthForDict[startDate] = meetings
  37. }
  38. }
  39. }
  40. }
  41. extension OOMeetingMainViewModel {
  42. //获取会议配置信息
  43. func loadMeetingConfig() -> Promise<OOMeetingConfigInfo> {
  44. return Promise { fulfill, reject in
  45. self.o2PersonalAPI.request(.meetingConfig, completion: { (result) in
  46. let config = OOResult<BaseModelClass<String>>(result)
  47. if config.isResultSuccess() {
  48. if let jsonString = config.model?.data {
  49. if let info = OOMeetingConfigInfo.deserialize(from: jsonString) {
  50. fulfill(info)
  51. }else {
  52. reject(OOAppError.jsonMapping(message: "json解析异常", statusCode: 1024, data: nil))
  53. }
  54. } else {
  55. reject(OOAppError.apiResponseError("返回数据是空"))
  56. }
  57. }else {
  58. reject(config.error!)
  59. }
  60. })
  61. }
  62. }
  63. //会议流程对应的身份信息
  64. func loadMeetingProcess(processId: String) -> Promise<[OOMeetingProcessIdentity]> {
  65. return Promise { fulfill, reject in
  66. self.o2ProcessAPI.request(.availableIdentityWithProcess(processId), completion: { (result) in
  67. let myResult = OOResult<BaseModelClass<[OOMeetingProcessIdentity]>>(result)
  68. if myResult.isResultSuccess() {
  69. if let item = myResult.model?.data {
  70. fulfill(item)
  71. }else{
  72. let customError = OOAppError.common(type: "会议异常", message: "会议流程身份读取错误", statusCode: 7001)
  73. reject(customError)
  74. }
  75. }else{
  76. reject(myResult.error!)
  77. }
  78. })
  79. }
  80. }
  81. //启动会议流程
  82. func startProcess(processId: String, identity: String) -> Promise<[TodoTaskData]> {
  83. return Promise { fulfill, reject in
  84. self.o2ProcessAPI.request(.startProcess(processId, identity, ""), completion: { (result) in
  85. let myResult = OOResult<BaseModelClass<[StartProcessData]>>(result)
  86. if myResult.isResultSuccess() {
  87. if let item = myResult.model?.data {
  88. if let taskList = item[0].taskList {
  89. fulfill(taskList)
  90. }else {
  91. let customError = OOAppError.common(type: "启动会议流程异常", message: "启动会议流程异常", statusCode: 7001)
  92. reject(customError)
  93. }
  94. }else {
  95. let customError = OOAppError.common(type: "启动会议流程异常", message: "启动会议流程异常", statusCode: 7001)
  96. reject(customError)
  97. }
  98. } else {
  99. reject(myResult.error!)
  100. }
  101. })
  102. }
  103. }
  104. // MARK:- 读取会议室信息
  105. func loadMeetingRoomById(_ roomId:String) -> Promise<OOMeetingRoomInfo> {
  106. return Promise { fulfill,reject in
  107. self.o2MeetingAPI.request(.roomItemById(roomId)) { (result) in
  108. let myResult = OOResult<BaseModelClass<OOMeetingRoomInfo>>(result)
  109. if myResult.isResultSuccess() {
  110. if let item = myResult.model?.data {
  111. fulfill(item)
  112. }else{
  113. let customError = OOAppError.common(type: "MeetingRoom load Error", message: "会议室信息读取错误", statusCode: 7001)
  114. reject(customError)
  115. }
  116. }else{
  117. reject(myResult.error!)
  118. }
  119. }
  120. }
  121. }
  122. func loadMeetingRoomById(_ roomId:String,completed:@escaping (_ room:OOMeetingRoomInfo?) -> Void){
  123. o2MeetingAPI.request(.roomItemById(roomId)) { (result) in
  124. let myResult = OOResult<BaseModelClass<OOMeetingRoomInfo>>(result)
  125. if myResult.isResultSuccess() {
  126. let item = myResult.model?.data
  127. completed(item)
  128. }else{
  129. completed(nil)
  130. }
  131. }
  132. }
  133. // MARK:- 按月读取会议信息
  134. func getMeetingsByYearAndMonth(_ theDate:Date) -> Promise<[String:[OOMeetingInfo]?]?> {
  135. let strYear = String(theDate.year)
  136. let strMonth = String(theDate.month)
  137. return Promise { fulfill,reject in
  138. self.o2MeetingAPI.request(.meetingListByYearMonth(strYear, strMonth)) { (result) in
  139. let myResult = OOResult<BaseModelClass<[OOMeetingInfo]>>(result)
  140. if myResult.isResultSuccess() {
  141. self.meetingsByMonth.removeAll()
  142. if let model = myResult.model?.data {
  143. model.forEach({ (item) in
  144. self.meetingsByMonth.append(item)
  145. })
  146. }
  147. self.formatData()
  148. fulfill(self.meetingsByMonthForDict)
  149. }else{
  150. reject(myResult.error!)
  151. }
  152. }
  153. }
  154. }
  155. //按月读取会议信息
  156. func getMeetingsByYearAndMonth(_ theDate:Date,completedCallback:@escaping (_ meetingsDict:[String:[OOMeetingInfo]?]?) -> Void){
  157. let strYear = String(theDate.year)
  158. let strMonth = String(theDate.month)
  159. o2MeetingAPI.request(.meetingListByYearMonth(strYear, strMonth)) { (result) in
  160. let myResult = OOResult<BaseModelClass<[OOMeetingInfo]>>(result)
  161. if myResult.isResultSuccess() {
  162. self.meetingsByMonth.removeAll()
  163. if let model = myResult.model?.data {
  164. model.forEach({ (item) in
  165. self.meetingsByMonth.append(item)
  166. })
  167. }
  168. self.formatData()
  169. completedCallback(self.meetingsByMonthForDict)
  170. }else{
  171. completedCallback(nil)
  172. }
  173. }
  174. }
  175. // MARK:- 读取指定日期的会议列表
  176. func getMeetingByTheDay(_ theDate:Date) -> Promise<[OOMeetingInfo]> {
  177. let strYear = String(theDate.year)
  178. let strMonth = String(theDate.month)
  179. let strDay = String(theDate.day)
  180. return Promise { fulfill,reject in
  181. self.o2MeetingAPI.request(.meetingListByYearMonthDay(strYear, strMonth, strDay)) { (result) in
  182. let myResult = OOResult<BaseModelClass<[OOMeetingInfo]>>(result)
  183. if myResult.isResultSuccess() {
  184. if let models = myResult.model?.data {
  185. fulfill(models)
  186. }else{
  187. fulfill([])
  188. }
  189. }else{
  190. reject(myResult.error!)
  191. }
  192. }
  193. }
  194. }
  195. // //读取指定日期的会议列表
  196. // func getMeetingByTheDay(_ theDate:Date){
  197. // let strYear = String(theDate.year)
  198. // let strMonth = String(theDate.month)
  199. // let strDay = String(theDate.day)
  200. // self.theMeetingsByDay.removeAll()
  201. // o2MeetingAPI.request(.meetingListByYearMonthDay(strYear, strMonth, strDay)) { (result) in
  202. // let myResult = OOResult<BaseModelClass<[OOMeetingInfo]>>(result)
  203. // if myResult.isResultSuccess() {
  204. // if let model = myResult.model?.data {
  205. // model.forEach({ (item) in
  206. // self.theMeetingsByDay.append(item)
  207. // })
  208. // }
  209. // }
  210. // guard let block = self.callbackExecutor else {
  211. // return
  212. // }
  213. // if myResult.isResultSuccess() {
  214. // block(nil)
  215. // }else{
  216. // block(myResult.error?.errorDescription)
  217. // }
  218. // }
  219. //
  220. // }
  221. }
  222. // MARK:- UITableView DataSource
  223. extension OOMeetingMainViewModel {
  224. func numberOfSections() -> Int {
  225. return 1
  226. }
  227. func numberOfRowsInSection(_ section: Int) -> Int {
  228. return theMeetingsByDay.count
  229. }
  230. func nodeForIndexPath(_ indexPath:IndexPath) -> OOMeetingInfo {
  231. return theMeetingsByDay[indexPath.row]
  232. }
  233. }