<?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.yunfeiyun.agmp.fms.mapper.SysWarnMapper">
    
    <resultMap type="SysWarn" id="SysWarnResult">
        <result property="warnId"    column="warnId"    />
        <result property="warnClass"    column="warnClass"    />
        <result property="warnType"    column="warnType"    />
        <result property="warnCreateddate"    column="warnCreateddate"    />
        <result property="warnJumpurl"    column="warnJumpurl"    />
        <result property="warnRemark"    column="warnRemark"    />
    </resultMap>

    <sql id="selectSysWarnVo">
        select warnId, warnClass, warnType, warnCreateddate, warnJumpurl, warnRemark from SysWarn
    </sql>

    <select id="selectSysWarnList" parameterType="SysWarnReqVo" resultType="SysWarnResVo">
        SELECT w.*, wr.warnreadCreateddate, (case when wr.warnreadId is null then 0 else 1 end) readStatus
        FROM SysWarn w LEFT JOIN SysWarnread wr on w.warnId = wr.warnId and wr.userId = #{userId}
        having 1 = 1
        <if test="startTime != null  and startTime != ''"> and w.warnCreateddate >= #{startTime}</if>
        <if test="endTime != null  and endTime != ''"> and w.warnCreateddate <![CDATA[ <= ]]> #{endTime}</if>
        <if test="warnClass != null  and warnClass != ''"> and w.warnClass = #{warnClass}</if>
        <if test="warnType != null  and warnType != ''"> and w.warnType = #{warnType}</if>
        <if test="readStatus != null  and readStatus != ''"> and readStatus = #{readStatus}</if>
        ORDER BY readStatus asc,w.warnCreateddate desc
    </select>

    <select id="selectSysWarnListOfScreen" parameterType="SysWarnReqVo" resultType="SysWarnResVo">
        SELECT w.*
        FROM SysWarn w
        <if test="startTime != null  and startTime != ''"> and warnCreateddate >= #{startTime}</if>
        <if test="endTime != null  and endTime != ''"> and warnCreateddate <![CDATA[ <= ]]> #{endTime}</if>
        <if test="warnClass != null  and warnClass != ''"> and warnClass = #{warnClass}</if>
        <if test="warnType != null  and warnType != ''"> and warnType = #{warnType}</if>
        ORDER BY warnCreateddate desc
        limit 8
    </select>

    <select id="selectCountOfSysWarn" parameterType="SysWarnReqVo" resultType="SysWarnResVo">
        SELECT count(1) count, warnClass
        FROM SysWarn
        <where>
            <if test="startTime != null  and startTime != ''"> and warnCreateddate >= #{startTime}</if>
            <if test="endTime != null  and endTime != ''"> and warnCreateddate <![CDATA[ <= ]]> #{endTime}</if>
        </where>
        group by warnClass
    </select>

    <select id="selectCount" parameterType="SelectCountOfScreenReqVo" resultType="Integer">
        SELECT count(1) FROM SysWarn
        <where>
            <if test="warnClass != null  and warnClass != ''"> and warnClass = #{warnClass}</if>
            <if test="warnType != null  and warnType != ''"> and warnType = #{warnType}</if>
            <if test="startTime != null  and startTime != ''"> and warnCreateddate >= #{startTime}</if>
            <if test="endTime != null  and endTime != ''"> and warnCreateddate <![CDATA[ <= ]]> #{endTime}</if>
        </where>
    </select>
    
    <select id="selectSysWarnByWarnId" parameterType="String" resultMap="SysWarnResult">
        <include refid="selectSysWarnVo"/>
        where warnId = #{warnId}
    </select>
        
    <insert id="insertSysWarn" parameterType="SysWarn">
        insert into SysWarn
        <trim prefix="(" suffix=")" suffixOverrides=",">
            <if test="warnId != null">warnId,</if>
            <if test="warnClass != null">warnClass,</if>
            <if test="warnType != null">warnType,</if>
            <if test="warnCreateddate != null">warnCreateddate,</if>
            <if test="warnJumpurl != null">warnJumpurl,</if>
            <if test="warnRemark != null">warnRemark,</if>
         </trim>
        <trim prefix="values (" suffix=")" suffixOverrides=",">
            <if test="warnId != null">#{warnId},</if>
            <if test="warnClass != null">#{warnClass},</if>
            <if test="warnType != null">#{warnType},</if>
            <if test="warnCreateddate != null">#{warnCreateddate},</if>
            <if test="warnJumpurl != null">#{warnJumpurl},</if>
            <if test="warnRemark != null">#{warnRemark},</if>
         </trim>
    </insert>

    <update id="updateSysWarn" parameterType="SysWarn">
        update SysWarn
        <trim prefix="SET" suffixOverrides=",">
            <if test="warnClass != null">warnClass = #{warnClass},</if>
            <if test="warnType != null">warnType = #{warnType},</if>
            <if test="warnCreateddate != null">warnCreateddate = #{warnCreateddate},</if>
            <if test="warnJumpurl != null">warnJumpurl = #{warnJumpurl},</if>
            <if test="warnRemark != null">warnRemark = #{warnRemark},</if>
        </trim>
        where warnId = #{warnId}
    </update>

    <delete id="deleteSysWarnByWarnId" parameterType="String">
        delete from SysWarn where warnId = #{warnId}
    </delete>

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