10 changed files with 391 additions and 118 deletions
@ -0,0 +1,150 @@ |
|||||
|
|
||||
|
# 创建新表(合并层) |
||||
|
CREATE TABLE dashboard_level_store_area_merge ( |
||||
|
id BIGINT NOT NULL AUTO_INCREMENT, |
||||
|
company_id BIGINT, |
||||
|
store_id BIGINT, |
||||
|
area_id BIGINT, |
||||
|
name VARCHAR(255), |
||||
|
code VARCHAR(255), |
||||
|
remark VARCHAR(255), |
||||
|
flag INT DEFAULT 0, |
||||
|
created_by BIGINT, |
||||
|
created_at BIGINT, |
||||
|
updated_at BIGINT, |
||||
|
updated_by BIGINT, |
||||
|
PRIMARY KEY (id) |
||||
|
); |
||||
|
|
||||
|
# 初始化数据 |
||||
|
INSERT INTO dashboard_level_store_area_merge ( |
||||
|
company_id, |
||||
|
store_id, |
||||
|
area_id, |
||||
|
name, |
||||
|
created_by, |
||||
|
created_at |
||||
|
) |
||||
|
SELECT |
||||
|
st.company_id, |
||||
|
r.store_id, |
||||
|
r.area_id, |
||||
|
CONCAT(st.name, ' ', ar.name), |
||||
|
r.created_by, |
||||
|
r.created_at |
||||
|
FROM dashboard_level_relation_store_area r |
||||
|
JOIN dashboard_level_store st ON st.id = r.store_id |
||||
|
JOIN dashboard_level_area ar ON ar.id = r.area_id |
||||
|
WHERE st.flag = 0 AND ar.flag = 0; |
||||
|
|
||||
|
# 重建 Branch 新层关系 |
||||
|
CREATE TABLE dashboard_level_relation_branch_store_area ( |
||||
|
branch_id BIGINT NOT NULL, |
||||
|
store_area_id BIGINT NOT NULL, |
||||
|
created_by BIGINT, |
||||
|
created_at BIGINT, |
||||
|
KEY idx_branch (branch_id), |
||||
|
KEY idx_store_area (store_area_id) |
||||
|
); |
||||
|
|
||||
|
# 数据迁移 |
||||
|
INSERT INTO dashboard_level_relation_branch_store_area ( |
||||
|
branch_id, |
||||
|
store_area_id, |
||||
|
created_by, |
||||
|
created_at |
||||
|
) |
||||
|
SELECT |
||||
|
bs.branch_id, |
||||
|
sam.id, |
||||
|
bs.created_by, |
||||
|
bs.created_at |
||||
|
FROM dashboard_level_relation_branch_store bs |
||||
|
JOIN dashboard_level_store_area_merge sam |
||||
|
ON bs.store_id = sam.store_id; |
||||
|
|
||||
|
# 重建 StoreArea-Site |
||||
|
CREATE TABLE dashboard_level_relation_store_area_site ( |
||||
|
store_area_id BIGINT NOT NULL, |
||||
|
site_id BIGINT NOT NULL, |
||||
|
created_by BIGINT, |
||||
|
created_at BIGINT, |
||||
|
KEY idx_sa (store_area_id), |
||||
|
KEY idx_site (site_id) |
||||
|
); |
||||
|
|
||||
|
# 数据迁移 |
||||
|
INSERT INTO dashboard_level_relation_store_area_site ( |
||||
|
store_area_id, |
||||
|
site_id, |
||||
|
created_by, |
||||
|
created_at |
||||
|
) |
||||
|
SELECT |
||||
|
sam.id, |
||||
|
ras.site_id, |
||||
|
ras.created_by, |
||||
|
ras.created_at |
||||
|
FROM dashboard_level_relation_area_site ras |
||||
|
JOIN dashboard_level_store_area_merge sam |
||||
|
ON ras.area_id = sam.area_id; |
||||
|
|
||||
|
# 迁移权限 |
||||
|
# 先改ENUM |
||||
|
ALTER TABLE dashboard_level_role_object MODIFY ref_type ENUM('BRANCH','STORE','AREA','SITE','BUILDING','STORE_AREA') NOT NULL; |
||||
|
# 迁移 AREA 权限 |
||||
|
INSERT IGNORE INTO dashboard_level_role_object ( |
||||
|
level_role_id, |
||||
|
ref_type, |
||||
|
ref_id, |
||||
|
created_by, |
||||
|
created_at, |
||||
|
updated_at, |
||||
|
updated_by |
||||
|
) |
||||
|
SELECT |
||||
|
ro.level_role_id, |
||||
|
'STORE_AREA', |
||||
|
sam.id, |
||||
|
ro.created_by, |
||||
|
ro.created_at, |
||||
|
ro.updated_at, |
||||
|
ro.updated_by |
||||
|
FROM dashboard_level_role_object ro |
||||
|
JOIN dashboard_level_store_area_merge sam |
||||
|
ON ro.ref_type = 'AREA' |
||||
|
AND ro.ref_id = sam.area_id; |
||||
|
|
||||
|
# 迁移 STORE 权限 |
||||
|
INSERT IGNORE INTO dashboard_level_role_object ( |
||||
|
level_role_id, |
||||
|
ref_type, |
||||
|
ref_id, |
||||
|
created_by, |
||||
|
created_at, |
||||
|
updated_at, |
||||
|
updated_by |
||||
|
) |
||||
|
SELECT |
||||
|
ro.level_role_id, |
||||
|
'STORE_AREA', |
||||
|
sam.id, |
||||
|
ro.created_by, |
||||
|
ro.created_at, |
||||
|
ro.updated_at, |
||||
|
ro.updated_by |
||||
|
FROM dashboard_level_role_object ro |
||||
|
JOIN dashboard_level_store_area_merge sam |
||||
|
ON ro.ref_type = 'STORE' |
||||
|
AND ro.ref_id = sam.store_id; |
||||
|
|
||||
|
# 删除旧权限 |
||||
|
DELETE FROM dashboard_level_role_object WHERE ref_type IN ('AREA','STORE'); |
||||
|
|
||||
|
# 最后改ENUM |
||||
|
ALTER TABLE dashboard_level_role_object MODIFY ref_type ENUM('BRANCH','STORE_AREA','SITE','BUILDING') NOT NULL; |
||||
|
|
||||
|
|
||||
|
# 添加编码 |
||||
|
ALTER TABLE dashboard_level_branch ADD code VARCHAR(255) AFTER `name`; |
||||
|
ALTER TABLE dashboard_level_site ADD code VARCHAR(255) AFTER `name`; |
||||
Loading…
Reference in new issue