import { http } from './http' import { unwrap } from './envelope' /** 业务严重度:紧急 / 高危 / 中危 / 低危(后端按 scene + level 推导)。 */ export type AdminNotificationSeverity = 'urgent' | 'high' | 'medium' | 'low' export type AdminNotificationSortMode = 'severity' | 'time' export const ADMIN_NOTIFICATION_SEVERITY_LABELS: Readonly> = { urgent: '紧急', high: '高危', medium: '中危', low: '低危', } /** 通知里的结构化定位锚点(当前只有 task_failed 使用:桶内每个失败任务一项)。 */ export interface AdminNotificationRef { taskId: number | null /** 任务号;品牌任务为 #。任务详情接口按它查。 */ taskNo: string moduleType?: string | null reason?: string | null updatedAt?: string | null deviceId?: string | null } /** 单条通知(后台铃铛)。 */ export interface AdminNotificationItem { id: number scene: string level: string /** 业务严重度;旧后端不返回时前端按 level 兜底。 */ severity?: string title: string content: string /** 可点的定位锚点;旧后端/历史行没有这一列时为空。 */ refs?: AdminNotificationRef[] | null read: boolean readAt: string | null createdAt: string | null } export interface AdminNotificationPage { items: AdminNotificationItem[] total: number page: number pageSize: number unreadCount: number } export interface AdminNotificationSummary { unreadCount: number /** 有未读的聚合组数;徽标用它。unreadCount 仍是"条数",给老明细视图与对账用。 */ unreadGroupCount?: number latestId: number } /** 聚合组:一个「场景 × 自然日 × 事件主体用户」折成的一行。 */ export interface AdminNotificationGroup { latestId: number scene: string category: string day: string subjectUserId: number | null rowCount: number unreadCount: number level: string /** 组内最高严重度。 */ severity?: string title: string content: string createdAt: string | null } export interface AdminNotificationGroupPage { groups: AdminNotificationGroup[] /** 组数(分页分母)。 */ total: number /** 折叠前的明细条数——必须与 total 同屏,否则「共 12 条」会被读成只有 12 条通知。 */ rowCount: number page: number pageSize: number unreadGroupCount: number unreadCount: number } /** 铃铛轮询:未读数 + 最新通知 id。 */ export async function fetchNotificationSummary(): Promise { const { data } = await http.get('/api/admin/notifications/summary') return unwrap(data) } /** 列表查询参数:关键字匹配标题/内容;startDate/endDate 为年月日闭区间(yyyy-MM-dd)。 */ export interface AdminNotificationListParams { page?: number pageSize?: number onlyUnread?: boolean keyword?: string startDate?: string endDate?: string /** 排序:severity(默认)或 time;默认值不下发。 */ sort?: AdminNotificationSortMode /** 展开聚合组:三个条件取自该组自己返回的值,原样回传。 */ scene?: string day?: string subjectUserId?: number | null /** true 表示展开「未区分用户」那一组(subject_user_id 为空的存量行)。 */ subjectUnlabelled?: boolean } /** 聚合列表(铃铛默认视图)。分页按组切。 */ export async function fetchNotificationGroups( params: AdminNotificationListParams = {}, ): Promise { const keyword = (params.keyword ?? '').trim() const { data } = await http.get('/api/admin/notifications/groups', { params: { page: params.page ?? 1, pageSize: params.pageSize ?? 20, onlyUnread: params.onlyUnread ? 'true' : 'false', keyword: keyword || undefined, startDate: params.startDate || undefined, endDate: params.endDate || undefined, sort: params.sort === 'time' ? 'time' : undefined, }, }) return unwrap(data) } export async function fetchNotificationList( params: AdminNotificationListParams = {}, ): Promise { const keyword = (params.keyword ?? '').trim() const { data } = await http.get('/api/admin/notifications', { params: { page: params.page ?? 1, pageSize: params.pageSize ?? 20, onlyUnread: params.onlyUnread ? 'true' : 'false', keyword: keyword || undefined, startDate: params.startDate || undefined, endDate: params.endDate || undefined, sort: params.sort === 'time' ? 'time' : undefined, scene: params.scene || undefined, day: params.day || undefined, subjectUserId: params.subjectUserId ?? undefined, subjectUnlabelled: params.subjectUnlabelled ? 'true' : undefined, }, }) return unwrap(data) } export async function markNotificationRead(id: number): Promise { const { data } = await http.post(`/api/admin/notifications/${id}/read`) return unwrap(data) } export async function markAllNotificationsRead(): Promise { const { data } = await http.post('/api/admin/notifications/read-all') return unwrap(data) }