diff --git a/app/model/inbox_group.js b/app/model/inbox_group.js index 4a3039b..63e6cd3 100644 --- a/app/model/inbox_group.js +++ b/app/model/inbox_group.js @@ -105,6 +105,11 @@ module.exports = app => { deletedAt: 'deleted_at', paranoid: true, comment: '站内信群表', + indexes: [ + // 群唯一性以 (warehouse_id, group_id) 为准,群名称变化只更新不建新 + { name: 'uk_warehouse_group_id', unique: true, fields: [ 'warehouse_id', 'group_id' ] }, + { name: 'idx_group_id', fields: [ 'group_id' ] }, + ], }); return InboxGroup; diff --git a/app/service/inbox/receiver.js b/app/service/inbox/receiver.js index 4eaab68..9c4529b 100644 --- a/app/service/inbox/receiver.js +++ b/app/service/inbox/receiver.js @@ -145,20 +145,61 @@ class InboxReceiverService extends Service { ctx.logger.info(`[inbox] 消息入库成功 ext_id=${extId} id=${record.id}`); - // 自动登记群(幂等 upsert) + // 自动登记群:优先按 (group_id, warehouse_id) 幂等 upsert。 + // 群ID不变、仅名称变化时只更新名称,不重复建群。 const groupName = String(data.group_name || '').trim(); if (groupName) { try { - await ctx.model.InboxGroup.findOrCreate({ - where: { group_name: groupName, warehouse_id: warehouseId }, - defaults: { - group_id: extGroupId, - store_id: storeId || null, - source_type: sourceType, - source_extra: data.source_extra || null, - ai_enabled: 0, - }, - }); + if (extGroupId) { + const group = await ctx.model.InboxGroup.findOne({ + where: { group_id: extGroupId, warehouse_id: warehouseId }, + }); + + if (group) { + // 群已存在:名称变化时更新名称,不新建 + if (group.group_name !== groupName) { + const oldName = group.group_name; + await group.update({ group_name: groupName }); + ctx.logger.info( + `[inbox] 群名称已更新 group_id=${extGroupId}: ${oldName} → ${groupName}` + ); + } + } else { + // 群ID不存在:优先兼容同名群回填群ID;都没有才新建。 + // 注意表上已无 (group_name, warehouse_id) 唯一索引,名称维度不再保证唯一。 + const byName = await ctx.model.InboxGroup.findOne({ + where: { group_name: groupName, warehouse_id: warehouseId }, + }); + if (byName) { + await byName.update({ group_id: extGroupId }); + ctx.logger.info( + `[inbox] 同名群回填群ID group_id=${extGroupId} name=${groupName}` + ); + } else { + await ctx.model.InboxGroup.create({ + group_name: groupName, + group_id: extGroupId, + warehouse_id: warehouseId, + store_id: storeId || null, + source_type: sourceType, + source_extra: data.source_extra || null, + ai_enabled: 0, + }); + } + } + } else { + // 无群ID:按 (group_name, warehouse_id) 幂等登记(向后兼容) + await ctx.model.InboxGroup.findOrCreate({ + where: { group_name: groupName, warehouse_id: warehouseId }, + defaults: { + group_id: null, + store_id: storeId || null, + source_type: sourceType, + source_extra: data.source_extra || null, + ai_enabled: 0, + }, + }); + } } catch (groupErr) { ctx.logger.warn(`[inbox] 群登记失败: ${groupErr.message}`); } diff --git a/database/migrations/20260815100000-fix-inbox-group-unique-index.js b/database/migrations/20260815100000-fix-inbox-group-unique-index.js new file mode 100644 index 0000000..1d70c66 --- /dev/null +++ b/database/migrations/20260815100000-fix-inbox-group-unique-index.js @@ -0,0 +1,138 @@ +'use strict'; + +const TABLE_NAME = 'wms_inbox_group'; + +/** 合并重复记录时允许从重复行补齐到保留行的字段 */ +const FILLABLE_FIELDS = [ + 'store_id', + 'group_type', + 'sub_type', + 'source_type', + 'source_extra', + 'express_interfaces', + 'system_express_code', +]; + +async function indexExists(queryInterface, tableName, indexName) { + const indexes = await queryInterface.showIndex(tableName); + return (indexes || []).some(i => (typeof i === 'string' ? i : i.name) === indexName); +} + +async function dropIndexIfExists(queryInterface, tableName, indexName) { + if (await indexExists(queryInterface, tableName, indexName)) { + await queryInterface.removeIndex(tableName, indexName); + console.log(`[idx] ${tableName}.${indexName} 已删除`); + } +} + +async function addIndexIfNotExists(queryInterface, tableName, indexName, options = {}) { + if (await indexExists(queryInterface, tableName, indexName)) { + console.log(`[idx] ${tableName}.${indexName} 已存在,跳过`); + return; + } + await queryInterface.addIndex(tableName, options.fields, { + name: indexName, + unique: !!options.unique, + }); + console.log(`[idx] ${tableName}.${indexName} 已创建`); +} + +/** + * 合并 (warehouse_id, group_id) 相同的重复群记录,为唯一索引铺路。 + * 保留策略:is_blocked=0(启用)优先,其次 created_at 最早; + * 群名称取 updated_at 最新一条的名称;其余字段为空时从重复行补齐;其余行软删除。 + * + * @param {Object} queryInterface Sequelize QueryInterface 实例。 + * @return {Promise} + */ +async function mergeDuplicateGroups(queryInterface) { + const q = queryInterface.sequelize; + + const dupGroups = await q.query( + `SELECT warehouse_id, group_id + FROM \`${TABLE_NAME}\` + WHERE group_id IS NOT NULL AND group_id <> '' AND deleted_at IS NULL + GROUP BY warehouse_id, group_id + HAVING COUNT(*) > 1`, + { type: q.QueryTypes.SELECT } + ); + + for (const g of dupGroups) { + const rows = await q.query( + `SELECT * FROM \`${TABLE_NAME}\` + WHERE warehouse_id = ? AND group_id = ? AND deleted_at IS NULL + ORDER BY is_blocked ASC, created_at ASC, id ASC`, + { replacements: [ g.warehouse_id, g.group_id ], type: q.QueryTypes.SELECT } + ); + if (!rows || rows.length < 2) continue; + + const keep = rows[0]; + const nameRow = [ ...rows ].sort((a, b) => new Date(b.updated_at) - new Date(a.updated_at))[0]; + const dropIds = rows.filter(r => r.id !== keep.id).map(r => r.id); + + const updates = {}; + if (keep.group_name !== nameRow.group_name) { + updates.group_name = nameRow.group_name; + } + for (const field of FILLABLE_FIELDS) { + if (keep[field] === null || keep[field] === undefined || keep[field] === '') { + for (const row of rows) { + if (row.id === keep.id) continue; + if (row[field] !== null && row[field] !== undefined && row[field] !== '') { + updates[field] = row[field]; + break; + } + } + } + } + + if (Object.keys(updates).length > 0) { + const setClauses = Object.keys(updates).map(k => `\`${k}\` = ?`).join(', '); + await q.query( + `UPDATE \`${TABLE_NAME}\` SET ${setClauses}, updated_at = NOW() WHERE id = ?`, + { replacements: [ ...Object.values(updates), keep.id ] } + ); + console.log(`[dedupe] 保留 id=${keep.id} group_id=${g.group_id},更新: ${JSON.stringify(updates)}`); + } + + const placeholders = dropIds.map(() => '?').join(','); + await q.query( + `UPDATE \`${TABLE_NAME}\` SET deleted_at = NOW() WHERE id IN (${placeholders}) AND deleted_at IS NULL`, + { replacements: dropIds } + ); + console.log(`[dedupe] 软删除 id=${dropIds.join(',')} group_id=${g.group_id}`); + } +} + +module.exports = { + // eslint-disable-next-line no-unused-vars + up: async (queryInterface, Sequelize) => { + // 1. 清理 (warehouse_id, group_id) 重复记录,否则唯一索引无法创建 + await mergeDuplicateGroups(queryInterface); + + // 2. 删除错误的唯一索引 uk_group_warehouse(group_name, warehouse_id) + // 群名称会变化,不应作为唯一约束维度 + await dropIndexIfExists(queryInterface, TABLE_NAME, 'uk_group_warehouse'); + + // 3. 删除与唯一索引覆盖范围重复的普通索引 idx_warehouse_group_id + await dropIndexIfExists(queryInterface, TABLE_NAME, 'idx_warehouse_group_id'); + + // 4. 新建唯一索引 (warehouse_id, group_id) + // group_id 允许 NULL,MySQL 唯一索引允许多个 NULL,不影响无群ID的记录 + await addIndexIfNotExists(queryInterface, TABLE_NAME, 'uk_warehouse_group_id', { + fields: [ 'warehouse_id', 'group_id' ], + unique: true, + }); + }, + + down: async queryInterface => { + await dropIndexIfExists(queryInterface, TABLE_NAME, 'uk_warehouse_group_id'); + await addIndexIfNotExists(queryInterface, TABLE_NAME, 'idx_warehouse_group_id', { + fields: [ 'warehouse_id', 'group_id' ], + }); + await addIndexIfNotExists(queryInterface, TABLE_NAME, 'uk_group_warehouse', { + fields: [ 'group_name', 'warehouse_id' ], + unique: true, + }); + }, +}; diff --git a/script/merge-inbox-group-duplicates.cjs b/script/merge-inbox-group-duplicates.cjs new file mode 100644 index 0000000..515e474 --- /dev/null +++ b/script/merge-inbox-group-duplicates.cjs @@ -0,0 +1,205 @@ +/** + * 合并 wms_inbox_group 中 (warehouse_id, group_id) 相同的重复群记录。 + * + * 背景:receiver.js 历史上按 (group_name, warehouse_id) 登记群, + * 群改名后会插入同群ID的新记录,产生重复。 + * 合并策略: + * 1. 保留记录:is_blocked=0(启用)优先,其次 created_at 最早。 + * 2. 保留记录的名称:取 updated_at 最新一条的名称(即当前最新群名)。 + * 3. 其余字段:保留记录为空时,从重复记录中补齐非空值。 + * 4. 其余重复记录软删除(deleted_at = NOW())。 + * + * 用法: + * node script/merge-inbox-group-duplicates.cjs --dry-run # 只预览,默认 + * node script/merge-inbox-group-duplicates.cjs --apply # 实际执行 + */ +const fs = require('fs'); +const path = require('path'); +const mysql = require('mysql2/promise'); + +const ROOT = process.cwd(); +const DEFAULT_CONFIG_PATH = path.join(ROOT, 'config', 'config.local.js'); +const TABLE_NAME = 'wms_inbox_group'; + +/** 允许从重复记录补齐到保留记录的字段 */ +const FILLABLE_FIELDS = [ + 'store_id', + 'group_type', + 'sub_type', + 'source_type', + 'source_extra', + 'express_interfaces', + 'system_express_code', +]; + +function parseArgs(argv) { + const args = { dryRun: true, apply: false, dbClient: 'wms', configPath: DEFAULT_CONFIG_PATH }; + for (const item of argv) { + if (item === '--apply') args.apply = true; + if (item === '--dry-run') args.dryRun = true; + if (item.startsWith('--db-client=')) args.dbClient = String(item.split('=')[1] || '').trim() || 'wms'; + if (item.startsWith('--config-path=')) args.configPath = path.resolve(ROOT, String(item.split('=')[1] || '').trim()); + } + if (args.apply) args.dryRun = false; + return args; +} + +function loadDbClient(configPath, dbClientName) { + if (!fs.existsSync(configPath)) { + throw new Error(`Config file not found: ${configPath}`); + } + const configFactory = require(configPath); + const cfg = typeof configFactory === 'function' + ? configFactory({ name: 'wms-inbox-group-merge' }) + : configFactory; + const dbClient = cfg && cfg.mysql && cfg.mysql.clients && cfg.mysql.clients[dbClientName]; + if (!dbClient) { + throw new Error(`MySQL client not found in config.mysql.clients: ${dbClientName}`); + } + return dbClient; +} + +async function findDuplicateGroups(conn) { + const [ rows ] = await conn.query( + `SELECT warehouse_id, group_id, COUNT(*) AS cnt + FROM \`${TABLE_NAME}\` + WHERE group_id IS NOT NULL AND group_id <> '' AND deleted_at IS NULL + GROUP BY warehouse_id, group_id + HAVING cnt > 1 + ORDER BY warehouse_id, group_id` + ); + return rows || []; +} + +async function fetchGroupRows(conn, warehouseId, groupId) { + const [ rows ] = await conn.query( + `SELECT id, group_name, group_id, warehouse_id, store_id, group_type, sub_type, + source_type, source_extra, express_interfaces, system_express_code, + ai_enabled, is_blocked, created_at, updated_at, deleted_at + FROM \`${TABLE_NAME}\` + WHERE warehouse_id = ? AND group_id = ? AND deleted_at IS NULL`, + [ warehouseId, groupId ] + ); + return rows || []; +} + +function pickKeepRow(rows) { + // 启用优先,其次创建时间最早,最后 id 最小 + return [ ...rows ].sort((a, b) => { + if (Number(a.is_blocked) !== Number(b.is_blocked)) return Number(a.is_blocked) - Number(b.is_blocked); + if (a.created_at !== b.created_at) return new Date(a.created_at) - new Date(b.created_at); + return a.id - b.id; + })[0]; +} + +function pickNewestNameRow(rows) { + return [ ...rows ].sort((a, b) => { + const da = new Date(a.updated_at); + const db = new Date(b.updated_at); + if (da !== db) return db - da; + return b.id - a.id; + })[0]; +} + +function buildMergePlan(rows) { + const keepRow = pickKeepRow(rows); + const nameRow = pickNewestNameRow(rows); + const dropRows = rows.filter(r => r.id !== keepRow.id); + + const updates = {}; + if (keepRow.group_name !== nameRow.group_name) { + updates.group_name = nameRow.group_name; + } + + // 保留记录字段为空时,从重复记录补齐非空值 + for (const field of FILLABLE_FIELDS) { + if (keepRow[field] === null || keepRow[field] === undefined || keepRow[field] === '') { + for (const row of rows) { + if (row.id === keepRow.id) continue; + if (row[field] !== null && row[field] !== undefined && row[field] !== '') { + updates[field] = row[field]; + break; + } + } + } + } + + return { keepRow, nameRow, dropRows, updates }; +} + +async function main() { + const args = parseArgs(process.argv.slice(2)); + const dbClient = loadDbClient(args.configPath, args.dbClient); + + const conn = await mysql.createConnection({ + host: dbClient.host, + port: dbClient.port, + user: dbClient.user, + password: dbClient.password, + database: dbClient.database, + }); + + try { + const duplicates = await findDuplicateGroups(conn); + console.log(`发现 ${duplicates.length} 组重复群 (group_id 相同):\n`); + + if (duplicates.length === 0) { + console.log('无重复群,无需处理。'); + return; + } + + let mergedCount = 0; + let updateCount = 0; + + for (const dup of duplicates) { + const rows = await fetchGroupRows(conn, dup.warehouse_id, dup.group_id); + if (rows.length < 2) continue; + + const { keepRow, nameRow, dropRows, updates } = buildMergePlan(rows); + mergedCount += 1; + if (Object.keys(updates).length > 0) updateCount += 1; + + console.log( + `[${dup.warehouse_id}] ${dup.group_id} (${rows.length} 条): ` + + `保留 id=${keepRow.id} "${keepRow.group_name}"` + ); + if (updates.group_name) { + console.log(` 名称更新: "${keepRow.group_name}" → "${updates.group_name}"`); + } + const otherUpdates = { ...updates }; + delete otherUpdates.group_name; + if (Object.keys(otherUpdates).length > 0) { + console.log(` 字段补齐: ${JSON.stringify(otherUpdates)}`); + } + for (const drop of dropRows) { + console.log(` 软删除: id=${drop.id} "${drop.group_name}" (${drop.is_blocked ? '屏蔽' : '启用'})`); + } + console.log(` 最新名称来源: id=${nameRow.id} "${nameRow.group_name}"`); + + if (!args.dryRun) { + if (Object.keys(updates).length > 0) { + await conn.query( + `UPDATE \`${TABLE_NAME}\` SET ? WHERE id = ?`, + [ updates, keepRow.id ] + ); + } + for (const drop of dropRows) { + await conn.query( + `UPDATE \`${TABLE_NAME}\` SET deleted_at = NOW() WHERE id = ?`, + [ drop.id ] + ); + } + } + } + + console.log(`\n${args.dryRun ? '[dry-run] 预览完成' : '执行完成'}: ` + + `共 ${mergedCount} 组重复群,其中 ${updateCount} 组需要更新保留记录。`); + } finally { + await conn.end(); + } +} + +main().catch(err => { + console.error('执行失败:', err.message); + process.exit(1); +});