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