chimes_rust/entity/
chimes_role_menus.rs

1use crate::entity::ChimesMenuInfo;
2use crate::entity::ChimesPermissionInfo;
3use crate::entity::ChimesRoleInfo;
4use crate::entity::ChimesRoleMenuInfo;
5use crate::entity::ChimesRolePermissionInfo;
6use rbatis::error::Error;
7use rbatis::rbatis::Rbatis;
8use serde_derive::{Deserialize, Serialize};
9/**
10 * Generate the file for chimes_role_menus.rs,
11 */
12use std::fmt::Debug;
13
14#[derive(Debug, Clone, Default, Deserialize, Serialize)]
15pub struct ChimesRoleMenus {
16    pub role_id: Option<i64>,
17    pub name: Option<String>,
18    pub role_code: Option<String>,
19    pub level: Option<i32>,
20    pub description: Option<String>,
21    pub data_scope: Option<String>,
22    pub create_by: Option<String>,
23    pub update_by: Option<String>,
24    pub create_time: Option<rbatis::DateTimeNative>,
25    pub update_time: Option<rbatis::DateTimeNative>,
26    #[serde(default)]
27    pub menus: Vec<ChimesMenuInfo>,
28    #[serde(default)]
29    pub permissions: Vec<ChimesPermissionInfo>,
30}
31
32impl ChimesRoleMenus {
33    #[allow(dead_code)]
34    pub fn from_role(param: &ChimesRoleInfo) -> Self {
35        ChimesRoleMenus {
36            role_id: param.role_id,
37            name: param.name.clone(),
38            role_code: param.role_code.clone(),
39            level: param.level,
40            description: param.description.clone(),
41            data_scope: param.data_scope.clone(),
42            create_by: param.create_by.clone(),
43            update_by: param.update_by.clone(),
44            create_time: param.create_time,
45            update_time: param.update_time,
46            menus: vec![],
47            permissions: vec![],
48        }
49    }
50
51    #[allow(dead_code)]
52    pub fn to_role(&self) -> ChimesRoleInfo {
53        ChimesRoleInfo {
54            role_id: self.role_id,
55            name: self.name.clone(),
56            role_code: self.role_code.clone(),
57            level: self.level,
58            description: self.description.clone(),
59            data_scope: self.data_scope.clone(),
60            create_by: self.create_by.clone(),
61            update_by: self.update_by.clone(),
62            create_time: self.create_time,
63            update_time: self.update_time,
64        }
65    }
66
67    #[allow(dead_code)]
68    pub async fn load(rb: &Rbatis, role_id: &i64) -> Result<Option<Self>, Error> {
69        match ChimesRoleInfo::from_id(rb, role_id).await {
70            Ok(ts) => match ts {
71                Some(mp) => {
72                    let mut selfmp = Self::from_role(&mp);
73                    let mut rb_args = vec![];
74                    let sql_menu = "SELECT tp.* FROM chimes_menu tp INNER JOIN chimes_roles_menus mt ON tp.menu_id = mt.menu_id WHERE mt.role_id = ?";
75                    rb_args.push(
76                        rbson::to_bson(selfmp.role_id.unwrap_or_default()).unwrap_or_default(),
77                    );
78                    selfmp.menus = match rb.fetch(sql_menu, rb_args).await {
79                        Ok(lst) => lst,
80                        Err(_) => {
81                            vec![]
82                        }
83                    };
84                    let mut rb_args = vec![];
85                    let sql_permission = "SELECT tp.* FROM chimes_permission tp INNER JOIN chimes_roles_permissions mt ON tp.id = mt.id WHERE mt.role_id = ?";
86                    rb_args.push(
87                        rbson::to_bson(selfmp.role_id.unwrap_or_default()).unwrap_or_default(),
88                    );
89                    selfmp.permissions = match rb.fetch(sql_permission, rb_args).await {
90                        Ok(lst) => lst,
91                        Err(_) => {
92                            vec![]
93                        }
94                    };
95                    Ok(Some(selfmp))
96                }
97                None => Ok(None),
98            },
99            Err(err) => Err(err),
100        }
101    }
102
103    #[allow(dead_code)]
104    pub async fn save(&self, rb: &Rbatis) -> Result<bool, Error> {
105        let mut ret: Option<Error>;
106        let mut self_role = self.to_role();
107        if self_role.role_id.is_none() {
108            ret = match self_role.save(rb).await {
109                Ok(_rs) => None,
110                Err(err) => {
111                    log::warn!("Save role occurred an error {}", err);
112                    Some(err)
113                }
114            }
115        } else {
116            ret = match self_role.update_selective(rb).await {
117                Ok(_rs) => None,
118                Err(err) => {
119                    log::warn!("Update role occurred an error {}", err);
120                    Some(err)
121                }
122            }
123        }
124        // remove batch for ChimesRoleMenuInfo.
125        if ret.is_none() {
126            let rm_role_menu_info = ChimesRoleMenuInfo {
127                role_id: self.role_id,
128                ..Default::default()
129            };
130            ret = match rm_role_menu_info.remove_batch(rb).await {
131                Ok(_) => None,
132                Err(err) => {
133                    log::warn!("Remove role_menu_info occurred an error {}", err);
134                    Some(err)
135                }
136            };
137        }
138        for row in self.menus.clone() {
139            let mut svrow_role_menu_info = ChimesRoleMenuInfo {
140                role_id: self.role_id,
141                menu_id: row.menu_id,
142            };
143
144            ret = match svrow_role_menu_info.save(rb).await {
145                Ok(_) => None,
146                Err(err) => {
147                    log::warn!("Save role_menu_info occurred an error {}", err);
148                    Some(err)
149                }
150            };
151        }
152        // remove batch for ChimesRolePermissionInfo.
153        if ret.is_none() {
154            let rm_role_permission_info = ChimesRolePermissionInfo {
155                role_id: self.role_id,
156                ..Default::default()
157            };
158            ret = match rm_role_permission_info.remove_batch(rb).await {
159                Ok(_) => None,
160                Err(err) => {
161                    log::warn!("Remove role_permission_info occurred an error {}", err);
162                    Some(err)
163                }
164            };
165        }
166        for row in self.permissions.clone() {
167            let mut svrow_role_permission_info = ChimesRolePermissionInfo {
168                role_id: self.role_id,
169                id: row.id,
170            };
171            ret = match svrow_role_permission_info.save(rb).await {
172                Ok(_) => None,
173                Err(err) => {
174                    log::warn!("Save role_permission_info occurred an error {}", err);
175                    Some(err)
176                }
177            };
178        }
179        match ret {
180            Some(err) => Err(err),
181            None => Ok(true),
182        }
183    }
184
185    #[allow(dead_code)]
186    pub async fn remove(&self, rb: &Rbatis) -> Result<bool, Error> {
187        let mut ret: Option<Error> = None;
188        // remove batch for ChimesRoleMenuInfo.
189        if ret.is_none() {
190            let rm_role_menu_info = ChimesRoleMenuInfo {
191                role_id: self.role_id,
192                ..Default::default()
193            };
194            ret = match rm_role_menu_info.remove_batch(rb).await {
195                Ok(_rtremove) => None,
196                Err(err) => {
197                    log::warn!("Remove role_menu_info occurred an error {}", err);
198                    Some(err)
199                }
200            };
201        }
202        // remove batch for ChimesRolePermissionInfo.
203        if ret.is_none() {
204            let rm_role_permission_info = ChimesRolePermissionInfo {
205                role_id: self.role_id,
206                ..Default::default()
207            };
208            ret = match rm_role_permission_info.remove_batch(rb).await {
209                Ok(_rtremove) => None,
210                Err(err) => {
211                    log::warn!("Remove role_permission_info occurred an error {}", err);
212                    Some(err)
213                }
214            };
215        }
216        if let Some(ret) = ret {
217            Err(ret)
218        } else {
219            match self.to_role().remove(rb).await {
220                Ok(_rs) => Ok(true),
221                Err(err) => {
222                    log::warn!("Remove role occurred an error {}", err);
223                    Err(err)
224                }
225            }
226        }
227    }
228}