feat(hospital): 新增医院昵称并支持昵称搜索
This commit is contained in:
@@ -0,0 +1,65 @@
|
||||
/**
|
||||
* 一次性回填:按主体名称给存量医院写入常用昵称。
|
||||
*
|
||||
* 用法:在 api/health 下执行 `node scripts/backfill_hospital_nicknames.js`
|
||||
* 同一主体下的分院/院区共享同一组昵称;未收录的医院写空数组。
|
||||
*/
|
||||
import { DBModel } from "../models/index.js";
|
||||
import { resolveHospitalNicknames } from "../resource/hospital_nicknames.js";
|
||||
|
||||
DBModel.init();
|
||||
const conn = DBModel.dbConnection;
|
||||
|
||||
async function backfill() {
|
||||
const hospitals = await DBModel.Hospital.find(
|
||||
{},
|
||||
{ name: 1, entityName: 1, nicknames: 1 }
|
||||
).lean();
|
||||
|
||||
const operations = [];
|
||||
let withNicknames = 0;
|
||||
|
||||
for (const hospital of hospitals) {
|
||||
const nicknames = resolveHospitalNicknames(hospital.entityName, hospital.name);
|
||||
if (nicknames.length > 0) withNicknames += 1;
|
||||
|
||||
const existing = Array.isArray(hospital.nicknames) ? hospital.nicknames : [];
|
||||
const unchanged = existing.length === nicknames.length
|
||||
&& existing.every((value, index) => value === nicknames[index]);
|
||||
if (unchanged) continue;
|
||||
|
||||
operations.push({
|
||||
updateOne: {
|
||||
filter: { _id: hospital._id },
|
||||
update: { $set: { nicknames } },
|
||||
},
|
||||
});
|
||||
}
|
||||
|
||||
if (operations.length > 0) {
|
||||
await DBModel.Hospital.bulkWrite(operations);
|
||||
}
|
||||
|
||||
console.log(
|
||||
`backfilled: total=${hospitals.length}, withNicknames=${withNicknames}, updated=${operations.length}`
|
||||
);
|
||||
await conn.close();
|
||||
}
|
||||
|
||||
if (conn.readyState === 1) {
|
||||
backfill().catch((error) => {
|
||||
console.error(error);
|
||||
process.exit(1);
|
||||
});
|
||||
} else {
|
||||
conn.once("connected", () => {
|
||||
backfill().catch((error) => {
|
||||
console.error(error);
|
||||
process.exit(1);
|
||||
});
|
||||
});
|
||||
conn.once("error", (error) => {
|
||||
console.error(error);
|
||||
process.exit(1);
|
||||
});
|
||||
}
|
||||
Reference in New Issue
Block a user