Files
doorcontrol/base/RuoYi-Vue-master/sql/upgrade_d23_auth_source.sql
T
zfjia 8438fb6b8e feat(lock): 按实测报文解析锁内授权并对无通道授权置终态
- readkey 应答按实测格式解析:类型码区分密码/人脸,人脸凭据为人员ID,时间为8位十六进制Unix秒
- lock_device_auth 增加 auth_type 快照字段,对账支持按类型匹配
- 授权无下发通道(未登记网关/网关无分组)置 sync_status=7,补偿不再重复尝试
- 整表清除后网关置重启中,收到上行再恢复在线
- 同步更新协议与设计文档、幂等升级脚本(D23/D24/D25)
2026-11-04 14:31:01 +08:00

112 lines
5.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.
-- =========================================================================
-- D23 授权来源归属(lock_auth.source / source_app_code / source_biz_no)
--
-- 背景:
-- 外部业务系统通过 OpenAPI 下发授权(直接授权、外部入住/换房)后,
-- lock_auth 没有来源标记,平台页面无法区分平台授权与外部授权,
-- 也无法按应用隔离开放接口的查询/下发/取消/删除。
--
-- 目标:
-- 1. lock_auth 增加来源三列与组合索引;
-- 2. 外部入住/换房产生的授权自动回填为 source='2'(A 类);
-- 3. 其余存量行保持 source='1'(平台,B 类);现场确认不存在直接接口授权(C 类);
-- 4. 提供核对语句,确认来源分布与归属一致性。
--
-- 执行顺序:
-- 备份 lock_auth、lock_stay → 加列加索引 → 回填 → 核对 → 发布后端/前端。
-- **必须先执行本脚本再发布代码**,否则新增的 source 列会让授权查询报错。
--
-- 兼容说明:
-- MySQL 不支持 add column if not exists。重复执行时「加列」段会报
-- Duplicate column name,「加索引」段会报 Duplicate key name,按现有升级脚本惯例
-- 忽略这些错误继续执行后面的回填与核对即可(回填语句本身幂等)。
-- 所有 DML 建议先在只读副本预演并核对影响行数,再在生产低峰执行。
-- =========================================================================
-- =========================================================================
-- 1、加列:source 与 lock_stay.source 同编码(1 平台、2 开放接口)
-- =========================================================================
alter table lock_auth
add column source char(1) default '1' comment '授权来源(1平台 2开放接口)' after auth_time,
add column source_app_code varchar(32) default null comment '来源应用编码(source=2,lock_open_app.app_code)' after source,
add column source_biz_no varchar(64) default null comment '来源业务单号(可空;外部入住为 biz_no)' after source_app_code;
alter table lock_auth
add key idx_lock_auth_source (source, source_app_code);
-- =========================================================================
-- 2、存量回填 A 类:外部入住/换房产生的授权
-- 判定依据:lock_stay.auth_id = lock_auth.auth_id 且 lock_stay.source='2'
-- =========================================================================
-- 2.1 在住单:直接回填,无覆盖风险
update lock_auth a
join lock_stay s on s.auth_id = a.auth_id
set a.source = '2',
a.source_app_code = s.biz_system,
a.source_biz_no = s.biz_no
where s.source = '2'
and s.stay_status = '1'
and s.del_flag = '0'
and s.biz_system is not null
and a.del_flag = '0'
and a.source = '1';
-- 2.2 已退房/已换房的历史单:仅在退房后没有被再次覆盖时回填
-- auth_id 会被「同人同锁同类型」复用:退房后若平台或接口又覆盖过该行,
-- update_time 会晚于退房时间,此时按平台保留,避免误标。
-- 初始插入不写 update_time,为空的存量行视为从未被覆盖。
update lock_auth a
join lock_stay s on s.auth_id = a.auth_id
set a.source = '2',
a.source_app_code = s.biz_system,
a.source_biz_no = s.biz_no
where s.source = '2'
and s.stay_status in ('2', '3', '4')
and s.del_flag = '0'
and s.biz_system is not null
and a.del_flag = '0'
and a.source = '1'
and (a.update_time is null or a.update_time <= coalesce(s.checkout_time, s.update_time));
-- 2.3 兜底:空值归一为平台(B 类)
update lock_auth set source = '1' where source is null or source = '';
-- =========================================================================
-- 3、核对(执行后逐条确认)
-- =========================================================================
-- 3.1 来源分布:现场应只有 1(平台)与 2(外部)
-- select source, count(1) from lock_auth where del_flag = '0' group by source;
-- 3.2 在住外部单对应授权应全部归属外部,结果应为 0
-- select count(1)
-- from lock_auth a
-- join lock_stay s on s.auth_id = a.auth_id
-- where s.source = '2' and s.stay_status = '1' and s.del_flag = '0'
-- and s.biz_system is not null
-- and a.del_flag = '0' and a.source <> '2';
-- 3.3 来源值只允许 1/2,结果应为 0
-- select count(1) from lock_auth where del_flag = '0' and source not in ('1', '2');
-- 3.4 外部授权必须有应用编码,结果应为 0
-- (历史单 biz_system 为空的行不会回填,若有需人工确认)
-- select count(1) from lock_auth
-- where del_flag = '0' and source = '2' and (source_app_code is null or source_app_code = '');
-- =========================================================================
-- 4、回滚
-- 回滚只撤销表结构;已回填的 source 值随列删除一并消失,不影响授权业务数据。
-- 如需保留来源数据再回滚,先导出 auth_id/source/source_app_code/source_biz_no。
-- =========================================================================
-- alter table lock_auth drop key idx_lock_auth_source;
-- alter table lock_auth
-- drop column source_biz_no,
-- drop column source_app_code,
-- drop column source;