工会管理-分工会干部:1.新增并初始化“基层干部”角色,并为已成功分配分工会相关角色的人员补齐该角色。2.修复从角色管理页直接添加的分工会相关的历史角色数据没有所属工会id。(暂未覆盖已有工会id的分工会角色);

This commit is contained in:
2026-10-20 09:19:28 +08:00
parent 1b98a1a24f
commit 56fe0c2933
2 changed files with 147 additions and 13 deletions
@@ -1,6 +1,13 @@
-- 初始化“基层干部”角色,并为已成功分配分工会干部角色的人员补齐该角色。
-- 初始化“基层干部”角色,并修复误从角色管理页直接添加的历史分工会干部角色。
-- 修复对象仅限:BRANCH_UNION_ROLES 的直接子角色、unionId 为空、且其他业务作用域均为空的 sys_user_role 记录。
-- 人员当前所属分工会以 vw_user.unionId 为准;已有非空 unionId 的角色和已有干部档案均不改写。
-- 历史修复不补建审核流程。
SET @now := CAST(UNIX_TIMESTAMP(NOW()) * 1000 AS UNSIGNED);
START TRANSACTION;
-- 一、初始化“基层干部”角色。
INSERT INTO `sys_role`
(`id`, `name`, `code`, `aliasName`, `disabled`, `unitid`, `note`, `sort`, `createdBy`, `createdAt`, `updatedBy`, `updatedAt`, `delFlag`)
SELECT
@@ -8,12 +15,25 @@ SELECT
0, NULL, '分工会干部统一身份角色', 50, '', @now, '', @now, 0
FROM DUAL
WHERE NOT EXISTS (
SELECT 1 FROM `sys_role` WHERE `code` = 'BRANCH_UNION_CADRE'
SELECT 1
FROM `sys_role`
WHERE `code` = 'BRANCH_UNION_CADRE'
);
INSERT INTO `sys_user_role` (`roleId`, `userId`, `unionId`)
DROP TEMPORARY TABLE IF EXISTS `tmp_role_manager_branch_cadre`;
-- 二、识别角色管理页直接添加的分工会干部角色。
-- 角色管理页只写 roleId、userId,因此这里以 unionId 和其他业务作用域均为空作为历史数据识别条件。
CREATE TEMPORARY TABLE `tmp_role_manager_branch_cadre` AS
SELECT DISTINCT
cadre_role.`id`, assigned_role.`userId`, assigned_role.`unionId`
assigned_role.`roleId`,
assigned_role.`userId`,
business_role.`code` AS `roleCode`,
user_info.`unionId`,
branch_union.`name` AS `unionName`,
user_info.`username`,
user_info.`loginname`,
user_info.`mobile`
FROM `sys_user_role` assigned_role
INNER JOIN `sys_role` business_role
ON business_role.`id` = assigned_role.`roleId`
@@ -22,15 +42,127 @@ INNER JOIN `sys_dict` branch_role_parent
INNER JOIN `sys_dict` branch_role_item
ON branch_role_item.`parentId` = branch_role_parent.`id`
AND branch_role_item.`code` = business_role.`code`
INNER JOIN `sys_role` cadre_role
ON cadre_role.`code` = 'BRANCH_UNION_CADRE'
INNER JOIN `vw_user` user_info
ON user_info.`id` = assigned_role.`userId`
INNER JOIN `sys_union` branch_union
ON branch_union.`id` = user_info.`unionId`
WHERE (assigned_role.`unionId` IS NULL OR assigned_role.`unionId` = '')
AND (assigned_role.`unitId` IS NULL OR assigned_role.`unitId` = '')
AND (assigned_role.`unionGroupId` IS NULL OR assigned_role.`unionGroupId` = '')
AND (assigned_role.`tcDelegationId` IS NULL OR assigned_role.`tcDelegationId` = '')
AND (assigned_role.`tcSessionId` IS NULL OR assigned_role.`tcSessionId` = '')
AND (assigned_role.`wcDelegationId` IS NULL OR assigned_role.`wcDelegationId` = '')
AND (assigned_role.`wcSessionId` IS NULL OR assigned_role.`wcSessionId` = '')
AND (assigned_role.`clubId` IS NULL OR assigned_role.`clubId` = '')
AND (assigned_role.`underTakeId` IS NULL OR assigned_role.`underTakeId` = '');
-- 将误从角色管理页添加的角色补成当前所属分工会的作用域角色。
INSERT INTO `sys_user_role` (`roleId`, `userId`, `unionId`)
SELECT
repair.`roleId`, repair.`userId`, repair.`unionId`
FROM `tmp_role_manager_branch_cadre` repair
WHERE NOT EXISTS (
SELECT 1
FROM `sys_user_role` existing_role
WHERE existing_role.`roleId` = repair.`roleId`
AND existing_role.`userId` = repair.`userId`
AND existing_role.`unionId` = repair.`unionId`
);
-- 确认分工会作用域角色存在后,删除同人员、同角色的原空作用域记录。
DELETE global_role
FROM `sys_user_role` global_role
INNER JOIN `tmp_role_manager_branch_cadre` repair
ON repair.`roleId` = global_role.`roleId`
AND repair.`userId` = global_role.`userId`
INNER JOIN `sys_user_role` scoped_role
ON scoped_role.`roleId` = repair.`roleId`
AND scoped_role.`userId` = repair.`userId`
AND scoped_role.`unionId` = repair.`unionId`
WHERE (global_role.`unionId` IS NULL OR global_role.`unionId` = '')
AND (global_role.`unitId` IS NULL OR global_role.`unitId` = '')
AND (global_role.`unionGroupId` IS NULL OR global_role.`unionGroupId` = '')
AND (global_role.`tcDelegationId` IS NULL OR global_role.`tcDelegationId` = '')
AND (global_role.`tcSessionId` IS NULL OR global_role.`tcSessionId` = '')
AND (global_role.`wcDelegationId` IS NULL OR global_role.`wcDelegationId` = '')
AND (global_role.`wcSessionId` IS NULL OR global_role.`wcSessionId` = '')
AND (global_role.`clubId` IS NULL OR global_role.`clubId` = '')
AND (global_role.`underTakeId` IS NULL OR global_role.`underTakeId` = '');
-- 角色管理页不会创建干部档案,为本次识别出的历史数据补建有效干部档案。
INSERT INTO `sys_union_cadre`
(`id`, `unionId`, `unionName`, `userId`, `userName`, `loginName`, `mobile`, `roleCode`, `applyDate`, `isJoin`,
`createdBy`, `createdAt`, `updatedBy`, `updatedAt`, `delFlag`)
SELECT
LOWER(REPLACE(UUID(), '-', '')),
repair.`unionId`,
repair.`unionName`,
repair.`userId`,
repair.`username`,
repair.`loginname`,
repair.`mobile`,
repair.`roleCode`,
NOW(),
1,
'', @now, '', @now, 0
FROM `tmp_role_manager_branch_cadre` repair
WHERE NOT EXISTS (
SELECT 1
FROM `sys_union_cadre` existing_cadre
WHERE existing_cadre.`userId` = repair.`userId`
AND existing_cadre.`unionId` = repair.`unionId`
AND existing_cadre.`roleCode` = repair.`roleCode`
AND (existing_cadre.`isJoin` = 1 OR existing_cadre.`isJoin` IS NULL)
);
DROP TEMPORARY TABLE IF EXISTS `tmp_valid_branch_cadre`;
-- 三、汇总当前分工会范围内已经生效的干部角色,用于初始化“基层干部”角色。
CREATE TEMPORARY TABLE `tmp_valid_branch_cadre` AS
SELECT DISTINCT
assigned_role.`userId`,
assigned_role.`unionId`
FROM `sys_user_role` assigned_role
INNER JOIN `sys_role` business_role
ON business_role.`id` = assigned_role.`roleId`
INNER JOIN `sys_dict` branch_role_parent
ON branch_role_parent.`code` = 'BRANCH_UNION_ROLES'
INNER JOIN `sys_dict` branch_role_item
ON branch_role_item.`parentId` = branch_role_parent.`id`
AND branch_role_item.`code` = business_role.`code`
INNER JOIN `vw_user` user_info
ON user_info.`id` = assigned_role.`userId`
INNER JOIN `sys_union` branch_union
ON branch_union.`id` = assigned_role.`unionId`
WHERE assigned_role.`userId` IS NOT NULL
AND assigned_role.`unionId` IS NOT NULL
AND assigned_role.`unionId` <> ''
AND NOT EXISTS (
SELECT 1
FROM `sys_user_role` existing_cadre
WHERE existing_cadre.`roleId` = cadre_role.`id`
AND existing_cadre.`userId` = assigned_role.`userId`
AND existing_cadre.`unionId` = assigned_role.`unionId`
);
AND assigned_role.`unionId` = user_info.`unionId`
AND (assigned_role.`unitId` IS NULL OR assigned_role.`unitId` = '')
AND (assigned_role.`unionGroupId` IS NULL OR assigned_role.`unionGroupId` = '')
AND (assigned_role.`tcDelegationId` IS NULL OR assigned_role.`tcDelegationId` = '')
AND (assigned_role.`tcSessionId` IS NULL OR assigned_role.`tcSessionId` = '')
AND (assigned_role.`wcDelegationId` IS NULL OR assigned_role.`wcDelegationId` = '')
AND (assigned_role.`wcSessionId` IS NULL OR assigned_role.`wcSessionId` = '')
AND (assigned_role.`clubId` IS NULL OR assigned_role.`clubId` = '')
AND (assigned_role.`underTakeId` IS NULL OR assigned_role.`underTakeId` = '');
-- 给现有有效分工会干部及本次修复的干部补充同分工会范围的“基层干部”角色。
INSERT INTO `sys_user_role` (`roleId`, `userId`, `unionId`)
SELECT
cadre_role.`id`, valid_cadre.`userId`, valid_cadre.`unionId`
FROM `tmp_valid_branch_cadre` valid_cadre
INNER JOIN `sys_role` cadre_role
ON cadre_role.`code` = 'BRANCH_UNION_CADRE'
WHERE NOT EXISTS (
SELECT 1
FROM `sys_user_role` existing_cadre_role
WHERE existing_cadre_role.`roleId` = cadre_role.`id`
AND existing_cadre_role.`userId` = valid_cadre.`userId`
AND existing_cadre_role.`unionId` = valid_cadre.`unionId`
);
DROP TEMPORARY TABLE IF EXISTS `tmp_valid_branch_cadre`;
DROP TEMPORARY TABLE IF EXISTS `tmp_role_manager_branch_cadre`;
COMMIT;
@@ -49,6 +49,7 @@ const branchUnionUserManage = {
</el-form-item>
<el-form-item prop="userId" label="人员">
<user-select
:key="union_id"
v-model="formData.userId"
style="width: 100%"
api="/platform/sys/union/listUnion"
@@ -86,6 +87,7 @@ const branchUnionUserManage = {
},
watch: {
union_id(val) {
this.$set(this.formData, "userId", "")
this.doSearch()
}
},