r
2025-09-19 8c309ff419690cc77c9b178096878e18d4849fc2
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
//
//  YardDetailDateTimeVC.swift
//  WanPai
//
//  Created by 无故事王国 on 2023/6/19.
//
 
import UIKit
import JQTools
import RxRelay
import RxSwift
 
 
let UpdateDetailDate_Noti = Notification.Name.init("UpdateDetailDate_Noti")
 
class YardDetailDateTimeVC: BaseVC {
 
    private var collectionView:UICollectionView!
    private var clouse:((Double,Int)->Void)?
    private(set) var date:Date!
    private var id:Int!
    private var currentPage:Int!
    private var items = [SiteDetailDateTimeModel]()
 
    var drop1:Int?
    var drop2:Int?
    var selects = [SiteDetailDateTimeModel]()
    private let cellW = (JQ_ScreenW - 32.0) / 3.0
    private let cellH = (JQ_ScreenW - 32.0) / 3.0 * 0.3167
 
    private var halfName:String?
    private var siteName:String?
 
        /// 缓存高度
    private(set) var innerHeight:Double = 0
 
    required init(id:Int,date:Date,currentPage:Int,halfName:String?,siteName:String?) {
        super.init(nibName: nil, bundle: nil)
        self.date = date
        self.id = id
        self.currentPage = currentPage
        self.halfName = halfName
        self.siteName = siteName
    }
 
