-- ========================================================================= -- 存量升级:D8 人员主数据(v1 库,lock.sql v1.0 → v1.1) -- -- 适用范围:已按 sql/lock.sql(v1.0,9 张表)建库、且代码为 D8 之前的版本。 -- 全新库请直接执行 sql/lock.sql(v1.1)或 sql/lock_v2.sql,不要执行本脚本。 -- -- 执行前置: -- 1) 停应用(或至少停止授权下发/人脸新增),避免迁移期间写入 lock_face; -- 2) 备份 lock_face / lock_auth / sys_menu; -- 3) 按顺序整段执行,中途失败按报错点重跑(各步骤可重复执行)。 -- -- 迁移内容: -- 1. 新建 lock_person(人员主数据,字段与 lock_v2.sql 第 5 节一致); -- 2. 从 lock_face 按 person_no 去重回填人员; -- 3. 从 lock_auth 补齐「只有密码/卡授权、没有人脸」的人员; -- 4. lock_face 增加 person_id 并回填,建立唯一索引(v1 一人一份人脸); -- 5. 新增人员管理菜单与按钮权限。 -- 6. 旧列 person_name/person_type/phone 暂不删除,代码已不再读写, -- 确认新版本稳定后再执行文末注释掉的清理语句。 -- -- 回滚:人员域为新增,回滚只需还原 lock_face 结构与删除 lock_person / 菜单; -- 回滚前请确认没有新版本写入的数据只存在于 lock_person。 -- ========================================================================= set names utf8mb4; -- ========================================================================= -- 1、人员主数据表 -- ========================================================================= create table if not exists lock_person ( person_id bigint(20) not null auto_increment comment '人员ID', person_no varchar(16) not null comment '人员编号(协议人员/人脸ID,2位字母+6位数字,全局唯一且不可变更)', person_name varchar(64) default null comment '姓名', person_type char(1) default '0' comment '人员类型(0住户 1员工 2访客 3其他,字典 lock_person_type)', dept_id bigint(20) default null comment '所属部门ID(数据权限)', sys_user_id bigint(20) default null comment '关联若依账号ID(员工自助场景,可为空)', id_card_no varchar(256) default null comment '证件号(v1 明文存储,见架构设计 D10 决策)', phone varchar(20) default null comment '联系电话', valid_from datetime comment '人员有效期起(未填不限)', valid_to datetime comment '人员有效期止(空表示永久)', status char(1) default '0' comment '状态(0正常 1停用)', del_flag char(1) default '0' comment '删除标志(0代表存在 2代表删除)', create_by varchar(64) default '' comment '创建者', create_time datetime comment '创建时间', update_by varchar(64) default '' comment '更新者', update_time datetime comment '更新时间', remark varchar(500) default null comment '备注', primary key (person_id), unique key uk_lock_person_no (person_no), key idx_lock_person_name (person_name), key idx_lock_person_dept (dept_id), key idx_lock_person_sys_user (sys_user_id) ) engine=innodb comment = '人员主数据表'; -- ========================================================================= -- 2、从 lock_face 回填人员(按 person_no 去重) -- 同一 person_no 在 v1 已被人脸唯一键约束,这里取聚合值只为兼容异常数据。 -- ========================================================================= insert into lock_person (person_no, person_name, person_type, phone, status, del_flag, create_by, create_time, update_by, update_time, remark) select f.person_no, max(f.person_name), max(f.person_type), max(f.phone), '0', '0', 'migrate', now(), 'migrate', now(), 'v1 lock_face 迁移' from lock_face f where f.person_no is not null and f.person_no <> '' group by f.person_no on duplicate key update update_time = values(update_time); -- 核对:应等于 lock_face 中不同 person_no 的数量 -- select count(distinct person_no) from lock_face; -- select count(*) from lock_person where remark = 'v1 lock_face 迁移'; -- ========================================================================= -- 3、从 lock_auth 补齐只有密码/卡授权、没有人脸的人员 -- ========================================================================= insert into lock_person (person_no, person_name, person_type, status, del_flag, create_by, create_time, update_by, update_time, remark) select a.person_no, max(a.person_name), '0', '0', '0', 'migrate', now(), 'migrate', now(), 'v1 lock_auth 迁移(无人脸)' from lock_auth a where a.person_no is not null and a.person_no <> '' and not exists (select 1 from lock_person p where p.person_no = a.person_no) group by a.person_no; -- ========================================================================= -- 4、lock_face 关联人员 -- 注意:MySQL 不支持 add column if not exists,重复执行时本段会报 -- "Duplicate column name 'person_id'",忽略即可,继续执行回填。 -- ========================================================================= alter table lock_face add column person_id bigint(20) not null default 0 comment '人员ID(lock_person.person_id)' after face_id; update lock_face f join lock_person p on p.person_no = f.person_no set f.person_id = p.person_id where f.person_id = 0; -- 核对:不应有 0;有 0 说明该人脸没有对应人员,需人工处理后再建索引 -- select face_id, person_no from lock_face where person_id = 0; alter table lock_face add unique key uk_lock_face_person_id (person_id), drop index idx_lock_face_name; -- ========================================================================= -- 5、菜单:人员管理(2100 号段)+ 按钮权限 -- 重复执行时先删人员菜单,且不再重复后移其它菜单顺序。 -- ========================================================================= set @has_person_menu = (select count(*) from sys_menu where menu_id = 2100); delete from sys_menu where perms like 'lock:person:%'; update sys_menu set order_num = order_num + 1 where parent_id = 2000 and menu_type = 'C' and order_num >= 4 and @has_person_menu = 0; insert into sys_menu (menu_id, menu_name, parent_id, order_num, path, component, query, route_name, is_frame, is_cache, menu_type, visible, status, perms, icon, create_by, create_time, remark) values (2100, '人员管理', 2000, 4, 'person', 'iot/person/index', '', '', 1, 0, 'C', '0', '0', 'lock:person:list', 'peoples', 'admin', CURRENT_TIMESTAMP, '人员主数据菜单'), (2101, '人员查询', 2100, 1, '', null, '', '', 1, 0, 'F', '0', '0', 'lock:person:query', '#', 'admin', CURRENT_TIMESTAMP, ''), (2102, '人员新增', 2100, 2, '', null, '', '', 1, 0, 'F', '0', '0', 'lock:person:add', '#', 'admin', CURRENT_TIMESTAMP, ''), (2103, '人员修改', 2100, 3, '', null, '', '', 1, 0, 'F', '0', '0', 'lock:person:edit', '#', 'admin', CURRENT_TIMESTAMP, ''), (2104, '人员删除', 2100, 4, '', null, '', '', 1, 0, 'F', '0', '0', 'lock:person:remove', '#', 'admin', CURRENT_TIMESTAMP, ''), (2105, '人员导出', 2100, 5, '', null, '', '', 1, 0, 'F', '0', '0', 'lock:person:export', '#', 'admin', CURRENT_TIMESTAMP, ''), (2106, '人员下拉', 2100, 6, '', null, '', '', 1, 0, 'F', '0', '0', 'lock:person:option', '#', 'admin', CURRENT_TIMESTAMP, '人员选择组件数据源'); -- ========================================================================= -- 6、旧列清理(可选,新版本上线并稳定后再执行) -- lock_face.person_name / person_type / phone 已由 lock_person 承载, -- 代码不再读写;保留期间为新旧版本混跑留缓冲。 -- ========================================================================= -- alter table lock_face -- drop column person_name, -- drop column person_type, -- drop column phone; -- ========================================================================= -- 脚本结束 -- =========================================================================