<?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.ruoyi.ss.commandLog.mapper.CommandLogMapper">

    <resultMap type="CommandLog" id="EtCommandLogResult">
        <result property="id"    column="id"    />
        <result property="command"    column="command"    />
        <result property="type"    column="type"    />
        <result property="reason"    column="reason"    />
        <result property="mac"    column="mac"    />
        <result property="sn"    column="sn"    />
        <result property="result"    column="result"    />
        <result property="code"    column="code"    />
        <result property="operaTime"    column="opera_time"    />
        <result property="userId"    column="user_id"    />
        <result property="userName"    column="user_name"    />
    </resultMap>

    <sql id="selectEtCommandLogVo">
        select id, command, type, reason, mac, sn, result, code, opera_time, user_id, user_name from ss_command_log
    </sql>

    <select id="selectEtCommandLogList" parameterType="CommandLog" resultMap="EtCommandLogResult">
        <include refid="selectEtCommandLogVo"/>
        <where>
            <if test="command != null  and command != ''"> and command like concat('%', #{command}, '%')</if>
            <if test="mac != null  and mac != ''"> and mac like concat('%', #{mac}, '%')</if>
            <if test="sn != null  and sn != ''"> and sn like concat('%', #{sn}, '%')</if>
            <if test="type != null  and type != ''"> and type like concat('%', #{type}, '%')</if>
            <if test="result != null  and result != ''"> and result like concat('%', #{result}, '%')</if>
            <if test="code != null  and code != ''"> and code = #{code}</if>
            <if test="userId != null"> and user_id = #{userId}</if>
            <if test="userName != null  and userName != ''"> and user_name like concat('%', #{userName}, '%')</if>
        </where>
        order by opera_time desc
    </select>

    <select id="selectEtCommandLogById" parameterType="Long" resultMap="EtCommandLogResult">
        <include refid="selectEtCommandLogVo"/>
        where id = #{id}
    </select>

    <insert id="insertEtCommandLog" parameterType="CommandLog" useGeneratedKeys="true" keyProperty="id">
        insert into ss_command_log
        <trim prefix="(" suffix=")" suffixOverrides=",">
            <if test="command != null">command,</if>
            <if test="type != null">type,</if>
            <if test="reason != null">reason,</if>
            <if test="mac != null">mac,</if>
            <if test="sn != null">sn,</if>
            <if test="result != null">result,</if>
            <if test="code != null">code,</if>
            <if test="userId != null">user_id,</if>
            <if test="userName != null">user_name,</if>
            opera_time
         </trim>
        <trim prefix="values (" suffix=")" suffixOverrides=",">
            <if test="command != null">#{command},</if>
            <if test="type != null">#{type},</if>
            <if test="reason != null">#{reason},</if>
            <if test="mac != null">#{mac},</if>
            <if test="sn != null">#{sn},</if>
            <if test="result != null">#{result},</if>
            <if test="code != null">#{code},</if>
            <if test="userId != null">#{userId},</if>
            <if test="userName != null">#{userName},</if>
            sysdate()
         </trim>
    </insert>

    <insert id="batchInsert" parameterType="CommandLog" keyProperty="id" useGeneratedKeys="true">
        insert into ss_command_log
        <trim prefix="(" suffix=")" suffixOverrides=",">
            type,
            mac,
            sn,
            reason,
            command,
            result,
            user_id,
            user_name,
            opera_time,
            code,
        </trim>
        values
        <foreach collection="list" item="i" separator=",">
            <trim prefix="(" suffix=")" suffixOverrides=",">
                <if test="i.type != null  and i.type != ''">#{i.type},</if>
                <if test="i.type == null  or i.type == ''">default,</if>
                <if test="i.mac != null  and i.mac != ''">#{i.mac},</if>
                <if test="i.mac == null  or i.mac == ''">default,</if>
                <if test="i.sn != null  and i.sn != ''">#{i.sn},</if>
                <if test="i.sn == null  or i.sn == ''">default,</if>
                <if test="i.reason != null ">#{i.reason},</if>
                <if test="i.reason == null ">default,</if>
                <if test="i.command != null ">#{i.command},</if>
                <if test="i.command == null ">default,</if>
                <if test="i.result != null ">#{i.result},</if>
                <if test="i.result == null ">default,</if>
                <if test="i.userId != null ">#{i.userId},</if>
                <if test="i.userId == null ">default,</if>
                <if test="i.userName != null ">#{i.userName},</if>
                <if test="i.userName == null ">default,</if>
                <if test="i.operaTime != null ">#{i.operaTime},</if>
                <if test="i.operaTime == null ">default,</if>
                <if test="i.code != null ">#{i.code},</if>
                <if test="i.code == null ">default,</if>
            </trim>
        </foreach>
    </insert>

    <update id="updateEtCommandLog" parameterType="CommandLog">
        update ss_command_log
        <trim prefix="SET" suffixOverrides=",">
            <if test="command != null">command = #{command},</if>
            <if test="type != null">type = #{type},</if>
            <if test="reason != null">reason = #{reason},</if>
            <if test="mac != null">mac = #{mac},</if>
            <if test="sn != null">sn = #{sn},</if>
            <if test="result != null">result = #{result},</if>
            <if test="code != null">code = #{code},</if>
            <if test="userId != null">user_id = #{userId},</if>
            <if test="userName != null">user_name = #{userName},</if>
        </trim>
        where id = #{id}
    </update>

    <delete id="deleteEtCommandLogById" parameterType="Long">
        delete from ss_command_log where id = #{id}
    </delete>

    <delete id="deleteEtCommandLogByIds" parameterType="String">
        delete from ss_command_log where id in
        <foreach item="id" collection="array" open="(" separator="," close=")">
            #{id}
        </foreach>
    </delete>

    <delete id="clearBeforeDateLater">
        delete from ss_command_log where date(opera_time) &lt; #{date}
    </delete>
</mapper>
