1use rbatis::crud::{Skip, CRUD};
2use rbatis::crud_table;
3use rbatis::error::Error;
4use rbatis::rbatis::Rbatis;
5use rbatis::Page;
6use rbatis::PageRequest;
7use rbson::Bson;
8use serde_derive::{Deserialize, Serialize};
9use std::fmt::Debug;
13
14#[crud_table(table_name:"chimes_permission"|table_columns:"id,alias,create_time,name,pid,api_pattern,service_id,api_method,api_bypass")]
15#[derive(Debug, Clone, Default, Deserialize, Serialize)]
16pub struct ChimesPermissionInfo {
17 pub id: Option<i64>,
18 pub alias: Option<String>,
19 pub create_time: Option<rbatis::DateTimeNative>,
20 pub name: Option<String>,
21 pub pid: Option<i64>,
22 pub api_pattern: Option<String>,
23 pub service_id: Option<String>,
24 pub api_method: Option<String>,
25 pub api_bypass: Option<String>,
26}
27
28impl ChimesPermissionInfo {
29 #[allow(dead_code)]
30 pub async fn from_id(rb: &Rbatis, id: &i64) -> Result<Option<Self>, Error> {
31 let wp = rb.new_wrapper().eq("id", id);
32 rb.fetch_by_wrapper::<Option<Self>>(wp).await
33 }
34
35 #[allow(dead_code)]
36 pub async fn save(&mut self, rb: &Rbatis) -> Result<u64, Error> {
37 match rb.save(self, &[Skip::Column("id")]).await {
38 Ok(ds) => {
39 self.id = ds.last_insert_id;
40 Ok(ds.rows_affected)
41 }
42 Err(err) => Err(err),
43 }
44 }
45
46 #[allow(dead_code)]
47 pub async fn update(&self, rb: &Rbatis) -> Result<u64, Error> {
48 let wp = rb.new_wrapper().eq("id", self.id);
49 rb.update_by_wrapper(self, wp, &[Skip::Column("id")]).await
50 }
51
52 #[allow(dead_code)]
53 pub async fn update_selective(&self, rb: &Rbatis) -> Result<u64, Error> {
54 let wp = rb.new_wrapper().eq("id", self.id);
55 rb.update_by_wrapper(self, wp, &[Skip::Value(Bson::Null)])
56 .await
57 }
58
59 #[allow(dead_code)]
60 pub async fn remove_batch(&self, rb: &Rbatis) -> Result<u64, Error> {
61 let wp = rb
62 .new_wrapper()
63 .r#if(self.id.clone().is_some(), |w| {
64 w.and().eq("id", self.id.unwrap())
65 })
66 .r#if(self.alias.clone().is_some(), |w| {
67 w.and().eq("alias", self.alias.clone().unwrap())
68 })
69 .r#if(self.create_time.clone().is_some(), |w| {
70 w.and().eq("create_time", self.create_time.unwrap())
71 })
72 .r#if(self.name.clone().is_some(), |w| {
73 w.and().eq("name", self.name.clone().unwrap())
74 })
75 .r#if(self.pid.clone().is_some(), |w| {
76 w.and().eq("pid", self.pid.unwrap())
77 })
78 .r#if(self.api_pattern.clone().is_some(), |w| {
79 w.and().eq("api_pattern", self.api_pattern.clone().unwrap())
80 })
81 .r#if(self.service_id.clone().is_some(), |w| {
82 w.and().eq("service_id", self.service_id.clone().unwrap())
83 })
84 .r#if(self.api_method.clone().is_some(), |w| {
85 w.and().eq("api_method", self.api_method.clone().unwrap())
86 })
87 .r#if(self.api_bypass.clone().is_some(), |w| {
88 w.and().eq("api_bypass", self.api_bypass.clone().unwrap())
89 });
90 rb.remove_by_wrapper::<Self>(wp).await
91 }
92
93 #[allow(dead_code)]
94 pub async fn remove(&mut self, rb: &Rbatis) -> Result<u64, Error> {
95 let wp = rb.new_wrapper().eq("id", self.id);
96 rb.remove_by_wrapper::<Self>(wp).await
97 }
98
99 #[allow(dead_code)]
100 pub async fn remove_ids(rb: &Rbatis, ids: &[i64]) -> Result<u64, Error> {
101 let wp = rb.new_wrapper().r#in("id", ids);
102 rb.remove_by_wrapper::<Self>(wp).await
103 }
104
105 #[allow(dead_code)]
106 pub async fn query_paged(&self, rb: &Rbatis, curr: u64, ps: u64) -> Result<Page<Self>, Error> {
107 let wp = rb
108 .new_wrapper()
109 .r#if(self.id.clone().is_some(), |w| {
110 w.and().eq("id", self.id.unwrap())
111 })
112 .r#if(self.alias.clone().is_some(), |w| {
113 w.and().eq("alias", self.alias.clone().unwrap())
114 })
115 .r#if(self.create_time.clone().is_some(), |w| {
116 w.and().eq("create_time", self.create_time.unwrap())
117 })
118 .r#if(self.name.clone().is_some(), |w| {
119 w.and().eq("name", self.name.clone().unwrap())
120 })
121 .r#if(self.pid.clone().is_some(), |w| {
122 w.and().eq("pid", self.pid.unwrap())
123 })
124 .r#if(self.pid.clone().is_none(), |w| w.and().eq("pid", Some(0)))
125 .r#if(self.api_pattern.clone().is_some(), |w| {
126 w.and().eq("api_pattern", self.api_pattern.clone().unwrap())
127 })
128 .r#if(self.service_id.clone().is_some(), |w| {
129 w.and().eq("service_id", self.service_id.clone().unwrap())
130 })
131 .r#if(self.api_method.clone().is_some(), |w| {
132 w.and().eq("api_method", self.api_method.clone().unwrap())
133 })
134 .r#if(self.api_bypass.clone().is_some(), |w| {
135 w.and().eq("api_bypass", self.api_bypass.clone().unwrap())
136 });
137 rb.fetch_page_by_wrapper::<Self>(wp, &PageRequest::new(curr, ps))
138 .await
139 }
140
141 #[allow(dead_code)]
142 pub async fn query_list(&self, rb: &Rbatis) -> Result<Vec<Self>, Error> {
143 let wp = rb
144 .new_wrapper()
145 .r#if(self.id.clone().is_some(), |w| {
146 w.and().eq("id", self.id.unwrap())
147 })
148 .r#if(self.alias.clone().is_some(), |w| {
149 w.and().eq("alias", self.alias.clone().unwrap())
150 })
151 .r#if(self.create_time.clone().is_some(), |w| {
152 w.and().eq("create_time", self.create_time.unwrap())
153 })
154 .r#if(self.name.clone().is_some(), |w| {
155 w.and().eq("name", self.name.clone().unwrap())
156 })
157 .r#if(self.pid.clone().is_some(), |w| {
158 w.and().eq("pid", self.pid.unwrap())
159 })
160 .r#if(self.pid.clone().is_none(), |w| w.and().eq("pid", Some(0)))
161 .r#if(self.api_pattern.clone().is_some(), |w| {
162 w.and().eq("api_pattern", self.api_pattern.clone().unwrap())
163 })
164 .r#if(self.service_id.clone().is_some(), |w| {
165 w.and().eq("service_id", self.service_id.clone().unwrap())
166 })
167 .r#if(self.api_method.clone().is_some(), |w| {
168 w.and().eq("api_method", self.api_method.clone().unwrap())
169 })
170 .r#if(self.api_bypass.clone().is_some(), |w| {
171 w.and().eq("api_bypass", self.api_bypass.clone().unwrap())
172 });
173 rb.fetch_list_by_wrapper::<Self>(wp).await
174 }
175
176 #[allow(dead_code)]
177 pub async fn query_all(rb: &Rbatis) -> Result<Vec<Self>, Error> {
178 let wp = rb.new_wrapper();
179 rb.fetch_list_by_wrapper::<Self>(wp).await
180 }
181
182 #[allow(dead_code)]
183 pub async fn query_tree(rb: &Rbatis, pid: &Option<i64>) -> Result<Vec<Self>, Error> {
184 let wp = rb
185 .new_wrapper()
186 .r#if(pid.clone().is_some(), |w| w.and().eq("pid", pid.unwrap()))
187 .r#if(pid.clone().is_none(), |w| w.and().eq("pid", 0));
188 rb.fetch_list_by_wrapper::<Self>(wp).await
189 }
190
191 fn find_children(list: &Vec<Self>, pid: &i64) -> Vec<Self> {
192 let mut found = vec![];
193 for it in list.clone() {
194 if it.pid == Some(*pid) {
195 found.push(it.clone());
196 let nid = it.id.unwrap_or_default();
197 let mut mt = Self::find_children(list, &nid);
198 if !mt.is_empty() {
199 found.append(&mut mt);
200 }
201 }
202 }
203 found
204 }
205
206 pub async fn query_children(rb: &Rbatis, pid: &i64) -> Result<Vec<Self>, Error> {
207 match Self::query_all(rb).await {
208 Ok(rs) => {
209 let mut found = Self::find_children(&rs, pid);
210 for it in rs.clone() {
211 if it.id == Some(*pid) {
212 found.insert(0, it);
213 }
214 }
215 Ok(found)
216 }
217 Err(err) => Err(err),
218 }
219 }
220}
221
222#[derive(Debug, Clone, Default, Deserialize, Serialize)]
223pub struct ChimesPermissionInfoValue {
224 pub id: Option<i64>,
225 pub alias: Option<String>,
226 pub create_time: Option<rbatis::DateTimeNative>,
227 pub name: Option<String>,
228 pub pid: Option<i64>,
229 pub api_pattern: Option<String>,
230 pub service_id: Option<String>,
231 pub api_method: Option<String>,
232 pub api_bypass: Option<String>,
233 pub leaf: bool,
234 pub label: Option<String>,
235 pub has_children: bool,
236 #[serde(default)]
237 pub children: Vec<ChimesPermissionInfoValue>,
238}
239
240impl ChimesPermissionInfoValue {
241 #[allow(dead_code)]
242 pub fn from_entity(param: &ChimesPermissionInfo) -> Self {
243 Self {
244 id: param.id,
245 alias: param.alias.clone(),
246 create_time: param.create_time,
247 name: param.name.clone(),
248 pid: param.pid,
249 api_pattern: param.api_pattern.clone(),
250 service_id: param.service_id.clone(),
251 api_method: param.api_method.clone(),
252 api_bypass: param.api_bypass.clone(),
253 has_children: false,
254 leaf: false,
255 children: vec![],
256 label: param.name.clone(),
257 }
258 }
259
260 #[allow(dead_code)]
261 pub fn from_entity_with(
262 param: &ChimesPermissionInfo,
263 haschild: bool,
264 children: &[Self],
265 ) -> Self {
266 Self {
267 id: param.id,
268 alias: param.alias.clone(),
269 create_time: param.create_time,
270 name: param.name.clone(),
271 pid: param.pid,
272 api_pattern: param.api_pattern.clone(),
273 service_id: param.service_id.clone(),
274 api_method: param.api_method.clone(),
275 api_bypass: param.api_bypass.clone(),
276 has_children: haschild,
277 leaf: !haschild,
278 children: children.to_vec(),
279 label: param.name.clone(),
280 }
281 }
282
283 #[allow(dead_code)]
284 pub fn to_entity(&self) -> ChimesPermissionInfo {
285 ChimesPermissionInfo {
286 id: self.id,
287 alias: self.alias.clone(),
288 create_time: self.create_time,
289 name: self.name.clone(),
290 pid: self.pid,
291 api_pattern: self.api_pattern.clone(),
292 service_id: self.service_id.clone(),
293 api_method: self.api_method.clone(),
294 api_bypass: self.api_bypass.clone(),
295 }
296 }
297
298 fn recurse_build_tree(items: &Vec<Self>, parent_item: &mut Self) {
299 for xip in items.clone() {
300 if xip.pid == parent_item.id {
301 let mut mip = xip;
302 Self::recurse_build_tree(items, &mut mip);
303 if mip.children.is_empty() {
304 mip.leaf = true;
305 mip.has_children = false;
306 }
307 parent_item.children.push(mip);
308 }
309 }
310 }
311
312 #[allow(dead_code)]
313 pub fn build_tree(items: &Vec<Self>) -> Vec<Self> {
314 let mut tmptree = vec![];
315 for xip in items.clone() {
316 if xip.pid.is_none() || xip.pid == Some(0) {
317 tmptree.push(xip.clone());
318 }
319 }
320 let mut tree = vec![];
321 for mut it in tmptree {
322 Self::recurse_build_tree(items, &mut it);
323 tree.push(it);
324 }
325 tree
326 }
327}