feat(attendant): 订单池按身份合并可见范围并支持接单与共享

实习可见未分配与被共享订单但不可接单并脱敏患者联系方式;正式可接未被分配的订单,接单不校验支付状态;接单本人可共享给任意在职陪诊师。
This commit is contained in:
lik
2026-11-10 12:27:59 +08:00
parent 67f2962574
commit d60a160180
5 changed files with 691 additions and 6 deletions
+68
View File
@@ -89,6 +89,13 @@ const EscortRecordSchema = mongoose.Schema(
name: { type: String, default: "", comment: "陪诊员姓名" },
sex: { type: String, enum: ["none", "male", "female"], comment: "陪诊员性别" },
mobile: { type: String, default: "", comment: "陪诊员联系电话" },
// 接单陪诊师把订单共享给其他陪诊师:存 employee._id(不是登录 userId),
// 这样展示共享对象时不依赖账号绑定关系
sharedWith: [{
type: mongoose.Schema.Types.ObjectId,
ref: "employee",
comment: "共享给其他陪诊师(employee._id)",
}],
},
/**
@@ -245,6 +252,7 @@ const EscortRecordSchema = mongoose.Schema(
EscortRecordSchema.statics.buildRecordFilter = function (options = {}) {
const {
status, cancelStatus, userId, appointmentDate, healthProfileId, attendantId,
attendantVisibility,
includeDeleted = false, includeCancelled = false,
paymentStatus, payChannel, startDate, endDate, keyword,
} = options;
@@ -279,6 +287,19 @@ EscortRecordSchema.statics.buildRecordFilter = function (options = {}) {
if (userId) filter.userId = userId;
// 陪诊员列表:只查 assign 到本人的订单
if (attendantId) filter["attendant.id"] = attendantId;
// 陪诊师订单池:按身份合并可见范围,scope 由服务端计算,不接受前端传入。
// - includeUnassigned:所有在职陪诊师都能看到未分配订单
// - userId:正式陪诊师还能看到自己已接的订单(实习不能接单,故不传)
// - employeeId:被共享的订单
if (attendantVisibility) {
const or = [];
if (attendantVisibility.includeUnassigned) or.push({ "attendant.id": null });
if (attendantVisibility.userId) or.push({ "attendant.id": attendantVisibility.userId });
if (attendantVisibility.employeeId) {
or.push({ "attendant.sharedWith": attendantVisibility.employeeId });
}
if (or.length) andConditions.push({ $or: or });
}
if (healthProfileId) filter.healthProfileId = healthProfileId;
// 支付状态 / 渠道:管理端按收款情况对账时使用
@@ -456,6 +477,53 @@ EscortRecordSchema.statics.findByIdempotencyKey = async function (userId, idempo
return await this.findOne({ userId, idempotencyKey, deletedAt: null }).exec();
};
// 接单:只有「未分配」的订单能被接走,条件更新保证并发下只有一个陪诊师成功。
// 与支付状态无关:订单是否付款不影响接单。
EscortRecordSchema.statics.acceptRecord = async function (id, attendant) {
return await this.findOneAndUpdate(
{
_id: id,
deletedAt: null,
"attendant.id": null,
cancelStatus: { $ne: "cancelled" },
status: { $ne: "cancelled" },
},
{
$set: {
"attendant.id": attendant.id,
"attendant.name": attendant.name || "",
"attendant.sex": attendant.sex || "none",
"attendant.mobile": attendant.mobile || "",
"meta.updatetime": Date.now(),
},
},
{ new: true, runValidators: true }
);
};
// 共享 / 取消共享:只有当前接单陪诊师本人能操作
EscortRecordSchema.statics.addSharedWith = async function (id, attendantId, employeeIds) {
return await this.findOneAndUpdate(
{ _id: id, deletedAt: null, "attendant.id": attendantId },
{
$addToSet: { "attendant.sharedWith": { $each: employeeIds } },
$set: { "meta.updatetime": Date.now() },
},
{ new: true, runValidators: true }
);
};
EscortRecordSchema.statics.removeSharedWith = async function (id, attendantId, employeeIds) {
return await this.findOneAndUpdate(
{ _id: id, deletedAt: null, "attendant.id": attendantId },
{
$pull: { "attendant.sharedWith": { $in: employeeIds } },
$set: { "meta.updatetime": Date.now() },
},
{ new: true, runValidators: true }
);
};
/**
* 根据ID查找陪诊记录
*