123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267 |
- //
- // OOMeetingMainViewModel.swift
- // o2app
- //
- // Created by 刘振兴 on 2018/1/17.
- // Copyright © 2018年 zone. All rights reserved.
- //
- import UIKit
- import Promises
- class OOMeetingMainViewModel: NSObject {
- //HTTP API
- private let o2MeetingAPI = OOMoyaProvider<O2MeetingAPI>()
- private let o2PersonalAPI = OOMoyaProvider<PersonalAPI>()
- private let o2ProcessAPI = OOMoyaProvider<OOApplicationAPI>()
-
- //所有本月所有会议
- private var meetingsByMonth:[OOMeetingInfo] = []
-
- //本月所有会议按日期生成key,value
- private var meetingsByMonthForDict:[String:[OOMeetingInfo]?] = [:]
-
- //指定日期的所有会议
- var theMeetingsByDay:[OOMeetingInfo] = []
-
- //回调块类型定义
- typealias CallbackBlockDefine = (_ msg:String?) -> Void
- //回调块定义
- var callbackExecutor:CallbackBlockDefine?
-
- override init() {
- super.init()
- }
-
- private func formatData() {
- self.meetingsByMonthForDict.removeAll()
- meetingsByMonth.forEach { (item) in
- let startDate = String((item.startTime?.split(separator: " ").first)!)
- if var meetings = meetingsByMonthForDict[startDate] {
- meetings?.append(item)
- }else{
- let meetings:[OOMeetingInfo] = [item]
- meetingsByMonthForDict[startDate] = meetings
- }
- }
- }
-
- }
- extension OOMeetingMainViewModel {
-
- //获取会议配置信息
- func loadMeetingConfig() -> Promise<OOMeetingConfigInfo> {
- return Promise { fulfill, reject in
- self.o2PersonalAPI.request(.meetingConfig, completion: { (result) in
- let config = OOResult<BaseModelClass<String>>(result)
- if config.isResultSuccess() {
- if let jsonString = config.model?.data {
- if let info = OOMeetingConfigInfo.deserialize(from: jsonString) {
- fulfill(info)
- }else {
- reject(OOAppError.jsonMapping(message: "json解析异常", statusCode: 1024, data: nil))
- }
- } else {
- reject(OOAppError.apiResponseError("返回数据是空"))
- }
- }else {
- reject(config.error!)
- }
- })
-
- }
- }
-
- //会议流程对应的身份信息
- func loadMeetingProcess(processId: String) -> Promise<[OOMeetingProcessIdentity]> {
- return Promise { fulfill, reject in
- self.o2ProcessAPI.request(.availableIdentityWithProcess(processId), completion: { (result) in
- let myResult = OOResult<BaseModelClass<[OOMeetingProcessIdentity]>>(result)
- if myResult.isResultSuccess() {
- if let item = myResult.model?.data {
- fulfill(item)
- }else{
- let customError = OOAppError.common(type: "会议异常", message: "会议流程身份读取错误", statusCode: 7001)
- reject(customError)
- }
- }else{
- reject(myResult.error!)
- }
- })
- }
- }
-
- //启动会议流程
- func startProcess(processId: String, identity: String) -> Promise<[TodoTaskData]> {
- return Promise { fulfill, reject in
- self.o2ProcessAPI.request(.startProcess(processId, identity, ""), completion: { (result) in
- let myResult = OOResult<BaseModelClass<[StartProcessData]>>(result)
- if myResult.isResultSuccess() {
- if let item = myResult.model?.data {
- if let taskList = item[0].taskList {
- fulfill(taskList)
- }else {
- let customError = OOAppError.common(type: "启动会议流程异常", message: "启动会议流程异常", statusCode: 7001)
- reject(customError)
- }
- }else {
- let customError = OOAppError.common(type: "启动会议流程异常", message: "启动会议流程异常", statusCode: 7001)
- reject(customError)
- }
- } else {
- reject(myResult.error!)
- }
- })
- }
- }
-
- // MARK:- 读取会议室信息
- func loadMeetingRoomById(_ roomId:String) -> Promise<OOMeetingRoomInfo> {
- return Promise { fulfill,reject in
- self.o2MeetingAPI.request(.roomItemById(roomId)) { (result) in
- let myResult = OOResult<BaseModelClass<OOMeetingRoomInfo>>(result)
- if myResult.isResultSuccess() {
- if let item = myResult.model?.data {
- fulfill(item)
- }else{
- let customError = OOAppError.common(type: "MeetingRoom load Error", message: "会议室信息读取错误", statusCode: 7001)
- reject(customError)
- }
-
- }else{
- reject(myResult.error!)
- }
- }
- }
- }
-
-
- func loadMeetingRoomById(_ roomId:String,completed:@escaping (_ room:OOMeetingRoomInfo?) -> Void){
- o2MeetingAPI.request(.roomItemById(roomId)) { (result) in
- let myResult = OOResult<BaseModelClass<OOMeetingRoomInfo>>(result)
- if myResult.isResultSuccess() {
- let item = myResult.model?.data
- completed(item)
- }else{
- completed(nil)
- }
- }
-
- }
-
- // MARK:- 按月读取会议信息
- func getMeetingsByYearAndMonth(_ theDate:Date) -> Promise<[String:[OOMeetingInfo]?]?> {
- let strYear = String(theDate.year)
- let strMonth = String(theDate.month)
- return Promise { fulfill,reject in
- self.o2MeetingAPI.request(.meetingListByYearMonth(strYear, strMonth)) { (result) in
- let myResult = OOResult<BaseModelClass<[OOMeetingInfo]>>(result)
- if myResult.isResultSuccess() {
- self.meetingsByMonth.removeAll()
- if let model = myResult.model?.data {
- model.forEach({ (item) in
- self.meetingsByMonth.append(item)
- })
- }
- self.formatData()
- fulfill(self.meetingsByMonthForDict)
- }else{
- reject(myResult.error!)
- }
- }
- }
- }
-
- //按月读取会议信息
- func getMeetingsByYearAndMonth(_ theDate:Date,completedCallback:@escaping (_ meetingsDict:[String:[OOMeetingInfo]?]?) -> Void){
- let strYear = String(theDate.year)
- let strMonth = String(theDate.month)
- o2MeetingAPI.request(.meetingListByYearMonth(strYear, strMonth)) { (result) in
- let myResult = OOResult<BaseModelClass<[OOMeetingInfo]>>(result)
- if myResult.isResultSuccess() {
- self.meetingsByMonth.removeAll()
- if let model = myResult.model?.data {
- model.forEach({ (item) in
- self.meetingsByMonth.append(item)
- })
- }
- self.formatData()
- completedCallback(self.meetingsByMonthForDict)
- }else{
- completedCallback(nil)
- }
- }
-
- }
-
- // MARK:- 读取指定日期的会议列表
- func getMeetingByTheDay(_ theDate:Date) -> Promise<[OOMeetingInfo]> {
- let strYear = String(theDate.year)
- let strMonth = String(theDate.month)
- let strDay = String(theDate.day)
- return Promise { fulfill,reject in
- self.o2MeetingAPI.request(.meetingListByYearMonthDay(strYear, strMonth, strDay)) { (result) in
- let myResult = OOResult<BaseModelClass<[OOMeetingInfo]>>(result)
- if myResult.isResultSuccess() {
- if let models = myResult.model?.data {
- fulfill(models)
- }else{
- fulfill([])
- }
- }else{
- reject(myResult.error!)
- }
- }
- }
- }
-
- // //读取指定日期的会议列表
- // func getMeetingByTheDay(_ theDate:Date){
- // let strYear = String(theDate.year)
- // let strMonth = String(theDate.month)
- // let strDay = String(theDate.day)
- // self.theMeetingsByDay.removeAll()
- // o2MeetingAPI.request(.meetingListByYearMonthDay(strYear, strMonth, strDay)) { (result) in
- // let myResult = OOResult<BaseModelClass<[OOMeetingInfo]>>(result)
- // if myResult.isResultSuccess() {
- // if let model = myResult.model?.data {
- // model.forEach({ (item) in
- // self.theMeetingsByDay.append(item)
- // })
- // }
- // }
- // guard let block = self.callbackExecutor else {
- // return
- // }
- // if myResult.isResultSuccess() {
- // block(nil)
- // }else{
- // block(myResult.error?.errorDescription)
- // }
- // }
- //
- // }
- }
- // MARK:- UITableView DataSource
- extension OOMeetingMainViewModel {
- func numberOfSections() -> Int {
- return 1
- }
-
- func numberOfRowsInSection(_ section: Int) -> Int {
- return theMeetingsByDay.count
- }
-
- func nodeForIndexPath(_ indexPath:IndexPath) -> OOMeetingInfo {
- return theMeetingsByDay[indexPath.row]
- }
- }
|