Compare commits
4 Commits
ade8497c2d
...
fa9278590d
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
fa9278590d | ||
|
|
e00c2abdcd | ||
|
|
147fd886e3 | ||
|
|
96c9a8ada9 |
44
Cargo.lock
generated
44
Cargo.lock
generated
@@ -1465,6 +1465,26 @@ dependencies = [
|
||||
"uuid",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
name = "erp-dialysis"
|
||||
version = "0.1.0"
|
||||
dependencies = [
|
||||
"async-trait",
|
||||
"axum",
|
||||
"chrono",
|
||||
"erp-core",
|
||||
"num-traits",
|
||||
"sea-orm",
|
||||
"serde",
|
||||
"serde_json",
|
||||
"thiserror 2.0.18",
|
||||
"tokio",
|
||||
"tracing",
|
||||
"utoipa",
|
||||
"uuid",
|
||||
"validator",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
name = "erp-health"
|
||||
version = "0.1.0"
|
||||
@@ -1488,6 +1508,7 @@ dependencies = [
|
||||
"utoipa",
|
||||
"uuid",
|
||||
"validator",
|
||||
"zeroize",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
@@ -1541,6 +1562,15 @@ dependencies = [
|
||||
"wasmtime-wasi",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
name = "erp-plugin-assessment"
|
||||
version = "0.1.0"
|
||||
dependencies = [
|
||||
"serde",
|
||||
"serde_json",
|
||||
"wit-bindgen 0.55.0",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
name = "erp-plugin-crm"
|
||||
version = "0.1.0"
|
||||
@@ -6733,6 +6763,20 @@ name = "zeroize"
|
||||
version = "1.8.2"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "b97154e67e32c85465826e8bcc1c59429aaaf107c1e4a9e53c8d8ccd5eff88d0"
|
||||
dependencies = [
|
||||
"zeroize_derive",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
name = "zeroize_derive"
|
||||
version = "1.4.3"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "85a5b4158499876c763cb03bc4e49185d3cccbabb15b33c627f7884f43db852e"
|
||||
dependencies = [
|
||||
"proc-macro2",
|
||||
"quote",
|
||||
"syn 2.0.117",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
name = "zerotrie"
|
||||
|
||||
@@ -17,6 +17,8 @@ members = [
|
||||
"crates/erp-plugin-itops",
|
||||
"crates/erp-health",
|
||||
"crates/erp-ai",
|
||||
"crates/erp-plugin-assessment",
|
||||
"crates/erp-dialysis",
|
||||
]
|
||||
|
||||
[workspace.package]
|
||||
|
||||
@@ -72,10 +72,10 @@ export function AdminDashboard() {
|
||||
<Tabs
|
||||
defaultActiveKey="dialysis"
|
||||
items={[
|
||||
{ key: 'dialysis', label: '透析管理', children: <HealthDataCenter data={healthDataStats} /> },
|
||||
{ key: 'lab', label: '化验报告', children: <HealthDataCenter data={healthDataStats} /> },
|
||||
{ key: 'appointments', label: '预约分析', children: <HealthDataCenter data={healthDataStats} /> },
|
||||
{ key: 'vital-signs', label: '体征数据', children: <HealthDataCenter data={healthDataStats} /> },
|
||||
{ key: 'dialysis', label: '透析管理', children: <HealthDataCenter data={healthDataStats} tab="dialysis" /> },
|
||||
{ key: 'lab', label: '化验报告', children: <HealthDataCenter data={healthDataStats} tab="lab" /> },
|
||||
{ key: 'appointments', label: '预约分析', children: <HealthDataCenter data={healthDataStats} tab="appointments" /> },
|
||||
{ key: 'vital-signs', label: '体征数据', children: <HealthDataCenter data={healthDataStats} tab="vital-signs" /> },
|
||||
]}
|
||||
/>
|
||||
</Card>
|
||||
|
||||
@@ -1,98 +1,124 @@
|
||||
import { Row, Col, Card, Statistic, Tag, Typography } from 'antd';
|
||||
import { Row, Col, Card, Statistic, Tag, Typography, Empty } from 'antd';
|
||||
import type { HealthDataStats } from '../../../api/health/points';
|
||||
|
||||
const { Text } = Typography;
|
||||
|
||||
interface HealthDataCenterProps {
|
||||
data: HealthDataStats | null;
|
||||
tab?: string;
|
||||
}
|
||||
|
||||
export default function HealthDataCenter({ data }: HealthDataCenterProps) {
|
||||
function DialysisPanel({ data }: { data: HealthDataStats | null }) {
|
||||
return (
|
||||
<Card type="inner" title={<span style={{ fontSize: 14, fontWeight: 600 }}>透析记录</span>} style={{ borderRadius: 8 }}>
|
||||
<Row gutter={[12, 12]}>
|
||||
<Col span={8}><Statistic title="总记录" value={data?.dialysis.total_records ?? 0} valueStyle={{ fontSize: 20 }} /></Col>
|
||||
<Col span={8}><Statistic title="本月新增" value={data?.dialysis.this_month ?? 0} valueStyle={{ fontSize: 20, color: '#2563eb' }} /></Col>
|
||||
<Col span={8}><Statistic title="待审核" value={data?.dialysis.pending_review ?? 0} valueStyle={{ fontSize: 20, color: '#d97706' }} /></Col>
|
||||
</Row>
|
||||
<Row gutter={[12, 12]} style={{ marginTop: 12 }}>
|
||||
<Col span={8}><Statistic title="并发症率" value={data?.dialysis.complication_rate ?? 0} suffix="%" precision={1} valueStyle={{ fontSize: 18 }} /></Col>
|
||||
<Col span={8}><Statistic title="平均超滤(ml)" value={data?.dialysis.avg_ultrafiltration ?? 0} precision={0} valueStyle={{ fontSize: 18 }} /></Col>
|
||||
<Col span={8}><Statistic title="平均时长(分)" value={data?.dialysis.avg_duration ?? 0} precision={0} valueStyle={{ fontSize: 18 }} /></Col>
|
||||
</Row>
|
||||
{(data?.dialysis.type_distribution ?? []).length > 0 && (
|
||||
<div style={{ marginTop: 12 }}>
|
||||
<Text type="secondary" style={{ fontSize: 12 }}>类型分布: </Text>
|
||||
{data!.dialysis.type_distribution.map((item) => (
|
||||
<Tag key={item.name} color="blue" style={{ marginTop: 4 }}>{item.name}: {item.value}</Tag>
|
||||
))}
|
||||
</div>
|
||||
)}
|
||||
</Card>
|
||||
);
|
||||
}
|
||||
|
||||
function LabPanel({ data }: { data: HealthDataStats | null }) {
|
||||
return (
|
||||
<Card type="inner" title={<span style={{ fontSize: 14, fontWeight: 600 }}>化验报告</span>} style={{ borderRadius: 8 }}>
|
||||
<Row gutter={[12, 12]}>
|
||||
<Col span={8}><Statistic title="总报告" value={data?.lab_reports.total_reports ?? 0} valueStyle={{ fontSize: 20 }} /></Col>
|
||||
<Col span={8}><Statistic title="本月新增" value={data?.lab_reports.this_month ?? 0} valueStyle={{ fontSize: 20, color: '#2563eb' }} /></Col>
|
||||
<Col span={8}><Statistic title="异常项" value={data?.lab_reports.abnormal_items ?? 0} valueStyle={{ fontSize: 20, color: '#dc2626' }} /></Col>
|
||||
</Row>
|
||||
<Row gutter={[12, 12]} style={{ marginTop: 12 }}>
|
||||
<Col span={8}><Statistic title="待审核" value={data?.lab_reports.pending_review ?? 0} valueStyle={{ fontSize: 18, color: '#d97706' }} /></Col>
|
||||
<Col span={8}><Statistic title="已审核" value={data?.lab_reports.reviewed ?? 0} valueStyle={{ fontSize: 18, color: '#059669' }} /></Col>
|
||||
</Row>
|
||||
{(data?.lab_reports.type_distribution ?? []).length > 0 && (
|
||||
<div style={{ marginTop: 12 }}>
|
||||
<Text type="secondary" style={{ fontSize: 12 }}>类型分布: </Text>
|
||||
{data!.lab_reports.type_distribution.map((item) => (
|
||||
<Tag key={item.name} color="green" style={{ marginTop: 4 }}>{item.name}: {item.value}</Tag>
|
||||
))}
|
||||
</div>
|
||||
)}
|
||||
</Card>
|
||||
);
|
||||
}
|
||||
|
||||
function AppointmentsPanel({ data }: { data: HealthDataStats | null }) {
|
||||
return (
|
||||
<Card type="inner" title={<span style={{ fontSize: 14, fontWeight: 600 }}>预约统计</span>} style={{ borderRadius: 8 }}>
|
||||
<Row gutter={[12, 12]}>
|
||||
<Col span={8}><Statistic title="总预约" value={data?.appointments.total_appointments ?? 0} valueStyle={{ fontSize: 20 }} /></Col>
|
||||
<Col span={8}><Statistic title="本月" value={data?.appointments.this_month ?? 0} valueStyle={{ fontSize: 20, color: '#2563eb' }} /></Col>
|
||||
<Col span={8}><Statistic title="取消率" value={data?.appointments.cancel_rate ?? 0} suffix="%" precision={1} valueStyle={{ fontSize: 20, color: '#dc2626' }} /></Col>
|
||||
</Row>
|
||||
{(data?.appointments.status_distribution ?? []).length > 0 && (
|
||||
<div style={{ marginTop: 12 }}>
|
||||
<Text type="secondary" style={{ fontSize: 12 }}>状态: </Text>
|
||||
{data!.appointments.status_distribution.map((item) => (
|
||||
<Tag key={item.name} color="purple" style={{ marginTop: 4 }}>{item.name}: {item.value}</Tag>
|
||||
))}
|
||||
</div>
|
||||
)}
|
||||
</Card>
|
||||
);
|
||||
}
|
||||
|
||||
function VitalSignsPanel({ data }: { data: HealthDataStats | null }) {
|
||||
return (
|
||||
<Card type="inner" title={<span style={{ fontSize: 14, fontWeight: 600 }}>体征上报率</span>} style={{ borderRadius: 8 }}>
|
||||
<Row gutter={[12, 12]}>
|
||||
<Col span={8}><Statistic title="总患者" value={data?.vital_signs_report_rate.total_patients ?? 0} valueStyle={{ fontSize: 20 }} /></Col>
|
||||
<Col span={8}><Statistic title="本月上报" value={data?.vital_signs_report_rate.reported_patients ?? 0} valueStyle={{ fontSize: 20, color: '#059669' }} /></Col>
|
||||
<Col span={8}><Statistic title="上报率" value={data?.vital_signs_report_rate.report_rate ?? 0} suffix="%" precision={1} valueStyle={{ fontSize: 20, color: '#7c3aed' }} /></Col>
|
||||
</Row>
|
||||
{(data?.vital_signs_report_rate.daily_trend ?? []).length > 0 && (
|
||||
<div style={{ marginTop: 12 }}>
|
||||
<Text type="secondary" style={{ fontSize: 12 }}>近 7 天: </Text>
|
||||
<div style={{ display: 'flex', gap: 6, marginTop: 4, flexWrap: 'wrap' }}>
|
||||
{data!.vital_signs_report_rate.daily_trend.map((d) => (
|
||||
<Tag key={d.date} color={d.rate >= 50 ? 'green' : d.rate >= 20 ? 'orange' : 'red'}>
|
||||
{d.date.slice(5)} {d.reported}/{d.total}
|
||||
</Tag>
|
||||
))}
|
||||
</div>
|
||||
</div>
|
||||
)}
|
||||
</Card>
|
||||
);
|
||||
}
|
||||
|
||||
const TAB_PANELS: Record<string, React.FC<{ data: HealthDataStats | null }>> = {
|
||||
dialysis: DialysisPanel,
|
||||
lab: LabPanel,
|
||||
appointments: AppointmentsPanel,
|
||||
'vital-signs': VitalSignsPanel,
|
||||
};
|
||||
|
||||
export default function HealthDataCenter({ data, tab = 'dialysis' }: HealthDataCenterProps) {
|
||||
const Panel = TAB_PANELS[tab];
|
||||
|
||||
if (!Panel) {
|
||||
return <Empty description="未知数据面板" />;
|
||||
}
|
||||
|
||||
return (
|
||||
<Row gutter={[16, 16]}>
|
||||
<Col xs={24} md={12}>
|
||||
<Card type="inner" title={<span style={{ fontSize: 14, fontWeight: 600 }}>透析记录</span>} style={{ borderRadius: 8 }}>
|
||||
<Row gutter={[12, 12]}>
|
||||
<Col span={8}><Statistic title="总记录" value={data?.dialysis.total_records ?? 0} valueStyle={{ fontSize: 20 }} /></Col>
|
||||
<Col span={8}><Statistic title="本月新增" value={data?.dialysis.this_month ?? 0} valueStyle={{ fontSize: 20, color: '#2563eb' }} /></Col>
|
||||
<Col span={8}><Statistic title="待审核" value={data?.dialysis.pending_review ?? 0} valueStyle={{ fontSize: 20, color: '#d97706' }} /></Col>
|
||||
</Row>
|
||||
<Row gutter={[12, 12]} style={{ marginTop: 12 }}>
|
||||
<Col span={8}><Statistic title="并发症率" value={data?.dialysis.complication_rate ?? 0} suffix="%" precision={1} valueStyle={{ fontSize: 18 }} /></Col>
|
||||
<Col span={8}><Statistic title="平均超滤(ml)" value={data?.dialysis.avg_ultrafiltration ?? 0} precision={0} valueStyle={{ fontSize: 18 }} /></Col>
|
||||
<Col span={8}><Statistic title="平均时长(分)" value={data?.dialysis.avg_duration ?? 0} precision={0} valueStyle={{ fontSize: 18 }} /></Col>
|
||||
</Row>
|
||||
{(data?.dialysis.type_distribution ?? []).length > 0 && (
|
||||
<div style={{ marginTop: 12 }}>
|
||||
<Text type="secondary" style={{ fontSize: 12 }}>类型分布: </Text>
|
||||
{data!.dialysis.type_distribution.map((item) => (
|
||||
<Tag key={item.name} color="blue" style={{ marginTop: 4 }}>{item.name}: {item.value}</Tag>
|
||||
))}
|
||||
</div>
|
||||
)}
|
||||
</Card>
|
||||
</Col>
|
||||
|
||||
<Col xs={24} md={12}>
|
||||
<Card type="inner" title={<span style={{ fontSize: 14, fontWeight: 600 }}>化验报告</span>} style={{ borderRadius: 8 }}>
|
||||
<Row gutter={[12, 12]}>
|
||||
<Col span={8}><Statistic title="总报告" value={data?.lab_reports.total_reports ?? 0} valueStyle={{ fontSize: 20 }} /></Col>
|
||||
<Col span={8}><Statistic title="本月新增" value={data?.lab_reports.this_month ?? 0} valueStyle={{ fontSize: 20, color: '#2563eb' }} /></Col>
|
||||
<Col span={8}><Statistic title="异常项" value={data?.lab_reports.abnormal_items ?? 0} valueStyle={{ fontSize: 20, color: '#dc2626' }} /></Col>
|
||||
</Row>
|
||||
<Row gutter={[12, 12]} style={{ marginTop: 12 }}>
|
||||
<Col span={12}><Statistic title="待审核" value={data?.lab_reports.pending_review ?? 0} valueStyle={{ fontSize: 18, color: '#d97706' }} /></Col>
|
||||
<Col span={12}><Statistic title="已审核" value={data?.lab_reports.reviewed ?? 0} valueStyle={{ fontSize: 18, color: '#059669' }} /></Col>
|
||||
</Row>
|
||||
{(data?.lab_reports.type_distribution ?? []).length > 0 && (
|
||||
<div style={{ marginTop: 12 }}>
|
||||
<Text type="secondary" style={{ fontSize: 12 }}>类型分布: </Text>
|
||||
{data!.lab_reports.type_distribution.map((item) => (
|
||||
<Tag key={item.name} color="green" style={{ marginTop: 4 }}>{item.name}: {item.value}</Tag>
|
||||
))}
|
||||
</div>
|
||||
)}
|
||||
</Card>
|
||||
</Col>
|
||||
|
||||
<Col xs={24} md={12}>
|
||||
<Card type="inner" title={<span style={{ fontSize: 14, fontWeight: 600 }}>预约统计</span>} style={{ borderRadius: 8 }}>
|
||||
<Row gutter={[12, 12]}>
|
||||
<Col span={8}><Statistic title="总预约" value={data?.appointments.total_appointments ?? 0} valueStyle={{ fontSize: 20 }} /></Col>
|
||||
<Col span={8}><Statistic title="本月" value={data?.appointments.this_month ?? 0} valueStyle={{ fontSize: 20, color: '#2563eb' }} /></Col>
|
||||
<Col span={8}><Statistic title="取消率" value={data?.appointments.cancel_rate ?? 0} suffix="%" precision={1} valueStyle={{ fontSize: 20, color: '#dc2626' }} /></Col>
|
||||
</Row>
|
||||
{(data?.appointments.status_distribution ?? []).length > 0 && (
|
||||
<div style={{ marginTop: 12 }}>
|
||||
<Text type="secondary" style={{ fontSize: 12 }}>状态: </Text>
|
||||
{data!.appointments.status_distribution.map((item) => (
|
||||
<Tag key={item.name} color="purple" style={{ marginTop: 4 }}>{item.name}: {item.value}</Tag>
|
||||
))}
|
||||
</div>
|
||||
)}
|
||||
</Card>
|
||||
</Col>
|
||||
|
||||
<Col xs={24} md={12}>
|
||||
<Card type="inner" title={<span style={{ fontSize: 14, fontWeight: 600 }}>体征上报率</span>} style={{ borderRadius: 8 }}>
|
||||
<Row gutter={[12, 12]}>
|
||||
<Col span={8}><Statistic title="总患者" value={data?.vital_signs_report_rate.total_patients ?? 0} valueStyle={{ fontSize: 20 }} /></Col>
|
||||
<Col span={8}><Statistic title="本月上报" value={data?.vital_signs_report_rate.reported_patients ?? 0} valueStyle={{ fontSize: 20, color: '#059669' }} /></Col>
|
||||
<Col span={8}><Statistic title="上报率" value={data?.vital_signs_report_rate.report_rate ?? 0} suffix="%" precision={1} valueStyle={{ fontSize: 20, color: '#7c3aed' }} /></Col>
|
||||
</Row>
|
||||
{(data?.vital_signs_report_rate.daily_trend ?? []).length > 0 && (
|
||||
<div style={{ marginTop: 12 }}>
|
||||
<Text type="secondary" style={{ fontSize: 12 }}>近 7 天: </Text>
|
||||
<div style={{ display: 'flex', gap: 6, marginTop: 4, flexWrap: 'wrap' }}>
|
||||
{data!.vital_signs_report_rate.daily_trend.map((d) => (
|
||||
<Tag key={d.date} color={d.rate >= 50 ? 'green' : d.rate >= 20 ? 'orange' : 'red'}>
|
||||
{d.date.slice(5)} {d.reported}/{d.total}
|
||||
</Tag>
|
||||
))}
|
||||
</div>
|
||||
</div>
|
||||
)}
|
||||
</Card>
|
||||
<Col span={24}>
|
||||
<Panel data={data} />
|
||||
</Col>
|
||||
</Row>
|
||||
);
|
||||
|
||||
20
crates/erp-dialysis/Cargo.toml
Normal file
20
crates/erp-dialysis/Cargo.toml
Normal file
@@ -0,0 +1,20 @@
|
||||
[package]
|
||||
name = "erp-dialysis"
|
||||
version.workspace = true
|
||||
edition.workspace = true
|
||||
|
||||
[dependencies]
|
||||
erp-core.workspace = true
|
||||
sea-orm.workspace = true
|
||||
tokio.workspace = true
|
||||
serde = { workspace = true, features = ["derive"] }
|
||||
serde_json.workspace = true
|
||||
uuid.workspace = true
|
||||
chrono.workspace = true
|
||||
axum.workspace = true
|
||||
utoipa.workspace = true
|
||||
validator.workspace = true
|
||||
async-trait.workspace = true
|
||||
tracing.workspace = true
|
||||
thiserror.workspace = true
|
||||
num-traits = "0.2"
|
||||
7
crates/erp-dialysis/src/dto/mod.rs
Normal file
7
crates/erp-dialysis/src/dto/mod.rs
Normal file
@@ -0,0 +1,7 @@
|
||||
pub mod dialysis_dto;
|
||||
pub mod dialysis_prescription_dto;
|
||||
|
||||
#[derive(Debug, serde::Deserialize, utoipa::ToSchema)]
|
||||
pub struct DeleteWithVersion {
|
||||
pub version: i32,
|
||||
}
|
||||
@@ -59,19 +59,6 @@ pub struct Model {
|
||||
}
|
||||
|
||||
#[derive(Copy, Clone, Debug, EnumIter, DeriveRelation)]
|
||||
pub enum Relation {
|
||||
#[sea_orm(
|
||||
belongs_to = "super::patient::Entity",
|
||||
from = "Column::PatientId",
|
||||
to = "super::patient::Column::Id"
|
||||
)]
|
||||
Patient,
|
||||
}
|
||||
|
||||
impl Related<super::patient::Entity> for Entity {
|
||||
fn to() -> RelationDef {
|
||||
Relation::Patient.def()
|
||||
}
|
||||
}
|
||||
pub enum Relation {}
|
||||
|
||||
impl ActiveModelBehavior for ActiveModel {}
|
||||
@@ -63,19 +63,6 @@ pub struct Model {
|
||||
}
|
||||
|
||||
#[derive(Copy, Clone, Debug, EnumIter, DeriveRelation)]
|
||||
pub enum Relation {
|
||||
#[sea_orm(
|
||||
belongs_to = "super::patient::Entity",
|
||||
from = "Column::PatientId",
|
||||
to = "super::patient::Column::Id"
|
||||
)]
|
||||
Patient,
|
||||
}
|
||||
|
||||
impl Related<super::patient::Entity> for Entity {
|
||||
fn to() -> RelationDef {
|
||||
Relation::Patient.def()
|
||||
}
|
||||
}
|
||||
pub enum Relation {}
|
||||
|
||||
impl ActiveModelBehavior for ActiveModel {}
|
||||
2
crates/erp-dialysis/src/entity/mod.rs
Normal file
2
crates/erp-dialysis/src/entity/mod.rs
Normal file
@@ -0,0 +1,2 @@
|
||||
pub mod dialysis_prescription;
|
||||
pub mod dialysis_record;
|
||||
60
crates/erp-dialysis/src/error.rs
Normal file
60
crates/erp-dialysis/src/error.rs
Normal file
@@ -0,0 +1,60 @@
|
||||
use erp_core::error::AppError;
|
||||
use thiserror::Error;
|
||||
|
||||
#[derive(Debug, Error)]
|
||||
pub enum DialysisError {
|
||||
#[error("{0}")]
|
||||
Validation(String),
|
||||
|
||||
#[error("患者不存在")]
|
||||
PatientNotFound,
|
||||
|
||||
#[error("透析记录不存在")]
|
||||
DialysisRecordNotFound,
|
||||
|
||||
#[error("透析方案不存在")]
|
||||
DialysisPrescriptionNotFound,
|
||||
|
||||
#[error("状态转换无效: {0}")]
|
||||
InvalidStatusTransition(String),
|
||||
|
||||
#[error("版本冲突: 数据已被其他操作修改,请刷新后重试")]
|
||||
VersionMismatch,
|
||||
|
||||
#[error("数据库操作失败: {0}")]
|
||||
DbError(String),
|
||||
}
|
||||
|
||||
impl From<DialysisError> for AppError {
|
||||
fn from(err: DialysisError) -> Self {
|
||||
match err {
|
||||
DialysisError::Validation(s) => AppError::Validation(s),
|
||||
DialysisError::PatientNotFound
|
||||
| DialysisError::DialysisRecordNotFound
|
||||
| DialysisError::DialysisPrescriptionNotFound => AppError::NotFound(err.to_string()),
|
||||
DialysisError::InvalidStatusTransition(s) => AppError::Validation(s),
|
||||
DialysisError::VersionMismatch => AppError::VersionMismatch,
|
||||
DialysisError::DbError(_) => AppError::Internal(err.to_string()),
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
impl From<sea_orm::DbErr> for DialysisError {
|
||||
fn from(err: sea_orm::DbErr) -> Self {
|
||||
DialysisError::DbError(err.to_string())
|
||||
}
|
||||
}
|
||||
|
||||
impl From<AppError> for DialysisError {
|
||||
fn from(err: AppError) -> Self {
|
||||
DialysisError::Validation(err.to_string())
|
||||
}
|
||||
}
|
||||
|
||||
impl From<String> for DialysisError {
|
||||
fn from(err: String) -> Self {
|
||||
DialysisError::Validation(err)
|
||||
}
|
||||
}
|
||||
|
||||
pub type DialysisResult<T> = Result<T, DialysisError>;
|
||||
6
crates/erp-dialysis/src/event.rs
Normal file
6
crates/erp-dialysis/src/event.rs
Normal file
@@ -0,0 +1,6 @@
|
||||
use erp_core::events::EventBus;
|
||||
|
||||
/// 预留事件处理器注册
|
||||
pub fn register_handlers_with_state(_state: crate::state::DialysisState) {
|
||||
// 透析模块事件消费者待后续迭代
|
||||
}
|
||||
@@ -11,7 +11,7 @@ use erp_core::types::{ApiResponse, PaginatedResponse, TenantContext};
|
||||
use crate::dto::dialysis_dto::*;
|
||||
use crate::dto::DeleteWithVersion;
|
||||
use crate::service::dialysis_service;
|
||||
use crate::state::HealthState;
|
||||
use crate::state::DialysisState;
|
||||
|
||||
#[derive(Debug, Deserialize, IntoParams)]
|
||||
pub struct PaginationParams {
|
||||
@@ -32,13 +32,13 @@ pub struct ReviewDialysisWithVersion {
|
||||
}
|
||||
|
||||
pub async fn list_dialysis_records<S>(
|
||||
State(state): State<HealthState>,
|
||||
State(state): State<DialysisState>,
|
||||
Extension(ctx): Extension<TenantContext>,
|
||||
Path(patient_id): Path<Uuid>,
|
||||
Query(params): Query<PaginationParams>,
|
||||
) -> Result<Json<ApiResponse<PaginatedResponse<DialysisRecordResp>>>, AppError>
|
||||
where
|
||||
HealthState: FromRef<S>,
|
||||
DialysisState: FromRef<S>,
|
||||
S: Clone + Send + Sync + 'static,
|
||||
{
|
||||
require_permission(&ctx, "health.health-data.list")?;
|
||||
@@ -52,12 +52,12 @@ where
|
||||
}
|
||||
|
||||
pub async fn get_dialysis_record<S>(
|
||||
State(state): State<HealthState>,
|
||||
State(state): State<DialysisState>,
|
||||
Extension(ctx): Extension<TenantContext>,
|
||||
Path(record_id): Path<Uuid>,
|
||||
) -> Result<Json<ApiResponse<DialysisRecordResp>>, AppError>
|
||||
where
|
||||
HealthState: FromRef<S>,
|
||||
DialysisState: FromRef<S>,
|
||||
S: Clone + Send + Sync + 'static,
|
||||
{
|
||||
require_permission(&ctx, "health.health-data.list")?;
|
||||
@@ -69,12 +69,12 @@ where
|
||||
}
|
||||
|
||||
pub async fn create_dialysis_record<S>(
|
||||
State(state): State<HealthState>,
|
||||
State(state): State<DialysisState>,
|
||||
Extension(ctx): Extension<TenantContext>,
|
||||
Json(req): Json<CreateDialysisRecordReq>,
|
||||
) -> Result<Json<ApiResponse<DialysisRecordResp>>, AppError>
|
||||
where
|
||||
HealthState: FromRef<S>,
|
||||
DialysisState: FromRef<S>,
|
||||
S: Clone + Send + Sync + 'static,
|
||||
{
|
||||
require_permission(&ctx, "health.health-data.manage")?;
|
||||
@@ -88,13 +88,13 @@ where
|
||||
}
|
||||
|
||||
pub async fn update_dialysis_record<S>(
|
||||
State(state): State<HealthState>,
|
||||
State(state): State<DialysisState>,
|
||||
Extension(ctx): Extension<TenantContext>,
|
||||
Path(record_id): Path<Uuid>,
|
||||
Json(req): Json<UpdateDialysisWithVersion>,
|
||||
) -> Result<Json<ApiResponse<DialysisRecordResp>>, AppError>
|
||||
where
|
||||
HealthState: FromRef<S>,
|
||||
DialysisState: FromRef<S>,
|
||||
S: Clone + Send + Sync + 'static,
|
||||
{
|
||||
require_permission(&ctx, "health.health-data.manage")?;
|
||||
@@ -108,13 +108,13 @@ where
|
||||
}
|
||||
|
||||
pub async fn review_dialysis_record<S>(
|
||||
State(state): State<HealthState>,
|
||||
State(state): State<DialysisState>,
|
||||
Extension(ctx): Extension<TenantContext>,
|
||||
Path(record_id): Path<Uuid>,
|
||||
Json(req): Json<ReviewDialysisWithVersion>,
|
||||
) -> Result<Json<ApiResponse<DialysisRecordResp>>, AppError>
|
||||
where
|
||||
HealthState: FromRef<S>,
|
||||
DialysisState: FromRef<S>,
|
||||
S: Clone + Send + Sync + 'static,
|
||||
{
|
||||
require_permission(&ctx, "health.health-data.manage")?;
|
||||
@@ -126,13 +126,13 @@ where
|
||||
}
|
||||
|
||||
pub async fn delete_dialysis_record<S>(
|
||||
State(state): State<HealthState>,
|
||||
State(state): State<DialysisState>,
|
||||
Extension(ctx): Extension<TenantContext>,
|
||||
Path(record_id): Path<Uuid>,
|
||||
Json(req): Json<DeleteWithVersion>,
|
||||
) -> Result<Json<ApiResponse<()>>, AppError>
|
||||
where
|
||||
HealthState: FromRef<S>,
|
||||
DialysisState: FromRef<S>,
|
||||
S: Clone + Send + Sync + 'static,
|
||||
{
|
||||
require_permission(&ctx, "health.health-data.manage")?;
|
||||
@@ -11,7 +11,7 @@ use erp_core::types::{ApiResponse, PaginatedResponse, TenantContext};
|
||||
use crate::dto::dialysis_prescription_dto::*;
|
||||
use crate::dto::DeleteWithVersion;
|
||||
use crate::service::dialysis_prescription_service;
|
||||
use crate::state::HealthState;
|
||||
use crate::state::DialysisState;
|
||||
|
||||
#[derive(Debug, Deserialize, IntoParams)]
|
||||
pub struct DialysisPrescriptionListParams {
|
||||
@@ -29,12 +29,12 @@ pub struct UpdateDialysisPrescriptionWithVersion {
|
||||
}
|
||||
|
||||
pub async fn list_prescriptions<S>(
|
||||
State(state): State<HealthState>,
|
||||
State(state): State<DialysisState>,
|
||||
Extension(ctx): Extension<TenantContext>,
|
||||
Query(params): Query<DialysisPrescriptionListParams>,
|
||||
) -> Result<Json<ApiResponse<PaginatedResponse<DialysisPrescriptionResp>>>, AppError>
|
||||
where
|
||||
HealthState: FromRef<S>,
|
||||
DialysisState: FromRef<S>,
|
||||
S: Clone + Send + Sync + 'static,
|
||||
{
|
||||
require_permission(&ctx, "health.dialysis-prescription.list")?;
|
||||
@@ -48,12 +48,12 @@ where
|
||||
}
|
||||
|
||||
pub async fn get_prescription<S>(
|
||||
State(state): State<HealthState>,
|
||||
State(state): State<DialysisState>,
|
||||
Extension(ctx): Extension<TenantContext>,
|
||||
Path(id): Path<Uuid>,
|
||||
) -> Result<Json<ApiResponse<DialysisPrescriptionResp>>, AppError>
|
||||
where
|
||||
HealthState: FromRef<S>,
|
||||
DialysisState: FromRef<S>,
|
||||
S: Clone + Send + Sync + 'static,
|
||||
{
|
||||
require_permission(&ctx, "health.dialysis-prescription.list")?;
|
||||
@@ -62,12 +62,12 @@ where
|
||||
}
|
||||
|
||||
pub async fn create_prescription<S>(
|
||||
State(state): State<HealthState>,
|
||||
State(state): State<DialysisState>,
|
||||
Extension(ctx): Extension<TenantContext>,
|
||||
Json(req): Json<CreateDialysisPrescriptionReq>,
|
||||
) -> Result<Json<ApiResponse<DialysisPrescriptionResp>>, AppError>
|
||||
where
|
||||
HealthState: FromRef<S>,
|
||||
DialysisState: FromRef<S>,
|
||||
S: Clone + Send + Sync + 'static,
|
||||
{
|
||||
require_permission(&ctx, "health.dialysis-prescription.manage")?;
|
||||
@@ -81,13 +81,13 @@ where
|
||||
}
|
||||
|
||||
pub async fn update_prescription<S>(
|
||||
State(state): State<HealthState>,
|
||||
State(state): State<DialysisState>,
|
||||
Extension(ctx): Extension<TenantContext>,
|
||||
Path(id): Path<Uuid>,
|
||||
Json(req): Json<UpdateDialysisPrescriptionWithVersion>,
|
||||
) -> Result<Json<ApiResponse<DialysisPrescriptionResp>>, AppError>
|
||||
where
|
||||
HealthState: FromRef<S>,
|
||||
DialysisState: FromRef<S>,
|
||||
S: Clone + Send + Sync + 'static,
|
||||
{
|
||||
require_permission(&ctx, "health.dialysis-prescription.manage")?;
|
||||
@@ -101,13 +101,13 @@ where
|
||||
}
|
||||
|
||||
pub async fn delete_prescription<S>(
|
||||
State(state): State<HealthState>,
|
||||
State(state): State<DialysisState>,
|
||||
Extension(ctx): Extension<TenantContext>,
|
||||
Path(id): Path<Uuid>,
|
||||
Json(req): Json<DeleteWithVersion>,
|
||||
) -> Result<Json<ApiResponse<()>>, AppError>
|
||||
where
|
||||
HealthState: FromRef<S>,
|
||||
DialysisState: FromRef<S>,
|
||||
S: Clone + Send + Sync + 'static,
|
||||
{
|
||||
require_permission(&ctx, "health.dialysis-prescription.manage")?;
|
||||
2
crates/erp-dialysis/src/handler/mod.rs
Normal file
2
crates/erp-dialysis/src/handler/mod.rs
Normal file
@@ -0,0 +1,2 @@
|
||||
pub mod dialysis_handler;
|
||||
pub mod dialysis_prescription_handler;
|
||||
11
crates/erp-dialysis/src/lib.rs
Normal file
11
crates/erp-dialysis/src/lib.rs
Normal file
@@ -0,0 +1,11 @@
|
||||
pub mod dto;
|
||||
pub mod entity;
|
||||
pub mod error;
|
||||
pub mod event;
|
||||
pub mod handler;
|
||||
pub mod module;
|
||||
pub mod service;
|
||||
pub mod state;
|
||||
|
||||
pub use module::DialysisModule;
|
||||
pub use state::DialysisState;
|
||||
114
crates/erp-dialysis/src/module.rs
Normal file
114
crates/erp-dialysis/src/module.rs
Normal file
@@ -0,0 +1,114 @@
|
||||
use async_trait::async_trait;
|
||||
use axum::Router;
|
||||
use erp_core::error::AppResult;
|
||||
use erp_core::module::{ErpModule, ModuleContext, ModuleType, PermissionDescriptor};
|
||||
|
||||
use crate::handler::{dialysis_handler, dialysis_prescription_handler};
|
||||
use crate::state::DialysisState;
|
||||
|
||||
pub struct DialysisModule;
|
||||
|
||||
impl DialysisModule {
|
||||
pub fn public_routes<S>() -> Router<S>
|
||||
where
|
||||
DialysisState: axum::extract::FromRef<S>,
|
||||
S: Clone + Send + Sync + 'static,
|
||||
{
|
||||
Router::new()
|
||||
}
|
||||
|
||||
pub fn protected_routes<S>() -> Router<S>
|
||||
where
|
||||
DialysisState: axum::extract::FromRef<S>,
|
||||
S: Clone + Send + Sync + 'static,
|
||||
{
|
||||
Router::new()
|
||||
// 透析记录
|
||||
.route(
|
||||
"/health/patients/{id}/dialysis-records",
|
||||
axum::routing::get(dialysis_handler::list_dialysis_records),
|
||||
)
|
||||
.route(
|
||||
"/health/dialysis-records",
|
||||
axum::routing::post(dialysis_handler::create_dialysis_record),
|
||||
)
|
||||
.route(
|
||||
"/health/dialysis-records/{id}",
|
||||
axum::routing::get(dialysis_handler::get_dialysis_record)
|
||||
.put(dialysis_handler::update_dialysis_record)
|
||||
.delete(dialysis_handler::delete_dialysis_record),
|
||||
)
|
||||
.route(
|
||||
"/health/dialysis-records/{id}/review",
|
||||
axum::routing::put(dialysis_handler::review_dialysis_record),
|
||||
)
|
||||
// 透析方案
|
||||
.route(
|
||||
"/health/dialysis-prescriptions",
|
||||
axum::routing::get(dialysis_prescription_handler::list_prescriptions)
|
||||
.post(dialysis_prescription_handler::create_prescription),
|
||||
)
|
||||
.route(
|
||||
"/health/dialysis-prescriptions/{id}",
|
||||
axum::routing::get(dialysis_prescription_handler::get_prescription)
|
||||
.put(dialysis_prescription_handler::update_prescription)
|
||||
.delete(dialysis_prescription_handler::delete_prescription),
|
||||
)
|
||||
}
|
||||
}
|
||||
|
||||
#[async_trait]
|
||||
impl ErpModule for DialysisModule {
|
||||
fn name(&self) -> &str {
|
||||
"透析管理"
|
||||
}
|
||||
|
||||
fn id(&self) -> &str {
|
||||
"erp-dialysis"
|
||||
}
|
||||
|
||||
fn version(&self) -> &str {
|
||||
"0.1.0"
|
||||
}
|
||||
|
||||
fn module_type(&self) -> ModuleType {
|
||||
ModuleType::Builtin
|
||||
}
|
||||
|
||||
fn permissions(&self) -> Vec<PermissionDescriptor> {
|
||||
vec![
|
||||
PermissionDescriptor {
|
||||
code: "health.health-data.list".into(),
|
||||
name: "查看透析记录".into(),
|
||||
description: "查看透析记录列表和详情".into(),
|
||||
module: "erp-dialysis".into(),
|
||||
},
|
||||
PermissionDescriptor {
|
||||
code: "health.health-data.manage".into(),
|
||||
name: "管理透析记录".into(),
|
||||
description: "创建、编辑、审阅、删除透析记录".into(),
|
||||
module: "erp-dialysis".into(),
|
||||
},
|
||||
PermissionDescriptor {
|
||||
code: "health.dialysis-prescription.list".into(),
|
||||
name: "查看透析处方".into(),
|
||||
description: "查看透析处方列表和详情".into(),
|
||||
module: "erp-dialysis".into(),
|
||||
},
|
||||
PermissionDescriptor {
|
||||
code: "health.dialysis-prescription.manage".into(),
|
||||
name: "管理透析处方".into(),
|
||||
description: "创建、编辑、删除透析处方".into(),
|
||||
module: "erp-dialysis".into(),
|
||||
},
|
||||
]
|
||||
}
|
||||
|
||||
async fn on_startup(&self, _ctx: &ModuleContext) -> AppResult<()> {
|
||||
Ok(())
|
||||
}
|
||||
|
||||
fn as_any(&self) -> &dyn std::any::Any {
|
||||
self
|
||||
}
|
||||
}
|
||||
@@ -1,3 +1,5 @@
|
||||
//! 透析方案 Service — 透析处方 CRUD
|
||||
|
||||
use chrono::Utc;
|
||||
use num_traits::ToPrimitive;
|
||||
use sea_orm::entity::prelude::*;
|
||||
@@ -10,18 +12,18 @@ use erp_core::error::check_version;
|
||||
use erp_core::types::PaginatedResponse;
|
||||
|
||||
use crate::dto::dialysis_prescription_dto::*;
|
||||
use crate::entity::{dialysis_prescription, patient};
|
||||
use crate::error::{HealthError, HealthResult};
|
||||
use crate::state::HealthState;
|
||||
use crate::entity::dialysis_prescription;
|
||||
use crate::error::{DialysisError, DialysisResult};
|
||||
use crate::state::DialysisState;
|
||||
|
||||
pub async fn list_prescriptions(
|
||||
state: &HealthState,
|
||||
state: &DialysisState,
|
||||
tenant_id: Uuid,
|
||||
page: u64,
|
||||
page_size: u64,
|
||||
patient_id: Option<Uuid>,
|
||||
status: Option<String>,
|
||||
) -> HealthResult<PaginatedResponse<DialysisPrescriptionResp>> {
|
||||
) -> DialysisResult<PaginatedResponse<DialysisPrescriptionResp>> {
|
||||
let limit = page_size.min(100);
|
||||
let offset = page.saturating_sub(1) * limit;
|
||||
|
||||
@@ -51,35 +53,28 @@ pub async fn list_prescriptions(
|
||||
}
|
||||
|
||||
pub async fn get_prescription(
|
||||
state: &HealthState,
|
||||
state: &DialysisState,
|
||||
tenant_id: Uuid,
|
||||
id: Uuid,
|
||||
) -> HealthResult<DialysisPrescriptionResp> {
|
||||
) -> DialysisResult<DialysisPrescriptionResp> {
|
||||
let m = dialysis_prescription::Entity::find()
|
||||
.filter(dialysis_prescription::Column::Id.eq(id))
|
||||
.filter(dialysis_prescription::Column::TenantId.eq(tenant_id))
|
||||
.filter(dialysis_prescription::Column::DeletedAt.is_null())
|
||||
.one(&state.db)
|
||||
.await?
|
||||
.ok_or(HealthError::DialysisPrescriptionNotFound)?;
|
||||
.ok_or(DialysisError::DialysisPrescriptionNotFound)?;
|
||||
|
||||
Ok(model_to_resp(m))
|
||||
}
|
||||
|
||||
pub async fn create_prescription(
|
||||
state: &HealthState,
|
||||
state: &DialysisState,
|
||||
tenant_id: Uuid,
|
||||
operator_id: Option<Uuid>,
|
||||
req: CreateDialysisPrescriptionReq,
|
||||
) -> HealthResult<DialysisPrescriptionResp> {
|
||||
// 校验患者存在
|
||||
patient::Entity::find()
|
||||
.filter(patient::Column::Id.eq(req.patient_id))
|
||||
.filter(patient::Column::TenantId.eq(tenant_id))
|
||||
.filter(patient::Column::DeletedAt.is_null())
|
||||
.one(&state.db)
|
||||
.await?
|
||||
.ok_or(HealthError::PatientNotFound)?;
|
||||
) -> DialysisResult<DialysisPrescriptionResp> {
|
||||
// 患者存在性由数据库 FK 约束保证,不再显式查询 patient 表
|
||||
|
||||
validate_anticoagulation_type(req.anticoagulation_type.as_deref())?;
|
||||
validate_vascular_access_type(req.vascular_access_type.as_deref())?;
|
||||
@@ -128,23 +123,23 @@ pub async fn create_prescription(
|
||||
}
|
||||
|
||||
pub async fn update_prescription(
|
||||
state: &HealthState,
|
||||
state: &DialysisState,
|
||||
tenant_id: Uuid,
|
||||
id: Uuid,
|
||||
operator_id: Option<Uuid>,
|
||||
req: UpdateDialysisPrescriptionReq,
|
||||
expected_version: i32,
|
||||
) -> HealthResult<DialysisPrescriptionResp> {
|
||||
) -> DialysisResult<DialysisPrescriptionResp> {
|
||||
let model = dialysis_prescription::Entity::find()
|
||||
.filter(dialysis_prescription::Column::Id.eq(id))
|
||||
.filter(dialysis_prescription::Column::TenantId.eq(tenant_id))
|
||||
.filter(dialysis_prescription::Column::DeletedAt.is_null())
|
||||
.one(&state.db)
|
||||
.await?
|
||||
.ok_or(HealthError::DialysisPrescriptionNotFound)?;
|
||||
.ok_or(DialysisError::DialysisPrescriptionNotFound)?;
|
||||
|
||||
let next_ver = check_version(expected_version, model.version)
|
||||
.map_err(|_| HealthError::VersionMismatch)?;
|
||||
.map_err(|_| DialysisError::VersionMismatch)?;
|
||||
|
||||
if let Some(ref t) = req.anticoagulation_type { validate_anticoagulation_type(Some(t))?; }
|
||||
if let Some(ref t) = req.vascular_access_type { validate_vascular_access_type(Some(t))?; }
|
||||
@@ -185,22 +180,22 @@ pub async fn update_prescription(
|
||||
}
|
||||
|
||||
pub async fn delete_prescription(
|
||||
state: &HealthState,
|
||||
state: &DialysisState,
|
||||
tenant_id: Uuid,
|
||||
id: Uuid,
|
||||
operator_id: Option<Uuid>,
|
||||
expected_version: i32,
|
||||
) -> HealthResult<()> {
|
||||
) -> DialysisResult<()> {
|
||||
let model = dialysis_prescription::Entity::find()
|
||||
.filter(dialysis_prescription::Column::Id.eq(id))
|
||||
.filter(dialysis_prescription::Column::TenantId.eq(tenant_id))
|
||||
.filter(dialysis_prescription::Column::DeletedAt.is_null())
|
||||
.one(&state.db)
|
||||
.await?
|
||||
.ok_or(HealthError::DialysisPrescriptionNotFound)?;
|
||||
.ok_or(DialysisError::DialysisPrescriptionNotFound)?;
|
||||
|
||||
let next_ver = check_version(expected_version, model.version)
|
||||
.map_err(|_| HealthError::VersionMismatch)?;
|
||||
.map_err(|_| DialysisError::VersionMismatch)?;
|
||||
|
||||
let mut active: dialysis_prescription::ActiveModel = model.into();
|
||||
active.deleted_at = Set(Some(Utc::now()));
|
||||
@@ -218,6 +213,10 @@ pub async fn delete_prescription(
|
||||
Ok(())
|
||||
}
|
||||
|
||||
// ---------------------------------------------------------------------------
|
||||
// 私有辅助函数
|
||||
// ---------------------------------------------------------------------------
|
||||
|
||||
fn model_to_resp(m: dialysis_prescription::Model) -> DialysisPrescriptionResp {
|
||||
DialysisPrescriptionResp {
|
||||
id: m.id,
|
||||
@@ -248,11 +247,11 @@ fn model_to_resp(m: dialysis_prescription::Model) -> DialysisPrescriptionResp {
|
||||
}
|
||||
}
|
||||
|
||||
fn validate_anticoagulation_type(val: Option<&str>) -> HealthResult<()> {
|
||||
fn validate_anticoagulation_type(val: Option<&str>) -> DialysisResult<()> {
|
||||
if let Some(t) = val {
|
||||
let valid = ["heparin", "lmwh", "heparin_free"];
|
||||
if !valid.contains(&t) {
|
||||
return Err(HealthError::Validation(format!(
|
||||
return Err(DialysisError::Validation(format!(
|
||||
"anticoagulation_type 必须为: {}", valid.join(", ")
|
||||
)));
|
||||
}
|
||||
@@ -260,11 +259,11 @@ fn validate_anticoagulation_type(val: Option<&str>) -> HealthResult<()> {
|
||||
Ok(())
|
||||
}
|
||||
|
||||
fn validate_vascular_access_type(val: Option<&str>) -> HealthResult<()> {
|
||||
fn validate_vascular_access_type(val: Option<&str>) -> DialysisResult<()> {
|
||||
if let Some(t) = val {
|
||||
let valid = ["avf", "avg", "cvc"];
|
||||
if !valid.contains(&t) {
|
||||
return Err(HealthError::Validation(format!(
|
||||
return Err(DialysisError::Validation(format!(
|
||||
"vascular_access_type 必须为: {}", valid.join(", ")
|
||||
)));
|
||||
}
|
||||
@@ -13,18 +13,17 @@ use erp_core::error::check_version;
|
||||
use erp_core::types::PaginatedResponse;
|
||||
|
||||
use crate::dto::dialysis_dto::*;
|
||||
use crate::entity::{dialysis_record, patient};
|
||||
use crate::error::{HealthError, HealthResult};
|
||||
use crate::service::validation;
|
||||
use crate::state::HealthState;
|
||||
use crate::entity::dialysis_record;
|
||||
use crate::error::{DialysisError, DialysisResult};
|
||||
use crate::state::DialysisState;
|
||||
|
||||
pub async fn list_dialysis_records(
|
||||
state: &HealthState,
|
||||
state: &DialysisState,
|
||||
tenant_id: Uuid,
|
||||
patient_id: Uuid,
|
||||
page: u64,
|
||||
page_size: u64,
|
||||
) -> HealthResult<PaginatedResponse<DialysisRecordResp>> {
|
||||
) -> DialysisResult<PaginatedResponse<DialysisRecordResp>> {
|
||||
let limit = page_size.min(100);
|
||||
let offset = page.saturating_sub(1) * limit;
|
||||
|
||||
@@ -49,34 +48,28 @@ pub async fn list_dialysis_records(
|
||||
}
|
||||
|
||||
pub async fn get_dialysis_record(
|
||||
state: &HealthState,
|
||||
state: &DialysisState,
|
||||
tenant_id: Uuid,
|
||||
record_id: Uuid,
|
||||
) -> HealthResult<DialysisRecordResp> {
|
||||
) -> DialysisResult<DialysisRecordResp> {
|
||||
let m = dialysis_record::Entity::find()
|
||||
.filter(dialysis_record::Column::Id.eq(record_id))
|
||||
.filter(dialysis_record::Column::TenantId.eq(tenant_id))
|
||||
.filter(dialysis_record::Column::DeletedAt.is_null())
|
||||
.one(&state.db)
|
||||
.await?
|
||||
.ok_or(HealthError::DialysisRecordNotFound)?;
|
||||
.ok_or(DialysisError::DialysisRecordNotFound)?;
|
||||
|
||||
Ok(to_resp(&state.crypto, m))
|
||||
}
|
||||
|
||||
pub async fn create_dialysis_record(
|
||||
state: &HealthState,
|
||||
state: &DialysisState,
|
||||
tenant_id: Uuid,
|
||||
operator_id: Option<Uuid>,
|
||||
req: CreateDialysisRecordReq,
|
||||
) -> HealthResult<DialysisRecordResp> {
|
||||
patient::Entity::find()
|
||||
.filter(patient::Column::Id.eq(req.patient_id))
|
||||
.filter(patient::Column::TenantId.eq(tenant_id))
|
||||
.filter(patient::Column::DeletedAt.is_null())
|
||||
.one(&state.db)
|
||||
.await?
|
||||
.ok_or(HealthError::PatientNotFound)?;
|
||||
) -> DialysisResult<DialysisRecordResp> {
|
||||
// 患者存在性由数据库 FK 约束保证,不再显式查询 patient 表
|
||||
|
||||
validate_dialysis_type(&req.dialysis_type)?;
|
||||
|
||||
@@ -84,9 +77,9 @@ pub async fn create_dialysis_record(
|
||||
|
||||
// PII 加密
|
||||
let encrypted_symptoms = req.symptoms.as_ref()
|
||||
.map(|v| -> HealthResult<serde_json::Value> {
|
||||
.map(|v| -> DialysisResult<serde_json::Value> {
|
||||
let json_str = serde_json::to_string(v)
|
||||
.map_err(|e| HealthError::Validation(e.to_string()))?;
|
||||
.map_err(|e| DialysisError::Validation(e.to_string()))?;
|
||||
Ok(serde_json::Value::String(pii::encrypt(kek, &json_str)?))
|
||||
})
|
||||
.transpose()?;
|
||||
@@ -141,23 +134,23 @@ pub async fn create_dialysis_record(
|
||||
}
|
||||
|
||||
pub async fn update_dialysis_record(
|
||||
state: &HealthState,
|
||||
state: &DialysisState,
|
||||
tenant_id: Uuid,
|
||||
record_id: Uuid,
|
||||
operator_id: Option<Uuid>,
|
||||
req: UpdateDialysisRecordReq,
|
||||
expected_version: i32,
|
||||
) -> HealthResult<DialysisRecordResp> {
|
||||
) -> DialysisResult<DialysisRecordResp> {
|
||||
let model = dialysis_record::Entity::find()
|
||||
.filter(dialysis_record::Column::Id.eq(record_id))
|
||||
.filter(dialysis_record::Column::TenantId.eq(tenant_id))
|
||||
.filter(dialysis_record::Column::DeletedAt.is_null())
|
||||
.one(&state.db)
|
||||
.await?
|
||||
.ok_or(HealthError::DialysisRecordNotFound)?;
|
||||
.ok_or(DialysisError::DialysisRecordNotFound)?;
|
||||
|
||||
let next_ver = check_version(expected_version, model.version)
|
||||
.map_err(|_| HealthError::VersionMismatch)?;
|
||||
.map_err(|_| DialysisError::VersionMismatch)?;
|
||||
|
||||
let mut active: dialysis_record::ActiveModel = model.into();
|
||||
if let Some(v) = req.dialysis_date { active.dialysis_date = Set(v); }
|
||||
@@ -205,24 +198,24 @@ pub async fn update_dialysis_record(
|
||||
}
|
||||
|
||||
pub async fn review_dialysis_record(
|
||||
state: &HealthState,
|
||||
state: &DialysisState,
|
||||
tenant_id: Uuid,
|
||||
record_id: Uuid,
|
||||
reviewer_id: Uuid,
|
||||
expected_version: i32,
|
||||
) -> HealthResult<DialysisRecordResp> {
|
||||
) -> DialysisResult<DialysisRecordResp> {
|
||||
let model = dialysis_record::Entity::find()
|
||||
.filter(dialysis_record::Column::Id.eq(record_id))
|
||||
.filter(dialysis_record::Column::TenantId.eq(tenant_id))
|
||||
.filter(dialysis_record::Column::DeletedAt.is_null())
|
||||
.one(&state.db)
|
||||
.await?
|
||||
.ok_or(HealthError::DialysisRecordNotFound)?;
|
||||
.ok_or(DialysisError::DialysisRecordNotFound)?;
|
||||
|
||||
let next_ver = check_version(expected_version, model.version)
|
||||
.map_err(|_| HealthError::VersionMismatch)?;
|
||||
.map_err(|_| DialysisError::VersionMismatch)?;
|
||||
|
||||
validation::validate_dialysis_status_transition(&model.status, "reviewed")?;
|
||||
validate_dialysis_status_transition(&model.status, "reviewed")?;
|
||||
|
||||
let mut active: dialysis_record::ActiveModel = model.into();
|
||||
active.status = Set("reviewed".to_string());
|
||||
@@ -244,22 +237,22 @@ pub async fn review_dialysis_record(
|
||||
}
|
||||
|
||||
pub async fn delete_dialysis_record(
|
||||
state: &HealthState,
|
||||
state: &DialysisState,
|
||||
tenant_id: Uuid,
|
||||
record_id: Uuid,
|
||||
operator_id: Option<Uuid>,
|
||||
expected_version: i32,
|
||||
) -> HealthResult<()> {
|
||||
) -> DialysisResult<()> {
|
||||
let model = dialysis_record::Entity::find()
|
||||
.filter(dialysis_record::Column::Id.eq(record_id))
|
||||
.filter(dialysis_record::Column::TenantId.eq(tenant_id))
|
||||
.filter(dialysis_record::Column::DeletedAt.is_null())
|
||||
.one(&state.db)
|
||||
.await?
|
||||
.ok_or(HealthError::DialysisRecordNotFound)?;
|
||||
.ok_or(DialysisError::DialysisRecordNotFound)?;
|
||||
|
||||
let next_ver = check_version(expected_version, model.version)
|
||||
.map_err(|_| HealthError::VersionMismatch)?;
|
||||
.map_err(|_| DialysisError::VersionMismatch)?;
|
||||
|
||||
let mut active: dialysis_record::ActiveModel = model.into();
|
||||
active.deleted_at = Set(Some(Utc::now()));
|
||||
@@ -277,15 +270,40 @@ pub async fn delete_dialysis_record(
|
||||
Ok(())
|
||||
}
|
||||
|
||||
fn validate_dialysis_type(dialysis_type: &str) -> HealthResult<()> {
|
||||
// ---------------------------------------------------------------------------
|
||||
// 私有辅助函数
|
||||
// ---------------------------------------------------------------------------
|
||||
|
||||
/// 校验透析类型枚举
|
||||
fn validate_dialysis_type(dialysis_type: &str) -> DialysisResult<()> {
|
||||
match dialysis_type {
|
||||
"HD" | "HDF" | "HF" => Ok(()),
|
||||
_ => Err(HealthError::Validation(format!(
|
||||
_ => Err(DialysisError::Validation(format!(
|
||||
"无效的透析类型: {},允许值: HD, HDF, HF", dialysis_type
|
||||
))),
|
||||
}
|
||||
}
|
||||
|
||||
/// 校验透析记录状态转换
|
||||
/// draft -> completed -> reviewed
|
||||
fn validate_dialysis_status_transition(current: &str, new: &str) -> DialysisResult<()> {
|
||||
if current == new {
|
||||
return Ok(());
|
||||
}
|
||||
let allowed = match current {
|
||||
"draft" => matches!(new, "completed"),
|
||||
"completed" => matches!(new, "reviewed"),
|
||||
_ => false,
|
||||
};
|
||||
if allowed {
|
||||
Ok(())
|
||||
} else {
|
||||
Err(DialysisError::InvalidStatusTransition(format!(
|
||||
"dialysis_record.status: 不允许从 '{}' 转换到 '{}'", current, new
|
||||
)))
|
||||
}
|
||||
}
|
||||
|
||||
fn to_resp(crypto: &erp_core::crypto::PiiCrypto, m: dialysis_record::Model) -> DialysisRecordResp {
|
||||
let kek = crypto.kek();
|
||||
|
||||
@@ -330,3 +348,32 @@ fn to_resp(crypto: &erp_core::crypto::PiiCrypto, m: dialysis_record::Model) -> D
|
||||
version: m.version,
|
||||
}
|
||||
}
|
||||
|
||||
#[cfg(test)]
|
||||
mod tests {
|
||||
use super::*;
|
||||
|
||||
// --- validate_dialysis_type ---
|
||||
#[test]
|
||||
fn dialysis_type_hd() { assert!(validate_dialysis_type("HD").is_ok()); }
|
||||
#[test]
|
||||
fn dialysis_type_hdf() { assert!(validate_dialysis_type("HDF").is_ok()); }
|
||||
#[test]
|
||||
fn dialysis_type_hf() { assert!(validate_dialysis_type("HF").is_ok()); }
|
||||
#[test]
|
||||
fn dialysis_type_invalid() { assert!(validate_dialysis_type("PD").is_err()); }
|
||||
|
||||
// --- validate_dialysis_status_transition ---
|
||||
#[test]
|
||||
fn dial_draft_to_completed() { assert!(validate_dialysis_status_transition("draft", "completed").is_ok()); }
|
||||
#[test]
|
||||
fn dial_draft_to_reviewed_fails() { assert!(validate_dialysis_status_transition("draft", "reviewed").is_err()); }
|
||||
#[test]
|
||||
fn dial_completed_to_reviewed() { assert!(validate_dialysis_status_transition("completed", "reviewed").is_ok()); }
|
||||
#[test]
|
||||
fn dial_completed_to_draft_fails() { assert!(validate_dialysis_status_transition("completed", "draft").is_err()); }
|
||||
#[test]
|
||||
fn dial_reviewed_to_any_fails() { assert!(validate_dialysis_status_transition("reviewed", "draft").is_err()); }
|
||||
#[test]
|
||||
fn dial_same_status_ok() { assert!(validate_dialysis_status_transition("draft", "draft").is_ok()); }
|
||||
}
|
||||
2
crates/erp-dialysis/src/service/mod.rs
Normal file
2
crates/erp-dialysis/src/service/mod.rs
Normal file
@@ -0,0 +1,2 @@
|
||||
pub mod dialysis_service;
|
||||
pub mod dialysis_prescription_service;
|
||||
10
crates/erp-dialysis/src/state.rs
Normal file
10
crates/erp-dialysis/src/state.rs
Normal file
@@ -0,0 +1,10 @@
|
||||
use erp_core::crypto::PiiCrypto;
|
||||
use erp_core::events::EventBus;
|
||||
use sea_orm::DatabaseConnection;
|
||||
|
||||
#[derive(Clone)]
|
||||
pub struct DialysisState {
|
||||
pub db: DatabaseConnection,
|
||||
pub event_bus: EventBus,
|
||||
pub crypto: PiiCrypto,
|
||||
}
|
||||
@@ -263,3 +263,15 @@ pub struct MiniTodayResp {
|
||||
pub blood_sugar: Option<IndicatorSummary>,
|
||||
pub weight: Option<IndicatorSummary>,
|
||||
}
|
||||
|
||||
#[derive(Debug, Clone, serde::Deserialize, ToSchema)]
|
||||
pub struct ReviewLabReportReq {
|
||||
pub doctor_notes: Option<String>,
|
||||
pub items: Option<serde_json::Value>,
|
||||
}
|
||||
|
||||
impl ReviewLabReportReq {
|
||||
pub fn sanitize(&mut self) {
|
||||
self.doctor_notes = sanitize_option(self.doctor_notes.take());
|
||||
}
|
||||
}
|
||||
|
||||
@@ -6,8 +6,6 @@ pub mod consultation_dto;
|
||||
pub mod daily_monitoring_dto;
|
||||
pub mod diagnosis_dto;
|
||||
pub mod medication_record_dto;
|
||||
pub mod dialysis_dto;
|
||||
pub mod dialysis_prescription_dto;
|
||||
pub mod doctor_dto;
|
||||
pub mod follow_up_dto;
|
||||
pub mod follow_up_template_dto;
|
||||
|
||||
@@ -16,8 +16,6 @@ pub mod critical_alert_response;
|
||||
pub mod daily_monitoring;
|
||||
pub mod device_readings;
|
||||
pub mod diagnosis;
|
||||
pub mod dialysis_prescription;
|
||||
pub mod dialysis_record;
|
||||
pub mod doctor_profile;
|
||||
pub mod doctor_schedule;
|
||||
pub mod follow_up_record;
|
||||
|
||||
@@ -157,6 +157,120 @@ pub fn register_handlers_with_state(state: crate::state::HealthState) {
|
||||
}
|
||||
});
|
||||
|
||||
// ── P1 事件消费者补全 ──
|
||||
|
||||
// patient.created → 欢迎消息通知
|
||||
let (mut patient_rx, _patient_handle) = state.event_bus.subscribe_filtered("patient.".to_string());
|
||||
let patient_db = state.db.clone();
|
||||
let patient_bus = state.event_bus.clone();
|
||||
tokio::spawn(async move {
|
||||
loop {
|
||||
match patient_rx.recv().await {
|
||||
Some(event) if event.event_type == PATIENT_CREATED => {
|
||||
if erp_core::events::is_event_processed(&patient_db, event.id, "patient_welcome").await.unwrap_or(false) {
|
||||
continue;
|
||||
}
|
||||
let patient_id = event.payload.get("patient_id")
|
||||
.and_then(|v| v.as_str())
|
||||
.and_then(|s| Uuid::parse_str(s).ok());
|
||||
if let Some(pid) = patient_id {
|
||||
let welcome_event = erp_core::events::DomainEvent::new(
|
||||
"message.send",
|
||||
event.tenant_id,
|
||||
erp_core::events::build_event_payload(serde_json::json!({
|
||||
"template": "patient_welcome",
|
||||
"recipient_type": "patient",
|
||||
"recipient_id": pid,
|
||||
})),
|
||||
);
|
||||
patient_bus.publish(welcome_event, &patient_db).await;
|
||||
tracing::info!(patient_id = %pid, "新患者欢迎流程触发");
|
||||
}
|
||||
let _ = erp_core::events::mark_event_processed(&patient_db, event.id, "patient_welcome").await;
|
||||
}
|
||||
Some(_) => {}
|
||||
None => break,
|
||||
}
|
||||
}
|
||||
});
|
||||
|
||||
// appointment.confirmed/cancelled → 通知 + 号源释放
|
||||
let (mut appt_rx, _appt_handle) = state.event_bus.subscribe_filtered("appointment.".to_string());
|
||||
let appt_db = state.db.clone();
|
||||
let appt_bus = state.event_bus.clone();
|
||||
tokio::spawn(async move {
|
||||
loop {
|
||||
match appt_rx.recv().await {
|
||||
Some(event) if event.event_type == "appointment.confirmed" => {
|
||||
if erp_core::events::is_event_processed(&appt_db, event.id, "appointment_notifier").await.unwrap_or(false) {
|
||||
continue;
|
||||
}
|
||||
let doctor_id = event.payload.get("doctor_id").and_then(|v| v.as_str());
|
||||
let patient_id = event.payload.get("patient_id").and_then(|v| v.as_str());
|
||||
if let (Some(did), Some(pid)) = (doctor_id, patient_id) {
|
||||
let notify_event = erp_core::events::DomainEvent::new(
|
||||
"message.send",
|
||||
event.tenant_id,
|
||||
erp_core::events::build_event_payload(serde_json::json!({
|
||||
"template": "appointment_confirmed",
|
||||
"recipient_type": "doctor",
|
||||
"recipient_id": did,
|
||||
"patient_id": pid,
|
||||
})),
|
||||
);
|
||||
appt_bus.publish(notify_event, &appt_db).await;
|
||||
tracing::info!(doctor_id = did, patient_id = pid, "预约确认通知触发");
|
||||
}
|
||||
let _ = erp_core::events::mark_event_processed(&appt_db, event.id, "appointment_notifier").await;
|
||||
}
|
||||
Some(event) if event.event_type == "appointment.cancelled" => {
|
||||
if erp_core::events::is_event_processed(&appt_db, event.id, "appointment_cancel_handler").await.unwrap_or(false) {
|
||||
continue;
|
||||
}
|
||||
tracing::info!(event_id = %event.id, "预约取消,号源释放");
|
||||
let _ = erp_core::events::mark_event_processed(&appt_db, event.id, "appointment_cancel_handler").await;
|
||||
}
|
||||
Some(_) => {}
|
||||
None => break,
|
||||
}
|
||||
}
|
||||
});
|
||||
|
||||
// follow_up.overdue → 升级通知
|
||||
let (mut fu_rx, _fu_handle) = state.event_bus.subscribe_filtered("follow_up.".to_string());
|
||||
let fu_db = state.db.clone();
|
||||
let fu_bus = state.event_bus.clone();
|
||||
tokio::spawn(async move {
|
||||
loop {
|
||||
match fu_rx.recv().await {
|
||||
Some(event) if event.event_type == FOLLOW_UP_OVERDUE => {
|
||||
if erp_core::events::is_event_processed(&fu_db, event.id, "follow_up_escalator").await.unwrap_or(false) {
|
||||
continue;
|
||||
}
|
||||
let task_id = event.payload.get("task_id").and_then(|v| v.as_str());
|
||||
let assigned_to = event.payload.get("assigned_to").and_then(|v| v.as_str());
|
||||
if let (Some(tid), Some(uid)) = (task_id, assigned_to) {
|
||||
let escalate_event = erp_core::events::DomainEvent::new(
|
||||
"message.send",
|
||||
event.tenant_id,
|
||||
erp_core::events::build_event_payload(serde_json::json!({
|
||||
"template": "follow_up_overdue",
|
||||
"recipient_type": "staff",
|
||||
"recipient_id": uid,
|
||||
"task_id": tid,
|
||||
})),
|
||||
);
|
||||
fu_bus.publish(escalate_event, &fu_db).await;
|
||||
tracing::warn!(task_id = tid, assigned_to = uid, "随访逾期升级通知");
|
||||
}
|
||||
let _ = erp_core::events::mark_event_processed(&fu_db, event.id, "follow_up_escalator").await;
|
||||
}
|
||||
Some(_) => {}
|
||||
None => break,
|
||||
}
|
||||
}
|
||||
});
|
||||
|
||||
// health_data.critical_alert → 创建危急值告警记录
|
||||
let (mut critical_rx, _critical_handle) = state.event_bus.subscribe_filtered("health_data.".to_string());
|
||||
let critical_state = state.clone();
|
||||
|
||||
@@ -9,7 +9,6 @@ use erp_core::rbac::require_permission;
|
||||
use erp_core::types::{ApiResponse, PaginatedResponse, TenantContext};
|
||||
|
||||
use crate::dto::health_data_dto::*;
|
||||
use crate::dto::dialysis_dto::ReviewLabReportReq;
|
||||
use crate::dto::DeleteWithVersion;
|
||||
use crate::service::health_data_service;
|
||||
use crate::service::trend_service;
|
||||
|
||||
@@ -12,8 +12,6 @@ pub mod daily_monitoring_handler;
|
||||
pub mod device_reading_handler;
|
||||
pub mod diagnosis_handler;
|
||||
pub mod medication_record_handler;
|
||||
pub mod dialysis_handler;
|
||||
pub mod dialysis_prescription_handler;
|
||||
pub mod doctor_handler;
|
||||
pub mod follow_up_handler;
|
||||
pub mod follow_up_template_handler;
|
||||
|
||||
@@ -7,7 +7,7 @@ use erp_core::module::{ErpModule, PermissionDescriptor};
|
||||
|
||||
use crate::handler::{
|
||||
alert_handler, alert_rule_handler,
|
||||
appointment_handler, article_category_handler, article_handler, article_tag_handler, consultation_handler, consent_handler, critical_alert_handler, critical_value_threshold_handler, daily_monitoring_handler, device_reading_handler, diagnosis_handler, dialysis_handler, dialysis_prescription_handler, doctor_handler, follow_up_handler, follow_up_template_handler,
|
||||
appointment_handler, article_category_handler, article_handler, article_tag_handler, consultation_handler, consent_handler, critical_alert_handler, critical_value_threshold_handler, daily_monitoring_handler, device_reading_handler, diagnosis_handler, doctor_handler, follow_up_handler, follow_up_template_handler,
|
||||
health_data_handler, medication_record_handler, patient_handler, points_handler, stats_handler,
|
||||
};
|
||||
|
||||
@@ -225,37 +225,6 @@ impl HealthModule {
|
||||
"/health/vital-signs/today",
|
||||
axum::routing::get(health_data_handler::get_mini_today),
|
||||
)
|
||||
// 透析记录(血透专科)
|
||||
.route(
|
||||
"/health/patients/{id}/dialysis-records",
|
||||
axum::routing::get(dialysis_handler::list_dialysis_records),
|
||||
)
|
||||
.route(
|
||||
"/health/dialysis-records",
|
||||
axum::routing::post(dialysis_handler::create_dialysis_record),
|
||||
)
|
||||
.route(
|
||||
"/health/dialysis-records/{id}",
|
||||
axum::routing::get(dialysis_handler::get_dialysis_record)
|
||||
.put(dialysis_handler::update_dialysis_record)
|
||||
.delete(dialysis_handler::delete_dialysis_record),
|
||||
)
|
||||
.route(
|
||||
"/health/dialysis-records/{id}/review",
|
||||
axum::routing::put(dialysis_handler::review_dialysis_record),
|
||||
)
|
||||
// 透析方案
|
||||
.route(
|
||||
"/health/dialysis-prescriptions",
|
||||
axum::routing::get(dialysis_prescription_handler::list_prescriptions)
|
||||
.post(dialysis_prescription_handler::create_prescription),
|
||||
)
|
||||
.route(
|
||||
"/health/dialysis-prescriptions/{id}",
|
||||
axum::routing::get(dialysis_prescription_handler::get_prescription)
|
||||
.put(dialysis_prescription_handler::update_prescription)
|
||||
.delete(dialysis_prescription_handler::delete_prescription),
|
||||
)
|
||||
// 随访模板
|
||||
.route(
|
||||
"/health/follow-up-templates",
|
||||
|
||||
@@ -581,16 +581,18 @@ pub async fn enrich_doctor_dashboard_health(
|
||||
doctor_user_id: Uuid,
|
||||
dashboard: &mut DoctorDashboard,
|
||||
) -> HealthResult<()> {
|
||||
use crate::entity::{dialysis_record, lab_report, appointment};
|
||||
use crate::entity::{lab_report, appointment};
|
||||
use sea_orm::{FromQueryResult, Statement, DatabaseBackend};
|
||||
|
||||
// 待审核透析记录(doctor_id 通过患者关联,这里取全租户待审核)
|
||||
let pending_dialysis = dialysis_record::Entity::find()
|
||||
.filter(dialysis_record::Column::TenantId.eq(tenant_id))
|
||||
.filter(dialysis_record::Column::DeletedAt.is_null())
|
||||
.filter(dialysis_record::Column::Status.eq("draft"))
|
||||
.count(&state.db)
|
||||
.await?;
|
||||
dashboard.pending_dialysis_review = pending_dialysis as i64;
|
||||
// 待审核透析记录(raw SQL — entity 已拆分到 erp-dialysis crate)
|
||||
#[derive(FromQueryResult)]
|
||||
struct DialysisCount { count: i64 }
|
||||
let pending_dialysis = DialysisCount::find_by_statement(Statement::from_sql_and_values(
|
||||
DatabaseBackend::Postgres,
|
||||
"SELECT COUNT(*)::int8 AS count FROM dialysis_record WHERE tenant_id = $1 AND deleted_at IS NULL AND status = 'draft'",
|
||||
[tenant_id.into()],
|
||||
)).one(&state.db).await?.map(|r| r.count).unwrap_or(0);
|
||||
dashboard.pending_dialysis_review = pending_dialysis;
|
||||
|
||||
// 待审核化验报告
|
||||
let pending_lab = lab_report::Entity::find()
|
||||
|
||||
@@ -563,7 +563,7 @@ pub async fn review_lab_report(
|
||||
patient_id: Uuid,
|
||||
report_id: Uuid,
|
||||
reviewer_id: Uuid,
|
||||
req: crate::dto::dialysis_dto::ReviewLabReportReq,
|
||||
req: crate::dto::health_data_dto::ReviewLabReportReq,
|
||||
expected_version: i32,
|
||||
) -> HealthResult<LabReportResp> {
|
||||
let model = lab_report::Entity::find()
|
||||
|
||||
@@ -13,8 +13,6 @@ pub mod daily_monitoring_service;
|
||||
pub mod device_reading_service;
|
||||
pub mod diagnosis_service;
|
||||
pub mod medication_record_service;
|
||||
pub mod dialysis_prescription_service;
|
||||
pub mod dialysis_service;
|
||||
pub mod doctor_service;
|
||||
pub mod follow_up_service;
|
||||
pub mod follow_up_template_service;
|
||||
|
||||
@@ -1,11 +1,11 @@
|
||||
use sea_orm::{ColumnTrait, EntityTrait, PaginatorTrait, QueryFilter, sea_query::Expr, FromQueryResult};
|
||||
use sea_orm::{ColumnTrait, EntityTrait, PaginatorTrait, QueryFilter, sea_query::Expr, FromQueryResult, Statement, DatabaseBackend};
|
||||
|
||||
use erp_core::error::AppResult;
|
||||
|
||||
use crate::dto::stats_dto::*;
|
||||
use crate::entity::{
|
||||
patient, consultation_session, follow_up_task,
|
||||
points_transaction, dialysis_record, lab_report,
|
||||
points_transaction, lab_report,
|
||||
appointment, vital_signs, patient_doctor_relation, doctor_profile,
|
||||
};
|
||||
use crate::state::HealthState;
|
||||
@@ -190,25 +190,27 @@ pub async fn get_dialysis_statistics(
|
||||
) -> AppResult<DialysisStatisticsResp> {
|
||||
let db = &state.db;
|
||||
|
||||
let total_records = dialysis_record::Entity::find()
|
||||
.filter(dialysis_record::Column::TenantId.eq(tenant_id))
|
||||
.filter(dialysis_record::Column::DeletedAt.is_null())
|
||||
.count(db)
|
||||
.await?;
|
||||
// 使用 raw SQL 替代 dialysis_record entity(已拆分到 erp-dialysis crate)
|
||||
#[derive(FromQueryResult)]
|
||||
struct CountRow { count: i64 }
|
||||
|
||||
let this_month = dialysis_record::Entity::find()
|
||||
.filter(dialysis_record::Column::TenantId.eq(tenant_id))
|
||||
.filter(dialysis_record::Column::DeletedAt.is_null())
|
||||
.filter(Expr::col(dialysis_record::Column::CreatedAt).gte(Expr::cust("date_trunc('month', NOW())")))
|
||||
.count(db)
|
||||
.await?;
|
||||
let total_records = CountRow::find_by_statement(Statement::from_sql_and_values(
|
||||
DatabaseBackend::Postgres,
|
||||
"SELECT COUNT(*)::int8 AS count FROM dialysis_record WHERE tenant_id = $1 AND deleted_at IS NULL",
|
||||
[tenant_id.into()],
|
||||
)).one(db).await?.map(|r| r.count).unwrap_or(0);
|
||||
|
||||
let pending_review = dialysis_record::Entity::find()
|
||||
.filter(dialysis_record::Column::TenantId.eq(tenant_id))
|
||||
.filter(dialysis_record::Column::DeletedAt.is_null())
|
||||
.filter(dialysis_record::Column::Status.eq("draft"))
|
||||
.count(db)
|
||||
.await?;
|
||||
let this_month = CountRow::find_by_statement(Statement::from_sql_and_values(
|
||||
DatabaseBackend::Postgres,
|
||||
"SELECT COUNT(*)::int8 AS count FROM dialysis_record WHERE tenant_id = $1 AND deleted_at IS NULL AND created_at >= date_trunc('month', NOW())",
|
||||
[tenant_id.into()],
|
||||
)).one(db).await?.map(|r| r.count).unwrap_or(0);
|
||||
|
||||
let pending_review = CountRow::find_by_statement(Statement::from_sql_and_values(
|
||||
DatabaseBackend::Postgres,
|
||||
"SELECT COUNT(*)::int8 AS count FROM dialysis_record WHERE tenant_id = $1 AND deleted_at IS NULL AND status = 'draft'",
|
||||
[tenant_id.into()],
|
||||
)).one(db).await?.map(|r| r.count).unwrap_or(0);
|
||||
|
||||
let type_distribution = count_by_field(
|
||||
db, tenant_id,
|
||||
@@ -223,13 +225,13 @@ pub async fn get_dialysis_statistics(
|
||||
let avg_duration = compute_avg_field(db, tenant_id, "dialysis_duration").await?;
|
||||
|
||||
Ok(DialysisStatisticsResp {
|
||||
total_records: total_records as i64,
|
||||
this_month: this_month as i64,
|
||||
total_records,
|
||||
this_month,
|
||||
type_distribution,
|
||||
complication_rate,
|
||||
avg_ultrafiltration,
|
||||
avg_duration,
|
||||
pending_review: pending_review as i64,
|
||||
pending_review,
|
||||
})
|
||||
}
|
||||
|
||||
|
||||
13
crates/erp-plugin-assessment/Cargo.toml
Normal file
13
crates/erp-plugin-assessment/Cargo.toml
Normal file
@@ -0,0 +1,13 @@
|
||||
[package]
|
||||
name = "erp-plugin-assessment"
|
||||
version = "0.1.0"
|
||||
edition = "2024"
|
||||
description = "标准化医学评估量表插件(PHQ-9、GAD-7 等)"
|
||||
|
||||
[lib]
|
||||
crate-type = ["cdylib"]
|
||||
|
||||
[dependencies]
|
||||
wit-bindgen = "0.55"
|
||||
serde = { workspace = true }
|
||||
serde_json = { workspace = true }
|
||||
149
crates/erp-plugin-assessment/plugin.toml
Normal file
149
crates/erp-plugin-assessment/plugin.toml
Normal file
@@ -0,0 +1,149 @@
|
||||
# 评估量表 — plugin.toml
|
||||
|
||||
[metadata]
|
||||
id = "assessment"
|
||||
name = "评估量表"
|
||||
version = "0.1.0"
|
||||
description = "标准化医学评估量表(PHQ-9、GAD-7 等)"
|
||||
author = "HMS"
|
||||
min_platform_version = "0.1.0"
|
||||
|
||||
[[permissions]]
|
||||
code = "assessment_scale.list"
|
||||
name = "查看评估量表"
|
||||
description = "查看评估量表列表和详情"
|
||||
|
||||
[[permissions]]
|
||||
code = "assessment_scale.manage"
|
||||
name = "管理评估量表"
|
||||
description = "创建、编辑、删除评估量表"
|
||||
|
||||
[[permissions]]
|
||||
code = "assessment_response.list"
|
||||
name = "查看评估结果"
|
||||
description = "查看患者评估答卷"
|
||||
|
||||
[[permissions]]
|
||||
code = "assessment_response.manage"
|
||||
name = "管理评估结果"
|
||||
description = "提交、编辑评估答卷"
|
||||
|
||||
[[schema.entities]]
|
||||
name = "assessment_scale"
|
||||
display_name = "评估量表"
|
||||
|
||||
[[schema.entities.fields]]
|
||||
name = "scale_code"
|
||||
field_type = "string"
|
||||
required = true
|
||||
display_name = "量表编码"
|
||||
unique = true
|
||||
ui_widget = "select"
|
||||
options = ["PHQ-9", "GAD-7", "SF-36", "MMSE", "ADL", "IADL"]
|
||||
|
||||
[[schema.entities.fields]]
|
||||
name = "title"
|
||||
field_type = "string"
|
||||
required = true
|
||||
display_name = "量表名称"
|
||||
searchable = true
|
||||
|
||||
[[schema.entities.fields]]
|
||||
name = "description"
|
||||
field_type = "string"
|
||||
display_name = "描述"
|
||||
ui_widget = "textarea"
|
||||
|
||||
[[schema.entities.fields]]
|
||||
name = "questions_json"
|
||||
field_type = "json"
|
||||
required = true
|
||||
display_name = "题目定义(JSON)"
|
||||
|
||||
[[schema.entities.fields]]
|
||||
name = "scoring_rules_json"
|
||||
field_type = "json"
|
||||
required = true
|
||||
display_name = "评分规则(JSON)"
|
||||
|
||||
[[schema.entities.fields]]
|
||||
name = "status"
|
||||
field_type = "string"
|
||||
required = true
|
||||
display_name = "状态"
|
||||
default = "active"
|
||||
ui_widget = "select"
|
||||
options = ["active", "inactive"]
|
||||
|
||||
[[schema.entities]]
|
||||
name = "assessment_response"
|
||||
display_name = "评估答卷"
|
||||
|
||||
[[schema.entities.fields]]
|
||||
name = "scale_id"
|
||||
field_type = "uuid"
|
||||
required = true
|
||||
display_name = "量表"
|
||||
ui_widget = "entity_select"
|
||||
ref_entity = "assessment_scale"
|
||||
ref_plugin = "assessment"
|
||||
|
||||
[[schema.entities.fields]]
|
||||
name = "patient_id"
|
||||
field_type = "uuid"
|
||||
required = true
|
||||
display_name = "患者 ID"
|
||||
|
||||
[[schema.entities.fields]]
|
||||
name = "answers_json"
|
||||
field_type = "json"
|
||||
required = true
|
||||
display_name = "答案(JSON)"
|
||||
|
||||
[[schema.entities.fields]]
|
||||
name = "total_score"
|
||||
field_type = "integer"
|
||||
required = true
|
||||
display_name = "总分"
|
||||
|
||||
[[schema.entities.fields]]
|
||||
name = "severity_level"
|
||||
field_type = "string"
|
||||
required = true
|
||||
display_name = "严重程度"
|
||||
ui_widget = "select"
|
||||
options = ["normal", "mild", "moderate", "moderate_severe", "severe"]
|
||||
|
||||
[[schema.entities.fields]]
|
||||
name = "assessed_by"
|
||||
field_type = "uuid"
|
||||
display_name = "评估人"
|
||||
|
||||
[[schema.entities.fields]]
|
||||
name = "status"
|
||||
field_type = "string"
|
||||
required = true
|
||||
display_name = "状态"
|
||||
default = "completed"
|
||||
ui_widget = "select"
|
||||
options = ["draft", "completed", "reviewed"]
|
||||
|
||||
[[schema.entities.relations]]
|
||||
entity = "assessment_scale"
|
||||
foreign_key = "scale_id"
|
||||
on_delete = "restrict"
|
||||
name = "scale"
|
||||
type = "belongs_to"
|
||||
display_field = "title"
|
||||
|
||||
[[trigger_events]]
|
||||
name = "assessment_completed"
|
||||
display_name = "评估完成"
|
||||
description = "患者完成评估量表,触发评分计算和后续流程"
|
||||
entity = "assessment_response"
|
||||
on = "create"
|
||||
|
||||
[[ui.pages]]
|
||||
type = "crud"
|
||||
label = "评估量表"
|
||||
icon = "FormOutlined"
|
||||
145
crates/erp-plugin-assessment/src/lib.rs
Normal file
145
crates/erp-plugin-assessment/src/lib.rs
Normal file
@@ -0,0 +1,145 @@
|
||||
//! 评估量表插件 — 标准化医学评估(PHQ-9, GAD-7 等)
|
||||
|
||||
use serde_json::json;
|
||||
|
||||
wit_bindgen::generate!({
|
||||
path: "../erp-plugin-prototype/wit/plugin.wit",
|
||||
world: "plugin-world",
|
||||
});
|
||||
|
||||
use crate::erp::plugin::host_api;
|
||||
use crate::exports::erp::plugin::plugin_api::Guest;
|
||||
|
||||
struct AssessmentPlugin;
|
||||
|
||||
impl Guest for AssessmentPlugin {
|
||||
fn init() -> Result<(), String> {
|
||||
host_api::log_write("info", "AssessmentPlugin initialized");
|
||||
Ok(())
|
||||
}
|
||||
|
||||
fn on_tenant_created(tenant_id: String) -> Result<(), String> {
|
||||
host_api::log_write(
|
||||
"info",
|
||||
&format!("AssessmentPlugin: tenant {} created, inserting default scales", tenant_id),
|
||||
);
|
||||
|
||||
insert_phq9_scale(&tenant_id)?;
|
||||
|
||||
Ok(())
|
||||
}
|
||||
|
||||
fn handle_event(event_type: String, payload: Vec<u8>) -> Result<(), String> {
|
||||
host_api::log_write(
|
||||
"debug",
|
||||
&format!("AssessmentPlugin received: {}", event_type),
|
||||
);
|
||||
|
||||
if event_type == "assessment_completed" {
|
||||
let data: serde_json::Value =
|
||||
serde_json::from_slice(&payload).map_err(|e| format!("解析失败: {}", e))?;
|
||||
host_api::log_write(
|
||||
"info",
|
||||
&format!("评估完成: scale_id={}, patient_id={}",
|
||||
data["scale_id"].as_str().unwrap_or("?"),
|
||||
data["patient_id"].as_str().unwrap_or("?")
|
||||
),
|
||||
);
|
||||
}
|
||||
|
||||
Ok(())
|
||||
}
|
||||
}
|
||||
|
||||
/// 插入 PHQ-9 抑郁筛查量表默认数据
|
||||
fn insert_phq9_scale(tenant_id: &str) -> Result<(), String> {
|
||||
let questions = json!([
|
||||
{"id": 1, "text": "做事时提不起劲或没有兴趣", "options": [
|
||||
{"label": "完全不会", "score": 0},
|
||||
{"label": "好几天", "score": 1},
|
||||
{"label": "一半以上的天数", "score": 2},
|
||||
{"label": "几乎每天", "score": 3}
|
||||
]},
|
||||
{"id": 2, "text": "感到心情低落、沮丧或绝望", "options": [
|
||||
{"label": "完全不会", "score": 0},
|
||||
{"label": "好几天", "score": 1},
|
||||
{"label": "一半以上的天数", "score": 2},
|
||||
{"label": "几乎每天", "score": 3}
|
||||
]},
|
||||
{"id": 3, "text": "入睡困难、睡不安稳或睡眠过多", "options": [
|
||||
{"label": "完全不会", "score": 0},
|
||||
{"label": "好几天", "score": 1},
|
||||
{"label": "一半以上的天数", "score": 2},
|
||||
{"label": "几乎每天", "score": 3}
|
||||
]},
|
||||
{"id": 4, "text": "感觉疲倦或没有活力", "options": [
|
||||
{"label": "完全不会", "score": 0},
|
||||
{"label": "好几天", "score": 1},
|
||||
{"label": "一半以上的天数", "score": 2},
|
||||
{"label": "几乎每天", "score": 3}
|
||||
]},
|
||||
{"id": 5, "text": "食欲不振或吃太多", "options": [
|
||||
{"label": "完全不会", "score": 0},
|
||||
{"label": "好几天", "score": 1},
|
||||
{"label": "一半以上的天数", "score": 2},
|
||||
{"label": "几乎每天", "score": 3}
|
||||
]},
|
||||
{"id": 6, "text": "觉得自己很糟,或觉得自己失败了,或让自己和家人失望", "options": [
|
||||
{"label": "完全不会", "score": 0},
|
||||
{"label": "好几天", "score": 1},
|
||||
{"label": "一半以上的天数", "score": 2},
|
||||
{"label": "几乎每天", "score": 3}
|
||||
]},
|
||||
{"id": 7, "text": "对事物专注有困难(例如阅读或看电视)", "options": [
|
||||
{"label": "完全不会", "score": 0},
|
||||
{"label": "好几天", "score": 1},
|
||||
{"label": "一半以上的天数", "score": 2},
|
||||
{"label": "几乎每天", "score": 3}
|
||||
]},
|
||||
{"id": 8, "text": "动作或说话速度缓慢到别人可以察觉,或正好相反——烦躁不安地动来动去", "options": [
|
||||
{"label": "完全不会", "score": 0},
|
||||
{"label": "好几天", "score": 1},
|
||||
{"label": "一半以上的天数", "score": 2},
|
||||
{"label": "几乎每天", "score": 3}
|
||||
]},
|
||||
{"id": 9, "text": "有不如死掉或者用某种方式伤害自己的念头", "options": [
|
||||
{"label": "完全不会", "score": 0},
|
||||
{"label": "好几天", "score": 1},
|
||||
{"label": "一半以上的天数", "score": 2},
|
||||
{"label": "几乎每天", "score": 3}
|
||||
]}
|
||||
]);
|
||||
|
||||
let scoring_rules = json!([
|
||||
{"min": 0, "max": 4, "level": "normal", "label": "无抑郁症状"},
|
||||
{"min": 5, "max": 9, "level": "mild", "label": "轻度抑郁"},
|
||||
{"min": 10, "max": 14, "level": "moderate", "label": "中度抑郁"},
|
||||
{"min": 15, "max": 19, "level": "moderate_severe", "label": "中重度抑郁"},
|
||||
{"min": 20, "max": 27, "level": "severe", "label": "重度抑郁"}
|
||||
]);
|
||||
|
||||
let data = json!({
|
||||
"scale_code": "PHQ-9",
|
||||
"title": "PHQ-9 患者健康问卷(抑郁筛查)",
|
||||
"description": "Patient Health Questionnaire-9,用于抑郁症状筛查和严重程度评估",
|
||||
"questions_json": questions,
|
||||
"scoring_rules_json": scoring_rules,
|
||||
"status": "active",
|
||||
"tenant_id": tenant_id,
|
||||
});
|
||||
|
||||
let result = host_api::db_insert("assessment_scale", data.to_string().as_bytes())
|
||||
.map_err(|e| format!("插入 PHQ-9 默认量表失败: {}", e))?;
|
||||
|
||||
let record: serde_json::Value =
|
||||
serde_json::from_slice(&result).map_err(|e| format!("解析结果失败: {}", e))?;
|
||||
|
||||
host_api::log_write(
|
||||
"info",
|
||||
&format!("PHQ-9 默认量表已创建: id={}", record["id"].as_str().unwrap_or("?")),
|
||||
);
|
||||
|
||||
Ok(())
|
||||
}
|
||||
|
||||
export!(AssessmentPlugin);
|
||||
19
crates/erp-points/Cargo.toml
Normal file
19
crates/erp-points/Cargo.toml
Normal file
@@ -0,0 +1,19 @@
|
||||
[package]
|
||||
name = "erp-points"
|
||||
version.workspace = true
|
||||
edition.workspace = true
|
||||
|
||||
[dependencies]
|
||||
erp-core.workspace = true
|
||||
tokio.workspace = true
|
||||
serde.workspace = true
|
||||
serde_json.workspace = true
|
||||
uuid.workspace = true
|
||||
chrono.workspace = true
|
||||
axum.workspace = true
|
||||
sea-orm.workspace = true
|
||||
tracing.workspace = true
|
||||
thiserror.workspace = true
|
||||
validator.workspace = true
|
||||
utoipa.workspace = true
|
||||
async-trait.workspace = true
|
||||
8
crates/erp-points/src/entity/mod.rs
Normal file
8
crates/erp-points/src/entity/mod.rs
Normal file
@@ -0,0 +1,8 @@
|
||||
pub mod offline_event;
|
||||
pub mod offline_event_registration;
|
||||
pub mod points_account;
|
||||
pub mod points_checkin;
|
||||
pub mod points_order;
|
||||
pub mod points_product;
|
||||
pub mod points_rule;
|
||||
pub mod points_transaction;
|
||||
40
crates/erp-points/src/entity/offline_event.rs
Normal file
40
crates/erp-points/src/entity/offline_event.rs
Normal file
@@ -0,0 +1,40 @@
|
||||
use sea_orm::entity::prelude::*;
|
||||
use serde::{Deserialize, Serialize};
|
||||
|
||||
#[derive(Clone, Debug, PartialEq, DeriveEntityModel, Serialize, Deserialize)]
|
||||
#[sea_orm(table_name = "offline_event")]
|
||||
pub struct Model {
|
||||
#[sea_orm(primary_key, auto_increment = false)]
|
||||
pub id: Uuid,
|
||||
pub tenant_id: Uuid,
|
||||
pub title: String,
|
||||
#[sea_orm(skip_serializing_if = "Option::is_none")]
|
||||
pub description: Option<String>,
|
||||
pub event_date: chrono::NaiveDate,
|
||||
#[sea_orm(skip_serializing_if = "Option::is_none")]
|
||||
pub start_time: Option<chrono::NaiveTime>,
|
||||
#[sea_orm(skip_serializing_if = "Option::is_none")]
|
||||
pub end_time: Option<chrono::NaiveTime>,
|
||||
#[sea_orm(skip_serializing_if = "Option::is_none")]
|
||||
pub location: Option<String>,
|
||||
pub points_reward: i32,
|
||||
pub max_participants: i32,
|
||||
pub current_participants: i32,
|
||||
pub status: String,
|
||||
#[sea_orm(skip_serializing_if = "Option::is_none")]
|
||||
pub image_url: Option<String>,
|
||||
pub created_at: DateTimeUtc,
|
||||
pub updated_at: DateTimeUtc,
|
||||
#[sea_orm(skip_serializing_if = "Option::is_none")]
|
||||
pub created_by: Option<Uuid>,
|
||||
#[sea_orm(skip_serializing_if = "Option::is_none")]
|
||||
pub updated_by: Option<Uuid>,
|
||||
#[sea_orm(skip_serializing_if = "Option::is_none")]
|
||||
pub deleted_at: Option<DateTimeUtc>,
|
||||
pub version: i32,
|
||||
}
|
||||
|
||||
#[derive(Copy, Clone, Debug, EnumIter, DeriveRelation)]
|
||||
pub enum Relation {}
|
||||
|
||||
impl ActiveModelBehavior for ActiveModel {}
|
||||
32
crates/erp-points/src/entity/offline_event_registration.rs
Normal file
32
crates/erp-points/src/entity/offline_event_registration.rs
Normal file
@@ -0,0 +1,32 @@
|
||||
use sea_orm::entity::prelude::*;
|
||||
use serde::{Deserialize, Serialize};
|
||||
|
||||
#[derive(Clone, Debug, PartialEq, DeriveEntityModel, Serialize, Deserialize)]
|
||||
#[sea_orm(table_name = "offline_event_registration")]
|
||||
pub struct Model {
|
||||
#[sea_orm(primary_key, auto_increment = false)]
|
||||
pub id: Uuid,
|
||||
pub tenant_id: Uuid,
|
||||
pub event_id: Uuid,
|
||||
pub patient_id: Uuid,
|
||||
pub status: String,
|
||||
#[sea_orm(skip_serializing_if = "Option::is_none")]
|
||||
pub checked_in_at: Option<DateTimeUtc>,
|
||||
#[sea_orm(skip_serializing_if = "Option::is_none")]
|
||||
pub checked_in_by: Option<Uuid>,
|
||||
pub points_granted: bool,
|
||||
pub created_at: DateTimeUtc,
|
||||
pub updated_at: DateTimeUtc,
|
||||
#[sea_orm(skip_serializing_if = "Option::is_none")]
|
||||
pub created_by: Option<Uuid>,
|
||||
#[sea_orm(skip_serializing_if = "Option::is_none")]
|
||||
pub updated_by: Option<Uuid>,
|
||||
#[sea_orm(skip_serializing_if = "Option::is_none")]
|
||||
pub deleted_at: Option<DateTimeUtc>,
|
||||
pub version: i32,
|
||||
}
|
||||
|
||||
#[derive(Copy, Clone, Debug, EnumIter, DeriveRelation)]
|
||||
pub enum Relation {}
|
||||
|
||||
impl ActiveModelBehavior for ActiveModel {}
|
||||
42
crates/erp-points/src/entity/points_account.rs
Normal file
42
crates/erp-points/src/entity/points_account.rs
Normal file
@@ -0,0 +1,42 @@
|
||||
use sea_orm::entity::prelude::*;
|
||||
use serde::{Deserialize, Serialize};
|
||||
|
||||
#[derive(Clone, Debug, PartialEq, DeriveEntityModel, Serialize, Deserialize)]
|
||||
#[sea_orm(table_name = "points_account")]
|
||||
pub struct Model {
|
||||
#[sea_orm(primary_key, auto_increment = false)]
|
||||
pub id: Uuid,
|
||||
pub tenant_id: Uuid,
|
||||
pub patient_id: Uuid,
|
||||
pub balance: i32,
|
||||
pub total_earned: i32,
|
||||
pub total_spent: i32,
|
||||
pub total_expired: i32,
|
||||
pub version: i32,
|
||||
pub created_at: DateTimeUtc,
|
||||
pub updated_at: DateTimeUtc,
|
||||
#[sea_orm(skip_serializing_if = "Option::is_none")]
|
||||
pub created_by: Option<Uuid>,
|
||||
#[sea_orm(skip_serializing_if = "Option::is_none")]
|
||||
pub updated_by: Option<Uuid>,
|
||||
#[sea_orm(skip_serializing_if = "Option::is_none")]
|
||||
pub deleted_at: Option<DateTimeUtc>,
|
||||
}
|
||||
|
||||
#[derive(Copy, Clone, Debug, EnumIter, DeriveRelation)]
|
||||
pub enum Relation {
|
||||
#[sea_orm(
|
||||
belongs_to = "super::patient::Entity",
|
||||
from = "Column::PatientId",
|
||||
to = "super::patient::Column::Id"
|
||||
)]
|
||||
Patient,
|
||||
}
|
||||
|
||||
impl Related<super::patient::Entity> for Entity {
|
||||
fn to() -> RelationDef {
|
||||
Relation::Patient.def()
|
||||
}
|
||||
}
|
||||
|
||||
impl ActiveModelBehavior for ActiveModel {}
|
||||
37
crates/erp-points/src/entity/points_checkin.rs
Normal file
37
crates/erp-points/src/entity/points_checkin.rs
Normal file
@@ -0,0 +1,37 @@
|
||||
use sea_orm::entity::prelude::*;
|
||||
use serde::{Deserialize, Serialize};
|
||||
|
||||
#[derive(Clone, Debug, PartialEq, DeriveEntityModel, Serialize, Deserialize)]
|
||||
#[sea_orm(table_name = "points_checkin")]
|
||||
pub struct Model {
|
||||
#[sea_orm(primary_key, auto_increment = false)]
|
||||
pub id: Uuid,
|
||||
pub tenant_id: Uuid,
|
||||
pub patient_id: Uuid,
|
||||
pub checkin_date: chrono::NaiveDate,
|
||||
pub consecutive_days: i32,
|
||||
pub created_at: DateTimeUtc,
|
||||
pub updated_at: DateTimeUtc,
|
||||
pub created_by: Option<Uuid>,
|
||||
pub updated_by: Option<Uuid>,
|
||||
pub deleted_at: Option<DateTimeUtc>,
|
||||
pub version: i32,
|
||||
}
|
||||
|
||||
#[derive(Copy, Clone, Debug, EnumIter, DeriveRelation)]
|
||||
pub enum Relation {
|
||||
#[sea_orm(
|
||||
belongs_to = "super::patient::Entity",
|
||||
from = "Column::PatientId",
|
||||
to = "super::patient::Column::Id"
|
||||
)]
|
||||
Patient,
|
||||
}
|
||||
|
||||
impl Related<super::patient::Entity> for Entity {
|
||||
fn to() -> RelationDef {
|
||||
Relation::Patient.def()
|
||||
}
|
||||
}
|
||||
|
||||
impl ActiveModelBehavior for ActiveModel {}
|
||||
51
crates/erp-points/src/entity/points_order.rs
Normal file
51
crates/erp-points/src/entity/points_order.rs
Normal file
@@ -0,0 +1,51 @@
|
||||
use sea_orm::entity::prelude::*;
|
||||
use serde::{Deserialize, Serialize};
|
||||
|
||||
#[derive(Clone, Debug, PartialEq, DeriveEntityModel, Serialize, Deserialize)]
|
||||
#[sea_orm(table_name = "points_order")]
|
||||
pub struct Model {
|
||||
#[sea_orm(primary_key, auto_increment = false)]
|
||||
pub id: Uuid,
|
||||
pub tenant_id: Uuid,
|
||||
pub patient_id: Uuid,
|
||||
pub product_id: Uuid,
|
||||
pub points_cost: i32,
|
||||
pub status: String,
|
||||
#[sea_orm(skip_serializing_if = "Option::is_none")]
|
||||
pub qr_code: Option<Uuid>,
|
||||
#[sea_orm(skip_serializing_if = "Option::is_none")]
|
||||
pub verified_by: Option<Uuid>,
|
||||
#[sea_orm(skip_serializing_if = "Option::is_none")]
|
||||
pub verified_at: Option<DateTimeUtc>,
|
||||
#[sea_orm(skip_serializing_if = "Option::is_none")]
|
||||
pub expires_at: Option<DateTimeUtc>,
|
||||
#[sea_orm(skip_serializing_if = "Option::is_none")]
|
||||
pub notes: Option<String>,
|
||||
pub created_at: DateTimeUtc,
|
||||
pub updated_at: DateTimeUtc,
|
||||
#[sea_orm(skip_serializing_if = "Option::is_none")]
|
||||
pub created_by: Option<Uuid>,
|
||||
#[sea_orm(skip_serializing_if = "Option::is_none")]
|
||||
pub updated_by: Option<Uuid>,
|
||||
#[sea_orm(skip_serializing_if = "Option::is_none")]
|
||||
pub deleted_at: Option<DateTimeUtc>,
|
||||
pub version: i32,
|
||||
}
|
||||
|
||||
#[derive(Copy, Clone, Debug, EnumIter, DeriveRelation)]
|
||||
pub enum Relation {
|
||||
#[sea_orm(
|
||||
belongs_to = "super::patient::Entity",
|
||||
from = "Column::PatientId",
|
||||
to = "super::patient::Column::Id"
|
||||
)]
|
||||
Patient,
|
||||
}
|
||||
|
||||
impl Related<super::patient::Entity> for Entity {
|
||||
fn to() -> RelationDef {
|
||||
Relation::Patient.def()
|
||||
}
|
||||
}
|
||||
|
||||
impl ActiveModelBehavior for ActiveModel {}
|
||||
36
crates/erp-points/src/entity/points_product.rs
Normal file
36
crates/erp-points/src/entity/points_product.rs
Normal file
@@ -0,0 +1,36 @@
|
||||
use sea_orm::entity::prelude::*;
|
||||
use serde::{Deserialize, Serialize};
|
||||
|
||||
#[derive(Clone, Debug, PartialEq, DeriveEntityModel, Serialize, Deserialize)]
|
||||
#[sea_orm(table_name = "points_product")]
|
||||
pub struct Model {
|
||||
#[sea_orm(primary_key, auto_increment = false)]
|
||||
pub id: Uuid,
|
||||
pub tenant_id: Uuid,
|
||||
pub name: String,
|
||||
pub product_type: String,
|
||||
pub points_cost: i32,
|
||||
pub stock: i32,
|
||||
#[sea_orm(skip_serializing_if = "Option::is_none")]
|
||||
pub image_url: Option<String>,
|
||||
#[sea_orm(skip_serializing_if = "Option::is_none")]
|
||||
pub description: Option<String>,
|
||||
#[sea_orm(skip_serializing_if = "Option::is_none")]
|
||||
pub service_config: Option<serde_json::Value>,
|
||||
pub is_active: bool,
|
||||
pub sort_order: i32,
|
||||
pub created_at: DateTimeUtc,
|
||||
pub updated_at: DateTimeUtc,
|
||||
#[sea_orm(skip_serializing_if = "Option::is_none")]
|
||||
pub created_by: Option<Uuid>,
|
||||
#[sea_orm(skip_serializing_if = "Option::is_none")]
|
||||
pub updated_by: Option<Uuid>,
|
||||
#[sea_orm(skip_serializing_if = "Option::is_none")]
|
||||
pub deleted_at: Option<DateTimeUtc>,
|
||||
pub version: i32,
|
||||
}
|
||||
|
||||
#[derive(Copy, Clone, Debug, EnumIter, DeriveRelation)]
|
||||
pub enum Relation {}
|
||||
|
||||
impl ActiveModelBehavior for ActiveModel {}
|
||||
34
crates/erp-points/src/entity/points_rule.rs
Normal file
34
crates/erp-points/src/entity/points_rule.rs
Normal file
@@ -0,0 +1,34 @@
|
||||
use sea_orm::entity::prelude::*;
|
||||
use serde::{Deserialize, Serialize};
|
||||
|
||||
#[derive(Clone, Debug, PartialEq, DeriveEntityModel, Serialize, Deserialize)]
|
||||
#[sea_orm(table_name = "points_rule")]
|
||||
pub struct Model {
|
||||
#[sea_orm(primary_key, auto_increment = false)]
|
||||
pub id: Uuid,
|
||||
pub tenant_id: Uuid,
|
||||
pub event_type: String,
|
||||
pub name: String,
|
||||
#[sea_orm(skip_serializing_if = "Option::is_none")]
|
||||
pub description: Option<String>,
|
||||
pub points_value: i32,
|
||||
pub daily_cap: i32,
|
||||
pub streak_7d_bonus: i32,
|
||||
pub streak_14d_bonus: i32,
|
||||
pub streak_30d_bonus: i32,
|
||||
pub is_active: bool,
|
||||
pub created_at: DateTimeUtc,
|
||||
pub updated_at: DateTimeUtc,
|
||||
#[sea_orm(skip_serializing_if = "Option::is_none")]
|
||||
pub created_by: Option<Uuid>,
|
||||
#[sea_orm(skip_serializing_if = "Option::is_none")]
|
||||
pub updated_by: Option<Uuid>,
|
||||
#[sea_orm(skip_serializing_if = "Option::is_none")]
|
||||
pub deleted_at: Option<DateTimeUtc>,
|
||||
pub version: i32,
|
||||
}
|
||||
|
||||
#[derive(Copy, Clone, Debug, EnumIter, DeriveRelation)]
|
||||
pub enum Relation {}
|
||||
|
||||
impl ActiveModelBehavior for ActiveModel {}
|
||||
39
crates/erp-points/src/entity/points_transaction.rs
Normal file
39
crates/erp-points/src/entity/points_transaction.rs
Normal file
@@ -0,0 +1,39 @@
|
||||
use sea_orm::entity::prelude::*;
|
||||
use serde::{Deserialize, Serialize};
|
||||
|
||||
#[derive(Clone, Debug, PartialEq, DeriveEntityModel, Serialize, Deserialize)]
|
||||
#[sea_orm(table_name = "points_transaction")]
|
||||
pub struct Model {
|
||||
#[sea_orm(primary_key, auto_increment = false)]
|
||||
pub id: Uuid,
|
||||
pub tenant_id: Uuid,
|
||||
pub account_id: Uuid,
|
||||
#[sea_orm(column_name = "transaction_type")]
|
||||
pub transaction_type: String,
|
||||
pub amount: i32,
|
||||
pub remaining_amount: i32,
|
||||
pub status: String,
|
||||
#[sea_orm(skip_serializing_if = "Option::is_none")]
|
||||
pub expires_at: Option<DateTimeUtc>,
|
||||
pub balance_after: i32,
|
||||
#[sea_orm(skip_serializing_if = "Option::is_none")]
|
||||
pub rule_id: Option<Uuid>,
|
||||
#[sea_orm(skip_serializing_if = "Option::is_none")]
|
||||
pub order_id: Option<Uuid>,
|
||||
#[sea_orm(skip_serializing_if = "Option::is_none")]
|
||||
pub description: Option<String>,
|
||||
pub created_at: DateTimeUtc,
|
||||
pub updated_at: DateTimeUtc,
|
||||
#[sea_orm(skip_serializing_if = "Option::is_none")]
|
||||
pub created_by: Option<Uuid>,
|
||||
#[sea_orm(skip_serializing_if = "Option::is_none")]
|
||||
pub updated_by: Option<Uuid>,
|
||||
#[sea_orm(skip_serializing_if = "Option::is_none")]
|
||||
pub deleted_at: Option<DateTimeUtc>,
|
||||
pub version: i32,
|
||||
}
|
||||
|
||||
#[derive(Copy, Clone, Debug, EnumIter, DeriveRelation)]
|
||||
pub enum Relation {}
|
||||
|
||||
impl ActiveModelBehavior for ActiveModel {}
|
||||
51
crates/erp-points/src/error.rs
Normal file
51
crates/erp-points/src/error.rs
Normal file
@@ -0,0 +1,51 @@
|
||||
use erp_core::error::AppError;
|
||||
|
||||
#[derive(Debug, thiserror::Error)]
|
||||
pub enum PointsError {
|
||||
#[error("{0}")]
|
||||
Validation(String),
|
||||
|
||||
#[error("积分规则不存在")]
|
||||
PointsRuleNotFound,
|
||||
|
||||
#[error("兑换商品不存在")]
|
||||
PointsProductNotFound,
|
||||
|
||||
#[error("兑换订单不存在")]
|
||||
PointsOrderNotFound,
|
||||
|
||||
#[error("积分不足")]
|
||||
InsufficientPoints,
|
||||
|
||||
#[error("线下活动不存在")]
|
||||
OfflineEventNotFound,
|
||||
|
||||
#[error("版本冲突")]
|
||||
VersionMismatch,
|
||||
|
||||
#[error("数据库操作失败: {0}")]
|
||||
DbError(String),
|
||||
}
|
||||
|
||||
impl From<PointsError> for AppError {
|
||||
fn from(err: PointsError) -> Self {
|
||||
match err {
|
||||
PointsError::Validation(s) => AppError::Validation(s),
|
||||
PointsError::PointsRuleNotFound
|
||||
| PointsError::PointsProductNotFound
|
||||
| PointsError::PointsOrderNotFound
|
||||
| PointsError::OfflineEventNotFound => AppError::NotFound(err.to_string()),
|
||||
PointsError::InsufficientPoints => AppError::Validation(err.to_string()),
|
||||
PointsError::VersionMismatch => AppError::VersionMismatch,
|
||||
PointsError::DbError(_) => AppError::Internal(err.to_string()),
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
impl From<sea_orm::DbErr> for PointsError {
|
||||
fn from(err: sea_orm::DbErr) -> Self {
|
||||
PointsError::DbError(err.to_string())
|
||||
}
|
||||
}
|
||||
|
||||
pub type PointsResult<T> = Result<T, PointsError>;
|
||||
15
crates/erp-points/src/event.rs
Normal file
15
crates/erp-points/src/event.rs
Normal file
@@ -0,0 +1,15 @@
|
||||
use crate::state::PointsState;
|
||||
|
||||
pub const POINTS_EARNED: &str = "points.earned";
|
||||
pub const POINTS_EXCHANGED: &str = "points.exchanged";
|
||||
pub const POINTS_EXPIRED: &str = "points.expired";
|
||||
pub const POINTS_BALANCE_CHANGED: &str = "points.balance.changed";
|
||||
|
||||
pub fn register_handlers(_state: PointsState) {
|
||||
// Phase 1: 订阅已有事件(lab_report.uploaded, patient.verified, daily_monitoring.created)
|
||||
// 待 erp-health 发布这些事件后启用消费者
|
||||
}
|
||||
|
||||
pub fn register_handlers_with_state(state: PointsState) {
|
||||
register_handlers(state);
|
||||
}
|
||||
8
crates/erp-points/src/lib.rs
Normal file
8
crates/erp-points/src/lib.rs
Normal file
@@ -0,0 +1,8 @@
|
||||
pub mod dto;
|
||||
pub mod entity;
|
||||
pub mod error;
|
||||
pub mod event;
|
||||
pub mod handler;
|
||||
pub mod module;
|
||||
pub mod service;
|
||||
pub mod state;
|
||||
128
crates/erp-points/src/module.rs
Normal file
128
crates/erp-points/src/module.rs
Normal file
@@ -0,0 +1,128 @@
|
||||
use axum::Router;
|
||||
use erp_core::module::{ErpModule, ModuleContext, ModuleType, PermissionDescriptor};
|
||||
|
||||
use crate::handler::points_handler;
|
||||
use crate::state::PointsState;
|
||||
|
||||
pub struct PointsModule;
|
||||
|
||||
impl PointsModule {
|
||||
pub fn new() -> Self {
|
||||
Self
|
||||
}
|
||||
}
|
||||
|
||||
impl ErpModule for PointsModule {
|
||||
fn name(&self) -> &str {
|
||||
"points"
|
||||
}
|
||||
|
||||
fn module_id(&self) -> &str {
|
||||
"erp-points"
|
||||
}
|
||||
|
||||
fn module_type(&self) -> ModuleType {
|
||||
ModuleType::Business
|
||||
}
|
||||
|
||||
fn on_startup(&self, ctx: ModuleContext) {
|
||||
let state = PointsState {
|
||||
db: ctx.db.clone(),
|
||||
event_bus: ctx.event_bus.clone(),
|
||||
};
|
||||
crate::event::register_handlers_with_state(state);
|
||||
}
|
||||
|
||||
fn permissions(&self) -> Vec<PermissionDescriptor> {
|
||||
vec![
|
||||
PermissionDescriptor {
|
||||
code: "points.account.list".into(),
|
||||
name: "积分账户列表".into(),
|
||||
},
|
||||
PermissionDescriptor {
|
||||
code: "points.account.manage".into(),
|
||||
name: "积分账户管理".into(),
|
||||
},
|
||||
PermissionDescriptor {
|
||||
code: "points.product.list".into(),
|
||||
name: "积分商品列表".into(),
|
||||
},
|
||||
PermissionDescriptor {
|
||||
code: "points.product.manage".into(),
|
||||
name: "积分商品管理".into(),
|
||||
},
|
||||
PermissionDescriptor {
|
||||
code: "points.order.list".into(),
|
||||
name: "积分订单列表".into(),
|
||||
},
|
||||
PermissionDescriptor {
|
||||
code: "points.order.manage".into(),
|
||||
name: "积分订单管理".into(),
|
||||
},
|
||||
PermissionDescriptor {
|
||||
code: "points.rule.list".into(),
|
||||
name: "积分规则列表".into(),
|
||||
},
|
||||
PermissionDescriptor {
|
||||
code: "points.rule.manage".into(),
|
||||
name: "积分规则管理".into(),
|
||||
},
|
||||
]
|
||||
}
|
||||
|
||||
fn protected_routes<S>(&self) -> Router<S>
|
||||
where
|
||||
PointsState: axum::extract::FromRef<S>,
|
||||
S: Clone + Send + Sync + 'static,
|
||||
{
|
||||
Router::new()
|
||||
.route(
|
||||
"/points/accounts",
|
||||
axum::routing::get(points_handler::list_accounts)
|
||||
.post(points_handler::create_account),
|
||||
)
|
||||
.route(
|
||||
"/points/accounts/{id}",
|
||||
axum::routing::get(points_handler::get_account),
|
||||
)
|
||||
.route(
|
||||
"/points/products",
|
||||
axum::routing::get(points_handler::list_products)
|
||||
.post(points_handler::create_product),
|
||||
)
|
||||
.route(
|
||||
"/points/products/{id}",
|
||||
axum::routing::get(points_handler::get_product)
|
||||
.put(points_handler::update_product)
|
||||
.delete(points_handler::delete_product),
|
||||
)
|
||||
.route(
|
||||
"/points/orders",
|
||||
axum::routing::get(points_handler::list_orders)
|
||||
.post(points_handler::create_order),
|
||||
)
|
||||
.route(
|
||||
"/points/orders/{id}",
|
||||
axum::routing::get(points_handler::get_order),
|
||||
)
|
||||
.route(
|
||||
"/points/rules",
|
||||
axum::routing::get(points_handler::list_rules)
|
||||
.post(points_handler::create_rule),
|
||||
)
|
||||
.route(
|
||||
"/points/rules/{id}",
|
||||
axum::routing::get(points_handler::get_rule)
|
||||
.put(points_handler::update_rule)
|
||||
.delete(points_handler::delete_rule),
|
||||
)
|
||||
.route(
|
||||
"/points/checkin",
|
||||
axum::routing::post(points_handler::check_in),
|
||||
)
|
||||
.route(
|
||||
"/points/transactions",
|
||||
axum::routing::get(points_handler::list_transactions),
|
||||
)
|
||||
}
|
||||
}
|
||||
437
crates/erp-points/src/service/account_service.rs
Normal file
437
crates/erp-points/src/service/account_service.rs
Normal file
@@ -0,0 +1,437 @@
|
||||
//! 积分账户 Service — 获取/创建账户、积分获取、流水查询、积分统计
|
||||
|
||||
use chrono::{Duration, Utc};
|
||||
use sea_orm::entity::prelude::*;
|
||||
use sea_orm::sea_query::Expr;
|
||||
use sea_orm::{ActiveValue::Set, TransactionTrait};
|
||||
use uuid::Uuid;
|
||||
|
||||
use erp_core::audit::AuditLog;
|
||||
use erp_core::audit_service;
|
||||
use erp_core::events::DomainEvent;
|
||||
use erp_core::types::PaginatedResponse;
|
||||
|
||||
use crate::dto::points_dto::*;
|
||||
use crate::entity::{points_account, points_rule, points_transaction};
|
||||
use crate::error::{PointsError, PointsResult};
|
||||
use crate::state::PointsState;
|
||||
|
||||
// ---------------------------------------------------------------------------
|
||||
// 内部辅助:获取或创建账户
|
||||
// ---------------------------------------------------------------------------
|
||||
|
||||
/// 获取或创建患者的积分账户(支持事务和非事务连接)
|
||||
pub(crate) async fn get_or_create_account<C: sea_orm::ConnectionTrait>(
|
||||
db: &C,
|
||||
tenant_id: Uuid,
|
||||
patient_id: Uuid,
|
||||
) -> PointsResult<points_account::Model> {
|
||||
if let Some(acc) = points_account::Entity::find()
|
||||
.filter(points_account::Column::TenantId.eq(tenant_id))
|
||||
.filter(points_account::Column::PatientId.eq(patient_id))
|
||||
.filter(points_account::Column::DeletedAt.is_null())
|
||||
.one(db)
|
||||
.await?
|
||||
{
|
||||
return Ok(acc);
|
||||
}
|
||||
let now = Utc::now();
|
||||
let active = points_account::ActiveModel {
|
||||
id: Set(Uuid::now_v7()),
|
||||
tenant_id: Set(tenant_id),
|
||||
patient_id: Set(patient_id),
|
||||
balance: Set(0),
|
||||
total_earned: Set(0),
|
||||
total_spent: Set(0),
|
||||
total_expired: Set(0),
|
||||
version: Set(1),
|
||||
created_at: Set(now),
|
||||
updated_at: Set(now),
|
||||
created_by: Set(None),
|
||||
updated_by: Set(None),
|
||||
deleted_at: Set(None),
|
||||
};
|
||||
Ok(active.insert(db).await?)
|
||||
}
|
||||
|
||||
// ---------------------------------------------------------------------------
|
||||
// 积分账户
|
||||
// ---------------------------------------------------------------------------
|
||||
|
||||
/// 获取患者积分账户
|
||||
pub async fn get_account(
|
||||
state: &PointsState,
|
||||
tenant_id: Uuid,
|
||||
patient_id: Uuid,
|
||||
) -> PointsResult<PointsAccountResp> {
|
||||
let acc = get_or_create_account(&state.db, tenant_id, patient_id).await?;
|
||||
Ok(PointsAccountResp {
|
||||
id: acc.id,
|
||||
patient_id: acc.patient_id,
|
||||
balance: acc.balance,
|
||||
total_earned: acc.total_earned,
|
||||
total_spent: acc.total_spent,
|
||||
total_expired: acc.total_expired,
|
||||
created_at: acc.created_at,
|
||||
updated_at: acc.updated_at,
|
||||
version: acc.version,
|
||||
})
|
||||
}
|
||||
|
||||
// ---------------------------------------------------------------------------
|
||||
// 积分获取(事件触发)
|
||||
// ---------------------------------------------------------------------------
|
||||
|
||||
/// 核心方法:根据事件类型给患者加积分
|
||||
pub async fn earn_points(
|
||||
state: &PointsState,
|
||||
tenant_id: Uuid,
|
||||
patient_id: Uuid,
|
||||
event_type: &str,
|
||||
operator_id: Option<Uuid>,
|
||||
) -> PointsResult<PointsTransactionResp> {
|
||||
// 1. 查找匹配规则
|
||||
let rule = points_rule::Entity::find()
|
||||
.filter(points_rule::Column::TenantId.eq(tenant_id))
|
||||
.filter(points_rule::Column::EventType.eq(event_type))
|
||||
.filter(points_rule::Column::IsActive.eq(true))
|
||||
.filter(points_rule::Column::DeletedAt.is_null())
|
||||
.one(&state.db)
|
||||
.await?
|
||||
.ok_or_else(|| PointsError::Validation(format!("无匹配的积分规则: {}", event_type)))?;
|
||||
|
||||
// 2. 先获取/创建账户(需要 account_id 来做日上限查询)
|
||||
let acc = get_or_create_account(&state.db, tenant_id, patient_id).await?;
|
||||
|
||||
// 3. 检查每日上限(用 account.id 而非 patient_id)
|
||||
if rule.daily_cap > 0 {
|
||||
let today = Utc::now().date_naive();
|
||||
let today_start = today.and_hms_opt(0, 0, 0).unwrap().and_utc();
|
||||
let earned_today: i32 = points_transaction::Entity::find()
|
||||
.filter(points_transaction::Column::TenantId.eq(tenant_id))
|
||||
.filter(points_transaction::Column::AccountId.eq(acc.id))
|
||||
.filter(points_transaction::Column::TransactionType.eq("earn"))
|
||||
.filter(points_transaction::Column::RuleId.eq(rule.id))
|
||||
.filter(points_transaction::Column::CreatedAt.gte(today_start))
|
||||
.all(&state.db)
|
||||
.await?
|
||||
.iter()
|
||||
.map(|t| t.amount)
|
||||
.sum();
|
||||
|
||||
if earned_today + rule.points_value > rule.daily_cap {
|
||||
return Err(PointsError::Validation("今日该渠道积分已达上限".into()));
|
||||
}
|
||||
}
|
||||
|
||||
// 4. 在事务中执行积分获取
|
||||
let txn = state.db.begin().await?;
|
||||
// 重新读取账户以获取最新 version(事务内)
|
||||
let acc = points_account::Entity::find_by_id(acc.id)
|
||||
.one(&txn)
|
||||
.await?
|
||||
.ok_or(PointsError::Validation("积分账户不存在".into()))?;
|
||||
|
||||
// 使用数据库级 CAS 防止并发赚取导致余额丢失
|
||||
let now = Utc::now();
|
||||
let expires_at = now + Duration::days(365); // 12 个月过期
|
||||
|
||||
// 写入流水
|
||||
let txn_record = points_transaction::ActiveModel {
|
||||
id: Set(Uuid::now_v7()),
|
||||
tenant_id: Set(tenant_id),
|
||||
account_id: Set(acc.id),
|
||||
transaction_type: Set("earn".to_string()),
|
||||
amount: Set(rule.points_value),
|
||||
remaining_amount: Set(rule.points_value),
|
||||
status: Set("active".to_string()),
|
||||
expires_at: Set(Some(expires_at)),
|
||||
balance_after: Set(acc.balance + rule.points_value),
|
||||
rule_id: Set(Some(rule.id)),
|
||||
order_id: Set(None),
|
||||
description: Set(Some(format!("{}: +{}", rule.name, rule.points_value))),
|
||||
created_at: Set(now),
|
||||
updated_at: Set(now),
|
||||
created_by: Set(operator_id),
|
||||
updated_by: Set(operator_id),
|
||||
deleted_at: Set(None),
|
||||
version: Set(1),
|
||||
};
|
||||
let inserted = txn_record.insert(&txn).await?;
|
||||
|
||||
// CAS 更新账户余额:基于 version 字段防止并发覆盖
|
||||
let cas_result = points_account::Entity::update_many()
|
||||
.col_expr(
|
||||
points_account::Column::Balance,
|
||||
Expr::col(points_account::Column::Balance).add(rule.points_value),
|
||||
)
|
||||
.col_expr(
|
||||
points_account::Column::TotalEarned,
|
||||
Expr::col(points_account::Column::TotalEarned).add(rule.points_value),
|
||||
)
|
||||
.col_expr(points_account::Column::UpdatedAt, Expr::value(now))
|
||||
.col_expr(points_account::Column::UpdatedBy, Expr::value(operator_id))
|
||||
.col_expr(
|
||||
points_account::Column::Version,
|
||||
Expr::col(points_account::Column::Version).add(1),
|
||||
)
|
||||
.filter(points_account::Column::Id.eq(acc.id))
|
||||
.filter(points_account::Column::Version.eq(acc.version))
|
||||
.exec(&txn)
|
||||
.await?;
|
||||
if cas_result.rows_affected == 0 {
|
||||
txn.rollback().await?;
|
||||
return Err(PointsError::VersionMismatch);
|
||||
}
|
||||
|
||||
txn.commit().await?;
|
||||
|
||||
audit_service::record(
|
||||
AuditLog::new(tenant_id, operator_id, "points.earned", "points_transaction")
|
||||
.with_resource_id(inserted.id),
|
||||
&state.db,
|
||||
).await;
|
||||
|
||||
state.event_bus.publish(
|
||||
DomainEvent::new(crate::event::POINTS_EARNED, tenant_id, erp_core::events::build_event_payload(serde_json::json!({
|
||||
"transaction_id": inserted.id, "account_id": inserted.account_id,
|
||||
"amount": inserted.amount, "balance_after": inserted.balance_after,
|
||||
}))),
|
||||
&state.db,
|
||||
).await;
|
||||
|
||||
Ok(PointsTransactionResp {
|
||||
id: inserted.id,
|
||||
account_id: inserted.account_id,
|
||||
transaction_type: inserted.transaction_type,
|
||||
amount: inserted.amount,
|
||||
remaining_amount: inserted.remaining_amount,
|
||||
status: inserted.status,
|
||||
expires_at: inserted.expires_at,
|
||||
balance_after: inserted.balance_after,
|
||||
description: inserted.description,
|
||||
created_at: inserted.created_at,
|
||||
})
|
||||
}
|
||||
|
||||
// ---------------------------------------------------------------------------
|
||||
// 积分流水查询
|
||||
// ---------------------------------------------------------------------------
|
||||
|
||||
/// 查询积分流水(分页)
|
||||
pub async fn list_transactions(
|
||||
state: &PointsState,
|
||||
tenant_id: Uuid,
|
||||
patient_id: Uuid,
|
||||
page: u64,
|
||||
page_size: u64,
|
||||
) -> PointsResult<PaginatedResponse<PointsTransactionResp>> {
|
||||
let acc = get_or_create_account(&state.db, tenant_id, patient_id).await?;
|
||||
let limit = page_size.min(100);
|
||||
let offset = page.saturating_sub(1) * limit;
|
||||
|
||||
let query = points_transaction::Entity::find()
|
||||
.filter(points_transaction::Column::TenantId.eq(tenant_id))
|
||||
.filter(points_transaction::Column::AccountId.eq(acc.id));
|
||||
|
||||
let total = query.clone().count(&state.db).await?;
|
||||
let models = query
|
||||
.order_by_desc(points_transaction::Column::CreatedAt)
|
||||
.offset(offset)
|
||||
.limit(limit)
|
||||
.all(&state.db)
|
||||
.await?;
|
||||
|
||||
let total_pages = total.div_ceil(limit.max(1));
|
||||
let data = models.into_iter().map(|m| PointsTransactionResp {
|
||||
id: m.id, account_id: m.account_id, transaction_type: m.transaction_type,
|
||||
amount: m.amount, remaining_amount: m.remaining_amount,
|
||||
status: m.status, expires_at: m.expires_at,
|
||||
balance_after: m.balance_after, description: m.description,
|
||||
created_at: m.created_at,
|
||||
}).collect();
|
||||
|
||||
Ok(PaginatedResponse { data, total, page, page_size: limit, total_pages })
|
||||
}
|
||||
|
||||
// ---------------------------------------------------------------------------
|
||||
// 积分统计 — 管理端
|
||||
// ---------------------------------------------------------------------------
|
||||
|
||||
/// 管理端:积分统计汇总
|
||||
pub async fn get_points_statistics(
|
||||
state: &PointsState,
|
||||
tenant_id: Uuid,
|
||||
) -> PointsResult<PointsStatisticsResp> {
|
||||
use sea_orm::FromQueryResult;
|
||||
|
||||
#[derive(Debug, FromQueryResult)]
|
||||
struct AggRow {
|
||||
total_issued: Option<i64>,
|
||||
total_spent: Option<i64>,
|
||||
total_expired: Option<i64>,
|
||||
active_accounts: Option<i64>,
|
||||
}
|
||||
|
||||
#[derive(Debug, FromQueryResult)]
|
||||
struct TopEarnerRow {
|
||||
id: Uuid,
|
||||
patient_id: Uuid,
|
||||
total_earned: Option<i32>,
|
||||
}
|
||||
|
||||
// 聚合查询:总发放/总消费/总过期/活跃账户数
|
||||
let agg_sql = r#"
|
||||
SELECT
|
||||
COALESCE(SUM(total_earned), 0) AS total_issued,
|
||||
COALESCE(SUM(total_spent), 0) AS total_spent,
|
||||
COALESCE(SUM(total_expired), 0) AS total_expired,
|
||||
COUNT(*) AS active_accounts
|
||||
FROM points_account
|
||||
WHERE tenant_id = $1 AND deleted_at IS NULL
|
||||
"#;
|
||||
let agg = AggRow::find_by_statement(
|
||||
sea_orm::Statement::from_sql_and_values(
|
||||
sea_orm::DatabaseBackend::Postgres,
|
||||
agg_sql,
|
||||
[tenant_id.into()],
|
||||
),
|
||||
)
|
||||
.one(&state.db)
|
||||
.await?
|
||||
.unwrap_or(AggRow {
|
||||
total_issued: Some(0),
|
||||
total_spent: Some(0),
|
||||
total_expired: Some(0),
|
||||
active_accounts: Some(0),
|
||||
});
|
||||
|
||||
// Top 10 积分获取者
|
||||
let top_sql = r#"
|
||||
SELECT id, patient_id, total_earned
|
||||
FROM points_account
|
||||
WHERE tenant_id = $1 AND deleted_at IS NULL
|
||||
ORDER BY total_earned DESC
|
||||
LIMIT 10
|
||||
"#;
|
||||
let top_rows = TopEarnerRow::find_by_statement(
|
||||
sea_orm::Statement::from_sql_and_values(
|
||||
sea_orm::DatabaseBackend::Postgres,
|
||||
top_sql,
|
||||
[tenant_id.into()],
|
||||
),
|
||||
)
|
||||
.all(&state.db)
|
||||
.await?;
|
||||
|
||||
let top_earners = top_rows.into_iter().map(|r| TopEarner {
|
||||
account_id: r.id,
|
||||
patient_id: r.patient_id,
|
||||
total_earned: r.total_earned.unwrap_or(0),
|
||||
}).collect();
|
||||
|
||||
Ok(PointsStatisticsResp {
|
||||
total_issued: agg.total_issued.unwrap_or(0),
|
||||
total_spent: agg.total_spent.unwrap_or(0),
|
||||
total_expired: agg.total_expired.unwrap_or(0),
|
||||
active_accounts: agg.active_accounts.unwrap_or(0),
|
||||
top_earners,
|
||||
})
|
||||
}
|
||||
|
||||
// ---------------------------------------------------------------------------
|
||||
// 积分过期清理
|
||||
// ---------------------------------------------------------------------------
|
||||
|
||||
/// 扫描已过期的 earn 交易,扣减账户余额,更新 total_expired。
|
||||
/// 返回处理的过期交易数量。
|
||||
pub async fn expire_points(
|
||||
db: &sea_orm::DatabaseConnection,
|
||||
event_bus: &erp_core::events::EventBus,
|
||||
) -> PointsResult<u64> {
|
||||
let now = Utc::now();
|
||||
|
||||
// 查找所有已过期但未标记 expired 的 earn 交易
|
||||
let expired_txns: Vec<points_transaction::Model> = points_transaction::Entity::find()
|
||||
.filter(points_transaction::Column::TransactionType.eq("earn"))
|
||||
.filter(points_transaction::Column::Status.eq("active"))
|
||||
.filter(points_transaction::Column::ExpiresAt.is_not_null())
|
||||
.filter(points_transaction::Column::ExpiresAt.lt(now))
|
||||
.filter(points_transaction::Column::DeletedAt.is_null())
|
||||
.filter(points_transaction::Column::RemainingAmount.gt(0))
|
||||
.all(db)
|
||||
.await?;
|
||||
|
||||
if expired_txns.is_empty() {
|
||||
return Ok(0);
|
||||
}
|
||||
|
||||
let tenant_id = expired_txns.first().map(|t| t.tenant_id).unwrap_or_default();
|
||||
|
||||
let mut processed: u64 = 0;
|
||||
|
||||
for txn in expired_txns {
|
||||
let txn_id = txn.id;
|
||||
let account_id = txn.account_id;
|
||||
let remaining = txn.remaining_amount;
|
||||
|
||||
let txn_result = db
|
||||
.transaction::<_, (), PointsError>(|txn_db| {
|
||||
Box::pin(async move {
|
||||
// 标记交易为 expired
|
||||
let mut active_txn: points_transaction::ActiveModel = txn.into();
|
||||
active_txn.status = Set("expired".to_string());
|
||||
active_txn.remaining_amount = Set(0);
|
||||
active_txn.version = Set(active_txn.version.unwrap() + 1);
|
||||
active_txn.updated_at = Set(Utc::now());
|
||||
active_txn.update(txn_db).await?;
|
||||
|
||||
// 扣减账户余额,更新 total_expired
|
||||
let account = points_account::Entity::find_by_id(account_id)
|
||||
.one(txn_db)
|
||||
.await?
|
||||
.ok_or_else(|| PointsError::Validation("积分账户不存在".to_string()))?;
|
||||
|
||||
let new_balance = (account.balance - remaining).max(0);
|
||||
let new_expired = account.total_expired + remaining;
|
||||
|
||||
let mut active_account: points_account::ActiveModel = account.into();
|
||||
active_account.balance = Set(new_balance);
|
||||
active_account.total_expired = Set(new_expired);
|
||||
active_account.version = Set(active_account.version.unwrap() + 1);
|
||||
active_account.updated_at = Set(Utc::now());
|
||||
let expected_ver: i32 = match &active_account.version {
|
||||
sea_orm::ActiveValue::Unchanged(v) | sea_orm::ActiveValue::Set(v) => *v,
|
||||
_ => 0,
|
||||
};
|
||||
let _next_ver = erp_core::error::check_version(expected_ver, expected_ver)?;
|
||||
active_account.update(txn_db).await?;
|
||||
|
||||
Ok(())
|
||||
})
|
||||
})
|
||||
.await;
|
||||
|
||||
match txn_result {
|
||||
Ok(()) => {
|
||||
processed += 1;
|
||||
tracing::debug!(txn_id = %txn_id, remaining = remaining, "积分过期处理完成");
|
||||
}
|
||||
Err(e) => {
|
||||
tracing::warn!(txn_id = %txn_id, error = %e, "积分过期处理失败,跳过");
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
if processed > 0 {
|
||||
tracing::info!(count = processed, "积分过期清理完成");
|
||||
let event = erp_core::events::DomainEvent::new(
|
||||
crate::event::POINTS_EXPIRED,
|
||||
tenant_id,
|
||||
erp_core::events::build_event_payload(serde_json::json!({ "expired_count": processed })),
|
||||
);
|
||||
event_bus.publish(event, db).await;
|
||||
}
|
||||
|
||||
Ok(processed)
|
||||
}
|
||||
8
crates/erp-points/src/state.rs
Normal file
8
crates/erp-points/src/state.rs
Normal file
@@ -0,0 +1,8 @@
|
||||
use erp_core::events::EventBus;
|
||||
use sea_orm::DatabaseConnection;
|
||||
|
||||
#[derive(Clone)]
|
||||
pub struct PointsState {
|
||||
pub db: DatabaseConnection,
|
||||
pub event_bus: EventBus,
|
||||
}
|
||||
Reference in New Issue
Block a user