Compare commits
2 Commits
32eef5ecf1
...
a96b065190
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
a96b065190 | ||
|
|
b00fe44880 |
@@ -46,14 +46,7 @@ impl DictionaryService {
|
||||
let mut resps = Vec::with_capacity(models.len());
|
||||
for m in &models {
|
||||
let items = Self::fetch_items(m.id, tenant_id, db).await?;
|
||||
resps.push(DictionaryResp {
|
||||
id: m.id,
|
||||
name: m.name.clone(),
|
||||
code: m.code.clone(),
|
||||
description: m.description.clone(),
|
||||
items,
|
||||
version: m.version,
|
||||
});
|
||||
resps.push(dict_model_to_resp(m, items));
|
||||
}
|
||||
|
||||
Ok((resps, total))
|
||||
@@ -76,14 +69,7 @@ impl DictionaryService {
|
||||
|
||||
let items = Self::fetch_items(model.id, tenant_id, db).await?;
|
||||
|
||||
Ok(DictionaryResp {
|
||||
id: model.id,
|
||||
name: model.name.clone(),
|
||||
code: model.code.clone(),
|
||||
description: model.description.clone(),
|
||||
items,
|
||||
version: model.version,
|
||||
})
|
||||
Ok(dict_model_to_resp(&model, items))
|
||||
}
|
||||
|
||||
/// Create a new dictionary within the current tenant.
|
||||
@@ -217,14 +203,7 @@ impl DictionaryService {
|
||||
)
|
||||
.await;
|
||||
|
||||
Ok(DictionaryResp {
|
||||
id: updated.id,
|
||||
name: updated.name.clone(),
|
||||
code: updated.code.clone(),
|
||||
description: updated.description.clone(),
|
||||
items,
|
||||
version: updated.version,
|
||||
})
|
||||
Ok(dict_model_to_resp(&updated, items))
|
||||
}
|
||||
|
||||
/// Soft-delete a dictionary by setting the `deleted_at` timestamp.
|
||||
@@ -415,15 +394,7 @@ impl DictionaryService {
|
||||
)
|
||||
.await;
|
||||
|
||||
Ok(DictionaryItemResp {
|
||||
id: updated.id,
|
||||
dictionary_id: updated.dictionary_id,
|
||||
label: updated.label.clone(),
|
||||
value: updated.value.clone(),
|
||||
sort_order: updated.sort_order,
|
||||
color: updated.color.clone(),
|
||||
version: updated.version,
|
||||
})
|
||||
Ok(item_model_to_resp(&updated))
|
||||
}
|
||||
|
||||
/// Soft-delete a dictionary item by setting the `deleted_at` timestamp.
|
||||
@@ -506,17 +477,152 @@ impl DictionaryService {
|
||||
.await
|
||||
.map_err(|e| ConfigError::Validation(e.to_string()))?;
|
||||
|
||||
Ok(items
|
||||
.iter()
|
||||
.map(|i| DictionaryItemResp {
|
||||
id: i.id,
|
||||
dictionary_id: i.dictionary_id,
|
||||
label: i.label.clone(),
|
||||
value: i.value.clone(),
|
||||
sort_order: i.sort_order,
|
||||
color: i.color.clone(),
|
||||
version: i.version,
|
||||
})
|
||||
.collect())
|
||||
Ok(items.iter().map(item_model_to_resp).collect())
|
||||
}
|
||||
}
|
||||
|
||||
/// Free function wrapping the private helper so the mapping logic is reusable
|
||||
/// in both async methods and synchronous unit tests without a database.
|
||||
fn item_model_to_resp(m: &dictionary_item::Model) -> DictionaryItemResp {
|
||||
DictionaryItemResp {
|
||||
id: m.id,
|
||||
dictionary_id: m.dictionary_id,
|
||||
label: m.label.clone(),
|
||||
value: m.value.clone(),
|
||||
sort_order: m.sort_order,
|
||||
color: m.color.clone(),
|
||||
version: m.version,
|
||||
}
|
||||
}
|
||||
|
||||
/// Free function for dictionary model -> response DTO mapping.
|
||||
fn dict_model_to_resp(m: &dictionary::Model, items: Vec<DictionaryItemResp>) -> DictionaryResp {
|
||||
DictionaryResp {
|
||||
id: m.id,
|
||||
name: m.name.clone(),
|
||||
code: m.code.clone(),
|
||||
description: m.description.clone(),
|
||||
items,
|
||||
version: m.version,
|
||||
}
|
||||
}
|
||||
|
||||
#[cfg(test)]
|
||||
mod tests {
|
||||
use super::*;
|
||||
use chrono::Utc;
|
||||
use uuid::Uuid;
|
||||
|
||||
fn sample_dict_model() -> dictionary::Model {
|
||||
dictionary::Model {
|
||||
id: Uuid::now_v7(),
|
||||
tenant_id: Uuid::now_v7(),
|
||||
name: "测试字典".to_string(),
|
||||
code: "test_dict".to_string(),
|
||||
description: Some("描述".to_string()),
|
||||
created_at: Utc::now(),
|
||||
updated_at: Utc::now(),
|
||||
created_by: Uuid::now_v7(),
|
||||
updated_by: Uuid::now_v7(),
|
||||
deleted_at: None,
|
||||
version: 1,
|
||||
}
|
||||
}
|
||||
|
||||
fn sample_item_model() -> dictionary_item::Model {
|
||||
dictionary_item::Model {
|
||||
id: Uuid::now_v7(),
|
||||
tenant_id: Uuid::now_v7(),
|
||||
dictionary_id: Uuid::now_v7(),
|
||||
label: "选项A".to_string(),
|
||||
value: "option_a".to_string(),
|
||||
sort_order: 1,
|
||||
color: Some("#FF0000".to_string()),
|
||||
created_at: Utc::now(),
|
||||
updated_at: Utc::now(),
|
||||
created_by: Uuid::now_v7(),
|
||||
updated_by: Uuid::now_v7(),
|
||||
deleted_at: None,
|
||||
version: 1,
|
||||
}
|
||||
}
|
||||
|
||||
// ---- dict_model_to_resp ----
|
||||
|
||||
#[test]
|
||||
fn dict_model_to_resp_with_items() {
|
||||
let m = sample_dict_model();
|
||||
let item = item_model_to_resp(&sample_item_model());
|
||||
let resp = dict_model_to_resp(&m, vec![item]);
|
||||
|
||||
assert_eq!(resp.id, m.id);
|
||||
assert_eq!(resp.name, "测试字典");
|
||||
assert_eq!(resp.code, "test_dict");
|
||||
assert_eq!(resp.description, Some("描述".to_string()));
|
||||
assert_eq!(resp.version, 1);
|
||||
assert_eq!(resp.items.len(), 1);
|
||||
assert_eq!(resp.items[0].label, "选项A");
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn dict_model_to_resp_without_description() {
|
||||
let mut m = sample_dict_model();
|
||||
m.description = None;
|
||||
let resp = dict_model_to_resp(&m, vec![]);
|
||||
|
||||
assert_eq!(resp.description, None);
|
||||
assert!(resp.items.is_empty());
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn dict_model_to_resp_preserves_version() {
|
||||
let mut m = sample_dict_model();
|
||||
m.version = 42;
|
||||
let resp = dict_model_to_resp(&m, vec![]);
|
||||
|
||||
assert_eq!(resp.version, 42);
|
||||
}
|
||||
|
||||
// ---- item_model_to_resp ----
|
||||
|
||||
#[test]
|
||||
fn item_model_to_resp_all_fields() {
|
||||
let m = sample_item_model();
|
||||
let resp = item_model_to_resp(&m);
|
||||
|
||||
assert_eq!(resp.id, m.id);
|
||||
assert_eq!(resp.dictionary_id, m.dictionary_id);
|
||||
assert_eq!(resp.label, "选项A");
|
||||
assert_eq!(resp.value, "option_a");
|
||||
assert_eq!(resp.sort_order, 1);
|
||||
assert_eq!(resp.color, Some("#FF0000".to_string()));
|
||||
assert_eq!(resp.version, 1);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn item_model_to_resp_without_color() {
|
||||
let mut m = sample_item_model();
|
||||
m.color = None;
|
||||
let resp = item_model_to_resp(&m);
|
||||
|
||||
assert_eq!(resp.color, None);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn item_model_to_resp_default_sort_order() {
|
||||
let mut m = sample_item_model();
|
||||
m.sort_order = 0;
|
||||
let resp = item_model_to_resp(&m);
|
||||
|
||||
assert_eq!(resp.sort_order, 0);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn item_model_to_resp_preserves_version() {
|
||||
let mut m = sample_item_model();
|
||||
m.version = 7;
|
||||
let resp = item_model_to_resp(&m);
|
||||
|
||||
assert_eq!(resp.version, 7);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -14,6 +14,41 @@ use erp_core::error::check_version;
|
||||
use erp_core::events::EventBus;
|
||||
use erp_core::types::Pagination;
|
||||
|
||||
/// 格式化编号字符串。
|
||||
///
|
||||
/// 拼接规则:
|
||||
/// 1. 以 `prefix` 开头
|
||||
/// 2. 若 `prefix` 非空,追加 `separator`
|
||||
/// 3. 若 `date_part` 为 `Some` 且非空,追加 `date_part` + `separator`
|
||||
/// 4. 追加零填充的 `seq_current`(填充到 `seq_length` 位,最少 1 位)
|
||||
pub(crate) fn format_number(
|
||||
prefix: &str,
|
||||
separator: &str,
|
||||
date_part: Option<&str>,
|
||||
seq_current: i64,
|
||||
seq_length: i32,
|
||||
) -> String {
|
||||
let mut result = String::with_capacity(32);
|
||||
result.push_str(prefix);
|
||||
|
||||
if !prefix.is_empty() {
|
||||
result.push_str(separator);
|
||||
}
|
||||
|
||||
if let Some(dp) = date_part {
|
||||
if !dp.is_empty() {
|
||||
result.push_str(dp);
|
||||
result.push_str(separator);
|
||||
}
|
||||
}
|
||||
|
||||
let width = (seq_length.max(1)) as usize;
|
||||
let seq_padded = format!("{:0>width$}", seq_current, width = width);
|
||||
result.push_str(&seq_padded);
|
||||
|
||||
result
|
||||
}
|
||||
|
||||
/// 编号规则 CRUD 服务 -- 创建、查询、更新、软删除编号规则,
|
||||
/// 以及线程安全地生成编号序列。
|
||||
pub struct NumberingService;
|
||||
@@ -363,20 +398,15 @@ impl NumberingService {
|
||||
.map_err(|e| ConfigError::Validation(e.to_string()))?;
|
||||
|
||||
// 拼接编号字符串: {prefix}{separator}{date_part}{separator}{seq_padded}
|
||||
let separator = &rule.separator;
|
||||
let mut parts = vec![rule.prefix.clone()];
|
||||
let date_part = rule.date_format.as_ref().map(|fmt| Utc::now().format(fmt).to_string());
|
||||
|
||||
// 日期部分(如果配置了 date_format)
|
||||
if let Some(date_fmt) = &rule.date_format {
|
||||
let date_part = Utc::now().format(date_fmt).to_string();
|
||||
parts.push(date_part);
|
||||
}
|
||||
|
||||
// 序列号补零
|
||||
let seq_padded = format!("{:0>width$}", seq_current, width = rule.seq_length as usize);
|
||||
parts.push(seq_padded);
|
||||
|
||||
let number = parts.join(separator);
|
||||
let number = format_number(
|
||||
&rule.prefix,
|
||||
&rule.separator,
|
||||
date_part.as_deref(),
|
||||
seq_current,
|
||||
rule.seq_length,
|
||||
);
|
||||
|
||||
Ok(number)
|
||||
}
|
||||
@@ -672,4 +702,41 @@ mod tests {
|
||||
assert_eq!(resp.last_reset_date, None);
|
||||
assert_eq!(resp.prefix, "");
|
||||
}
|
||||
|
||||
// ---- format_number 测试 ----
|
||||
|
||||
#[test]
|
||||
fn format_basic_prefix_no_date() {
|
||||
// 基础:前缀 + 序列号
|
||||
let result = format_number("ORD", "/", None, 1, 5);
|
||||
assert_eq!(result, "ORD/00001");
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn format_with_date_part() {
|
||||
// 前缀 + 日期 + 序列号
|
||||
let result = format_number("INV", "-", Some("20260430"), 42, 4);
|
||||
assert_eq!(result, "INV-20260430-0042");
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn format_no_prefix() {
|
||||
// 无前缀,直接输出序列号
|
||||
let result = format_number("", "/", None, 7, 3);
|
||||
assert_eq!(result, "007");
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn format_no_prefix_no_date() {
|
||||
// 无前缀无日期,仅序列号
|
||||
let result = format_number("", "-", None, 99, 6);
|
||||
assert_eq!(result, "000099");
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn format_seq_length_zero_pads_to_one() {
|
||||
// seq_length=0 时仍至少填充 1 位
|
||||
let result = format_number("", "", None, 5, 0);
|
||||
assert_eq!(result, "5");
|
||||
}
|
||||
}
|
||||
|
||||
@@ -144,6 +144,21 @@ impl ReviewArticleReq {
|
||||
}
|
||||
}
|
||||
|
||||
// ---------------------------------------------------------------------------
|
||||
// 修订历史 DTOs
|
||||
// ---------------------------------------------------------------------------
|
||||
|
||||
#[derive(Debug, Clone, Serialize, Deserialize, ToSchema)]
|
||||
pub struct ArticleRevisionResp {
|
||||
pub id: Uuid,
|
||||
pub article_id: Uuid,
|
||||
pub revision_number: i32,
|
||||
pub title: String,
|
||||
pub summary: Option<String>,
|
||||
pub created_by: Option<Uuid>,
|
||||
pub created_at: chrono::DateTime<chrono::Utc>,
|
||||
}
|
||||
|
||||
// ---------------------------------------------------------------------------
|
||||
// 分类 DTOs
|
||||
// ---------------------------------------------------------------------------
|
||||
|
||||
@@ -203,3 +203,29 @@ where
|
||||
article_service::increment_view_count(&state, ctx.tenant_id, id).await?;
|
||||
Ok(Json(ApiResponse::ok(())))
|
||||
}
|
||||
|
||||
#[derive(Debug, serde::Deserialize)]
|
||||
pub struct ListRevisionsQuery {
|
||||
pub page: Option<u64>,
|
||||
pub page_size: Option<u64>,
|
||||
}
|
||||
|
||||
/// 查询文章修订历史
|
||||
pub async fn list_revisions<S>(
|
||||
State(state): State<HealthState>,
|
||||
Extension(ctx): Extension<TenantContext>,
|
||||
Path(id): Path<uuid::Uuid>,
|
||||
Query(params): Query<ListRevisionsQuery>,
|
||||
) -> Result<Json<ApiResponse<PaginatedResponse<crate::dto::article_dto::ArticleRevisionResp>>>, AppError>
|
||||
where
|
||||
HealthState: FromRef<S>,
|
||||
S: Clone + Send + Sync + 'static,
|
||||
{
|
||||
require_permission(&ctx, "health.articles.list")?;
|
||||
let page = params.page.unwrap_or(1);
|
||||
let page_size = params.page_size.unwrap_or(20);
|
||||
let result = article_service::list_revisions(
|
||||
&state, ctx.tenant_id, id, page, page_size,
|
||||
).await?;
|
||||
Ok(Json(ApiResponse::ok(result)))
|
||||
}
|
||||
|
||||
@@ -433,6 +433,10 @@ impl HealthModule {
|
||||
"/health/articles/{id}/view",
|
||||
axum::routing::post(article_handler::view_article),
|
||||
)
|
||||
.route(
|
||||
"/health/articles/{id}/revisions",
|
||||
axum::routing::get(article_handler::list_revisions),
|
||||
)
|
||||
// 资讯分类
|
||||
.route(
|
||||
"/health/article-categories",
|
||||
|
||||
@@ -588,3 +588,54 @@ async fn save_revision(
|
||||
active.insert(&state.db).await?;
|
||||
Ok(())
|
||||
}
|
||||
|
||||
pub async fn list_revisions(
|
||||
state: &HealthState,
|
||||
tenant_id: Uuid,
|
||||
article_id: Uuid,
|
||||
page: u64,
|
||||
page_size: u64,
|
||||
) -> HealthResult<PaginatedResponse<crate::dto::article_dto::ArticleRevisionResp>> {
|
||||
use crate::entity::article_revision;
|
||||
|
||||
let page = page.max(1);
|
||||
let page_size = page_size.clamp(1, 100);
|
||||
|
||||
let condition = article_revision::Column::ArticleId
|
||||
.eq(article_id)
|
||||
.and(article_revision::Column::TenantId.eq(tenant_id));
|
||||
|
||||
let total = article_revision::Entity::find()
|
||||
.filter(condition.clone())
|
||||
.count(&state.db)
|
||||
.await?;
|
||||
|
||||
let items = article_revision::Entity::find()
|
||||
.filter(condition)
|
||||
.order_by_desc(article_revision::Column::RevisionNumber)
|
||||
.offset(((page - 1) * page_size) as u64)
|
||||
.limit(page_size)
|
||||
.all(&state.db)
|
||||
.await?;
|
||||
|
||||
let data = items
|
||||
.into_iter()
|
||||
.map(|m| crate::dto::article_dto::ArticleRevisionResp {
|
||||
id: m.id,
|
||||
article_id: m.article_id,
|
||||
revision_number: m.revision_number,
|
||||
title: m.title,
|
||||
summary: m.summary,
|
||||
created_by: m.created_by,
|
||||
created_at: m.created_at,
|
||||
})
|
||||
.collect();
|
||||
|
||||
Ok(PaginatedResponse {
|
||||
data,
|
||||
total,
|
||||
page,
|
||||
page_size,
|
||||
total_pages: ((total as f64) / (page_size as f64)).ceil() as u64,
|
||||
})
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user