<?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.gameplatform.server.mapper.history.DeviceStatusTransitionMapper">
    <resultMap id="DeviceStatusTransitionMap" type="com.gameplatform.server.model.entity.history.DeviceStatusTransition">
        <id property="id" column="id" />
        <result property="deviceId" column="device_id" />
        <result property="prevStatus" column="prev_status" />
        <result property="newStatus" column="new_status" />
        <result property="reason" column="reason" />
        <result property="snapshotVal" column="snapshot_val" />
        <result property="series" column="series" />
        <result property="indexNo" column="index_no" />
        <result property="occurredAt" column="occurred_at" />
        <result property="createdAt" column="created_at" />
    </resultMap>

    <insert id="insert" parameterType="com.gameplatform.server.model.entity.history.DeviceStatusTransition" useGeneratedKeys="true" keyProperty="id">
        INSERT INTO device_status_transition (device_id, prev_status, new_status, reason, snapshot_val, series, index_no, occurred_at, created_at)
        VALUES (#{deviceId}, #{prevStatus}, #{newStatus}, #{reason}, #{snapshotVal}, #{series}, #{indexNo}, #{occurredAt}, #{createdAt})
    </insert>

    <select id="findRecentByDeviceId" resultMap="DeviceStatusTransitionMap">
        SELECT id, device_id, prev_status, new_status, reason, snapshot_val, series, index_no, occurred_at, created_at
        FROM device_status_transition
        WHERE device_id = #{deviceId}
        ORDER BY created_at DESC
        LIMIT #{size}
    </select>

    <delete id="deleteOldRecords">
        DELETE FROM device_status_transition
        WHERE created_at &lt; DATE_SUB(NOW(), INTERVAL 24 HOUR)
    </delete>
</mapper>


