<?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.mst.OrgMapper">

    <!-- 通用设置 -->
    <!-- 通用查询列 -->
    <sql id="Base_Column_List">
        org_id
        , org_name, parent_id, display_no, top_id, level_upper, level_lower, level_leaf, level_no, level_code
        , level_name, remarks, flg_valid, cp_id, op_create_time, op_create_user_id, op_update_time, op_update_user_id
        , op_app_code, op_timestamp, op_db_user, org_code
    </sql>
    <sql id="Base_Column_List_view">
                 parent.org_id
                ,parent.org_name
                ,parent.parent_id
                ,parent.flg_business
                ,parent.display_no
                ,parent.top_id
                ,parent.level_upper
                ,parent.level_lower
                ,parent.level_leaf
                ,parent.level_no
                ,parent.level_code
                ,parent.level_name
                ,parent.remarks
                ,parent.flg_valid
                ,parent.cp_id
                         ,
                (
                SELECT
                string_agg ( ma.mac_name, ',' )
                FROM
                dkic_b.t_mst_org_mac AS orgMac
                left join dkic_b.t_mst_money_account ma on orgMac.mac_id=ma.mac_id
                WHERE
                orgMac.org_id = parent.org_id
                ) AS "orgMac"
          ,
                (
                SELECT
                string_agg ( wh.wh_name, ',' )
                FROM
                dkic_b.t_mst_org_wh AS orgWh
                left join dkic_b.t_mst_warehouse wh on wh.wh_id=orgWh.wh_id
                WHERE
                orgWh.org_id = parent.org_id
                ) AS "whName"
    </sql>
    <sql id="Base_Column_List_viewp">
        tparent.org_id
        ,tparent.org_name
                ,tparent.parent_id
                ,tparent.flg_business
                ,tparent.display_no
                ,tparent.top_id
                ,tparent.level_upper
                ,tparent.level_lower
                ,tparent.level_leaf
                ,tparent.level_no
                ,tparent.level_code
                ,tparent.level_name
                ,tparent.remarks
                ,tparent.flg_valid
                ,tparent.cp_id
                         ,
                (
                SELECT
                string_agg ( ma.mac_name, ',' )
                FROM
                dkic_b.t_mst_org_mac AS orgMac
                left join dkic_b.t_mst_money_account ma on orgMac.mac_id=ma.mac_id
                WHERE
                orgMac.org_id = tparent.org_id
                ) AS "orgMac"
          ,
                (
                SELECT
                string_agg ( wh.wh_name, ',' )
                FROM
                dkic_b.t_mst_org_wh AS orgWh
                left join dkic_b.t_mst_warehouse wh on wh.wh_id=orgWh.wh_id
                WHERE
                orgWh.org_id = tparent.org_id
                ) AS "whName"
    </sql>
    <sql id="Base_Column_List_viewT">
        T.org_id
        ,T.org_name
                ,T.parent_id
                ,T.display_no
                ,T.flg_business
                ,T.top_id
                ,T.level_upper
                ,T.level_lower
                ,T.level_leaf
                ,T.level_no
                ,T.level_code
                ,T.level_name
                ,T.remarks
                ,T.flg_valid
                ,T.cp_id
                         ,
             T."orgMac"
          ,
              T."whName"
    </sql>

    <!-- 通用查询映射结果 -->
    <resultMap id="BaseResultMap" type="com.dk.mdm.model.pojo.mst.Org">
        <id column="org_id" property="orgId"/>
        <result column="org_name" property="orgName"/>
        <result column="parent_id" property="parentId" typeHandler="UuidTypeHandler"/>
        <result column="display_no" property="displayNo"/>
        <result column="parent_name" property="parentName"/>
        <result column="flg_business" property="flgBusiness"/>

        <result column="top_id" property="topId" typeHandler="UuidTypeHandler"/>
        <result column="level_upper" property="levelUpper" typeHandler="UuidListTypeHandler"/>
        <result column="level_lower" property="levelLower" typeHandler="UuidListTypeHandler"/>
        <result column="level_leaf" property="levelLeaf"/>
        <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="cp_id" property="cpId"/>
        <result column="op_create_time" property="opCreateTime" typeHandler="TimestampTypeHandler"/>
        <result column="op_create_user_id" property="opCreateUserId" typeHandler="UuidTypeHandler"/>
        <result column="op_update_time" property="opUpdateTime" typeHandler="TimestampTypeHandler"/>
        <result column="op_update_user_id" property="opUpdateUserId" typeHandler="UuidTypeHandler"/>
        <result column="op_app_code" property="opAppCode"/>
        <result column="op_timestamp" property="opTimestamp" typeHandler="TimestampTypeHandler"/>
        <result column="op_db_user" property="opDbUser"/>
        <result column="org_code" property="orgCode"/>
        <collection property="itemData" javaType="java.util.ArrayList" ofType="com.dk.mdm.model.pojo.mst.Org"
                    column="org_item_id">
            <id column="org_item_id" property="orgId"/>
            <result column="org_item_name" property="orgName"/>
        </collection>
    </resultMap>

    <!-- 通用查询映射结果 -->
    <resultMap id="BaseResultMapResponse" type="com.dk.common.model.response.mst.OrgResponse">
        <id column="org_id" property="orgId"/>
        <result column="org_name" property="orgName"/>
        <result column="parent_id" property="parentId" typeHandler="UuidTypeHandler"/>
        <result column="flg_business" property="flgBusiness"/>
        <result column="display_no" property="displayNo"/>
        <result column="top_id" property="topId" typeHandler="UuidTypeHandler"/>
        <result column="level_upper" property="levelUpper" typeHandler="UuidListTypeHandler"/>
        <result column="level_lower" property="levelLower" typeHandler="UuidListTypeHandler"/>
        <result column="level_leaf" property="levelLeaf"/>
        <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="cp_id" property="cpId"/>
        <result column="op_create_time" property="opCreateTime" typeHandler="TimestampTypeHandler"/>
        <result column="op_create_user_id" property="opCreateUserId" typeHandler="UuidTypeHandler"/>
        <result column="op_update_time" property="opUpdateTime" typeHandler="TimestampTypeHandler"/>
        <result column="op_update_user_id" property="opUpdateUserId" typeHandler="UuidTypeHandler"/>
        <result column="op_app_code" property="opAppCode"/>
        <result column="op_timestamp" property="opTimestamp" typeHandler="TimestampTypeHandler"/>
        <result column="op_db_user" property="opDbUser"/>
        <result column="org_code" property="orgCode"/>
        <result column="whName" property="whName"/>
        <result column="orgMac" property="orgMac"/>
        <collection property="itemData" javaType="java.util.ArrayList" ofType="com.dk.mdm.model.pojo.mst.Org"
                    column="org_item_id">
            <id column="org_item_id" property="orgId"/>
            <result column="org_item_name" property="orgName"/>
        </collection>
    </resultMap>

    <!-- 通用条件列 -->
    <sql id="Condition">
        <where>
            <if test="orgName != null and orgName != ''">
                AND parent.org_name = #{orgName}
            </if>
            <if test="flgValidList != null and flgValidList.size>0">
                AND parent.flg_valid =any(#{flgValidList, typeHandler=BooleanListTypeHandler})
            </if>
            <if test="flgValid != null">
                AND flg_valid = #{flgValid}
            </if>
            <if test="cpId != null">
                AND parent.cp_id = #{cpId}
            </if>
            <if test="parentId != null and parentId != ''">
                AND parent.parent_id = #{parentId}::uuid
            </if>
            <if test="filterFlag != null and filterFlag != ''">
                AND  level_code NOT LIKE concat(#{levelCode}, '%')
            </if>

            <if test="searchText !=null">
                AND   parent.org_name LIKE concat('%',my_ex.likequery(#{searchText}), '%')
            </if>
            <if test="pageSize != null and currentPage != null and pageSize != 0 and currentPage != 0">
                limit #{end} offset #{start}
            </if>
        </where>
    </sql>
    <!-- 通用条件列 -->
    <sql id="Condition_no_page">
        <where>
            <if test="orgName != null and orgName != ''">
                AND parent.org_name = #{orgName}
            </if>
            <if test="flgValidList != null and flgValidList.size>0">
                AND parent.flg_valid =any(#{flgValidList, typeHandler=BooleanListTypeHandler})
            </if>
            <if test="flgValid != null">
                AND parent.flg_valid = #{flgValid}
            </if>
            <if test="cpId != null">
                AND parent.cp_id = #{cpId}
            </if>
            <if test="parentId != null and parentId != ''">
                AND parent.parent_id = #{parentId}::uuid
            </if>
            <if test="filterFlag != null and filterFlag != ''">
                AND  level_code NOT LIKE concat(#{levelCode}, '%')
            </if>

            <if test="searchText !=null">
                AND   parent.org_name LIKE concat('%',my_ex.likequery(#{searchText}), '%')
            </if>

        </where>
    </sql>
    <!-- 查询表t_mst_org，（条件查询+分页）列表 -->
    <select id="selectByCond" resultMap="BaseResultMapResponse">
        SELECT
        <include refid="Base_Column_List_view"/>
        FROM
        dkic_b.t_mst_org AS parent
        <include refid="Condition"/>
    </select>
    <!-- 查询表t_mst_org，（条件查询+分页）列表 -->
    <select id="selectByCondTree" resultMap="BaseResultMapResponse">
        SELECT
        <include refid="Base_Column_List_viewT"/>
        FROM
        (
        SELECT
        <include refid="Base_Column_List_view"/>
        FROM
        dkic_b.t_mst_org AS parent
        <include refid="Condition_no_page"/>
        UNION ALL
        SELECT
        <include refid="Base_Column_List_viewp"/>
        FROM
        dkic_b.t_mst_org AS parent
        LEFT JOIN   dkic_b.t_mst_org AS tparent   ON parent.org_id = ANY ( tparent.level_lower )
        <include refid="Condition_no_page"/>
        ) T
        WHERE T.org_id IS NOT NULL
        GROUP BY
        <include refid="Base_Column_List_viewT"/>
    </select>
    <select id="resetLevelOrg" resultType="java.lang.Boolean">
        select dkic_b.f_reset_level_org(#{cpId}, #{topId}::uuid)
    </select>

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

    <!-- 根据主键查询表t_mst_org的一行数据 -->
    <select id="selectById" resultMap="BaseResultMap">
        SELECT
        <include refid="Base_Column_List_view"/>
        ,tmg.org_name as parent_name
        ,parent.org_code
        FROM dkic_b.t_mst_org AS parent
        left join dkic_b.t_mst_org tmg on tmg.org_id=parent.parent_id
        WHERE parent.org_id = #{id}::uuid
    </select>

    <!-- 根据主键锁定表t_mst_org的一行数据 -->
    <select id="selectByIdForUpdate" resultMap="BaseResultMap">
        SELECT
        <include refid="Base_Column_List"/>
        FROM dkic_b.t_mst_org
        WHERE org_id = #{orgId}
        for update
    </select>
    <select id="selectChooseOrg" resultType="com.dk.common.model.response.mst.OrgResponse">
        SELECT
        <include refid="Base_Column_List_view"/>
        FROM dkic_b.t_mst_org parent
        <include refid="Condition"/>
        ORDER BY level_code
    </select>

</mapper>
