<?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="CommandLogVO" id="CommandLogResult" autoMapping="true"/>

    <sql id="selectCommandLogVo">
        select
            scl.id,
            scl.type,
            scl.mac,
            scl.reason,
            scl.command,
            scl.result,
            scl.user_id,
            scl.user_name,
            scl.user_type,
            scl.opera_time,
            scl.iot_code
        from ss_command_log scl
    </sql>

    <sql id="searchCondition">
        <if test="query.id != null "> and scl.id = #{query.id}</if>
        <if test="query.type != null  and query.type != ''"> and scl.type = #{query.type}</if>
        <if test="query.mac != null  and query.mac != ''"> and scl.mac like concat('%', #{query.mac}, '%')</if>
        <if test="query.reason != null  and query.reason != ''"> and scl.reason like concat('%', #{query.reason}, '%')</if>
        <if test="query.command != null  and query.command != ''"> and scl.command like concat('%', #{query.command}, '%')</if>
        <if test="query.result != null  and query.result != ''"> and scl.result like concat('%', #{query.result}, '%')</if>
        <if test="query.userId != null "> and scl.user_id = #{query.userId}</if>
        <if test="query.userName != null  and query.userName != ''"> and scl.user_name like concat('%', #{query.userName}, '%')</if>
        <if test="query.userType != null  and query.userType != ''"> and scl.user_type = #{query.userType}</if>
        <if test="query.operaTime != null "> and scl.opera_time = #{query.operaTime}</if>
        <if test="query.iotCode != null "> and iot_code = #{query.iotCode}</if>
        <if test="query.operaTimeRange != null and query.operaTimeRange.size() > 1">
            and scl.opera_time >= #{query.operaTimeRange[0]} and scl.opera_time &lt;= #{query.operaTimeRange[1]}
        </if>
        <if test="query.macList != null and query.macList.size() > 0">
            and scl.mac in
            <foreach collection="query.macList" item="item" open="(" close=")" separator=",">
                #{item}
            </foreach>
        </if>
    </sql>

    <select id="selectCommandLogList" parameterType="CommandLogQuery" resultMap="CommandLogResult">
        <include refid="selectCommandLogVo"/>
        <where>
            <include refid="searchCondition"/>
        </where>
    </select>

    <select id="selectCommandLogById" parameterType="Long" resultMap="CommandLogResult">
        <include refid="selectCommandLogVo"/>
        where id = #{id}
    </select>

    <insert id="insertCommandLog" parameterType="CommandLog" keyProperty="id" useGeneratedKeys="true">
        insert into ss_command_log
        <trim prefix="(" suffix=")" suffixOverrides=",">
            <if test="type != null and type != ''">`type`,</if>
            <if test="mac != null and mac != ''">mac,</if>
            <if test="reason != null">reason,</if>
            <if test="command != null">command,</if>
            <if test="result != null">`result`,</if>
            <if test="userId != null">user_id,</if>
            <if test="userName != null">user_name,</if>
            <if test="userType != null">user_type,</if>
            <if test="operaTime != null">opera_time,</if>
            <if test="iotCode != null">iot_code,</if>
         </trim>
        <trim prefix="values (" suffix=")" suffixOverrides=",">
            <if test="type != null and type != ''">#{type},</if>
            <if test="mac != null and mac != ''">#{mac},</if>
            <if test="reason != null">#{reason},</if>
            <if test="command != null">#{command},</if>
            <if test="result != null">#{result},</if>
            <if test="userId != null">#{userId},</if>
            <if test="userName != null">#{userName},</if>
            <if test="userType != null">#{userType},</if>
            <if test="operaTime != null">#{operaTime},</if>
            <if test="iotCode != null">#{iotCode},</if>
         </trim>
    </insert>


    <insert id="batchInsert" parameterType="CommandLog" keyProperty="id" useGeneratedKeys="true">
        insert into ss_command_log
        <trim prefix="(" suffix=")" suffixOverrides=",">
            type,
            mac,
            reason,
            command,
            result,
            user_id,
            user_name,
            user_type,
            opera_time,
            iot_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.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.userType != null ">#{i.userType},</if>
                <if test="i.userType == null ">default,</if>
                <if test="i.operaTime != null ">#{i.operaTime},</if>
                <if test="i.operaTime == null ">default,</if>
                <if test="i.iotCode != null ">#{i.iotCode},</if>
                <if test="i.iotCode == null ">default,</if>
            </trim>
        </foreach>
    </insert>

    <update id="updateCommandLog" parameterType="CommandLog">
        update ss_command_log
        <trim prefix="SET" suffixOverrides=",">
            <include refid="updateColumns"/>
        </trim>
        where id = #{data.id}
    </update>

    <sql id="updateColumns">
        <if test="data.type != null and data.type != ''">`type` = #{data.type},</if>
        <if test="data.mac != null and data.mac != ''">mac = #{data.mac},</if>
        <if test="data.reason != null">reason = #{data.reason},</if>
        <if test="data.command != null">command = #{data.command},</if>
        <if test="data.result != null">`result` = #{data.result},</if>
        <if test="data.userId != null">user_id = #{data.userId},</if>
        <if test="data.userName != null">user_name = #{data.userName},</if>
        <if test="data.userType != null">user_type = #{data.userType},</if>
        <if test="data.operaTime != null">opera_time = #{data.operaTime},</if>
        <if test="data.iotCode != null">iot_code = #{data.iotCode},</if>
    </sql>

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

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