66 lines
1.7 KiB
JavaScript
66 lines
1.7 KiB
JavaScript
/**
|
|
* 一次性回填:按主体名称给存量医院写入常用昵称。
|
|
*
|
|
* 用法:在 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);
|
|
});
|
|
}
|