1use chimes_utils::i64_from_str;
2use rbatis::crud::{CRUDMut, Skip, CRUD};
3use rbatis::crud_table;
4use rbatis::error::Error;
5use rbatis::executor::{RBatisTxExecutor, RbatisRef};
6use rbatis::rbatis::Rbatis;
7use rbatis::Page;
8use rbatis::PageRequest;
9use rbson::Bson;
10use serde_derive::{Deserialize, Serialize};
11use std::fmt::Debug;
15
16#[crud_table(table_name:"chimes_attachments"|table_columns:"attachment_id,attach_hash,original_name,attachment_name,storage_path,access_url,file_ext,content_type,file_size,file_size_display,modify_by,create_time,update_time")]
17#[derive(Debug, Clone, Default, Deserialize, Serialize)]
18pub struct ChimesAttachmentInfo {
19 #[serde(default)]
20 #[serde(deserialize_with = "i64_from_str")]
21 pub attachment_id: Option<i64>,
22 pub attach_hash: Option<String>,
23 pub original_name: Option<String>,
24 pub attachment_name: Option<String>,
25 pub storage_path: Option<String>,
26 pub access_url: Option<String>,
27 pub file_ext: Option<String>,
28 pub content_type: Option<String>,
29 #[serde(default)]
30 #[serde(deserialize_with = "i64_from_str")]
31 pub file_size: Option<i64>,
32 pub file_size_display: Option<String>,
33 pub modify_by: Option<String>,
34 pub create_time: Option<rbatis::DateTimeNative>,
35 pub update_time: Option<rbatis::DateTimeNative>,
36}
37
38impl ChimesAttachmentInfo {
39 #[allow(dead_code)]
40 pub async fn from_id(rb: &Rbatis, attachment_id: &i64) -> Result<Option<Self>, Error> {
41 let wp = rb.new_wrapper().eq("attachment_id", attachment_id);
42 rb.fetch_by_wrapper::<Option<Self>>(wp).await
43 }
44
45 #[allow(dead_code)]
46 pub async fn from_attachment_name(
47 rb: &Rbatis,
48 attachment_name: &String,
49 ) -> Result<Option<Self>, Error> {
50 let wp = rb.new_wrapper().eq("attachment_name", attachment_name);
51 rb.fetch_by_wrapper::<Option<Self>>(wp).await
52 }
53
54 #[allow(dead_code)]
55 pub async fn save(&mut self, rb: &mut RBatisTxExecutor<'_>) -> Result<u64, Error> {
56 match rb.save(self, &[Skip::Column("attachment_id")]).await {
57 Ok(ds) => {
58 self.attachment_id = ds.last_insert_id;
59 Ok(ds.rows_affected)
60 }
61 Err(err) => Err(err),
62 }
63 }
64
65 #[allow(dead_code)]
66 pub async fn update(&self, rb: &mut RBatisTxExecutor<'_>) -> Result<u64, Error> {
67 let wp = rb
68 .get_rbatis()
69 .new_wrapper()
70 .eq("attachment_id", self.attachment_id);
71 rb.update_by_wrapper(self, wp, &[Skip::Column("attachment_id")])
72 .await
73 }
74
75 #[allow(dead_code)]
76 pub async fn update_selective(&self, rb: &mut RBatisTxExecutor<'_>) -> Result<u64, Error> {
77 let wp = rb
78 .get_rbatis()
79 .new_wrapper()
80 .eq("attachment_id", self.attachment_id);
81 rb.update_by_wrapper(self, wp, &[Skip::Value(Bson::Null)])
82 .await
83 }
84
85 #[allow(dead_code)]
86 pub async fn remove_batch(&self, rb: &mut RBatisTxExecutor<'_>) -> Result<u64, Error> {
87 let wp = rb
88 .get_rbatis()
89 .new_wrapper()
90 .r#if(self.attachment_id.clone().is_some(), |w| {
91 w.and().eq("attachment_id", self.attachment_id.unwrap())
92 })
93 .r#if(self.attach_hash.clone().is_some(), |w| {
94 w.and().eq("attach_hash", self.attach_hash.clone().unwrap())
95 })
96 .r#if(self.attachment_name.clone().is_some(), |w| {
97 w.and()
98 .eq("attachment_name", self.attachment_name.clone().unwrap())
99 })
100 .r#if(self.storage_path.clone().is_some(), |w| {
101 w.and()
102 .eq("storage_path", self.storage_path.clone().unwrap())
103 })
104 .r#if(self.access_url.clone().is_some(), |w| {
105 w.and().eq("access_url", self.access_url.clone().unwrap())
106 })
107 .r#if(self.file_ext.clone().is_some(), |w| {
108 w.and().eq("file_ext", self.file_ext.clone().unwrap())
109 })
110 .r#if(self.content_type.clone().is_some(), |w| {
111 w.and()
112 .eq("content_type", self.content_type.clone().unwrap())
113 })
114 .r#if(self.file_size.clone().is_some(), |w| {
115 w.and().eq("file_size", self.file_size.unwrap())
116 })
117 .r#if(self.file_size_display.clone().is_some(), |w| {
118 w.and()
119 .eq("file_size_display", self.file_size_display.clone().unwrap())
120 })
121 .r#if(self.modify_by.clone().is_some(), |w| {
122 w.and().eq("modify_by", self.modify_by.clone().unwrap())
123 })
124 .r#if(self.create_time.clone().is_some(), |w| {
125 w.and().eq("create_time", self.create_time.unwrap())
126 })
127 .r#if(self.update_time.clone().is_some(), |w| {
128 w.and().eq("update_time", self.update_time.unwrap())
129 });
130 rb.remove_by_wrapper::<Self>(wp).await
131 }
132
133 #[allow(dead_code)]
134 pub async fn remove(&mut self, rb: &mut RBatisTxExecutor<'_>) -> Result<u64, Error> {
135 let wp = rb
136 .get_rbatis()
137 .new_wrapper()
138 .eq("attachment_id", self.attachment_id);
139 rb.remove_by_wrapper::<Self>(wp).await
140 }
141
142 #[allow(dead_code)]
143 pub async fn remove_ids(rb: &mut RBatisTxExecutor<'_>, ids: &[i64]) -> Result<u64, Error> {
144 let wp = rb.get_rbatis().new_wrapper().r#in("attachment_id", ids);
145 rb.remove_by_wrapper::<Self>(wp).await
146 }
147
148 #[allow(dead_code)]
149 pub async fn load_ids(rb: &Rbatis, ids: &[i64]) -> Result<Vec<Self>, Error> {
150 let wp = rb.new_wrapper().r#in("attachment_id", ids);
151 rb.fetch_list_by_wrapper::<Self>(wp).await
152 }
153
154 #[allow(dead_code)]
155 pub async fn query_paged(&self, rb: &Rbatis, curr: u64, ps: u64) -> Result<Page<Self>, Error> {
156 let wp = rb
157 .new_wrapper()
158 .r#if(self.attachment_id.clone().is_some(), |w| {
159 w.and().eq("attachment_id", self.attachment_id.unwrap())
160 })
161 .r#if(self.attach_hash.clone().is_some(), |w| {
162 w.and().eq("attach_hash", self.attach_hash.clone().unwrap())
163 })
164 .r#if(self.attachment_name.clone().is_some(), |w| {
165 w.and()
166 .eq("attachment_name", self.attachment_name.clone().unwrap())
167 })
168 .r#if(self.storage_path.clone().is_some(), |w| {
169 w.and()
170 .eq("storage_path", self.storage_path.clone().unwrap())
171 })
172 .r#if(self.access_url.clone().is_some(), |w| {
173 w.and().eq("access_url", self.access_url.clone().unwrap())
174 })
175 .r#if(self.file_ext.clone().is_some(), |w| {
176 w.and().eq("file_ext", self.file_ext.clone().unwrap())
177 })
178 .r#if(self.content_type.clone().is_some(), |w| {
179 w.and()
180 .eq("content_type", self.content_type.clone().unwrap())
181 })
182 .r#if(self.file_size.clone().is_some(), |w| {
183 w.and().eq("file_size", self.file_size.unwrap())
184 })
185 .r#if(self.file_size_display.clone().is_some(), |w| {
186 w.and()
187 .eq("file_size_display", self.file_size_display.clone().unwrap())
188 })
189 .r#if(self.modify_by.clone().is_some(), |w| {
190 w.and().eq("modify_by", self.modify_by.clone().unwrap())
191 })
192 .r#if(self.create_time.clone().is_some(), |w| {
193 w.and().eq("create_time", self.create_time.unwrap())
194 })
195 .r#if(self.update_time.clone().is_some(), |w| {
196 w.and().eq("update_time", self.update_time.unwrap())
197 });
198 rb.fetch_page_by_wrapper::<Self>(wp, &PageRequest::new(curr, ps))
199 .await
200 }
201
202 #[allow(dead_code)]
203 pub async fn query_list(&self, rb: &Rbatis) -> Result<Vec<Self>, Error> {
204 let wp = rb
205 .new_wrapper()
206 .r#if(self.attachment_id.clone().is_some(), |w| {
207 w.and().eq("attachment_id", self.attachment_id.unwrap())
208 })
209 .r#if(self.attach_hash.clone().is_some(), |w| {
210 w.and().eq("attach_hash", self.attach_hash.clone().unwrap())
211 })
212 .r#if(self.attachment_name.clone().is_some(), |w| {
213 w.and()
214 .eq("attachment_name", self.attachment_name.clone().unwrap())
215 })
216 .r#if(self.storage_path.clone().is_some(), |w| {
217 w.and()
218 .eq("storage_path", self.storage_path.clone().unwrap())
219 })
220 .r#if(self.access_url.clone().is_some(), |w| {
221 w.and().eq("access_url", self.access_url.clone().unwrap())
222 })
223 .r#if(self.file_ext.clone().is_some(), |w| {
224 w.and().eq("file_ext", self.file_ext.clone().unwrap())
225 })
226 .r#if(self.content_type.clone().is_some(), |w| {
227 w.and()
228 .eq("content_type", self.content_type.clone().unwrap())
229 })
230 .r#if(self.file_size.clone().is_some(), |w| {
231 w.and().eq("file_size", self.file_size.unwrap())
232 })
233 .r#if(self.file_size_display.clone().is_some(), |w| {
234 w.and()
235 .eq("file_size_display", self.file_size_display.clone().unwrap())
236 })
237 .r#if(self.modify_by.clone().is_some(), |w| {
238 w.and().eq("modify_by", self.modify_by.clone().unwrap())
239 })
240 .r#if(self.create_time.clone().is_some(), |w| {
241 w.and().eq("create_time", self.create_time.unwrap())
242 })
243 .r#if(self.update_time.clone().is_some(), |w| {
244 w.and().eq("update_time", self.update_time.unwrap())
245 });
246 rb.fetch_list_by_wrapper::<Self>(wp).await
247 }
248
249 #[allow(dead_code)]
250 pub async fn query_all(rb: &Rbatis) -> Result<Vec<Self>, Error> {
251 let wp = rb.new_wrapper();
252 rb.fetch_list_by_wrapper::<Self>(wp).await
253 }
254
255 pub async fn find_attachments(
256 rb: &Rbatis,
257 busitype: &String,
258 busiid: &Option<i64>,
259 ) -> Result<Vec<Self>, Error> {
260 let sql = "SELECT p.* FROM chimes_attachments p INNER JOIN chimes_attachment_rel rp ON p.attachment_id = rp.attachment_id WHERE rp.business_name = ? AND rp.business_id = ?".to_string();
261 let mut rb_args = vec![];
262 rb_args.push(rbson::to_bson(busitype).unwrap_or_default());
263 rb_args.push(rbson::to_bson(busiid).unwrap_or_default());
264 rb.fetch(&sql, rb_args).await
265 }
266}