<?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.ktg.iscs.mapper.IsExceptionMapper">

    <select id="getIsExceptionPage" resultType="com.ktg.iscs.domain.IsException">

        select a.* from (
        SELECT
        e.*,
        d.dict_label AS exception_category_name,
        CASE WHEN e.exception_category = 1 THEN l.loto_name WHEN e.exception_category = 2 THEN m.cabinet_name END AS
        source_name,
        dd.dict_label AS exception_type_name,
        ddd.dict_label AS exception_level_name,
        u.nick_name AS raiser_name
        FROM
        is_exception e
        LEFT JOIN sys_dict_data d ON e.exception_category = d.dict_value AND d.dict_type =
        'classification_of_exceptions'
        LEFT JOIN is_loto_station l ON l.loto_id = e.source_id
        LEFT JOIN is_materials_cabinet m ON m.cabinet_id = e.source_id
        LEFT JOIN sys_dict_data dd ON e.exception_type = dd.dict_value AND dd.dict_type = 'type_of_exception'
        LEFT JOIN sys_dict_data ddd ON e.exception_level = ddd.dict_value AND ddd.dict_type = 'severity_level'
        LEFT JOIN sys_user u ON u.user_id = e.raiser
        <where>
            <if test="isException.exceptionCategory != null and isException.exceptionCategory.trim != ''">
                and e.exception_category = #{isException.exceptionCategory}
            </if>
            <if test="isException.exceptionType != null and isException.exceptionType.trim != ''">
                and e.exception_type = #{isException.exceptionType}
            </if>
            <if test="isException.exceptionLevel != null and isException.exceptionLevel.trim != ''">
                and e.exception_level = #{isException.exceptionLevel}
            </if>
            <if test="isException.raiserName != null and isException.raiserName.trim != ''">
                and u.nick_name like concat('%',#{isException.raiserName},'%')
            </if>
            <if test="isException.startRaiseTime != null and isException.startRaiseTime.trim != ''">
                and e.raise_time &gt;= #{isException.startRaiseTime}
            </if>
            <if test="isException.endRaiseTime != null and isException.endRaiseTime.trim != ''">
                and e.raise_time &lt;= #{isException.endRaiseTime}
            </if>
            <if test="isException.startHandleTime != null and isException.startHandleTime.trim != ''">
                and e.handle_time &gt;= #{isException.startHandleTime}
            </if>
            <if test="isException.endHandleTime != null and isException.endHandleTime.trim != ''">
                and e.handle_time &lt;= #{isException.endHandleTime}
            </if>
            <if test="isException.status != null and isException.status.trim != ''">
                and e.status = #{isException.status}
            </if>
        </where>
        ) a
        <where>
            <if test="isException.sourceName != null and isException.sourceName.trim != ''">
                and a.source_name like concat('%',#{isException.sourceName},'%')
            </if>
        </where>
        order by a.exception_id desc
    </select>
    <select id="selectIsExceptionById" resultType="com.ktg.iscs.domain.IsException">
        SELECT
            e.*,
            d.dict_label AS exception_category_name,
            CASE WHEN e.exception_category = 1 THEN l.loto_name WHEN e.exception_category = 2 THEN m.cabinet_name END AS source_name,
            dd.dict_label AS exception_type_name,
            ddd.dict_label AS exception_level_name,
            u.nick_name AS raiser_name
        FROM
            is_exception e
                LEFT JOIN sys_dict_data d ON e.exception_category = d.dict_value AND d.dict_type = 'classification_of_exceptions'
                LEFT JOIN is_loto_station l ON l.loto_id = e.source_id
                LEFT JOIN is_materials_cabinet m ON m.cabinet_id = e.source_id
                LEFT JOIN sys_dict_data dd ON e.exception_type = dd.dict_value AND dd.dict_type = 'type_of_exception'
                LEFT JOIN sys_dict_data ddd ON e.exception_level = ddd.dict_value AND ddd.dict_type = 'severity_level'
                LEFT JOIN sys_user u ON u.user_id = e.raiser
        where e.exception_id = #{exceptionId}
    </select>
</mapper>
