39 lines
1.1 KiB
TypeScript
39 lines
1.1 KiB
TypeScript
import * as dotenv from 'dotenv';
|
|
dotenv.config();
|
|
import pool from './src/config/database';
|
|
|
|
async function run() {
|
|
// 找出所有 self_score 为空但有 perf_item 的记录
|
|
const [perfs] = await pool.query<any[]>(
|
|
`SELECT perf_id FROM performance_month WHERE self_score IS NULL OR self_score = 0`
|
|
);
|
|
|
|
for (const perf of perfs) {
|
|
const [items] = await pool.query<any[]>(
|
|
'SELECT weight, self_score FROM perf_item WHERE perf_id = ? AND self_score IS NOT NULL',
|
|
[perf.perf_id]
|
|
);
|
|
if (items.length === 0) continue;
|
|
|
|
let weightedSum = 0;
|
|
let totalWeight = 0;
|
|
for (const item of items) {
|
|
weightedSum += Number(item.self_score) * Number(item.weight);
|
|
totalWeight += Number(item.weight);
|
|
}
|
|
if (totalWeight === 0) continue;
|
|
|
|
const selfScore = parseFloat((weightedSum / totalWeight).toFixed(2));
|
|
await pool.query(
|
|
'UPDATE performance_month SET self_score = ? WHERE perf_id = ?',
|
|
[selfScore, perf.perf_id]
|
|
);
|
|
console.log(`perfId=${perf.perf_id} 自评总分=${selfScore}`);
|
|
}
|
|
|
|
await pool.end();
|
|
console.log('完成');
|
|
}
|
|
|
|
run().catch(console.error);
|