1use parking_lot::RwLock;
6
7fn hk_clt_to_api(clt: u32) -> i32 {
19 match clt {
20 1 => 3, 2 => 1, 3 => 2, 4 => 4, _ => 0, }
26}
27
28fn us_clt_to_api(clt: u32) -> i32 {
29 match clt {
30 1 => 2, 2 => 5, _ => 5, }
34}
35
36fn cn_clt_to_api(clt: u32) -> i32 {
37 match clt {
38 1 => 2, 2 => 3, 3 => 5, _ => 5, }
43}
44
45fn other_clt_to_api(clt: u32) -> i32 {
46 match clt {
49 1 => 3, 2 => 2, _ => 5, }
53}
54
55fn us_future_clt_to_api(clt: u32) -> i32 {
59 match clt {
60 0 => 5, 1 => 3, 2 => 1, 3 => 3, 4 => 2, _ => 5, }
67}
68
69fn us_option_clt_to_api(clt: u32) -> i32 {
71 match clt {
72 0 => 1, 1 => 2, _ => 1,
75 }
76}
77
78#[derive(Debug, Clone)]
80pub struct QotRightData {
81 pub hk_qot_right: i32,
83 pub us_qot_right: i32,
85 pub sh_qot_right: i32,
87 pub sz_qot_right: i32,
88 pub hk_option_qot_right: i32,
90 pub hk_future_qot_right: i32,
91 pub has_us_option_qot_right: bool,
93 pub us_option_qot_right: i32,
94 pub us_index_qot_right: i32,
95 pub us_otc_qot_right: i32,
96 pub us_cme_future_qot_right: i32,
98 pub us_cbot_future_qot_right: i32,
99 pub us_nymex_future_qot_right: i32,
100 pub us_comex_future_qot_right: i32,
101 pub us_cboe_future_qot_right: i32,
102 pub sg_future_qot_right: i32,
104 pub jp_future_qot_right: i32,
105 pub sub_quota: i32,
107 pub history_kl_quota: i32,
108}
109
110impl Default for QotRightData {
111 fn default() -> Self {
112 let bmp = 1; Self {
114 hk_qot_right: bmp,
115 us_qot_right: bmp,
116 sh_qot_right: bmp,
117 sz_qot_right: bmp,
118 hk_option_qot_right: bmp,
119 hk_future_qot_right: bmp,
120 has_us_option_qot_right: true,
121 us_option_qot_right: bmp,
122 us_index_qot_right: bmp,
123 us_otc_qot_right: bmp,
124 us_cme_future_qot_right: bmp,
125 us_cbot_future_qot_right: bmp,
126 us_nymex_future_qot_right: bmp,
127 us_comex_future_qot_right: bmp,
128 us_cboe_future_qot_right: bmp,
129 sg_future_qot_right: bmp,
130 jp_future_qot_right: bmp,
131 sub_quota: 4000,
132 history_kl_quota: 100,
133 }
134 }
135}
136
137pub struct QotRightCache {
139 data: RwLock<QotRightData>,
140}
141
142impl Default for QotRightCache {
143 fn default() -> Self {
144 Self::new()
145 }
146}
147
148impl QotRightCache {
149 pub fn new() -> Self {
150 Self {
151 data: RwLock::new(QotRightData::default()),
152 }
153 }
154
155 pub fn get(&self) -> QotRightData {
157 self.data.read().clone()
158 }
159
160 #[allow(clippy::too_many_arguments)]
166 pub fn update_from_backend(
167 &self,
168 hk_got: Option<u32>,
169 us_got: Option<u32>,
170 cn_got: Option<u32>,
171 sh_auth: Option<u32>,
172 sz_auth: Option<u32>,
173 hk_option: Option<u32>,
174 hk_future: Option<u32>,
175 us_option: Option<u32>,
176 us_future_cme_cboe: Option<u32>,
177 us_future_detail: Option<(u32, u32, u32, u32)>, sg_future: Option<u32>,
179 jp_future: Option<u32>,
180 sub_limit: Option<u32>,
181 kl_limit: Option<u32>,
182 ) {
183 let mut d = self.data.write();
184
185 if let Some(v) = hk_got {
187 d.hk_qot_right = hk_clt_to_api(v);
188 }
189
190 if let Some(v) = us_got {
192 d.us_qot_right = us_clt_to_api(v);
193 d.us_index_qot_right = us_clt_to_api(v);
195 d.us_otc_qot_right = us_clt_to_api(v);
196 }
197
198 if let Some(v) = sh_auth {
200 d.sh_qot_right = cn_clt_to_api(v);
201 } else if let Some(v) = cn_got {
202 d.sh_qot_right = cn_clt_to_api(v);
203 }
204 if let Some(v) = sz_auth {
205 d.sz_qot_right = cn_clt_to_api(v);
206 } else if let Some(v) = cn_got {
207 d.sz_qot_right = cn_clt_to_api(v);
208 }
209
210 if let Some(v) = hk_option {
212 d.hk_option_qot_right = hk_clt_to_api(v);
213 }
214 if let Some(v) = hk_future {
215 d.hk_future_qot_right = hk_clt_to_api(v);
216 }
217
218 if let Some(v) = us_option {
220 d.us_option_qot_right = us_option_clt_to_api(v);
221 d.has_us_option_qot_right = v != 0; }
223
224 if let Some(v) = us_future_cme_cboe {
226 let api_right = us_future_clt_to_api(v);
227 d.us_cme_future_qot_right = api_right;
228 d.us_cboe_future_qot_right = api_right;
229 }
230
231 if let Some((cme, cbot, nymex, comex)) = us_future_detail {
233 if cme > 0 {
234 d.us_cme_future_qot_right = us_future_clt_to_api(cme);
235 }
236 if cbot > 0 {
237 d.us_cbot_future_qot_right = us_future_clt_to_api(cbot);
238 }
239 if nymex > 0 {
240 d.us_nymex_future_qot_right = us_future_clt_to_api(nymex);
241 }
242 if comex > 0 {
243 d.us_comex_future_qot_right = us_future_clt_to_api(comex);
244 }
245 }
246
247 if let Some(v) = sg_future {
249 d.sg_future_qot_right = other_clt_to_api(v);
250 }
251 if let Some(v) = jp_future {
252 d.jp_future_qot_right = other_clt_to_api(v);
253 }
254
255 if let Some(v) = sub_limit {
257 if v > 0 {
258 d.sub_quota = v as i32;
259 }
260 }
261 if let Some(v) = kl_limit {
262 if v > 0 {
263 d.history_kl_quota = v as i32;
264 }
265 }
266 }
267}