'use strict'; /** * 补齐 wms_permission 表的 API 权限记录: * 1. 新增约 50 条 type='api' 的权限记录(幂等:先查后插) * 2. 修正 28 条现有记录的 meta_title(原值统一为"获取审核信息接口") * * 幂等设计: * - 新增:先查询 path + type='api' 是否已存在,已存在则跳过 * - 修正:UPDATE ... WHERE id = N AND meta_title = '获取审核信息接口' */ const TABLE = 'wms_permission'; /** * 批量查询指定 path 是否已存在 type='api' 的记录。 * @param {import('sequelize').QueryInterface} queryInterface 查询接口 * @param {string[]} paths 待查询的 path 列表 * @return {Promise>} 已存在的 path 集合 */ const getExistingApiPaths = async (queryInterface, paths) => { const placeholders = paths.map(() => '?').join(', '); const [ rows ] = await queryInterface.sequelize.query( `SELECT path FROM \`${TABLE}\` WHERE type = 'api' AND path IN (${placeholders})`, { replacements: paths } ); return new Set(rows.map(r => r.path)); }; /** * 正向迁移。 * @param {import('sequelize').QueryInterface} queryInterface 查询接口 * @return {Promise} 无返回值 */ module.exports.up = async queryInterface => { // ─── 1. 新增 50 条 API 权限 ───────────────────────────────── const newPermissions = [ // warehouse 模块 { path: 'warehouse.getWarehouseZones', meta_title: '获取所有仓库' }, { path: 'warehouse.create', meta_title: '创建实体库' }, { path: 'warehouse.update', meta_title: '更新仓库元素' }, { path: 'warehouse.delete', meta_title: '删除仓库元素' }, { path: 'warehouse.toggleActive', meta_title: '切换仓库元素启用状态' }, { path: 'warehouse.getPath', meta_title: '获取仓库路径' }, { path: 'warehouse.getPickPath', meta_title: '获取拣货路径' }, { path: 'warehouse.updateRackPosition', meta_title: '更新货架位置' }, { path: 'warehouse.recalculatePickPath', meta_title: '重算拣货路径' }, { path: 'warehouse.updateNeighbors', meta_title: '更新相邻仓位' }, { path: 'warehouse.getAreaRacks', meta_title: '获取区域货架' }, { path: 'warehouse.getWarehouseTree', meta_title: '获取仓库树' }, { path: 'warehouse.getChildren', meta_title: '获取仓库子元素' }, { path: 'warehouse.updateSpaceInfo', meta_title: '修改仓位信息' }, { path: 'warehouse.batchUpdateSpaceInfo', meta_title: '批量修改仓位信息' }, { path: 'warehouse.exportRackSpaces', meta_title: '导出货架仓位' }, // shops 模块 { path: 'shops.list', meta_title: '店铺列表' }, { path: 'shops.add', meta_title: '新增店铺' }, { path: 'shops.del', meta_title: '删除店铺' }, { path: 'shops.edit', meta_title: '编辑店铺' }, { path: 'shops.GetAuthorizeSessionKey', meta_title: '获取授权SessionKey' }, // stock.warehouse 模块 { path: 'stock.warehouse.saveWarehouseStock', meta_title: '保存商品资料' }, { path: 'stock.warehouse.createWarehouseStock', meta_title: '创建仓库商品' }, { path: 'stock.warehouse.import', meta_title: '商品导入' }, { path: 'stock.warehouse.saveWarehouseStockBarcode', meta_title: '修改商品条码' }, { path: 'stock.warehouse.bindItem', meta_title: '绑定货主商品' }, { path: 'stock.warehouse.unbindItem', meta_title: '解绑货主商品' }, { path: 'stock.warehouse.handleBatchDelete', meta_title: '批量删除仓库商品' }, { path: 'stock.warehouse.handleBatchChange', meta_title: '批量修改商品' }, // index 模块 { path: 'index.ReferenceFile', meta_title: '图片引用设置' }, { path: 'index.test', meta_title: '测试接口' }, { path: 'index.getShopList', meta_title: '获取仓库店铺列表' }, { path: 'index.getShopExpressList', meta_title: '获取店铺签约快递' }, { path: 'index.createPlatformOauthReservation', meta_title: '创建平台授权预留码' }, // order 模块 { path: 'order.bindExpressPackageMaterial', meta_title: '绑定快递包材' }, { path: 'order.pullReviewDataByWaybill', meta_title: '按运单拉取复核数据' }, // order.wave 模块 { path: 'order.wave.waveStrategyCreate', meta_title: '波次策略新增' }, { path: 'order.wave.waveStrategyUpdate', meta_title: '波次策略修改' }, { path: 'order.wave.wavePickTaskCancel', meta_title: '拣货任务取消' }, { path: 'order.wave.previewWaveStrategyNextFetchOrderCount', meta_title: '预演策略抓取数量' }, // order.* 明细模块 { path: 'order.pendingOrder.detail', meta_title: '待处理订单明细' }, { path: 'order.archiveOrder.detail', meta_title: '归档订单明细' }, { path: 'order.deliveryOrder.detail', meta_title: '配送订单明细' }, { path: 'order.deliveryExpectionOrder.detail', meta_title: '配送异常订单明细' }, { path: 'order.entrustOrder.detail', meta_title: '委托外仓订单明细' }, { path: 'order.reviewOrder.detail', meta_title: '待审核订单明细' }, { path: 'order.outboundOrder.detail', meta_title: '待出库订单明细' }, { path: 'order.expectionOrder.detail', meta_title: '异常订单明细' }, // storage.inventory 模块 { path: 'storage.inventory.quickPutaway', meta_title: '快递入库' }, // store 模块 { path: 'store.getStoreRoles', meta_title: '获取审核信息' }, { path: 'store.auditApply', meta_title: '提交审核' }, // storeWarehouse.store 模块 { path: 'storeWarehouse.store.adjustPrintQuantity', meta_title: '货主余额变更' }, { path: 'storeWarehouse.store.queryPrintQuantityLogs', meta_title: '货主面单余额查询' }, ]; const allPaths = newPermissions.map(p => p.path); const existingPaths = await getExistingApiPaths(queryInterface, allPaths); const toInsert = newPermissions.filter(p => !existingPaths.has(p.path)); if (toInsert.length > 0) { // 构建批量 INSERT 语句,name 与 path 保持一致 const values = toInsert .map( p => `('${p.path.replace(/'/g, "\\'")}', '${p.path.replace(/'/g, "\\'")}', '${p.meta_title.replace(/'/g, "\\'")}', 'api', 1, NOW(), NOW())` ) .join(',\n'); await queryInterface.sequelize.query(` INSERT INTO \`${TABLE}\` (path, name, meta_title, type, status, created_at, update_at) VALUES ${values} `); } console.log( `[Migration] wms_permission API 权限新增:共 ${newPermissions.length} 条,` + `已存在 ${newPermissions.length - toInsert.length} 条,实际插入 ${toInsert.length} 条` ); // ─── 2. 修正 28 条现有记录的 meta_title ──────────────────── const corrections = [ { id: 52, meta_title: '获取审核信息' }, { id: 84, meta_title: '获取COS上传配置' }, { id: 85, meta_title: '获取快递列表' }, { id: 86, meta_title: '自动翻译新键' }, { id: 87, meta_title: '上传成功通知' }, { id: 88, meta_title: '获取订单模型列表' }, { id: 89, meta_title: '绑定货主商品到仓库' }, { id: 90, meta_title: '创建禁售规则' }, { id: 91, meta_title: '创建单位' }, { id: 92, meta_title: '创建仓库商品' }, { id: 93, meta_title: '删除禁售规则' }, { id: 94, meta_title: '删除单位' }, { id: 95, meta_title: '批量修改商品' }, { id: 96, meta_title: '批量删除商品' }, { id: 97, meta_title: '保存商品资料' }, { id: 98, meta_title: '修改商品条码' }, { id: 99, meta_title: '解除绑定货主商品' }, { id: 100, meta_title: '添加收货明细' }, { id: 101, meta_title: '删除收货明细' }, { id: 102, meta_title: '保存收货单' }, { id: 103, meta_title: '创建货架' }, { id: 104, meta_title: '创建货架含仓位' }, { id: 105, meta_title: '重算拣货路径' }, { id: 106, meta_title: '更新相邻仓位' }, { id: 107, meta_title: '更新货架位置' }, { id: 108, meta_title: '获取商品信息' }, { id: 306, meta_title: '获取订单模型列表' }, { id: 307, meta_title: '获取区域货架' }, ]; let correctedCount = 0; for (const { id, meta_title } of corrections) { const [ result ] = await queryInterface.sequelize.query( `UPDATE \`${TABLE}\` SET meta_title = :meta_title, update_at = NOW() WHERE id = :id AND meta_title = '获取审核信息接口'`, { replacements: { id, meta_title } } ); // result 为 affectedRows(MySQL)或 rowCount if (result && (result.affectedRows > 0 || result.rowCount > 0 || result.changedRows > 0)) { correctedCount++; } } console.log( `[Migration] wms_permission meta_title 修正:共 ${corrections.length} 条,` + `实际修正 ${correctedCount} 条` ); // ─── 3. 修正 10 条 meta_title 为 "path+接口" 格式的记录 ────── const pathLikeCorrections = [ { id: 347, path: 'warehouse.getChildren', meta_title: '获取仓库子元素' }, { id: 348, path: 'warehouse.toggleActive', meta_title: '切换仓库启用状态' }, { id: 349, path: 'warehouse.getPath', meta_title: '获取仓库路径' }, { id: 350, path: 'warehouse.getPickPath', meta_title: '获取拣货路径' }, { id: 351, path: 'storage.receiving.save', meta_title: '保存收货单' }, { id: 367, path: 'shops.list', meta_title: '店铺列表' }, { id: 368, path: 'shops.add', meta_title: '新增店铺' }, { id: 369, path: 'shops.del', meta_title: '删除店铺' }, { id: 370, path: 'shops.edit', meta_title: '编辑店铺' }, { id: 371, path: 'shop.GetAuthorizeSessionKey', meta_title: '获取授权SessionKey' }, ]; let pathLikeCorrectedCount = 0; for (const { id, meta_title } of pathLikeCorrections) { const [ result ] = await queryInterface.sequelize.query( `UPDATE \`${TABLE}\` SET meta_title = :meta_title, update_at = NOW() WHERE id = :id AND meta_title LIKE '%接口'`, { replacements: { id, meta_title } } ); if (result && (result.affectedRows > 0 || result.rowCount > 0 || result.changedRows > 0)) { pathLikeCorrectedCount++; } } console.log( `[Migration] wms_permission "path+接口" 格式修正:共 ${pathLikeCorrections.length} 条,` + `实际修正 ${pathLikeCorrectedCount} 条` ); // ── 3b. 修正 setting.permission.add/delete/update 的 meta_title ── const permissionCrudCorrections = [ { id: 119, path: 'setting.permission.add', meta_title: '新增权限' }, { id: 120, path: 'setting.permission.delete', meta_title: '删除权限' }, { id: 121, path: 'setting.permission.update', meta_title: '修改权限' }, ]; let permCrudCorrectedCount = 0; for (const { id, path, meta_title } of permissionCrudCorrections) { const [ result ] = await queryInterface.sequelize.query( `UPDATE \`${TABLE}\` SET meta_title = :meta_title, update_at = NOW() WHERE id = :id AND meta_title = :oldMetaTitle`, { replacements: { id, meta_title, oldMetaTitle: path } } ); if (result && (result.affectedRows > 0 || result.rowCount > 0 || result.changedRows > 0)) { permCrudCorrectedCount++; } } console.log( `[Migration] wms_permission setting.permission CRUD 修正:共 ${permissionCrudCorrections.length} 条,` + `实际修正 ${permCrudCorrectedCount} 条` ); }; /** * 回滚迁移:无法安全回滚 meta_title 修正与新增记录(可能已被后续业务引用), * 仅输出提示信息。 * @return {Promise} 无返回值 */ module.exports.down = async () => { console.log( '[Migration] wms_permission API 权限补齐脚本无安全回滚方案,' + '如需回滚请手动处理 meta_title 与新增记录。' ); };