feat(清理机器人): 启动时执行清理

This commit is contained in:
fit2cloud-chenyw 2022-11-01 12:56:24 +08:00
parent 1feda5cc47
commit de41484364
5 changed files with 241 additions and 36 deletions

View File

@ -0,0 +1,26 @@
package io.dataease.ext;
public interface CleaningRebotMapper {
int delFloatingDept();
void updateUserDept();
void delFloatingRoleMapping();
void delFloatingPanelShare();
void delFloatingTargetShare();
void delFloatingPanelStore();
void delFloatingTargetStore();
void delFloatingPanelLink();
void delFloatingPanelLinkMapping();
void delFloatingCreatorLink();
void delFloatingCreatorLinkMapping();
}

View File

@ -0,0 +1,114 @@
<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE mapper PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN" "http://mybatis.org/dtd/mybatis-3-mapper.dtd" >
<mapper namespace="io.dataease.ext.CleaningRebotMapper">
<delete id="delFloatingDept">
delete
from sys_dept
where pid not in (select * from (select dept_id from sys_dept) a)
and pid != 0;
</delete>
<update id="updateUserDept">
update sys_user
set dept_id = 0
where dept_id is not null
and dept_id not in (select * from (select dept_id from sys_dept) a)
and dept_id != 0;
</update>
<delete id="delFloatingRoleMapping">
delete
from sys_users_roles
where user_id not in (select * from (select u.user_id from sys_user u) a)
or role_id not in (select * from (select r.role_id from sys_role r) b);
</delete>
<delete id="delFloatingPanelShare">
delete
from panel_share
where share_id in (select *
from (SELECT s.share_id
FROM panel_share s
left join panel_group p on s.panel_group_id = p.id
where p.id is null) a)
</delete>
<delete id="delFloatingTargetShare">
delete
from panel_share
where share_id in (select share_id
from (select s.share_id,
(
CASE s.type
WHEN 0 THEN (select nick_name from sys_user where user_id = s.target_id)
WHEN 1 THEN (select name from sys_role where role_id = s.target_id)
WHEN 2 THEN (select name from sys_dept where dept_id = s.target_id)
END
) as target_name
from panel_share s) query_temp
where target_name is null)
</delete>
<delete id="delFloatingPanelStore">
delete
from panel_store
where store_id in (select *
from (SELECT s.store_id
FROM panel_store s
left join panel_group p on s.panel_group_id = p.id
where p.id is null) a)
</delete>
<delete id="delFloatingTargetStore">
delete
from panel_store
where store_id in (select *
from (SELECT s.store_id
FROM panel_store s
left join sys_user u on s.user_id = u.user_id
where u.user_id is null) a)
</delete>
<delete id="delFloatingPanelLink">
delete
from panel_link
where resource_id in (select *
from (SELECT l.resource_id
FROM panel_link l
left join panel_group p on l.resource_id = p.id
where p.id is null) a);
</delete>
<delete id="delFloatingPanelLinkMapping">
delete
from panel_link_mapping
where id in (select *
from (SELECT m.id
FROM panel_link_mapping m
left join panel_group p on m.resource_id = p.id
where p.id is null) a);
</delete>
<delete id="delFloatingCreatorLink">
delete
from panel_link
where resource_id in (select *
from (SELECT l.resource_id
FROM panel_link l
left join sys_user u on l.user_id = u.user_id
where u.user_id is null) a);
</delete>
<delete id="delFloatingCreatorLinkMapping">
delete
from panel_link_mapping
where id in (select *
from (SELECT m.resource_id
FROM panel_link_mapping m
left join sys_user u on m.user_id = u.user_id
where u.user_id is null) a);
</delete>
</mapper>

View File

