OrganizationMapper.xml 10.7 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.OrganizationMapper">
    <resultMap id="BaseResultMap" type="cn.com.glsx.shield.modules.entity.Organization">
        <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_organization(superior_id, sub_id, depth, tenant_id, created_date, created_by)
        values (#{superiorId}, #{subId}, 0, #{tenantId}, #{createdDate}, #{createdBy})
    </insert>

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

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

    <select id="selectOrgList" resultType="cn.com.glsx.usercenter.api.dto.tree.OrgModel">
        select tor.sub_id orgId,
        max(tor.superior_id) parentId,
        min(tor2.superior_id) superiorId,
        max(tor.depth) depth,
        td.tenant_id tenantId,
        td.department_name orgName,
        td.is_root isRoot,
        '' type,
        td.order_num orderNum
        from t_organization tor,t_organization tor2
        left join t_department td on tor2.sub_id = td.id
        where td.del_flag = 0
        and td.enable_status = 1
        and tor.id = tor2.id
        and (tor.superior_id <![CDATA[<>]]> tor.sub_id or td.is_root = 1)
        <if test="tenantId != null">
            and td.tenant_id = #{tenantId}
        </if>
        <if test="tenantIds != null and tenantIds.size() > 0">
            and td.tenant_id in
            <foreach collection="tenantIds" index="index" item="item" open="(" separator="," close=")">
                #{item}
            </foreach>
        </if>
        <if test="orgIds != null and orgIds.size() > 0">
            and tor.sub_id in
            <foreach collection="orgIds" index="index" item="item" open="(" separator="," close=")">
                #{item}
            </foreach>
        </if>
        group by tor.sub_id
        order by max(tor.superior_id),max(tor.depth),tor.sub_id
    </select>

    <select id="selectSuperiorIdsByOrgId" resultType="cn.com.glsx.shield.modules.model.OrgSuperiorModel">
        select td1.id orgId, group_concat(to1.superior_id) superiorIds
        from t_department td1,
             t_organization to1
        where td1.del_flag = 0
          and td1.id = to1.sub_id
          and td1.id = #{orgId}
        group by to1.sub_id
    </select>

    <select id="selectSuperiorIdsByOrgIds" resultType="cn.com.glsx.shield.modules.model.OrgSuperiorModel">
        select td1.id orgId,group_concat(to1.superior_id) superiorIds
        from t_department td1, t_organization to1
        where td1.del_flag = 0
        and td1.id = to1.sub_id
        and td1.id in
        <foreach collection="orgIds" index="index" item="item" open="(" separator="," close=")">
            #{item}
        </foreach>
        group by to1.sub_id
    </select>

    <select id="selectSuperiorIdsByOrg" resultType="cn.com.glsx.shield.modules.model.OrgSuperiorModel">
        select td1.id orgId,group_concat(to1.superior_id) superiorIds
        from t_department td1, t_organization to1
        where td1.del_flag = 0
        and td1.id = to1.sub_id
        <if test="tenantId != null">
            and td1.tenant_id = #{tenantId}
        </if>
        <if test="orgId != null">
            and td1.id = #{orgId}
        </if>
        <if test="orgName != null and orgName !=''">
            and td1.department_name LIKE CONCAT('%',#{orgName},'%')
        </if>
        <if test="enableStatus != null">
            and td1.enable_status = #{enableStatus}
        </if>
        <if test="tenantIds != null and tenantIds.size() > 0">
            and td1.tenant_id in
            <foreach collection="tenantIds" index="index" item="item" open="(" separator="," close=")">
                #{item}
            </foreach>
        </if>
        <if test="orgIds != null and orgIds.size() > 0">
            and td1.id in
            <foreach collection="orgIds" index="index" item="item" open="(" separator="," close=")">
                #{item}
            </foreach>
        </if>
        group by to1.sub_id
    </select>

    <select id="selectRootSubAllPaths" resultMap="BaseResultMap">
        select
        <include refid="Base_Column_List"/>
        from t_organization
        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_organization
        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="selectAllSuperiorBySubId" resultMap="BaseResultMap">
        select
        <include refid="Base_Column_List"/>
        from t_organization
        where del_flag = 0
        and sub_id = #{subId}
        order by sub_id, depth desc
    </select>

    <select id="selectAllSubBySuperiorId" resultMap="BaseResultMap">
        select
        <include refid="Base_Column_List"/>
        from t_organization
        where del_flag = 0
        and superior_id = #{superiorId}
        order by superior_id, depth
    </select>

    <select id="selectTenantsSubBySuperiorId" resultMap="BaseResultMap">
        select
        <include refid="Base_Column_List"/>
        from t_organization
        where del_flag = 0
        and superior_id = #{superiorId}
        and tenant_id in
        <foreach collection="tenantIds" index="index" item="item" open="(" separator="," close=")">
            #{item}
        </foreach>
        order by depth
    </select>

    <select id="selectSuperiorOrgByDepth" resultMap="BaseResultMap">
        select
        <include refid="Base_Column_List"/>
        from t_organization
        where sub_id = #{superiorId}
        and depth = #{depth}
        and del_flag = 0
    </select>

    <select id="selectSuperiorOrgsByDepths" resultMap="BaseResultMap">
        select
        <include refid="Base_Column_List"/>
        from t_organization
        where sub_id = #{superiorId}
        and depth in
        <foreach collection="depths" separator="," open="(" close=")" item="depth">
            #{depth}
        </foreach>
        and del_flag = 0
        order by depth
    </select>

    <select id="selectRootIdList" resultType="cn.com.glsx.shield.modules.entity.Organization">
        select superior_id superiorId, sub_id subId ,max(depth) depth
        from t_organization
        where del_flag = 0
        and sub_id in
        <foreach collection="subIds" separator="," open="(" close=")" item="subId">
            #{subId}
        </foreach>
        group by sub_id
    </select>

    <select id="selectChildrenList" resultType="cn.com.glsx.shield.modules.entity.Department"
            parameterType="cn.com.glsx.shield.modules.model.param.OrganizationSearch">
        select tde.id id, tde.department_name departmentName, order_num orderNum,
        tde.created_date createdDate, tde.enable_status enableStatus
        from t_organization tor, t_department tde
        where tor.sub_id = tde.id
        and tor.del_flag = 0 and tde.del_flag = 0
        and tor.superior_id = #{rootId} and tor.depth = 1
        <if test="tenantId != null">
            and tde.tenant_id = #{tenantId}
        </if>
        <if test="orgName != null">
            and tde.department_name like CONCAT('%',#{orgName},'%')
        </if>
        <if test="enableStatus != null">
            and tde.enable_status = #{enableStatus}
        </if>
        <if test="tenantIds != null and tenantIds.size() > 0">
            and tde.tenant_id in
            <foreach collection="tenantIds" index="index" item="item" open="(" separator="," close=")">
                #{item}
            </foreach>
        </if>
        <if test="orgIds != null and orgIds.size() > 0">
            and tde.id in
            <foreach collection="orgIds" index="index" item="item" open="(" separator="," close=")">
                #{item}
            </foreach>
        </if>
        order by tde.order_num
    </select>

    <select id="selectSubOrgList" resultType="cn.com.glsx.shield.modules.entity.Organization">
        select
        distinct(sub_id) subId, superior_id superiorId
        from t_organization
        where del_flag = 0
        and superior_id in
        <foreach collection="superiorIds" item="superiorId" open="(" close=")" separator=",">
            #{superiorId}
        </foreach>
        <if test="depth != null">
            and depth = #{depth}
        </if>
        order by sub_id
    </select>

    <select id="selectList" resultMap="BaseResultMap">
        select
        <include refid="Base_Column_List"/>
        from t_organization
        where del_flag = 0
        <if test="subIdList != null and subIdList.size() > 0">
            and sub_id in
            <foreach collection="subIdList" item="subId" open="(" close=")" separator=",">
                #{subId}
            </foreach>
        </if>
        <if test="superiorIdList != null">
            and superior_id in
            <foreach collection="superiorIdList" item="superiorId" open="(" close=")" separator=",">
                #{superiorId}
            </foreach>
        </if>
        <if test="biggerDepth != null">
            and depth >= #{biggerDepth}
        </if>
    </select>

</mapper>