Files
plates-server/src/medications/models/medication-record.model.ts
richarjiang 188b4addca feat(medications): 新增完整的药物管理和服药提醒功能
实现了包含药物信息管理、服药记录追踪、统计分析、自动状态更新和推送提醒的完整药物管理系统。

核心功能:
- 药物 CRUD 操作,支持多种剂型和自定义服药时间
- 惰性生成服药记录策略,查询时才生成当天记录
- 定时任务自动更新过期记录状态(每30分钟)
- 服药前15分钟自动推送提醒(每5分钟检查)
- 每日/范围/总体统计分析功能
- 完整的 API 文档和数据库建表脚本

技术实现:
- 使用 Sequelize ORM 管理 MySQL 数据表
- 集成 @nestjs/schedule 实现定时任务
- 复用现有推送通知系统发送提醒
- 采用软删除和权限验证保障数据安全
2025-11-07 17:29:11 +08:00

89 lines
1.9 KiB
TypeScript
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

import { Column, Model, Table, DataType, BelongsTo, ForeignKey } from 'sequelize-typescript';
import { MedicationStatusEnum } from '../enums/medication-status.enum';
import { Medication } from './medication.model';
/**
* 服药记录模型
*/
@Table({
tableName: 't_medication_records',
underscored: true,
paranoid: false, // 使用软删除字段 deleted 而不是 deletedAt
})
export class MedicationRecord extends Model {
@Column({
type: DataType.STRING(50),
primaryKey: true,
comment: '记录唯一标识',
})
declare id: string;
@ForeignKey(() => Medication)
@Column({
type: DataType.STRING(50),
allowNull: false,
comment: '关联的药物ID',
})
declare medicationId: string;
@Column({
type: DataType.STRING(50),
allowNull: false,
comment: '用户ID',
})
declare userId: string;
@Column({
type: DataType.DATE,
allowNull: false,
comment: '计划服药时间UTC时间',
})
declare scheduledTime: Date;
@Column({
type: DataType.DATE,
allowNull: true,
comment: '实际服药时间UTC时间',
})
declare actualTime: Date;
@Column({
type: DataType.STRING(20),
allowNull: false,
comment: '服药状态',
})
declare status: MedicationStatusEnum;
@Column({
type: DataType.TEXT,
allowNull: true,
comment: '备注',
})
declare note: string;
@Column({
type: DataType.DATE,
defaultValue: DataType.NOW,
comment: '创建时间',
})
declare createdAt: Date;
@Column({
type: DataType.DATE,
defaultValue: DataType.NOW,
comment: '更新时间',
})
declare updatedAt: Date;
@Column({
type: DataType.BOOLEAN,
allowNull: false,
defaultValue: false,
comment: '软删除标记',
})
declare deleted: boolean;
// 关联关系
@BelongsTo(() => Medication, 'medicationId')
declare medication: Medication;
}