宽窄优行-由【嘉易行】项目成品而来
无故事王国
2023-05-25 dc1998fc1ac124f6b9a0e434ccf91103dd936409
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
//
//  YYSpecialCarViewModel.swift
//  OKProject
//
//  Created by alvin_y on 2020/9/17.
//  Copyright © 2020 yangwang. All rights reserved.
//
 
import UIKit
import RxSwift
import RxCocoa
import HandyJSON
class YYSpecialCarViewModel: YYViewModel {
 
    /// 订单Id
    let orderId = BehaviorRelay<Int>(value: 0)
    
    /// 司机信息
    let driverInfo = BehaviorRelay<SocketInfoModel?>(value: nil)
    
    /// 订单状态
    let state = BehaviorRelay<SpecialCarState>(value: .witingForResponse)
    
    /// 订单类型
    let orderType = BehaviorRelay<OrderType>(value: .special)
    
    /// 数据源
    let dataSource = BehaviorRelay<YYSpecialCarModel?>(value: nil)
    
    /// 下单时间
    let orderTime = BehaviorRelay<String?>(value: nil)
    
    /// 出行时间
    let travelTime = BehaviorRelay<String?>(value: nil)
    
    /// 取消订单Id
    let cancelId = BehaviorRelay<Int>(value: 0)
    
    /// 支付方式(0=其他方式,1=微信,2=支付宝)
    let payType = BehaviorRelay<Int?>(value: nil)
    
    /// 取消订单需要支付金额
    let amount = BehaviorRelay<Double>(value: 0)
    
    /// 取消原因
    let reason = BehaviorRelay<String?>(value: nil)
    
    /// 取消备注
    let remark = BehaviorRelay<String?>(value: nil)
    
    /// 评分
    let fraction = BehaviorRelay<Int>(value: 0)
    
    /// 评价内容
    let content = BehaviorRelay<String?>(value: nil)
 
    /// 评价数组
    var evaluationArray: [String] = []
    
    /// 报警电话
    let policeNumber = BehaviorRelay<String>(value: "")
    
    /// 投诉电话
    let complaintsNumber = BehaviorRelay<String>(value: "")
    
    /// 分享红包金额
    let shareRedEnvelopeAmount = BehaviorRelay<Double>(value: 0)
    
    /// 获取订单详情
    func getOrderDetails() -> Observable<Result<YYSpecialCarModel?, Error>> {
        
        guard orderId.value != 0 else {
            return Observable.just(.failure(YYError.errorDescription("订单ID获取失败")))
        }
        
        return
            APIManager.shared.provider.rx
            .request(.queryOrderInfo(orderId: orderId.value, orderType: orderType.value.rawValue))
            .mapThenValidate(YYSpecialCarModel.self)
            .catchError { Single.just(.failure($0)) }
            .asObservable()
    }
    
    
    /// 取消订单获取金额
    func queryCancleAmount()  -> Observable<Result<CancleAmountModel?, Error>> {
        return APIManager.shared.provider.rx
            .request(.queryCancleAmount(id: orderId.value,orderType: orderType.value.rawValue))
            .mapThenValidate(CancleAmountModel.self)
            .catchError { Single.just(.failure($0)) }
            .asObservable()
    }
    
    /// 添加取消记录
    func addCancledRecord(orderType:OrderType = .special)  -> Observable<Result<SystemMessageModel?, Error>> {
        return APIManager.shared.provider.rx
            .request(.addCancle(id: orderId.value, orderType: orderType.rawValue, reason: reason.value, remark: remark.value))
            .mapThenValidate(SystemMessageModel.self)
            .catchError { Single.just(.failure($0)) }
            .asObservable()
    }
    /// 取消订单
    func cancleOrder() -> Observable<Result<String?, Error>>  {
        return
            APIManager.shared.provider.rx
            .request(.cancleOrderTaxi(cancleId: cancelId.value, id: orderId.value, orderType: orderType.value.rawValue, payType: payType.value))
            .mapThenValidate(String.self)
            .catchError { Single.just(.failure($0)) }
            .asObservable()
    }
    
    /// 获取司机位置
    func queryOrderServer() -> Observable<Result<SocketInfoModel?, Error>> {
        return
            APIManager.shared.provider.rx
            .request(.queryOrderServer(orderId: self.orderId.value, orderType: orderType.value.rawValue))
            .mapThenValidate(SocketInfoModel.self)
            .catchError { Single.just(.failure($0)) }
            .asObservable()
    }
    
    /// 获取没有司机接单的提醒数据
    func queryEndPush() -> Observable<Result<SocketInfoModel?, Error>> {
        return
            APIManager.shared.provider.rx
            .request(.queryEndPush(orderId: orderId.value, orderType: orderType.value.rawValue))
            .mapThenValidate(SocketInfoModel.self)
            .catchError { Single.just(.failure($0)) }
            .asObservable()
    }
    
    
    /// 继续等待推单操作
    func pushOrder() -> Observable<Result<TravelServiceModel?, Error>> {
        return
            APIManager.shared.provider.rx
            .request(.pushOrderTaxi(id: orderId.value, orderType: orderType.value.rawValue))
            .mapThenValidate(TravelServiceModel.self)
            .catchError { Single.just(.failure($0)) }
            .asObservable()
    }
    
