<?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.InstallListMapper">
  <resultMap id="BaseResultMap" type="com.zcxk.rmcp.core.entity.InstallList">
    <!--@mbg.generated-->
    <!--@Table rmcp_install_list-->
    <id column="id" property="id" />
    <result column="tenant_id" property="tenantId" />
    <result column="install_plan_id" property="installPlanId" />
    <result column="install_plan_data_id" property="installPlanDataId" />
    <result column="door_no" property="doorNo" />
    <result column="device_id" property="deviceId" />
    <result column="new_begin_degree" property="newBeginDegree" />
    <result column="old_end_degree" property="oldEndDegree" />
    <result column="old_image_url" property="oldImageUrl" />
    <result column="meter_code" property="meterCode" />
    <result column="user_name" property="userName" />
    <result column="user_phone" property="userPhone" />
    <result column="id_card" property="idCard" />
    <result column="is_installed" property="isInstalled" />
    <result column="is_accepted" property="isAccepted" />
    <result column="accept_time" property="acceptTime" />
    <result column="remark" property="remark" />
    <result column="status" property="status" />
    <result column="create_date" property="createDate" />
    <result column="update_date" property="updateDate" />
    <result column="create_by" property="createBy" />
    <result column="update_by" property="updateBy" />
  </resultMap>
  <sql id="Base_Column_List">
    <!--@mbg.generated-->
    id, tenant_id, install_plan_id, install_plan_data_id, door_no, device_id, new_begin_degree, 
    old_end_degree, old_image_url, meter_code, user_name, user_phone, id_card, is_installed, 
    is_accepted, accept_time, remark, `status`, create_date, update_date, create_by, 
    update_by
  </sql>
  <insert id="insertSelective" keyColumn="id" keyProperty="id" parameterType="com.zcxk.rmcp.core.entity.InstallList" useGeneratedKeys="true">
    <!--@mbg.generated-->
    insert into rmcp_install_list
    <trim prefix="(" suffix=")" suffixOverrides=",">
      <if test="tenantId != null">
        tenant_id,
      </if>
      <if test="installPlanId != null">
        install_plan_id,
      </if>
      <if test="installPlanDataId != null">
        install_plan_data_id,
      </if>
      <if test="doorNo != null">
        door_no,
      </if>
      <if test="deviceId != null">
        device_id,
      </if>
      <if test="newBeginDegree != null">
        new_begin_degree,
      </if>
      <if test="oldEndDegree != null">
        old_end_degree,
      </if>
      <if test="oldImageUrl != null">
        old_image_url,
      </if>
      <if test="meterCode != null">
        meter_code,
      </if>
      <if test="userName != null">
        user_name,
      </if>
      <if test="userPhone != null">
        user_phone,
      </if>
      <if test="idCard != null">
        id_card,
      </if>
      <if test="isInstalled != null">
        is_installed,
      </if>
      <if test="isAccepted != null">
        is_accepted,
      </if>
      <if test="acceptTime != null">
        accept_time,
      </if>
      <if test="remark != null">
        remark,
      </if>
      <if test="status != null">
        `status`,
      </if>
      <if test="createDate != null">
        create_date,
      </if>
      <if test="updateDate != null">
        update_date,
      </if>
      <if test="createBy != null">
        create_by,
      </if>
      <if test="updateBy != null">
        update_by,
      </if>
    </trim>
    <trim prefix="values (" suffix=")" suffixOverrides=",">
      <if test="tenantId != null">
        #{tenantId},
      </if>
      <if test="installPlanId != null">
        #{installPlanId},
      </if>
      <if test="installPlanDataId != null">
        #{installPlanDataId},
      </if>
      <if test="doorNo != null">
        #{doorNo},
      </if>
      <if test="deviceId != null">
        #{deviceId},
      </if>
      <if test="newBeginDegree != null">
        #{newBeginDegree},
      </if>
      <if test="oldEndDegree != null">
        #{oldEndDegree},
      </if>
      <if test="oldImageUrl != null">
        #{oldImageUrl},
      </if>
      <if test="meterCode != null">
        #{meterCode},
      </if>
      <if test="userName != null">
        #{userName},
      </if>
      <if test="userPhone != null">
        #{userPhone},
      </if>
      <if test="idCard != null">
        #{idCard},
      </if>
      <if test="isInstalled != null">
        #{isInstalled},
      </if>
      <if test="isAccepted != null">
        #{isAccepted},
      </if>
      <if test="acceptTime != null">
        #{acceptTime},
      </if>
      <if test="remark != null">
        #{remark},
      </if>
      <if test="status != null">
        #{status},
      </if>
      <if test="createDate != null">
        #{createDate},
      </if>
      <if test="updateDate != null">
        #{updateDate},
      </if>
      <if test="createBy != null">
        #{createBy},
      </if>
      <if test="updateBy != null">
        #{updateBy},
      </if>
    </trim>
  </insert>
  <update id="updateByPrimaryKeySelective" parameterType="com.zcxk.rmcp.core.entity.InstallList">
    <!--@mbg.generated-->
    update rmcp_install_list
    <set>
      <if test="tenantId != null">
        tenant_id = #{tenantId},
      </if>
      <if test="installPlanId != null">
        install_plan_id = #{installPlanId},
      </if>
      <if test="installPlanDataId != null">
        install_plan_data_id = #{installPlanDataId},
      </if>
      <if test="doorNo != null">
        door_no = #{doorNo},
      </if>
      <if test="deviceId != null">
        device_id = #{deviceId},
      </if>
      <if test="newBeginDegree != null">
        new_begin_degree = #{newBeginDegree},
      </if>
      <if test="oldEndDegree != null">
        old_end_degree = #{oldEndDegree},
      </if>
      <if test="oldImageUrl != null">
        old_image_url = #{oldImageUrl},
      </if>
      <if test="meterCode != null">
        meter_code = #{meterCode},
      </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="isInstalled != null">
        is_installed = #{isInstalled},
      </if>
      <if test="isAccepted != null">
        is_accepted = #{isAccepted},
      </if>
      <if test="acceptTime != null">
        accept_time = #{acceptTime},
      </if>
      <if test="remark != null">
        remark = #{remark},
      </if>
      <if test="status != null">
        `status` = #{status},
      </if>
      <if test="createDate != null">
        create_date = #{createDate},
      </if>
      <if test="updateDate != null">
        update_date = #{updateDate},
      </if>
      <if test="createBy != null">
        create_by = #{createBy},
      </if>
      <if test="updateBy != null">
        update_by = #{updateBy},
      </if>
    </set>
    where id = #{id}
  </update>
  <insert id="batchInsert" keyColumn="id" keyProperty="id" parameterType="map" useGeneratedKeys="true">
    <!--@mbg.generated-->
    insert into rmcp_install_list
    (id, tenant_id, install_plan_id, install_plan_data_id, door_no, device_id, new_begin_degree,
    old_end_degree, old_image_url, meter_code, user_name, user_phone, id_card, is_installed,
    is_accepted, accept_time, remark, `status`, create_date, update_date, create_by,
    update_by)
    values
    <foreach collection="list" item="item" separator=",">
      (#{item.id}, #{item.tenantId}, #{item.installPlanId}, #{item.installPlanDataId}, #{item.doorNo},
      #{item.deviceId}, #{item.newBeginDegree}, #{item.oldEndDegree}, #{item.oldImageUrl},
      #{item.meterCode}, #{item.userName}, #{item.userPhone}, #{item.idCard}, #{item.isInstalled},
      #{item.isAccepted}, #{item.acceptTime}, #{item.remark}, #{item.status}, #{item.createDate},
      #{item.updateDate}, #{item.createBy}, #{item.updateBy})
    </foreach>
  </insert>

  <select id="getList" resultType="com.zcxk.rmcp.api.dto.install.InstallListDto">
    select ril.id,ril.install_plan_data_id,ril.device_id,ril.door_no,ril.is_installed,ril.is_accepted,rd.device_status
    from rmcp_install_list ril
    left join rmcp_device rd on ril.device_id = rd.id
    where ril.status = 1 and ril.install_plan_id = #{planId}
  </select>

  <select id="getInstallList" resultType="com.zcxk.rmcp.api.dto.install.InstallDeviceDto">
    select
    ril.id,
    rd.id as device_id,
    rd.device_no,
    rd.meter_no,
    rd.device_status,
    rd.address,
    rd.read_data,
    rd.valve_status,
    rd.last_receive_time,
    rd.user_name,
    rd.user_phone,
    rd.is_accept
    from rmcp_install_list ril
    left join rmcp_device rd on ril.device_id = rd.id
    where ril.status = 1 and rd.status = 1 and ril.install_plan_id = #{dto.id}
    <if test="userCondition.tenantId != null">and ril.tenant_id = #{userCondition.tenantId}</if>
    <if test="dto.deviceNo != null and dto.deviceNo != ''">and (rd.meter_no LIKE concat('%',#{dto.deviceNo},'%') or rd.file_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.isAccept != null">and rd.is_accept = #{dto.isAccept}</if>
    order by ril.create_date desc
  </select>

  <select id="getUnInstallList" resultType="com.zcxk.rmcp.api.vo.InstallListVo">
    select id,door_no,meter_code,user_name,user_phone,id_card from rmcp_install_list where status = 1 and install_plan_id = #{planId} and is_installed = 0
  </select>

  <select id="getUnInstallDeviceList" resultType="com.zcxk.rmcp.api.dto.install.InstallDeviceDto">

  </select>

  <select id="findById" resultMap="BaseResultMap">
    select <include refid="Base_Column_List">
  </include>
    from rmcp_install_list where status = 1 and id = #{id}
  </select>

  <update id="accept">
    UPDATE rmcp_install_list ril LEFT JOIN rmcp_device rd on (ril.device_id = rd.id)
    set ril.is_accepted = 1,ril.accept_time=NOW(),rd.is_accept = 1,rd.accept_date=NOW()
    WHERE ril.`status` = 1 and ril.is_accepted = 0
    and ril.id in <foreach collection="ids" item="item" open="(" separator="," close=")">#{item}</foreach>
  </update>

  <update id="delete">
    UPDATE rmcp_install_list ril LEFT JOIN rmcp_device rd on (ril.device_id = rd.id)
    set
    ril.is_installed=0,
    ril.is_accepted=0,
    ril.accept_time=null,
    ril.device_id=null,
    ril.update_date=now(),
    rd.is_install=0,
    rd.install_date=null,
    rd.is_accept=0,
    rd.accept_date=null,
    rd.update_date=now()
    WHERE ril.`status` = 1
    and ril.id in <foreach collection="ids" item="item" open="(" separator="," close=")">#{item}</foreach>
  </update>

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