Files
iotlcm/schema/12-repair-dept-ancestors.sql
T
MiFaZhan bc2c7bcc6b init: IotLCM 开源版部署仓
内容:
- docker-compose.yml:14 个基础/业务服务 + 3 个可选 profile(with-mock 挡板、with-bot、with-nginx)
- .env.example:集中管理镜像仓库、口令、端口;必填项缺失时 compose 直接报错退出
- config.example/:10 份配置模板,全部为占位值与注释说明
- schema/:12 个初始化脚本,按文件名顺序由 MySQL initdb 自动执行

脱敏处理:
- 生产/内网 IP、镜像仓库地址、公司名、真实域名全部清除
- 演示账号保留 18 个(出库与划拨依赖账号与部门树的关联),
  昵称/邮箱/手机号/登录IP/头像改为占位或编造值
- 数据库账号与 admin 口令改为初始化时按 .env 注入计算
- 通道模板与 Mock 通道的上游密钥置空
- Mock 通道地址指向 compose 内 onelink-mock 服务

部署时用 --profile with-mock 启动运营商接口挡板,即可在无真实运营商环境时跑通全链路。
2026-10-28 13:40:12 +08:00

71 lines
3.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.
-- ============================================
-- 演示环境数据修复: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`;