WorkViewModel.swift 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363
  1. //
  2. // WorkViewModel.swift
  3. // O2Platform
  4. //
  5. // Created by FancyLou on 2021/2/24.
  6. // Copyright © 2021 zoneland. All rights reserved.
  7. //
  8. import Promises
  9. import CocoaLumberjack
  10. class WorkViewModel : NSObject {
  11. override init() {
  12. super.init()
  13. }
  14. private let pageCount = 15
  15. private let api = OOMoyaProvider<OOApplicationAPI>()
  16. }
  17. extension WorkViewModel {
  18. /// 分页查询待办列表
  19. func taskListNext(lastId: String) -> Promise<[TodoCellModel<TodoTaskData>]> {
  20. return Promise{ fulfill, reject in
  21. self.api.request(.taskListNext(lastId, self.pageCount), completion: {result in
  22. let response = OOResult<BaseModelClass<[TodoTaskData]>>(result)
  23. if response.isResultSuccess() {
  24. if let list = response.model?.data {
  25. var taskList:[TodoCellModel<TodoTaskData>] = []
  26. for task in list {
  27. let model = TodoCellModel<TodoTaskData>(title: task.title,applicationName: task.applicationName,status: task.activityName,time: task.updateTime,sourceObj: task)
  28. taskList.append(model)
  29. }
  30. fulfill(taskList)
  31. }else {
  32. reject(OOAppError.apiEmptyResultError)
  33. }
  34. }else {
  35. reject(response.error!)
  36. }
  37. })
  38. }
  39. }
  40. /// 分页查询已办列表
  41. func taskcompletedListNext(lastId: String, key: String) -> Promise<[TodoCellModel<TodoTaskData>]> {
  42. return Promise{ fulfill, reject in
  43. self.api.request(.taskcompletedListNext(lastId, self.pageCount, key), completion: {result in
  44. let response = OOResult<BaseModelClass<[TodoTaskData]>>(result)
  45. if response.isResultSuccess() {
  46. if let list = response.model?.data {
  47. var taskList:[TodoCellModel<TodoTaskData>] = []
  48. for task in list {
  49. let model = TodoCellModel<TodoTaskData>(title: task.title,applicationName: task.applicationName,status: task.activityName,time: task.updateTime,sourceObj: task)
  50. taskList.append(model)
  51. }
  52. fulfill(taskList)
  53. }else {
  54. reject(OOAppError.apiEmptyResultError)
  55. }
  56. }else {
  57. reject(response.error!)
  58. }
  59. })
  60. }
  61. }
  62. /// 待阅
  63. func readListNext(lastId: String) -> Promise<[TodoCellModel<TodoTaskData>]> {
  64. return Promise{ fulfill, reject in
  65. self.api.request(.readListNext(lastId, self.pageCount), completion: {result in
  66. let response = OOResult<BaseModelClass<[TodoTaskData]>>(result)
  67. if response.isResultSuccess() {
  68. if let list = response.model?.data {
  69. var taskList:[TodoCellModel<TodoTaskData>] = []
  70. for task in list {
  71. let model = TodoCellModel<TodoTaskData>(title: task.title,applicationName: task.applicationName,status: task.activityName,time: task.updateTime,sourceObj: task)
  72. taskList.append(model)
  73. }
  74. fulfill(taskList)
  75. }else {
  76. reject(OOAppError.apiEmptyResultError)
  77. }
  78. }else {
  79. reject(response.error!)
  80. }
  81. })
  82. }
  83. }
  84. /// 已阅
  85. func readcompletedListNext(lastId: String) -> Promise<[TodoCellModel<TodoTaskData>]> {
  86. return Promise{ fulfill, reject in
  87. self.api.request(.readcompletedListNext(lastId, self.pageCount), completion: {result in
  88. let response = OOResult<BaseModelClass<[TodoTaskData]>>(result)
  89. if response.isResultSuccess() {
  90. if let list = response.model?.data {
  91. var taskList:[TodoCellModel<TodoTaskData>] = []
  92. for task in list {
  93. let model = TodoCellModel<TodoTaskData>(title: task.title,applicationName: task.applicationName,status: task.activityName,time: task.updateTime,sourceObj: task)
  94. taskList.append(model)
  95. }
  96. fulfill(taskList)
  97. }else {
  98. reject(OOAppError.apiEmptyResultError)
  99. }
  100. }else {
  101. reject(response.error!)
  102. }
  103. })
  104. }
  105. }
  106. /// 已办参考数据 包含worklog
  107. func getReferenc(id: String) -> Promise<TaskCompletedReference> {
  108. return Promise { fulfill, reject in
  109. self.api.request(.taskcompletedGetReference(id), completion: { result in
  110. let response = OOResult<BaseModelClass<TaskCompletedReference>>(result)
  111. if response.isResultSuccess() {
  112. if let reference = response.model?.data {
  113. fulfill(reference)
  114. }else {
  115. reject(OOAppError.apiEmptyResultError)
  116. }
  117. }else {
  118. reject(response.error!)
  119. }
  120. })
  121. }
  122. }
  123. /// 删除工作
  124. func deleteWork(workId: String) -> Promise<Bool> {
  125. return Promise{ fulfill, reject in
  126. self.api.request(.workDelete(workId), completion: { result in
  127. let response = OOResult<BaseModelClass<OOCommonIdModel>>(result)
  128. if response.isResultSuccess() {
  129. fulfill(true)
  130. } else {
  131. reject(response.error!)
  132. }
  133. })
  134. }
  135. }
  136. /// 保存工作
  137. func saveWorkData(workId: String, data: [String: AnyObject]) -> Promise<Bool> {
  138. return Promise{ fulfill, reject in
  139. self.api.request(.dataUpdateWithWork(workId, data), completion: { result in
  140. let response = OOResult<BaseModelClass<OOCommonIdModel>>(result)
  141. if response.isResultSuccess() {
  142. fulfill(true)
  143. } else {
  144. reject(response.error!)
  145. }
  146. })
  147. }
  148. }
  149. /// 上传附件
  150. func uploadAttachment(workId: String, site: String, fileName:String, fileData: Data) -> Promise<OOCommonIdModel> {
  151. return Promise { fulfill, reject in
  152. self.api.request(.attachmentUpload(workId, site, fileName, fileData), completion: { result in
  153. let response = OOResult<BaseModelClass<OOCommonIdModel>>(result)
  154. if response.isResultSuccess() {
  155. if let idData = response.model?.data {
  156. fulfill(idData)
  157. }else {
  158. reject(OOAppError.apiEmptyResultError)
  159. }
  160. } else {
  161. reject(response.error!)
  162. }
  163. })
  164. }
  165. }
  166. /// 替换附件
  167. func replaceAttachment(id: String, workId: String, site: String, fileName: String, fileData: Data) -> Promise<OOCommonIdModel> {
  168. return Promise { fulfill, reject in
  169. self.api.request(.attachmentReplace(id, workId, site, fileName, fileData), completion: { result in
  170. let response = OOResult<BaseModelClass<OOCommonIdModel>>(result)
  171. if response.isResultSuccess() {
  172. if let idData = response.model?.data {
  173. fulfill(idData)
  174. }else {
  175. reject(OOAppError.apiEmptyResultError)
  176. }
  177. } else {
  178. reject(response.error!)
  179. }
  180. })
  181. }
  182. }
  183. /// work的附件
  184. func getWorkAttachment(workId: String, id: String) -> Promise<String> {
  185. return Promise { fulfill, reject in
  186. self.getAttachmentWithWork(workId:workId, id: id).then { (info) -> Promise<URL> in
  187. return self.downloadAttachmentWithWorkId(id: id, workId: workId, info: info)
  188. }.then { (url) in
  189. fulfill(url.path)
  190. }.catch { (err) in
  191. reject(err)
  192. }
  193. }
  194. }
  195. /// workcompleted的附件
  196. func getWorkcompletedAttachment(workcompleted: String, id: String) -> Promise<String> {
  197. return Promise { fulfill, reject in
  198. self.getAttachmentWithWorkCompleted(workCompletedId: workcompleted, id: id).then { (info) -> Promise<URL> in
  199. return self.downloadAttachmentWithWorkcompleted(id: id, workcompleted: workcompleted, info: info)
  200. }.then { (url) in
  201. fulfill(url.path)
  202. }.catch { (err) in
  203. reject(err)
  204. }
  205. }
  206. }
  207. /// 获取附件对象 新api 老版本的系统不支持 换回老的api @Date 2021-06-05
  208. // func getAttachmentWithworkOrWorkcompleted(workOrWorkcompleted: String, id: String) -> Promise<O2WorkAttachmentInfo> {
  209. // return Promise { fulfill, reject in
  210. // self.api.request(.attachmentGetWithWorkOrWorkCompleted(workOrWorkcompleted, id), completion: { result in
  211. // let response = OOResult<BaseModelClass<O2WorkAttachmentInfo>>(result)
  212. // if response.isResultSuccess() {
  213. // if let atta = response.model?.data {
  214. // fulfill(atta)
  215. // }else {
  216. // reject(OOAppError.apiEmptyResultError)
  217. // }
  218. // } else {
  219. // reject(response.error!)
  220. // }
  221. // })
  222. // }
  223. // }
  224. // 工作的附件对象
  225. private func getAttachmentWithWork(workId: String, id: String) -> Promise<O2WorkAttachmentInfo> {
  226. return Promise { fulfill, reject in
  227. self.api.request(.attachmentGetWithWork(workId, id), completion: { result in
  228. let response = OOResult<BaseModelClass<O2WorkAttachmentInfo>>(result)
  229. if response.isResultSuccess() {
  230. if let atta = response.model?.data {
  231. fulfill(atta)
  232. }else {
  233. reject(OOAppError.apiEmptyResultError)
  234. }
  235. } else {
  236. reject(response.error!)
  237. }
  238. })
  239. }
  240. }
  241. // 已完成工作的附件对象
  242. private func getAttachmentWithWorkCompleted(workCompletedId: String, id: String) -> Promise<O2WorkAttachmentInfo> {
  243. return Promise { fulfill, reject in
  244. self.api.request(.attachmentGetWithWorkCompleted(workCompletedId, id), completion: { result in
  245. let response = OOResult<BaseModelClass<O2WorkAttachmentInfo>>(result)
  246. if response.isResultSuccess() {
  247. if let atta = response.model?.data {
  248. fulfill(atta)
  249. }else {
  250. reject(OOAppError.apiEmptyResultError)
  251. }
  252. } else {
  253. reject(response.error!)
  254. }
  255. })
  256. }
  257. }
  258. /// 下载附件
  259. private func downloadAttachmentWithWorkId(id: String, workId: String, info: O2WorkAttachmentInfo) -> Promise<URL> {
  260. let path = FileUtil.share.cacheDir().appendingPathComponent("\(info.name ?? "temp").\(info.extension ?? "")")
  261. DDLogDebug("file Path: \(path)")
  262. if FileUtil.share.fileExist(filePath: path.path) {
  263. if let updateTime = info.updateTime, let updateDate = Date.date(updateTime), let fileDate = FileUtil.share.fileModificationDate(filePath: path.path) as Date? {
  264. DDLogDebug("file updateDate: \(updateDate) fileDate: \(fileDate)")
  265. if updateDate > (fileDate) { // 重新下载
  266. return Promise { fulfill, reject in
  267. self.api.request(.attachmentDownloadWithWorkId(id, workId, path), completion: { result in
  268. switch result {
  269. case .success(_):
  270. DDLogError("重新下载成功。。。。。")
  271. fulfill(path)
  272. break
  273. case .failure(let err):
  274. DDLogError(err.localizedDescription)
  275. reject(err)
  276. break
  277. }
  278. })
  279. }
  280. }
  281. }
  282. return Promise { fulfill, reject in
  283. fulfill(path)
  284. }
  285. }else {
  286. return Promise { fulfill, reject in
  287. self.api.request(.attachmentDownloadWithWorkId(id, workId, path), completion: { result in
  288. switch result {
  289. case .success(_):
  290. DDLogError("下载成功。。。。。")
  291. fulfill(path)
  292. break
  293. case .failure(let err):
  294. DDLogError(err.localizedDescription)
  295. reject(err)
  296. break
  297. }
  298. })
  299. }
  300. }
  301. }
  302. /// 下载附件
  303. private func downloadAttachmentWithWorkcompleted(id: String, workcompleted: String, info: O2WorkAttachmentInfo) -> Promise<URL> {
  304. let path = FileUtil.share.cacheDir().appendingPathComponent("\(info.name ?? "temp").\(info.extension ?? "")")
  305. DDLogDebug("file Path: \(path)")
  306. if FileUtil.share.fileExist(filePath: path.path) {
  307. if let updateTime = info.updateTime, let updateDate = Date.date(updateTime), let fileDate = FileUtil.share.fileModificationDate(filePath: path.path) as Date? {
  308. DDLogDebug("file updateDate: \(updateDate) fileDate: \(fileDate)")
  309. if updateDate > (fileDate) { // 重新下载
  310. return Promise { fulfill, reject in
  311. self.api.request(.attachmentDownloadWithWorkCompleted(id, workcompleted, path), completion: { result in
  312. switch result {
  313. case .success(_):
  314. DDLogError("重新下载成功。。。。。")
  315. fulfill(path)
  316. break
  317. case .failure(let err):
  318. DDLogError(err.localizedDescription)
  319. reject(err)
  320. break
  321. }
  322. })
  323. }
  324. }
  325. }
  326. return Promise { fulfill, reject in
  327. fulfill(path)
  328. }
  329. }else {
  330. return Promise { fulfill, reject in
  331. self.api.request(.attachmentDownloadWithWorkCompleted(id, workcompleted, path), completion: { result in
  332. switch result {
  333. case .success(_):
  334. DDLogError("下载成功。。。。。")
  335. fulfill(path)
  336. break
  337. case .failure(let err):
  338. DDLogError(err.localizedDescription)
  339. reject(err)
  340. break
  341. }
  342. })
  343. }
  344. }
  345. }
  346. }