Files
dafuweng-saiadmin6.x/server/db/dept_flatten_channels.sql
2026-05-26 09:43:42 +08:00

27 lines
1.3 KiB
SQL
Raw Blame History

This file contains ambiguous Unicode characters
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
-- 渠道扁平化:将子渠道用户归并到顶级渠道,删除子渠道,更新表注释
-- 执行前请备份数据库
-- 1. 表及字段注释改为「渠道」
ALTER TABLE `sa_system_dept` COMMENT = '渠道表';
ALTER TABLE `sa_system_dept`
MODIFY COLUMN `parent_id` bigint(20) UNSIGNED NULL DEFAULT 0 COMMENT '父级ID扁平渠道固定为0',
MODIFY COLUMN `name` varchar(64) NOT NULL COMMENT '渠道名称',
MODIFY COLUMN `code` varchar(64) NULL DEFAULT NULL COMMENT '渠道编码',
MODIFY COLUMN `leader_id` bigint(20) UNSIGNED NULL DEFAULT NULL COMMENT '渠道负责人ID';
-- 2. 菜单名称(按实际 id 调整id=5 为渠道管理菜单)
UPDATE `sa_system_menu` SET `name` = '渠道管理' WHERE `id` = 5 OR `name` LIKE '%渠道(部门)%' OR `name` = '部门管理';
-- 3. 将子渠道下的用户 dept_id 提升到顶级渠道(需配合 run_dept_flatten_channels.php 处理多级)
-- 以下为单级子渠道快速迁移parent_id != 0 的直接挂到父级)
UPDATE `sa_system_user` u
INNER JOIN `sa_system_dept` d ON u.dept_id = d.id AND d.parent_id > 0
INNER JOIN `sa_system_dept` p ON d.parent_id = p.id
SET u.dept_id = p.id;
-- 4. 删除所有子渠道parent_id > 0
DELETE FROM `sa_system_dept` WHERE `parent_id` > 0;
-- 5. 剩余渠道统一为顶级
UPDATE `sa_system_dept` SET `parent_id` = 0, `level` = '0';