<?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.inspect.metadata.mqtt.mapper.HrWarnMapper">
    <resultMap id="BaseResultMap" type="com.inspect.metadata.mqtt.domain.HrWarn">
        <id column="warn_id" property="warnId"/>
        <result column="nest_code" property="nestCode"/>
        <result column="type" property="type"/>
        <result column="err_code" property="errCode"/>
        <result column="err_msg" property="errMsg"/>
        <result column="err_grade" property="errGrade"/>
        <result column="create_time" property="createTime"/>
        <result column="update_time" property="updateTime"/>
    </resultMap>

    <sql id="selectHrWarnVo">
        select warn_id,
               nest_code,
               type,
               err_code,
               err_msg,
               err_grade,
               create_time,
               update_time
        from basedata_hr_warn
    </sql>
    <update id="updateWarn">
        update basedata_hr_warn
        <set>
            <if test="nestCode != null">nest_code = #{nestCode},</if>
            <if test="type != null">type = #{type},</if>
            <if test="errCode != null">err_code = #{errCode},</if>
            <if test="errMsg != null">err_msg = #{errMsg},</if>
            <if test="errGrade != null">err_grade = #{errGrade},</if>
        </set>
        where warn_id = #{warnId}
    </update>

    <select id="selectWarnList" parameterType="com.inspect.metadata.mqtt.domain.HrWarn"
            resultMap="BaseResultMap">
        <include refid="selectHrWarnVo"/>
        <where>
            <if test="nestCode != null ">and nest_code = #{nestCode}</if>
            <if test="beginTime != null and beginTime != ''"><!-- 开始时间检索 -->
                and date_format(update_time,'%y%m%d') &gt;= date_format(#{beginTime},'%y%m%d')
            </if>
            <if test="endTime != null and endTime != ''"><!-- 结束时间检索 -->
                and date_format(update_time,'%y%m%d') &lt;= date_format(#{endTime},'%y%m%d')
            </if>
        </where>
        order by update_time desc
    </select>
    <select id="countWarnThreeDay" resultType="java.lang.Integer">
        select count(1) from basedata_hr_warn
          <where>
              <if test="type != null ">and type = #{type}</if>
              <if test="nestCode != null ">and nest_code = #{nestCode}</if>
          </where>
        and update_time >= DATE_SUB(CURDATE(),INTERVAL 3 DAY)
    </select>

    <select id="selectWarn" resultType="com.inspect.metadata.mqtt.domain.HrWarn">
        <include refid="selectHrWarnVo"/>
        <where>
            <if test="nestCode != null">and nest_code = #{nestCode}</if>
            <if test="errMsg != null">and err_msg = #{errMsg}</if>
            <if test="type != null">and type = #{type}</if>
        </where>
        order by update_time desc
        limit 1
    </select>

    <insert id="insertWarn">
        insert into basedata_hr_warn(nest_code, type, err_code, err_msg, err_grade, create_time)
        values (#{nestCode}, #{type}, #{errCode}, #{errMsg}, #{errGrade}, sysdate())
    </insert>

</mapper>