<?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="com.dk.mdm.mapper.core.OrganizationMapper">

    <!-- 通用设置 -->
    <!-- 通用查询列 -->
    <sql id="Base_Column_List">
        org.org_id, org.org_code, org.org_name, org.parent_id, org.display_no, org.level_no, org.level_code,
        org.level_name, org.remarks, org.flg_valid, org.top_id, org.fty_id, org.level_upper,org.level_lower,
        org.level_leaf, org.op_create_time, org.op_create_user_id,
         org.op_update_time, org.op_update_user_id, org.op_app_code, org.op_timestamp, org.op_db_user
    </sql>

    <!-- 通用查询映射结果 -->
    <resultMap id="BaseResultMap" type="com.dk.mdm.model.response.core.OrganizationResponse">
        <id column="org_id" property="orgId"/>
                <result column="org_code" property="orgCode"/>
                <result column="org_name" property="orgName"/>
                <result column="parent_id" property="parentId"/>
                <result column="parent_name" property="parentName"/>
                <result column="display_no" property="displayNo"/>
                <result column="level_no" property="levelNo"/>
                <result column="level_code" property="levelCode"/>
                <result column="level_name" property="levelName"/>
                <result column="remarks" property="remarks"/>
                <result column="flg_valid" property="flgValid"/>
                <result column="fty_id" property="ftyId"/>
                <result column="top_id" property="topId"/>
            <result column="level_upper" property="levelUpper"
                typeHandler="com.dk.common.infrastructure.handler.IntListTypeHandler"/>
            <result column="level_lower" property="levelLower"
                typeHandler="com.dk.common.infrastructure.handler.IntListTypeHandler"/>
            <result column="level_leaf" property="levelLeaf"/>
            <result column="op_create_time" property="opCreateTime"
                    typeHandler="com.dk.common.infrastructure.handler.TimestampTypeHandler"/>
                <result column="op_create_user_id" property="opCreateUserId"/>
            <result column="op_update_time" property="opUpdateTime"
                    typeHandler="com.dk.common.infrastructure.handler.TimestampTypeHandler"/>
                <result column="op_update_user_id" property="opUpdateUserId"/>
                <result column="op_app_code" property="opAppCode"/>
            <result column="op_timestamp" property="opTimestamp"
                    typeHandler="com.dk.common.infrastructure.handler.TimestampTypeHandler"/>
                <result column="op_db_user" property="opDbUser"/>
    </resultMap>

    <!-- 通用条件列 -->
    <sql id="Condition">
        <where>
            <if test="orgCode != null and orgCode != ''">
                AND position (#{orgCode} in org.org_code) >0
            </if>
            <if test="orgName != null and orgName != ''">
                AND position (#{orgName} in org.org_name) >0
            </if>
            <if test="parentId != null">
                AND org.arent_id = #{parentId}
            </if>
            <if test="displayNo != null">
                AND org.display_no = #{displayNo}
            </if>
            <if test="levelNo != null">
                AND org.level_no = #{levelNo}
            </if>
            <if test="levelCode != null and levelCode != ''">
                AND org.level_code = #{levelCode}
            </if>
            <if test="levelName != null and levelName != ''">
                AND org.level_name = #{levelName}
            </if>
            <if test="remarks != null and remarks != ''">
                AND org.remarks = #{remarks}
            </if>
            <if test="flgValid != null">
                AND org.flg_valid = #{flgValid}
            </if>
            <if test="ftyId != null">
                AND org.fty_id = #{ftyId}
            </if>
            <if test="flgValidList != null and flgValidList.size()>0">
                AND org.flg_valid =
                any(#{flgValidList, typeHandler=BooleanListTypeHandler})
            </if>
            <if test="orgId != null">
                AND org.org_id = #{orgId}
            </if>
        </where>
    </sql>

    <sql id="idsForeach">
        <!-- 根据主键orgId批量操作 -->
        WHERE org_id in
        <foreach collection="ids" index="index" item="item" separator="," open="(" close=")">
            #{item}
        </foreach>
    </sql>

    <!-- 查询表core.t_organization，（条件查询+分页）列表 -->
    <select id="selectByCond" resultMap="BaseResultMap">
        SELECT
        <include refid="Base_Column_List"/> ,parent.org_name AS "parent_name"
        FROM core.t_organization AS org
        LEFT JOIN core.t_organization AS parent
        ON org.parent_id = parent.org_id
        <include refid="Condition"/>
        ORDER BY org.flg_valid DESC,org.display_no
        <if test="pageSize != null and currentPage != null and pageSize != 0 and currentPage != 0">
            limit #{end} offset #{start}
        </if>
    </select>

    <!-- 查询表core.t_organization，（条件查询）个数 -->
    <select id="countByCond" resultType="Long">
        SELECT
        count(1)
        FROM core.t_organization AS org
        <include refid="Condition"/>
    </select>

    <!--组织部门编码是否重复-->
    <select id="selectByCode" resultType="Long">
        select count(1)
        from core.t_organization
        where #{orgCode} = org_code and fty_id = #{ftyId}
        <if test="orgId != null and orgId != 0">
            and org_id != #{orgId}
        </if>
    </select>

    <!--更新组织部门层级相关字段-->
    <select id="selectResetOrgLevel" resultType="boolean">
        select core.f_reset_org_level(#{ftyId},#{topId})
    </select>

    <!--更新最顶级部门的topId-->
    <update id="updateOrgTopId">
        update core.t_organization
        set top_id = #{orgId}
        where #{orgId} = org_id
    </update>

    <!--更新部门的有效标识-->
    <update id="updateOrgFlgValid">
        update core.t_organization
        set flg_valid = #{flgValid}
        where #{orgId} = org_id
    </update>

    <!--根据部门Id获取有效标识-->
    <select id="selectFlgValidIdById" resultType="boolean">
        select flg_valid
        from core.t_organization
        where #{parentId} = org_id
    </select>
</mapper>
