OutInQueryViewController.swift 5.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170
  1. //
  2. // OutInQueryViewController.swift
  3. // MTP2_iOS
  4. //
  5. // Created by zhongyuan on 2018/3/20.
  6. // Copyright © 2018年 zhongyuan.All rights reserved.
  7. //
  8. import UIKit
  9. import SwiftDate
  10. class OutInQueryViewController: BaseTableViewController<MoMoneyOutIn>, UITextFieldDelegate {
  11. @IBOutlet weak var startTimeTextField: UITextField!
  12. @IBOutlet weak var endTimeTextField: UITextField!
  13. @IBOutlet weak var queryButton: UIButton!
  14. @IBOutlet weak var tableView: UITableView!
  15. //MARK: TAG值
  16. private static let STARTTIMETEXTFIELDTAG = 100
  17. private static let ENDTIMETEXTFIELDTAG = 101
  18. private lazy var datePickerPopupView = Bundle.main.loadNibNamed("DatePicker", owner: nil, options: nil)?.first as! DatePickerPopupView
  19. /// 开始时间
  20. var startTime: Date?
  21. /// 结束时间
  22. var endTime: Date?
  23. /// 当前页面索引
  24. var pageIndex: Int?
  25. override func viewDidLoad() {
  26. super.viewDidLoad()
  27. addLoadingView()
  28. buildView()
  29. reloadData()
  30. }
  31. private func buildView() {
  32. /// 添加loading组件
  33. addLoadingView()
  34. /// cell重用标识
  35. cellReuseIdentifier = "OutInMoneyTableViewCellID"
  36. setupTextField(textField: startTimeTextField)
  37. setupTextField(textField: endTimeTextField)
  38. }
  39. /// 查询
  40. ///
  41. /// - Parameter sender: queryButton
  42. @IBAction func onClickQueryButton(_ sender: UIButton) {
  43. if !checkValue() { return }
  44. queryOutInMoney(startTime: startTimeTextField.text!, endTime: endTimeTextField.text!)
  45. }
  46. func checkValue() -> Bool {
  47. if let startTime = self.startTimeTextField.text,
  48. let endTime = self.endTimeTextField.text {
  49. if startTime.compare(endTime) == .orderedDescending {
  50. self.showHintController(title: "提示", message: "开始日期不能大于结束日期")
  51. return false
  52. }else if self.startTime?.getString(formatter: "yyyy-MM-dd").compare(DateUtils.getLastDate(self.endTime!).getString(formatter: "yyyy-MM-dd")) == .orderedAscending {
  53. self.showHintController(title: "提示", message: "查询间隔不能超过一个月")
  54. return false
  55. }else {
  56. return true
  57. }
  58. }else {
  59. self.showHintController(title: "提示", message: "日期不能为空")
  60. return false
  61. }
  62. }
  63. func setupTextField(textField: UITextField) {
  64. let formatter = DateFormatter()
  65. formatter.dateFormat = "yyyy-MM-dd"
  66. let startTime = formatter.string(from: Date()-7.days)
  67. let endTime = formatter.string(from: Date())
  68. /// 设置开始和结束时间
  69. self.startTime = Date()-7.days
  70. self.endTime = Date()
  71. switch textField.tag {
  72. case OutInQueryViewController.STARTTIMETEXTFIELDTAG:
  73. startTimeTextField.text = startTime
  74. case OutInQueryViewController.ENDTIMETEXTFIELDTAG:
  75. endTimeTextField.text = endTime
  76. default:
  77. break
  78. }
  79. }
  80. func reloadData() {
  81. queryOutInMoney(startTime: startTime!.toDateZero().getString(), endTime: endTime!.toDateNext().getString())
  82. }
  83. private func queryOutInMoney(startTime: String, endTime: String) {
  84. /// 异常
  85. guard let bankManager = MTP2BusinessCore.shared.bankManager,
  86. let accountManager = MTP2BusinessCore.shared.accountManager,
  87. let accountId = accountManager.getCurrentTAAccountInfo()?.accountId else { return }
  88. if tableView.contentOffset.y == 0 {
  89. _anim?.startAnimating()
  90. }
  91. if !noDataButton.isHidden {
  92. noDataButton.isHidden = true
  93. }
  94. bankManager.searchClientMoneyOutIn(accountId: accountId, beginDate: startTime, endDate: endTime){ (isSuccess, errorInfo, results) in
  95. DispatchQueue.main.async {
  96. if self.tableView.contentOffset.y == 0 {
  97. self._anim?.stopAnimating()
  98. }
  99. if isSuccess {
  100. /// 查询成功
  101. self.tableCellModels = results ?? []
  102. if self.tableCellModels.count == 0 {
  103. self.noDataButton.isHidden = false
  104. }else {
  105. self.noDataButton.isHidden = true
  106. }
  107. self.tableView.reloadData()
  108. }else {
  109. /// 查询失败
  110. }
  111. }
  112. }
  113. }
  114. // MARK: - UITextFieldDelegate
  115. func textFieldShouldBeginEditing(_ textField: UITextField) -> Bool {
  116. datePickerPopupView.delegate = self
  117. datePickerPopupView.datePicker.tag = textField.tag
  118. textField.tag == OutInQueryViewController.STARTTIMETEXTFIELDTAG ? datePickerPopupView.showInView(self.view, currentDate: startTime!) : datePickerPopupView.showInView(self.view, currentDate: endTime!)
  119. return false
  120. }
  121. }
  122. // MARK: - DatePickerPopupViewDelegate
  123. extension OutInQueryViewController: DatePickerPopupViewDelegate {
  124. func datePickerPopupView(_ sender: DatePickerPopupView, selectedDate date: Date, selectedDateString value: String) {
  125. switch sender.datePicker.tag {
  126. case OutInQueryViewController.STARTTIMETEXTFIELDTAG:
  127. startTimeTextField.text = value
  128. startTime = date
  129. case OutInQueryViewController.ENDTIMETEXTFIELDTAG:
  130. endTimeTextField.text = value
  131. endTime = date
  132. default:
  133. break
  134. }
  135. sender.hidePopupView()
  136. }
  137. }
  138. // MARK: - PageChangeDelegate
  139. extension OutInQueryViewController: PageChangeDelegate {
  140. func didShow() {
  141. reloadData()
  142. }
  143. func didHide() {
  144. //do something
  145. }
  146. }