2022-06-30 23:00:03 +08:00
|
|
|
use crate::entities::FieldType;
|
2022-06-19 21:10:07 +08:00
|
|
|
use indexmap::IndexMap;
|
2022-06-21 16:56:50 +08:00
|
|
|
use nanoid::nanoid;
|
2022-06-15 17:24:46 +08:00
|
|
|
use serde::{Deserialize, Serialize};
|
|
|
|
|
use serde_repr::*;
|
2022-06-30 23:00:03 +08:00
|
|
|
use std::sync::Arc;
|
2022-06-21 16:56:50 +08:00
|
|
|
|
|
|
|
|
pub fn gen_grid_filter_id() -> String {
|
|
|
|
|
nanoid!(6)
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
pub fn gen_grid_group_id() -> String {
|
|
|
|
|
nanoid!(6)
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
pub fn gen_grid_sort_id() -> String {
|
|
|
|
|
nanoid!(6)
|
|
|
|
|
}
|
2022-06-15 17:24:46 +08:00
|
|
|
|
2022-06-20 10:24:43 +08:00
|
|
|
#[derive(Debug, Clone, Serialize, Deserialize, Default, Eq, PartialEq)]
|
2022-06-20 09:37:52 +08:00
|
|
|
pub struct GridSettingRevision {
|
2022-06-24 18:13:40 +08:00
|
|
|
pub layout: GridLayoutRevision,
|
2022-06-30 23:00:03 +08:00
|
|
|
// layout:
|
|
|
|
|
// field_id:
|
|
|
|
|
// FieldType: GridFilterRevision
|
|
|
|
|
// FieldType: GridFilterRevision
|
|
|
|
|
// layout:
|
|
|
|
|
// field_id:
|
|
|
|
|
// FieldType: GridFilterRevision
|
|
|
|
|
// field_id:
|
|
|
|
|
// FieldType: GridFilterRevision
|
2022-06-19 21:10:07 +08:00
|
|
|
#[serde(with = "indexmap::serde_seq")]
|
2022-06-30 23:00:03 +08:00
|
|
|
pub filters: IndexMap<GridLayoutRevision, IndexMap<String, GridFilterRevisionMap>>,
|
2022-06-15 17:24:46 +08:00
|
|
|
|
2022-06-21 16:56:50 +08:00
|
|
|
#[serde(skip, with = "indexmap::serde_seq")]
|
2022-06-29 16:55:52 +08:00
|
|
|
pub groups: IndexMap<GridLayoutRevision, Vec<GridGroupRevision>>,
|
2022-06-19 21:10:07 +08:00
|
|
|
|
2022-06-21 16:56:50 +08:00
|
|
|
#[serde(skip, with = "indexmap::serde_seq")]
|
2022-06-29 16:55:52 +08:00
|
|
|
pub sorts: IndexMap<GridLayoutRevision, Vec<GridSortRevision>>,
|
2022-06-15 17:24:46 +08:00
|
|
|
}
|
|
|
|
|
|
2022-06-30 23:00:03 +08:00
|
|
|
impl GridSettingRevision {
|
|
|
|
|
pub fn get_mut_filters(
|
|
|
|
|
&mut self,
|
|
|
|
|
layout: &GridLayoutRevision,
|
|
|
|
|
field_id: &str,
|
|
|
|
|
field_type: &FieldType,
|
|
|
|
|
) -> Option<&mut Vec<Arc<GridFilterRevision>>> {
|
|
|
|
|
self.filters
|
|
|
|
|
.get_mut(layout)
|
|
|
|
|
.and_then(|filter_rev_map_by_field_id| filter_rev_map_by_field_id.get_mut(field_id))
|
|
|
|
|
.and_then(|filter_rev_map| filter_rev_map.get_mut(field_type))
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
pub fn get_filters(
|
|
|
|
|
&self,
|
|
|
|
|
layout: &GridLayoutRevision,
|
|
|
|
|
field_id: &str,
|
|
|
|
|
field_type: &FieldType,
|
|
|
|
|
) -> Option<Vec<Arc<GridFilterRevision>>> {
|
|
|
|
|
self.filters
|
|
|
|
|
.get(layout)
|
|
|
|
|
.and_then(|filter_rev_map_by_field_id| filter_rev_map_by_field_id.get(field_id))
|
|
|
|
|
.and_then(|filter_rev_map| filter_rev_map.get(field_type))
|
|
|
|
|
.cloned()
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
pub fn insert_filter(
|
|
|
|
|
&mut self,
|
|
|
|
|
layout: &GridLayoutRevision,
|
|
|
|
|
field_id: &str,
|
|
|
|
|
field_type: &FieldType,
|
|
|
|
|
filter_rev: GridFilterRevision,
|
|
|
|
|
) {
|
|
|
|
|
let filter_rev_map_by_field_id = self.filters.entry(layout.clone()).or_insert_with(IndexMap::new);
|
|
|
|
|
let filter_rev_map = filter_rev_map_by_field_id
|
|
|
|
|
.entry(field_id.to_string())
|
|
|
|
|
.or_insert_with(GridFilterRevisionMap::new);
|
|
|
|
|
|
|
|
|
|
filter_rev_map
|
|
|
|
|
.entry(field_type.clone())
|
|
|
|
|
.or_insert_with(Vec::new)
|
|
|
|
|
.push(Arc::new(filter_rev))
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
#[derive(Debug, Clone, Serialize, Deserialize, Default, Eq, PartialEq)]
|
|
|
|
|
#[serde(transparent)]
|
|
|
|
|
pub struct GridFilterRevisionMap {
|
|
|
|
|
#[serde(with = "indexmap::serde_seq")]
|
|
|
|
|
pub filter_by_field_type: IndexMap<FieldType, Vec<Arc<GridFilterRevision>>>,
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
impl GridFilterRevisionMap {
|
|
|
|
|
pub fn new() -> Self {
|
|
|
|
|
GridFilterRevisionMap::default()
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
impl std::ops::Deref for GridFilterRevisionMap {
|
|
|
|
|
type Target = IndexMap<FieldType, Vec<Arc<GridFilterRevision>>>;
|
|
|
|
|
|
|
|
|
|
fn deref(&self) -> &Self::Target {
|
|
|
|
|
&self.filter_by_field_type
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
impl std::ops::DerefMut for GridFilterRevisionMap {
|
|
|
|
|
fn deref_mut(&mut self) -> &mut Self::Target {
|
|
|
|
|
&mut self.filter_by_field_type
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2022-06-19 21:10:07 +08:00
|
|
|
#[derive(Debug, PartialEq, Eq, Hash, Clone, Serialize_repr, Deserialize_repr)]
|
2022-06-15 17:24:46 +08:00
|
|
|
#[repr(u8)]
|
2022-06-19 21:10:07 +08:00
|
|
|
pub enum GridLayoutRevision {
|
2022-06-15 17:24:46 +08:00
|
|
|
Table = 0,
|
|
|
|
|
Board = 1,
|
|
|
|
|
}
|
|
|
|
|
|
2022-06-19 21:10:07 +08:00
|
|
|
impl ToString for GridLayoutRevision {
|
|
|
|
|
fn to_string(&self) -> String {
|
|
|
|
|
let layout_rev = self.clone() as u8;
|
|
|
|
|
layout_rev.to_string()
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
impl std::default::Default for GridLayoutRevision {
|
2022-06-15 17:24:46 +08:00
|
|
|
fn default() -> Self {
|
2022-06-19 21:10:07 +08:00
|
|
|
GridLayoutRevision::Table
|
2022-06-15 17:24:46 +08:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2022-06-30 23:00:03 +08:00
|
|
|
#[derive(Debug, Clone, Serialize, Deserialize, Default, PartialEq, Eq, Hash)]
|
2022-06-15 17:24:46 +08:00
|
|
|
pub struct GridFilterRevision {
|
2022-06-21 16:56:50 +08:00
|
|
|
pub id: String,
|
|
|
|
|
pub field_id: String,
|
2022-06-22 17:11:56 +08:00
|
|
|
pub condition: u8,
|
|
|
|
|
pub content: Option<String>,
|
2022-06-15 17:24:46 +08:00
|
|
|
}
|
|
|
|
|
|
2022-06-20 10:24:43 +08:00
|
|
|
#[derive(Debug, Clone, Serialize, Deserialize, Default, PartialEq, Eq)]
|
2022-06-15 17:24:46 +08:00
|
|
|
pub struct GridGroupRevision {
|
2022-06-21 16:56:50 +08:00
|
|
|
pub id: String,
|
|
|
|
|
pub field_id: Option<String>,
|
|
|
|
|
pub sub_field_id: Option<String>,
|
2022-06-15 17:24:46 +08:00
|
|
|
}
|
|
|
|
|
|
2022-06-20 10:24:43 +08:00
|
|
|
#[derive(Debug, Clone, Serialize, Deserialize, Default, PartialEq, Eq)]
|
2022-06-15 17:24:46 +08:00
|
|
|
pub struct GridSortRevision {
|
2022-06-21 16:56:50 +08:00
|
|
|
pub id: String,
|
2022-06-20 09:37:52 +08:00
|
|
|
pub field_id: Option<String>,
|
2022-06-15 17:24:46 +08:00
|
|
|
}
|