OOAttanceTotalController.swift 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133
  1. //
  2. // OOAttanceTotalController.swift
  3. // O2Platform
  4. //
  5. // Created by 刘振兴 on 2018/5/14.
  6. // Copyright © 2018年 zoneland. All rights reserved.
  7. //
  8. import UIKit
  9. import Promises
  10. class OOAttanceTotalController: UITableViewController {
  11. private var viewModel:OOAttandanceViewModel = {
  12. return OOAttandanceViewModel()
  13. }()
  14. private lazy var headerView:OOAttandanceTotalHeaderView = {
  15. let view = Bundle.main.loadNibNamed("OOAttandanceTotalHeaderView", owner: self, options: nil)?.first as! OOAttandanceTotalHeaderView
  16. return view
  17. }()
  18. private var models:[OOAttandanceCheckinTotal] = []
  19. private var year: String = {
  20. let currentDate = Date()
  21. return String(currentDate.year)
  22. }()
  23. private var month: String = {
  24. let currentDate = Date()
  25. return currentDate.month > 9 ? "\(currentDate.month)" : "0\(currentDate.month)"
  26. }()
  27. override func viewDidLoad() {
  28. super.viewDidLoad()
  29. NotificationCenter.default.addObserver(self, selector: #selector(showDatePicker(_:)), name: OONotification.staticsTotal.notificationName, object: nil)
  30. tableView.register(UINib.init(nibName: "OOAttandanceTotalItemCell", bundle: nil), forCellReuseIdentifier: "OOAttandanceTotalItemCell")
  31. tableView.mj_header = MJRefreshNormalHeader(refreshingBlock: {
  32. self.getTotalDetailList()
  33. })
  34. getTotalDetailList()
  35. }
  36. @objc private func showDatePicker(_ notification:Notification) {
  37. let datePicker = DatePickerView.datePicker(style: .yearMonth, scrollToDate: Date()) { date in
  38. guard let d = date else { return }
  39. self.year = d.toString("yyyy")
  40. self.month = d.toString("MM")
  41. DispatchQueue.main.async {
  42. self.getTotalDetailList()
  43. }
  44. }
  45. datePicker.show()
  46. }
  47. func getTotalDetailList(){
  48. self.showLoading()
  49. viewModel.getCheckinCycle(self.year, self.month).then { (cycleDetail) -> Promise<(OOAttandanceAnalyze?, [OOAttandanceCheckinTotal])> in
  50. self.headerView.requestBean = cycleDetail
  51. return all(self.viewModel.getCheckinAnalyze(cycleDetail), self.viewModel.getCheckinTotal(cycleDetail))
  52. }.always {
  53. self.hideLoading()
  54. }.then { (result) in
  55. self.headerView.config(withItem: result.0)
  56. let list = result.1
  57. self.models.removeAll()
  58. if !list.isEmpty {
  59. let newList = list.sorted { (f, s) -> Bool in
  60. if let fd = f.recordDateString, let sd = s.recordDateString {
  61. return fd.toDate(formatter: "yyyy-MM-dd") < sd.toDate(formatter: "yyyy-MM-dd")
  62. }else {
  63. return false
  64. }
  65. }
  66. self.models.append(contentsOf: newList)
  67. }
  68. self.tableView.reloadData()
  69. if self.tableView.mj_header.isRefreshing() {
  70. self.tableView.mj_header.endRefreshing()
  71. }
  72. }.catch { (myError) in
  73. let customError = myError as? OOAppError
  74. self.showError(title: (customError?.failureReason)!)
  75. }
  76. }
  77. @objc func closeWindow() {
  78. self.tabBarController?.navigationController?.dismiss(animated: true, completion: nil)
  79. }
  80. override func didReceiveMemoryWarning() {
  81. super.didReceiveMemoryWarning()
  82. // Dispose of any resources that can be recreated.
  83. }
  84. // MARK: - Table view data source
  85. override func numberOfSections(in tableView: UITableView) -> Int {
  86. return 1
  87. }
  88. override func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
  89. return models.count
  90. }
  91. override func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
  92. let cell = tableView.dequeueReusableCell(withIdentifier: "OOAttandanceTotalItemCell", for: indexPath) as! (OOAttandanceTotalItemCell & Configurable)
  93. let item = models[indexPath.row]
  94. cell.config(withItem: item)
  95. return cell
  96. }
  97. override func tableView(_ tableView: UITableView, viewForHeaderInSection section: Int) -> UIView? {
  98. return headerView
  99. }
  100. override func tableView(_ tableView: UITableView, heightForHeaderInSection section: Int) -> CGFloat {
  101. return 130.0
  102. }
  103. override func tableView(_ tableView: UITableView, viewForFooterInSection section: Int) -> UIView? {
  104. return UIView()
  105. }
  106. deinit {
  107. NotificationCenter.default.removeObserver(self)
  108. }
  109. }