<?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.njcn.csharmonic.mapper.CsEventUserPOMapper">
  <resultMap id="BaseResultMap" type="com.njcn.csharmonic.pojo.po.CsEventUserPO">
    <!--@mbg.generated-->
    <!--@Table cs_event_user-->
    <result column="user_id" jdbcType="VARCHAR" property="userId" />
    <result column="event_id" jdbcType="VARCHAR" property="eventId" />
    <result column="status" jdbcType="INTEGER" property="status" />
  </resultMap>
  <sql id="Base_Column_List">
    <!--@mbg.generated-->
    device_id, user_id, event_id, `status`
  </sql>

  <select id="queryEventCount" resultType="java.lang.Integer">
    select count(DISTINCT(event_id)) from cs_event_user a where 1=1
<!--    <if test="csEventUserQueryParam!=null and csEventUserQueryParam.enginnerId != null and csEventUserQueryParam.enginnerId !=''">-->
<!--      AND   a.enginner_id =#{ csEventUserQueryParam.enginnerId}-->
<!--    </if>-->
<!--      <if test="csEventUserQueryParam!=null and csEventUserQueryParam.deviceId != null and csEventUserQueryParam.deviceId !=''">-->
<!--          AND   a.device_id =#{ csEventUserQueryParam.deviceId}-->
<!--      </if>-->
      AND a.`status`= 0
      and a.user_id=#{ csEventUserQueryParam.userId}
      and a.event_id in(select id FROM cs_event b where DATE(b.start_time) =DATE(NOW())
    )

  </select>

  <select id="queryEngineeringEventCount" resultType="com.njcn.csharmonic.pojo.dto.UnReadEventDto">
    SELECT
        t2.device_id AS deviceId,
        COUNT(DISTINCT event_id) AS count
    FROM
        cs_event_user t1 LEFT JOIN cs_event t2 ON t1.event_id = t2.id
    WHERE
      t1.`status` = 0
      AND t2.type IN (0, 1)
      AND t1.user_id = #{userIndex}
    GROUP BY t2.device_id
  </select>

  <select id="queryUserEventList" resultType="com.njcn.csharmonic.pojo.vo.EventDetailVO">
    select DISTINCT a.event_id id,
    <if test="flag">
      a.status status,
    </if>
    <if test="!flag">
      1 status,
    </if>
    b.device_id deviceId,b.line_id lineId,
    b.start_time startTime,b.tag tag ,b.wave_path wavePath,b.instant_pics,b.rms_pics , b.type type,b.code code,b.level level,b.location location
    from cs_event_user a inner join cs_event b on a.event_id=b.id inner join cs_equipment_delivery c on b.device_id=c.id where 1=1
    and b.process=c.process
    <if test="csEventUserQueryParam!=null and csEventUserQueryParam.endTime  != null and csEventUserQueryParam.endTime !=''">
      AND DATE(b.start_time) &lt;= DATE(#{csEventUserQueryParam.endTime})
    </if>
    <if test="csEventUserQueryParam!=null and csEventUserQueryParam.startTime != null and csEventUserQueryParam.startTime !=''">
      AND DATE(b.start_time) &gt;= DATE(#{csEventUserQueryParam.startTime})
    </if>
    <if test="csEventUserQueryParam!=null and csEventUserQueryParam.target != null and csEventUserQueryParam.target.size()>0">

    and b.tag in
    <foreach collection="csEventUserQueryParam.target" index="index" item="item" open="(" separator="," close=")">
      #{item}
    </foreach>
    </if>
    and b.device_id in
    <foreach collection="devIds" index="index" item="item" open="(" separator="," close=")">
      #{item}
    </foreach>

    <if test="csEventUserQueryParam!=null and csEventUserQueryParam.type != null and csEventUserQueryParam.type !=''">
      AND   b.type =#{ csEventUserQueryParam.type}
    </if>
    <if test="csEventUserQueryParam!=null and csEventUserQueryParam.level != null and csEventUserQueryParam.level !=''">
      AND   b.level =#{ csEventUserQueryParam.level}
    </if>
    <if test="csEventUserQueryParam!=null and csEventUserQueryParam.status != null and csEventUserQueryParam.status !=''">
      AND   a.status =#{ csEventUserQueryParam.status}
    </if>
    <if test="csEventUserQueryParam!=null and csEventUserQueryParam.userId != null  and csEventUserQueryParam.userId !=''">
    and a.user_id=#{ csEventUserQueryParam.userId}
  </if>
    order by b.start_time desc

  </select>

<!--  <select id="queryEventpage" resultType="com.njcn.csharmonic.pojo.vo.EventDetailVO">-->
<!--    select DISTINCT a.event_id id,-->
<!--    <if test="flag">-->
<!--      a.status status,-->
<!--    </if>-->
<!--    <if test="!flag">-->
<!--      1 status,-->
<!--    </if>-->
<!--    b.device_id deviceId,b.line_id lineId,b.code code,b.start_time startTime,b.amplitude,b.persist_time,-->
<!--    round(b.amplitude,2) evtParamVVaDepth,round(b.persist_time,2) evtParamTm,b.tag tag ,b.wave_path wavePath,b.phase evtParamPhase,b.location evtParamPosition,b.instant_pics,b.rms_pics , b.type type,b.level level,b.location location,d.name lineName-->
<!--    from cs_event_user a inner join cs_event b on a.event_id=b.id inner join cs_equipment_delivery c on b.device_id=c.id inner join cs_line d on d.device_id=b.device_id and d.clDid=b.cl_did where 1=1-->
<!--    and b.process=c.process-->
<!--    <if test="csEventUserQueryPage!=null and csEventUserQueryPage.endTime  != null and csEventUserQueryPage.endTime !=''">-->
<!--      AND DATE(b.start_time) &lt;= DATE(#{csEventUserQueryPage.endTime})-->
<!--    </if>-->
<!--    <if test="csEventUserQueryPage!=null and csEventUserQueryPage.startTime != null and csEventUserQueryPage.startTime !=''">-->
<!--      AND DATE(b.start_time) &gt;= DATE(#{csEventUserQueryPage.startTime})-->
<!--    </if>-->
<!--    <if test="csEventUserQueryPage!=null and csEventUserQueryPage.target != null and csEventUserQueryPage.target.size()>0">-->

<!--      and b.tag in-->
<!--      <foreach collection="csEventUserQueryPage.target" index="index" item="item" open="(" separator="," close=")">-->
<!--        #{item}-->
<!--      </foreach>-->
<!--    </if>-->
<!--    and b.device_id in-->
<!--    <foreach collection="devIds" index="index" item="item" open="(" separator="," close=")">-->
<!--      #{item}-->
<!--    </foreach>-->
<!--    <if test="csEventUserQueryPage!=null and csEventUserQueryPage.type != null and csEventUserQueryPage.type !=''">-->
<!--      AND   b.type =#{ csEventUserQueryPage.type}-->
<!--    </if>-->
<!--    <if test="csEventUserQueryPage!=null and csEventUserQueryPage.level != null and csEventUserQueryPage.level !=''">-->
<!--      AND b.level IN-->
<!--      <foreach collection="csEventUserQueryPage.level.split(',')" item="level" open="(" separator="," close=")">-->
<!--        #{level}-->
<!--      </foreach>-->
<!--    </if>-->
<!--    <if test="csEventUserQueryPage!=null and csEventUserQueryPage.status != null and csEventUserQueryPage.status !=''">-->
<!--      AND   a.status =#{ csEventUserQueryPage.status}-->
<!--    </if>-->
<!--    <if test="csEventUserQueryPage!=null and csEventUserQueryPage.userId != null  and csEventUserQueryPage.userId !=''">-->
<!--      and a.user_id=#{ csEventUserQueryPage.userId}-->
<!--    </if>-->
<!--    <if test="csEventUserQueryPage!=null and csEventUserQueryPage.sortField != null">-->
<!--      <choose>-->
<!--        <when test="csEventUserQueryPage.sortField == 0">-->
<!--          order by b.start_time desc-->
<!--        </when>-->
<!--        <when test="csEventUserQueryPage.sortField == 1">-->
<!--          order by b.amplitude desc-->
<!--        </when>-->
<!--        <when test="csEventUserQueryPage.sortField == 2">-->
<!--          order by b.persist_time desc-->
<!--        </when>-->
<!--        <otherwise>-->
<!--          order by b.start_time desc-->
<!--        </otherwise>-->
<!--      </choose>-->
<!--    </if>-->
<!--  </select>-->

  <select id="queryEventpage" resultType="com.njcn.csharmonic.pojo.vo.EventDetailVO">
    SELECT DISTINCT b.id,
      b.device_id deviceId,
      b.line_id lineId,
      b.CODE CODE,
      b.start_time startTime,
      b.amplitude,
      b.persist_time,
      round(b.amplitude, 2) evtParamVVaDepth,
      round(b.persist_time, 2) evtParamTm,
      b.phase evtParamPhase,
      b.location evtParamPosition,
      b.tag tag,
      b.wave_path wavePath,
      b.instant_pics,
      b.rms_pics,
      b.type type,
      b.LEVEL LEVEL,
      b.location location,
      c.dev_type devType,
      b.land_point landPoint,
      b.advance_reason advanceReason,
      b.advance_type advanceType,
      b.sag_source sagSource
      <if test="csEventUserQueryPage != null and csEventUserQueryPage.type != null and csEventUserQueryPage.type != '' and csEventUserQueryPage.type ==0">
      ,d.NAME lineName
      </if>
    from
      cs_event b
          inner join cs_equipment_delivery c on b.device_id=c.id
          <if test="csEventUserQueryPage != null and csEventUserQueryPage.type != null and csEventUserQueryPage.type != '' and csEventUserQueryPage.type ==0">
          inner join cs_line d on d.device_id = b.device_id and d.clDid=b.cl_did
          </if>
    where 1=1
    and b.process=c.process
    <if test="csEventUserQueryPage!=null and csEventUserQueryPage.endTime  != null and csEventUserQueryPage.endTime !=''">
      AND DATE(b.start_time) &lt;= DATE(#{csEventUserQueryPage.endTime})
    </if>
    <if test="csEventUserQueryPage!=null and csEventUserQueryPage.startTime != null and csEventUserQueryPage.startTime !=''">
      AND DATE(b.start_time) &gt;= DATE(#{csEventUserQueryPage.startTime})
    </if>
    <if test="csEventUserQueryPage!=null and csEventUserQueryPage.target != null and csEventUserQueryPage.target.size()>0">
      and b.tag in
      <foreach collection="csEventUserQueryPage.target" index="index" item="item" open="(" separator="," close=")">
        #{item}
      </foreach>
    </if>
    and b.device_id in
    <foreach collection="devIds" index="index" item="item" open="(" separator="," close=")">
      #{item}
    </foreach>
    <if test="csEventUserQueryPage!=null and csEventUserQueryPage.lineId != null and csEventUserQueryPage.lineId !=''">
      AND b.line_id = #{csEventUserQueryPage.lineId}
    </if>
    <if test="csEventUserQueryPage != null and csEventUserQueryPage.type != null and csEventUserQueryPage.type != ''">
      AND b.type = #{csEventUserQueryPage.type}
      <if test="csEventUserQueryPage.type == '0'">
        AND d.clDid = b.cl_did
      </if>
    </if>
    <if test="csEventUserQueryPage!=null and csEventUserQueryPage.level != null and csEventUserQueryPage.level !=''">
      AND b.level IN
      <foreach collection="csEventUserQueryPage.level.split(',')" item="level" open="(" separator="," close=")">
        #{level}
      </foreach>
    </if>
    <choose>
      <when test="csEventUserQueryPage!=null and csEventUserQueryPage.sortField != null">
        <choose>
          <when test="csEventUserQueryPage.sortField == 0">
            order by b.start_time desc
          </when>
          <when test="csEventUserQueryPage.sortField == 1">
            order by b.amplitude desc
          </when>
          <when test="csEventUserQueryPage.sortField == 2">
            order by b.persist_time desc
          </when>
          <otherwise>
            order by b.start_time desc
          </otherwise>
        </choose>
      </when>
      <otherwise>
        order by b.start_time desc
      </otherwise>
    </choose>

  </select>

  <select id="queryEventPageWeb" resultType="com.njcn.csharmonic.pojo.vo.EventDetailVO">
    select DISTINCT b.id id,
    b.device_id deviceId,b.line_id lineId,b.code code,
    b.start_time startTime,b.tag tag ,b.wave_path wavePath,b.instant_pics,b.rms_pics , b.type type,b.level level,b.location location,b.cl_did clDid
    ,d.name lineName,b.advance_reason advanceReason,b.advance_type advanceType,b.sag_source sagSource
    from  cs_event b
    left join  cs_line d on d.line_id=b.line_id
    where 1=1
    <if test="csEventUserQueryPage!=null and csEventUserQueryPage.endTime  != null and csEventUserQueryPage.endTime !=''">
      AND DATE(b.start_time) &lt;= DATE(#{csEventUserQueryPage.endTime})
    </if>
    <if test="csEventUserQueryPage!=null and csEventUserQueryPage.startTime != null and csEventUserQueryPage.startTime !=''">
      AND DATE(b.start_time) &gt;= DATE(#{csEventUserQueryPage.startTime})
    </if>
    <if test="csEventUserQueryPage!=null and csEventUserQueryPage.target != null and csEventUserQueryPage.target.size()>0">
      and b.tag in
      <foreach collection="csEventUserQueryPage.target" index="index" item="item" open="(" separator="," close=")">
        #{item}
      </foreach>
    </if>
    and b.device_id in
    <foreach collection="devIds" index="index" item="item" open="(" separator="," close=")">
      #{item}
    </foreach>
    <if test="csEventUserQueryPage!=null and csEventUserQueryPage.type != null and csEventUserQueryPage.type !=''">
      <if test="csEventUserQueryPage!=null and csEventUserQueryPage.type == 3 ">
        AND   b.type in(2,3)
      </if>
      <if test="csEventUserQueryPage!=null and csEventUserQueryPage.type != 3 ">
        AND   b.type =#{ csEventUserQueryPage.type}
      </if>

    </if>
    <if test="csEventUserQueryPage!=null and csEventUserQueryPage.level != null and csEventUserQueryPage.level !=''">
      AND b.level IN
      <foreach collection="csEventUserQueryPage.level.split(',')" item="level" open="(" separator="," close=")">
        #{level}
      </foreach>
    </if>
    <if test="csEventUserQueryPage!=null and csEventUserQueryPage.eventType != null and csEventUserQueryPage.eventType !=''">
      AND b.tag = #{csEventUserQueryPage.eventType}
    </if>

    <if test="csEventUserQueryPage!=null and csEventUserQueryPage.location != null and csEventUserQueryPage.location !=''">
      AND b.location = #{csEventUserQueryPage.location}
    </if>
    order by b.start_time desc
  </select>

  <select id="queryTempEvent" resultType="java.lang.String">
    SELECT
    t1.event_id
    FROM
    cs_event_user t1 right join cs_event t2 on t1.event_id = t2.id
    WHERE
    t1.user_id = #{userId}
    and t1.`status`= 0
    and t2.type = 0
    <if test="ids != null and ids.size > 0">
      and t2.line_id in
      <foreach collection="ids" item="item" index="index" open="(" close=")" separator=",">
        #{item}
      </foreach>
    </if>
  </select>

  <select id="queryTempEventDetail" resultType="com.njcn.csharmonic.pojo.po.CsEventPO">
    SELECT
    t2.*
    FROM
    cs_event_user t1 right join cs_event t2 on t1.event_id = t2.id
    WHERE
    t1.user_id = #{userId}
    and t1.`status`= 0
    and t2.type = 0
    <if test="ids != null and ids.size > 0">
      and t2.line_id in
      <foreach collection="ids" item="item" index="index" open="(" close=")" separator=",">
        #{item}
      </foreach>
    </if>
    and t2.start_time between #{startTime} and #{endTime}
  </select>

  <select id="queryAlarmEvent" resultType="java.lang.String">
    SELECT
    t1.event_id
    FROM
    cs_event_user t1 right join cs_alarm t2 on t1.event_id = t2.id
    WHERE
    t1.user_id = #{userId}
    and t1.`status`= 0
    <if test="ids != null and ids.size > 0">
      and (
      <foreach collection="ids" item="tag" separator=" OR ">
        FIND_IN_SET(#{tag}, t2.dev_list) > 0
      </foreach>
      )
    </if>
  </select>

  <select id="queryRunEvent" resultType="java.lang.String">
    SELECT
    t1.event_id
    FROM
    cs_event_user t1 right join cs_event t2 on t1.event_id = t2.id
    WHERE
    t1.user_id = #{userId}
    and t1.`status`= 0
    and t2.type = 2
    <if test="ids != null and ids.size > 0">
      and t2.device_id in
      <foreach collection="ids" item="item" index="index" open="(" close=")" separator=",">
        #{item}
      </foreach>
    </if>
  </select>

  <select id="queryTempHarmonic" resultType="java.lang.String">
    SELECT
    t1.event_id
    FROM
    cs_event_user t1 right join cs_harmonic t2 on t1.event_id = t2.id
    WHERE
    t1.user_id = #{userId}
    and t1.`status`= 0
    <if test="ids != null and ids.size > 0">
      and t2.line_id in
      <foreach collection="ids" item="item" index="index" open="(" close=")" separator=",">
        #{item}
      </foreach>
    </if>
  </select>

  <select id="getEventCounts" resultType="com.njcn.csharmonic.pojo.vo.event.EventStatisticVO">
    SELECT
      SUM(CASE WHEN tag = 'Evt_Sys_DipStr' THEN 1 ELSE 0 END) AS eventDown,
      SUM(CASE WHEN tag = 'Evt_Sys_IntrStr' THEN 1 ELSE 0 END) AS eventOff,
      SUM(CASE WHEN tag = 'Evt_Sys_SwlStr' THEN 1 ELSE 0 END) AS eventUp,
      SUM(CASE WHEN tag IN ('Evt_Sys_DipStr', 'Evt_Sys_IntrStr', 'Evt_Sys_SwlStr') THEN 1 ELSE 0 END) AS allNum
    FROM
      cs_event
    WHERE
      type = 0
      AND start_time BETWEEN #{startTime} AND #{endTime}
      AND device_id IN
      <foreach collection="ids" item="item" index="index" open="(" close=")" separator=",">
        #{item}
      </foreach>
  </select>

</mapper>