'use strict'; /** * 修复站内信群表 / 消息表 ext_data.source 的渠道错配。 * * 规则(生产 11 万+ 入站消息 100% 满足): * - group_id 以 @chatroom 结尾 → source 应为 wx(个人微信) * - group_id 为纯数字 → source 应为 qiwe(企业微信) * * 背景:群表 ext_data 的渠道来源会被回执/通知原样透传给桥接, * 错配时回执投递到错误渠道(典型:wx 群按 qiwe 投递 → "qiwe offline" 503,回信丢失)。 * * 用法: * 开发库 dry-run: node script/fix-inbox-group-channel.cjs * 开发库执行 : node script/fix-inbox-group-channel.cjs --apply * 生产库 dry-run: $env:NODE_ENV='production'; node script/fix-inbox-group-channel.cjs * 生产库执行 : $env:NODE_ENV='production'; node script/fix-inbox-group-channel.cjs --apply --messages * * 参数: * --apply 实际写入(默认仅统计将修改的行数) * --messages 同时修复 wms_inbox_message 历史记录(默认只修群表) * --no-backup 跳过备份(默认写入前把待改行导出到 temp/ 便于回滚) * * 幂等:只改 source 取值与群ID格式不符的行,可重复执行。 */ /** 条件:@chatroom 群却记了 qiwe */ const CHATROOM_AS_QIWE = "group_id LIKE '%@chatroom' AND JSON_UNQUOTE(JSON_EXTRACT(ext_data, '$.source')) = ?"; /** 条件:纯数字群却记了 wx */ const NUMERIC_AS_WX = "group_id REGEXP '^[0-9]+$' AND JSON_UNQUOTE(JSON_EXTRACT(ext_data, '$.source')) = ?"; (async () => { const args = process.argv.slice(2); const APPLY = args.includes('--apply'); const WITH_MESSAGES = args.includes('--messages'); const NO_BACKUP = args.includes('--no-backup'); try { const fs = require('node:fs'); const path = require('node:path'); const configs = require('../database/config.js'); const env = process.env.NODE_ENV || 'development'; const cfg = configs[env]; if (!cfg) { console.error(`NO_CONFIG_FOR_ENV: ${env}`); process.exit(2); } const mysql = require('mysql2/promise'); const conn = await mysql.createConnection({ host: cfg.host, port: cfg.port || 3306, user: cfg.username, password: cfg.password, database: cfg.database, }); console.log(`环境 : ${env}`); console.log(`目标库 : ${cfg.host}:${cfg.port || 3306}/${cfg.database}`); console.log(`模式 : ${APPLY ? 'APPLY(写入)' : 'DRY-RUN(只读)'}`); console.log(`修复范围 : 群表${WITH_MESSAGES ? ' + 消息表(历史记录)' : ''}\n`); const tasks = [ { table: 'wms_inbox_group', label: '群 @chatroom→wx ', where: CHATROOM_AS_QIWE, from: 'qiwe', to: 'wx' }, { table: 'wms_inbox_group', label: '群 数字→qiwe ', where: NUMERIC_AS_WX, from: 'wx', to: 'qiwe' }, ]; if (WITH_MESSAGES) { tasks.push( { table: 'wms_inbox_message', label: '消息 @chatroom→wx', where: CHATROOM_AS_QIWE, from: 'qiwe', to: 'wx' }, { table: 'wms_inbox_message', label: '消息 数字→qiwe ', where: NUMERIC_AS_WX, from: 'wx', to: 'qiwe' } ); } const backupRows = []; let total = 0; for (const task of tasks) { const sql = `SELECT id, group_id, group_name, ext_data, updated_at FROM ${task.table} WHERE ${task.where}`; const [ rows ] = await conn.query(sql, [ task.from ]); console.log(`[${task.label}] 待修复 ${rows.length} 行(source: ${task.from} → ${task.to})`); total += rows.length; for (const row of rows) { backupRows.push({ table: task.table, id: row.id, group_id: row.group_id, ext_data: row.ext_data }); } } if (total === 0) { console.log('\n无需修复,数据已符合「@chatroom→wx / 纯数字→qiwe」。'); await conn.end(); process.exit(0); } const backupFile = path.join( __dirname, '..', 'temp', `inbox-extdata-channel-backup-${new Date().toISOString().replace(/[:.]/g, '-')}.json` ); if (APPLY && !NO_BACKUP) { fs.mkdirSync(path.dirname(backupFile), { recursive: true }); fs.writeFileSync(backupFile, JSON.stringify(backupRows, null, 2), 'utf8'); console.log(`\n备份已写入: ${backupFile}(${backupRows.length} 行原值)`); } if (!APPLY) { console.log('\nDRY-RUN 结束,未修改任何数据。确认无误后加 --apply 执行。'); await conn.end(); process.exit(0); } console.log(''); for (const task of tasks) { const [ result ] = await conn.query( `UPDATE ${task.table} SET ext_data = JSON_SET(ext_data, '$.source', ?) WHERE ${task.where}`, [ task.to, task.from ] ); console.log(`[${task.label}] 已更新 ${result.affectedRows} 行`); } // 复核 console.log('\n复核(应全为 0):'); for (const task of tasks) { const [ rows ] = await conn.query( `SELECT COUNT(*) AS c FROM ${task.table} WHERE ${task.where}`, [ task.from ] ); console.log(` ${task.label} 剩余: ${rows[0].c}`); } await conn.end(); process.exit(0); } catch (err) { console.error('ERROR', err.message); process.exit(3); } })();