<?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.dgtly.system.mapper.AlertLogMapper">
    
    <resultMap type="AlertLog" id="AlertLogResult">
        <result property="alertLogId"    column="alert_log_id"    />
        <result property="alertId"    column="alert_id"    />
        <result property="createBy"    column="create_by"    />
        <result property="createTime"    column="create_time"    />
        <result property="updateBy"    column="update_by"    />
        <result property="updateTime"    column="update_time"    />
        <result property="isDelete"    column="is_delete"    />
        <result property="userId"    column="user_id"    />
        <result property="loginName"    column="login_name"    />
        <result property="alertNum"    column="alert_num"    />
        <result property="userType"    column="user_type"    />
        <result property="isClearCache"    column="is_clear_cache"    />
    </resultMap>

    <sql id="selectAlertLogVo">
        select alert_log_id, alert_id, create_by, create_time, update_by, update_time, is_delete, user_id, login_name, alert_num, user_type,is_clear_cache from alert_log
    </sql>

    <select id="selectAlertLogList" parameterType="AlertLog" resultMap="AlertLogResult">
        <include refid="selectAlertLogVo"/>
        <where>  
            <if test="alertId != null "> and alert_id = #{alertId}</if>
            <if test="isDelete != null "> and is_delete = #{isDelete}</if>
            <if test="userId != null "> and user_id = #{userId}</if>
            <if test="loginName != null  and loginName != ''"> and login_name like concat('%', #{loginName}, '%')</if>
            <if test="alertNum != null "> and alert_num = #{alertNum}</if>
            <if test="userType != null  and userType != ''"> and user_type = #{userType}</if>
        </where>
    </select>
    
    <select id="selectAlertLogById" parameterType="Long" resultMap="AlertLogResult">
        <include refid="selectAlertLogVo"/>
        where alert_log_id = #{alertLogId}
    </select>

    <select id="selectAlertLogByUserId" parameterType="AlertLog" resultMap="AlertLogResult">
        <include refid="selectAlertLogVo"/>
        where user_id = #{userId}
    </select>

    <select id="selectAlertLogByUserIdAndAlertId" parameterType="AlertLog" resultMap="AlertLogResult">
        <include refid="selectAlertLogVo"/>
        where 1=1
        <if test="userId!=null">and user_id = #{userId}</if>
        <if test="alertId != null ">and alert_id = #{alertId}</if>
        <if test="isCacheType !=null and  isCacheType == '0'.toString()">
            and DATE_FORMAT(create_time,'%Y%m%d') = DATE_FORMAT(NOW(),'%Y%m%d')
        </if>
        order by  create_time desc
        limit 1
    </select>

    <insert id="insertAlertLog" parameterType="AlertLog" useGeneratedKeys="true" keyProperty="alertLogId">
        insert into alert_log
        <trim prefix="(" suffix=")" suffixOverrides=",">
            <if test="alertId != null ">alert_id,</if>
            <if test="createBy != null  and createBy != ''">create_by,</if>
            <if test="createTime != null ">create_time,</if>
            <if test="updateBy != null  and updateBy != ''">update_by,</if>
            <if test="updateTime != null ">update_time,</if>
            <if test="isDelete != null ">is_delete,</if>
            <if test="userId != null ">user_id,</if>
            <if test="loginName != null  and loginName != ''">login_name,</if>
            <if test="alertNum != null ">alert_num,</if>
            <if test="userType != null  and userType != ''">user_type,</if>
            <if test="isClearCache !=null and isClearCache !='' ">is_clear_cache,</if>
         </trim>
        <trim prefix="values (" suffix=")" suffixOverrides=",">
            <if test="alertId != null ">#{alertId},</if>
            <if test="createBy != null  and createBy != ''">#{createBy},</if>
            <if test="createTime != null ">#{createTime},</if>
            <if test="updateBy != null  and updateBy != ''">#{updateBy},</if>
            <if test="updateTime != null ">#{updateTime},</if>
            <if test="isDelete != null ">#{isDelete},</if>
            <if test="userId != null ">#{userId},</if>
            <if test="loginName != null  and loginName != ''">#{loginName},</if>
            <if test="alertNum != null ">#{alertNum},</if>
            <if test="userType != null  and userType != ''">#{userType},</if>
            <if test="isClearCache !=null and isClearCache !='' ">#{isClearCache},</if>
         </trim>
    </insert>

    <update id="updateAlertLog" parameterType="AlertLog">
        update alert_log
        <trim prefix="SET" suffixOverrides=",">
            <if test="alertId != null ">alert_id = #{alertId},</if>
            <if test="createBy != null  and createBy != ''">create_by = #{createBy},</if>
            <if test="createTime != null ">create_time = #{createTime},</if>
            <if test="updateBy != null  and updateBy != ''">update_by = #{updateBy},</if>
            <if test="updateTime != null ">update_time = #{updateTime},</if>
            <if test="isDelete != null ">is_delete = #{isDelete},</if>
            <if test="userId != null ">user_id = #{userId},</if>
            <if test="loginName != null  and loginName != ''">login_name = #{loginName},</if>
            <if test="alertNum != null ">alert_num = #{alertNum},</if>
            <if test="userType != null  and userType != ''">user_type = #{userType},</if>
            <if test="isClearCache !=null and isClearCache !='' ">is_clear_cache = #{isClearCache},</if>
        </trim>
        where alert_log_id = #{alertLogId}
    </update>

    <delete id="deleteAlertLogById" parameterType="Long">
        delete from alert_log where alert_log_id = #{alertLogId}
    </delete>

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