feat: 订单获取链路收尾:管理端指派、接单开关与用户指定陪诊师

This commit is contained in:
lik
2026-11-10 15:13:48 +08:00
parent 78138a417b
commit b160102f12
8 changed files with 725 additions and 32 deletions
+45 -6
View File
@@ -3,6 +3,10 @@
import mongoose from "mongoose";
import { pinyin } from "pinyin-pro";
// /employee/me 与接单开关返回的公共字段
const MY_EMPLOYEE_SELECT =
"employeeNo role status profile.name profile.sex profile.phone profile.avatar attendantInfo";
/**
* 培训记录子文档
*
@@ -122,7 +126,8 @@ const EmployeeSchema = mongoose.Schema(
// 对外评级一律由 employee_review 真实评价聚合得出(employee_review.js),
// 达阈值才展示,详见 doc/architecture-overview.html 第 10 节「陪诊师成长与资金闭环」
rating: { type: Number, default: 5.0, min: 1, max: 5, comment: "评分(无写入点,勿用于公示)" },
orderCount: { type: Number, default: 0, comment: "接单数量(无写入点恒 0,公示/后台展示已摘除,勿用于排序)" },
// 累计接单数:陪诊师接单 / 管理员指派成功时 +1;取消不扣减
orderCount: { type: Number, default: 0, comment: "累计接单数量" },
isOnline: { type: Boolean, default: false, comment: "是否在线" },
},
@@ -256,8 +261,8 @@ EmployeeSchema.statics.updateEmployee = async function (id, update) {
/**
* 查询在线陪诊员列表
*
* 排序口径与 handler/employee.js 的公示列表保持一致:只按服务年限。
* 曾按 attendantInfo.rating / orderCount 排,但两者均无写入点,等于没排序
* 排序口径与 handler/employee.js 的公示列表保持一致:服务年限优先,累计接单数次之。
* rating 仍无写入点,不参与排序。
*/
EmployeeSchema.statics.findOnlineAttendants = async function (options = {}) {
const { page = 1, pageSize = 20 } = options;
@@ -267,12 +272,46 @@ EmployeeSchema.statics.findOnlineAttendants = async function (options = {}) {
status: "active",
"attendantInfo.isOnline": true,
})
.sort({ "work.workYears": -1 })
.sort({ "work.workYears": -1, "attendantInfo.orderCount": -1 })
.skip(skip)
.limit(pageSize)
.exec();
};
/**
* 累计接单数原子加一(接单 / 管理员指派成功后调用)
*/
EmployeeSchema.statics.incrementOrderCount = async function (employeeId) {
if (!employeeId) return null;
return await this.updateOne(
{ _id: employeeId },
{
$inc: { "attendantInfo.orderCount": 1 },
$set: { "meta.updatetime": Date.now() },
}
).exec();
};
/**
* 按登录账号更新接单开关(仅在职陪诊师)
*/
EmployeeSchema.statics.setOnlineByUserId = async function (userId, isOnline) {
if (!userId) return null;
return await this.findOneAndUpdate(
{ userId, role: "attendant", status: "active" },
{
$set: {
"attendantInfo.isOnline": Boolean(isOnline),
"meta.updatetime": Date.now(),
},
},
{ new: true }
)
.select(MY_EMPLOYEE_SELECT)
.lean()
.exec();
};
// ==================== 索引定义 ====================
EmployeeSchema.index({ "profile.name": 1 });
@@ -291,5 +330,5 @@ EmployeeSchema.index(
);
// genPinyin/genPinyinFL 一并导出:handler 层走 findOneAndUpdate 时绕过了 pre("save"),
// 需要自行同步拼音字段
export { EmployeeSchema, genPinyin, genPinyinFL };
// 需要自行同步拼音字段。MY_EMPLOYEE_SELECT 供 handler 复用,避免字段清单两处漂移
export { EmployeeSchema, genPinyin, genPinyinFL, MY_EMPLOYEE_SELECT };
+1 -1
View File
@@ -10,7 +10,7 @@ import mongoose from "mongoose";
* 但表本身按「员工」建,不绑死陪诊师——后期护士 / 医生等角色接评价时直接复用,
* 不必再建一张平行的表(这也是它叫 employee_review 而不是 attendant_review 的原因)。
*
* 设计前提(见 .trae/documents/陪诊师公示与指定陪诊师.md):
* 设计前提(见 doc/architecture-overview.html 第 07/10 节):
* - 一次服务最多产生一条评价,故 recordId 建唯一索引,从数据层断掉刷分
* - 评级不由 employee.attendantInfo.rating 提供——那个字段没有任何写入点
* - 只有 status 为 published 的评价计入评分,运营隐藏掉的不参与