chimes_rust/entity/
chimes_account_role_info.rs1use 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_account_roles"|table_columns:"account_id,role_id")]
15#[derive(Debug, Clone, Default, Deserialize, Serialize)]
16pub struct ChimesAccountRoleInfo {
17 pub account_id: Option<i64>,
18 pub role_id: Option<i64>,
19}
20
21impl ChimesAccountRoleInfo {
22 #[allow(dead_code)]
23 pub async fn from_id(
24 rb: &Rbatis,
25 account_id: &i64,
26 role_id: &i64,
27 ) -> Result<Option<Self>, Error> {
28 let wp = rb
29 .new_wrapper()
30 .eq("account_id", account_id)
31 .and()
32 .eq("role_id", role_id);
33 rb.fetch_by_wrapper::<Option<Self>>(wp).await
34 }
35
36 #[allow(dead_code)]
37 pub async fn save(&mut self, rb: &Rbatis) -> Result<u64, Error> {
38 match rb.save(self, &[]).await {
39 Ok(ds) => Ok(ds.rows_affected),
40 Err(err) => Err(err),
41 }
42 }
43
44 #[allow(dead_code)]
45 pub async fn update(&self, rb: &Rbatis) -> Result<u64, Error> {
46 let wp = rb
47 .new_wrapper()
48 .eq("account_id", self.account_id)
49 .and()
50 .eq("role_id", self.role_id);
51 rb.update_by_wrapper(
52 self,
53 wp,
54 &[Skip::Column("account_id"), Skip::Column("role_id")],
55 )
56 .await
57 }
58
59 #[allow(dead_code)]
60 pub async fn update_selective(&self, rb: &Rbatis) -> Result<u64, Error> {
61 let wp = rb
62 .new_wrapper()
63 .eq("account_id", self.account_id)
64 .and()
65 .eq("role_id", self.role_id);
66 rb.update_by_wrapper(self, wp, &[Skip::Value(Bson::Null)])
67 .await
68 }
69
70 #[allow(dead_code)]
71 pub async fn remove_batch(&self, rb: &Rbatis) -> Result<u64, Error> {
72 let wp = rb
73 .new_wrapper()
74 .r#if(self.account_id.clone().is_some(), |w| {
75 w.and().eq("account_id", self.account_id.unwrap())
76 })
77 .r#if(self.role_id.clone().is_some(), |w| {
78 w.and().eq("role_id", self.role_id.unwrap())
79 });
80 rb.remove_by_wrapper::<Self>(wp).await
81 }
82
83 #[allow(dead_code)]
84 pub async fn remove(&mut self, rb: &Rbatis) -> Result<u64, Error> {
85 let wp = rb
86 .new_wrapper()
87 .eq("account_id", self.account_id)
88 .and()
89 .eq("role_id", self.role_id);
90 rb.remove_by_wrapper::<Self>(wp).await
91 }
92
93 #[allow(dead_code)]
94 pub async fn query_paged(&self, rb: &Rbatis, curr: u64, ps: u64) -> Result<Page<Self>, Error> {
95 let wp = rb
96 .new_wrapper()
97 .r#if(self.account_id.clone().is_some(), |w| {
98 w.and().eq("account_id", self.account_id.unwrap())
99 })
100 .r#if(self.role_id.clone().is_some(), |w| {
101 w.and().eq("role_id", self.role_id.unwrap())
102 });
103 rb.fetch_page_by_wrapper::<Self>(wp, &PageRequest::new(curr, ps))
104 .await
105 }
106
107 #[allow(dead_code)]
108 pub async fn query_list(&self, rb: &Rbatis) -> Result<Vec<Self>, Error> {
109 let wp = rb
110 .new_wrapper()
111 .r#if(self.account_id.clone().is_some(), |w| {
112 w.and().eq("account_id", self.account_id.unwrap())
113 })
114 .r#if(self.role_id.clone().is_some(), |w| {
115 w.and().eq("role_id", self.role_id.unwrap())
116 });
117 rb.fetch_list_by_wrapper::<Self>(wp).await
118 }
119
120 #[allow(dead_code)]
121 pub async fn query_all(rb: &Rbatis) -> Result<Vec<Self>, Error> {
122 let wp = rb.new_wrapper();
123 rb.fetch_list_by_wrapper::<Self>(wp).await
124 }
125}