<?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.xinda.iot.mapper.PipMapper">
    
    <resultMap type="Pip" id="PipResult">
        <result property="deptId"    column="dept_id"    />
        <result property="pipJson"    column="pip_json"    />
    </resultMap>

    <sql id="selectPipVo">
        select p.dept_id, p.pip_json from iot_pip p
    </sql>

    <select id="selectPipList" parameterType="Pip" resultMap="PipResult">
        <include refid="selectPipVo"/>
        <where>
            <if test="deptId != null and deptId != 0 ">
                and
                (   d.dept_id in (SELECT de.dept_id FROM  sys_dept de
                WHERE FIND_IN_SET(#{deptId}, de.ancestors) OR de.dept_id = #{deptId})
                )
            </if>
            <if test="pipJson != null  and pipJson != ''"> and p.pip_json = #{pipJson}</if>
        </where>
    </select>
    
    <select id="selectPipByDeptId" parameterType="Long" resultMap="PipResult">
        <include refid="selectPipVo"/>
        where p.dept_id = #{deptId}
    </select>
        
    <insert id="insertPip" parameterType="Pip">
        insert into iot_pip
        <trim prefix="(" suffix=")" suffixOverrides=",">
            <if test="deptId != null">dept_id,</if>
            <if test="pipJson != null">pip_json,</if>
         </trim>
        <trim prefix="values (" suffix=")" suffixOverrides=",">
            <if test="deptId != null">#{deptId},</if>
            <if test="pipJson != null">#{pipJson},</if>
         </trim>
    </insert>

    <update id="updatePip" parameterType="Pip">
        update iot_pip
        <trim prefix="SET" suffixOverrides=",">
            <if test="pipJson != null">pip_json = #{pipJson},</if>
        </trim>
        where dept_id = #{deptId}
    </update>

    <delete id="deletePipByDeptId" parameterType="Long">
        delete from iot_pip where dept_id = #{deptId}
    </delete>

    <delete id="deletePipByDeptIds" parameterType="String">
        delete from iot_pip where dept_id in 
        <foreach item="deptId" collection="array" open="(" separator="," close=")">
            #{deptId}
        </foreach>
    </delete>
</mapper>