async function combinedMethod() {
const batchSize = 100000; // 每次查询的批量大小
const startId = 49904812;
const endId = 133784381;
const totalIterations = Math.ceil((endId - startId + 1) / batchSize);
for (let i = 0; i < totalIterations; i++) {
const batchStartId = startId + i * batchSize;
const batchEndId = Math.min(batchStartId + batchSize - 1, endId);
const users = await this.userservice.find({
where: {
id: Between(batchStartId, batchEndId),
},
});
const promises = users.map(async (user) => {
const courtProvinceId = await getCourtProvinceId(user.court_province);
const courtCityId = await getCourtCityId(user.court_city);
if (courtProvinceId) {
user.court_province_id = courtProvinceId;
await this.userservice.save(user);
}
if (courtCityId) {
user.court_city_id = courtCityId;
await this.userservice.save(user);
}
});
await Promise.all(promises);
}
}
async function getCourtProvinceId(courtProvince) {
const courtProvinces = await this.authser.find({
where: {
name: `${courtProvince}省`,
},
});
if (courtProvinces.length > 0) {
return courtProvinces[0].code;
} else {
return null;
}
}
async function getCourtCityId(courtCity) {
try {
const courtCityData = await this.authser.findOne({ where: { name: courtCity } });
if (courtCityData) {
return courtCityData.code;
} else {
return null;
}
} catch (error) {
// 处理错误
return null;
}
}
查询可以合并为一次查询,后续的操作和保存似乎不影响其他数据,用promiseAll处理一下