<?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="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.tag tag ,b.wave_path wavePath,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>
    order by b.start_time desc
  </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
    <if test="csEventUserQueryPage!=null and (csEventUserQueryPage.type == 0 or csEventUserQueryPage.type == 1 )">
    ,d.name lineName
    </if>
    from  cs_event b
    <if test="csEventUserQueryPage!=null and (csEventUserQueryPage.type == 0 or csEventUserQueryPage.type == 1 )">
    inner join  cs_line d on d.line_id=b.line_id
    </if>
    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>
</mapper>