OOAttandanceViewModel.swift 8.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227
  1. //
  2. // OOAttandanceViewModel.swift
  3. // O2Platform
  4. //
  5. // Created by 刘振兴 on 2018/5/16.
  6. // Copyright © 2018年 zoneland. All rights reserved.
  7. //
  8. import Foundation
  9. import Moya
  10. import Promises
  11. public enum OOAttandanceResultType {
  12. case ok(Any?)
  13. case fail(String)
  14. case reload
  15. }
  16. public enum OOAttandanceCustomError:Error {
  17. case checkinCycle(OOAppError)
  18. case checkinTotal(OOAppError)
  19. }
  20. final class OOAttandanceViewModel: NSObject {
  21. //HTTP API
  22. private let ooAttanceAPI = OOMoyaProvider<OOAttendanceAPI>()
  23. //当天我的所有打卡记录
  24. private var myAttanceDetailList:[OOAttandanceMobileDetail] = []
  25. //回调块类型定义
  26. typealias CallbackBlockDefine = (_ resultType:OOAttandanceResultType) -> Void
  27. //回调块定义
  28. var callbackExecutor:CallbackBlockDefine?
  29. override init() {
  30. super.init()
  31. }
  32. }
  33. extension OOAttandanceViewModel{
  34. // MARK: - 当天打卡记录和打卡班次情况
  35. func listMyRecords(_ completedBlock:@escaping CallbackBlockDefine) {
  36. ooAttanceAPI.request(.listMyRecord) { response in
  37. let myResult = OOResult<BaseModelClass<OOMyAttandanceRecords>>(response)
  38. if myResult.isResultSuccess() {
  39. let records = myResult.model?.data
  40. completedBlock(.ok(records))
  41. }else {
  42. let errorMessage = myResult.error?.errorDescription ?? ""
  43. completedBlock(.fail(errorMessage))
  44. }
  45. }
  46. }
  47. // MARK:- 读取配置的打卡位置
  48. func getLocationWorkPlace(_ completedBlock:@escaping CallbackBlockDefine) {
  49. ooAttanceAPI.request(.myWorkplace) { (responseResult) in
  50. let myResult = OOResult<BaseModelClass<[OOAttandanceWorkPlace]>>(responseResult)
  51. if myResult.isResultSuccess() {
  52. let workPlaces = myResult.model?.data ?? []
  53. completedBlock(.ok(workPlaces))
  54. }else{
  55. let errorMessage = myResult.error?.errorDescription ?? ""
  56. completedBlock(.fail(errorMessage))
  57. }
  58. }
  59. }
  60. // MARK:- 删除配置
  61. func deleteLocationWorkPlace(_ bean:OOAttandanceWorkPlace,_ completedBlock:@escaping CallbackBlockDefine) {
  62. ooAttanceAPI.request(.delWorkplace(bean.id!)) { (responseResult) in
  63. let myResult = OOResult<BaseModelClass<OOCommonModel>>(responseResult)
  64. if myResult.isResultSuccess() {
  65. completedBlock(.reload)
  66. }else{
  67. let errorMessage = myResult.error?.errorDescription ?? ""
  68. completedBlock(.fail(errorMessage))
  69. }
  70. }
  71. }
  72. // MARK:- 检测
  73. // MARK:- 读取本人当天的打卡记录
  74. func getMyCheckinList(_ pageModel:CommonPageModel,_ bean:OOAttandanceMobileQueryBean,_ completedBlock:@escaping CallbackBlockDefine) {
  75. myAttanceDetailList.removeAll()
  76. ooAttanceAPI.request(.myAttendanceDetailMobileByPage(pageModel, bean)) { (responseResult) in
  77. let myResult = OOResult<BaseModelClass<[OOAttandanceMobileDetail]>>(responseResult)
  78. if myResult.isResultSuccess() {
  79. self.myAttanceDetailList.append(contentsOf: myResult.model?.data ?? [])
  80. completedBlock(.reload)
  81. }else{
  82. let errorMessage = myResult.error?.errorDescription ?? ""
  83. completedBlock(.fail(errorMessage))
  84. }
  85. }
  86. }
  87. // MARK:- 提交打卡
  88. func postMyCheckin(_ bean:OOAttandanceMobileCheckinForm,completedBlock:@escaping CallbackBlockDefine) {
  89. ooAttanceAPI.request(.attendanceDetailCheckIn(bean)) { (responseResult) in
  90. let myResult = OOResult<BaseModelClass<[OOAttandanceMobileDetail]>>(responseResult)
  91. if myResult.isResultSuccess() {
  92. completedBlock(.ok(nil))
  93. }else{
  94. let errorMessage = myResult.error?.errorDescription ?? ""
  95. completedBlock(.fail(errorMessage))
  96. }
  97. }
  98. }
  99. //MARK:- 提交位置
  100. func postCheckinLocation(_ bean:OOAttandanceNewWorkPlace,completedBlock:@escaping CallbackBlockDefine){
  101. ooAttanceAPI.request(.addWorkplace(bean)) { (responseResult) in
  102. let myResult = OOResult<BaseModelClass<OOCommonModel>>(responseResult)
  103. if myResult.isResultSuccess() {
  104. completedBlock(.ok(nil))
  105. }else{
  106. let errorMessage = myResult.error?.errorDescription ?? ""
  107. completedBlock(.fail(errorMessage))
  108. }
  109. }
  110. }
  111. // MARK:- 读取打卡周期
  112. func getCheckinCycle(_ year:String,_ month:String) -> Promise<OOAttandanceCycleDetail> {
  113. return Promise { fulfill,reject in
  114. self.ooAttanceAPI.request(.checkinCycle(year, month), completion: { (result) in
  115. let myResult = OOResult<BaseModelClass<OOAttandanceCycleDetail>>(result)
  116. if myResult.isResultSuccess() {
  117. fulfill((myResult.model?.data!)!)
  118. }else{
  119. reject(myResult.error!)
  120. }
  121. })
  122. }
  123. }
  124. // MARK:- 读取指定月份及及本月打卡周期的所有打统统计数据
  125. func getCheckinTotal(_ cycleDetail:OOAttandanceCycleDetail) -> Promise<[OOAttandanceCheckinTotal]> {
  126. let bean = getRequestBean(cycleDetail)
  127. return Promise { fulfill,reject in
  128. self.ooAttanceAPI.request(.checkinTotalForMonth(bean), completion: { (result) in
  129. let myResult = OOResult<BaseModelClass<[OOAttandanceCheckinTotal]>>(result)
  130. if myResult.isResultSuccess() {
  131. fulfill((myResult.model?.data!)!)
  132. }else{
  133. reject(myResult.error!)
  134. }
  135. })
  136. }
  137. }
  138. private func getRequestBean(_ cycleDetail:OOAttandanceCycleDetail) -> OOAttandanceTotalBean {
  139. let bean = OOAttandanceTotalBean()
  140. bean.q_year = cycleDetail.cycleYear
  141. bean.q_month = cycleDetail.cycleMonth
  142. bean.cycleYear = cycleDetail.cycleYear
  143. bean.cycleMonth = cycleDetail.cycleMonth
  144. bean.q_empName = O2AuthSDK.shared.myInfo()?.distinguishedName
  145. return bean
  146. }
  147. // MARK:- 读取考勤分析数据
  148. func getCheckinAnalyze(_ cycleDetail:OOAttandanceCycleDetail) -> Promise<OOAttandanceAnalyze?>{
  149. let bean = getRequestBean(cycleDetail)
  150. return Promise { fulfill,reject in
  151. self.ooAttanceAPI.request(.checkinAnalyze(bean)) { (result) in
  152. let myResult = OOResult<BaseModelClass<[OOAttandanceAnalyze]>>(result)
  153. if myResult.isResultSuccess() {
  154. if let data = myResult.model?.data {
  155. fulfill((data.first)!)
  156. }else{
  157. // reject(OOAppError.common(type: "checkinError", message: "本月无考勤数据", statusCode: 5001))
  158. fulfill(nil)
  159. }
  160. }else{
  161. //let errorMessage = myResult.error?.errorDescription
  162. reject(myResult.error!)
  163. }
  164. }
  165. }
  166. }
  167. // MARK:- 读取配置管理员
  168. func getAttendanceAdmin() -> Promise<[OOAttandanceAdmin]> {
  169. return Promise { fulfill,reject in
  170. self.ooAttanceAPI.request(.attendanceAdmin, completion: { (result) in
  171. let myResult = OOResult<BaseModelClass<[OOAttandanceAdmin]>>(result)
  172. if myResult.isResultSuccess() {
  173. if let data = myResult.model?.data {
  174. fulfill(data)
  175. }else{
  176. reject(OOAppError.common(type: "checkinError", message: "没有配置管理员", statusCode: 5002))
  177. }
  178. }else{
  179. //let errorMessage = myResult.error?.errorDescription
  180. reject(myResult.error!)
  181. }
  182. })
  183. }
  184. }
  185. }
  186. // MARK:- UITableView DataSource
  187. extension OOAttandanceViewModel {
  188. func numberOfSections() -> Int {
  189. return 1
  190. }
  191. func numberOfRowsInSection(_ section: Int) -> Int {
  192. return myAttanceDetailList.count
  193. }
  194. func nodeForIndexPath(_ indexPath:IndexPath) -> OOAttandanceMobileDetail? {
  195. return myAttanceDetailList[indexPath.row]
  196. }
  197. }