<?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.cm.central.control.dao.clientexception.IClientExceptionDao">

    <resultMap id="clientExceptionDTO" type="com.cm.central.control.pojo.dtos.clientexception.ClientExceptionDTO">
        <id column="client_exception_id" property="clientExceptionId"/>
        <result column="client_id" property="clientId"/>
        <result column="client_client_name" property="clientClientName"/>
        <result column="client_client_version" property="clientClientVersion"/>
        <result column="client_server_ip" property="clientServerIp"/>
        <result column="exception_type" property="exceptionType"/>
        <result column="exception_name" property="exceptionName"/>
        <result column="exception_summary" property="exceptionSummary"/>
        <result column="is_notice_project_leader" property="isNoticeProjectLeader"/>
        <result column="is_handle" property="isHandle"/>
        <result column="gmt_create" property="gmtCreate"/>
    </resultMap>

    <!-- 新增客户端异常 -->
    <insert id="saveClientException" parameterType="map">
        INSERT INTO central_client_exception(
            client_exception_id,
            client_id,
            exception_type,
            exception_name,
            exception_summary,
            is_notice_project_leader,
            is_handle,
            creator,
            gmt_create,
            modifier,
            gmt_modified,
            is_delete
        ) VALUES(
            #{clientExceptionId},
            #{clientId},
            #{exceptionType},
            #{exceptionName},
            #{exceptionSummary},
            #{isNoticeProjectLeader},
            #{isHandle},
            #{creator},
            #{gmtCreate},
            #{modifier},
            #{gmtModified},
            #{isDelete}
        )
    </insert>

    <!-- 删除客户端异常 -->
    <update id="removeClientException" parameterType="map">
        UPDATE
            central_client_exception
        SET
            is_delete = 1,
            modifier = #{modifier},
            gmt_modified = #{gmtModified}
        WHERE
            client_exception_id IN
        <foreach collection="clientExceptionIds" index="index" open="(" separator="," close=")">
            #{clientExceptionIds[${index}]}
        </foreach>
    </update>

    <!-- 删除客户端异常（物理） -->
    <update id="deleteClientException" parameterType="map">
        DELETE FROM
            central_client_exception
        WHERE
            client_exception_id IN
        <foreach collection="clientExceptionIds" index="index" open="(" separator="," close=")">
            #{clientExceptionIds[${index}]}
        </foreach>
    </update>

    <!-- 修改客户端异常 -->
    <update id="updateClientException" parameterType="map">
        UPDATE
            central_client_exception
        SET
        <if test="clientId != null and clientId != ''">
            client_id = #{clientId},
        </if>
        <if test="exceptionType != null and exceptionType != ''">
            exception_type = #{exceptionType},
        </if>
        <if test="exceptionName != null and exceptionName != ''">
            exception_name = #{exceptionName},
        </if>
        <if test="exceptionSummary != null and exceptionSummary != ''">
            exception_summary = #{exceptionSummary},
        </if>
        <if test="isNoticeProjectLeader != null">
            is_notice_project_leader = #{isNoticeProjectLeader},
        </if>
        <if test="isHandle != null">
            is_handle = #{isHandle},
        </if>
            modifier = #{modifier},
            gmt_modified = #{gmtModified}
        WHERE
            client_exception_id = #{clientExceptionId}
    </update>

    <!-- 更新通知状态 -->
    <update id="updateNoticeStatusByClientExceptionIds" parameterType="map">
        UPDATE
            central_client_exception
        SET
            is_notice_project_leader = #{isNoticeProjectLeader},
            modifier = #{modifier},
            gmt_modified = #{gmtModified}
        WHERE
            client_exception_id IN
        <foreach collection="clientExceptionIds" index="index" open="(" separator="," close=")">
            #{clientExceptionIds[${index}]}
        </foreach>
    </update>

    <!-- 客户端异常详情 -->
    <select id="getClientException" parameterType="map" resultMap="clientExceptionDTO">
        SELECT
            t1.client_id,
            t1.exception_type,
            t1.exception_name,
            t1.exception_summary,
            t1.is_notice_project_leader,
            t1.is_handle,
            LEFT(t1.gmt_create, 19) gmt_create,
            t1.client_exception_id
        FROM
            central_client_exception t1
        WHERE
            t1.is_delete = 0
        <if test="clientExceptionId != null and clientExceptionId != ''">
            AND
            t1.client_exception_id = #{clientExceptionId}
        </if>
    </select>

    <!-- 客户端异常列表 -->
    <select id="listClientException" parameterType="map" resultMap="clientExceptionDTO">
        SELECT
            jt1.client_name client_client_name,
            jt1.client_version client_client_version,
            jt1.customer_id client_customer_id,
            jt1.server_ip client_server_ip,
            t1.exception_type,
            t1.exception_name,
            t1.is_notice_project_leader,
            t1.is_handle,
            LEFT(t1.gmt_create, 19) gmt_create,
            t1.client_exception_id
        FROM
            central_client_exception t1
        LEFT JOIN
            central_client jt1
        ON
            t1.client_id = jt1.client_id
        AND
            jt1.is_delete = 0
        WHERE
            t1.is_delete = 0
        <if test="keywords != null and keywords != ''">
        AND (
            t1.client_name LIKE CONCAT('%', #{keywords}, '%')
            OR
            t1.exception_name LIKE CONCAT('%', #{keywords}, '%')
        )
        </if>
        <if test="startTime != null and startTime != ''">
        AND
            LEFT(t1.gmt_create, 10) <![CDATA[ >= ]]> #{startTime}
        </if>
        <if test="endTime != null and endTime != ''">
        AND
            LEFT(t1.gmt_create, 10) <![CDATA[ <= ]]> #{endTime}
        </if>
        <if test="isNoticeProjectLeader != null">
        AND
            t1.is_notice_project_leader = #{isNoticeProjectLeader}
        </if>
        <if test="clientIds != null and clientIds.size > 0">
        AND
            t1.client_id IN
            <foreach collection="clientIds" index="index" open="(" separator="," close=")">
                #{clientIds[${index}]}
            </foreach>
        </if>
        <if test="clientExceptionIds != null and clientExceptionIds.size > 0">
        AND
            t1.client_exception_id IN
            <foreach collection="clientExceptionIds" index="index" open="(" separator="," close=")">
                #{clientExceptionIds[${index}]}
            </foreach>
        </if>
        ORDER BY
            t1.gmt_create DESC
    </select>

    <!-- 客户端异常统计  -->
    <select id="countClientException" parameterType="map" resultType="Integer">
        SELECT
            COUNT(*)
        FROM
            central_client_exception t1
        WHERE
            t1.is_delete = 0
    </select>

</mapper>