<?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.StaffPurviewMapper">

    <!-- 通用设置 -->
    <!-- 通用查询列 -->
    <sql id="Base_Column_List">
        staff_id
        , purview_code, flg_nolimit, data_ids, cp_id, op_create_time, op_create_user_id, op_update_time, op_update_user_id, op_app_code, op_timestamp, op_db_user
    </sql>

    <!-- 通用查询映射结果 -->
    <resultMap id="BaseResultMap" type="com.dk.common.model.pojo.mst.StaffPurview">
        <id column="staff_id" property="staffId"/>
        <result column="purview_code" property="purviewCode"/>
        <result column="flg_nolimit" property="flgNolimit"/>
        <result column="data_ids" property="dataIds" typeHandler="UuidListTypeHandler"/>
        <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"/>
    </resultMap>

    <!-- 通用条件列 -->
    <sql id="Condition">
        <where>
            <if test="staffId != null and staffId != ''">
                AND staff_id = #{staffId}::uuid
            </if>
            <if test="purviewCode != null and purviewCode != ''">
                AND purview_code = #{purviewCode}
            </if>
            <if test="flgNolimit != null">
                AND flg_nolimit = #{flgNolimit}
            </if>
            <if test="dataIds != null and dataIds != ''">
                AND data_ids = #{dataIds}
            </if>
            <if test="cpId != null">
                AND cp_id = #{cpId}
            </if>
            <if test="opCreateTime != null">
                AND op_create_time = #{opCreateTime}
            </if>
            <if test="opCreateUserId != null and opCreateUserId != ''">
                AND op_create_user_id = #{opCreateUserId}
            </if>
            <if test="opUpdateTime != null">
                AND op_update_time = #{opUpdateTime}
            </if>
            <if test="opUpdateUserId != null and opUpdateUserId != ''">
                AND op_update_user_id = #{opUpdateUserId}
            </if>
            <if test="opAppCode != null and opAppCode != ''">
                AND op_app_code = #{opAppCode}
            </if>
            <if test="opTimestamp != null">
                AND op_timestamp = #{opTimestamp}
            </if>
            <if test="opDbUser != null and opDbUser != ''">
                AND op_db_user = #{opDbUser}
            </if>
        </where>
    </sql>

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

    <!-- 查询表t_mst_staff_purview，（条件查询+分页）列表 -->
    <select id="selectByCond" resultMap="BaseResultMap">
        SELECT
        <include refid="Base_Column_List"/>
        FROM dkic_b.t_mst_staff_purview
        <include refid="Condition"/>
        <if test="pageSize != null and currentPage != null and pageSize != 0 and currentPage != 0">
            limit #{end} offset #{start}
        </if>
    </select>

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

    <!-- 根据主键查询表t_mst_staff_purview的一行数据 -->
    <select id="selectById" resultMap="BaseResultMap">
        SELECT
        <include refid="Base_Column_List"/>
        FROM t_mst_staff_purview
        WHERE staff_id = #{id}::uuid
    </select>

    <!-- 根据主键锁定表t_mst_staff_purview的一行数据 -->
    <select id="selectByIdForUpdate" resultMap="BaseResultMap">
        SELECT
        <include refid="Base_Column_List"/>
        FROM t_mst_staff_purview
        WHERE staff_id = #{staffId}
        for update
    </select>

    <!-- 根据主键锁定表t_mst_staff_purview的多行数据 -->
    <select id="selectByIdsForUpdate" resultMap="BaseResultMap">
        SELECT
        <include refid="Base_Column_List"/>
        FROM t_mst_staff_purview
        <include refid="idsForeach"/>
        for update
    </select>

    <insert id="insertBatch">
        insert into t_mst_staff_purview
        (
        <trim suffixOverrides=",">
            purview_code,
            flg_nolimit,
            data_ids,
            cp_id,
        </trim>
        )
        values
        <foreach collection="list" index="index" item="item" separator=",">
            (
            <trim suffixOverrides=",">
                #{item.purviewCode},
                #{item.flgNolimit},
                #{item.dataIds},
                #{item.cpId},
            </trim>
            )
        </foreach>
    </insert>


    <!-- 获取范围权限 -->
    <select id="getStaffPurview" resultType="java.util.Map">
        SELECT org.org_id                                             AS "id",
               org.org_name                                           AS "orgName",
               org.parent_id                                          AS "parentId",
               orgb.org_id                                            AS "orgId",
               org.level_no                                           AS "levelNo",
               org.level_code                                         as "levelCode",
               COALESCE ( ( SELECT flg_nolimit FROM dkic_b.t_mst_staff_purview WHERE staff_id = #{staffId} :: UUID and purview_code = #{purviewCode}), FALSE ) "flgNolimit",
               CASE WHEN orgb.org_id IS NULL THEN FALSE ELSE TRUE END AS "checked"
        FROM dkic_b.t_mst_org org
                 left JOIN dkic_b.t_mst_org orgb ON orgb.org_id = org.org_id and org.org_id::uuid =  any ( (
                 select  data_ids from dkic_b.t_mst_staff_purview where staff_id = #{staffId}::uuid and purview_code = #{purviewCode})::uuid[])
        WHERE org.flg_valid
          AND org.cp_id = #{cpId}
        ORDER BY org.level_code;
    </select>

    <!-- 批量插入   -->
    <insert id="insertOrUpdateCondition">
        insert into dkic_b.t_mst_staff_purview
        (
        <trim suffixOverrides=",">
            staff_id,
            purview_code,
            flg_nolimit,
            data_ids,
            cp_id,
        </trim>
        )
        values
        <foreach collection="list" index="index" item="item" separator=",">
            (
            <trim suffixOverrides=",">
                #{item.staffId}::uuid,
                #{item.purviewCode},
                #{item.flgNolimit},
                #{item.dataIds, typeHandler=UuidListTypeHandler},
                #{item.cpId},
            </trim>
            )
        </foreach>
        on conflict (staff_id,purview_code) do update set
        flg_nolimit = excluded."flg_nolimit",
        data_ids = excluded."data_ids";
    </insert>

    <!--    通过用户删除-->
    <delete id="deleteByStaff">
        delete from  dkic_b.t_mst_staff_purview
        where Staff_id = #{staffId}::uuid
    </delete>
</mapper>
