diff --git a/backend/app/controller/customExpressCode.js b/backend/app/controller/customExpressCode.js index 0c6449e..b4d7086 100644 --- a/backend/app/controller/customExpressCode.js +++ b/backend/app/controller/customExpressCode.js @@ -65,17 +65,42 @@ class CustomExpressCodeController extends Controller { offset, limit, order: [[ 'sort', 'ASC' ], [ 'id', 'ASC' ]], - include: [ - { - model: ctx.model.ExpressCompany, - as: 'expressCompany', - attributes: [ 'id', 'code', 'name' ], - required: false, - }, - ], + raw: true, }); - ctx.body = { code: 0, message: 'success', data: buildPageResult(rows, count, { page, pageSize }) }; + ctx.body = { + code: 0, + message: 'success', + data: buildPageResult(await this._attachExpressCompany(rows), count, { page, pageSize }), + }; + } + + /** + * 给记录补上对应的系统快递(id/code/name) + * + * 不使用 Sequelize include(LEFT JOIN):custom_express_codes 与 express_companies 若字符集/排序规则 + * 不一致(历史环境常见),JOIN 会直接抛 ER_CANT_AGGREGATE_2COLLATIONS 导致列表 500; + * 这里改成一次 IN 查询后在内存里组装,既避免排序规则冲突,也少一次 JOIN。 + * @param {Array} rows 记录(已转 plain / raw) + * @return {Promise>} 带 expressCompany / express_name 的记录 + */ + async _attachExpressCompany(rows) { + const { ctx } = this; + const Op = this.app.Sequelize.Op; + const list = rows || []; + const codes = [ ...new Set(list.map(row => row.express_code).filter(Boolean)) ]; + const companies = codes.length + ? await ctx.model.ExpressCompany.findAll({ + where: { code: { [Op.in]: codes } }, + attributes: [ 'id', 'code', 'name' ], + raw: true, + }) + : []; + const byCode = new Map(companies.map(company => [ company.code, company ])); + return list.map(row => { + const company = byCode.get(row.express_code) || null; + return { ...row, expressCompany: company, express_name: company ? company.name : '' }; + }); } /** @@ -133,21 +158,13 @@ class CustomExpressCodeController extends Controller { */ async show() { const { ctx } = this; - const item = await ctx.model.CustomExpressCode.findByPk(ctx.params.id, { - include: [ - { - model: ctx.model.ExpressCompany, - as: 'expressCompany', - attributes: [ 'id', 'code', 'name' ], - required: false, - }, - ], - }); + const item = await ctx.model.CustomExpressCode.findByPk(ctx.params.id, { raw: true }); if (!item) { this._fail(ctx, notFound('自定义快递编码不存在'), '自定义快递编码不存在'); return; } - ctx.body = { code: 0, message: 'success', data: item }; + const [ data ] = await this._attachExpressCompany([ item ]); + ctx.body = { code: 0, message: 'success', data }; } /** diff --git a/backend/database/migrations/20261105090000-normalize-charset-for-dictionary-tables.js b/backend/database/migrations/20261105090000-normalize-charset-for-dictionary-tables.js new file mode 100644 index 0000000..336ad1f --- /dev/null +++ b/backend/database/migrations/20261105090000-normalize-charset-for-dictionary-tables.js @@ -0,0 +1,41 @@ +'use strict'; + +/** + * 统一「字典表 / 任务表」的字符集与排序规则 + * + * 这些表当初建表时没有显式指定 charset/collate,字符集由数据库默认值决定: + * - 本地/部分环境默认是 utf8mb3(utf8_general_ci) + * - 另一些环境默认是 utf8mb4(utf8mb4_general_ci 或 utf8mb4_0900_ai_ci) + * 而 `express_companies` 等核心字典表固定为 utf8mb4 / utf8mb4_unicode_ci。 + * + * 只要两边同字符集但排序规则不同,MySQL 在 JOIN / 比较时直接报 + * `ER_CANT_AGGREGATE_2COLLATIONS: Illegal mix of collations`(自定义快递编码列表 500 即由此触发), + * 因此这里把相关表统一转换成 utf8mb4 / utf8mb4_unicode_ci。 + */ +const TABLES = [ + 'custom_express_codes', + 'external_print_express_interfaces', + 'warehouse_express_mappings', + 'waybill_fetch_records', + 'order_sync_tasks', + 'shop_database_configs', + 'operation_logs', + 'openapi_request_idempotency', +]; + +module.exports = { + async up(queryInterface) { + const existTables = new Set(await queryInterface.showAllTables()); + for (const table of TABLES) { + if (!existTables.has(table)) continue; + await queryInterface.sequelize.query( + `ALTER TABLE \`${table}\` CONVERT TO CHARACTER SET utf8mb4 COLLATE utf8mb4_unicode_ci` + ); + } + }, + + // 字符集统一属于修复性变更(回滚会重新引入排序规则冲突导致 JOIN 报错),故不做反向操作 + async down() { + return Promise.resolve(); + }, +};