feat(escort): 病历与诉求支持多材料解析为四段摘要
新增独立病历整理 Agent 与异步解析任务,支持 PDF 文本、图片 OCR、 语音 ASR 与文字输入,统一整理为基础健康、检查治疗、用药和问诊诉求。 Agent 只产出草稿,订单本人在编辑页核对确认后才写入当前订单; 原件在任务结束、失败或取消时立即清除。 inquiry 增加三段规范字段,medicalHistory 保留为旧版兼容摘要, 并兼容旧客户端只写 medicalHistory 的更新方式。
This commit is contained in:
@@ -12,6 +12,7 @@ import {
|
||||
CANCEL_STATUS_VALUES,
|
||||
} from "../utils/record_state.js";
|
||||
import { HandlerPayment } from "./payment.js";
|
||||
import { INQUIRY_FIELDS, buildLegacyMedicalHistory, normalizeInquiryView } from "../utils/inquiry.js";
|
||||
|
||||
class HandlerEscortRecord {
|
||||
constructor() {
|
||||
@@ -30,7 +31,14 @@ class HandlerEscortRecord {
|
||||
// - 待支付/已支付:可改医院与排期(改期会写审计时间)
|
||||
// - 进行中:服务已开始,仅可补充病历与诉求,排期与医院不允许用户单方变更
|
||||
// - 终态:全部不可改
|
||||
static OWNER_NOTE_FIELDS = ["notes.patientNote", "inquiry.medicalHistory", "inquiry.demand"];
|
||||
static OWNER_NOTE_FIELDS = [
|
||||
"notes.patientNote",
|
||||
"inquiry.medicalHistory",
|
||||
"inquiry.basicHealth",
|
||||
"inquiry.treatmentHistory",
|
||||
"inquiry.medications",
|
||||
"inquiry.demand",
|
||||
];
|
||||
static OWNER_BOOKING_FIELDS = [
|
||||
"hospital.province", "hospital.name", "hospital.address", "hospital.department",
|
||||
"hospital.doctor", "hospital.medicalRecordNo", "schedule.date", "schedule.startTime",
|
||||
@@ -322,9 +330,9 @@ class HandlerEscortRecord {
|
||||
record.shippingAddress.mobile = HandlerEscortRecord.maskMobile(record.shippingAddress.mobile);
|
||||
if (record.shippingAddress.address) record.shippingAddress.address = "接单后可见";
|
||||
}
|
||||
if (record.inquiry?.medicalHistory) record.inquiry.medicalHistory = "接单后可见";
|
||||
// 诉求是用户自由文本,可能夹带联系方式,一并脱敏
|
||||
if (record.inquiry?.demand) record.inquiry.demand = "接单后可见";
|
||||
for (const field of [...INQUIRY_FIELDS, "medicalHistory"]) {
|
||||
if (record.inquiry?.[field]) record.inquiry[field] = "接单后可见";
|
||||
}
|
||||
}
|
||||
return record;
|
||||
}
|
||||
@@ -338,6 +346,7 @@ class HandlerEscortRecord {
|
||||
static withPaymentWindow(record) {
|
||||
if (!record) return record;
|
||||
const plain = record.toObject ? record.toObject() : { ...record };
|
||||
plain.inquiry = normalizeInquiryView(plain.inquiry);
|
||||
const end = HandlerPayment.getPayWindowEnd(plain);
|
||||
plain.paymentWindowEnd = end ? end.toISOString() : null;
|
||||
return plain;
|
||||
@@ -1089,9 +1098,31 @@ class HandlerEscortRecord {
|
||||
}
|
||||
}
|
||||
|
||||
const legacyInquiryWrite = payload["inquiry.medicalHistory"] !== undefined
|
||||
|| payload.inquiry?.medicalHistory !== undefined;
|
||||
const structuredInquiryWrite = INQUIRY_FIELDS.some(
|
||||
(field) => payload[`inquiry.${field}`] !== undefined || payload.inquiry?.[field] !== undefined
|
||||
);
|
||||
|
||||
// 嵌套子文档摊平为点号路径,避免整块覆盖与路径冲突
|
||||
HandlerEscortRecord.flattenNestedFields(payload, existing);
|
||||
|
||||
// 旧客户端只写 medicalHistory 时,把它迁移到检查与治疗病历;
|
||||
// 新客户端写任一规范字段时,重新生成旧版兼容摘要。
|
||||
if (legacyInquiryWrite && !structuredInquiryWrite) {
|
||||
payload["inquiry.basicHealth"] = "";
|
||||
payload["inquiry.treatmentHistory"] = payload["inquiry.medicalHistory"] || "";
|
||||
payload["inquiry.medications"] = "";
|
||||
} else if (structuredInquiryWrite) {
|
||||
const nextInquiry = normalizeInquiryView(existing.inquiry);
|
||||
for (const field of INQUIRY_FIELDS) {
|
||||
if (payload[`inquiry.${field}`] !== undefined) {
|
||||
nextInquiry[field] = payload[`inquiry.${field}`];
|
||||
}
|
||||
}
|
||||
payload["inquiry.medicalHistory"] = buildLegacyMedicalHistory(nextInquiry);
|
||||
}
|
||||
|
||||
// 已收款订单一律锁定支付状态与实收金额:否则编辑退款中的订单会把它改回未支付,
|
||||
// 用户可再次支付造成重复扣款;金额改动也会与 payment_record 对不上
|
||||
const currentPayStatus = existing.payment?.status || "unpaid";
|
||||
|
||||
@@ -0,0 +1,121 @@
|
||||
import mongoose from "mongoose";
|
||||
import { DBModel } from "../models/index.js";
|
||||
import { HandlerEscortRecord } from "./escort_record.js";
|
||||
import ResponseUtil from "../utils/responseUtil.js";
|
||||
import { MediaInputError } from "../agent/infra/media/index.js";
|
||||
import { buildInquiryUpdate, normalizeInquiry, normalizeInquiryView } from "../utils/inquiry.js";
|
||||
import { medicalInquiryTask, toInquiryJobView } from "../agent/bots/bot-medical-inquiry/task.js";
|
||||
|
||||
class HandlerMedicalInquiry {
|
||||
async _ownedEditableRecord(ctx) {
|
||||
if (!mongoose.Types.ObjectId.isValid(ctx.params.id)) {
|
||||
ResponseUtil.badRequest(ctx, "订单 ID 不正确");
|
||||
return null;
|
||||
}
|
||||
const record = await DBModel.EscortRecord.findRecordById(ctx.params.id);
|
||||
if (!record) {
|
||||
ResponseUtil.notFound(ctx, "陪诊记录不存在");
|
||||
return null;
|
||||
}
|
||||
if (String(record.userId) !== String(ctx.state.user?._id)) {
|
||||
ResponseUtil.forbidden(ctx, "仅订单本人可整理病历与诉求");
|
||||
return null;
|
||||
}
|
||||
const editable = (record.cancelStatus || "none") === "none"
|
||||
&& HandlerEscortRecord.ownerEditableFields(record.status).includes("inquiry.demand");
|
||||
if (!editable) {
|
||||
ResponseUtil.forbidden(ctx, "该订单当前状态不支持修改病历与诉求");
|
||||
return null;
|
||||
}
|
||||
return record;
|
||||
}
|
||||
|
||||
async _job(ctx) {
|
||||
if (!mongoose.Types.ObjectId.isValid(ctx.params.jobId)) {
|
||||
ResponseUtil.badRequest(ctx, "解析任务 ID 不正确");
|
||||
return null;
|
||||
}
|
||||
const job = await DBModel.InquiryParseJob.findOne({
|
||||
_id: ctx.params.jobId,
|
||||
recordId: ctx.params.id,
|
||||
userId: ctx.state.user?._id,
|
||||
});
|
||||
if (!job) ResponseUtil.notFound(ctx, "解析任务不存在或已过期");
|
||||
return job;
|
||||
}
|
||||
|
||||
async create(ctx) {
|
||||
try {
|
||||
const record = await this._ownedEditableRecord(ctx);
|
||||
if (!record) return;
|
||||
const body = { ...(ctx.request.body || {}), current: normalizeInquiryView(record.inquiry) };
|
||||
const job = await medicalInquiryTask.create({ record, userId: ctx.state.user._id, body });
|
||||
return ResponseUtil.success(ctx, { job: toInquiryJobView(job) }, "解析任务已创建");
|
||||
} catch (err) {
|
||||
if (err instanceof MediaInputError || err.status === 400 || err.status === 409) {
|
||||
return ResponseUtil.error(ctx, err.message, null, err.status || 400);
|
||||
}
|
||||
return ResponseUtil.internalError(ctx, err);
|
||||
}
|
||||
}
|
||||
|
||||
async get(ctx) {
|
||||
try {
|
||||
const record = await this._ownedEditableRecord(ctx);
|
||||
if (!record) return;
|
||||
const job = await this._job(ctx);
|
||||
if (!job) return;
|
||||
return ResponseUtil.success(ctx, { job: toInquiryJobView(job) }, "查询成功");
|
||||
} catch (err) {
|
||||
return ResponseUtil.internalError(ctx, err);
|
||||
}
|
||||
}
|
||||
|
||||
async confirm(ctx) {
|
||||
try {
|
||||
const record = await this._ownedEditableRecord(ctx);
|
||||
if (!record) return;
|
||||
const job = await this._job(ctx);
|
||||
if (!job) return;
|
||||
if (job.status !== "waiting_confirmation") {
|
||||
return ResponseUtil.error(ctx, "解析草稿尚未完成或已处理", null, 409);
|
||||
}
|
||||
const inquiry = normalizeInquiry(ctx.request.body || {});
|
||||
const update = buildInquiryUpdate(inquiry);
|
||||
// 老订单可能没有 meta.updatetime:此时退回普通更新,否则草稿永远无法确认。
|
||||
const updated = job.baseUpdatedAt
|
||||
? await DBModel.EscortRecord.updateRecordIfUnchanged(record._id, job.baseUpdatedAt, update)
|
||||
: await DBModel.EscortRecord.updateRecord(record._id, update);
|
||||
if (!updated) return ResponseUtil.error(ctx, "订单已更新,请刷新后重新核对草稿", null, 409);
|
||||
await DBModel.InquiryParseJob.updateOne(
|
||||
{ _id: job._id, status: "waiting_confirmation" },
|
||||
{ $set: { status: "confirmed", active: false }, $unset: { inputs: 1, draft: 1, uncertainties: 1 } }
|
||||
);
|
||||
return ResponseUtil.success(ctx, { record: updated }, "病历与诉求已保存");
|
||||
} catch (err) {
|
||||
return ResponseUtil.internalError(ctx, err);
|
||||
}
|
||||
}
|
||||
|
||||
async cancel(ctx) {
|
||||
try {
|
||||
const record = await this._ownedEditableRecord(ctx);
|
||||
if (!record) return;
|
||||
const job = await this._job(ctx);
|
||||
if (!job) return;
|
||||
if (["confirmed", "cancelled"].includes(job.status)) {
|
||||
return ResponseUtil.error(ctx, "解析任务已结束", null, 409);
|
||||
}
|
||||
await DBModel.InquiryParseJob.updateOne(
|
||||
{ _id: job._id },
|
||||
{ $set: { status: "cancelled", active: false }, $unset: { inputs: 1, draft: 1, uncertainties: 1 } }
|
||||
);
|
||||
return ResponseUtil.success(ctx, null, "解析任务已取消");
|
||||
} catch (err) {
|
||||
return ResponseUtil.internalError(ctx, err);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
export { HandlerMedicalInquiry };
|
||||
export default HandlerMedicalInquiry;
|
||||
Reference in New Issue
Block a user