StatisticsSelectDateView.swift 5.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170
  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. var showCount = 0 //show的次数
  31. override func awakeFromNib() {
  32. super.awakeFromNib()
  33. //本月
  34. month_button.tag = 99+0
  35. //上月
  36. lastmonth_button.tag = 99+1
  37. //3个月
  38. threemonth_button.tag = 99+2
  39. //本年
  40. year_button.tag = 99+3
  41. current_button = month_button
  42. }
  43. //日月年
  44. func setDateType(type:DateType){
  45. dateType = type
  46. if showCount>=1 {return}
  47. switch dateType {
  48. case .day:
  49. month_button.setTitle("昨日", for: .normal)
  50. lastmonth_button.setTitle("今日", for: .normal)
  51. threemonth_button.setTitle("本月", for: .normal)
  52. year_button.setTitle("上月", for: .normal)
  53. current_button.backgroundColor = UIColor("#0B2B4D")!
  54. current_button = threemonth_button
  55. current_button.backgroundColor = UIColor("#597CA1")!
  56. case .month:
  57. month_button.setTitle("本月", for: .normal)
  58. lastmonth_button.setTitle("上月", for: .normal)
  59. threemonth_button.setTitle("近3月", for: .normal)
  60. year_button.setTitle("本年", for: .normal)
  61. current_button.backgroundColor = UIColor("#0B2B4D")!
  62. current_button = year_button
  63. current_button.backgroundColor = UIColor("#597CA1")!
  64. case .year:
  65. month_button.setTitle("本年", for: .normal)
  66. lastmonth_button.setTitle("去年", for: .normal)
  67. threemonth_button.setTitle("近3年", for: .normal)
  68. year_button.setTitle("近5年", for: .normal)
  69. current_button.backgroundColor = UIColor("#0B2B4D")!
  70. current_button = year_button
  71. current_button.backgroundColor = UIColor("#597CA1")!
  72. }
  73. showCount+=1
  74. }
  75. //选中 - func
  76. func selectItemIndex(index:Int){
  77. selectIndex = index
  78. let button = self.viewWithTag(99+index) as! UIButton
  79. // button.backgroundColor = UIColor("#06203A")!
  80. if current_button != button {
  81. current_button.backgroundColor = UIColor("#0B2B4D")!
  82. button.backgroundColor = UIColor("#597CA1")!
  83. current_button = button
  84. }
  85. datehandledo()
  86. }
  87. //计算 - 日期
  88. func datehandledo(){
  89. switch dateType {
  90. case .day:
  91. dayhandledo()
  92. case .month:
  93. monthhandledo()
  94. case .year:
  95. yearhandledo()
  96. }
  97. }
  98. func dayhandledo(){
  99. switch selectIndex {
  100. case 0:
  101. let date = NYDate.getLastDate() //昨日
  102. startdate_button.setTitle(date, for: .normal)
  103. enddate_button.setTitle(date, for: .normal)
  104. case 1:
  105. let date = NYDate.getThisDate() //今日
  106. startdate_button.setTitle(date, for: .normal)
  107. enddate_button.setTitle(date, for: .normal)
  108. case 2:
  109. let dates = NYDate.getDateMonth()//本月
  110. startdate_button.setTitle(dates[0], for: .normal)
  111. enddate_button.setTitle(dates[1], for: .normal)
  112. case 3:
  113. let dates = NYDate.getDateMonthLast()//上月
  114. startdate_button.setTitle(dates[0], for: .normal)
  115. enddate_button.setTitle(dates[1], for: .normal)
  116. default:
  117. let date = NYDate.getLastDate() //昨日
  118. startdate_button.setTitle(date, for: .normal)
  119. enddate_button.setTitle(date, for: .normal)
  120. }
  121. }
  122. func monthhandledo(){
  123. var dates = [""]
  124. switch selectIndex {
  125. case 0:
  126. dates = NYDate.getDateMonth()//本月
  127. case 1:
  128. dates = NYDate.getDateMonthLast()//上月
  129. case 2:
  130. dates = NYDate.getDateMonthSectionNum(num: -2)//近3月
  131. case 3:
  132. dates = NYDate.getDateYear() //本年
  133. default:
  134. let date = NYDate.getLastDate() //昨日
  135. startdate_button.setTitle(date, for: .normal)
  136. enddate_button.setTitle(date, for: .normal)
  137. }
  138. startdate_button.setTitle(String(dates[0].prefix(7)), for: .normal)
  139. enddate_button.setTitle(String(dates[1].prefix(7)), for: .normal)
  140. }
  141. func yearhandledo(){
  142. var dates = [""]
  143. switch selectIndex {
  144. case 0:
  145. dates = NYDate.getDateYear() //本年
  146. case 1:
  147. dates = NYDate.getDateYearSectionNum(num: 1)//去年
  148. startdate_button.setTitle(String(dates[0].prefix(4)), for: .normal)
  149. enddate_button.setTitle(String(dates[0].prefix(4)), for: .normal)
  150. return
  151. case 2:
  152. dates = NYDate.getDateYearSectionNum(num: 2)//近3年
  153. case 3:
  154. dates = NYDate.getDateYearSectionNum(num: 4)//近5年
  155. default:
  156. let date = NYDate.getLastDate() //昨日
  157. startdate_button.setTitle(date, for: .normal)
  158. enddate_button.setTitle(date, for: .normal)
  159. }
  160. startdate_button.setTitle(String(dates[0].prefix(4)), for: .normal)
  161. enddate_button.setTitle(String(dates[1].prefix(4)), for: .normal)
  162. }
  163. }