// // OutInQueryViewController.swift // MTP2_iOS // // Created by zhongyuan on 2018/3/20. // Copyright © 2018年 zhongyuan.All rights reserved. // import UIKit class OutInQueryViewController: BaseTableViewController, UITextFieldDelegate { @IBOutlet weak var startTimeTextField: UITextField! @IBOutlet weak var endTimeTextField: UITextField! @IBOutlet weak var queryButton: UIButton! @IBOutlet weak var tableView: UITableView! //MARK: TAG值 private static let STARTTIMETEXTFIELDTAG = 100 private static let ENDTIMETEXTFIELDTAG = 101 private lazy var datePickerPopupView = Bundle.main.loadNibNamed("DatePicker", owner: nil, options: nil)?.first as! DatePickerPopupView /// 开始时间 var startTime: Date? /// 结束时间 var endTime: Date? /// 当前页面索引 var pageIndex: Int? override func viewDidLoad() { super.viewDidLoad() addLoadingView() buildView() reloadData() } private func buildView() { /// 添加loading组件 addLoadingView() /// cell重用标识 cellReuseIdentifier = "OutInMoneyTableViewCellID" setupTextField(textField: startTimeTextField) setupTextField(textField: endTimeTextField) } /// 查询 /// /// - Parameter sender: queryButton @IBAction func onClickQueryButton(_ sender: UIButton) { if !checkValue() { return } queryOutInMoney(startTime: startTimeTextField.text!, endTime: endTimeTextField.text!) } func checkValue() -> Bool { if let startTime = self.startTimeTextField.text, let endTime = self.endTimeTextField.text { if startTime.compare(endTime) == .orderedDescending { self.showHintController(title: "提示", message: "开始日期不能大于结束日期") return false }else if self.startTime?.getString(formatter: "yyyy-MM-dd").compare(DateUtils.getLastDate(self.endTime!).getString(formatter: "yyyy-MM-dd")) == .orderedAscending { self.showHintController(title: "提示", message: "查询间隔不能超过一个月") return false }else { return true } }else { self.showHintController(title: "提示", message: "日期不能为空") return false } } func setupTextField(textField: UITextField) { let formatter = DateFormatter() formatter.dateFormat = "yyyy-MM-dd" let startTime = formatter.string(from: DateUtils.getLastDate()) let endTime = formatter.string(from: Date()) /// 设置开始和结束时间 self.startTime = DateUtils.getLastDate() self.endTime = Date() switch textField.tag { case OutInQueryViewController.STARTTIMETEXTFIELDTAG: startTimeTextField.text = startTime case OutInQueryViewController.ENDTIMETEXTFIELDTAG: endTimeTextField.text = endTime default: break } } func reloadData() { queryOutInMoney(startTime: startTime!.toDateZero().getString(), endTime: endTime!.toDateNext().getString()) } private func queryOutInMoney(startTime: String, endTime: String) { /// 异常 guard let bankManager = MTP2BusinessCore.shared.bankManager, let accountManager = MTP2BusinessCore.shared.accountManager, let accountId = accountManager.getCurrentTAAccountInfo()?.accountId else { return } if tableView.contentOffset.y == 0 { _anim?.startAnimating() } if !noDataButton.isHidden { noDataButton.isHidden = true } bankManager.searchClientMoneyOutIn(accountId: accountId, beginDate: startTime, endDate: endTime){ (isSuccess, errorInfo, results) in DispatchQueue.main.async { if self.tableView.contentOffset.y == 0 { self._anim?.stopAnimating() } if isSuccess { /// 查询成功 self.tableCellModels = results ?? [] if self.tableCellModels.count == 0 { self.noDataButton.isHidden = false }else { self.noDataButton.isHidden = true } self.tableView.reloadData() }else { /// 查询失败 } } } } // MARK: - UITextFieldDelegate func textFieldShouldBeginEditing(_ textField: UITextField) -> Bool { datePickerPopupView.delegate = self datePickerPopupView.datePicker.tag = textField.tag textField.tag == OutInQueryViewController.STARTTIMETEXTFIELDTAG ? datePickerPopupView.showInView(self.view, currentDate: startTime!) : datePickerPopupView.showInView(self.view, currentDate: endTime!) return false } } // MARK: - DatePickerPopupViewDelegate extension OutInQueryViewController: DatePickerPopupViewDelegate { func datePickerPopupView(_ sender: DatePickerPopupView, selectedDate date: Date, selectedDateString value: String) { switch sender.datePicker.tag { case OutInQueryViewController.STARTTIMETEXTFIELDTAG: startTimeTextField.text = value startTime = date case OutInQueryViewController.ENDTIMETEXTFIELDTAG: endTimeTextField.text = value endTime = date default: break } sender.hidePopupView() } } // MARK: - PageChangeDelegate extension OutInQueryViewController: PageChangeDelegate { func didShow() { reloadData() } func didHide() { //do something } }