123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227 |
- //
- // OOAttandanceViewModel.swift
- // O2Platform
- //
- // Created by 刘振兴 on 2018/5/16.
- // Copyright © 2018年 zoneland. All rights reserved.
- //
- import Foundation
- import Moya
- import Promises
- public enum OOAttandanceResultType {
- case ok(Any?)
- case fail(String)
- case reload
- }
- public enum OOAttandanceCustomError:Error {
- case checkinCycle(OOAppError)
- case checkinTotal(OOAppError)
- }
- final class OOAttandanceViewModel: NSObject {
- //HTTP API
- private let ooAttanceAPI = OOMoyaProvider<OOAttendanceAPI>()
- //当天我的所有打卡记录
- private var myAttanceDetailList:[OOAttandanceMobileDetail] = []
- //回调块类型定义
- typealias CallbackBlockDefine = (_ resultType:OOAttandanceResultType) -> Void
- //回调块定义
- var callbackExecutor:CallbackBlockDefine?
-
- override init() {
- super.init()
- }
-
- }
- extension OOAttandanceViewModel{
-
- // MARK: - 当天打卡记录和打卡班次情况
- func listMyRecords(_ completedBlock:@escaping CallbackBlockDefine) {
- ooAttanceAPI.request(.listMyRecord) { response in
- let myResult = OOResult<BaseModelClass<OOMyAttandanceRecords>>(response)
- if myResult.isResultSuccess() {
- let records = myResult.model?.data
- completedBlock(.ok(records))
- }else {
- let errorMessage = myResult.error?.errorDescription ?? ""
- completedBlock(.fail(errorMessage))
- }
- }
- }
-
- // MARK:- 读取配置的打卡位置
- func getLocationWorkPlace(_ completedBlock:@escaping CallbackBlockDefine) {
- ooAttanceAPI.request(.myWorkplace) { (responseResult) in
- let myResult = OOResult<BaseModelClass<[OOAttandanceWorkPlace]>>(responseResult)
- if myResult.isResultSuccess() {
- let workPlaces = myResult.model?.data ?? []
- completedBlock(.ok(workPlaces))
- }else{
- let errorMessage = myResult.error?.errorDescription ?? ""
- completedBlock(.fail(errorMessage))
- }
- }
- }
-
- // MARK:- 删除配置
- func deleteLocationWorkPlace(_ bean:OOAttandanceWorkPlace,_ completedBlock:@escaping CallbackBlockDefine) {
- ooAttanceAPI.request(.delWorkplace(bean.id!)) { (responseResult) in
- let myResult = OOResult<BaseModelClass<OOCommonModel>>(responseResult)
- if myResult.isResultSuccess() {
- completedBlock(.reload)
- }else{
- let errorMessage = myResult.error?.errorDescription ?? ""
- completedBlock(.fail(errorMessage))
- }
- }
- }
- // MARK:- 检测
- // MARK:- 读取本人当天的打卡记录
- func getMyCheckinList(_ pageModel:CommonPageModel,_ bean:OOAttandanceMobileQueryBean,_ completedBlock:@escaping CallbackBlockDefine) {
- myAttanceDetailList.removeAll()
- ooAttanceAPI.request(.myAttendanceDetailMobileByPage(pageModel, bean)) { (responseResult) in
- let myResult = OOResult<BaseModelClass<[OOAttandanceMobileDetail]>>(responseResult)
- if myResult.isResultSuccess() {
- self.myAttanceDetailList.append(contentsOf: myResult.model?.data ?? [])
- completedBlock(.reload)
- }else{
- let errorMessage = myResult.error?.errorDescription ?? ""
- completedBlock(.fail(errorMessage))
- }
- }
- }
-
-
- // MARK:- 提交打卡
- func postMyCheckin(_ bean:OOAttandanceMobileCheckinForm,completedBlock:@escaping CallbackBlockDefine) {
- ooAttanceAPI.request(.attendanceDetailCheckIn(bean)) { (responseResult) in
- let myResult = OOResult<BaseModelClass<[OOAttandanceMobileDetail]>>(responseResult)
- if myResult.isResultSuccess() {
- completedBlock(.ok(nil))
- }else{
- let errorMessage = myResult.error?.errorDescription ?? ""
- completedBlock(.fail(errorMessage))
- }
- }
- }
-
- //MARK:- 提交位置
- func postCheckinLocation(_ bean:OOAttandanceNewWorkPlace,completedBlock:@escaping CallbackBlockDefine){
- ooAttanceAPI.request(.addWorkplace(bean)) { (responseResult) in
- let myResult = OOResult<BaseModelClass<OOCommonModel>>(responseResult)
- if myResult.isResultSuccess() {
- completedBlock(.ok(nil))
- }else{
- let errorMessage = myResult.error?.errorDescription ?? ""
- completedBlock(.fail(errorMessage))
- }
- }
- }
-
- // MARK:- 读取打卡周期
- func getCheckinCycle(_ year:String,_ month:String) -> Promise<OOAttandanceCycleDetail> {
- return Promise { fulfill,reject in
- self.ooAttanceAPI.request(.checkinCycle(year, month), completion: { (result) in
- let myResult = OOResult<BaseModelClass<OOAttandanceCycleDetail>>(result)
- if myResult.isResultSuccess() {
- fulfill((myResult.model?.data!)!)
- }else{
- reject(myResult.error!)
- }
- })
-
- }
- }
-
- // MARK:- 读取指定月份及及本月打卡周期的所有打统统计数据
- func getCheckinTotal(_ cycleDetail:OOAttandanceCycleDetail) -> Promise<[OOAttandanceCheckinTotal]> {
- let bean = getRequestBean(cycleDetail)
- return Promise { fulfill,reject in
- self.ooAttanceAPI.request(.checkinTotalForMonth(bean), completion: { (result) in
- let myResult = OOResult<BaseModelClass<[OOAttandanceCheckinTotal]>>(result)
- if myResult.isResultSuccess() {
- fulfill((myResult.model?.data!)!)
- }else{
- reject(myResult.error!)
- }
- })
-
- }
- }
-
- private func getRequestBean(_ cycleDetail:OOAttandanceCycleDetail) -> OOAttandanceTotalBean {
- let bean = OOAttandanceTotalBean()
- bean.q_year = cycleDetail.cycleYear
- bean.q_month = cycleDetail.cycleMonth
- bean.cycleYear = cycleDetail.cycleYear
- bean.cycleMonth = cycleDetail.cycleMonth
- bean.q_empName = O2AuthSDK.shared.myInfo()?.distinguishedName
- return bean
- }
-
- // MARK:- 读取考勤分析数据
- func getCheckinAnalyze(_ cycleDetail:OOAttandanceCycleDetail) -> Promise<OOAttandanceAnalyze?>{
- let bean = getRequestBean(cycleDetail)
- return Promise { fulfill,reject in
- self.ooAttanceAPI.request(.checkinAnalyze(bean)) { (result) in
- let myResult = OOResult<BaseModelClass<[OOAttandanceAnalyze]>>(result)
- if myResult.isResultSuccess() {
- if let data = myResult.model?.data {
- fulfill((data.first)!)
- }else{
- // reject(OOAppError.common(type: "checkinError", message: "本月无考勤数据", statusCode: 5001))
- fulfill(nil)
- }
- }else{
- //let errorMessage = myResult.error?.errorDescription
- reject(myResult.error!)
- }
- }
- }
- }
-
- // MARK:- 读取配置管理员
- func getAttendanceAdmin() -> Promise<[OOAttandanceAdmin]> {
- return Promise { fulfill,reject in
- self.ooAttanceAPI.request(.attendanceAdmin, completion: { (result) in
- let myResult = OOResult<BaseModelClass<[OOAttandanceAdmin]>>(result)
- if myResult.isResultSuccess() {
- if let data = myResult.model?.data {
- fulfill(data)
- }else{
- reject(OOAppError.common(type: "checkinError", message: "没有配置管理员", statusCode: 5002))
- }
- }else{
- //let errorMessage = myResult.error?.errorDescription
- reject(myResult.error!)
- }
- })
-
- }
- }
- }
- // MARK:- UITableView DataSource
- extension OOAttandanceViewModel {
- func numberOfSections() -> Int {
- return 1
- }
-
- func numberOfRowsInSection(_ section: Int) -> Int {
- return myAttanceDetailList.count
- }
-
- func nodeForIndexPath(_ indexPath:IndexPath) -> OOAttandanceMobileDetail? {
- return myAttanceDetailList[indexPath.row]
- }
-
-
-
- }
|