<?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.project.system.mapper.TAlarmhistoryMapper">
    
    <resultMap type="TAlarmhistory" id="TAlarmhistoryResult">
        <result property="id"    column="id"    />
        <result property="plantCode"    column="plant_code"    />
        <result property="typeId"    column="type_id"    />
        <result property="tableName"    column="table_name"    />
        <result property="staffid"    column="staffid"    />
        <result property="typeName"    column="tableName"    />
        <result property="fieldName"    column="field_name"    />
        <result property="principalName"    column="name"    />
        <result property="principalEmail"    column="mail"    />
        <result property="alarmLevel"    column="alarm_level"    />
        <result property="alarmContent"    column="alarm_content"    />
        <result property="sendTime"    column="send_time"    />
        <result property="delFlag"    column="del_flag"    />
        <result property="createrCode"    column="creater_code"    />
        <result property="createdate"    column="createdate"    />
        <result property="updaterCode"    column="updater_code"    />
        <result property="updatedate"    column="updatedate"    />
        <result property="deptId"    column="dept_id"    />
        <result property="deptName" column="dept_name" />
        <result property="startDate"    column="send_time"    />
        <result property="endDate"    column="send_time"    />
        <result property="alarmNum"    column="alarm_num"    />
    </resultMap>

    <sql id="selectTAlarmhistoryVo">
        select d.id, d.plant_code, d.type_id, d.table_name, a.table_name as tableName, a.field_name, d.staffid, t.name, t.mail, d.alarm_level, d.alarm_content, d.alarm_num, d.send_time, d.del_flag, d.creater_code, d.createdate, d.updater_code, d.updatedate, d.dept_id ,s.dept_name from t_alarmhistory d
      left join sys_dept s on s.dept_id = d.dept_id
      left join t_staffmgr t on t.staffid = d.staffid
      left join t_alarmtype a on a.id = d.type_id
    </sql>

    <sql id="selectTAlarmhistoryHome">
        select distinct d.plant_code, d.type_id, d.table_name, a.table_name as tableName, a.field_name, d.alarm_level, d.alarm_num, d.dept_id ,s.dept_name from t_alarmhistory d
      left join sys_dept s on s.dept_id = d.dept_id
      left join t_staffmgr t on t.staffid = d.staffid
      left join t_alarmtype a on a.id = d.type_id
    </sql>

    <select id="selectTAlarmhistoryList" parameterType="TAlarmhistory" resultMap="TAlarmhistoryResult">
        <include refid="selectTAlarmhistoryVo"/>
        <where>  
            <if test="typeName != null "> and a.table_name like concat(concat('%', #{typeName}), '%')</if>
            <if test="alarmLevel != null  and alarmLevel != ''"> and alarm_level = #{alarmLevel}</if>
            <if test="startDate != null ">and send_time <![CDATA[>=]]> #{startDate}</if>
            <if test="endDate != null">and send_time <![CDATA[<=]]>#{endDate}</if>
            and d.del_flag = 0
        </where>
        <!-- 数据范围过滤 -->
        ${params.dataScope}
        order by d.id desc
    </select>

    <select id="selectHomeList" parameterType="TAlarmhistory" resultMap="TAlarmhistoryResult">
        <include refid="selectTAlarmhistoryHome"/>
        <where>
            <if test="isHome != null "> and send_time >= to_date(#{homeTime}, 'yyyy-mm-dd') and send_time &lt; to_date(#{homeTimeNight}, 'yyyy-mm-dd HH24:MI:SS')</if>
            and d.del_flag = 0
        </where>
        <!-- 数据范围过滤 -->
        ${params.dataScope}
    </select>
    
    <select id="selectTAlarmhistoryById" parameterType="Long" resultMap="TAlarmhistoryResult">
        <include refid="selectTAlarmhistoryVo"/>
        where d.id = #{id}
    </select>
        
    <insert id="insertTAlarmhistory" parameterType="TAlarmhistory">
        <selectKey keyProperty="id" resultType="long" order="BEFORE">
            SELECT seq_t_alarmhistory.NEXTVAL as id FROM DUAL
        </selectKey>
        insert into t_alarmhistory
        <trim prefix="(" suffix=")" suffixOverrides=",">
            <if test="id != null">id,</if>
            <if test="plantCode != null and plantCode != ''">plant_code,</if>
            <if test="typeId != null">type_id,</if>
            <if test="tableName != null">table_name,</if>
            <if test="staffid != null and staffid != ''">staffid,</if>
            <if test="alarmLevel != null">alarm_level,</if>
            <if test="alarmContent != null">alarm_content,</if>
            <if test="sendTime != null">send_time,</if>
            <if test="delFlag != null">del_flag,</if>
            <if test="createrCode != null">creater_code,</if>
            <if test="createdate != null">createdate,</if>
            <if test="updaterCode != null">updater_code,</if>
            <if test="updatedate != null">updatedate,</if>
            <if test="deptId != null">dept_id,</if>
            <if test="alarmNum != null">alarm_num,</if>
         </trim>
        <trim prefix="values (" suffix=")" suffixOverrides=",">
            <if test="id != null">#{id},</if>
            <if test="plantCode != null and plantCode != ''">#{plantCode},</if>
            <if test="typeId != null">#{typeId},</if>
            <if test="tableName != null">#{tableName},</if>
            <if test="staffid != null and staffid != ''">#{staffid},</if>
            <if test="alarmLevel != null">#{alarmLevel},</if>
            <if test="alarmContent != null">#{alarmContent},</if>
            <if test="sendTime != null">#{sendTime},</if>
            <if test="delFlag != null">#{delFlag},</if>
            <if test="createrCode != null">#{createrCode},</if>
            <if test="createdate != null">#{createdate},</if>
            <if test="updaterCode != null">#{updaterCode},</if>
            <if test="updatedate != null">#{updatedate},</if>
            <if test="deptId != null">#{deptId},</if>
            <if test="alarmNum != null">#{alarmNum},</if>
         </trim>
    </insert>

    <update id="updateTAlarmhistory" parameterType="TAlarmhistory">
        update t_alarmhistory
        <trim prefix="SET" suffixOverrides=",">
            <if test="plantCode != null and plantCode != ''">plant_code = #{plantCode},</if>
            <if test="typeId != null">type_id = #{typeId},</if>
            <if test="staffid != null and staffid != ''">staffid = #{staffid},</if>
            <if test="alarmLevel != null">alarm_level = #{alarmLevel},</if>
            <if test="alarmContent != null">alarm_content = #{alarmContent},</if>
            <if test="sendTime != null">send_time = #{sendTime},</if>
            <if test="delFlag != null">del_flag = #{delFlag},</if>
            <if test="createrCode != null">creater_code = #{createrCode},</if>
            <if test="createdate != null">createdate = #{createdate},</if>
            <if test="updaterCode != null">updater_code = #{updaterCode},</if>
            <if test="updatedate != null">updatedate = #{updatedate},</if>
            <if test="deptId != null">dept_id = #{deptId},</if>
            <if test="alarmNum != null">alarm_num = #{alarmNum},</if>
        </trim>
        where id = #{id}
    </update>

    <update id="deleteTAlarmhistoryById" parameterType="Long">
        update t_alarmhistory set del_flag = 2 where id = #{id}
    </update>

    <update id="deleteTAlarmhistoryByIds" parameterType="String">
        update t_alarmhistory set del_flag = 2 where id in
        <foreach item="id" collection="array" open="(" separator="," close=")">
            #{id}
        </foreach>
    </update>
    
</mapper>