OOMeetingMainViewModel.swift 9.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262
  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. didSet {
  18. meetingsByMonth.forEach { (item) in
  19. let startDate = String((item.startTime?.split(separator: " ").first)!)
  20. if var meetings = meetingsByMonthForDict[startDate] {
  21. meetings?.append(item)
  22. }else{
  23. let meetings:[OOMeetingInfo] = [item]
  24. meetingsByMonthForDict[startDate] = meetings
  25. }
  26. }
  27. }
  28. }
  29. //本月所有会议按日期生成key,value
  30. private var meetingsByMonthForDict:[String:[OOMeetingInfo]?] = [:]
  31. //指定日期的所有会议
  32. var theMeetingsByDay:[OOMeetingInfo] = []
  33. //回调块类型定义
  34. typealias CallbackBlockDefine = (_ msg:String?) -> Void
  35. //回调块定义
  36. var callbackExecutor:CallbackBlockDefine?
  37. override init() {
  38. super.init()
  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. if let model = myResult.model?.data {
  142. model.forEach({ (item) in
  143. self.meetingsByMonth.append(item)
  144. })
  145. }
  146. fulfill(self.meetingsByMonthForDict)
  147. }else{
  148. reject(myResult.error!)
  149. }
  150. }
  151. }
  152. }
  153. //按月读取会议信息
  154. func getMeetingsByYearAndMonth(_ theDate:Date,completedCallback:@escaping (_ meetingsDict:[String:[OOMeetingInfo]?]?) -> Void){
  155. let strYear = String(theDate.year)
  156. let strMonth = String(theDate.month)
  157. o2MeetingAPI.request(.meetingListByYearMonth(strYear, strMonth)) { (result) in
  158. let myResult = OOResult<BaseModelClass<[OOMeetingInfo]>>(result)
  159. if myResult.isResultSuccess() {
  160. if let model = myResult.model?.data {
  161. model.forEach({ (item) in
  162. self.meetingsByMonth.append(item)
  163. })
  164. }
  165. completedCallback(self.meetingsByMonthForDict)
  166. }else{
  167. completedCallback(nil)
  168. }
  169. }
  170. }
  171. // MARK:- 读取指定日期的会议列表
  172. func getMeetingByTheDay(_ theDate:Date) -> Promise<[OOMeetingInfo]> {
  173. let strYear = String(theDate.year)
  174. let strMonth = String(theDate.month)
  175. let strDay = String(theDate.day)
  176. return Promise { fulfill,reject in
  177. self.o2MeetingAPI.request(.meetingListByYearMonthDay(strYear, strMonth, strDay)) { (result) in
  178. let myResult = OOResult<BaseModelClass<[OOMeetingInfo]>>(result)
  179. if myResult.isResultSuccess() {
  180. if let models = myResult.model?.data {
  181. fulfill(models)
  182. }else{
  183. fulfill([])
  184. }
  185. }else{
  186. reject(myResult.error!)
  187. }
  188. }
  189. }
  190. }
  191. // //读取指定日期的会议列表
  192. // func getMeetingByTheDay(_ theDate:Date){
  193. // let strYear = String(theDate.year)
  194. // let strMonth = String(theDate.month)
  195. // let strDay = String(theDate.day)
  196. // self.theMeetingsByDay.removeAll()
  197. // o2MeetingAPI.request(.meetingListByYearMonthDay(strYear, strMonth, strDay)) { (result) in
  198. // let myResult = OOResult<BaseModelClass<[OOMeetingInfo]>>(result)
  199. // if myResult.isResultSuccess() {
  200. // if let model = myResult.model?.data {
  201. // model.forEach({ (item) in
  202. // self.theMeetingsByDay.append(item)
  203. // })
  204. // }
  205. // }
  206. // guard let block = self.callbackExecutor else {
  207. // return
  208. // }
  209. // if myResult.isResultSuccess() {
  210. // block(nil)
  211. // }else{
  212. // block(myResult.error?.errorDescription)
  213. // }
  214. // }
  215. //
  216. // }
  217. }
  218. // MARK:- UITableView DataSource
  219. extension OOMeetingMainViewModel {
  220. func numberOfSections() -> Int {
  221. return 1
  222. }
  223. func numberOfRowsInSection(_ section: Int) -> Int {
  224. return theMeetingsByDay.count
  225. }
  226. func nodeForIndexPath(_ indexPath:IndexPath) -> OOMeetingInfo {
  227. return theMeetingsByDay[indexPath.row]
  228. }
  229. }