统一订单预约与服务端状态契约
由服务目录派生排期和费用,补齐幂等下单、取消退款联动及分页计数,避免客户端生成错误订单数据。 Co-Authored-By: Kimi <noreply@moonshot.cn>
This commit is contained in:
@@ -64,7 +64,20 @@ const EscortRecordSchema = mongoose.Schema(
|
||||
serviceName: { type: String, default: "", comment: "陪诊服务名称" },
|
||||
sexRequirement: { type: String, enum: ["none", "male", "female"], comment: "陪诊员性别要求" },
|
||||
},
|
||||
|
||||
|
||||
// 下单幂等键:同一用户重复提交同一键时返回原订单,不重复创建。
|
||||
idempotencyKey: {
|
||||
type: String,
|
||||
comment: "用户端下单幂等标识"
|
||||
},
|
||||
|
||||
// 收货信息是下单当刻的快照,不随用户资料或地址变化。
|
||||
shippingAddress: {
|
||||
recipientName: { type: String, default: "", comment: "收货人姓名" },
|
||||
mobile: { type: String, default: "", comment: "收货联系电话" },
|
||||
address: { type: String, default: "", comment: "完整收货地址" },
|
||||
},
|
||||
|
||||
/**
|
||||
* 陪诊员信息 - 为患者提供服务的陪诊员
|
||||
*/
|
||||
@@ -107,9 +120,9 @@ const EscortRecordSchema = mongoose.Schema(
|
||||
paidFee: { type: Number, default: 0, comment: "已支付费用(元)" },
|
||||
status: {
|
||||
type: String,
|
||||
enum: ["unpaid", "partial", "paid", "refunded"],
|
||||
enum: ["unpaid", "partial", "paid", "refunding", "refunded"],
|
||||
default: "unpaid",
|
||||
comment: "支付状态:未支付、部分支付、已支付、已退款",
|
||||
comment: "支付状态:未支付、部分支付、已支付、退款中、已退款",
|
||||
},
|
||||
},
|
||||
|
||||
@@ -137,6 +150,9 @@ const EscortRecordSchema = mongoose.Schema(
|
||||
comment: "陪诊状态:待确认、已确认、进行中、已完成、已取消",
|
||||
},
|
||||
|
||||
cancelReason: { type: String, default: "", comment: "订单取消原因" },
|
||||
cancelledAt: { type: Date, default: null, comment: "订单取消时间" },
|
||||
|
||||
/**
|
||||
* 元数据 - 系统管理信息
|
||||
*/
|
||||
@@ -165,29 +181,27 @@ const EscortRecordSchema = mongoose.Schema(
|
||||
* @param {Function} [cb] - 可选的回调函数
|
||||
* @returns {Promise<Array>} 陪诊记录列表(按预约日期倒序)
|
||||
*/
|
||||
EscortRecordSchema.statics.findRecords = async function (options = {}, cb) {
|
||||
const { page = 1, pageSize = 20, status, userId, appointmentDate, healthProfileId } = options;
|
||||
EscortRecordSchema.statics.buildRecordFilter = function (options = {}) {
|
||||
const { status, userId, appointmentDate, healthProfileId } = options;
|
||||
const filter = {};
|
||||
|
||||
if (status && Array.isArray(status) && status.length > 0) {
|
||||
filter.status = { $in: status };
|
||||
}
|
||||
|
||||
if (userId) {
|
||||
filter.userId = userId;
|
||||
}
|
||||
|
||||
if (healthProfileId) {
|
||||
filter.healthProfileId = healthProfileId;
|
||||
}
|
||||
|
||||
if (userId) filter.userId = userId;
|
||||
if (healthProfileId) filter.healthProfileId = healthProfileId;
|
||||
if (appointmentDate) {
|
||||
const start = new Date(appointmentDate);
|
||||
const start = new Date(`${appointmentDate}T00:00:00.000Z`);
|
||||
const end = new Date(start);
|
||||
end.setDate(end.getDate() + 1);
|
||||
end.setUTCDate(end.getUTCDate() + 1);
|
||||
filter["schedule.date"] = { $gte: start, $lt: end };
|
||||
}
|
||||
return filter;
|
||||
};
|
||||
|
||||
EscortRecordSchema.statics.findRecords = async function (options = {}, cb) {
|
||||
const { page = 1, pageSize = 20 } = options;
|
||||
const filter = this.buildRecordFilter(options);
|
||||
const skip = (page - 1) * pageSize;
|
||||
|
||||
return await this.find(filter)
|
||||
@@ -197,6 +211,21 @@ EscortRecordSchema.statics.findRecords = async function (options = {}, cb) {
|
||||
.exec(cb);
|
||||
};
|
||||
|
||||
// 列表记录、筛选总数和各页签计数一次返回,避免前端按当前页过滤或统计。
|
||||
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 skip = (page - 1) * pageSize;
|
||||
const [records, total, all, ongoing] = 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({ ...baseFilter, status: { $in: ["pending", "confirmed", "in_progress"] } }).exec(),
|
||||
]);
|
||||
return { records, total, counts: { all, ongoing } };
|
||||
};
|
||||
|
||||
/**
|
||||
* 查找陪诊员的陪诊记录
|
||||
*
|
||||
@@ -252,7 +281,20 @@ EscortRecordSchema.statics.createRecord = async function (record, cb) {
|
||||
*/
|
||||
EscortRecordSchema.statics.updateRecord = async function (id, update, cb) {
|
||||
const $set = { ...update, "meta.updatetime": Date.now() };
|
||||
return await this.findByIdAndUpdate(id, { $set }, { new: true }, cb);
|
||||
return await this.findByIdAndUpdate(id, { $set }, { new: true, runValidators: true }, cb);
|
||||
};
|
||||
|
||||
// 状态迁移必须仍以读取时的状态为条件,避免并发请求重复取消或跨过服务阶段。
|
||||
EscortRecordSchema.statics.updateRecordStatus = async function (id, fromStatus, update) {
|
||||
return await this.findOneAndUpdate(
|
||||
{ _id: id, status: fromStatus },
|
||||
{ $set: { ...update, "meta.updatetime": Date.now() } },
|
||||
{ new: true, runValidators: true }
|
||||
);
|
||||
};
|
||||
|
||||
EscortRecordSchema.statics.findByIdempotencyKey = async function (userId, idempotencyKey) {
|
||||
return await this.findOne({ userId, idempotencyKey }).exec();
|
||||
};
|
||||
|
||||
/**
|
||||
@@ -305,6 +347,12 @@ EscortRecordSchema.statics.deleteRecord = async function (id, cb) {
|
||||
*/
|
||||
EscortRecordSchema.index({ userId: 1, "schedule.date": -1 });
|
||||
|
||||
// 仅带幂等键的新订单参与唯一约束,历史订单不受影响。
|
||||
EscortRecordSchema.index(
|
||||
{ userId: 1, idempotencyKey: 1 },
|
||||
{ unique: true, sparse: true }
|
||||
);
|
||||
|
||||
/**
|
||||
* 健康档案ID索引 - 稀疏索引,用于快速按健康档案筛选记录
|
||||
*/
|
||||
|
||||
Reference in New Issue
Block a user