refactor(escort): 取消状态与 status 解耦
- 新增 cancelStatus(none/requested/cancelled),status 只描述服务生命周期 - record_state 拆分 canCancelTransition 与 CANCEL_TRANSITIONS - 管理端 Agent 工具、查询工具与接口层统一走取消状态机 - 列表默认隐藏已取消订单,支持 cancelStatus / includeCancelled 筛选
This commit is contained in:
@@ -155,19 +155,33 @@ const EscortRecordSchema = mongoose.Schema(
|
||||
},
|
||||
|
||||
/**
|
||||
* 陪诊状态
|
||||
* 陪诊状态(只描述服务生命周期,不含取消)
|
||||
* - pending: 待确认 - 订单已创建,等待确认
|
||||
* - confirmed: 已确认 - 订单已确认,等待服务
|
||||
* - cancel_requested: 待取消 - 用户已申请取消,等待陪诊师或管理员确认
|
||||
* - in_progress: 进行中 - 陪诊服务正在进行
|
||||
* - completed: 已完成 - 陪诊服务已完成
|
||||
* - cancelled: 已取消 - 订单已取消
|
||||
*
|
||||
* 取消单独用 cancelStatus 表达,取消后 status 保留取消前的服务状态。
|
||||
*/
|
||||
status: {
|
||||
type: String,
|
||||
enum: ["pending", "confirmed", "cancel_requested", "in_progress", "completed", "cancelled"],
|
||||
enum: ["pending", "confirmed", "in_progress", "completed"],
|
||||
default: "pending",
|
||||
comment: "陪诊状态:待确认、已确认、待取消、进行中、已完成、已取消",
|
||||
comment: "陪诊状态:待确认、已确认、进行中、已完成",
|
||||
},
|
||||
|
||||
/**
|
||||
* 取消状态(与 status 解耦)
|
||||
* - none: 未取消
|
||||
* - requested: 用户已申请取消,等待陪诊师/管理员确认
|
||||
* - cancelled: 已取消
|
||||
*/
|
||||
cancelStatus: {
|
||||
type: String,
|
||||
enum: ["none", "requested", "cancelled"],
|
||||
default: "none",
|
||||
index: true,
|
||||
comment: "取消状态:无、待取消、已取消",
|
||||
},
|
||||
|
||||
cancelReason: { type: String, default: "", comment: "订单取消原因" },
|
||||
@@ -209,7 +223,10 @@ const EscortRecordSchema = mongoose.Schema(
|
||||
* @returns {Promise<Array>} 陪诊记录列表(按预约日期倒序)
|
||||
*/
|
||||
EscortRecordSchema.statics.buildRecordFilter = function (options = {}) {
|
||||
const { status, userId, appointmentDate, healthProfileId, includeDeleted = false } = options;
|
||||
const {
|
||||
status, cancelStatus, userId, appointmentDate, healthProfileId,
|
||||
includeDeleted = false, includeCancelled = false,
|
||||
} = options;
|
||||
const filter = {};
|
||||
|
||||
// 软删除的订单默认不进入列表:数据库里保留审计,界面上视为已删除
|
||||
@@ -218,6 +235,19 @@ EscortRecordSchema.statics.buildRecordFilter = function (options = {}) {
|
||||
if (status && Array.isArray(status) && status.length > 0) {
|
||||
filter.status = { $in: status };
|
||||
}
|
||||
// 取消状态与 status 解耦:默认隐藏已取消订单,显式指定 cancelStatus 或 includeCancelled 时放开
|
||||
if (cancelStatus && Array.isArray(cancelStatus) && cancelStatus.length > 0) {
|
||||
if (cancelStatus.length === 1 && cancelStatus[0] === "cancelled") {
|
||||
// 兼容旧数据:取消曾存放在 status 中
|
||||
filter.$or = [{ cancelStatus: "cancelled" }, { status: "cancelled" }];
|
||||
} else {
|
||||
filter.cancelStatus = { $in: cancelStatus };
|
||||
}
|
||||
} else if (!includeCancelled) {
|
||||
// 历史数据没有 cancelStatus 字段,按未取消处理;旧数据仍可能把 cancelled 存在 status 里
|
||||
filter.cancelStatus = { $ne: "cancelled" };
|
||||
if (!filter.status) filter.status = { $ne: "cancelled" };
|
||||
}
|
||||
if (userId) filter.userId = userId;
|
||||
if (healthProfileId) filter.healthProfileId = healthProfileId;
|
||||
if (appointmentDate) {
|
||||
@@ -251,7 +281,11 @@ EscortRecordSchema.statics.findRecordPage = async function (options = {}) {
|
||||
this.find(filter).sort({ "schedule.date": -1 }).skip(skip).limit(pageSize).exec(),
|
||||
this.countDocuments(filter).exec(),
|
||||
this.countDocuments(baseFilter).exec(),
|
||||
this.countDocuments({ ...baseFilter, status: { $in: ["pending", "confirmed", "in_progress"] } }).exec(),
|
||||
this.countDocuments({
|
||||
...baseFilter,
|
||||
status: { $in: ["pending", "confirmed", "in_progress"] },
|
||||
cancelStatus: { $ne: "cancelled" },
|
||||
}).exec(),
|
||||
]);
|
||||
return { records, total, counts: { all, ongoing } };
|
||||
};
|
||||
@@ -329,6 +363,19 @@ EscortRecordSchema.statics.updateRecordStatus = async function (id, fromStatus,
|
||||
);
|
||||
};
|
||||
|
||||
// 取消状态迁移以读取时的 cancelStatus 为条件;历史数据缺字段时按 none 处理。
|
||||
EscortRecordSchema.statics.updateRecordCancel = async function (id, fromCancelStatus, update) {
|
||||
const from = fromCancelStatus || "none";
|
||||
const cancelFilter = from === "none"
|
||||
? { cancelStatus: { $in: ["none", null] } }
|
||||
: { cancelStatus: from };
|
||||
return await this.findOneAndUpdate(
|
||||
{ _id: id, deletedAt: null, ...cancelFilter },
|
||||
{ $set: { ...update, "meta.updatetime": Date.now() } },
|
||||
{ new: true, runValidators: true }
|
||||
);
|
||||
};
|
||||
|
||||
EscortRecordSchema.statics.findByIdempotencyKey = async function (userId, idempotencyKey) {
|
||||
return await this.findOne({ userId, idempotencyKey, deletedAt: null }).exec();
|
||||
};
|
||||
|
||||
Reference in New Issue
Block a user