@ -3,20 +3,20 @@
<mapper namespace="io.dataease.ext.ExtPanelShareMapper">
<resultMap id="treeNodeMap" type="io.dataease.dto.panel.PanelSharePo">
<id column="id" property="id" />
<result column="name" property="name" />
<result column="status" property="status" />
<result column="creator" property="creator" />
<result column="user_id" jdbcType="BIGINT" property="userId" />
<id column="id" property="id"/>
<result column="name" property="name"/>
<result column="status" property="status"/>
<result column="creator" property="creator"/>
<result column="user_id" jdbcType="BIGINT" property="userId"/>
</resultMap>
<resultMap id="targetMap" type="io.dataease.dto.panel.PanelShareOutDTO">
<result column="panel_id" property="panelId" />
<result column="share_id" property="shareId" />
<result column="type" property="type" />
<result column="target_Id" property="targetId" />
<result column="target_Name" property="targetName" />
<result column="create_time" property="createTime" />
<result column="panel_id" property="panelId"/>
<result column="share_id" property="shareId"/>
<result column="type" property="type"/>
<result column="target_Id" property="targetId"/>
<result column="target_Name" property="targetName"/>
<result column="create_time" property="createTime"/>
</resultMap>
<insert id="batchInsert" parameterType="io.dataease.plugins.common.base.domain.PanelShare">
@ -27,10 +27,10 @@
</foreach>
</insert>
<delete id="batchDelete" parameterType="java.lang.Long" >
<delete id="batchDelete" parameterType="java.lang.Long">
delete from panel_share
where share_id in
<foreach collection="shareIds" item="shareId" open="(" separator="," close=")" >
<foreach collection="shareIds" item="shareId" open="(" separator="," close=")">
#{shareId}
</foreach>
</delete>
@ -81,43 +81,47 @@
</select>
<select id="queryWithResource" parameterType="io.dataease.controller.request.panel.PanelShareSearchRequest" resultMap="io.dataease.plugins.common.base.mapper.PanelShareMapper.BaseResultMap">
<select id="queryWithResource" parameterType="io.dataease.controller.request.panel.PanelShareSearchRequest"
resultMap="io.dataease.plugins.common.base.mapper.PanelShareMapper.BaseResultMap">
select s.*
from panel_share s
left join panel_group g on g.id = s.panel_group_id
where
s.panel_group_id = #{resourceId}
<if test="type != null">
<if test="type != null">
and s.type = #{type}
</if>
and (( s.granter is not null and s.granter = #{currentUserName} ) or ( s.granter is null and g.create_by = #{currentUserName} ))
and (( s.granter is not null and s.granter = #{currentUserName} ) or ( s.granter is null and g.create_by =
#{currentUserName} ))
order by s.create_time desc
</select>
<select id="queryTargets" resultMap="targetMap">
select
s.share_id,
s.panel_group_id as panel_id,
s.type,
s.target_id,
s.create_time,
(
CASE s.type
WHEN 0 THEN (select nick_name from sys_user where user_id = s.target_id)
WHEN 1 THEN (select name from sys_role where role_id = s.target_id)
WHEN 2 THEN (select name from sys_dept where dept_id = s.target_id)
END
) as target_name
<select id="queryTargets" resultMap="targetMap">
select s.share_id,
s.panel_group_id as panel_id,
s.type,
s.target_id,
s.create_time,
(
CASE s.type
WHEN 0 THEN (select nick_name from sys_user where user_id = s.target_id)
WHEN 1 THEN (select name from sys_role where role_id = s.target_id)
WHEN 2 THEN (select name from sys_dept where dept_id = s.target_id)
END
) as target_name
from panel_share s
where s.panel_group_id = #{panelId}
and s.granter = #{userName}
and (
(s.granter is not null and s.granter = #{userName})
or
(#{userName} = 'admin' and s.granter is null)
)
</select>
<select id="queryUserIdWithRoleIds" resultType="java.lang.Long" >
<select id="queryUserIdWithRoleIds" resultType="java.lang.Long">
select user_id
from sys_users_roles
where role_id in
@ -127,7 +131,7 @@
</select>
<select id="queryUserIdWithDeptIds" resultType="java.lang.Long" >
<select id="queryUserIdWithDeptIds" resultType="java.lang.Long">
select user_id
from sys_user
where dept_id in
@ -145,6 +149,4 @@
</delete>
</mapper>

View File

@ -0,0 +1,26 @@
package io.dataease.listener;
import io.dataease.commons.utils.LogUtil;
import io.dataease.service.CleaningRebotService;
import org.springframework.boot.context.event.ApplicationReadyEvent;
import org.springframework.context.ApplicationListener;
import org.springframework.stereotype.Component;
import javax.annotation.Resource;
@Component
public class CleaningRobotListener implements ApplicationListener<ApplicationReadyEvent> {
@Resource
private CleaningRebotService cleaningRebotService;
@Override
public void onApplicationEvent(ApplicationReadyEvent event) {
try {
cleaningRebotService.execute();
} catch (Exception e) {
LogUtil.error(e.getMessage(), e);
}
}
}

View File

@ -0,0 +1,37 @@
package io.dataease.service;
import io.dataease.ext.CleaningRebotMapper;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Service;
import javax.annotation.Resource;
@Service
public class CleaningRebotService {
@Value("${dataease.clean-nobody-link:false}")
private Boolean cleanNobodyLink;
@Resource
private CleaningRebotMapper cleaningRebotMapper;
public void execute() {
int floatDept = 0;
do {
floatDept = cleaningRebotMapper.delFloatingDept();
} while (floatDept > 0);
cleaningRebotMapper.updateUserDept();
cleaningRebotMapper.delFloatingRoleMapping();
cleaningRebotMapper.delFloatingPanelShare();
cleaningRebotMapper.delFloatingTargetShare();
cleaningRebotMapper.delFloatingPanelStore();
cleaningRebotMapper.delFloatingTargetStore();
cleaningRebotMapper.delFloatingPanelLink();
cleaningRebotMapper.delFloatingPanelLinkMapping();
if (cleanNobodyLink) {
cleaningRebotMapper.delFloatingCreatorLink();
cleaningRebotMapper.delFloatingCreatorLinkMapping();
}
}
}