Compare commits
4 Commits
3cba699ca0
...
75bf900950
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
75bf900950 | ||
|
|
6d66a392db | ||
|
|
81dd3d2bda | ||
|
|
758bc210e1 |
@@ -27,6 +27,7 @@ export default defineAppConfig({
|
||||
'followup/index', 'followup/detail/index',
|
||||
'report/index', 'report/detail/index',
|
||||
'alerts/index', 'alerts/detail/index',
|
||||
'action-inbox/index',
|
||||
'dialysis/index', 'dialysis/detail/index', 'dialysis/create/index',
|
||||
'prescription/index', 'prescription/detail/index', 'prescription/create/index',
|
||||
],
|
||||
|
||||
182
apps/miniprogram/src/pages/doctor/action-inbox/index.scss
Normal file
182
apps/miniprogram/src/pages/doctor/action-inbox/index.scss
Normal file
@@ -0,0 +1,182 @@
|
||||
.action-inbox-page {
|
||||
min-height: 100vh;
|
||||
background: #f5f5f5;
|
||||
}
|
||||
|
||||
.inbox-tabs {
|
||||
display: flex;
|
||||
background: white;
|
||||
padding: 0 16px;
|
||||
border-bottom: 1px solid #eee;
|
||||
|
||||
.inbox-tab {
|
||||
flex: 1;
|
||||
text-align: center;
|
||||
padding: 12px 0;
|
||||
|
||||
&.active {
|
||||
.inbox-tab-text {
|
||||
color: #C4623A;
|
||||
font-weight: 600;
|
||||
position: relative;
|
||||
|
||||
&::after {
|
||||
content: '';
|
||||
position: absolute;
|
||||
bottom: -12px;
|
||||
left: 30%;
|
||||
right: 30%;
|
||||
height: 3px;
|
||||
background: #C4623A;
|
||||
border-radius: 2px;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
.inbox-tab-text {
|
||||
font-size: 14px;
|
||||
color: #666;
|
||||
}
|
||||
}
|
||||
|
||||
.inbox-list {
|
||||
height: calc(100vh - 50px);
|
||||
padding: 12px;
|
||||
}
|
||||
|
||||
.inbox-card {
|
||||
background: white;
|
||||
border-radius: 12px;
|
||||
padding: 14px 16px;
|
||||
margin-bottom: 10px;
|
||||
|
||||
.inbox-card-header {
|
||||
display: flex;
|
||||
align-items: center;
|
||||
gap: 8px;
|
||||
margin-bottom: 6px;
|
||||
}
|
||||
|
||||
.inbox-type-tag {
|
||||
color: white;
|
||||
font-size: 10px;
|
||||
padding: 2px 6px;
|
||||
border-radius: 4px;
|
||||
flex-shrink: 0;
|
||||
}
|
||||
|
||||
.inbox-card-title {
|
||||
font-size: 14px;
|
||||
font-weight: 500;
|
||||
}
|
||||
|
||||
.inbox-card-desc {
|
||||
font-size: 12px;
|
||||
color: #999;
|
||||
}
|
||||
}
|
||||
|
||||
.inbox-empty {
|
||||
text-align: center;
|
||||
padding: 80px 0;
|
||||
|
||||
.inbox-empty-text {
|
||||
font-size: 14px;
|
||||
color: #999;
|
||||
}
|
||||
}
|
||||
|
||||
// 半屏弹窗
|
||||
.half-screen-dialog {
|
||||
position: fixed;
|
||||
bottom: 0;
|
||||
left: 0;
|
||||
right: 0;
|
||||
max-height: 70vh;
|
||||
background: white;
|
||||
border-radius: 16px 16px 0 0;
|
||||
z-index: 1000;
|
||||
overflow-y: auto;
|
||||
|
||||
.dialog-header {
|
||||
display: flex;
|
||||
justify-content: space-between;
|
||||
align-items: center;
|
||||
padding: 16px 20px;
|
||||
border-bottom: 1px solid #f0f0f0;
|
||||
|
||||
.dialog-title {
|
||||
font-size: 16px;
|
||||
font-weight: 600;
|
||||
}
|
||||
|
||||
.dialog-close {
|
||||
font-size: 13px;
|
||||
color: #999;
|
||||
}
|
||||
}
|
||||
|
||||
.dialog-body {
|
||||
padding: 16px 20px;
|
||||
}
|
||||
|
||||
.dialog-patient {
|
||||
font-size: 13px;
|
||||
color: #666;
|
||||
display: block;
|
||||
margin-bottom: 12px;
|
||||
}
|
||||
|
||||
.thread-item {
|
||||
display: flex;
|
||||
align-items: flex-start;
|
||||
gap: 10px;
|
||||
padding: 6px 0;
|
||||
}
|
||||
|
||||
.thread-dot {
|
||||
width: 8px;
|
||||
height: 8px;
|
||||
border-radius: 50%;
|
||||
margin-top: 4px;
|
||||
flex-shrink: 0;
|
||||
|
||||
&.completed { background: #52c41a; }
|
||||
&.in_progress { background: #faad14; }
|
||||
&.pending { background: #d9d9d9; }
|
||||
&.dismissed { background: #ff4d4f; }
|
||||
}
|
||||
|
||||
.thread-content {
|
||||
.thread-label {
|
||||
font-size: 13px;
|
||||
display: block;
|
||||
}
|
||||
|
||||
.thread-time {
|
||||
font-size: 11px;
|
||||
color: #999;
|
||||
}
|
||||
}
|
||||
|
||||
.dialog-actions {
|
||||
display: flex;
|
||||
gap: 8px;
|
||||
padding: 12px 20px 20px;
|
||||
border-top: 1px solid #f0f0f0;
|
||||
|
||||
.action-btn {
|
||||
flex: 1;
|
||||
text-align: center;
|
||||
padding: 10px;
|
||||
border-radius: 8px;
|
||||
font-size: 14px;
|
||||
font-weight: 500;
|
||||
|
||||
&.primary { background: #C4623A; color: white; }
|
||||
&.danger { background: #ff4d4f; color: white; }
|
||||
&.default { background: #f5f5f5; color: #666; }
|
||||
}
|
||||
}
|
||||
}
|
||||
228
apps/miniprogram/src/pages/doctor/action-inbox/index.tsx
Normal file
228
apps/miniprogram/src/pages/doctor/action-inbox/index.tsx
Normal file
@@ -0,0 +1,228 @@
|
||||
import React, { useState, useCallback, useRef } from 'react';
|
||||
import { View, Text, ScrollView } from '@tarojs/components';
|
||||
import Taro, { useDidShow, usePullDownRefresh } from '@tarojs/taro';
|
||||
import {
|
||||
listActionItems,
|
||||
getActionThread,
|
||||
type ActionItem,
|
||||
type ThreadResponse,
|
||||
} from '@/services/action-inbox';
|
||||
import Loading from '@/components/Loading';
|
||||
import './index.scss';
|
||||
|
||||
const TYPE_LABEL: Record<string, string> = {
|
||||
ai_suggestion: 'AI建议',
|
||||
alert: '告警',
|
||||
followup: '随访',
|
||||
data_anomaly: '异常',
|
||||
};
|
||||
|
||||
const TYPE_COLOR: Record<string, string> = {
|
||||
ai_suggestion: '#722ed1',
|
||||
alert: '#f5222d',
|
||||
followup: '#1890ff',
|
||||
data_anomaly: '#fa8c16',
|
||||
};
|
||||
|
||||
const STATUS_TABS = [
|
||||
{ key: '', label: '全部' },
|
||||
{ key: 'pending', label: '待处理' },
|
||||
{ key: 'in_progress', label: '进行中' },
|
||||
{ key: 'completed', label: '已完成' },
|
||||
];
|
||||
|
||||
export default function ActionInboxPage() {
|
||||
const [items, setItems] = useState<ActionItem[]>([]);
|
||||
const [total, setTotal] = useState(0);
|
||||
const [_page, setPage] = useState(1);
|
||||
const [loading, setLoading] = useState(false);
|
||||
const [activeTab, setActiveTab] = useState('');
|
||||
const [threadData, setThreadData] = useState<ThreadResponse | null>(null);
|
||||
const [showDetail, setShowDetail] = useState(false);
|
||||
const loadingRef = useRef(false);
|
||||
|
||||
const fetchItems = useCallback(
|
||||
async (pageNum: number, status: string, isRefresh = false) => {
|
||||
if (loadingRef.current) return;
|
||||
loadingRef.current = true;
|
||||
setLoading(true);
|
||||
try {
|
||||
const resp = await listActionItems({
|
||||
page: pageNum,
|
||||
page_size: 20,
|
||||
status: status || undefined,
|
||||
});
|
||||
const list = resp.data || [];
|
||||
if (isRefresh) {
|
||||
setItems(list);
|
||||
} else {
|
||||
setItems((prev) => [...prev, ...list]);
|
||||
}
|
||||
setTotal(resp.total);
|
||||
setPage(pageNum);
|
||||
} catch {
|
||||
Taro.showToast({ title: '加载失败', icon: 'none' });
|
||||
} finally {
|
||||
setLoading(false);
|
||||
loadingRef.current = false;
|
||||
}
|
||||
},
|
||||
[],
|
||||
);
|
||||
|
||||
useDidShow(() => {
|
||||
Taro.setNavigationBarTitle({ title: '待办事项' });
|
||||
fetchItems(1, activeTab, true);
|
||||
});
|
||||
|
||||
usePullDownRefresh(() => {
|
||||
fetchItems(1, activeTab, true).then(() =>
|
||||
Taro.stopPullDownRefresh(),
|
||||
);
|
||||
});
|
||||
|
||||
const handleTabChange = (key: string) => {
|
||||
setActiveTab(key);
|
||||
fetchItems(1, key, true);
|
||||
};
|
||||
|
||||
const handleItemClick = async (item: ActionItem) => {
|
||||
try {
|
||||
const data = await getActionThread(item.source_ref);
|
||||
setThreadData(data);
|
||||
setShowDetail(true);
|
||||
} catch {
|
||||
Taro.showToast({ title: '加载失败', icon: 'none' });
|
||||
}
|
||||
};
|
||||
|
||||
const handleAction = async (action: {
|
||||
key: string;
|
||||
api_endpoint?: string;
|
||||
}) => {
|
||||
if (!action.api_endpoint || !threadData) return;
|
||||
try {
|
||||
await Taro.request({
|
||||
url: `${process.env.TARO_APP_API_URL}${action.api_endpoint}`,
|
||||
method: 'POST',
|
||||
header: { 'Content-Type': 'application/json' },
|
||||
data: { action: action.key },
|
||||
});
|
||||
Taro.showToast({ title: '操作成功', icon: 'success' });
|
||||
setShowDetail(false);
|
||||
fetchItems(1, activeTab, true);
|
||||
} catch {
|
||||
Taro.showToast({ title: '操作失败', icon: 'none' });
|
||||
}
|
||||
};
|
||||
|
||||
return (
|
||||
<View className="action-inbox-page">
|
||||
<View className="inbox-tabs">
|
||||
{STATUS_TABS.map((tab) => (
|
||||
<View
|
||||
key={tab.key}
|
||||
className={`inbox-tab ${activeTab === tab.key ? 'active' : ''}`}
|
||||
onClick={() => handleTabChange(tab.key)}
|
||||
>
|
||||
<Text
|
||||
className={`inbox-tab-text ${activeTab === tab.key ? 'active' : ''}`}
|
||||
>
|
||||
{tab.label}
|
||||
</Text>
|
||||
</View>
|
||||
))}
|
||||
</View>
|
||||
|
||||
{items.length === 0 && !loading ? (
|
||||
<View className="inbox-empty">
|
||||
<Text className="inbox-empty-text">暂无待办事项</Text>
|
||||
</View>
|
||||
) : (
|
||||
<ScrollView scrollY className="inbox-list">
|
||||
{items.map((item) => (
|
||||
<View
|
||||
key={item.id}
|
||||
className="inbox-card"
|
||||
onClick={() => handleItemClick(item)}
|
||||
>
|
||||
<View className="inbox-card-header">
|
||||
<Text
|
||||
className="inbox-type-tag"
|
||||
style={{
|
||||
background: TYPE_COLOR[item.action_type] || '#999',
|
||||
}}
|
||||
>
|
||||
{TYPE_LABEL[item.action_type] || '未知'}
|
||||
</Text>
|
||||
<Text className="inbox-card-title">{item.title}</Text>
|
||||
</View>
|
||||
<Text className="inbox-card-desc">
|
||||
{item.patient_name} ·{' '}
|
||||
{new Date(item.created_at).toLocaleDateString('zh-CN')}
|
||||
</Text>
|
||||
</View>
|
||||
))}
|
||||
{loading && <Loading />}
|
||||
{!loading && items.length >= total && total > 0 && (
|
||||
<Loading text="没有更多了" />
|
||||
)}
|
||||
</ScrollView>
|
||||
)}
|
||||
|
||||
{showDetail && threadData && (
|
||||
<View className="half-screen-dialog">
|
||||
<View className="dialog-header">
|
||||
<Text className="dialog-title">
|
||||
{threadData.action_item.title}
|
||||
</Text>
|
||||
<Text
|
||||
className="dialog-close"
|
||||
onClick={() => setShowDetail(false)}
|
||||
>
|
||||
收起
|
||||
</Text>
|
||||
</View>
|
||||
<View className="dialog-body">
|
||||
<Text className="dialog-patient">
|
||||
{threadData.action_item.patient_name} ·{' '}
|
||||
{threadData.action_item.priority === 'urgent'
|
||||
? '紧急'
|
||||
: threadData.action_item.priority === 'high'
|
||||
? '高'
|
||||
: '中'}
|
||||
</Text>
|
||||
<View className="thread-timeline">
|
||||
{threadData.thread.map((evt, idx) => (
|
||||
<View key={idx} className="thread-item">
|
||||
<View className={`thread-dot ${evt.status}`} />
|
||||
<View className="thread-content">
|
||||
<Text className="thread-label">{evt.label}</Text>
|
||||
{evt.timestamp && (
|
||||
<Text className="thread-time">
|
||||
{new Date(evt.timestamp).toLocaleDateString('zh-CN')}
|
||||
</Text>
|
||||
)}
|
||||
</View>
|
||||
</View>
|
||||
))}
|
||||
</View>
|
||||
</View>
|
||||
{threadData.available_actions.length > 0 && (
|
||||
<View className="dialog-actions">
|
||||
{threadData.available_actions.map((action) => (
|
||||
<View
|
||||
key={action.key}
|
||||
className={`action-btn ${action.variant}`}
|
||||
onClick={() => handleAction(action)}
|
||||
>
|
||||
{action.label}
|
||||
</View>
|
||||
))}
|
||||
</View>
|
||||
)}
|
||||
</View>
|
||||
)}
|
||||
</View>
|
||||
);
|
||||
}
|
||||
60
apps/miniprogram/src/services/action-inbox.ts
Normal file
60
apps/miniprogram/src/services/action-inbox.ts
Normal file
@@ -0,0 +1,60 @@
|
||||
import { api } from './request';
|
||||
|
||||
export interface ActionItem {
|
||||
id: string;
|
||||
action_type: string;
|
||||
priority: string;
|
||||
status: string;
|
||||
title: string;
|
||||
summary: string;
|
||||
patient_id: string;
|
||||
patient_name: string;
|
||||
source_ref: string;
|
||||
created_at: string;
|
||||
updated_at: string;
|
||||
}
|
||||
|
||||
export interface ThreadEvent {
|
||||
step: string;
|
||||
label: string;
|
||||
status: string;
|
||||
detail?: string;
|
||||
timestamp?: string;
|
||||
link_to?: string;
|
||||
}
|
||||
|
||||
export interface ActionDef {
|
||||
key: string;
|
||||
label: string;
|
||||
variant: string;
|
||||
api_endpoint?: string;
|
||||
}
|
||||
|
||||
export interface ThreadResponse {
|
||||
action_item: ActionItem;
|
||||
thread: ThreadEvent[];
|
||||
available_actions: ActionDef[];
|
||||
}
|
||||
|
||||
interface PaginatedData {
|
||||
data: ActionItem[];
|
||||
total: number;
|
||||
}
|
||||
|
||||
export async function listActionItems(params?: {
|
||||
status?: string;
|
||||
type?: string;
|
||||
page?: number;
|
||||
page_size?: number;
|
||||
}) {
|
||||
return api.get<PaginatedData>(
|
||||
'/health/action-inbox',
|
||||
params as Record<string, string | number | undefined>,
|
||||
);
|
||||
}
|
||||
|
||||
export async function getActionThread(sourceRef: string) {
|
||||
return api.get<ThreadResponse>(
|
||||
`/health/action-inbox/${encodeURIComponent(sourceRef)}/thread`,
|
||||
);
|
||||
}
|
||||
@@ -48,6 +48,8 @@ const AlertList = lazy(() => import('./pages/health/AlertList'));
|
||||
const AlertDashboard = lazy(() => import('./pages/health/AlertDashboard'));
|
||||
const AlertRuleList = lazy(() => import('./pages/health/AlertRuleList'));
|
||||
const DeviceManage = lazy(() => import('./pages/health/DeviceManage'));
|
||||
const DialysisManageList = lazy(() => import('./pages/health/DialysisManageList'));
|
||||
const ActionInbox = lazy(() => import('./pages/health/ActionInbox'));
|
||||
|
||||
// 内容管理
|
||||
const ArticleManageList = lazy(() => import('./pages/health/ArticleManageList'));
|
||||
@@ -254,6 +256,8 @@ export default function App() {
|
||||
<Route path="/health/alert-dashboard" element={<AlertDashboard />} />
|
||||
<Route path="/health/alert-rules" element={<AlertRuleList />} />
|
||||
<Route path="/health/devices" element={<DeviceManage />} />
|
||||
<Route path="/health/dialysis" element={<DialysisManageList />} />
|
||||
<Route path="/health/action-inbox" element={<ActionInbox />} />
|
||||
{/* 内容管理 */}
|
||||
<Route path="/health/articles" element={<ArticleManageList />} />
|
||||
<Route path="/health/articles/new" element={<ArticleEditor />} />
|
||||
|
||||
66
apps/web/src/api/health/actionInbox.ts
Normal file
66
apps/web/src/api/health/actionInbox.ts
Normal file
@@ -0,0 +1,66 @@
|
||||
import client from '../client';
|
||||
import type { PaginatedResponse } from '../types';
|
||||
|
||||
export type ActionType = 'ai_suggestion' | 'alert' | 'followup' | 'data_anomaly';
|
||||
export type ActionPriority = 'urgent' | 'high' | 'medium' | 'low';
|
||||
export type ActionStatus = 'pending' | 'in_progress' | 'completed' | 'dismissed';
|
||||
|
||||
export interface ActionItem {
|
||||
id: string;
|
||||
action_type: ActionType;
|
||||
priority: ActionPriority;
|
||||
status: ActionStatus;
|
||||
title: string;
|
||||
summary: string;
|
||||
patient_id: string;
|
||||
patient_name: string;
|
||||
source_ref: string;
|
||||
created_at: string;
|
||||
updated_at: string;
|
||||
}
|
||||
|
||||
export interface ThreadEvent {
|
||||
step: string;
|
||||
label: string;
|
||||
status: ActionStatus;
|
||||
detail?: string;
|
||||
timestamp?: string;
|
||||
operator?: string;
|
||||
link_to?: string;
|
||||
}
|
||||
|
||||
export interface ActionDefinition {
|
||||
key: string;
|
||||
label: string;
|
||||
variant: 'primary' | 'danger' | 'default';
|
||||
api_endpoint?: string;
|
||||
}
|
||||
|
||||
export interface ThreadResponse {
|
||||
action_item: ActionItem;
|
||||
thread: ThreadEvent[];
|
||||
available_actions: ActionDefinition[];
|
||||
}
|
||||
|
||||
export const actionInboxApi = {
|
||||
list: async (params?: {
|
||||
status?: string;
|
||||
type?: string;
|
||||
page?: number;
|
||||
page_size?: number;
|
||||
}) => {
|
||||
const { data } = await client.get<{
|
||||
success: boolean;
|
||||
data: PaginatedResponse<ActionItem>;
|
||||
}>('/health/action-inbox', { params });
|
||||
return data.data;
|
||||
},
|
||||
|
||||
getThread: async (sourceRef: string) => {
|
||||
const { data } = await client.get<{
|
||||
success: boolean;
|
||||
data: ThreadResponse;
|
||||
}>(`/health/action-inbox/${encodeURIComponent(sourceRef)}/thread`);
|
||||
return data.data;
|
||||
},
|
||||
};
|
||||
226
apps/web/src/components/ActionThreadDrawer.tsx
Normal file
226
apps/web/src/components/ActionThreadDrawer.tsx
Normal file
@@ -0,0 +1,226 @@
|
||||
import { useCallback, useEffect, useState } from 'react';
|
||||
import { Drawer, Timeline, Button, Spin, Result, Space, Tag } from 'antd';
|
||||
import {
|
||||
CheckCircleOutlined,
|
||||
ClockCircleOutlined,
|
||||
MinusCircleOutlined,
|
||||
CloseCircleOutlined,
|
||||
UserOutlined,
|
||||
} from '@ant-design/icons';
|
||||
import { useNavigate } from 'react-router-dom';
|
||||
import {
|
||||
actionInboxApi,
|
||||
type ActionItem,
|
||||
type ThreadResponse,
|
||||
type ActionPriority,
|
||||
} from '../api/health/actionInbox';
|
||||
import client from '../api/client';
|
||||
|
||||
interface Props {
|
||||
open: boolean;
|
||||
item: ActionItem | null;
|
||||
onClose: () => void;
|
||||
onActionComplete?: () => void;
|
||||
}
|
||||
|
||||
const PRIORITY_COLOR: Record<ActionPriority, string> = {
|
||||
urgent: 'red',
|
||||
high: 'orange',
|
||||
medium: 'blue',
|
||||
low: 'default',
|
||||
};
|
||||
|
||||
const PRIORITY_LABEL: Record<ActionPriority, string> = {
|
||||
urgent: '紧急',
|
||||
high: '高',
|
||||
medium: '中',
|
||||
low: '低',
|
||||
};
|
||||
|
||||
export default function ActionThreadDrawer({
|
||||
open,
|
||||
item,
|
||||
onClose,
|
||||
onActionComplete,
|
||||
}: Props) {
|
||||
const navigate = useNavigate();
|
||||
const [loading, setLoading] = useState(false);
|
||||
const [error, setError] = useState<string | null>(null);
|
||||
const [threadData, setThreadData] = useState<ThreadResponse | null>(null);
|
||||
const [actionLoading, setActionLoading] = useState<string | null>(null);
|
||||
|
||||
const fetchThread = useCallback(async () => {
|
||||
if (!item) return;
|
||||
setLoading(true);
|
||||
setError(null);
|
||||
try {
|
||||
const data = await actionInboxApi.getThread(item.source_ref);
|
||||
setThreadData(data);
|
||||
} catch (e: unknown) {
|
||||
const msg = e instanceof Error ? e.message : '获取线程失败';
|
||||
setError(msg);
|
||||
} finally {
|
||||
setLoading(false);
|
||||
}
|
||||
}, [item]);
|
||||
|
||||
useEffect(() => {
|
||||
if (open && item) fetchThread();
|
||||
if (!open) setThreadData(null);
|
||||
}, [open, item, fetchThread]);
|
||||
|
||||
const handleAction = async (endpoint: string, key: string) => {
|
||||
setActionLoading(key);
|
||||
try {
|
||||
await client.post(endpoint, { action: key });
|
||||
onActionComplete?.();
|
||||
fetchThread();
|
||||
} catch {
|
||||
// 全局拦截器处理
|
||||
} finally {
|
||||
setActionLoading(null);
|
||||
}
|
||||
};
|
||||
|
||||
const handleLinkClick = (linkTo?: string) => {
|
||||
if (linkTo) {
|
||||
onClose();
|
||||
navigate(linkTo);
|
||||
}
|
||||
};
|
||||
|
||||
const timelineDot = (status: string) => {
|
||||
switch (status) {
|
||||
case 'completed':
|
||||
return <CheckCircleOutlined style={{ color: '#52c41a' }} />;
|
||||
case 'in_progress':
|
||||
return <ClockCircleOutlined style={{ color: '#faad14' }} />;
|
||||
case 'dismissed':
|
||||
return <CloseCircleOutlined style={{ color: '#ff4d4f' }} />;
|
||||
default:
|
||||
return <MinusCircleOutlined style={{ color: '#d9d9d9' }} />;
|
||||
}
|
||||
};
|
||||
|
||||
return (
|
||||
<Drawer
|
||||
title={null}
|
||||
open={open}
|
||||
onClose={onClose}
|
||||
width={480}
|
||||
styles={{ body: { padding: 0 } }}
|
||||
>
|
||||
{loading && (
|
||||
<div style={{ padding: 40, textAlign: 'center' }}>
|
||||
<Spin size="large" />
|
||||
</div>
|
||||
)}
|
||||
|
||||
{error && (
|
||||
<div style={{ padding: 24 }}>
|
||||
<Result status="error" title="加载失败" subTitle={error} />
|
||||
</div>
|
||||
)}
|
||||
|
||||
{threadData && !loading && (
|
||||
<>
|
||||
<div
|
||||
style={{
|
||||
padding: '16px 24px',
|
||||
borderBottom: '1px solid #f0f0f0',
|
||||
}}
|
||||
>
|
||||
<div style={{ fontSize: 16, fontWeight: 600 }}>
|
||||
{threadData.action_item.title}
|
||||
</div>
|
||||
<div
|
||||
style={{ marginTop: 4, color: '#8c8c8c', fontSize: 13 }}
|
||||
>
|
||||
<UserOutlined /> {threadData.action_item.patient_name}
|
||||
<Tag
|
||||
color={PRIORITY_COLOR[threadData.action_item.priority]}
|
||||
style={{ marginLeft: 8 }}
|
||||
>
|
||||
{PRIORITY_LABEL[threadData.action_item.priority]}
|
||||
</Tag>
|
||||
</div>
|
||||
</div>
|
||||
|
||||
<div style={{ padding: '20px 24px' }}>
|
||||
<div style={{ fontWeight: 500, marginBottom: 16 }}>
|
||||
处理进度
|
||||
</div>
|
||||
<Timeline
|
||||
items={threadData.thread.map((evt) => ({
|
||||
color:
|
||||
evt.status === 'completed'
|
||||
? 'green'
|
||||
: evt.status === 'in_progress'
|
||||
? 'blue'
|
||||
: 'gray',
|
||||
dot: timelineDot(evt.status),
|
||||
children: (
|
||||
<div>
|
||||
<div
|
||||
style={{
|
||||
fontWeight:
|
||||
evt.status === 'in_progress' ? 600 : 400,
|
||||
}}
|
||||
>
|
||||
{evt.label}
|
||||
</div>
|
||||
{evt.detail && (
|
||||
<div style={{ color: '#8c8c8c', fontSize: 12 }}>
|
||||
{evt.detail}
|
||||
</div>
|
||||
)}
|
||||
{evt.timestamp && (
|
||||
<div style={{ color: '#8c8c8c', fontSize: 12 }}>
|
||||
{new Date(evt.timestamp).toLocaleString('zh-CN')}
|
||||
</div>
|
||||
)}
|
||||
{evt.link_to && (
|
||||
<a
|
||||
onClick={() => handleLinkClick(evt.link_to)}
|
||||
style={{ fontSize: 12 }}
|
||||
>
|
||||
查看详情 →
|
||||
</a>
|
||||
)}
|
||||
</div>
|
||||
),
|
||||
}))}
|
||||
/>
|
||||
</div>
|
||||
|
||||
{threadData.available_actions.length > 0 && (
|
||||
<div
|
||||
style={{
|
||||
padding: '12px 24px',
|
||||
borderTop: '1px solid #f0f0f0',
|
||||
}}
|
||||
>
|
||||
<Space>
|
||||
{threadData.available_actions.map((action) => (
|
||||
<Button
|
||||
key={action.key}
|
||||
type={action.variant === 'primary' ? 'primary' : 'default'}
|
||||
danger={action.variant === 'danger'}
|
||||
loading={actionLoading === action.key}
|
||||
onClick={() => {
|
||||
if (action.api_endpoint) {
|
||||
handleAction(action.api_endpoint, action.key);
|
||||
}
|
||||
}}
|
||||
>
|
||||
{action.label}
|
||||
</Button>
|
||||
))}
|
||||
</Space>
|
||||
</div>
|
||||
)}
|
||||
</>
|
||||
)}
|
||||
</Drawer>
|
||||
);
|
||||
}
|
||||
@@ -1,37 +1,47 @@
|
||||
import { useEffect, useRef } from 'react';
|
||||
import { Badge, List, Popover, Button, Empty, Typography } from 'antd';
|
||||
import { useCallback, useEffect, useRef, useState } from 'react';
|
||||
import { Badge, Divider, List, Popover, Button, Empty, Typography } from 'antd';
|
||||
import { BellOutlined } from '@ant-design/icons';
|
||||
import { useNavigate } from 'react-router-dom';
|
||||
import { useMessageStore } from '../stores/message';
|
||||
import { useThemeMode } from '../hooks/useThemeMode';
|
||||
import { actionInboxApi, type ActionItem } from '../api/health/actionInbox';
|
||||
|
||||
const { Text } = Typography;
|
||||
|
||||
export default function NotificationPanel() {
|
||||
const navigate = useNavigate();
|
||||
// 使用独立 selector:数据订阅和函数引用分离,避免 effect 重复触发
|
||||
const unreadCount = useMessageStore((s) => s.unreadCount);
|
||||
const recentMessages = useMessageStore((s) => s.recentMessages);
|
||||
const markAsRead = useMessageStore((s) => s.markAsRead);
|
||||
const isDark = useThemeMode();
|
||||
const initializedRef = useRef(false);
|
||||
|
||||
const [pendingActions, setPendingActions] = useState<ActionItem[]>([]);
|
||||
|
||||
const fetchPendingActions = useCallback(async () => {
|
||||
try {
|
||||
const resp = await actionInboxApi.list({ status: 'pending', page_size: 3 });
|
||||
setPendingActions(resp.data);
|
||||
} catch {
|
||||
// 静默失败,不影响通知面板
|
||||
}
|
||||
}, []);
|
||||
|
||||
useEffect(() => {
|
||||
// 防止 StrictMode 双重 mount 和路由切换导致的重复初始化
|
||||
if (initializedRef.current) return;
|
||||
initializedRef.current = true;
|
||||
|
||||
const { fetchUnreadCount, fetchRecentMessages, connectSSE } = useMessageStore.getState();
|
||||
fetchUnreadCount();
|
||||
fetchRecentMessages();
|
||||
fetchPendingActions();
|
||||
|
||||
// SSE 实时推送,收到消息即刷新
|
||||
const disconnectSSE = connectSSE();
|
||||
|
||||
// 降级轮询(SSE 断开时兜底)
|
||||
const interval = setInterval(() => {
|
||||
fetchUnreadCount();
|
||||
fetchRecentMessages();
|
||||
fetchPendingActions();
|
||||
}, 60000);
|
||||
|
||||
return () => {
|
||||
@@ -39,7 +49,9 @@ export default function NotificationPanel() {
|
||||
disconnectSSE();
|
||||
initializedRef.current = false;
|
||||
};
|
||||
}, []);
|
||||
}, [fetchPendingActions]);
|
||||
|
||||
const totalBadge = unreadCount + pendingActions.length;
|
||||
|
||||
const content = (
|
||||
<div style={{ width: 360 }}>
|
||||
@@ -149,6 +161,52 @@ export default function NotificationPanel() {
|
||||
</Button>
|
||||
</div>
|
||||
)}
|
||||
|
||||
{/* 待办预览区域 */}
|
||||
<Divider style={{ margin: '8px 0' }} />
|
||||
<div style={{ padding: '4px 0' }}>
|
||||
<div style={{
|
||||
display: 'flex',
|
||||
justifyContent: 'space-between',
|
||||
alignItems: 'center',
|
||||
marginBottom: 4,
|
||||
}}>
|
||||
<Text strong style={{ fontSize: 13 }}>待办事项</Text>
|
||||
<Button
|
||||
type="link"
|
||||
size="small"
|
||||
onClick={() => navigate('/health/action-inbox')}
|
||||
style={{ fontSize: 12, padding: 0 }}
|
||||
>
|
||||
查看全部
|
||||
</Button>
|
||||
</div>
|
||||
{pendingActions.length === 0 ? (
|
||||
<Text type="secondary" style={{ fontSize: 12, display: 'block', textAlign: 'center', padding: '8px 0' }}>
|
||||
暂无待办
|
||||
</Text>
|
||||
) : (
|
||||
<List
|
||||
size="small"
|
||||
dataSource={pendingActions}
|
||||
renderItem={(item) => (
|
||||
<List.Item
|
||||
style={{ padding: '4px 0', cursor: 'pointer', border: 'none' }}
|
||||
onClick={() => navigate('/health/action-inbox')}
|
||||
>
|
||||
<List.Item.Meta
|
||||
title={<Text style={{ fontSize: 12 }}>{item.title}</Text>}
|
||||
description={
|
||||
<Text type="secondary" style={{ fontSize: 11 }}>
|
||||
{item.patient_name}
|
||||
</Text>
|
||||
}
|
||||
/>
|
||||
</List.Item>
|
||||
)}
|
||||
/>
|
||||
)}
|
||||
</div>
|
||||
</div>
|
||||
);
|
||||
|
||||
@@ -178,7 +236,7 @@ export default function NotificationPanel() {
|
||||
e.currentTarget.style.background = 'transparent';
|
||||
}}
|
||||
>
|
||||
<Badge count={unreadCount} size="small" offset={[4, -4]}>
|
||||
<Badge count={totalBadge} size="small" offset={[4, -4]}>
|
||||
<BellOutlined style={{
|
||||
fontSize: 16,
|
||||
color: isDark ? '#94a3b8' : '#475569',
|
||||
|
||||
170
apps/web/src/pages/health/ActionInbox.tsx
Normal file
170
apps/web/src/pages/health/ActionInbox.tsx
Normal file
@@ -0,0 +1,170 @@
|
||||
import { useCallback, useEffect, useState } from 'react';
|
||||
import { Tag, List, Badge, Tabs, Spin, Empty } from 'antd';
|
||||
import { PageContainer } from '../../components/PageContainer';
|
||||
import ActionThreadDrawer from '../../components/ActionThreadDrawer';
|
||||
import {
|
||||
actionInboxApi,
|
||||
type ActionItem,
|
||||
type ActionType,
|
||||
type ActionPriority,
|
||||
} from '../../api/health/actionInbox';
|
||||
import { formatRelative } from '../../utils/format';
|
||||
|
||||
const TYPE_CONFIG: Record<ActionType, { label: string; color: string }> = {
|
||||
ai_suggestion: { label: 'AI建议', color: '#722ed1' },
|
||||
alert: { label: '告警', color: '#f5222d' },
|
||||
followup: { label: '随访', color: '#1890ff' },
|
||||
data_anomaly: { label: '异常', color: '#fa8c16' },
|
||||
};
|
||||
|
||||
const PRIORITY_LABEL: Record<ActionPriority, string> = {
|
||||
urgent: '紧急',
|
||||
high: '高',
|
||||
medium: '中',
|
||||
low: '低',
|
||||
};
|
||||
|
||||
const PRIORITY_COLOR: Record<ActionPriority, string> = {
|
||||
urgent: 'red',
|
||||
high: 'orange',
|
||||
medium: 'blue',
|
||||
low: 'default',
|
||||
};
|
||||
|
||||
const STATUS_TABS = [
|
||||
{ key: 'all', label: '全部' },
|
||||
{ key: 'pending', label: '待处理' },
|
||||
{ key: 'in_progress', label: '进行中' },
|
||||
{ key: 'completed', label: '已完成' },
|
||||
];
|
||||
|
||||
const BADGE_STATUS: Record<string, 'error' | 'processing' | 'default'> = {
|
||||
pending: 'error',
|
||||
in_progress: 'processing',
|
||||
completed: 'default',
|
||||
};
|
||||
|
||||
export default function ActionInbox() {
|
||||
const [items, setItems] = useState<ActionItem[]>([]);
|
||||
const [total, setTotal] = useState(0);
|
||||
const [page, setPage] = useState(1);
|
||||
const [loading, setLoading] = useState(false);
|
||||
const [statusFilter, setStatusFilter] = useState('all');
|
||||
const [drawerOpen, setDrawerOpen] = useState(false);
|
||||
const [selectedItem, setSelectedItem] = useState<ActionItem | null>(null);
|
||||
|
||||
const fetchData = useCallback(
|
||||
async (p: number, status?: string) => {
|
||||
setLoading(true);
|
||||
try {
|
||||
const resp = await actionInboxApi.list({
|
||||
page: p,
|
||||
page_size: 20,
|
||||
status: status === 'all' ? undefined : status,
|
||||
});
|
||||
setItems(resp.data);
|
||||
setTotal(resp.total);
|
||||
setPage(p);
|
||||
} finally {
|
||||
setLoading(false);
|
||||
}
|
||||
},
|
||||
[],
|
||||
);
|
||||
|
||||
useEffect(() => {
|
||||
fetchData(1, 'all');
|
||||
}, [fetchData]);
|
||||
|
||||
const handleTabChange = (key: string) => {
|
||||
setStatusFilter(key);
|
||||
fetchData(1, key);
|
||||
};
|
||||
|
||||
const handleItemClick = (item: ActionItem) => {
|
||||
setSelectedItem(item);
|
||||
setDrawerOpen(true);
|
||||
};
|
||||
|
||||
const handleActionComplete = () => {
|
||||
fetchData(page, statusFilter);
|
||||
};
|
||||
|
||||
return (
|
||||
<PageContainer
|
||||
title="行动收件箱"
|
||||
subtitle={`共 ${total} 项待办`}
|
||||
>
|
||||
<Tabs
|
||||
activeKey={statusFilter}
|
||||
onChange={handleTabChange}
|
||||
items={STATUS_TABS.map((tab) => ({
|
||||
key: tab.key,
|
||||
label: tab.label,
|
||||
}))}
|
||||
/>
|
||||
|
||||
<Spin spinning={loading}>
|
||||
{items.length === 0 && !loading ? (
|
||||
<Empty description="暂无待办事项" />
|
||||
) : (
|
||||
<List
|
||||
dataSource={items}
|
||||
pagination={{
|
||||
current: page,
|
||||
total,
|
||||
pageSize: 20,
|
||||
onChange: (p) => fetchData(p, statusFilter),
|
||||
showTotal: (t) => `共 ${t} 条`,
|
||||
}}
|
||||
renderItem={(item) => {
|
||||
const typeConf =
|
||||
TYPE_CONFIG[item.action_type] ??
|
||||
({ label: '未知', color: '#999' } as {
|
||||
label: string;
|
||||
color: string;
|
||||
});
|
||||
return (
|
||||
<List.Item
|
||||
style={{ cursor: 'pointer', padding: '12px 16px' }}
|
||||
onClick={() => handleItemClick(item)}
|
||||
>
|
||||
<List.Item.Meta
|
||||
avatar={
|
||||
<Badge
|
||||
status={BADGE_STATUS[item.status] ?? 'default'}
|
||||
/>
|
||||
}
|
||||
title={
|
||||
<div
|
||||
style={{
|
||||
display: 'flex',
|
||||
alignItems: 'center',
|
||||
gap: 8,
|
||||
}}
|
||||
>
|
||||
<Tag color={typeConf.color}>{typeConf.label}</Tag>
|
||||
<span>{item.title}</span>
|
||||
<Tag color={PRIORITY_COLOR[item.priority]}>
|
||||
{PRIORITY_LABEL[item.priority]}
|
||||
</Tag>
|
||||
</div>
|
||||
}
|
||||
description={`${item.patient_name} · ${formatRelative(item.created_at)}`}
|
||||
/>
|
||||
</List.Item>
|
||||
);
|
||||
}}
|
||||
/>
|
||||
)}
|
||||
</Spin>
|
||||
|
||||
<ActionThreadDrawer
|
||||
open={drawerOpen}
|
||||
item={selectedItem}
|
||||
onClose={() => setDrawerOpen(false)}
|
||||
onActionComplete={handleActionComplete}
|
||||
/>
|
||||
</PageContainer>
|
||||
);
|
||||
}
|
||||
43
crates/erp-health/src/handler/action_inbox_handler.rs
Normal file
43
crates/erp-health/src/handler/action_inbox_handler.rs
Normal file
@@ -0,0 +1,43 @@
|
||||
use axum::extract::{FromRef, Json, Path, Query, State};
|
||||
use axum::Extension;
|
||||
use erp_core::error::AppError;
|
||||
use erp_core::rbac::require_permission;
|
||||
use erp_core::types::{ApiResponse, PaginatedResponse, TenantContext};
|
||||
|
||||
use crate::service::action_inbox_service::{
|
||||
self, ActionInboxQuery, ActionItem, ThreadResponse,
|
||||
};
|
||||
use crate::state::HealthState;
|
||||
|
||||
pub async fn list_action_inbox<S>(
|
||||
State(state): State<HealthState>,
|
||||
Extension(ctx): Extension<TenantContext>,
|
||||
Query(query): Query<ActionInboxQuery>,
|
||||
) -> Result<Json<ApiResponse<PaginatedResponse<ActionItem>>>, AppError>
|
||||
where
|
||||
HealthState: FromRef<S>,
|
||||
S: Clone + Send + Sync + 'static,
|
||||
{
|
||||
require_permission(&ctx, "health.action-inbox.list")?;
|
||||
let result =
|
||||
action_inbox_service::list_action_items(&state.db, ctx.tenant_id, &query).await?;
|
||||
Ok(Json(ApiResponse::ok(result)))
|
||||
}
|
||||
|
||||
pub async fn get_action_thread<S>(
|
||||
State(state): State<HealthState>,
|
||||
Extension(ctx): Extension<TenantContext>,
|
||||
Path(source_ref): Path<String>,
|
||||
) -> Result<Json<ApiResponse<ThreadResponse>>, AppError>
|
||||
where
|
||||
HealthState: FromRef<S>,
|
||||
S: Clone + Send + Sync + 'static,
|
||||
{
|
||||
require_permission(&ctx, "health.action-inbox.list")?;
|
||||
let result =
|
||||
action_inbox_service::get_action_thread(&state.db, ctx.tenant_id, &source_ref).await?;
|
||||
match result {
|
||||
Some(resp) => Ok(Json(ApiResponse::ok(resp))),
|
||||
None => Err(crate::error::HealthError::Validation("行动项未找到".into()).into()),
|
||||
}
|
||||
}
|
||||
@@ -1,3 +1,4 @@
|
||||
pub mod action_inbox_handler;
|
||||
pub mod alert_handler;
|
||||
pub mod alert_rule_handler;
|
||||
pub mod appointment_handler;
|
||||
|
||||
@@ -6,6 +6,7 @@ use erp_core::events::EventBus;
|
||||
use erp_core::module::{ErpModule, PermissionDescriptor};
|
||||
|
||||
use crate::handler::{
|
||||
action_inbox_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_handler, device_reading_handler, diagnosis_handler, doctor_handler, follow_up_handler, follow_up_template_handler,
|
||||
health_data_handler, medication_record_handler, medication_reminder_handler, patient_handler, points_handler, stats_handler,
|
||||
@@ -685,6 +686,15 @@ impl HealthModule {
|
||||
"/health/devices/{id}",
|
||||
axum::routing::delete(device_handler::unbind_device),
|
||||
)
|
||||
// 行动收件箱
|
||||
.route(
|
||||
"/health/action-inbox",
|
||||
axum::routing::get(action_inbox_handler::list_action_inbox),
|
||||
)
|
||||
.route(
|
||||
"/health/action-inbox/{source_ref}/thread",
|
||||
axum::routing::get(action_inbox_handler::get_action_thread),
|
||||
)
|
||||
}
|
||||
}
|
||||
|
||||
@@ -1051,6 +1061,19 @@ impl ErpModule for HealthModule {
|
||||
description: "创建/编辑/删除药物提醒".into(),
|
||||
module: "health".into(),
|
||||
},
|
||||
// 行动收件箱
|
||||
PermissionDescriptor {
|
||||
code: "health.action-inbox.list".into(),
|
||||
name: "查看行动收件箱".into(),
|
||||
description: "查看统一行动收件箱中的待办事项".into(),
|
||||
module: "health".into(),
|
||||
},
|
||||
PermissionDescriptor {
|
||||
code: "health.action-inbox.manage".into(),
|
||||
name: "管理行动项".into(),
|
||||
description: "审批/拒绝/标记行动收件箱中的事项".into(),
|
||||
module: "health".into(),
|
||||
},
|
||||
]
|
||||
}
|
||||
|
||||
|
||||
434
crates/erp-health/src/service/action_inbox_service.rs
Normal file
434
crates/erp-health/src/service/action_inbox_service.rs
Normal file
@@ -0,0 +1,434 @@
|
||||
use chrono::{DateTime, Utc};
|
||||
use erp_core::types::PaginatedResponse;
|
||||
use sea_orm::{DatabaseBackend, DatabaseConnection, FromQueryResult, Statement};
|
||||
use serde::{Deserialize, Serialize};
|
||||
use uuid::Uuid;
|
||||
|
||||
use crate::error::HealthError;
|
||||
|
||||
// ── DTO ──────────────────────────────────────────────────────────────
|
||||
|
||||
#[derive(Debug, Clone, Serialize, Deserialize, PartialEq, Eq)]
|
||||
#[serde(rename_all = "snake_case")]
|
||||
pub enum ActionType {
|
||||
AiSuggestion,
|
||||
}
|
||||
|
||||
#[derive(Debug, Clone, Serialize, Deserialize, PartialEq, Eq)]
|
||||
#[serde(rename_all = "snake_case")]
|
||||
pub enum ActionPriority {
|
||||
Urgent,
|
||||
High,
|
||||
Medium,
|
||||
Low,
|
||||
}
|
||||
|
||||
#[derive(Debug, Clone, Serialize, Deserialize, PartialEq, Eq)]
|
||||
#[serde(rename_all = "snake_case")]
|
||||
pub enum ActionStatus {
|
||||
Pending,
|
||||
InProgress,
|
||||
Completed,
|
||||
Dismissed,
|
||||
}
|
||||
|
||||
#[derive(Debug, Clone, Serialize)]
|
||||
pub struct ActionItem {
|
||||
pub id: String,
|
||||
pub action_type: ActionType,
|
||||
pub priority: ActionPriority,
|
||||
pub status: ActionStatus,
|
||||
pub title: String,
|
||||
pub summary: String,
|
||||
pub patient_id: Uuid,
|
||||
pub patient_name: String,
|
||||
pub source_ref: String,
|
||||
pub created_at: DateTime<Utc>,
|
||||
pub updated_at: DateTime<Utc>,
|
||||
}
|
||||
|
||||
#[derive(Debug, Clone, Serialize)]
|
||||
pub struct ThreadEvent {
|
||||
pub step: String,
|
||||
pub label: String,
|
||||
pub status: ActionStatus,
|
||||
#[serde(skip_serializing_if = "Option::is_none")]
|
||||
pub detail: Option<String>,
|
||||
#[serde(skip_serializing_if = "Option::is_none")]
|
||||
pub timestamp: Option<DateTime<Utc>>,
|
||||
#[serde(skip_serializing_if = "Option::is_none")]
|
||||
pub operator: Option<String>,
|
||||
#[serde(skip_serializing_if = "Option::is_none")]
|
||||
pub link_to: Option<String>,
|
||||
}
|
||||
|
||||
#[derive(Debug, Clone, Serialize)]
|
||||
pub struct ActionDefinition {
|
||||
pub key: String,
|
||||
pub label: String,
|
||||
pub variant: String,
|
||||
#[serde(skip_serializing_if = "Option::is_none")]
|
||||
pub api_endpoint: Option<String>,
|
||||
}
|
||||
|
||||
#[derive(Debug, Clone, Serialize)]
|
||||
pub struct ThreadResponse {
|
||||
pub action_item: ActionItem,
|
||||
pub thread: Vec<ThreadEvent>,
|
||||
pub available_actions: Vec<ActionDefinition>,
|
||||
}
|
||||
|
||||
#[derive(Debug, Deserialize)]
|
||||
pub struct ActionInboxQuery {
|
||||
pub status: Option<String>,
|
||||
#[serde(rename = "type")]
|
||||
pub action_type: Option<String>,
|
||||
pub page: Option<u64>,
|
||||
pub page_size: Option<u64>,
|
||||
}
|
||||
|
||||
// ── 内部查询结构体 ──────────────────────────────────────────────────
|
||||
|
||||
#[derive(Debug, FromQueryResult)]
|
||||
struct ActionItemRow {
|
||||
id: Uuid,
|
||||
suggestion_type: String,
|
||||
risk_level: String,
|
||||
status: String,
|
||||
params: Option<serde_json::Value>,
|
||||
created_at: DateTime<Utc>,
|
||||
updated_at: DateTime<Utc>,
|
||||
patient_id: Uuid,
|
||||
patient_name: String,
|
||||
result_content: Option<String>,
|
||||
_analysis_id: Uuid,
|
||||
}
|
||||
|
||||
#[derive(Debug, FromQueryResult)]
|
||||
struct CountRow {
|
||||
cnt: i64,
|
||||
}
|
||||
|
||||
#[derive(Debug, FromQueryResult)]
|
||||
struct SuggestionDetail {
|
||||
id: Uuid,
|
||||
suggestion_type: String,
|
||||
risk_level: String,
|
||||
status: String,
|
||||
params: Option<serde_json::Value>,
|
||||
workflow_instance_id: Option<Uuid>,
|
||||
reanalysis_id: Option<Uuid>,
|
||||
created_at: DateTime<Utc>,
|
||||
updated_at: DateTime<Utc>,
|
||||
_analysis_id: Uuid,
|
||||
patient_id: Uuid,
|
||||
patient_name: String,
|
||||
result_content: Option<String>,
|
||||
analysis_created_at: DateTime<Utc>,
|
||||
}
|
||||
|
||||
// ── 辅助函数 ────────────────────────────────────────────────────────
|
||||
|
||||
fn risk_to_priority(risk: &str) -> ActionPriority {
|
||||
match risk {
|
||||
"high" => ActionPriority::Urgent,
|
||||
"medium" => ActionPriority::High,
|
||||
"low" => ActionPriority::Medium,
|
||||
_ => ActionPriority::Low,
|
||||
}
|
||||
}
|
||||
|
||||
fn suggestion_status_to_action(status: &str) -> ActionStatus {
|
||||
match status {
|
||||
"pending" => ActionStatus::Pending,
|
||||
"approved" => ActionStatus::InProgress,
|
||||
"executed" => ActionStatus::Completed,
|
||||
_ => ActionStatus::Dismissed, // rejected, expired, parse_failed
|
||||
}
|
||||
}
|
||||
|
||||
fn suggestion_type_to_title(suggestion_type: &str) -> String {
|
||||
match suggestion_type {
|
||||
"followup" => "建议安排随访".into(),
|
||||
"appointment" => "建议预约复查".into(),
|
||||
"alert" => "建议关注指标异常".into(),
|
||||
_ => "AI 健康建议".into(),
|
||||
}
|
||||
}
|
||||
|
||||
fn extract_title(params: &Option<serde_json::Value>, suggestion_type: &str) -> String {
|
||||
params
|
||||
.as_ref()
|
||||
.and_then(|p| p.get("reason"))
|
||||
.and_then(|v| v.as_str())
|
||||
.map(|s| s.to_string())
|
||||
.unwrap_or_else(|| suggestion_type_to_title(suggestion_type))
|
||||
}
|
||||
|
||||
// ── 公开 API ────────────────────────────────────────────────────────
|
||||
|
||||
pub async fn list_action_items(
|
||||
db: &DatabaseConnection,
|
||||
tenant_id: Uuid,
|
||||
query: &ActionInboxQuery,
|
||||
) -> Result<PaginatedResponse<ActionItem>, HealthError> {
|
||||
let page = query.page.unwrap_or(1).max(1);
|
||||
let page_size = query.page_size.unwrap_or(20).min(100);
|
||||
let offset = (page - 1) * page_size;
|
||||
|
||||
let status_filter = match query.status.as_deref() {
|
||||
Some("pending") => "AND s.status = 'pending'",
|
||||
Some("in_progress") => "AND s.status = 'approved'",
|
||||
Some("completed") => "AND s.status = 'executed'",
|
||||
Some("dismissed") => "AND s.status IN ('rejected', 'expired', 'parse_failed')",
|
||||
_ => "",
|
||||
};
|
||||
|
||||
let data_sql = format!(
|
||||
r#"
|
||||
SELECT s.id, s.suggestion_type, s.risk_level, s.status, s.params,
|
||||
s.created_at, s.updated_at,
|
||||
a.patient_id, p.name AS patient_name,
|
||||
a.result_content, a.id AS analysis_id
|
||||
FROM ai_suggestion s
|
||||
JOIN ai_analysis a ON s.analysis_id = a.id
|
||||
JOIN patient p ON a.patient_id = p.id
|
||||
WHERE s.tenant_id = $1
|
||||
AND s.deleted_at IS NULL
|
||||
{status_filter}
|
||||
ORDER BY
|
||||
CASE s.risk_level WHEN 'high' THEN 1 WHEN 'medium' THEN 2 ELSE 3 END,
|
||||
s.created_at DESC
|
||||
LIMIT $2 OFFSET $3
|
||||
"#
|
||||
);
|
||||
|
||||
let rows: Vec<ActionItemRow> = FromQueryResult::find_by_statement(
|
||||
Statement::from_sql_and_values(
|
||||
DatabaseBackend::Postgres,
|
||||
data_sql,
|
||||
[
|
||||
tenant_id.into(),
|
||||
(page_size as i64).into(),
|
||||
(offset as i64).into(),
|
||||
],
|
||||
),
|
||||
)
|
||||
.all(db)
|
||||
.await
|
||||
.map_err(|e| HealthError::DbError(e.to_string()))?;
|
||||
|
||||
let count_sql = format!(
|
||||
r#"
|
||||
SELECT COUNT(*) AS cnt
|
||||
FROM ai_suggestion s
|
||||
JOIN ai_analysis a ON s.analysis_id = a.id
|
||||
WHERE s.tenant_id = $1
|
||||
AND s.deleted_at IS NULL
|
||||
{status_filter}
|
||||
"#
|
||||
);
|
||||
|
||||
let count_row: Option<CountRow> = FromQueryResult::find_by_statement(
|
||||
Statement::from_sql_and_values(DatabaseBackend::Postgres, count_sql, [tenant_id.into()]),
|
||||
)
|
||||
.one(db)
|
||||
.await
|
||||
.map_err(|e| HealthError::DbError(e.to_string()))?;
|
||||
|
||||
let total = count_row.map(|r| r.cnt).unwrap_or(0) as u64;
|
||||
|
||||
let items: Vec<ActionItem> = rows
|
||||
.into_iter()
|
||||
.map(|r| ActionItem {
|
||||
id: format!("ai_suggestion:{}", r.id),
|
||||
action_type: ActionType::AiSuggestion,
|
||||
priority: risk_to_priority(&r.risk_level),
|
||||
status: suggestion_status_to_action(&r.status),
|
||||
title: extract_title(&r.params, &r.suggestion_type),
|
||||
summary: r.result_content.unwrap_or_default().chars().take(100).collect(),
|
||||
patient_id: r.patient_id,
|
||||
patient_name: r.patient_name,
|
||||
source_ref: r.id.to_string(),
|
||||
created_at: r.created_at,
|
||||
updated_at: r.updated_at,
|
||||
})
|
||||
.collect();
|
||||
|
||||
Ok(PaginatedResponse {
|
||||
data: items,
|
||||
total,
|
||||
page,
|
||||
page_size,
|
||||
total_pages: if page_size > 0 { (total + page_size - 1) / page_size } else { 0 },
|
||||
})
|
||||
}
|
||||
|
||||
pub async fn get_action_thread(
|
||||
db: &DatabaseConnection,
|
||||
tenant_id: Uuid,
|
||||
source_ref: &str,
|
||||
) -> Result<Option<ThreadResponse>, HealthError> {
|
||||
let suggestion_id = source_ref
|
||||
.strip_prefix("ai_suggestion:")
|
||||
.ok_or_else(|| HealthError::Validation("无效的 source_ref 格式".into()))?;
|
||||
let uuid = Uuid::parse_str(suggestion_id)
|
||||
.map_err(|e| HealthError::Validation(format!("无效的 UUID: {e}")))?;
|
||||
|
||||
let detail: Option<SuggestionDetail> = FromQueryResult::find_by_statement(
|
||||
Statement::from_sql_and_values(
|
||||
DatabaseBackend::Postgres,
|
||||
r#"
|
||||
SELECT s.id, s.suggestion_type, s.risk_level, s.status, s.params,
|
||||
s.workflow_instance_id, s.reanalysis_id,
|
||||
s.created_at, s.updated_at,
|
||||
s.analysis_id, a.patient_id, p.name AS patient_name,
|
||||
a.result_content, a.created_at AS analysis_created_at
|
||||
FROM ai_suggestion s
|
||||
JOIN ai_analysis a ON s.analysis_id = a.id
|
||||
JOIN patient p ON a.patient_id = p.id
|
||||
WHERE s.id = $1 AND s.tenant_id = $2 AND s.deleted_at IS NULL
|
||||
"#,
|
||||
[uuid.into(), tenant_id.into()],
|
||||
),
|
||||
)
|
||||
.one(db)
|
||||
.await
|
||||
.map_err(|e| HealthError::DbError(e.to_string()))?;
|
||||
|
||||
let detail = match detail {
|
||||
Some(d) => d,
|
||||
None => return Ok(None),
|
||||
};
|
||||
|
||||
let action_status = suggestion_status_to_action(&detail.status);
|
||||
|
||||
// ── 拼装线程时间线 ──
|
||||
let mut thread = Vec::new();
|
||||
|
||||
// Step 1: AI 分析完成
|
||||
thread.push(ThreadEvent {
|
||||
step: "ai_analysis".into(),
|
||||
label: "AI 分析完成".into(),
|
||||
status: ActionStatus::Completed,
|
||||
detail: None,
|
||||
timestamp: Some(detail.analysis_created_at),
|
||||
operator: None,
|
||||
link_to: Some("/health/ai-analysis".into()),
|
||||
});
|
||||
|
||||
// Step 2: 医生审批
|
||||
let approval_status = match detail.status.as_str() {
|
||||
"approved" | "executed" => ActionStatus::Completed,
|
||||
"rejected" => ActionStatus::Dismissed,
|
||||
_ => ActionStatus::InProgress,
|
||||
};
|
||||
let is_terminal = matches!(approval_status, ActionStatus::Completed | ActionStatus::Dismissed);
|
||||
thread.push(ThreadEvent {
|
||||
step: "doctor_approval".into(),
|
||||
label: "医生审批".into(),
|
||||
status: approval_status,
|
||||
detail: None,
|
||||
timestamp: if is_terminal {
|
||||
Some(detail.updated_at)
|
||||
} else {
|
||||
None
|
||||
},
|
||||
operator: None,
|
||||
link_to: None,
|
||||
});
|
||||
|
||||
// Step 3: 执行安排
|
||||
let has_workflow = detail.workflow_instance_id.is_some();
|
||||
if has_workflow || detail.status == "approved" || detail.status == "executed" {
|
||||
thread.push(ThreadEvent {
|
||||
step: "action_dispatched".into(),
|
||||
label: "执行安排".into(),
|
||||
status: if has_workflow {
|
||||
ActionStatus::Completed
|
||||
} else {
|
||||
ActionStatus::Pending
|
||||
},
|
||||
detail: None,
|
||||
timestamp: None,
|
||||
operator: None,
|
||||
link_to: None,
|
||||
});
|
||||
}
|
||||
|
||||
// Step 4: 再分析对比
|
||||
if detail.reanalysis_id.is_some() || detail.status == "executed" {
|
||||
thread.push(ThreadEvent {
|
||||
step: "reanalysis".into(),
|
||||
label: "前后对比".into(),
|
||||
status: if detail.reanalysis_id.is_some() {
|
||||
ActionStatus::Completed
|
||||
} else {
|
||||
ActionStatus::Pending
|
||||
},
|
||||
detail: None,
|
||||
timestamp: None,
|
||||
operator: None,
|
||||
link_to: detail.reanalysis_id.map(|id| format!("/health/ai-analysis/{id}")),
|
||||
});
|
||||
}
|
||||
|
||||
// ── 动态操作按钮 ──
|
||||
let available_actions = match detail.status.as_str() {
|
||||
"pending" => vec![
|
||||
ActionDefinition {
|
||||
key: "approve".into(),
|
||||
label: "批准并执行".into(),
|
||||
variant: "primary".into(),
|
||||
api_endpoint: Some(format!("/api/v1/ai/suggestions/{}/approve", detail.id)),
|
||||
},
|
||||
ActionDefinition {
|
||||
key: "reject".into(),
|
||||
label: "拒绝".into(),
|
||||
variant: "danger".into(),
|
||||
api_endpoint: Some(format!("/api/v1/ai/suggestions/{}/approve", detail.id)),
|
||||
},
|
||||
],
|
||||
"approved" => vec![ActionDefinition {
|
||||
key: "acknowledge".into(),
|
||||
label: "标记已知悉".into(),
|
||||
variant: "default".into(),
|
||||
api_endpoint: None,
|
||||
}],
|
||||
"executed" => vec![ActionDefinition {
|
||||
key: "view_comparison".into(),
|
||||
label: "查看前后对比".into(),
|
||||
variant: "primary".into(),
|
||||
api_endpoint: Some(format!(
|
||||
"/api/v1/ai/suggestions/{}/comparison",
|
||||
detail.id
|
||||
)),
|
||||
}],
|
||||
_ => vec![],
|
||||
};
|
||||
|
||||
let action_item = ActionItem {
|
||||
id: format!("ai_suggestion:{}", detail.id),
|
||||
action_type: ActionType::AiSuggestion,
|
||||
priority: risk_to_priority(&detail.risk_level),
|
||||
status: action_status,
|
||||
title: extract_title(&detail.params, &detail.suggestion_type),
|
||||
summary: detail
|
||||
.result_content
|
||||
.unwrap_or_default()
|
||||
.chars()
|
||||
.take(100)
|
||||
.collect(),
|
||||
patient_id: detail.patient_id,
|
||||
patient_name: detail.patient_name,
|
||||
source_ref: detail.id.to_string(),
|
||||
created_at: detail.created_at,
|
||||
updated_at: detail.updated_at,
|
||||
};
|
||||
|
||||
Ok(Some(ThreadResponse {
|
||||
action_item,
|
||||
thread,
|
||||
available_actions,
|
||||
}))
|
||||
}
|
||||
@@ -1,3 +1,4 @@
|
||||
pub mod action_inbox_service;
|
||||
pub mod ai_action_dispatcher;
|
||||
pub mod ai_suggestion_loader;
|
||||
pub mod alert_engine;
|
||||
|
||||
Reference in New Issue
Block a user