<?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.inspur.mapper.ElevatorInfoMapper">

    <resultMap type="com.inspur.domain.ElevatorInfo" id="ElevatorInfoResult">
        <result property="id"    column="id"    />
        <result property="name"    column="name"    />
        <result property="areaCode"    column="area_code"    />
        <result property="inArea"    column="in_area"    />
        <result property="inVillage"    column="in_village"    />
        <result property="inBuild"    column="in_build"    />
        <result property="safeCode"    column="safe_code"    />
        <result property="unitName"    column="unit_name"    />
        <result property="unitCode"    column="unit_code"    />
        <result property="factoryOutTime"    column="factory_out_time"    />
        <result property="createTime"    column="create_time"    />
        <result property="deviceNo"    column="device_no"    />
        <result property="deviceSn"    column="device_sn"    />
        <result property="deviceId"    column="device_id"    />
    </resultMap>

    <sql id="selectElevatorInfoVo">
        select id, name, area_code, in_area, in_village, in_build, safe_code, unit_name, unit_code, factory_out_time, create_time, device_no, device_sn, device_id from ELEVATOR_EMERGENCY.elevator_info
    </sql>

    <select id="selectElevatorInfoList" parameterType="com.inspur.domain.ElevatorInfo" resultMap="ElevatorInfoResult">
        <include refid="selectElevatorInfoVo"/>
        <where>
            <if test="name != null  and name != ''"> and name like concat('%', #{name}, '%')</if>
            <if test="areaCode != null  and areaCode != ''"> and area_code = #{areaCode}</if>
            <if test="inArea != null  and inArea != ''"> and in_area = #{inArea}</if>
            <if test="inVillage != null  and inVillage != ''"> and in_village = #{inVillage}</if>
            <if test="inBuild != null  and inBuild != ''"> and in_build = #{inBuild}</if>
            <if test="safeCode != null  and safeCode != ''"> and safe_code = #{safeCode}</if>
            <if test="unitName != null  and unitName != ''"> and unit_name like concat('%', #{unitName}, '%')</if>
            <if test="unitCode != null  and unitCode != ''"> and unit_code = #{unitCode}</if>
            <if test="factoryOutTime != null  and factoryOutTime != ''"> and factory_out_time = #{factoryOutTime}</if>
            <if test="deviceNo != null  and deviceNo != ''"> and device_no = #{deviceNo}</if>
            <if test="deviceSn != null  and deviceSn != ''"> and device_sn = #{deviceSn}</if>
            <if test="deviceId != null  and deviceId != ''"> and device_id = #{deviceId}</if>
            <if test="elevatorIds != null"> and device_id in
                <foreach item="elevatorId" collection="elevatorIds" open="(" separator="," close=")">
                    #{elevatorId}
                </foreach>
            </if>
            <if test="areaCodes != null"> and area_code in
                <foreach item="areaCode" collection="areaCodes" open="(" separator="," close=")">
                    #{areaCode}
                </foreach>
            </if>
        </where>
    </select>

    <select id="selectElevatorInfoById" parameterType="Long" resultMap="ElevatorInfoResult">
        <include refid="selectElevatorInfoVo"/>
        where id = #{id}
    </select>

    <select id="selectElevatorInfoBySafeCode" parameterType="String" resultMap="ElevatorInfoResult">
        <include refid="selectElevatorInfoVo" />
        where safe_code = #{safe_code} order by create_time desc limit 1
    </select>

    <insert id="insertElevatorInfo" parameterType="com.inspur.domain.ElevatorInfo" useGeneratedKeys="true" keyProperty="id">
        insert into ELEVATOR_EMERGENCY.elevator_info
        <trim prefix="(" suffix=")" suffixOverrides=",">
            <if test="name != null">name,</if>
            <if test="areaCode != null">area_code,</if>
            <if test="inArea != null">in_area,</if>
            <if test="inVillage != null">in_village,</if>
            <if test="inBuild != null">in_build,</if>
            <if test="safeCode != null">safe_code,</if>
            <if test="unitName != null">unit_name,</if>
            <if test="unitCode != null">unit_code,</if>
            <if test="factoryOutTime != null">factory_out_time,</if>
            <if test="createTime != null">create_time,</if>
            <if test="deviceNo != null">device_no,</if>
            <if test="deviceSn != null">device_sn,</if>
            <if test="deviceId != null">device_id,</if>
        </trim>
        <trim prefix="values (" suffix=")" suffixOverrides=",">
            <if test="name != null">#{name},</if>
            <if test="areaCode != null">#{areaCode},</if>
            <if test="inArea != null">#{inArea},</if>
            <if test="inVillage != null">#{inVillage},</if>
            <if test="inBuild != null">#{inBuild},</if>
            <if test="safeCode != null">#{safeCode},</if>
            <if test="unitName != null">#{unitName},</if>
            <if test="unitCode != null">#{unitCode},</if>
            <if test="factoryOutTime != null">#{factoryOutTime},</if>
            <if test="createTime != null">#{createTime},</if>
            <if test="deviceNo != null">#{deviceNo},</if>
            <if test="deviceSn != null">#{deviceSn},</if>
            <if test="deviceId != null">#{deviceId},</if>
        </trim>
    </insert>

    <update id="updateElevatorInfo" parameterType="com.inspur.domain.ElevatorInfo">
        update ELEVATOR_EMERGENCY.elevator_info
        <trim prefix="SET" suffixOverrides=",">
            <if test="name != null">name = #{name},</if>
            <if test="areaCode != null">area_code = #{areaCode},</if>
            <if test="inArea != null">in_area = #{inArea},</if>
            <if test="inVillage != null">in_village = #{inVillage},</if>
            <if test="inBuild != null">in_build = #{inBuild},</if>
            <if test="safeCode != null">safe_code = #{safeCode},</if>
            <if test="unitName != null">unit_name = #{unitName},</if>
            <if test="unitCode != null">unit_code = #{unitCode},</if>
            <if test="factoryOutTime != null">factory_out_time = #{factoryOutTime},</if>
            <if test="createTime != null">create_time = #{createTime},</if>
            <if test="deviceNo != null">device_no = #{deviceNo},</if>
            <if test="deviceSn != null">device_sn = #{deviceSn},</if>
            <if test="deviceId != null">device_id = #{deviceId},</if>
        </trim>
        where id = #{id}
    </update>

    <delete id="deleteElevatorInfoById" parameterType="Long">
        delete from ELEVATOR_EMERGENCY.elevator_info where id = #{id}
    </delete>

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