-- ============================================ -- 演示环境数据修复:sys_dept.ancestors 尾逗号归一化(2026-10-27) -- -- 症状:一级代理账号在「业务受理 → 卡片划拨」里选不到下级(下拉“暂无数据”)。 -- -- 根因(两处代码缺陷,见 hotfix/dept-ancestors-20261027): -- 1) 写入端 `agent_dept.go childDeptAncestors` 生成 "0,1,172"(缺尾逗号), -- 与平台约定格式 "0,1,172," 不一致; -- 2) 匹配端 `xgorm/handler/filter_auth.go GetDeptAndSub` 只按 '%,id,%' 匹配, -- 缺尾逗号时匹配不到,于是「本机构及以下」数据范围里没有下级, -- 卡片划拨的 targets 接口(parent_id = 当前机构 + 数据权限过滤)返回空。 -- -- 本文件只修数据:按父子链重算 ancestors,统一为「以逗号结尾」的平台格式。 -- 已创建的机构(如 dept 206「二级代理」= '0,1,172')会被修正为 '0,1,172,'。 -- -- 状态(2026-10-27 更新):**两端代码修复已上线** -- admin-svr `6e3253c`(tag v1.0.3)已规范部署到演示与生产,写入端不再产生缺尾逗号。 -- 因此: -- · 对**全新种子库**:无脏数据,本文件为空操作(保留仅为对称与可追溯); -- · 对**既有库**:仍可用于修复历史上已写坏的 ancestors(幂等,可随时重跑)。 -- 代码修复见 wiki `projects/iotlcm-deploy/references/dept-ancestors-hotfix-20261027`。 -- -- 执行顺序:11 之后(12-)。 -- -- 幂等:可重复执行。MySQL 8.0 递归 CTE + 中转表(避免 1093 自表更新限制)。 -- 注:递归 CTE 的列宽取自非递归部分,必须 CAST,否则 CONCAT 结果报 1406。 -- ============================================ SET NAMES utf8mb4; USE `db_iotlcm`; DROP TABLE IF EXISTS `_tmp_dept_ancestors_fix`; CREATE TABLE `_tmp_dept_ancestors_fix` ( `dept_id` BIGINT NOT NULL, `ancestors_calc` VARCHAR(255) NOT NULL, PRIMARY KEY (`dept_id`) ); INSERT INTO `_tmp_dept_ancestors_fix` (`dept_id`, `ancestors_calc`) WITH RECURSIVE `dept_tree` (`dept_id`, `tenant_id`, `ancestors_calc`, `depth`) AS ( SELECT `dept_id`, `tenant_id`, CAST('0,' AS CHAR(255)), 1 FROM `sys_dept` WHERE `parent_id` = 0 UNION ALL SELECT d.`dept_id`, d.`tenant_id`, CONCAT(t.`ancestors_calc`, d.`parent_id`, ','), t.`depth` + 1 FROM `sys_dept` d JOIN `dept_tree` t ON d.`parent_id` = t.`dept_id` AND d.`tenant_id` = t.`tenant_id` WHERE t.`depth` < 32 ) SELECT `dept_id`, `ancestors_calc` FROM `dept_tree`; UPDATE `sys_dept` d JOIN `_tmp_dept_ancestors_fix` t ON t.`dept_id` = d.`dept_id` SET d.`ancestors` = t.`ancestors_calc` WHERE d.`ancestors` <> t.`ancestors_calc`; DROP TABLE `_tmp_dept_ancestors_fix`; -- 自检 1:不应再有非空且不以逗号结尾的 ancestors SELECT COUNT(*) AS still_missing_trailing_comma FROM `sys_dept` WHERE `ancestors` IS NOT NULL AND `ancestors` <> '' AND `ancestors` <> '0,' AND RIGHT(`ancestors`, 1) <> ','; -- 自检 2:复现划拨接口的取数条件(parent_id = 一级代理 + 数据权限展开结果) SELECT `dept_id`, `dept_name`, `parent_id`, `ancestors` FROM `sys_dept` WHERE `deleted_at` IS NULL AND `status` = '0' AND `parent_id` = 172 AND (`ancestors` LIKE '%,172,%' OR `ancestors` LIKE '%,172') ORDER BY `dept_id`;