<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE mapper PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN"
  "https://mybatis.org/dtd/mybatis-3-mapper.dtd">
<mapper namespace="com.zioinfo.mes.admin.mapper.AuditLogMapper">

  <resultMap id="auditMap" type="com.zioinfo.mes.admin.dto.AuditLog">
    <id property="id" column="id"/>
    <result property="actor" column="actor"/>
    <result property="action" column="action"/>
    <result property="target" column="target"/>
    <result property="detail" column="detail"/>
    <result property="createdAt" column="created_at"/>
  </resultMap>

  <insert id="insert" parameterType="com.zioinfo.mes.admin.dto.AuditLog"
          useGeneratedKeys="true" keyProperty="id">
    INSERT INTO mes_audit_log (actor, action, target, detail)
    VALUES (#{actor}, #{action}, #{target}, #{detail})
  </insert>

  <select id="find" resultMap="auditMap">
    SELECT * FROM mes_audit_log
    <where>
      <if test="action != null and action != ''">AND action = #{action}</if>
      <if test="actor != null and actor != ''">AND actor = #{actor}</if>
    </where>
    ORDER BY id DESC
    LIMIT #{limit}
  </select>

</mapper>
