<?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.DocMapper">
    
    <resultMap type="Doc" id="DocResult">
        <result property="id"    column="id"    />
        <result property="deptId"    column="dept_id"    />
        <result property="deptName"    column="dept_name"    />
        <result property="fileName"    column="file_name"    />
        <result property="resourceUrl"    column="resource_url"    />
        <result property="tenantId"    column="tenant_id"    />
        <result property="tenantName"    column="tenant_name"    />
        <result property="createBy"    column="create_by"    />
        <result property="createTime"    column="create_time"    />
        <result property="updateBy"    column="update_by"    />
        <result property="updateTime"    column="update_time"    />
    </resultMap>

    <sql id="selectDocVo">
        select d.id, d.dept_id, d.dept_name, d.file_name, d.resource_url, d.tenant_id, d.tenant_name, d.create_by, d.create_time, d.update_by, d.update_time from iot_doc d
    </sql>

    <select id="selectDocList" parameterType="Doc" resultMap="DocResult">
        <include refid="selectDocVo"/>
        <where>  
<!--            <if test="deptId != null "> and dept_id = #{deptId}</if>-->
            <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="deptName != null  and deptName != ''"> and d.dept_name like concat('%', #{deptName}, '%')</if>
            <if test="fileName != null  and fileName != ''"> and d.file_name like concat('%', #{fileName}, '%')</if>
            <if test="resourceUrl != null  and resourceUrl != ''"> and d.resource_url = #{resourceUrl}</if>
<!--            <if test="tenantId != null "> and d.tenant_id = #{tenantId}</if>-->
            <if test="tenantName != null  and tenantName != ''"> and d.tenant_name like concat('%', #{tenantName}, '%')</if>
        </where>
    </select>
    
    <select id="selectDocById" parameterType="Long" resultMap="DocResult">
        <include refid="selectDocVo"/>
        where d.id = #{id}
    </select>
        
    <insert id="insertDoc" parameterType="Doc" useGeneratedKeys="true" keyProperty="id">
        insert into iot_doc
        <trim prefix="(" suffix=")" suffixOverrides=",">
            <if test="deptId != null">dept_id,</if>
            <if test="deptName != null">dept_name,</if>
            <if test="fileName != null">file_name,</if>
            <if test="resourceUrl != null">resource_url,</if>
            <if test="tenantId != null">tenant_id,</if>
            <if test="tenantName != null">tenant_name,</if>
            <if test="createBy != null">create_by,</if>
            <if test="createTime != null">create_time,</if>
            <if test="updateBy != null">update_by,</if>
            <if test="updateTime != null">update_time,</if>
         </trim>
        <trim prefix="values (" suffix=")" suffixOverrides=",">
            <if test="deptId != null">#{deptId},</if>
            <if test="deptName != null">#{deptName},</if>
            <if test="fileName != null">#{fileName},</if>
            <if test="resourceUrl != null">#{resourceUrl},</if>
            <if test="tenantId != null">#{tenantId},</if>
            <if test="tenantName != null">#{tenantName},</if>
            <if test="createBy != null">#{createBy},</if>
            <if test="createTime != null">#{createTime},</if>
            <if test="updateBy != null">#{updateBy},</if>
            <if test="updateTime != null">#{updateTime},</if>
         </trim>
    </insert>

    <update id="updateDoc" parameterType="Doc">
        update iot_doc
        <trim prefix="SET" suffixOverrides=",">
            <if test="deptId != null">dept_id = #{deptId},</if>
            <if test="deptName != null">dept_name = #{deptName},</if>
            <if test="fileName != null">file_name = #{fileName},</if>
            <if test="resourceUrl != null">resource_url = #{resourceUrl},</if>
            <if test="tenantId != null">tenant_id = #{tenantId},</if>
            <if test="tenantName != null">tenant_name = #{tenantName},</if>
            <if test="createBy != null">create_by = #{createBy},</if>
            <if test="createTime != null">create_time = #{createTime},</if>
            <if test="updateBy != null">update_by = #{updateBy},</if>
            <if test="updateTime != null">update_time = #{updateTime},</if>
        </trim>
        where id = #{id}
    </update>

    <delete id="deleteDocById" parameterType="Long">
        delete from iot_doc where id = #{id}
    </delete>

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