<?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.dcsoft.system.mapper.SysSpaceMapper">

    <resultMap type="SysSpace" id="SysSpaceResult">
        <result property="id"    column="id"    />
        <result property="name"    column="name"    />
        <result property="parentId"    column="parent_id"    />
        <result property="ancestors"    column="ancestors"    />
        <result property="type"    column="type"    />
        <result property="remark"    column="remark"    />
        <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="selectSysSpaceVo">
        select id, name, parent_id, ancestors, type, remark, create_by, create_time, update_by, update_time from sys_space
    </sql>

    <select id="selectSysSpaceList" parameterType="SysSpace" resultMap="SysSpaceResult">
        <include refid="selectSysSpaceVo"/>
        <where>
            <if test="name != null  and name != ''"> and name like concat('%', #{name}, '%')</if>
            <if test="type != null  and type != ''"> and type = #{type}</if>

        </where>
    </select>

    <select id="selectSysSpaceById" parameterType="Long" resultMap="SysSpaceResult">
        <include refid="selectSysSpaceVo"/>
        where id = #{id}
    </select>
    <select id="selectTree" resultType="com.dcsoft.system.domain.vo.SysAuthTree">
        select id, name label, parent_id parentId, type, '1' deviceId, '1' channelId
        from sys_space
        where 1=1
        <if test="name != null and name != ''">
            and name like concat('%', #{name}, '%')
        </if>
        union all
        select p.id, p.name label, p.space_id parentId, '1' type, '1' deviceId, '1' channelId
        from sys_point p
        where 1=1
        <if test="name != null and name != ''">
            and name like concat('%', #{name}, '%')
        </if>
        union all
        select c.id, c.name label, c.point_id parentId, '2' type, c.device_id deviceId, c.channel_id channelId
        from tb_camera c
        where 1=1
        <if test="name != null and name != ''">
            and name like concat('%', #{name}, '%')
        </if>
    </select>

    <insert id="insertSysSpace" parameterType="SysSpace">
        insert into sys_space
        <trim prefix="(" suffix=")" suffixOverrides=",">
            <if test="id != null and id != 0">id,</if>
            <if test="name != null and name != ''">name,</if>
            <if test="parentId != null and parentId != 0">parent_id,</if>
            <if test="ancestors != null">ancestors,</if>
            <if test="type != null and type != ''">type,</if>
            <if test="remark != null">remark,</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="id != null and id != 0">#{id},</if>
            <if test="name != null and name != ''">#{name},</if>
            <if test="parentId != null and parentId != 0">#{parentId},</if>
            <if test="ancestors != null">#{ancestors},</if>
            <if test="type != null and type != ''">#{type},</if>
            <if test="remark != null">#{remark},</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="updateSysSpace" parameterType="SysSpace">
        update sys_space
        <trim prefix="SET" suffixOverrides=",">
            <if test="name != null and name != ''">name = #{name},</if>
            <if test="parentId != null">parent_id = #{parentId},</if>
            <if test="ancestors != null">ancestors = #{ancestors},</if>
            <if test="type != null and type != ''">type = #{type},</if>
            <if test="remark != null">remark = #{remark},</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="deleteSysSpaceById" parameterType="Long">
        delete from sys_space where id = #{id}
    </delete>

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