UserPathMapper.xml 4.55 KB
<?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="cn.com.glsx.shield.modules.mapper.UserPathMapper">
    <resultMap id="BaseResultMap" type="cn.com.glsx.shield.modules.entity.UserPath">
        <id column="id" jdbcType="INTEGER" property="id"/>
        <result column="superior_id" jdbcType="INTEGER" property="superiorId"/>
        <result column="sub_id" jdbcType="INTEGER" property="subId"/>
        <result column="depth" jdbcType="INTEGER" property="depth"/>
        <result column="tenant_id" jdbcType="INTEGER" property="tenantId"/>
        <result column="del_flag" jdbcType="TINYINT" property="delFlag"/>
        <result column="created_date" jdbcType="TIMESTAMP" property="createdDate"/>
        <result column="created_by" jdbcType="INTEGER" property="createdBy"/>
        <result column="updated_date" jdbcType="TIMESTAMP" property="updatedDate"/>
        <result column="updated_by" jdbcType="INTEGER" property="updatedBy"/>
    </resultMap>
    <sql id="Base_Column_List">
        id
        , superior_id, sub_id, `depth`, tenant_id, del_flag, created_date, created_by,
    updated_date, updated_by
    </sql>

    <insert id="insertRootPath">
        insert into t_user_path
            (superior_id, sub_id, depth, tenant_id, created_date, created_by)
        values (#{superiorId}, #{subId}, 0, #{tenantId}, #{createdDate}, #{createdBy})
    </insert>

    <insert id="insertUserPath" useGeneratedKeys="false">
        insert into t_user_path(superior_id, sub_id, depth, tenant_id, created_date, created_by)
        select tor.superior_id, #{subId}, tor.depth + 1, #{tenantId}, #{createdDate}, #{createdBy}
        from t_user_path as tor
        where tor.sub_id = #{superiorId}
        union all
        select #{subId}, #{subId}, 0, #{tenantId}, #{createdDate}, #{createdBy}
    </insert>

    <delete id="deleteUserSuperiorPath">
        delete
        from t_user_path
        where sub_id = #{nodeId};
    </delete>

    <delete id="deleteUserSubPath">
        delete
        from t_user_path
        where superior_id = #{nodeId};
    </delete>

    <delete id="deleteUserAllPath">
        delete
        a from t_user_path a join t_user_path b on (a.sub_id = b.sub_id)
        where b.superior_id=
        #{nodeId};
    </delete>

    <select id="selectAllSuperiorBySubId" resultMap="BaseResultMap">
        select
        <include refid="Base_Column_List"/>
        from t_user_path
        where del_flag = 0
        and sub_id = #{subId}
    </select>

    <select id="selectSubordinateBySuperiorId" resultMap="BaseResultMap">
        select
        <include refid="Base_Column_List"/>
        from t_user_path
        where del_flag = 0
        and superior_id = #{superiorId}
    </select>

    <select id="selectRootSubAllPaths" resultMap="BaseResultMap">
        select
        <include refid="Base_Column_List"/>
        from t_user_path
        where tenant_id = #{tenantId}
        and depth = 0
        and del_flag = 0
    </select>

    <select id="selectRootPathBySubId" resultMap="BaseResultMap">
        select
        <include refid="Base_Column_List"/>
        from t_user_path
        where sub_id = #{subId} and del_flag = 0
        and depth = (
        select max(depth)
        from t_organization
        where sub_id = #{subId} and del_flag = 0
        )
    </select>

    <select id="selectSubordinateDepartmentList" resultType="cn.com.glsx.shield.modules.model.view.DepartmentUserCount">
        select to2.superior_id superiorId, tu.department_id departmentId, tu.id userId, tup.depth
        from t_user tu
        inner join t_user_path tup on tup.sub_id = tu.id
        left join t_organization to2 on to2.sub_id = tu.department_id
        where tu.del_flag = 0
        and tup.del_flag = 0
        and tup.superior_id = #{superiorId}
        <if test="userIds != null and userIds.size() > 0">
            and tu.id in
            <foreach collection="userIds" item="item" open="(" close=")" separator=",">
                #{item}
            </foreach>
        </if>
        <if test="orgIds != null and orgIds.size() > 0">
            and tu.department_id in
            <foreach collection="orgIds" item="item" open="(" close=")" separator=",">
                #{item}
            </foreach>
        </if>
        <if test="subOrgIds != null and subOrgIds.size() > 0">
            and to2.superior_id in
            <foreach collection="subOrgIds" item="item" open="(" close=")" separator=",">
                #{item}
            </foreach>
        </if>
    </select>
</mapper>