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

    <resultMap type="com.inspur.domain.ElevatorWorkOrder" id="ElevatorWorkOrderResult">
        <result property="id"    column="id"    />
        <result property="trappedPersonTel"    column="trapped_person_tel"    />
        <result property="region"    column="region"    />
        <result property="regionCode"    column="region_code"    />
        <result property="community"    column="community"    />
        <result property="building"    column="building"    />
        <result property="elevatorName"    column="elevator_name"    />
        <result property="securityCode"    column="security_code"    />
        <result property="status"    column="status"    />
        <result property="createDate"    column="create_date"    />
        <result property="recordingDuration"    column="recording_duration"    />
        <result property="callRecording"    column="call_recording"    />
        <result property="maintCompany"    column="maint_company"    />
        <result property="maintFristPerson"    column="maint_frist_person"    />
        <result property="maintfristTel"    column="maintfrist_tel"    />
        <result property="maintSecondPerson"    column="maint_second_person"    />
        <result property="maintSecondTel"    column="maint_second_tel"    />
        <result property="tenementPerson"    column="tenement_person"    />
        <result property="tenementTel"    column="tenement_tel"    />
        <result property="handler"    column="handler"    />
        <result property="handleContactTel"    column="handle_contact_tel"    />
        <result property="handleDepart"    column="handle_depart"    />
        <result property="handleTime"    column="handle_time"    />
        <result property="delFlag"    column="del_flag"    />
        <result property="createTime"    column="create_time"    />
        <result property="updateTime"    column="update_time"    />
        <result property="remark"    column="remark"    />
        <result property="callId"    column="call_id"    />
        <result property="failNum"    column="fail_num"    />
        <result property="deviceSn"    column="device_sn"    />
        <result property="deviceNo"    column="device_no"    />
    </resultMap>

    <sql id="selectElevatorWorkOrderVo">
        select id, trapped_person_tel, region, community, building, elevator_name, security_code, status, create_date, recording_duration, call_recording, maint_company, maint_frist_person, maintfrist_tel,
               maint_second_person, maint_second_tel, tenement_person, tenement_tel, handler, handle_contact_tel, handle_depart, handle_time, del_flag,
               create_time, update_time, remark,fail_num,call_id, device_sn,device_no, region_code
        from ELEVATOR_EMERGENCY.elevator_work_order
    </sql>

    <select id="selectElevatorWorkOrderList" parameterType="com.inspur.domain.ElevatorWorkOrder" resultMap="ElevatorWorkOrderResult">
        <include refid="selectElevatorWorkOrderVo"/>
        <where>
            <if test="elevatorName != null  and elevatorName != ''"> and elevator_name like concat('%', #{elevatorName}, '%')</if>
            <if test="securityCode != null  and securityCode != ''"> and security_code like concat('%', #{securityCode}, '%')</if>
            <if test="region != null  and region != ''"> and region like concat('%', #{region}, '%')</if>
            <if test="status != null "> and status = #{status}</if>
            <!--<if test="createDate != null "> and create_date = #{createDate}</if>-->
            <if test="createDateStart != null"><!-- 开始时间检索 -->
                AND date_format(create_time,'%Y%m%d') &gt;= date_format(#{createDateStart},'%Y%m%d')
            </if>
            <if test="createDateEnd != null"><!-- 结束时间检索 -->
                AND date_format(create_time,'%Y%m%d') &lt;= date_format(#{createDateEnd},'%Y%m%d')
            </if>
            <if test="deviceSn != null  and deviceSn != ''"> and device_sn = #{deviceSn}</if>
            <if test="deviceNo != null  and deviceNo != ''"> and device_no = #{deviceNo}</if>
            <if test="trappedPersonTel != null  and trappedPersonTel != ''"> and trapped_person_tel = #{trappedPersonTel}</if>
            <if test="regionCode != null  and regionCode != ''"> and region_code = #{regionCode}</if>
            <if test="community != null  and community != ''"> and community = #{community}</if>
            <if test="building != null  and building != ''"> and building = #{building}</if>
            <if test="createDate != null "> and create_date = #{createDate}</if>
            <if test="recordingDuration != null "> and recording_duration = #{recordingDuration}</if>
            <if test="callRecording != null  and callRecording != ''"> and call_recording = #{callRecording}</if>
            <if test="maintCompany != null  and maintCompany != ''"> and maint_company = #{maintCompany}</if>
            <if test="maintFristPerson != null  and maintFristPerson != ''"> and maint_frist_person = #{maintFristPerson}</if>
            <if test="maintfristTel != null  and maintfristTel != ''"> and maintfrist_tel = #{maintfristTel}</if>
            <if test="maintSecondPerson != null  and maintSecondPerson != ''"> and maint_second_person = #{maintSecondPerson}</if>
            <if test="maintSecondTel != null  and maintSecondTel != ''"> and maint_second_tel = #{maintSecondTel}</if>
            <if test="tenementPerson != null  and tenementPerson != ''"> and tenement_person = #{tenementPerson}</if>
            <if test="tenementTel != null  and tenementTel != ''"> and tenement_tel = #{tenementTel}</if>
            <if test="handler != null  and handler != ''"> and handler = #{handler}</if>
            <if test="handleContactTel != null  and handleContactTel != ''"> and handle_contact_tel = #{handleContactTel}</if>
            <if test="handleDepart != null  and handleDepart != ''"> and handle_depart = #{handleDepart}</if>
            <if test="handleTime != null "> and handle_time = #{handleTime}</if>
        </where>
        order by create_time desc
    </select>

    <select id="selectElevatorWorkOrderById" parameterType="Long" resultMap="ElevatorWorkOrderResult">
        <include refid="selectElevatorWorkOrderVo"/>
        where id = #{id}
    </select>

    <select id="selectElevatorWorkOrderByCallId" parameterType="String" resultMap="ElevatorWorkOrderResult">
        <include refid="selectElevatorWorkOrderVo" />
        where call_id = #{callId} order by create_time desc limit 1
    </select>

    <insert id="insertElevatorWorkOrder" parameterType="com.inspur.domain.ElevatorWorkOrder" useGeneratedKeys="true" keyProperty="id">
        insert into ELEVATOR_EMERGENCY.elevator_work_order
        <trim prefix="(" suffix=")" suffixOverrides=",">
            <if test="trappedPersonTel != null">trapped_person_tel,</if>
            <if test="region != null">region,</if>
            <if test="community != null">community,</if>
            <if test="building != null">building,</if>
            <if test="elevatorName != null">elevator_name,</if>
            <if test="securityCode != null">security_code,</if>
            <if test="status != null">status,</if>
            <if test="createDate != null">create_date,</if>
            <if test="recordingDuration != null">recording_duration,</if>
            <if test="callRecording != null">call_recording,</if>
            <if test="maintCompany != null">maint_company,</if>
            <if test="maintFristPerson != null">maint_frist_person,</if>
            <if test="maintfristTel != null">maintfrist_tel,</if>
            <if test="maintSecondPerson != null">maint_second_person,</if>
            <if test="maintSecondTel != null">maint_second_tel,</if>
            <if test="tenementPerson != null">tenement_person,</if>
            <if test="tenementTel != null">tenement_tel,</if>
            <if test="handler != null">handler,</if>
            <if test="handleContactTel != null">handle_contact_tel,</if>
            <if test="handleDepart != null">handle_depart,</if>
            <if test="handleTime != null">handle_time,</if>
            <if test="delFlag != null">del_flag,</if>
            <if test="createTime != null">create_time,</if>
            <if test="updateTime != null">update_time,</if>
            <if test="remark != null">remark,</if>
            <if test="callId != null">call_id,</if>
            <if test="failNum != null">fail_num,</if>
            <if test="deviceSn != null">device_sn,</if>
            <if test="deviceNo != null">device_no,</if>
            <if test="regionCode != null">region_code,</if>
         </trim>
        <trim prefix="values (" suffix=")" suffixOverrides=",">
            <if test="trappedPersonTel != null">#{trappedPersonTel},</if>
            <if test="region != null">#{region},</if>
            <if test="community != null">#{community},</if>
            <if test="building != null">#{building},</if>
            <if test="elevatorName != null">#{elevatorName},</if>
            <if test="securityCode != null">#{securityCode},</if>
            <if test="status != null">#{status},</if>
            <if test="createDate != null">#{createDate},</if>
            <if test="recordingDuration != null">#{recordingDuration},</if>
            <if test="callRecording != null">#{callRecording},</if>
            <if test="maintCompany != null">#{maintCompany},</if>
            <if test="maintFristPerson != null">#{maintFristPerson},</if>
            <if test="maintfristTel != null">#{maintfristTel},</if>
            <if test="maintSecondPerson != null">#{maintSecondPerson},</if>
            <if test="maintSecondTel != null">#{maintSecondTel},</if>
            <if test="tenementPerson != null">#{tenementPerson},</if>
            <if test="tenementTel != null">#{tenementTel},</if>
            <if test="handler != null">#{handler},</if>
            <if test="handleContactTel != null">#{handleContactTel},</if>
            <if test="handleDepart != null">#{handleDepart},</if>
            <if test="handleTime != null">#{handleTime},</if>
            <if test="delFlag != null">#{delFlag},</if>
            <if test="createTime != null">#{createTime},</if>
            <if test="updateTime != null">#{updateTime},</if>
            <if test="remark != null">#{remark},</if>
            <if test="callId != null">#{callId},</if>
            <if test="failNum != null">#{failNum},</if>
            <if test="deviceSn != null">#{deviceSn},</if>
            <if test="deviceNo != null">#{deviceNo},</if>
            <if test="regionCode != null">#{regionCode},</if>
         </trim>
    </insert>

    <update id="updateElevatorWorkOrder" parameterType="ElevatorWorkOrder">
        update ELEVATOR_EMERGENCY.elevator_work_order
        <trim prefix="SET" suffixOverrides=",">
            <if test="trappedPersonTel != null">trapped_person_tel = #{trappedPersonTel},</if>
            <if test="region != null">region = #{region},</if>
            <if test="community != null">community = #{community},</if>
            <if test="building != null">building = #{building},</if>
            <if test="elevatorName != null">elevator_name = #{elevatorName},</if>
            <if test="securityCode != null">security_code = #{securityCode},</if>
            <if test="status != null">status = #{status},</if>
            <if test="createDate != null">create_date = #{createDate},</if>
            <if test="recordingDuration != null">recording_duration = #{recordingDuration},</if>
            <if test="callRecording != null">call_recording = #{callRecording},</if>
            <if test="maintCompany != null">maint_company = #{maintCompany},</if>
            <if test="maintFristPerson != null">maint_frist_person = #{maintFristPerson},</if>
            <if test="maintfristTel != null">maintfrist_tel = #{maintfristTel},</if>
            <if test="maintSecondPerson != null">maint_second_person = #{maintSecondPerson},</if>
            <if test="maintSecondTel != null">maint_second_tel = #{maintSecondTel},</if>
            <if test="tenementPerson != null">tenement_person = #{tenementPerson},</if>
            <if test="tenementTel != null">tenement_tel = #{tenementTel},</if>
            <if test="handler != null">handler = #{handler},</if>
            <if test="handleContactTel != null">handle_contact_tel = #{handleContactTel},</if>
            <if test="handleDepart != null">handle_depart = #{handleDepart},</if>
            <if test="handleTime != null">handle_time = #{handleTime},</if>
            <if test="delFlag != null">del_flag = #{delFlag},</if>
            <if test="createTime != null">create_time = #{createTime},</if>
            <if test="updateTime != null">update_time = #{updateTime},</if>
            <if test="remark != null">remark = #{remark},</if>
            <if test="failNum != null">fail_num = #{failNum},</if>
            <if test="deviceSn != null">device_sn = #{deviceSn},</if>
            <if test="deviceNo != null">device_no = #{deviceNo},</if>
            <if test="regionCode != null">region_code = #{regionCode},</if>
        </trim>
        where id = #{id}
    </update>

    <update id="updateWorkOrderStatusByCallId">
        update ELEVATOR_EMERGENCY.elevator_work_order set status = #{status} where call_id = #{callId}
    </update>

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

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