    required init?(coder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
 
 
    override func viewDidLoad() {
        super.viewDidLoad()
 
        let layout = UICollectionViewFlowLayout()
        layout.itemSize = CGSize(width: cellW, height: cellH)
        layout.minimumLineSpacing = 1
        layout.minimumInteritemSpacing = 1
        collectionView = UICollectionView(frame: .zero,collectionViewLayout: layout)
        collectionView.delegate = self
        collectionView.dataSource = self
        collectionView.isScrollEnabled = false
        collectionView.register(UINib(nibName: "CalendarDateTimeTCell", bundle: nil), forCellWithReuseIdentifier: "_CalendarDateTimeTCell")
        view.addSubview(collectionView)
        collectionView.backgroundColor = .white
        collectionView.snp.makeConstraints { make in
            make.edges.equalToSuperview()
        }
 
        querySiteTimes(halfName: halfName, siteName: siteName)
    }
 
    override func setRx() {
        NotificationCenter.default.rx.notification(UpdateDetailDate_Noti, object: nil).take(until: self.rx.deallocated).subscribe(onNext: {[weak self] data in
            guard let weakSelf = self else { return }
            self?.clearALL()
            self?.querySiteTimes(halfName: weakSelf.halfName, siteName: weakSelf.siteName)
        }).disposed(by: disposeBag)
    }
 
    //清除水
    public func clearALL(){
        drop1 = nil
        drop2 = nil
        selects.removeAll()
        var val = YardDetailDateManager.shared().dates.value
        val.removeAll()
        YardDetailDateManager.shared().dates.accept(val)
 
        collectionView.reloadData()
    }
 
    public func querySiteTimes(halfName:String?,siteName:String?){
        self.halfName = halfName
        self.siteName = siteName
        Services.querySiteTimes(id: id, day: date.string(withFormat: "yyyy-MM-dd"),halfName: halfName,siteName:siteName).subscribe(onNext: { [weak self] data in
            if let models = data.data{
                guard let weakSelf = self else { return }
                weakSelf.items = models
                for item in weakSelf.items {
                    if item.selectable == 1{
                        let d = weakSelf.date.jq_format("yyyy-MM-dd ") + item.time.components(separatedBy: "-").last!
                        let interval = Date.jq_StringToTimeInterval(d, "yyyy-MM-dd HH:mm")
                        let canSelect = Date().timeIntervalSince1970 < interval
                        item.selectable = canSelect ? 1:0
                    }
                }
                weakSelf.collectionView.reloadData()
                weakSelf.innerHeight = ceil(Double(models.count) / 3.0) * weakSelf.cellH + floor(Double(models.count) / 3.0) * 1.0
                weakSelf.clouse?(weakSelf.innerHeight,weakSelf.currentPage)
            }
        }).disposed(by: disposeBag)
    }
 
 
        /// 获得请求后,更新的高度,来使父更新并缓存,滑动时获取
    func updateInnerHeight(clouse:@escaping (Double,Int)->Void){
        self.clouse = clouse
    }
 
    private func dropDate(_ item:Int){
        var tempSelects = [SiteDetailDateTimeModel]()
        ///临时变量,用于恢复
        let tempDrop1 = drop1
        let tempDrop2 = drop2
 
 
        if drop1 == nil{
            drop1 = item
        }else if drop1 == item && drop2 == nil{
            drop1 = nil
        }else if drop1 == item && drop2 != nil {
            drop1 = drop2;drop2 = nil
        }else{
            drop2 = item
        }
 
        if let f = drop1{
            tempSelects.append(items[f])
        }
 
        if let f = drop2{
            tempSelects.append(items[f])
        }
 
 
        if let f = drop1,let l = drop2{
            let first = min(f, l)
            let last = max(f, l)
            tempSelects = Array(items[first...last])
        }
 
        if tempSelects.filter({$0.selectable == 0}).count > 0{
            alertError(msg: "不能跨时间段选择")
            drop1 = tempDrop1
            drop2 = tempDrop2
            return
        }
 
        selects = tempSelects
        YardDetailDateManager.shared().dates.accept(["\(self.date.jq_format("yyyy-MM-dd"))":selects])
        collectionView.reloadData()
 
    }
}
 
extension YardDetailDateTimeVC:UICollectionViewDelegate{
    func collectionView(_ collectionView: UICollectionView, didSelectItemAt indexPath: IndexPath) {
 
        let keys = YardDetailDateManager.shared().dates.value.jq_sortKey().filter({$0 != date.jq_format("yyyy-MM-dd")})
        var hasOther:Bool = false
        for key in keys{
            if (YardDetailDateManager.shared().dates.value[key]?.count ?? 0) > 0{
                hasOther = true;break
            }
        }
 
        guard !hasOther else {
            alertError(msg: "不能跨天选择");return
        }
 
        let m = items[indexPath.row]
        guard m.selectable == 1 else {return}
        dropDate(indexPath.row)
    }
}
 
extension YardDetailDateTimeVC:UICollectionViewDataSource{
    func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {
        let cell = collectionView.dequeueReusableCell(withReuseIdentifier: "_CalendarDateTimeTCell", for: indexPath) as! CalendarDateTimeTCell
        let m = items[indexPath.row]
        cell.label_content.text = m.time
//        let d = date.jq_format("yyyy-MM-dd ") + m.time.components(separatedBy: "-").last!
//        let interval = Date.jq_StringToTimeInterval(d, "yyyy-MM-dd HH:mm")
//
//        let canSelect = Date().timeIntervalSince1970 < interval
 
        if m.selectable == 1{
            cell.contentView.backgroundColor = UIColor.white
            cell.label_content.textColor = UIColor(hexStr: "#323232")
        }else{
            cell.contentView.backgroundColor = UIColor(hexStr: "#EC0808").withAlphaComponent(0.16)
            cell.label_content.textColor = UIColor(hexStr: "#EC0808")
        }
 
//        if m.selectable == 0{
//            cell.contentView.backgroundColor = UIColor(hexStr: "#EC0808").withAlphaComponent(0.16)
//            cell.label_content.textColor = UIColor(hexStr: "#EC0808")
//        }else if m.selectable == 1{
//            cell.contentView.backgroundColor = UIColor.white
//            cell.label_content.textColor = UIColor(hexStr: "#323232")
//        }
 
        if indexPath.row == drop2 || indexPath.row == drop1{
            cell.contentView.backgroundColor = UIColor(hexStr: "#256D0F").withAlphaComponent(0.24)
            cell.label_content.textColor = UIColor(hexStr: "#256D0F")
        }
 
        if selects.contains(where: {$0 == m}){
            cell.contentView.backgroundColor = UIColor(hexStr: "#256D0F").withAlphaComponent(0.24)
            cell.label_content.textColor = UIColor(hexStr: "#256D0F")
        }
 
 
 
 
 
        return cell
    }
 
    func collectionView(_ collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int {
        return items.count
    }
}