br_plugin/
model.rs

1#[cfg(any(feature = "mysql", feature = "mssql", feature = "sqlite"))]
2use std::{fs};
3#[cfg(any(feature = "mysql", feature = "mssql", feature = "sqlite"))]
4use std::collections::HashMap;
5#[cfg(any(feature = "mysql", feature = "mssql", feature = "sqlite"))]
6use std::fs::File;
7#[cfg(any(feature = "mysql", feature = "mssql", feature = "sqlite"))]
8use std::io::Write;
9use std::path::PathBuf;
10use br_fields::Field;
11use json::{array, JsonValue, object};
12
13#[cfg(any(feature = "mysql", feature = "mssql", feature = "sqlite"))]
14use br_db::Table;
15use crate::{BtnColor, BtnMode, PLUGIN_TOOLS, Tools};
16use crate::action::{Action, ActionTemp};
17use crate::addon::AddonTemp;
18#[cfg(any(feature = "mysql", feature = "mssql", feature = "sqlite"))]
19use crate::table::Tables;
20
21
22/// 模型
23pub trait Model {
24    /// 版本号
25    fn version(&mut self) -> &'static str {
26        "0.0.1"
27    }
28    /// 模型名称
29    fn name(&mut self) -> &'static str { "" }
30    /// 模型标题
31    fn title(&mut self) -> &'static str;
32    /// 模型描述
33    fn describe(&mut self) -> &'static str { "" }
34
35    /// 数据库表名称
36    fn table(&mut self) -> &'static str;
37    /// 数据库唯一约束
38    fn unique(&mut self) -> Vec<&'static str> {
39        vec![]
40    }
41    /// 查询索引
42    fn index(&mut self) -> Vec<Vec<&'static str>> {
43        vec![]
44    }
45    /// 主键
46    fn primary_key(&mut self) -> &'static str {
47        "id"
48    }
49    /// 数据库分区
50    fn partition(&mut self) -> bool {
51        false
52    }
53    /// 分区配置
54    /// *列名
55    /// *分区key
56    /// *实例 array![str, array![range1, range2, range3...]]
57    /// *如果在已有分区的情况下想要修改分区的参照列,先要将当前分区代码删除后刷库,然后写新的分区规则再刷库
58    fn partition_columns(&mut self) -> JsonValue {
59        array![]
60    }
61    /// 字段列表
62    fn fields(&mut self) -> JsonValue;
63    /// 初始化数据库
64    fn init_data(&mut self) {}
65    /// 创建安装json
66    #[cfg(any(feature = "mysql", feature = "mssql", feature = "sqlite"))]
67    fn json(&mut self) -> Table {
68        Table {
69            version: self.version().parse().unwrap(),
70            table: self.table().parse().unwrap(),
71            title: self.title().parse().unwrap(),
72            primary_key: self.primary_key().parse().unwrap(),
73            unique: self.unique().iter().map(|x| x.to_string()).collect(),
74            index: self.index().iter().map(|x| x.iter().map(|y| y.to_string()).collect()).collect(),
75            fields: self.fields(),
76            partition: self.partition(),
77            partition_columns: self.partition_columns(),
78        }
79    }
80    /// 创建安装文件
81    #[cfg(any(feature = "mysql", feature = "mssql", feature = "sqlite"))]
82    fn create_json_file(&mut self, path: &str) -> bool {
83        let json = self.json();
84        let o = PathBuf::from(path);
85        if !o.is_dir() {
86            fs::create_dir_all(o.clone()).unwrap();
87        }
88        let version = self.version();
89        let version = version.replace(".", "_");
90        let dir = o.join(format!("{}_{}.json", self.table(), version));
91        let mut f = File::create(dir.to_str().unwrap()).unwrap();
92        f.write_all(json.to_str().as_bytes()).is_ok()
93    }
94    /// 模型动作
95    fn action(&mut self, name: &str) -> Box<dyn Action>;
96    /// 前端搜索字段
97    fn table_fields(&mut self) -> JsonValue {
98        let mut params = object! {};
99        params["load"] = br_fields::select::Radio::new(false, "load", "加载内容", vec!["col", "data"], "data").field();
100        params["page"] = br_fields::int::Int::new(false, "page", "页数", 15, 1).field();
101        params["limit"] = br_fields::int::Int::new(false, "limit", "记录数", 10, 25).field();
102        params["where"] = br_fields::text::Json::new(false, "where", "条件", array![]).field();
103        params["filter"] = br_fields::text::Text::new(false, "filter", "模糊搜索", "").field();
104        params["order"] = br_fields::text::Json::new(false, "order", "排序", array!["id",true]).field();
105        params
106    }
107    /// 获取工具集合
108    fn tools(&mut self) -> Tools {
109        let db = PLUGIN_TOOLS.lock().unwrap();
110        let db = db.get("tools").unwrap().clone();
111        db
112    }
113    /// 前端-数据表
114    #[cfg(any(feature = "mysql", feature = "mssql", feature = "sqlite"))]
115    fn tables(&mut self, _header: JsonValue, request: JsonValue, fields: Vec<&str>, query_fields: Vec<&str>, filter_fields: Vec<&str>) -> JsonValue {
116        let load = request["load"].as_str().unwrap();
117        let page = request["page"].as_i32().unwrap();
118        let limit = request["limit"].as_i32().unwrap();
119        let wheres = request["where"].clone();
120        let filter = request["filter"].to_string();
121        let order = request["order"].clone();
122        let columns = self.columns(fields.clone());
123        let mut fields_json = vec![];
124        for field in columns.members() {
125            match field["mode"].as_str().unwrap() {
126                "select" | "array" | "json" | "object" => {
127                    fields_json.push(field["name"].clone().to_string());
128                }
129                _ => {}
130            }
131        }
132        // 查询到的数据
133        let mut data = {
134            let mut db = self.tools().db;
135            let db = db.table(self.table());
136            // 整合搜索字段
137            if !filter_fields.is_empty() && !filter.is_empty() {
138                db.where_and(filter_fields.join("|").as_str(), "like", format!("%{}%", filter).into());
139            }
140            if !fields.is_empty() {
141                db.field(fields.join(",").as_str());
142            }
143            for item in wheres.members() {
144                db.where_and(item[0].as_str().unwrap_or(""), item[1].as_str().unwrap_or(""), item[2].clone());
145            }
146            if !order.is_empty() {
147                db.order(order[0].as_str().unwrap(), order[1].as_bool().unwrap());
148            }
149            if !fields_json.is_empty() {
150                for json in fields_json.iter() {
151                    db.json(json);
152                }
153            }
154            db.page(page, limit).select()
155        };
156        // 总记录数
157        let total = {
158            let mut db = self.tools().db;
159            let db = db.table(self.table());
160            if !filter_fields.is_empty() && !filter.is_empty() {
161                db.where_and(filter_fields.join("|").as_str(), "like", format!("%{}%", filter).into());
162            }
163            for item in wheres.members() {
164                db.where_and(item[0].as_str().unwrap_or(""), item[1].as_str().unwrap_or(""), item[2].clone());
165            }
166            db.count().as_i32().unwrap_or(0)
167        };
168
169
170        let mut fields_table = false;
171        let mut fields_list = object! {};
172        for field in columns.members() {
173            fields_list[field["field"].as_str().unwrap()] = field.clone();
174            if field["mode"].as_str().unwrap() == "table" {
175                fields_table = true;
176            }
177        }
178        if fields_table {
179            let mut fields_table_ids = object! {};
180            for item in data.members_mut() {
181                for (key, field) in fields_list.entries() {
182                    if field["mode"].as_str().unwrap() == "table" {
183                        let id = item[key].clone();
184                        if id.is_empty() {
185                            continue;
186                        }
187                        if id.to_string().contains(",") {
188                            item[key] = array![];
189                            let ids = id.to_string();
190                            let ids = ids.split(",").collect::<Vec<&str>>();
191                            for id in ids.iter() {
192                                if fields_table_ids[id.to_string().as_str()].is_null() {
193                                    let table = field["table"].as_str().unwrap();
194                                    let fields_list: Vec<&str> = field["fields"].members().map(|x| x.as_str().unwrap()).collect();
195                                    let find = self.tools().db.table(table)
196                                        .where_and("id", "=", id.to_string().into())
197                                        .field(format!("id,{}", fields_list.join(",")).as_str())
198                                        .find();
199                                    let mut row = object! {};
200                                    if !find.is_empty() {
201                                        row["id"] = find["id"].clone();
202                                        for field in fields_list.iter() {
203                                            if row["value"].is_null() {
204                                                row["value"] = format!("{}", find[field.to_string().as_str()]).into();
205                                            } else {
206                                                row["value"] = format!("{} | {}", row["value"], find[field.to_string().as_str()]).into();
207                                            }
208                                        }
209                                        fields_table_ids[id.to_string()] = row.clone();
210                                    } else {
211                                        fields_table_ids[id.to_string()] = object! {};
212                                    }
213                                }
214                                if !fields_table_ids[id.to_string()].clone().is_empty() {
215                                    item[key].push(fields_table_ids[id.to_string()].clone()).unwrap();
216                                }
217                            }
218                        } else {
219                            item[key] = object! {};
220                            if fields_table_ids[id.as_str().unwrap()].is_null() {
221                                let table = field["table"].as_str().unwrap();
222                                let fields_list: Vec<&str> = field["fields"].members().map(|x| x.as_str().unwrap()).collect();
223                                let find = self.tools().db.table(table)
224                                    .where_and("id", "=", id.clone())
225                                    .field(format!("id,{}", fields_list.join(",")).as_str())
226                                    .find();
227
228
229                                let mut row = object! {};
230                                if !find.is_empty() {
231                                    row["id"] = find["id"].clone();
232                                    for field in fields_list.iter() {
233                                        if row["value"].is_null() {
234                                            row["value"] = format!("{}", find[field.to_string()]).into();
235                                        } else {
236                                            row["value"] = format!("{} | {}", row["value"], find[field.to_string()]).into();
237                                        }
238                                    }
239                                    fields_table_ids[id.as_str().unwrap()] = row;
240                                } else {
241                                    fields_table_ids[id.as_str().unwrap()] = object! {};
242                                }
243                            }
244                            if !fields_table_ids[id.as_str().unwrap()].clone().is_empty() {
245                                // item[key].push(fields_table_ids[id.as_str().unwrap()].clone()).unwrap();
246                                item[key] = fields_table_ids[id.as_str().unwrap()].clone();
247                            }
248                        }
249                    }
250                }
251            }
252        }
253
254        let mut table = object! {};
255        table["total_page"] = JsonValue::from((total as f64 / limit as f64).ceil() as i64);
256        table["total_data"] = total.into();
257        table["data"] = data.clone();
258        if load == "col" {
259            table["col"] = columns.clone();
260            table["query_fields"] = self.query_fields(query_fields.clone());
261            table["filter_title"] = self.filter_title(filter_fields.clone()).into();
262        }
263        table
264    }
265    /// 前端-链表-数据表
266    #[cfg(any(feature = "mysql", feature = "mssql", feature = "sqlite"))]
267    fn table_join(&mut self, table: &str, header: JsonValue, request: JsonValue) -> Tables {
268        let mut db = self.tools().db.clone();
269        db.table(table);
270        Tables {
271            table: table.to_string(),
272            header,
273            request,
274            db: db.clone(),
275            search_fields: HashMap::new(),
276            hidden_fields: HashMap::new(),
277            query_fields: HashMap::new(),
278            tables: Vec::new(),
279            fields: HashMap::new(),
280            columns: array![],
281            main_field: HashMap::new(),
282            right_field: HashMap::new(),
283        }
284    }
285    /// 前端-树型-搜索字段
286    fn table_tree_fields(&mut self) -> JsonValue {
287        let mut params = object! {};
288        params["where"] = br_fields::text::Json::new(false, "where", "条件", array![]).field();
289        params
290    }
291    /// 前端-树型-数据表
292    #[cfg(any(feature = "mysql", feature = "mssql", feature = "sqlite"))]
293    fn table_tree(&mut self, _header: JsonValue, request: JsonValue, sort: (&str, bool), label: &str, filter_fields: Vec<&str>, search_fields: Vec<&str>) -> JsonValue {
294        let t = self.fields_name_list(filter_fields);
295        let fields = t.iter().map(|x| x.as_str()).collect::<Vec<&str>>();
296        let wheres = request["where"].clone();
297        let table = self.table();
298
299        let mut db = self.tools().db;
300        let db = db.table(table);
301
302        // 处理字段
303        if !fields.is_empty() {
304            db.field(fields.join(",").as_str());
305        }
306
307        for item in wheres.members() {
308            db.where_and(item[0].as_str().unwrap_or(""), item[1].as_str().unwrap_or(""), item[2].clone());
309        }
310
311        db.order(sort.0, sort.1);
312
313        let data = db.select();
314        let children = self.table_tree_assemble(data, "", table, label);
315        let mut table = object! {};
316        table["data"] = children.clone();
317        table["search_fields"] = self.filter_title(search_fields.clone()).into();
318        table
319    }
320    #[cfg(any(feature = "mysql", feature = "mssql", feature = "sqlite"))]
321    fn table_tree_assemble(&mut self, data: JsonValue, pid: &str, field: &str, label: &str) -> JsonValue {
322        let mut list = array![];
323        let mut children = array![];
324
325        for item in data.members() {
326            let pid_pid = item[field].to_string();
327            if pid_pid == pid {
328                let res = object! {
329                    id:item["id"].clone(),
330                    label:item[label].clone(),
331                    data:item.clone()
332                };
333                list.push(res.clone()).unwrap();
334            } else {
335                children.push(item.clone()).unwrap();
336            }
337        }
338        for item in list.members_mut() {
339            let pid_new = item["id"].clone();
340            let children_list = self.table_tree_assemble(children.clone(), pid_new.to_string().as_str(), field, label);
341            if !children_list.is_empty() {
342                item["children"] = children_list.clone();
343            }
344        }
345        list
346    }
347    /// 获取 tree 上级级指定列
348    #[cfg(any(feature = "mysql", feature = "mssql", feature = "sqlite"))]
349    fn get_tree_parent_ids(&mut self, pid_field: &str, column_field: &str, value: &str) -> JsonValue {
350        let mut list = array![value];
351        let ids = self.tools().db.table(self.table()).where_and(column_field, "=", value.into()).column(pid_field);
352        for item in ids.members() {
353            let res = self.get_tree_parent_ids(pid_field, column_field, item.as_str().unwrap());
354            for member in res.members() {
355                list.push(member.clone()).unwrap();
356            }
357        }
358        list
359    }
360    /// 获取 tree 下级指定列
361    #[cfg(any(feature = "mysql", feature = "mssql", feature = "sqlite"))]
362    fn get_subordinate_ids(&mut self, pid_field: &str, column_field: &str, value: &str) -> JsonValue {
363        let mut list = array![value];
364        let ids = self.tools().db.table(self.table()).where_and(pid_field, "=", value.into()).column(column_field);
365        for item in ids.members() {
366            let res = self.get_subordinate_ids(pid_field, column_field, item.as_str().unwrap());
367            for member in res.members() {
368                list.push(member.clone()).unwrap();
369            }
370        }
371        list
372    }
373    /// 前端-字典-数据表
374    #[cfg(any(feature = "mysql", feature = "mssql", feature = "sqlite"))]
375    fn dicts(&mut self, _header: JsonValue, request: JsonValue, fields: Vec<&str>, query_fields: Vec<&str>, filter_fields: Vec<&str>) -> JsonValue {
376        let load = request["load"].as_str().unwrap();
377        let page = request["page"].as_i32().unwrap();
378        let limit = request["limit"].as_i32().unwrap();
379        let wheres = request["where"].clone();
380        let filter = request["filter"].to_string();
381        let order = request["order"].clone();
382        let columns = self.columns(fields.clone());
383        let mut fields_json = vec![];
384        for field in columns.members() {
385            match field["mode"].as_str().unwrap() {
386                "select" | "array" | "json" | "object" => {
387                    fields_json.push(field["name"].as_str().unwrap());
388                }
389                _ => {}
390            }
391        }
392        // 查询到的数据
393        let mut data = {
394            let mut db = self.tools().db;
395            let db = db.table(self.table());
396            // 整合搜索字段
397            if !filter_fields.is_empty() && !filter.is_empty() {
398                db.where_and(filter_fields.join("|").as_str(), "like", format!("%{}%", filter).into());
399            }
400            if !fields.is_empty() {
401                db.field(fields.join(",").as_str());
402            }
403            for item in wheres.members() {
404                db.where_and(item[0].as_str().unwrap_or(""), item[1].as_str().unwrap_or(""), item[2].clone());
405            }
406            if !order.is_empty() {
407                db.order(order[0].as_str().unwrap(), order[1].as_bool().unwrap());
408            }
409            if !fields_json.is_empty() {
410                for json in fields_json.iter() {
411                    db.json(json);
412                }
413            }
414            db.page(page, limit).select()
415        };
416
417        // 总记录数
418        let total = {
419            let mut db = self.tools().db;
420            let db = db.table(self.table());
421            if !filter_fields.is_empty() && !filter.is_empty() {
422                db.where_and(filter_fields.join("|").as_str(), "like", format!("%{}%", filter).into());
423            }
424            for item in wheres.members() {
425                db.where_and(item[0].as_str().unwrap_or(""), item[1].as_str().unwrap_or(""), item[2].clone());
426            }
427            db.count().as_i32().unwrap_or(0)
428        };
429
430
431        let mut fields_table = false;
432        let mut fields_list = object! {};
433        for field in columns.members() {
434            fields_list[field["field"].as_str().unwrap()] = field.clone();
435            if field["mode"].as_str().unwrap() == "table" {
436                fields_table = true;
437            }
438        }
439        if fields_table {
440            let mut fields_table_ids = object! {};
441            for item in data.members_mut() {
442                for (key, field) in fields_list.entries() {
443                    if field["mode"].as_str().unwrap() == "table" {
444                        let id = item[key].clone();
445                        if id.is_empty() {
446                            continue;
447                        }
448                        if fields_table_ids[id.as_str().unwrap()].is_null() {
449                            let table = field["table"].as_str().unwrap();
450                            let fields_list: Vec<&str> = field["fields"].members().map(|x| x.as_str().unwrap()).collect();
451                            let find = self.tools().db.table(table)
452                                .where_and("id", "=", id.clone())
453                                .field(format!("id,{}", fields_list.join(",")).as_str())
454                                .find();
455                            let mut row = object! {};
456                            if !find.is_empty() {
457                                row["id"] = find["id"].clone();
458                                for field in fields_list.iter() {
459                                    if row["value"].is_null() {
460                                        row["value"] = format!("{}", find[field.to_string()]).into();
461                                    } else {
462                                        row["value"] = format!("{} | {}", row["value"], find[field.to_string()]).into();
463                                    }
464                                }
465                                fields_table_ids[id.as_str().unwrap()] = row;
466                            } else {
467                                fields_table_ids[id.as_str().unwrap()] = object! {};
468                            }
469                        }
470                        item[key] = fields_table_ids[id.as_str().unwrap()].clone();
471                    }
472                }
473            }
474        }
475
476        let mut table = object! {};
477        table["total_page"] = JsonValue::from((total as f64 / limit as f64).ceil() as i64);
478        table["total_data"] = total.into();
479        table["data"] = data.clone();
480        if load == "col" {
481            table["col"] = columns.clone();
482            table["query_fields"] = self.query_fields(query_fields.clone());
483            table["filter_title"] = self.filter_title(filter_fields.clone()).into();
484        }
485        table
486    }
487
488    /// 前端-列数据
489    fn columns(&mut self, fields: Vec<&str>) -> JsonValue {
490        let columns = self.fields();
491        let mut data = array![];
492        for (key, field) in columns.entries() {
493            if fields.contains(&key) || fields.is_empty() {
494                let mut row = field.clone();
495                row["name"] = field["field"].clone();
496                row["label"] = field["title"].clone();
497                row["align"] = "center".into();
498                row["sortable"] = match field["mode"].as_str().unwrap() {
499                    "int" | "float" | "switch" | "datetime" | "date" | "time" | "year" | "timestamp" => {
500                        true.into()
501                    }
502                    _ => {
503                        false.into()
504                    }
505                };
506                data.push(row.clone()).unwrap();
507            }
508        }
509        data
510    }
511    /// 前端-高级查询条件字段
512    fn query_fields(&mut self, fields: Vec<&str>) -> JsonValue {
513        let columns = self.fields();
514        let mut data = array![];
515        for (key, field) in columns.entries() {
516            if fields.contains(&key) {
517                let mut row = field.clone();
518                row["require"] = JsonValue::from(false);
519                data.push(row.clone()).unwrap();
520            }
521        }
522        data
523    }
524    /// 前端-模糊查询标题
525    fn filter_title(&mut self, fields: Vec<&str>) -> String {
526        let columns = self.fields();
527        let mut data = vec![];
528        for (key, field) in columns.entries() {
529            if fields.contains(&key) {
530                data.push(field["title"].as_str().unwrap());
531            }
532        }
533        format!("搜索 {}", data.join(" "))
534    }
535
536    /// 前端-按钮数据
537    fn btn_data(&mut self, title: &str, mut action: Box<dyn Action>, mode: BtnMode, color: BtnColor, match_condition: Vec<Vec<&str>>) -> JsonValue {
538        let mut btn = object! {};
539        if title.is_empty() {
540            btn["title"] = action.title().into();
541        } else {
542            btn["title"] = title.into();
543        }
544        btn["api"] = action.api().into();
545        let mut params = array![];
546        for (_, item) in action.params().entries() {
547            params.push(item.clone()).unwrap();
548        }
549        btn["params"] = params;
550        btn["color"] = JsonValue::from(color.from());
551        btn["mode"] = JsonValue::from(mode.from());
552        btn["match_condition"] = match_condition.into();
553        btn["auth"] = action.auth().into();
554        btn
555    }
556
557    /// 前端 关联搜索字段
558    fn table_select_fields(&mut self) -> JsonValue {
559        let mut fields = object! {};
560        fields[self.primary_key()] = br_fields::str::Key::new(false, self.primary_key(), "ID", 20).field();
561        fields["filter"] = br_fields::text::Text::new(false, "filter", "模糊搜索", "").field();
562        fields["page"] = br_fields::int::Int::new(false, "page", "页数", 15, 1).field();
563        fields["limit"] = br_fields::int::Int::new(false, "limit", "记录数", 10, 25).field();
564        fields
565    }
566    /// 前端-select
567    #[cfg(any(feature = "mysql", feature = "mssql", feature = "sqlite"))]
568    fn table_select(&mut self, request: JsonValue, fields: Vec<&str>, filter: Vec<Vec<&str>>) -> JsonValue {
569        let id = request["id"].as_str().unwrap_or("");
570        let wheres = request["filter"].as_str().unwrap();
571        let page = request["page"].as_i32().unwrap();
572        let limit = request["limit"].as_i32().unwrap();
573
574        let mut db = self.tools().db;
575        let db = db.table(self.table());
576        db.field(format!("id,{}", fields.join(",")).as_str());
577
578        for item in filter.iter() {
579            db.where_and(item[0], item[1], item[2].into());
580        }
581        if !wheres.is_empty() {
582            if !id.is_empty() {
583                db.where_or("id", "=", id.into());
584            }
585            db.where_or(fields.join("|").as_str(), "like", format!("%{}%", wheres).into());
586        } else if !id.is_empty() {
587            db.where_and("id", "=", id.into());
588        }
589
590        let data = db.page(page, limit).select();
591        let mut list = array![];
592        for item in data.members() {
593            let mut row = object! {};
594            row["id"] = item["id"].clone();
595            for field in fields.iter() {
596                if row["value"].is_null() {
597                    row["value"] = format!("{}", item[field.to_string()]).into();
598                } else {
599                    row["value"] = format!("{} | {}", row["value"], item[field.to_string()]).into();
600                }
601            }
602            list.push(row).unwrap();
603        }
604        list
605    }
606
607    /// 前端-select-where-or
608    #[cfg(any(feature = "mysql", feature = "mssql", feature = "sqlite"))]
609    fn table_select_or(&mut self, request: JsonValue, fields: Vec<&str>, filter: Vec<Vec<&str>>, where_or: Vec<Vec<&str>>, table: &str) -> JsonValue {
610        let id = request["id"].as_str().unwrap_or("");
611        let wheres = request["filter"].as_str().unwrap();
612        let page = request["page"].as_i32().unwrap();
613        let limit = request["limit"].as_i32().unwrap();
614
615        let mut db = self.tools().db;
616        let db = if table.is_empty() { db.table(self.table()) } else { db.table(table) };
617        db.field(format!("id,{}", fields.join(",")).as_str());
618
619        for item in filter.iter() {
620            db.where_and(item[0], item[1], item[2].into());
621        }
622
623        for item in where_or.iter() {
624            db.where_or(item[0], item[1], item[2].into());
625            if !wheres.is_empty() {
626                db.where_and(fields.join("|").as_str(), "like", format!("%{}%", wheres).into());
627            }
628        }
629
630        if !wheres.is_empty() {
631            if !id.is_empty() {
632                db.where_or("id", "=", id.into());
633            }
634            db.where_or(fields.join("|").as_str(), "like", format!("%{}%", wheres).into());
635        } else if !id.is_empty() {
636            db.where_or("id", "=", id.into()).where_or("id", "!=", id.into());
637        }
638
639        let data = db.page(page, limit).select();
640        let mut list = array![];
641        for item in data.members() {
642            let mut row = object! {};
643            row["id"] = item["id"].clone();
644            for field in fields.iter() {
645                if row["value"].is_null() {
646                    row["value"] = format!("{}", item[field.to_string()]).into();
647                } else {
648                    row["value"] = format!("{} | {}", row["value"], item[field.to_string()]).into();
649                }
650            }
651            list.push(row).unwrap();
652        }
653        list
654    }
655
656    /// 前端-select-分页
657    #[cfg(any(feature = "mysql", feature = "mssql", feature = "sqlite"))]
658    fn table_select_page(&mut self, request: JsonValue, fields: Vec<&str>, filter: Vec<Vec<&str>>, where_or: Vec<Vec<&str>>) -> JsonValue {
659        let id = request["id"].as_str().unwrap_or("");
660        let wheres = request["filter"].as_str().unwrap();
661        let page = request["page"].as_i32().unwrap();
662        let limit = request["limit"].as_i32().unwrap();
663
664        let mut db = self.tools().db;
665        let db = db.table(self.table());
666        db.field(format!("id,{}", fields.join(",")).as_str());
667
668        for item in filter.iter() {
669            db.where_and(item[0], item[1], item[2].into());
670        }
671        for item in where_or.iter() {
672            db.where_or(item[0], item[1], item[2].into());
673            if !wheres.is_empty() {
674                db.where_and(fields.join("|").as_str(), "like", format!("%{}%", wheres).into());
675            }
676        }
677
678        if !wheres.is_empty() {
679            if !id.is_empty() {
680                db.where_or("id", "=", id.into());
681            }
682            db.where_or(fields.join("|").as_str(), "like", format!("%{}%", wheres).into());
683        } else if !id.is_empty() {
684            db.where_or("id", "=", id.into()).where_or("id", "!=", id.into());
685        }
686        let total_count = db.clone().count();
687        let total_page = (total_count.to_string().parse::<f64>().unwrap() / limit.clone().to_string().parse::<f64>().unwrap()).ceil();
688        let data = db.page(page, limit).select();
689        let mut list = array![];
690        for item in data.members() {
691            let mut row = object! {};
692            row["id"] = item["id"].clone();
693            for field in fields.iter() {
694                if row["value"].is_null() {
695                    row["value"] = format!("{}", item[field.to_string()]).into();
696                } else {
697                    row["value"] = format!("{} | {}", row["value"], item[field.to_string()]).into();
698                }
699            }
700            list.push(row).unwrap();
701        }
702
703        object! {
704            data: list.clone(),
705            total_page: total_page
706        }
707    }
708
709    /// 前端 关联字典搜索字段
710    fn dict_select_fields(&mut self) -> JsonValue {
711        let mut fields = object! {};
712        fields["value"] = br_fields::str::Str::new(false, "value", "当前值", 100, "").field();
713        fields["filter"] = br_fields::text::Text::new(false, "filter", "模糊搜索", "").field();
714        fields["page"] = br_fields::int::Int::new(false, "page", "页数", 15, 1).field();
715        fields["limit"] = br_fields::int::Int::new(false, "limit", "记录数", 10, 25).field();
716        fields["field"] = br_fields::str::Str::new(true, "field", "值字段", 100, "").field();
717        fields
718    }
719    /// 前端-dict-select
720    #[cfg(any(feature = "mysql", feature = "mssql", feature = "sqlite"))]
721    fn dict_select(&mut self, request: JsonValue, fields: Vec<&str>, filter: Vec<Vec<&str>>) -> JsonValue {
722        let value = request["value"].as_str().unwrap_or("");
723        let wheres = request["filter"].as_str().unwrap();
724        let page = request["page"].as_i32().unwrap();
725        let limit = request["limit"].as_i32().unwrap();
726        let field = request["field"].as_str().unwrap();
727
728        let mut db = self.tools().db;
729        let db = db.table(self.table());
730        db.field(format!("{},{}", field, fields.join(",")).as_str());
731
732        for item in filter.iter() {
733            db.where_and(item[0], item[1], item[2].into());
734        }
735        if !wheres.is_empty() {
736            if !value.is_empty() {
737                db.where_or(field, "=", value.into());
738            }
739            db.where_or(fields.join("|").as_str(), "like", format!("%{}%", wheres).into());
740        } else if !value.is_empty() {
741            db.where_or(field, "=", value.into()).where_or(field, "!=", field.into());
742        }
743        let data = db.page(page, limit).select();
744        let mut list = array![];
745        for item in data.members() {
746            let mut row = object! {};
747            row["value"] = item[field].clone();
748            for field in fields.iter() {
749                if row["data"].is_null() {
750                    row["data"] = format!("{}", item[field.to_string()]).into();
751                } else {
752                    row["data"] = format!("{} | {}", row["data"], item[field.to_string()]).into();
753                }
754            }
755            list.push(row).unwrap();
756        }
757        list
758    }
759
760    /// 模型字段名称列表
761    fn fields_name_list(&mut self, filter: Vec<&str>) -> Vec<String> {
762        let mut fields = vec![];
763        for (key, _) in self.fields().entries() {
764            if filter.contains(&key) {
765                continue;
766            }
767            fields.push(key.to_string());
768        }
769        fields
770    }
771
772    /// 获取模块菜单列表
773    #[cfg(any(feature = "mysql", feature = "mssql", feature = "sqlite"))]
774    fn get_menu_list(&mut self, mode: &str, addon: &str) -> JsonValue {
775        let mut data = self.tools().db;
776        data.table("auth_auth")
777            .where_and("mode", "like", format!("%{}%", mode).into())
778            .where_and("addon", "=", addon.into())
779            .where_and("interface_type", "=", "menu".into())
780            .order("menu_sort", true).select()
781    }
782    /// 获取按钮列表
783    #[cfg(any(feature = "mysql", feature = "mssql", feature = "sqlite"))]
784    fn btn_list(&mut self, mode: &str, addon: &str, model: &str) -> JsonValue {
785        self.tools().db.table("auth_auth")
786            .where_and("mode", "like", format!("%{}%", mode).into())
787            .where_and("addon", "=", addon.into())
788            .where_and("model", "=", model.into())
789            .where_and("auth", "=", 1.into())
790            .select()
791    }
792}
793
794
795/// 模型模版
796pub struct ModelTemp {
797    pub addon: AddonTemp,
798}
799
800impl Model for ModelTemp {
801    fn name(&mut self) -> &'static str { todo!() }
802    fn table(&mut self) -> &'static str {
803        todo!()
804    }
805
806    fn title(&mut self) -> &'static str {
807        todo!()
808    }
809
810    fn fields(&mut self) -> JsonValue {
811        todo!()
812    }
813
814    fn action(&mut self, _name: &str) -> Box<dyn Action> {
815        Box::new(ActionTemp { model: ModelTemp { addon: AddonTemp {} } })
816    }
817}