<?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.sckw.slope.detection.dao.mysql.KwsAlarmMapper">
    <resultMap id="BaseResultMap" type="com.sckw.slope.detection.model.dos.mysql.KwsAlarm">
        <!--@mbg.generated-->
        <!--@Table kws_alarm-->
        <id column="id" jdbcType="BIGINT" property="id"/>
        <result column="title" jdbcType="VARCHAR" property="title"/>
        <result column="company_id" jdbcType="VARCHAR" property="companyId"/>
        <result column="mountain_id" jdbcType="VARCHAR" property="mountainId"/>
        <result column="project_id" jdbcType="VARCHAR" property="projectId"/>
        <result column="device_id" jdbcType="VARCHAR" property="deviceId"/>
        <result column="level" jdbcType="INTEGER" property="level"/>
        <result column="trigger_times" jdbcType="INTEGER" property="triggerTimes"/>
        <result column="type" jdbcType="INTEGER" property="type"/>
        <result column="create_time" jdbcType="TIMESTAMP" property="createTime"/>
        <result column="update_time" jdbcType="TIMESTAMP" property="updateTime"/>
        <result column="status" jdbcType="TINYINT" property="status"/>
        <result column="is_history" jdbcType="TINYINT" property="isHistory"/>
        <result column="del_flag" jdbcType="TINYINT" property="delFlag"/>
    </resultMap>
    <sql id="Base_Column_List">
        <!--@mbg.generated-->
        id,
        title,
        `level`,
        `type`,
        create_time,
        `status`,
        company_id,
        mountain_id,
        project_id,
        update_time,
        device_id,
        trigger_times,
        is_history,
        del_flag
    </sql>
    <select id="selectByPrimaryKey" parameterType="java.lang.Long" resultMap="BaseResultMap">
        <!--@mbg.generated-->
        select
        <include refid="Base_Column_List"/>
        from kws_alarm
        where id = #{id,jdbcType=BIGINT}
    </select>

    <select id="selectByDevicesId" parameterType="java.lang.String" resultType="java.util.Map">
        <!--@mbg.generated-->
        select count(d.id) as count,a.device_id,type
        from kws_alarm as a inner join kws_alarm_detail as d
        on a.id=d.alarm_id
        where a.type = 1
        and a.del_flag = 0
        <if test="devicesId != null and devicesId.size() > 0">
            AND device_id IN
            <foreach collection="devicesId" open="(" close=")" item="item" separator=",">
                #{item}
            </foreach>
            AND d.pid IN
            <foreach collection="devicesId" open="(" close=")" item="item" separator=",">
                #{item}
            </foreach>
        </if>
        group by device_id, type
    </select>

    <select id="selectDataByDevicesId" parameterType="java.lang.String" resultType="java.util.Map">
        <!--@mbg.generated-->
        select sum(trigger_times) as count,a.device_id,type
        from kws_alarm as a
        where a.del_flag = 0
        <if test="devicesId != null and devicesId.size() > 0">
            AND device_id IN
            <foreach collection="devicesId" open="(" close=")" item="item" separator=",">
                #{item}
            </foreach>
        </if>
        group by device_id
    </select>

    <delete id="deleteByPrimaryKey" parameterType="java.lang.Long">
        <!--@mbg.generated-->
        delete
        from kws_alarm
        where id = #{id,jdbcType=BIGINT}
    </delete>
    <insert id="insertSelective" parameterType="com.sckw.slope.detection.model.dos.mysql.KwsAlarm">
        <!--@mbg.generated-->
        insert into kws_alarm
        <trim prefix="(" suffix=")" suffixOverrides=",">
            <if test="id != null">
                id,
            </if>
            <if test="pid != null">
                pid,
            </if>
            <if test="title != null">
                title,
            </if>
            <if test="val != null">
                val,
            </if>
            <if test="content != null">
                content,
            </if>
            <if test="level != null">
                `level`,
            </if>
            <if test="type != null">
                `type`,
            </if>
            <if test="lat != null">
                lat,
            </if>
            <if test="lng != null">
                lng,
            </if>
            <if test="alt != null">
                alt,
            </if>
            <if test="createTime != null">
                create_time,
            </if>
            <if test="triggerTimes != null">
                trigger_times,
            </if>
            <if test="status != null">
                `status`,
            </if>
        </trim>
        <trim prefix="values (" suffix=")" suffixOverrides=",">
            <if test="id != null">
                #{id,jdbcType=BIGINT},
            </if>
            <if test="pid != null">
                #{pid,jdbcType=BIGINT},
            </if>
            <if test="title != null">
                #{title,jdbcType=VARCHAR},
            </if>
            <if test="val != null">
                #{val,jdbcType=VARCHAR},
            </if>
            <if test="content != null">
                #{content,jdbcType=VARCHAR},
            </if>
            <if test="level != null">
                #{level,jdbcType=INTEGER},
            </if>
            <if test="type != null">
                #{type,jdbcType=INTEGER},
            </if>
            <if test="lat != null">
                #{lat,jdbcType=VARCHAR},
            </if>
            <if test="lng != null">
                #{lng,jdbcType=VARCHAR},
            </if>
            <if test="alt != null">
                #{alt,jdbcType=VARCHAR},
            </if>
            <if test="createTime != null">
                #{createTime,jdbcType=TIMESTAMP},
            </if>
            <if test="triggerTimes != null">
                #{triggerTimes,jdbcType=INTEGER},
            </if>
            <if test="status != null">
                #{status,jdbcType=TINYINT},
            </if>
        </trim>
    </insert>
    <update id="updateByPrimaryKeySelective" parameterType="com.sckw.slope.detection.model.dos.mysql.KwsAlarm">
        <!--@mbg.generated-->
        update kws_alarm
        <set>
            <if test="pid != null">
                pid = #{pid,jdbcType=BIGINT},
            </if>
            <if test="title != null">
                title = #{title,jdbcType=VARCHAR},
            </if>
            <if test="val != null">
                val = #{val,jdbcType=VARCHAR},
            </if>
            <if test="content != null">
                content = #{content,jdbcType=VARCHAR},
            </if>
            <if test="level != null">
                `level` = #{level,jdbcType=INTEGER},
            </if>
            <if test="type != null">
                `type` = #{type,jdbcType=INTEGER},
            </if>
            <if test="lat != null">
                lat = #{lat,jdbcType=VARCHAR},
            </if>
            <if test="lng != null">
                lng = #{lng,jdbcType=VARCHAR},
            </if>
            <if test="alt != null">
                alt = #{alt,jdbcType=VARCHAR},
            </if>
            <if test="createTime != null">
                create_time = #{createTime,jdbcType=TIMESTAMP},
            </if>
            <if test="triggerTimes != null">
                trigger_times = #{triggerTimes,jdbcType=INTEGER},
            </if>
            <if test="status != null">
                `status` = #{status,jdbcType=TINYINT},
            </if>
        </set>
        where id = #{id,jdbcType=BIGINT}
    </update>
    <update id="updateByPrimaryKey" parameterType="com.sckw.slope.detection.model.dos.mysql.KwsAlarm">
        <!--@mbg.generated-->
        update kws_alarm
        set pid           = #{pid,jdbcType=BIGINT},
            title         = #{title,jdbcType=VARCHAR},
            val           = #{val,jdbcType=VARCHAR},
            content       = #{content,jdbcType=VARCHAR},
            `level`       = #{level,jdbcType=INTEGER},
            `type`        = #{type,jdbcType=INTEGER},
            lat           = #{lat,jdbcType=VARCHAR},
            lng           = #{lng,jdbcType=VARCHAR},
            alt           = #{alt,jdbcType=VARCHAR},
            create_time   = #{createTime,jdbcType=TIMESTAMP},
            trigger_times = #{triggerTimes,jdbcType=INTEGER},
            `status`      = #{status,jdbcType=TINYINT}
        where id = #{id,jdbcType=BIGINT}
    </update>

    <select id="getDataAlarmToDay" resultType="java.util.Map">
        SELECT DATE_FORMAT(h.h, '%Y-%m-%d %H:00:00') AS dateTime, COUNT(t.id) AS number
        FROM (SELECT #{startTime} + INTERVAL n HOUR AS h
              FROM (
                  SELECT 0 AS n
                  UNION ALL SELECT 1 UNION ALL SELECT 2 UNION ALL SELECT 3 UNION ALL SELECT 4 UNION ALL SELECT 5
                  UNION ALL SELECT 6 UNION ALL SELECT 7 UNION ALL SELECT 8 UNION ALL SELECT 9 UNION ALL SELECT 10
                  UNION ALL SELECT 11 UNION ALL SELECT 12 UNION ALL SELECT 13 UNION ALL SELECT 14 UNION ALL SELECT 15
                  UNION ALL SELECT 16 UNION ALL SELECT 17 UNION ALL SELECT 18 UNION ALL SELECT 19 UNION ALL SELECT 20
                  UNION ALL SELECT 21 UNION ALL SELECT 22 UNION ALL SELECT 23
                  ) hours) h
            LEFT JOIN
        (SELECT a.id,
                a.create_time,
                b.project_id  as projectId,
                b.company_id  as companyId,
                b.mountain_id as mountainId
         FROM kws_alarm_detail a
                  LEFT JOIN kws_alarm b ON a.alarm_id = b.id and a.del_flag = 0
        <where>
            <if test="type != null and type != ''">
                and b.type = #{type}
            </if>
            <if test="mountainId != null and mountainId != ''">
                and b.mountain_id = #{mountainId}
            </if>
            <if test="level != null and level != ''">
                and b.level = #{level}
            </if>
        </where>
        ) t
        ON DATE_FORMAT(t.create_time, '%Y-%m-%d %H') = DATE_FORMAT(h.h, '%Y-%m-%d %H')
        WHERE h.h BETWEEN #{startTime}
                  AND #{endTime}
        GROUP BY dateTime
        ORDER BY dateTime ASC;
    </select>

    <select id="getDataAlarmToWeek" resultType="java.util.Map">
        SELECT a.date as dateTime, COALESCE(b.number, 0) as number
        from (select date_add(#{dateTimeFormat}, interval row1 DAY) date
              from
                  (
                  SELECT @row := @row + 1 as row1 FROM
                  (select 0 union all select 1 union all select 2 union all select 3 union all select 4 union all select 5 union
                  all select 6 union all select 7 union all select 8 union all select 9) t, (select 0 union all select 1 union all
                  select 2 union all select 3 union all select 4 union all select 5 union all select 6 union all select 7 union
                  all select 8 union all select 9) t2, (select 0 union all select 1 union all select 2 union all select 3 union
                  all select 4 union all select 5 union all select 6 union all select 7 union all select 8 union all select 9) t3, (select 0 union all select 1 union all select 2 union all select 3 union all select 4 union all select 5 union
                  all select 6 union all select 7 union all select 8 union all select 9) t4, (SELECT @row := -1) r
                  ) se
              where date_add(#{startTime}
                  , interval row1 DAY) &lt;= #{endTime}) a
            LEFT JOIN
        (SELECT count(b.id)   as                       number,
                DATE_FORMAT(b.create_time, '%Y-%m-%d') date_time,
                a.project_id  as                       projectId,
                a.company_id  as                       companyId,
                a.mountain_id as                       mountainId
         FROM kws_alarm a
                  LEFT JOIN
              kws_alarm_detail b on a.id = b.alarm_id and b.del_flag = 0
        <where>
            <if test="type != null and type != ''">
                and a.type = #{type}
            </if>
            <if test="mountainId != null and mountainId != ''">
                and a.mountain_id = #{mountainId}
            </if>
            <if test="projectId != null and projectId != ''">
                and a.project_id = #{projectId}
            </if>
            <if test="level != null and level != ''">
                and a.level = #{level}
            </if>
        </where>
        GROUP BY DATE_FORMAT(b.create_time, '%Y-%m-%d')) b ON a.date = b.date_time
    </select>

    <select id="getDataAlarmToYear" resultType="java.util.Map">
        SELECT a.dateTime            AS dateTime,
               COALESCE(b.number, 0) AS number
        FROM (SELECT DATE_FORMAT(DATE_ADD(#{startTime}, INTERVAL ROW1 MONTH ), '%Y-%m') dateTime
              FROM (SELECT @ROW := @ROW + 1 AS row1
                    FROM
                        (select 0 union all select 1 union all select 2 union all select 3 union all select 4 union all select 5 union all select 6 union all select 7 union all select 8 union all select 9) t, (select 0 union all select 1 union all select 2 union all select 3 union all select 4 union all select 5 union all select 6 union all select 7 union all select 8 union all select 9) t2, (select 0 union all select 1 union all select 2 union all select 3 union all select 4 union all select 5 union all select 6 union all select 7 union all select 8 union all select 9) t3, (select 0 union all select 1 union all select 2 union all select 3 union all select 4 union all select 5 union all select 6 union all select 7 union all select 8 union all select 9) t4, ( SELECT @ROW := - 1 ) r) se
              WHERE date_add(#{startTime}, INTERVAL row1 MONTH ) &lt;= #{endTime}) a
            LEFT JOIN (SELECT count(b.id) AS                      number,
                              DATE_FORMAT(b.create_time, '%Y-%m') dateTime
                       FROM kws_alarm a
                                LEFT JOIN kws_alarm_detail b ON a.id = b.alarm_id and b.del_flag = 0
        <where>
            <if test="type != null and type != ''">
                and a.type = #{type}
            </if>
            <if test="mountainId != null and mountainId != ''">
                and a.mountain_id = #{mountainId}
            </if>
            <if test="projectId != null and projectId != ''">
                and a.project_id = #{projectId}
            </if>
            <if test="level != null and level != ''">
                and a.level = #{level}
            </if>
        </where>
        GROUP BY DATE_FORMAT(b.create_time, '%Y-%m')) b ON a.dateTime = b.dateTime
    </select>

    <select id="getProjectAlarmList" resultType="java.util.Map">
        SELECT project_id           as projectId,
               COUNT(DISTINCT b.id) as alarmTotal
        FROM kws_alarm a
                 LEFT JOIN kws_alarm_detail b ON a.id = b.alarm_id and b.del_flag = 0
        <where>
            <if test="type != null and type != ''">
                and a.type = #{type}
            </if>
            <if test="startTime != null and startTime != ''">
                and b.create_time &gt; #{startTime}
            </if>
            <if test="endTime != null and endTime != ''">
                and b.create_time &lt; #{endTime}
            </if>
            <if test="level != null and level != ''">
                and a.level = #{level}
            </if>
        </where>
        GROUP BY a.project_id
    </select>

    <select id="selectDetailList" resultType="com.sckw.slope.detection.model.vo.AlarmLogThresholdVO">
        SELECT a.id          AS id,
               b.project_id  AS projectId,
               b.mountain_id AS mountainId,
               b.title       AS title,
               a.val         AS val,
               b.`level`     AS LEVEL,
               a.create_time AS createTime
        FROM kws_alarm_detail a
                 LEFT JOIN kws_alarm b ON a.alarm_id = b.id
        WHERE a.mountain_id = b.mountain_id
          and b.`status` = 0
        ORDER BY a.create_time DESC
<!--        <if test="page != null and pageSize !=null ">-->
<!--            limit #{page} , #{pageSize}-->
<!--        </if>-->
    </select>

    <select id="selectDetailCount" resultType="int">
    </select>

    <select id="selectListByQuery" resultMap="BaseResultMap">
        select
        <include refid="Base_Column_List">
        </include>
        from kws_alarm
        <where>
            del_flag = 0
            and trigger_times > 0
            <if test="mountainId != null and mountainId != ''">
                and mountain_id = #{mountainId}
            </if>
            <if test="query.projectId != null and query.projectId != ''">
                and project_id = #{query.projectId}
            </if>
            <if test="query.alarmInfo != null and query.alarmInfo != ''">
                and title = #{query.alarmInfo}
            </if>
            <if test="query.status != null">
                and status = #{query.status}
            </if>
<!--            <if test="query.status != null and query.status != ''">-->
<!--                and is_history = #{query.status}-->
<!--            </if>-->
            <if test="query.alarmLevel != null and query.alarmLevel != ''">
                and level = #{query.alarmLevel}
            </if>
            <if test="query.deviceName != null and query.deviceName != ''">
                and device_id in
                    (SELECT id FROM kws_device WHERE del_flag = 0 and name like concat('%', #{query.deviceName}, '%'))
            </if>
        </where>
    </select>

    <select id="selectExportListByQuery" resultMap="BaseResultMap">
        select
        <include refid="Base_Column_List">
        </include>
        from kws_alarm
        <where>
            del_flag = 0 and trigger_times > 0
            <if test="idsList != null and idsList.size() > 0">
                and id in
                <foreach collection="idsList" item="item" open="(" close=")" separator=",">
                    #{item}
                </foreach>
            </if>
            <if test="mountainId != null and mountainId != ''">
                and mountain_id = #{mountainId}
            </if>
            <if test="query.projectId != null and query.projectId != ''">
                and project_id = #{query.projectId}
            </if>
            <if test="query.alarmInfo != null and query.alarmInfo != ''">
                and title = #{query.alarmInfo}
            </if>
            <if test="query.status != null and query.status != ''">
                and status = #{query.status}
            </if>
<!--            <if test="query.status != null and query.status != ''">-->
<!--                and is_history = #{query.status}-->
<!--            </if>-->
            <if test="query.alarmLevel != null and query.alarmLevel != ''">
                and level = #{query.alarmLevel}
            </if>
            <if test="query.deviceName != null and query.deviceName != ''">
                and device_id in
                    (SELECT id
                     FROM kws_device
                     WHERE del_flag = 0
                       and name like concat('%'
                         , #{query.deviceName}
                         , '%'))
            </if>
        </where>
    </select>
</mapper>