Update README and project cleanup
This commit is contained in:
80
scripts/fix-family-role-final-slimdown.js
Normal file
80
scripts/fix-family-role-final-slimdown.js
Normal file
@@ -0,0 +1,80 @@
|
||||
const { getDb } = require('../db/init');
|
||||
|
||||
const db = getDb('onion');
|
||||
const CATEGORY_KEY = 'basic_info_role';
|
||||
|
||||
const MERGE_TO_OTHER = ['家长', '父母'];
|
||||
const DELETE_ONLY = ['妻子', '女儿', '姐姐', '儿子'];
|
||||
|
||||
function updateStats(dbConn) {
|
||||
const totalUsers = dbConn.prepare('SELECT COUNT(*) AS n FROM users').get().n || 1;
|
||||
const stmt = dbConn.prepare(`
|
||||
UPDATE tags
|
||||
SET
|
||||
coverage = (SELECT COUNT(DISTINCT user_id) FROM user_tags WHERE tag_id = tags.id),
|
||||
coverage_rate = ROUND((SELECT COUNT(DISTINCT user_id) FROM user_tags WHERE tag_id = tags.id) * 100.0 / ?, 2)
|
||||
WHERE id = ?
|
||||
`);
|
||||
|
||||
const tagIds = dbConn.prepare('SELECT id FROM tags').all();
|
||||
for (const tag of tagIds) stmt.run(totalUsers, tag.id);
|
||||
}
|
||||
|
||||
function main() {
|
||||
try {
|
||||
const category = db.prepare('SELECT id FROM tag_categories WHERE key = ?').get(CATEGORY_KEY);
|
||||
if (!category) throw new Error(`找不到分类: ${CATEGORY_KEY}`);
|
||||
const catId = category.id;
|
||||
|
||||
const getTag = db.prepare('SELECT id, name FROM tags WHERE category_id = ? AND name = ?');
|
||||
const mergeRel = db.prepare(`
|
||||
INSERT OR IGNORE INTO user_tags (user_id, tag_id)
|
||||
SELECT user_id, ? FROM user_tags WHERE tag_id = ?
|
||||
`);
|
||||
const deleteRel = db.prepare('DELETE FROM user_tags WHERE tag_id = ?');
|
||||
const deleteTag = db.prepare('DELETE FROM tags WHERE id = ?');
|
||||
|
||||
const other = getTag.get(catId, '其他监护人');
|
||||
if (!other) throw new Error('找不到“其他监护人”标签,无法合并');
|
||||
|
||||
const tx = db.transaction(() => {
|
||||
for (const name of MERGE_TO_OTHER) {
|
||||
const tag = getTag.get(catId, name);
|
||||
if (!tag || tag.id === other.id) continue;
|
||||
mergeRel.run(other.id, tag.id);
|
||||
deleteRel.run(tag.id);
|
||||
deleteTag.run(tag.id);
|
||||
console.log(`✅ 合并: ${name} -> 其他监护人`);
|
||||
}
|
||||
|
||||
for (const name of DELETE_ONLY) {
|
||||
const tag = getTag.get(catId, name);
|
||||
if (!tag) continue;
|
||||
deleteRel.run(tag.id);
|
||||
deleteTag.run(tag.id);
|
||||
console.log(`🗑️ 删除: ${name}`);
|
||||
}
|
||||
});
|
||||
|
||||
tx();
|
||||
updateStats(db);
|
||||
|
||||
const stats = db.prepare(`
|
||||
SELECT
|
||||
(SELECT COUNT(*) FROM tags t JOIN tag_categories c ON c.id=t.category_id WHERE c.key = ?) AS tag_count,
|
||||
(SELECT COUNT(*) FROM user_tags) AS rel_count
|
||||
`).get(CATEGORY_KEY);
|
||||
|
||||
console.log('\n✨ 二次收敛完成');
|
||||
console.log(` • 家庭角色标签剩余: ${stats.tag_count}`);
|
||||
console.log(` • 用户-标签关系总数: ${stats.rel_count}`);
|
||||
|
||||
db.close();
|
||||
} catch (error) {
|
||||
console.error('❌ 二次收敛失败:', error);
|
||||
try { db.close(); } catch (_) {}
|
||||
process.exit(1);
|
||||
}
|
||||
}
|
||||
|
||||
main();
|
||||
Reference in New Issue
Block a user