TodoedTaskViewController.swift 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271
  1. //
  2. // TodoedTaskViewController.swift
  3. // O2Platform
  4. //
  5. // Created by 刘振兴 on 16/8/15.
  6. // Copyright © 2016年 zoneland. All rights reserved.
  7. //
  8. import UIKit
  9. import Alamofire
  10. import AlamofireImage
  11. import AlamofireObjectMapper
  12. import ObjectMapper
  13. import CocoaLumberjack
  14. class TodoedTaskViewController: UITableViewController {
  15. var loadUrl:String?
  16. var todoedActions:[TodoedActionModel] = []
  17. var todoedStatus:[TodoedStatusModel] = []
  18. var todoTask:TodoTaskData? {
  19. didSet {
  20. let url = AppDelegate.o2Collect.generateURLWithAppContextKey(TaskedContext.taskedContextKey, query: TaskedContext.taskedDataByIdQuery, parameter: ["##id##":(todoTask?.id)! as AnyObject])
  21. self.loadUrl = url
  22. }
  23. }
  24. private lazy var viewModel: WorkViewModel = {
  25. return WorkViewModel()
  26. }()
  27. override func viewDidLoad() {
  28. super.viewDidLoad()
  29. var t = self.todoTask?.title
  30. if t == nil || t?.isEmpty == true {
  31. t = self.todoTask?.processName ?? ""
  32. }
  33. title = t
  34. self.loadTodoedData()
  35. }
  36. func loadTodoedData(){
  37. if let id = self.todoTask?.id {
  38. self.showLoading(title: "加载中...")
  39. self.viewModel.getReferenc(id: id).then { (reference) in
  40. let workList = reference.workList
  41. let workcompletedList = reference.workCompletedList
  42. if workList.count > 0 {
  43. self.setActionModel(workList,completed: false)
  44. }else if workcompletedList.count > 0 {
  45. self.setActionModel(workcompletedList,completed: true)
  46. }
  47. let worklogList = reference.workLogList
  48. self.setStatusModel(worklogList)
  49. self.tableView.reloadData()
  50. }.always {
  51. self.hideLoading()
  52. }.catch { (err) in
  53. DDLogError(err.localizedDescription)
  54. self.showError(title: "加载失败")
  55. }
  56. }
  57. // AF.request(loadUrl!).responseJSON(completionHandler:{ response in
  58. // debugPrint(response.result)
  59. // switch response.result {
  60. // case .success(let val):
  61. // let data = JSON(val)["data"]
  62. // let type = JSON(val)["type"]
  63. // DDLogDebug(data.description)
  64. // if type == "success" {
  65. // self.setActionModel(data["taskCompleted"].array,completed: true)
  66. // self.setActionModel(data["workCompletedList"].array, completed: true)
  67. // self.setActionModel(data["workList"].array,completed: false)
  68. // self.setStatusModel(Mapper<ActivityTask>().mapArray(JSONString:data["workLogList"].description))
  69. // self.tableView.reloadData()
  70. // self.showSuccess(title: "加载完成")
  71. // }else{
  72. // DDLogError(JSON(val)["message"].description)
  73. // self.showError(title: "加载失败")
  74. // }
  75. // case .failure(let err):
  76. // DDLogError(err.localizedDescription)
  77. // self.showError(title: "加载失败")
  78. // }
  79. // })
  80. }
  81. func setActionModel(_ actionArray:[WorkData],completed:Bool){
  82. for action in actionArray {
  83. if completed {
  84. let title = "\(action.title ?? "") 完成于\(action.completedTime ?? "")"
  85. let id = action.id
  86. let workType = "workCompletedList"
  87. var workTitle = ""
  88. let wtitle = action.title ?? ""
  89. let pName = action.processName ?? ""
  90. if !wtitle.trim().isEmpty {
  91. workTitle = wtitle
  92. }else if !pName.trim().isEmpty {
  93. workTitle = pName
  94. }
  95. let actionModel = TodoedActionModel(destText: title, workType: workType, workId: id, workTitle: workTitle)
  96. self.todoedActions.append(actionModel)
  97. }else{
  98. let title = "\(action.title ?? "") 当前在\(action.activityName ?? "")"
  99. let id = action.id
  100. let workType = "workList"
  101. var workTitle = ""
  102. let wtitle = action.title ?? ""
  103. let pName = action.processName ?? ""
  104. if !wtitle.trim().isEmpty {
  105. workTitle = wtitle
  106. }else if !pName.trim().isEmpty {
  107. workTitle = pName
  108. }
  109. let actionModel = TodoedActionModel(destText: title, workType: workType, workId: id, workTitle: workTitle)
  110. self.todoedActions.append(actionModel)
  111. }
  112. }
  113. }
  114. func setStatusModel(_ statusArray:[ActivityTaskData]){
  115. for task in statusArray {
  116. if task.fromActivityType == "begin" {
  117. continue
  118. }
  119. if (task.taskCompletedList == nil || task.taskCompletedList!.count == 0) && (task.taskList == nil || task.taskList!.count == 0) {
  120. continue
  121. }
  122. // 计算任务数
  123. var identities: [String] = []
  124. if (task.taskCompletedList == nil || task.taskCompletedList!.count == 0) {
  125. if (task.taskList!.count > 0 ) {
  126. task.taskList?.forEach({ (data) in
  127. if let id = data.identity {
  128. identities.append(id)
  129. }
  130. })
  131. }
  132. }else{
  133. // identity = (task.taskCompletedList![0] as! NSDictionary)["identity"]! as! String;
  134. task.taskCompletedList?.forEach({ (data) in
  135. if let id = (data as? NSDictionary)?["identity"] as? String {
  136. identities.append(id)
  137. }
  138. })
  139. }
  140. if identities.count > 0 {
  141. for item in identities {
  142. let activity = task.arrivedActivityName == nil ? task.fromActivityName : "\(task.fromActivityName ?? "") -> \(task.arrivedActivityName ?? "")"
  143. let status = task.routeName == nil ? "正在处理":"选择了【\(task.routeName ?? "")】"
  144. let time = task.arrivedTime == nil ? "到达于:\(task.fromTime ?? "")" : "提交于:\(task.arrivedTime ?? "")"
  145. let identity = item.components(separatedBy: "@").first ?? ""
  146. let statusModel = TodoedStatusModel(activity: activity, identity: identity, status: status, statusTime: time)
  147. self.todoedStatus.append(statusModel)
  148. }
  149. }
  150. }
  151. }
  152. override func didReceiveMemoryWarning() {
  153. super.didReceiveMemoryWarning()
  154. }
  155. // MARK: - Table view data source
  156. override func numberOfSections(in tableView: UITableView) -> Int {
  157. return 2
  158. }
  159. override func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
  160. switch section {
  161. case 0:
  162. return self.todoedActions.count
  163. case 1:
  164. return self.todoedStatus.count
  165. default:
  166. return 0
  167. }
  168. }
  169. override func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
  170. switch (indexPath as NSIndexPath).section {
  171. case 0:
  172. let cell = tableView.dequeueReusableCell(withIdentifier: "todoedActionCell", for: indexPath) as! TodoedActionCell
  173. cell.delegate = self
  174. cell.actionModel = self.todoedActions[(indexPath as NSIndexPath).row]
  175. return cell
  176. case 1:
  177. let cell = tableView.dequeueReusableCell(withIdentifier: "todoedStatusCell", for: indexPath) as! TodoedStatusCell
  178. cell.statusModel = self.todoedStatus[(indexPath as NSIndexPath).row]
  179. return cell
  180. default:
  181. return UITableViewCell(style: .default, reuseIdentifier: "none")
  182. }
  183. }
  184. override func tableView(_ tableView: UITableView, willDisplay cell: UITableViewCell, forRowAt indexPath: IndexPath) {
  185. if (indexPath as NSIndexPath).section == 1{
  186. cell.separatorInset = UIEdgeInsets.zero
  187. cell.layoutMargins = UIEdgeInsets.zero
  188. cell.layer.transform = CATransform3DMakeScale(0.1, 0.1, 1)
  189. //设置动画时间为0.25秒,xy方向缩放的最终值为1
  190. UIView.animate(withDuration: 0.25 * (Double((indexPath as NSIndexPath).row) + 1.0), animations: { () -> Void in
  191. cell.layer.transform = CATransform3DMakeScale(1, 1, 1)
  192. })
  193. }
  194. }
  195. override func tableView(_ tableView: UITableView, heightForRowAt indexPath: IndexPath) -> CGFloat {
  196. switch (indexPath as NSIndexPath).section {
  197. case 0:
  198. return 60.0
  199. case 1:
  200. return 100.0
  201. default:
  202. return 44.0
  203. }
  204. }
  205. override func prepare(for segue: UIStoryboardSegue, sender: Any?) {
  206. if segue.identifier == "showTodoedWork" {
  207. let destVC = segue.destination as! TodoTaskDetailViewController
  208. if let model = sender as? TodoedActionModel {
  209. let id = model.workId ?? ""
  210. let title = model.workTitle ?? ""
  211. let json: String
  212. if model.workType == "workCompletedList" {
  213. json = """
  214. {"workCompleted":"\(id)", "title":"\(title)"}
  215. """
  216. }else {
  217. json = """
  218. {"work":"\(id)", "title":"\(title)"}
  219. """
  220. }
  221. let todo = TodoTask(JSONString: json)
  222. destVC.todoTask = todo
  223. }
  224. destVC.backFlag = 4 // 特殊来源
  225. }
  226. }
  227. }
  228. extension TodoedTaskViewController:TodoedActionCellDelegate{
  229. func open(_ actionModel: TodoedActionModel) {
  230. DDLogDebug(actionModel.workId!)
  231. self.performSegue(withIdentifier: "showTodoedWork", sender: actionModel)
  232. }
  233. }