StatisticsSelectDateView.swift 5.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167
  1. //
  2. // StatisticsSelectDateView.swift
  3. // JiaPeiManage
  4. //
  5. // Created by Ning.ge on 2023/6/29.
  6. //
  7. final class StatisticsSelectDateView: UIView,NibLoadable {
  8. //开始时间
  9. @IBOutlet weak var startdate_button: UIButton!
  10. //结束时间
  11. @IBOutlet weak var enddate_button: UIButton!
  12. //本月
  13. @IBOutlet weak var month_button: UIButton!
  14. //上月
  15. @IBOutlet weak var lastmonth_button: UIButton!
  16. //3个月
  17. @IBOutlet weak var threemonth_button: UIButton!
  18. //本年
  19. @IBOutlet weak var year_button: UIButton!
  20. //重置
  21. @IBOutlet weak var reset_button: UIButton!
  22. //确定
  23. @IBOutlet weak var confirm_button: UIButton!
  24. //选中的 btn
  25. public var current_button: UIButton!
  26. var dateType:DateType = .day
  27. var startDate:String = ""
  28. var endDate:String = ""
  29. var selectIndex = 0
  30. override func awakeFromNib() {
  31. super.awakeFromNib()
  32. //本月
  33. month_button.tag = 99+0
  34. //上月
  35. lastmonth_button.tag = 99+1
  36. //3个月
  37. threemonth_button.tag = 99+2
  38. //本年
  39. year_button.tag = 99+3
  40. current_button = month_button
  41. }
  42. //日月年
  43. func setDateType(type:DateType){
  44. dateType = type
  45. switch dateType {
  46. case .day:
  47. month_button.setTitle("昨日", for: .normal)
  48. lastmonth_button.setTitle("今日", for: .normal)
  49. threemonth_button.setTitle("本月", for: .normal)
  50. year_button.setTitle("上月", for: .normal)
  51. current_button.backgroundColor = UIColor("#0B2B4D")!
  52. current_button = threemonth_button
  53. current_button.backgroundColor = UIColor("#597CA1")!
  54. case .month:
  55. month_button.setTitle("本月", for: .normal)
  56. lastmonth_button.setTitle("上月", for: .normal)
  57. threemonth_button.setTitle("近3月", for: .normal)
  58. year_button.setTitle("本年", for: .normal)
  59. current_button.backgroundColor = UIColor("#0B2B4D")!
  60. current_button = threemonth_button
  61. current_button.backgroundColor = UIColor("#597CA1")!
  62. case .year:
  63. month_button.setTitle("本年", for: .normal)
  64. lastmonth_button.setTitle("去年", for: .normal)
  65. threemonth_button.setTitle("近3年", for: .normal)
  66. year_button.setTitle("近5年", for: .normal)
  67. current_button.backgroundColor = UIColor("#0B2B4D")!
  68. current_button = year_button
  69. current_button.backgroundColor = UIColor("#597CA1")!
  70. }
  71. }
  72. //选中 - func
  73. func selectItemIndex(index:Int){
  74. selectIndex = index
  75. let button = self.viewWithTag(99+index) as! UIButton
  76. // button.backgroundColor = UIColor("#06203A")!
  77. if current_button != button {
  78. current_button.backgroundColor = UIColor("#0B2B4D")!
  79. button.backgroundColor = UIColor("#597CA1")!
  80. current_button = button
  81. datehandledo()
  82. }
  83. }
  84. //计算 - 日期
  85. func datehandledo(){
  86. switch dateType {
  87. case .day:
  88. dayhandledo()
  89. case .month:
  90. monthhandledo()
  91. case .year:
  92. yearhandledo()
  93. }
  94. }
  95. func dayhandledo(){
  96. switch selectIndex {
  97. case 0:
  98. let date = NYDate.getLastDate() //昨日
  99. startdate_button.setTitle(date, for: .normal)
  100. enddate_button.setTitle(date, for: .normal)
  101. case 1:
  102. let date = NYDate.getThisDate() //今日
  103. startdate_button.setTitle(date, for: .normal)
  104. enddate_button.setTitle(date, for: .normal)
  105. case 2:
  106. let dates = NYDate.getDateMonth()//本月
  107. startdate_button.setTitle(dates[0], for: .normal)
  108. enddate_button.setTitle(dates[1], for: .normal)
  109. case 3:
  110. let dates = NYDate.getDateMonthLast()//上月
  111. startdate_button.setTitle(dates[0], for: .normal)
  112. enddate_button.setTitle(dates[1], for: .normal)
  113. default:
  114. let date = NYDate.getLastDate() //昨日
  115. startdate_button.setTitle(date, for: .normal)
  116. enddate_button.setTitle(date, for: .normal)
  117. }
  118. }
  119. func monthhandledo(){
  120. var dates = [""]
  121. switch selectIndex {
  122. case 0:
  123. dates = NYDate.getDateMonth()//本月
  124. case 1:
  125. dates = NYDate.getDateMonthLast()//上月
  126. case 2:
  127. dates = NYDate.getDateMonthSectionNum(num: -2)//近3月
  128. case 3:
  129. dates = NYDate.getDateYear() //本年
  130. default:
  131. let date = NYDate.getLastDate() //昨日
  132. startdate_button.setTitle(date, for: .normal)
  133. enddate_button.setTitle(date, for: .normal)
  134. }
  135. startdate_button.setTitle(String(dates[0].prefix(7)), for: .normal)
  136. enddate_button.setTitle(String(dates[1].prefix(7)), for: .normal)
  137. }
  138. func yearhandledo(){
  139. var dates = [""]
  140. switch selectIndex {
  141. case 0:
  142. dates = NYDate.getDateYear() //本年
  143. case 1:
  144. dates = NYDate.getDateYearSectionNum(num: 1)//去年
  145. startdate_button.setTitle(String(dates[0].prefix(4)), for: .normal)
  146. enddate_button.setTitle(String(dates[0].prefix(4)), for: .normal)
  147. return
  148. case 2:
  149. dates = NYDate.getDateYearSectionNum(num: 2)//近3年
  150. case 3:
  151. dates = NYDate.getDateYearSectionNum(num: 4)//近5年
  152. default:
  153. let date = NYDate.getLastDate() //昨日
  154. startdate_button.setTitle(date, for: .normal)
  155. enddate_button.setTitle(date, for: .normal)
  156. }
  157. startdate_button.setTitle(String(dates[0].prefix(4)), for: .normal)
  158. enddate_button.setTitle(String(dates[1].prefix(4)), for: .normal)
  159. }
  160. }