<?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.DeviceMapper">
  <resultMap id="BaseResultMap" type="com.zcxk.rmcp.core.entity.Device">
    <!--@mbg.generated-->
    <!--@Table rmcp_device-->
    <id column="id" property="id" />
    <result column="tenant_id" property="tenantId" />
    <result column="company_org_id" property="companyOrgId" />
    <result column="dept_org_id" property="deptOrgId" />
    <result column="product_id" property="productId" />
    <result column="device_no" property="deviceNo" />
    <result column="meter_no" property="meterNo" />
    <result column="file_no" property="fileNo" />
    <result column="seal_no" property="sealNo" />
    <result column="device_status" property="deviceStatus" />
    <result column="address" property="address" />
    <result column="lng" property="lng" />
    <result column="lat" property="lat" />
    <result column="category_id" property="categoryId" />
    <result column="community_id" property="communityId" />
    <result column="read_data" property="readData" />
    <result column="valve_status" property="valveStatus" />
    <result column="last_receive_time" property="lastReceiveTime" />
    <result column="user_name" property="userName" />
    <result column="user_phone" property="userPhone" />
    <result column="id_card" property="idCard" />
    <result column="old_end_degree" property="oldEndDegree" />
    <result column="old_image_url" property="oldImageUrl" />
    <result column="last_alarm_date" property="lastAlarmDate" />
    <result column="offline_date" property="offlineDate" />
    <result column="accept_date" property="acceptDate" />
    <result column="install_date" property="installDate" />
    <result column="is_install" property="isInstall" />
    <result column="is_accept" property="isAccept" />
    <result column="status" property="status" />
    <result column="create_by" property="createBy" />
    <result column="create_date" property="createDate" />
    <result column="update_by" property="updateBy" />
    <result column="update_date" property="updateDate" />
  </resultMap>

  <resultMap type="com.zcxk.rmcp.api.vo.DeviceStatusVo" id="deviceStatusMap">
    <result property="name" column="name" jdbcType="VARCHAR"/>
    <collection property="statistics" ofType="com.zcxk.rmcp.api.vo.StatusStatisticsVo" javaType="list">
      <result property="status" column="status" jdbcType="INTEGER"/>
      <result property="total" column="total" jdbcType="INTEGER"/>
    </collection>
  </resultMap>

  <resultMap type="com.zcxk.rmcp.api.vo.ProductCategoryCommunityVo" id="productCategoryCommunityVoMap">
    <result property="categoryId" column="categoryId" jdbcType="INTEGER"/>
    <result property="categoryName" column="categoryName" jdbcType="INTEGER"/>
    <collection property="communityVos" resultMap="communityVos"/>
  </resultMap>

  <resultMap id="communityVos" type="com.zcxk.rmcp.api.vo.DeviceCommunityVo">
    <result property="communityId" column="communityId" jdbcType="BIGINT"/>
    <result property="name" column="name" jdbcType="VARCHAR"/>
    <result property="address" column="address" jdbcType="VARCHAR"/>
    <result property="lng" column="lng" jdbcType="DOUBLE"/>
    <result property="lat" column="lat" jdbcType="DOUBLE"/>
    <collection property="statusVos" resultMap="statusVos"/>
  </resultMap>

  <resultMap id="statusVos" type="com.zcxk.rmcp.api.vo.StatusStatisticsVo">
    <result property="status" column="status" jdbcType="INTEGER"/>
    <result property="total" column="total" jdbcType="INTEGER"/>
  </resultMap>

  <sql id="Base_Column_List">
    <!--@mbg.generated-->
    id, tenant_id, company_org_id, dept_org_id, product_id, device_no, meter_no, file_no, 
    seal_no, device_status, address, lng, lat, category_id, community_id, read_data,
    valve_status, last_receive_time, user_name, user_phone, id_card, old_end_degree, old_image_url,
    last_alarm_date,offline_date,install_date,accept_date,is_install,is_accept,`status`, create_by, create_date, update_by, update_date
  </sql>
  <insert id="insertSelective" keyColumn="id" keyProperty="id" parameterType="com.zcxk.rmcp.core.entity.Device" useGeneratedKeys="true">
    <!--@mbg.generated-->
    insert into rmcp_device
    <trim prefix="(" suffix=")" suffixOverrides=",">
      <if test="id != null">
        id,
      </if>
      <if test="tenantId != null">
        tenant_id,
      </if>
      <if test="companyOrgId != null">
        company_org_id,
      </if>
      <if test="deptOrgId != null">
        dept_org_id,
      </if>
      <if test="productId != null">
        product_id,
      </if>
      <if test="deviceNo != null">
        device_no,
      </if>
      <if test="meterNo != null">
        meter_no,
      </if>
      <if test="fileNo != null">
        file_no,
      </if>
      <if test="sealNo != null">
        seal_no,
      </if>
      <if test="deviceStatus != null">
        device_status,
      </if>
      <if test="address != null">
        address,
      </if>
      <if test="lng != null">
        lng,
      </if>
      <if test="lat != null">
        lat,
      </if>
      <if test="categoryId != null">
        category_id,
      </if>
      <if test="communityId != null">
        community_id,
      </if>
      <if test="readData != null">
        read_data,
      </if>
      <if test="valveStatus != null">
        valve_status,
      </if>
      <if test="lastReceiveTime != null">
        last_receive_time,
      </if>
      <if test="userName != null">
        `user_name`,
      </if>
      <if test="userPhone != null">
        `user_phone`,
      </if>
      <if test="idCard != null">
        `id_card`,
      </if>
      <if test="oldEndDegree != null">
        `old_end_degree`,
      </if>
      <if test="oldImageUrl != null">
        `old_image_url`,
      </if>
      <if test="lastAlarmDate != null">
        `last_alarm_date`,
      </if>
      <if test="offlineDate != null">
        `offline_date`,
      </if>
      <if test="acceptDate != null">
        `accept_date`,
      </if>
      <if test="installDate != null">
        `install_date`,
      </if>
      <if test="isInstall != null">
        `is_install`,
      </if>
      <if test="isAccept != null">
        `is_accept`,
      </if>
      <if test="status != null">
        `status`,
      </if>
      <if test="createBy != null">
        create_by,
      </if>
      <if test="createDate != null">
        create_date,
      </if>
      <if test="updateBy != null">
        update_by,
      </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="companyOrgId != null">
        #{companyOrgId},
      </if>
      <if test="deptOrgId != null">
        #{deptOrgId},
      </if>
      <if test="productId != null">
        #{productId},
      </if>
      <if test="deviceNo != null">
        #{deviceNo},
      </if>
      <if test="meterNo != null">
        #{meterNo},
      </if>
      <if test="fileNo != null">
        #{fileNo},
      </if>
      <if test="sealNo != null">
        #{sealNo},
      </if>
      <if test="deviceStatus != null">
        #{deviceStatus},
      </if>
      <if test="address != null">
        #{address},
      </if>
      <if test="lng != null">
        #{lng},
      </if>
      <if test="lat != null">
        #{lat},
      </if>
      <if test="categoryId != null">
        #{categoryId},
      </if>
      <if test="communityId != null">
        #{communityId},
      </if>
      <if test="readData != null">
        #{readData},
      </if>
      <if test="valveStatus != null">
        #{valveStatus},
      </if>
      <if test="lastReceiveTime != null">
        #{lastReceiveTime},
      </if>
      <if test="userName != null">
        #{userName},
      </if>
      <if test="userPhone != null">
        #{userPhone},
      </if>
      <if test="idCard != null">
        #{idCard},
      </if>
      <if test="oldEndDegree != null">
        #{oldEndDegree},
      </if>
      <if test="oldImageUrl != null">
        #{oldImageUrl},
      </if>
      <if test="lastAlarmDate != null">
        #{lastAlarmDate},
      </if>
      <if test="offlineDate != null">
        #{offlineDate},
      </if>
      <if test="acceptDate != null">
        #{acceptDate},
      </if>
      <if test="installDate != null">
        #{installDate},
      </if>
      <if test="isInstall != null">
        #{isInstall},
      </if>
      <if test="isAccept != null">
        #{isAccept},
      </if>
      <if test="status != null">
        #{status},
      </if>
      <if test="createBy != null">
        #{createBy},
      </if>
      <if test="createDate != null">
        #{createDate},
      </if>
      <if test="updateBy != null">
        #{updateBy},
      </if>
      <if test="updateDate != null">
        #{updateDate},
      </if>
    </trim>
  </insert>
  <update id="updateByPrimaryKeySelective" parameterType="com.zcxk.rmcp.core.entity.Device">
    <!--@mbg.generated-->
    update rmcp_device
    <set>
      <if test="tenantId != null">
        tenant_id = #{tenantId},
      </if>
      <if test="companyOrgId != null">
        company_org_id = #{companyOrgId},
      </if>
      <if test="deptOrgId != null">
        dept_org_id = #{deptOrgId},
      </if>
      <if test="productId != null">
        product_id = #{productId},
      </if>
      <if test="deviceNo != null">
        device_no = #{deviceNo},
      </if>
      <if test="meterNo != null">
        meter_no = #{meterNo},
      </if>
      <if test="fileNo != null">
        file_no = #{fileNo},
      </if>
      <if test="sealNo != null">
        seal_no = #{sealNo},
      </if>
      <if test="deviceStatus != null">
        device_status = #{deviceStatus},
      </if>
      <if test="address != null">
        address = #{address},
      </if>
      <if test="lng != null">
        lng = #{lng},
      </if>
      <if test="lat != null">
        lat = #{lat},
      </if>
      <if test="categoryId != null">
        category_id = #{categoryId},
      </if>
      <if test="communityId != null">
        community_id = #{communityId},
      </if>
      <if test="readData != null">
        read_data = #{readData},
      </if>
      <if test="valveStatus != null">
        valve_status = #{valveStatus},
      </if>
      <if test="lastReceiveTime != null">
        last_receive_time = #{lastReceiveTime},
      </if>
      <if test="userName != null">
        `user_name` = #{userName},
      </if>
      <if test="userPhone != null">
        `user_phone` = #{userPhone},
      </if>
      <if test="idCard != null">
        `id_card` = #{idCard},
      </if>
      <if test="oldEndDegree != null">
        `old_end_degree` = #{oldEndDegree},
      </if>
      <if test="oldImageUrl != null">
        `old_image_url` = #{oldImageUrl},
      </if>
      <if test="lastAlarmDate != null">
        `last_alarm_date` = #{lastAlarmDate},
      </if>
      <if test="offlineDate != null">
        `offline_date` = #{offlineDate},
      </if>
      <if test="acceptDate != null">
        `accept_date` = #{acceptDate},
      </if>
      <if test="installDate != null">
        `install_date` = #{installDate},
      </if>
      <if test="isInstall != null">
        `is_install` = #{isInstall},
      </if>
      <if test="isAccept != null">
        `is_accept` = #{isAccept},
      </if>
      <if test="status != null">
        `status` = #{status},
      </if>
      <if test="createBy != null">
        create_by = #{createBy},
      </if>
      <if test="createDate != null">
        create_date = #{createDate},
      </if>
      <if test="updateBy != null">
        update_by = #{updateBy},
      </if>
      <if test="updateDate != null">
        update_date = #{updateDate},
      </if>
    </set>
    where id = #{id}
  </update>

  <insert id="batchInsert" parameterType="map">
    <!--@mbg.generated-->
    insert into rmcp_device
    (id, tenant_id, category_id, company_org_id, dept_org_id, product_id, device_no,
    meter_no, file_no, seal_no, device_status, address, lng, lat, community_id, read_data,
    valve_status, last_receive_time, user_name, user_phone, id_card, old_end_degree,
    old_image_url, last_alarm_date, offline_date, accept_date, install_date, is_install,
    is_accept, `status`, create_by, create_date, update_by, update_date)
    values
    <foreach collection="list" item="item" separator=",">
      (#{item.id}, #{item.tenantId}, #{item.categoryId}, #{item.companyOrgId}, #{item.deptOrgId},
      #{item.productId}, #{item.deviceNo}, #{item.meterNo}, #{item.fileNo}, #{item.sealNo},
      #{item.deviceStatus}, #{item.address}, #{item.lng}, #{item.lat}, #{item.communityId},
      #{item.readData}, #{item.valveStatus}, #{item.lastReceiveTime}, #{item.userName},
      #{item.userPhone}, #{item.idCard}, #{item.oldEndDegree}, #{item.oldImageUrl}, #{item.lastAlarmDate},
      #{item.offlineDate}, #{item.acceptDate}, #{item.installDate}, #{item.isInstall},
      #{item.isAccept}, #{item.status}, #{item.createBy}, #{item.createDate}, #{item.updateBy},
      #{item.updateDate})
    </foreach>
  </insert>

  <select id="findById" resultType="com.zcxk.rmcp.core.entity.Device">
  select <include refid="Base_Column_List"/> from rmcp_device where status = 1 and id = #{id}
  </select>

  <select id="findDetail" resultType="com.zcxk.rmcp.api.vo.DeviceDetailVo">
    select
      rd.id,
      rd.tenant_id,
      rd.category_id,
      rd.company_org_id,
      rd.dept_org_id,
      rd.product_id,
      rd.community_id,
      rd.device_no,
      rd.meter_no,
      rd.file_no,
      rd.seal_no,
      rd.device_status,
      rd.read_data,
      rd.valve_status,
      rd.last_receive_time,
      rd.address,
      rd.lng,
      rd.lat,
      rd.user_name,
      rd.user_phone,
      rd.id_card,
      rd.old_end_degree,
      rd.old_image_url,
      rd.is_install,
      rd.install_date,
      rd.is_accept,
      rd.accept_date,
      rd.create_date,
      rp.product_name,
      rp.product_model,
      rp.is_support_valve,
      rm.name as manufacturer_name,
      roc.ORG_NAME as company_name,
      rod.ORG_NAME as dept_name,
      rc.name as community_name,
      sa1.name as province_name,
      sa2.name as city_name,
      sa3.name as region_name,
      (to_days(now()) - to_days(rd.offline_date)) as offline_days
    from rmcp_device rd
    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)
    left join rmcp_community rc on (rc.id = rd.community_id)
    left join rmcp_area sa1 on sa1.id = rc.province
    left join rmcp_area sa2 on sa2.id = rc.city
    left join rmcp_area sa3 on sa3.id = rc.region
    where rd.status = 1 and rd.id = #{id}
  </select>

  <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="findList" resultType="com.zcxk.rmcp.api.vo.DeviceVo">
    select
    rd.id,
    rd.company_org_id,
    rd.dept_org_id,
    rd.product_id,
    rd.community_id,
    rd.device_no,
    rd.meter_no,
    rd.file_no,
    rd.seal_no,
    rd.device_status,
    rd.read_data,
    rd.valve_status,
    rd.last_receive_time,
    rd.address,
    rd.lng,
    rd.lat,
    rp.product_name,
    rp.product_model,
    rp.is_support_valve,
    rm.name as manufacturer_name,
    roc.ORG_NAME as company_name,
    rod.ORG_NAME as dept_name,
    rc.name as community_name
    from rmcp_device rd
    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)
    left join rmcp_community rc on (rc.id = rd.community_id)
    <where>
      rd.status = 1
      <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.companyOrgId != null">and rd.company_org_id = #{dto.companyOrgId}</if>
      <if test="dto.deptOrgId != null">and rd.dept_org_id = #{dto.deptOrgId}</if>
      <if test="dto.communityId != null">and rd.community_id = #{dto.communityId}</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.address != null and dto.address != ''">and rd.address LIKE concat('%',#{dto.address},'%') </if>
      <if test="dto.deviceStatus != null">and rd.device_status = #{dto.deviceStatus}</if>
      <if test="dto.valveStatus != null">and rd.valve_status = #{dto.deviceStatus}</if>
      <if test="dto.startDate != null"> and rd.install_date <![CDATA[ >= ]]> #{dto.startDate}</if>
      <if test="dto.endDate != null"> and rd.install_date <![CDATA[ <= ]]> #{dto.endDate}</if>
      <include refid="permissionCondition"/>
    </where>
    order by rd.create_date desc
  </select>

  <select id="findList_COUNT" resultType="Long">
    select count(1)
    from rmcp_device rd
    <where>
      rd.status = 1
      <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.companyOrgId != null">and rd.company_org_id = #{dto.companyOrgId}</if>
      <if test="dto.deptOrgId != null">and rd.dept_org_id = #{dto.deptOrgId}</if>
      <if test="dto.communityId != null">and rd.community_id = #{dto.communityId}</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.address != null and dto.address != ''">and rd.address LIKE concat('%',#{dto.address},'%') </if>
      <if test="dto.deviceStatus != null">and rd.device_status = #{dto.deviceStatus}</if>
      <if test="dto.valveStatus != null">and rd.valve_status = #{dto.deviceStatus}</if>
      <if test="dto.startDate != null"> and rd.install_date <![CDATA[ >= ]]> #{dto.startDate}</if>
      <if test="dto.endDate != null"> and rd.install_date <![CDATA[ <= ]]> #{dto.endDate}</if>
      <include refid="permissionCondition"/>
    </where>
  </select>

  <update id="delByIds">
    update rmcp_device set status = 0,update_date = NOW() where status = 1
    and id in <foreach collection="ids" item="item" open="(" separator="," close=")">#{item}</foreach>
  </update>

  <select id="findByDeviceNoUnique" resultType="int">
    select count(1) from rmcp_device where status = 1 and device_no = #{deviceNo}
    <if test="id != null"> and id != #{id}</if>
  </select>

  <select id="findByMeterNoUnique" resultType="int">
    select count(1) from rmcp_device where status = 1 and meter_no = #{meterNo}
    <if test="id != null"> and id != #{id}</if>
  </select>

  <select id="totalDeviceStatus" resultMap="deviceStatusMap">
    SELECT
    rc.name, rd.device_status as status, count(1) total
    FROM
    rmcp_device rd
    INNER JOIN rmcp_product_category rc on rd.category_id = rc.id
    WHERE
    rd.status = 1 and rc.status = 1
    <include refid="permissionCondition"/>
    GROUP BY rd.device_status, rc.name
  </select>

  <select id="listCommunityDevice" resultType="com.zcxk.rmcp.api.vo.DeviceCategoryCommunityVo">
  SELECT rc.id as communityId, rc.name,rp.name as categoryName,rp.id as categoryId,rc.address,rc.lng,rc.lat,rd.device_status as status,count(1) as total
  FROM
     rmcp_device rd
  LEFT JOIN rmcp_community rc on rd.community_id = rc.id
  LEFT JOIN rmcp_product_category rp on rp.id = rd.category_id
  WHERE
    rd.status = 1 and rc.status = 1
    <if test="categoryId != null and categoryId != ''">
      and rp.id = #{categoryId}
    </if>
  <include refid="permissionCondition"/>
  AND rd.community_id > 0
  GROUP BY rd.community_id, rp.id,rp.name, rd.device_status
  </select>

  <select id="listNoCellDevice" resultType="com.zcxk.rmcp.api.vo.DeviceNoCellVo">
    SELECT rd.id as deviceId,rc.name as categoryName,rc.id as categoryId, rd.meter_no, rd.address, rd.lng, rd.lat,rd.read_data,device_status as status
     FROM
    rmcp_device rd
    LEFT JOIN rmcp_product_category rc on rc.id = rd.category_id
     WHERE
    rd.status = 1 and rc.status = 1
    <if test="categoryId != null and categoryId != ''">
      and rc.id = 1 = #{categoryId}
    </if>
    <include refid="permissionCondition"/>
      AND rd.community_id = 0
  </select>

  <select id="findMeasurementDevice" resultType="com.zcxk.rmcp.core.entity.Device">
    select * from rmcp_device rd
    <where>
      status=1
      <if test="companyOrgId != null">and rd.company_org_id = #{companyOrgId}</if>
      <if test="deptOrgId != null">and rd.dept_org_id = #{deptOrgId}</if>
      <if test="communityId != null">and rd.community_id = #{communityId}</if>
    </where>
  </select>

  <select id="getUnInstallDeviceList" resultType="com.zcxk.rmcp.api.dto.install.InstallDeviceDto">
    <if test="listId != null">
      select
      rd.id as device_id,
      rd.device_no,
      rd.meter_no,
      rd.file_no,
      rp.product_name,
      rp.product_model,
      rm.name as manufacturer_name
      from rmcp_install_list ril
      left join rmcp_device rd on ril.device_id = rd.id
      left join rmcp_product rp on (rp.id = rd.product_id)
      left join rmcp_manufacturer rm on (rm.id = rp.manufacturer_id)
      where ril.id = #{listId}
      union
    </if>
    select
    rd.id as device_id,
    rd.device_no,
    rd.meter_no,
    rd.file_no,
    rp.product_name,
    rp.product_model,
    rm.name as manufacturer_name
    from rmcp_device rd
    left join rmcp_product rp on (rp.id = rd.product_id)
    left join rmcp_manufacturer rm on (rm.id = rp.manufacturer_id)
    <where>
      rd.status = 1 and rd.is_install = 0
      <if test="companyOrgId != null">and rd.company_org_id = #{companyOrgId}</if>
      <if test="deptOrgId != null">and rd.dept_org_id = #{deptOrgId}</if>
      <if test="productId != null">and rd.product_id = #{productId}</if>
    </where>
  </select>

  <select id="findByDeviceNo" resultMap="BaseResultMap">
    select id,category_id,product_id,device_no,meter_no,file_no
    from rmcp_device where status = 1 and device_no = #{deviceNo} limit 1;
  </select>

  <update id="updateByAlarm">
    update rmcp_device
    <set>
      <if test="deviceStatus != null">
        device_status = #{deviceStatus},
      </if>
      <if test="lastReceiveTime != null">
        last_receive_time = #{lastReceiveTime},
      </if>
        `last_alarm_date` = #{lastAlarmDate},
      <if test="updateBy != null">
        update_by = #{updateBy},
      </if>
      <if test="updateDate != null">
        update_date = #{updateDate},
      </if>
    </set>
    where id = #{id}
  </update>

  <update id="updateByDeviceData">
    update rmcp_device
    <set>
      <if test="deviceStatus != null">
        device_status = #{deviceStatus},
      </if>
      <if test="readData != null">
        read_data = #{readData},
      </if>
      <if test="valveStatus != null">
        valve_status = #{valveStatus},
      </if>
      <if test="lastReceiveTime != null">
        last_receive_time = #{lastReceiveTime},
      </if>
      `offline_date` = #{lastAlarmDate},
      <if test="updateBy != null">
        update_by = #{updateBy},
      </if>
      <if test="updateDate != null">
        update_date = #{updateDate},
      </if>
    </set>
    where id = #{id}
  </update>

  <select id="countWaterMeter" resultType="int">
    select count(1) from rmcp_device where status = 1
  </select>

  <select id="findWaterMeterListWithPage" resultType="com.zcxk.rmcp.api.dto.readmeter.WaterMeterDto">
    select
    rd.id,
    rd.tenant_id,
    rd.category_id,
    rd.company_org_id,
    rd.dept_org_id,
    rd.product_id,
    rd.community_id,
    rd.device_no,
    rd.meter_no,
    rd.file_no,
    rd.seal_no,
    rd.device_status,
    rd.read_data,
    rd.valve_status,
    rd.last_receive_time,
    rd.address,
    rd.lng,
    rd.lat,
    rp.product_name,
    rp.product_model,
    rp.is_support_valve,
    rm.name as manufacturer_name,
    roc.ORG_NAME as company_name,
    rod.ORG_NAME as dept_name,
    rc.name as community_name
    from rmcp_device rd
    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)
    left join rmcp_community rc on (rc.id = rd.community_id)
    where rd.status = 1
    order by rd.id asc limit #{startIndex},#{pageSize}
  </select>

  <select id="queryDeviceInfoList" resultType="com.zcxk.rmcp.api.vo.DeviceVo">
    select
            rd.id,
            rd.company_org_id,
            rd.dept_org_id,
            rd.product_id,
            rd.community_id,
            rd.device_no,
            rd.meter_no,
            rd.file_no,
            rd.seal_no,
            rd.device_status,
            rd.read_data,
            rd.valve_status,
            rd.last_receive_time,
            rd.address,
            rd.lng,
            rd.lat,
            rp.product_name,
            rp.product_model,
            rp.is_support_valve,
            rm.name as manufacturer_name,
            roc.ORG_NAME as company_name,
            rod.ORG_NAME as dept_name,
            rc.name as community_name
    from rmcp_device rd
                 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)
                 left join rmcp_community rc on (rc.id = rd.community_id)
    <where>
    rd.status = 1
    <if test="deviceNo != null and deviceNo != ''"> AND rd.device_no = #{deviceNo}</if>
    <if test="meterNo != null and meterNo != ''"> AND rd.meter_no = #{meterNo}</if>
    <include refid="permissionCondition"/>
    </where>
  </select>
</mapper>