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

    <resultMap type="SysRule" id="SysRuleResult">
        <result property="id"    column="id"    />
        <result property="name"    column="name"    />
        <result property="startTime"    column="start_time"    />
        <result property="endTime"    column="end_time"    />
        <result property="admittanceStart"    column="admittance_start"    />
        <result property="admittanceEnd"    column="admittance_end"    />
        <result property="spaceId"    column="space_id"    />
        <result property="spaceName"    column="space_name"    />
        <result property="pointId"    column="point_id"    />
        <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"    />
        <result property="type"    column="type"    />
        <result property="permission"    column="permission"    />
    </resultMap>

    <sql id="selectSysRuleVo">
        select sr.id, sr.name, sr.start_time, sr.end_time, sr.admittance_start, sr.admittance_end, sr.space_id, sr.point_id,
               sr.remark, sr.create_by, sr.create_time, sr.update_by, sr.update_time, sr.type, sr.permission,ss.name space_name
        from sys_rule sr
        left join sys_space ss on sr.space_id =ss.id
    </sql>

    <select id="selectSysRuleList" parameterType="SysRule" resultMap="SysRuleResult">
        <include refid="selectSysRuleVo"/>
        <where>
            <if test="name != null  and name != ''"> and sr.name like concat('%', #{name}, '%')</if>
            <if test="startTime != null "> and sr.start_time = #{startTime}</if>
            <if test="endTime != null "> and sr.end_time = #{endTime}</if>
            <if test="admittanceStart != null  and admittanceStart != ''"> and sr.admittance_start = #{admittanceStart}</if>
            <if test="admittanceEnd != null  and admittanceEnd != ''"> and sr.admittance_end = #{admittanceEnd}</if>
            <if test="spaceId != null "> and sr.space_id = #{spaceId}</if>
            <if test="pointId != null  and pointId != ''"> and sr.point_id = #{pointId}</if>
            <if test="type != null  and type != ''"> and sr.type = #{type}</if>
            <if test="permission != null  and permission != ''"> and sr.permission = #{permission}</if>
        </where>
    </select>

    <select id="selectSysRuleById" parameterType="Long" resultMap="SysRuleResult">
        <include refid="selectSysRuleVo"/>
        where sr.id = #{id}
    </select>

    <insert id="insertSysRule" parameterType="SysRule" useGeneratedKeys="true" keyProperty="id">
        insert into sys_rule
        <trim prefix="(" suffix=")" suffixOverrides=",">
            <if test="name != null and name != ''">name,</if>
            <if test="startTime != null">start_time,</if>
            <if test="endTime != null">end_time,</if>
            <if test="admittanceStart != null">admittance_start,</if>
            <if test="admittanceEnd != null">admittance_end,</if>
            <if test="spaceId != null">space_id,</if>
            <if test="pointId != null">point_id,</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>
            <if test="type != null">type,</if>
            <if test="permission != null">permission,</if>
         </trim>
        <trim prefix="values (" suffix=")" suffixOverrides=",">
            <if test="name != null and name != ''">#{name},</if>
            <if test="startTime != null">#{startTime},</if>
            <if test="endTime != null">#{endTime},</if>
            <if test="admittanceStart != null">#{admittanceStart},</if>
            <if test="admittanceEnd != null">#{admittanceEnd},</if>
            <if test="spaceId != null">#{spaceId},</if>
            <if test="pointId != null">#{pointId},</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>
            <if test="type != null">#{type},</if>
            <if test="permission != null">#{permission},</if>
         </trim>
    </insert>

    <update id="updateSysRule" parameterType="SysRule">
        update sys_rule
        <trim prefix="SET" suffixOverrides=",">
            <if test="name != null and name != ''">name = #{name},</if>
            <if test="startTime != null">start_time = #{startTime},</if>
            <if test="endTime != null">end_time = #{endTime},</if>
            <if test="admittanceStart != null">admittance_start = #{admittanceStart},</if>
            <if test="admittanceEnd != null">admittance_end = #{admittanceEnd},</if>
            <if test="spaceId != null">space_id = #{spaceId},</if>
            <if test="pointId != null">point_id = #{pointId},</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>
            <if test="type != null">type = #{type},</if>
            <if test="permission != null">permission = #{permission},</if>
        </trim>
        where id = #{id}
    </update>

    <delete id="deleteSysRuleById" parameterType="Long">
        delete from sys_rule where id = #{id}
    </delete>

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