    /// 评价
    func orderEvaluate() -> Observable<Result<Nothing?, Error>>  {
        return
            APIManager.shared.provider.rx
            .request(.orderEvaluate(orderId: self.orderId.value, orderType: orderType.value.rawValue, fraction: fraction.value, content: content.value))
            .mapThenValidate(Nothing.self)
            .catchError { Single.just(.failure($0)) }
            .asObservable()
    }
    
    
    /// 查询报警电话
    /// - Returns: PhonesModel
    func queryPhones()  {
            APIManager.shared.provider.rx
            .request(.queryPhones(code: YYLocationManager.shared.currentCityCode))
            .map(YYModel<[PhonesModel]>.self)
            .validate()
            .subscribe(onSuccess: {[unowned self]  (model) in
                guard let data = model.data else{return}
                for item in data{
                    if item.type == 1{
                        self.policeNumber.accept(item.phone)
                    }else if item.type == 2{
                        self.complaintsNumber.accept(item.phone)
                    }
                }
            }) {  (error) in
        }
        .disposed(by: disposeBag)
    }
    
    /// 查询是否有红包
    func queryRedMoney()  -> Observable<Result<CancleAmountModel?, Error>> {
        return
            APIManager.shared.provider.rx
            .request(.queryRedMoney(orderId: orderId.value,orderType: orderType.value.rawValue))
            .mapThenValidate(CancleAmountModel.self)
            .catchError { Single.just(.failure($0)) }
            .asObservable()
    }
    
    /// 评论成功后获取红包金额
    func shareRedEnvelope()  -> Observable<Result<Nothing?, Error>> {
        APIManager.shared.provider.rx
            .request(.shareRedEnvelope(orderId: orderId.value, orderType: orderType.value.rawValue))
            .mapThenValidate(Nothing.self)
            .catchError { Single.just(.failure($0)) }
            .asObservable()
    }
    
    /// 获取底部显示文字
    func attributedString(state: Int,data: SocketInfoModel) -> NSMutableAttributedString? {
        if state == 2 || state == 3{
            let attributedString = AttributedStringbuilder.build()
            .add(string: "司机接单,距预约地点", withFont: UIFont.init(name: Medium, size: 14)!, withColor: ThemeColor)
                .add(string: "\(data.reservationMileage)", withFont: UIFont.init(name: Medium, size: 14)!, withColor: UIColor(hexString: "#FB692A")!)
            .add(string: "公里,预计", withFont: UIFont.init(name: Medium, size: 14)!, withColor: ThemeColor)
            .add(string: "\(data.reservationTime)", withFont: UIFont.init(name: Medium, size: 14)!, withColor: UIColor(hexString: "#FB692A")!)
            .add(string: "分钟到达", withFont: UIFont.init(name: Medium, size: 14)!, withColor: ThemeColor)
            .mutableAttributedString
            return attributedString
        }else if state == 5 || state == 6{
            let attributedString = AttributedStringbuilder.build()
            .add(string: "已为您服务", withFont: UIFont.init(name: Medium, size: 12)!, withColor: UIColor.color(hexString: "#00BF30"))
            .add(string: "\(data.servedTime)", withFont: UIFont.init(name: Medium, size: 12)!, withColor: UIColor(hexString: "#FB692A")!)
            .add(string: "分钟,累计行驶", withFont: UIFont.init(name: Medium, size: 12)!, withColor: UIColor.color(hexString: "#00BF30"))
            .add(string: "\(data.servedMileage)", withFont: UIFont.init(name: Medium, size: 12)!, withColor: UIColor(hexString: "#FB692A")!)
            .add(string: "公里\n距离目的地还有大约", withFont: UIFont.init(name: Medium, size: 12)!, withColor: UIColor.color(hexString: "#00BF30"))
            .add(string: "\(data.laveMileage)", withFont: UIFont.init(name: Medium, size: 12)!, withColor: UIColor(hexString: "#FB692A")!)
            .add(string: "公里,预计行驶大约", withFont: UIFont.init(name: Medium, size: 12)!, withColor: UIColor.color(hexString: "#00BF30"))
            .add(string: "\(data.laveTime)", withFont: UIFont.init(name: Medium, size: 12)!, withColor: UIColor(hexString: "#FB692A")!)
            .add(string: "分钟", withFont: UIFont.init(name: Medium, size: 12)!, withColor: UIColor.color(hexString: "#00BF30"))
            .mutableAttributedString
            return attributedString
        }
        return nil
    }
}