<?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.cm.smartgate.dao.ISmartGateDao">

    <cache/>

    <resultMap id="smartGateDTO" type="com.cm.smartgate.pojo.dtos.SmartGateDTO">
        <id column="smartgate_id" property="smartGateId"/>
        <result column="smartgate_serial_no" property="smartGateSerialNo"/>
        <result column="smartgate_name" property="smartGateName"/>
        <result column="smartgate_summary" property="smartGateSummary"/>
        <result column="smartgate_ip_address" property="smartGateIpAddress"/>
        <result column="smartgate_port" property="smartGatePort"/>
        <result column="smartgate_username" property="smartGateUsername"/>
        <result column="smartgate_password" property="smartGatePassword"/>
        <result column="gmt_create" property="gmtCreate"/>
    </resultMap>

    <!-- 新增智慧闸机 -->
    <insert id="saveSmartGate" parameterType="map" flushCache="true">
        INSERT INTO hardware_smartgate(
            smartgate_id,
            smartgate_serial_no,
            smartgate_name,
            smartgate_summary,
            smartgate_ip_address,
            smartgate_port,
            smartgate_username,
            smartgate_password,
            creator,
            gmt_create,
            modifier,
            gmt_modified,
            is_delete
        ) VALUES(
            #{smartGateId},
            #{smartGateSerialNo},
            #{smartGateName},
            #{smartGateSummary},
            #{smartGateIpAddress},
            #{smartGatePort},
            #{smartGateUsername},
            #{smartGatePassword},
            #{creator},
            #{gmtCreate},
            #{modifier},
            #{gmtModified},
            #{isDelete}
        )
    </insert>

    <!-- 删除智慧闸机 -->
    <update id="removeSmartGate" parameterType="map" flushCache="true">
        UPDATE
            hardware_smartgate
        SET
            is_delete = 1,
            modifier = #{modifier},
            gmt_modified = #{gmtModified}
        WHERE
            smartgate_id IN
        <foreach collection="smartGateIds" index="index" open="(" separator="," close=")">
            #{smartGateIds[${index}]}
        </foreach>
    </update>

    <!-- 删除智慧闸机（物理） -->
    <update id="deleteSmartGate" parameterType="map" flushCache="true">
        DELETE FROM
            hardware_smartgate
        WHERE
            smartgate_id IN
        <foreach collection="smartGateIdIds" index="index" open="(" separator="," close=")">
            #{smartGateIds[${index}]}
        </foreach>
    </update>

    <!-- 修改智慧闸机 -->
    <update id="updateSmartGate" parameterType="map" flushCache="true">
        UPDATE
            hardware_smartgate
        SET
        <if test="smartGateSerialNo != null and smartGateSerialNo != ''">
            smartgate_serial_no = #{smartGateSerialNo},
        </if>
        <if test="smartGateName != null and smartGateName != ''">
            smartgate_name = #{smartGateName},
        </if>
        <if test="smartGateSummary != null">
            smartgate_summary = #{smartGateSummary},
        </if>
        <if test="smartGateIpAddress != null and smartGateIpAddress != ''">
            smartgate_ip_address = #{smartGateIpAddress},
        </if>
        <if test="smartGatePort != null and smartGatePort != ''">
            smartgate_port = #{smartGatePort},
        </if>
        <if test="smartGateUsername != null">
            smartgate_username = #{smartGateUsername},
        </if>
        <if test="smartGatePassword != null">
            smartgate_password = #{smartGatePassword},
        </if>
            modifier = #{modifier},
            gmt_modified = #{gmtModified}
        WHERE
            smartgate_id = #{smartGateId}
    </update>

    <!-- 智慧闸机详情(通过ID) -->
    <select id="getSmartGate" parameterType="map" resultMap="smartGateDTO" useCache="true">
        SELECT
            smartgate_id,
            smartgate_serial_no,
            smartgate_name,
            smartgate_summary,
            smartgate_ip_address,
            smartgate_port,
            smartgate_username,
            smartgate_password,
            LEFT(gmt_create, 19) gmt_create
        FROM
            hardware_smartgate
        WHERE
            is_delete = 0
        <if test="smartGateId != null and smartGateId != ''">
        AND
            smartgate_id = #{smartGateId}
        </if>
        <if test="smartGateSerialNo != null and smartGateSerialNo != ''">
        AND
            smartgate_serial_no = #{smartGateSerialNo}
        </if>
    </select>

    <!-- 智慧闸机列表 -->
    <select id="listSmartGate" parameterType="map" resultMap="smartGateDTO" useCache="true">
        SELECT
            smartgate_id,
            smartgate_serial_no,
            smartgate_name,
            smartgate_summary,
            smartgate_ip_address,
            smartgate_port,
            LEFT(gmt_create, 19) gmt_create
        FROM
            hardware_smartgate
        WHERE
            is_delete = 0
        <if test="keywords != null and keywords != ''">
        AND (
            smartgate_name LIKE CONCAT('%', #{keywords}, '%')
        )
        </if>
        <if test="startTime != null and startTime != ''">
        AND
            LEFT(gmt_create, 10) <![CDATA[ >= ]]> #{startTime}
        </if>
        <if test="endTime != null and endTime != ''">
        AND
            LEFT(gmt_create, 10) <![CDATA[ <= ]]> #{endTime}
        </if>
        <if test="smartGateIds != null and smartGateIds.size > 0">
        AND
            smartgate_id IN
            <foreach collection="smartGateIds" index="index" open="(" separator="," close=")">
                #{smartGateIds[${index}]}
            </foreach>
        </if>
    </select>

    <!-- 智慧闸机统计 -->
    <select id="countSmartGate" parameterType="map" resultType="java.lang.Integer" useCache="true">
        SELECT
            COUNT(*)
        FROM
            central_client
        WHERE
            is_delete = 0
    </select>

</mapper>