<?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.zcxk.rmcp.core.dao.DeviceAlarmMapper">
  <resultMap id="BaseResultMap" type="com.zcxk.rmcp.core.entity.DeviceAlarm">
    <!--@mbg.generated-->
    <!--@Table rmcp_device_alarm-->
    <id column="id" property="id" />
    <result column="tenant_id" property="tenantId" />
    <result column="device_id" property="deviceId" />
    <result column="measuring_id" property="measuringId" />
    <result column="alarm_type_id" property="alarmTypeId" />
    <result column="alarm_time" property="alarmTime" />
    <result column="last_alarm_time" property="lastAlarmTime" />
    <result column="alarm_count" property="alarmCount" />
    <result column="handle_status" property="handleStatus" />
    <result column="handle_result" property="handleResult" />
    <result column="measuring_data" property="measuringData" />
    <result column="status" property="status" />
    <result column="create_by" property="createBy" />
    <result column="update_by" property="updateBy" />
    <result column="create_date" property="createDate" />
    <result column="update_date" property="updateDate" />
  </resultMap>
  <sql id="Base_Column_List">
    <!--@mbg.generated-->
    id, tenant_id, device_id, measuring_id, alarm_type_id, alarm_time, last_alarm_time, 
    alarm_count, handle_status, handle_result, measuring_data, `status`, create_by, update_by, 
    create_date, update_date
  </sql>
  <insert id="insertSelective" keyColumn="id" keyProperty="id" parameterType="com.zcxk.rmcp.core.entity.DeviceAlarm" useGeneratedKeys="true">
    <!--@mbg.generated-->
    insert into rmcp_device_alarm
    <trim prefix="(" suffix=")" suffixOverrides=",">
      <if test="id != null">
        id,
      </if>
      <if test="tenantId != null">
        tenant_id,
      </if>
      <if test="deviceId != null">
        device_id,
      </if>
      <if test="measuringId != null">
        measuring_id,
      </if>
      <if test="alarmTypeId != null">
        alarm_type_id,
      </if>
      <if test="alarmTime != null">
        alarm_time,
      </if>
      <if test="lastAlarmTime != null">
        last_alarm_time,
      </if>
      <if test="alarmCount != null">
        alarm_count,
      </if>
      <if test="handleStatus != null">
        handle_status,
      </if>
      <if test="handleTime != null">
        handle_time,
      </if>
      <if test="handleResult != null">
        handle_result,
      </if>
      <if test="measuringData != null">
        measuring_data,
      </if>
      <if test="status != null">
        `status`,
      </if>
      <if test="createBy != null">
        create_by,
      </if>
      <if test="updateBy != null">
        update_by,
      </if>
      <if test="createDate != null">
        create_date,
      </if>
      <if test="updateDate != null">
        update_date,
      </if>
    </trim>
    <trim prefix="values (" suffix=")" suffixOverrides=",">
      <if test="id != null">
        #{id},
      </if>
      <if test="tenantId != null">
        #{tenantId},
      </if>
      <if test="deviceId != null">
        #{deviceId},
      </if>
      <if test="measuringId != null">
        #{measuringId},
      </if>
      <if test="alarmTypeId != null">
        #{alarmTypeId},
      </if>
      <if test="alarmTime != null">
        #{alarmTime},
      </if>
      <if test="lastAlarmTime != null">
        #{lastAlarmTime},
      </if>
      <if test="alarmCount != null">
        #{alarmCount},
      </if>
      <if test="handleStatus != null">
        #{handleStatus},
      </if>
      <if test="handleTime != null">
        #{handleTime},
      </if>
      <if test="handleResult != null">
        #{handleResult},
      </if>
      <if test="measuringData != null">
        #{measuringData},
      </if>
      <if test="status != null">
        #{status},
      </if>
      <if test="createBy != null">
        #{createBy},
      </if>
      <if test="updateBy != null">
        #{updateBy},
      </if>
      <if test="createDate != null">
        #{createDate},
      </if>
      <if test="updateDate != null">
        #{updateDate},
      </if>
    </trim>
  </insert>
  <update id="updateByPrimaryKeySelective" parameterType="com.zcxk.rmcp.core.entity.DeviceAlarm">
    <!--@mbg.generated-->
    update rmcp_device_alarm
    <set>
      <if test="tenantId != null">
        tenant_id = #{tenantId},
      </if>
      <if test="deviceId != null">
        device_id = #{deviceId},
      </if>
      <if test="measuringId != null">
        measuring_id = #{measuringId},
      </if>
      <if test="alarmTypeId != null">
        alarm_type_id = #{alarmTypeId},
      </if>
      <if test="alarmTime != null">
        alarm_time = #{alarmTime},
      </if>
      <if test="lastAlarmTime != null">
        last_alarm_time = #{lastAlarmTime},
      </if>
      <if test="alarmCount != null">
        alarm_count = #{alarmCount},
      </if>
      <if test="handleStatus != null">
        handle_status = #{handleStatus},
      </if>
      <if test="handleTime != null">
        handle_time = #{handleTime},
      </if>
      <if test="handleResult != null">
        handle_result = #{handleResult},
      </if>
      <if test="measuringData != null">
        measuring_data = #{measuringData},
      </if>
      <if test="status != null">
        `status` = #{status},
      </if>
      <if test="createBy != null">
        create_by = #{createBy},
      </if>
      <if test="updateBy != null">
        update_by = #{updateBy},
      </if>
      <if test="createDate != null">
        create_date = #{createDate},
      </if>
      <if test="updateDate != null">
        update_date = #{updateDate},
      </if>
    </set>
    where id = #{id}
  </update>
  <insert id="batchInsert" keyColumn="id" keyProperty="id" parameterType="map" useGeneratedKeys="true">
    <!--@mbg.generated-->
    insert into rmcp_device_alarm
    (tenant_id, device_id, measuring_id, alarm_type_id, alarm_time, last_alarm_time, 
      alarm_count, handle_status, handle_time, handle_result, measuring_data, `status`, create_by,
      update_by, create_date, update_date)
    values
    <foreach collection="list" item="item" separator=",">
      (#{item.tenantId}, #{item.deviceId}, #{item.measuringId}, #{item.alarmTypeId}, #{item.alarmTime}, 
        #{item.lastAlarmTime}, #{item.alarmCount}, #{item.handleStatus}, #{item.handleResult}, 
        #{item.measuringData}, #{item.status}, #{item.createBy}, #{item.updateBy}, #{item.createDate}, 
        #{item.updateDate})
    </foreach>
  </insert>


  <sql id="permissionCondition">
    <if test="userCondition.tenantId != null">and rd.tenant_id = #{userCondition.tenantId}</if>
    <if test="userCondition.userType!=null and userCondition.userType!=-999 and userCondition.userType!=-9999 and  userCondition.programItems != null and userCondition.programItems.size() > 0">
      <if test="userCondition.permissionType == 5 or userCondition.permissionType == 2">
        and ( rd.dept_org_id in
        <foreach collection="userCondition.programItems" item="item" open="(" close=")" separator=",">
          #{item.orgId}
        </foreach>
        or
        rd.company_org_id in
        <foreach collection="userCondition.programItems" item="item" open="(" close=")" separator=",">
          #{item.orgId}
        </foreach>
        )
      </if>
      <if test="userCondition.permissionType == 4 or userCondition.permissionType == 3">
        and rd.dept_org_id in
        <foreach collection="userCondition.programItems" item="item" open="(" close=")" separator=",">
          #{item.orgId}
        </foreach>
      </if>
      <if test="userCondition.permissionType == 1">
        and rd.company_org_id in
        <foreach collection="userCondition.programItems" item="item" open="(" close=")" separator=",">
          #{item.orgId}
        </foreach>
        and (rd.dept_org_id is null or rd.dept_org_id =0)
      </if>
    </if>
  </sql>

  <select id="getLastDeviceErrorList" resultMap="BaseResultMap">
    SELECT *
    FROM rmcp_device_alarm
    WHERE last_alarm_time IN (
      SELECT MAX(last_alarm_time)
      FROM rmcp_device_alarm WHERE device_id = #{deviceId}
      GROUP BY alarm_type_id
    ) AND
    status = 1 and device_id = #{deviceId} and handle_status = 0
  </select>

  <select id="findList" resultType="com.zcxk.rmcp.api.vo.DeviceAlarmVo">
    select
      rda.id,
      rda.device_id,
      rda.last_alarm_time,
      rda.handle_status,
      rda.handle_time,
      rda.measuring_data,
      rd.device_no,
      rd.meter_no,
      rd.file_no,
      rd.address,
      rat.name as alarm_type_name,
      rp.product_name,
      rp.product_model,
      rm.name as manufacturer_name,
      roc.ORG_NAME as company_name,
      rod.ORG_NAME as dept_name
    from rmcp_device_alarm rda
    left join rmcp_device rd on rda.device_id = rd.id
    left join rmcp_alarm_type rat on rda.alarm_type_id = rat.id
    left join rmcp_product rp on (rp.id = rd.product_id)
    left join rmcp_manufacturer rm on (rm.id = rp.manufacturer_id)
    left join rmcp_org roc on (roc.ID = rd.company_org_id)
    left join rmcp_org rod on (rod.ID = rd.dept_org_id)
    <where>
      rda.status = 1 and rd.status = 1
      <if test="dto.deviceId != null">and rd.id = #{dto.deviceId}</if>
      <if test="dto.categoryId != null">and rd.category_id = #{dto.categoryId}</if>
      <if test="dto.productId != null">and rd.product_id = #{dto.productId}</if>
      <if test="dto.deviceNo != null and dto.deviceNo != ''">and (rd.device_no LIKE concat('%',#{dto.deviceNo},'%') or rd.meter_no LIKE concat('%',#{dto.deviceNo},'%') )</if>
      <if test="dto.alarmTypeId != null">and rda.alarm_type_id = #{dto.alarmTypeId}</if>
      <if test="dto.handleStatus != null">and rda.handle_status = #{dto.handleStatus}</if>
      <if test="dto.startDate != null"> and rd.last_receive_time <![CDATA[ >= ]]> #{dto.startDate}</if>
      <if test="dto.endDate != null"> and rd.last_receive_time <![CDATA[ <= ]]> #{dto.endDate}</if>
      <include refid="permissionCondition"/>
    </where>
    order by rda.last_alarm_time desc
  </select>

  <update id="deleteByDeviceId">
    UPDATE rmcp_device_alarm
    set
    status = 0,
    update_date=now()
    WHERE `status` = 1
    and device_id in <foreach collection="deviceIds" item="item" open="(" separator="," close=")">#{item}</foreach>
  </update>
</mapper>