<?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.aegis.resource.equipment.mapper.ResourceEquipmentLogMapper">

    <resultMap type="EquipmentLog" id="EquipmentLogResult">
        <id     property="id"              column="id"               />
        <result property="equipmentId"     column="equipment_id"     />
        <result property="action"          column="action"           />
        <result property="fromStatus"      column="from_status"      />
        <result property="toStatus"        column="to_status"        />
        <result property="dispatchId"      column="dispatch_id"      />
        <result property="eventId"         column="event_id"         />
        <result property="diff"            column="diff"             />
        <result property="remark"          column="remark"           />
        <result property="operatorId"      column="operator_id"      />
        <result property="operatorName"    column="operator_name"    />
        <result property="requestId"       column="request_id"       />
        <result property="createdAt"       column="created_at"       />
    </resultMap>

    <sql id="selectEquipmentLogVo">
        select id, equipment_id, action, from_status, to_status, dispatch_id, event_id,
               diff, remark, operator_id, operator_name, request_id, created_at
        from resource_equipment_log
    </sql>

    <select id="selectEquipmentLogList" parameterType="EquipmentLog" resultMap="EquipmentLogResult">
        <include refid="selectEquipmentLogVo"/>
        <where>
            <if test="equipmentId != null">
                AND equipment_id = #{equipmentId}
            </if>
            <if test="action != null and action != ''">
                AND action = #{action}
            </if>
            <if test="fromStatus != null and fromStatus != ''">
                AND from_status = #{fromStatus}
            </if>
            <if test="toStatus != null and toStatus != ''">
                AND to_status = #{toStatus}
            </if>
            <if test="params.beginTime != null and params.beginTime != ''">
                AND date_format(created_at,'%Y%m%d') &gt;= date_format(#{params.beginTime},'%Y%m%d')
            </if>
            <if test="params.endTime != null and params.endTime != ''">
                AND date_format(created_at,'%Y%m%d') &lt;= date_format(#{params.endTime},'%Y%m%d')
            </if>
        </where>
        order by created_at desc
    </select>

    <select id="selectEquipmentLogListByEquipmentId" parameterType="Long" resultMap="EquipmentLogResult">
        <include refid="selectEquipmentLogVo"/>
        where equipment_id = #{equipmentId}
        order by created_at desc
    </select>

    <insert id="insertEquipmentLog" parameterType="EquipmentLog" useGeneratedKeys="true" keyProperty="id">
        insert into resource_equipment_log(
            <if test="equipmentId != null">equipment_id,</if>
            <if test="action != null and action != ''">action,</if>
            <if test="fromStatus != null">from_status,</if>
            <if test="toStatus != null">to_status,</if>
            <if test="dispatchId != null">dispatch_id,</if>
            <if test="eventId != null">event_id,</if>
            <if test="diff != null">diff,</if>
            <if test="remark != null">remark,</if>
            <if test="operatorId != null">operator_id,</if>
            <if test="operatorName != null">operator_name,</if>
            <if test="requestId != null">request_id,</if>
            created_at
        )values(
            <if test="equipmentId != null">#{equipmentId},</if>
            <if test="action != null and action != ''">#{action},</if>
            <if test="fromStatus != null">#{fromStatus},</if>
            <if test="toStatus != null">#{toStatus},</if>
            <if test="dispatchId != null">#{dispatchId},</if>
            <if test="eventId != null">#{eventId},</if>
            <if test="diff != null">#{diff},</if>
            <if test="remark != null">#{remark},</if>
            <if test="operatorId != null">#{operatorId},</if>
            <if test="operatorName != null">#{operatorName},</if>
            <if test="requestId != null">#{requestId},</if>
            sysdate()
        )
    </insert>

</mapper>