<?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.yushu.system.mapper.SysErrorLogMapper">
    
    <resultMap type="SysErrorLog" id="SysErrorLogResult">
        <id     property="errorId"       column="error_id"       />
        <result property="errorCode"     column="error_code"     />
        <result property="errorType"     column="error_type"     />
        <result property="errorLevel"    column="error_level"    />
        <result property="errorMessage"  column="error_message"  />
        <result property="errorDetail"   column="error_detail"   />
        <result property="requestUrl"    column="request_url"    />
        <result property="requestMethod" column="request_method" />
        <result property="requestParams" column="request_params" />
        <result property="requestIp"     column="request_ip"     />
        <result property="userAgent"     column="user_agent"     />
        <result property="userId"        column="user_id"        />
        <result property="userName"      column="user_name"      />
        <result property="sourceClass"   column="source_class"   />
        <result property="sourceMethod"  column="source_method"  />
        <result property="sourceLine"    column="source_line"    />
        <result property="status"        column="status"         />
        <result property="resolveBy"     column="resolve_by"     />
        <result property="resolveTime"   column="resolve_time"   />
        <result property="resolveRemark" column="resolve_remark" />
        <result property="occurCount"    column="occur_count"    />
        <result property="firstTime"     column="first_time"     />
        <result property="lastTime"      column="last_time"      />
        <result property="createTime"    column="create_time"    />
    </resultMap>

    <sql id="selectErrorLogVo">
        select error_id, error_code, error_type, error_level, error_message, error_detail,
               request_url, request_method, request_params, request_ip, user_agent,
               user_id, user_name, source_class, source_method, source_line,
               status, resolve_by, resolve_time, resolve_remark,
               occur_count, first_time, last_time, create_time
        from sys_error_log
    </sql>

    <select id="selectErrorLogById" parameterType="Long" resultMap="SysErrorLogResult">
        <include refid="selectErrorLogVo"/>
        where error_id = #{errorId}
    </select>

    <select id="selectErrorLogList" parameterType="SysErrorLog" resultMap="SysErrorLogResult">
        <include refid="selectErrorLogVo"/>
        <where>
            <if test="errorCode != null and errorCode != ''">
                AND error_code like concat('%', #{errorCode}, '%')
            </if>
            <if test="errorType != null and errorType != ''">
                AND error_type = #{errorType}
            </if>
            <if test="errorLevel != null and errorLevel != ''">
                AND error_level = #{errorLevel}
            </if>
            <if test="errorMessage != null and errorMessage != ''">
                AND error_message like concat('%', #{errorMessage}, '%')
            </if>
            <if test="requestUrl != null and requestUrl != ''">
                AND request_url like concat('%', #{requestUrl}, '%')
            </if>
            <if test="userName != null and userName != ''">
                AND user_name like concat('%', #{userName}, '%')
            </if>
            <if test="status != null and status != ''">
                AND status = #{status}
            </if>
            <if test="params.beginTime != null and params.beginTime != ''">
                AND create_time &gt;= #{params.beginTime}
            </if>
            <if test="params.endTime != null and params.endTime != ''">
                AND create_time &lt;= #{params.endTime}
            </if>
        </where>
        order by create_time desc
    </select>

    <select id="countUnresolved" resultType="int">
        select count(*) from sys_error_log where status = '0'
    </select>

    <select id="selectBySignature" resultMap="SysErrorLogResult">
        <include refid="selectErrorLogVo"/>
        where error_code = #{param1} 
          and source_class = #{param2} 
          and source_method = #{param3}
          and status = '0'
        limit 1
    </select>

    <insert id="insertErrorLog" parameterType="SysErrorLog" useGeneratedKeys="true" keyProperty="errorId">
        insert into sys_error_log (
            error_code, error_type, error_level, error_message, error_detail,
            request_url, request_method, request_params, request_ip, user_agent,
            user_id, user_name, source_class, source_method, source_line,
            status, occur_count, first_time, last_time, create_time
        ) values (
            #{errorCode}, #{errorType}, #{errorLevel}, #{errorMessage}, #{errorDetail},
            #{requestUrl}, #{requestMethod}, #{requestParams}, #{requestIp}, #{userAgent},
            #{userId}, #{userName}, #{sourceClass}, #{sourceMethod}, #{sourceLine},
            '0', 1, sysdate(), sysdate(), sysdate()
        )
    </insert>

    <update id="updateErrorLog" parameterType="SysErrorLog">
        update sys_error_log
        <set>
            <if test="status != null">status = #{status},</if>
            <if test="resolveBy != null">resolve_by = #{resolveBy},</if>
            <if test="resolveTime != null">resolve_time = #{resolveTime},</if>
            <if test="resolveRemark != null">resolve_remark = #{resolveRemark},</if>
            <if test="occurCount != null">occur_count = #{occurCount},</if>
            <if test="lastTime != null">last_time = #{lastTime},</if>
        </set>
        where error_id = #{errorId}
    </update>

    <delete id="deleteErrorLogById" parameterType="Long">
        delete from sys_error_log where error_id = #{errorId}
    </delete>

    <delete id="deleteErrorLogByIds" parameterType="Long">
        delete from sys_error_log where error_id in
        <foreach collection="array" item="errorId" open="(" separator="," close=")">
            #{errorId}
        </foreach>
    </delete>

    <delete id="cleanErrorLog">
        truncate table sys_error_log
    </delete>
</mapper>
