feat: 新增员工管理端 CRUD 并增强订单筛选

- /employee 管理端列表/新增/更新/启停用,/employee/me 供管理端判定陪诊员身份

- 订单列表补充支付状态、支付渠道、日期区间、关键词筛选与各页签计数

- 陪诊员订单复用统一列表查询,过滤与分页口径与管理员一致

- 管理员代客下单时订单归属以就诊人档案用户为准,无归属订单跳过未完成上限

- 管理员判定兼容 web-escort-admin 与重构后的 apps 成员视图
This commit is contained in:
lik
2026-11-06 19:47:42 +08:00
parent 420f379ce8
commit af9a1543ae
6 changed files with 492 additions and 96 deletions
+96 -40
View File
@@ -233,11 +233,15 @@ const EscortRecordSchema = mongoose.Schema(
*/
EscortRecordSchema.statics.buildRecordFilter = function (options = {}) {
const {
status, cancelStatus, userId, appointmentDate, healthProfileId,
status, cancelStatus, userId, appointmentDate, healthProfileId, attendantId,
includeDeleted = false, includeCancelled = false,
paymentStatus, payChannel, startDate, endDate, keyword,
} = options;
const filter = {};
// 多个 $or 条件(取消状态兼容、关键词)不能互相覆盖,统一收进 $and
const andConditions = [];
// 软删除的订单默认不进入列表:数据库里保留审计,界面上视为已删除
if (!includeDeleted) filter.deletedAt = null;
@@ -248,9 +252,13 @@ EscortRecordSchema.statics.buildRecordFilter = function (options = {}) {
if (cancelStatus && Array.isArray(cancelStatus) && cancelStatus.length > 0) {
if (cancelStatus.length === 1 && cancelStatus[0] === "cancelled") {
// 兼容旧数据:取消曾存放在 status 中
filter.$or = [{ cancelStatus: "cancelled" }, { status: "cancelled" }];
andConditions.push({ $or: [{ cancelStatus: "cancelled" }, { status: "cancelled" }] });
} else {
filter.cancelStatus = { $in: cancelStatus };
// 历史数据缺 cancelStatus 字段时按 none 处理
const values = cancelStatus.includes("none")
? [...cancelStatus, null]
: cancelStatus;
filter.cancelStatus = { $in: values };
}
} else if (!includeCancelled) {
// 历史数据没有 cancelStatus 字段,按未取消处理;旧数据仍可能把 cancelled 存在 status 里
@@ -258,13 +266,58 @@ EscortRecordSchema.statics.buildRecordFilter = function (options = {}) {
if (!filter.status) filter.status = { $ne: "cancelled" };
}
if (userId) filter.userId = userId;
// 陪诊员列表:只查 assign 到本人的订单
if (attendantId) filter["attendant.id"] = attendantId;
if (healthProfileId) filter.healthProfileId = healthProfileId;
if (appointmentDate) {
const start = new Date(`${appointmentDate}T00:00:00.000Z`);
const end = new Date(start);
end.setUTCDate(end.getUTCDate() + 1);
filter["schedule.date"] = { $gte: start, $lt: end };
// 支付状态 / 渠道:管理端按收款情况对账时使用
if (paymentStatus && Array.isArray(paymentStatus) && paymentStatus.length > 0) {
filter["payment.status"] = { $in: paymentStatus };
}
if (payChannel) filter["payment.payChannel"] = payChannel;
// 预约日期:单日(appointmentDate)与区间(startDate/endDate)可并存,
// 都按 UTC 日界换算,与历史口径保持一致
const parseDay = (value) => {
const date = new Date(`${value}T00:00:00.000Z`);
return Number.isNaN(date.getTime()) ? null : date;
};
if (appointmentDate) {
const start = parseDay(appointmentDate);
if (start) {
const end = new Date(start);
end.setUTCDate(end.getUTCDate() + 1);
filter["schedule.date"] = { $gte: start, $lt: end };
}
} else if (startDate || endDate) {
const range = {};
const start = startDate ? parseDay(startDate) : null;
const end = endDate ? parseDay(endDate) : null;
if (start) range.$gte = start;
if (end) {
end.setUTCDate(end.getUTCDate() + 1); // 含结束日当天
range.$lt = end;
}
if (Object.keys(range).length) filter["schedule.date"] = range;
}
// 关键词:就诊人姓名/电话、陪诊员姓名、医院名称,以及订单ID
if (keyword && String(keyword).trim()) {
const text = String(keyword).trim();
const safe = text.replace(/[.*+?^${}()|[\]\\]/g, "\\$&");
const reg = new RegExp(safe, "i");
const or = [
{ "patient.name": reg },
{ "patient.mobile": reg },
{ "attendant.name": reg },
{ "hospital.name": reg },
];
if (/^[0-9a-fA-F]{24}$/.test(text)) or.push({ _id: text });
andConditions.push({ $or: or });
}
if (andConditions.length) filter.$and = andConditions;
return filter;
};
@@ -281,50 +334,53 @@ EscortRecordSchema.statics.findRecords = async function (options = {}, cb) {
};
// 列表记录、筛选总数和各页签计数一次返回,避免前端按当前页过滤或统计。
// 计数只受非状态筛选(关键词/日期/支付/归属/陪诊员)影响,不受当前页签的 status/cancelStatus 影响
EscortRecordSchema.statics.findRecordPage = async function (options = {}) {
const { page = 1, pageSize = 20 } = options;
const filter = this.buildRecordFilter(options);
const baseFilter = this.buildRecordFilter({ ...options, status: null });
const countBase = this.buildRecordFilter({
...options,
status: null,
cancelStatus: null,
includeCancelled: true,
});
// 生命周期页签只统计未申请取消的订单,避免同一单同时出现在「待确认」与「待取消」
const activeBase = { ...countBase, cancelStatus: { $in: ["none", null] } };
// 兼容历史数据:取消曾存放在 status 中
const cancelledFilter = {
...countBase,
$and: [
...(countBase.$and || []),
{ $or: [{ cancelStatus: "cancelled" }, { status: "cancelled" }] },
],
};
const skip = (page - 1) * pageSize;
const [records, total, all, ongoing] = await Promise.all([
const [
records, total, all, ongoing,
pending, cancelRequested, confirmed, inProgress, completed, cancelled,
] = await Promise.all([
this.find(filter).sort({ "schedule.date": -1 }).skip(skip).limit(pageSize).exec(),
this.countDocuments(filter).exec(),
this.countDocuments(baseFilter).exec(),
this.countDocuments(countBase).exec(),
this.countDocuments({
...baseFilter,
...countBase,
status: { $in: ["pending", "confirmed", "in_progress"] },
cancelStatus: { $ne: "cancelled" },
}).exec(),
this.countDocuments({ ...activeBase, status: "pending" }).exec(),
this.countDocuments({ ...countBase, cancelStatus: "requested" }).exec(),
this.countDocuments({ ...activeBase, status: "confirmed" }).exec(),
this.countDocuments({ ...activeBase, status: "in_progress" }).exec(),
this.countDocuments({ ...activeBase, status: "completed" }).exec(),
this.countDocuments(cancelledFilter).exec(),
]);
return { records, total, counts: { all, ongoing } };
};
/**
* 查找陪诊员的陪诊记录
*
* @param {ObjectId|string} attendantId - 陪诊员用户ID
* @param {Object} options - 查询选项
* @param {number} [options.page=1] - 页码
* @param {number} [options.pageSize=20] - 每页数量
* @param {string} [options.status] - 状态筛选(可选)
* @param {Function} [cb] - 可选的回调函数
* @returns {Promise<Array>} 陪诊记录列表(按预约日期倒序)
*/
EscortRecordSchema.statics.findRecordsByAttendant = async function (attendantId, options = {}, cb) {
const { page = 1, pageSize = 20, status } = options;
const filter = { "attendant.id": attendantId };
if (status) {
filter.status = status;
}
const skip = (page - 1) * pageSize;
return await this.find(filter)
.sort({ "schedule.date": -1 })
.skip(skip)
.limit(pageSize)
.exec(cb);
return {
records,
total,
counts: { all, ongoing, pending, cancelRequested, confirmed, inProgress, completed, cancelled },
};
};
/**