<?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.bz.smart_city.dao.CollectorMapper">
  <resultMap id="BaseResultMap" type="com.bz.smart_city.entity.Collector">
    <!--@mbg.generated-->
    <!--@Table sc_collector-->
    <id column="id" property="id" />
    <result column="site_id" property="siteId" />
    <result column="customer_id" property="customerId" />
    <result column="building_id" property="buildingId" />
    <result column="collector_no" property="collectorNo" />
    <result column="concentrator_id" property="concentratorId" />
    <result column="device_type" property="deviceType" />
    <result column="address" property="address" />
    <result column="status" property="status" />
    <result column="remark" property="remark" />
    <result column="date_create" property="dateCreate" />
    <result column="date_update" property="dateUpdate" />
    <result column="create_by" property="createBy" />
    <result column="update_by" property="updateBy" />
  </resultMap>
  <sql id="Base_Column_List">
    <!--@mbg.generated-->
    id, site_id, customer_id, building_id, collector_no, concentrator_id, device_type, 
    address, `status`, remark, date_create, date_update, create_by, update_by
  </sql>
  <insert id="insertSelective" keyColumn="id" keyProperty="id" parameterType="com.bz.smart_city.entity.Collector" useGeneratedKeys="true">
    <!--@mbg.generated-->
    insert into sc_collector
    <trim prefix="(" suffix=")" suffixOverrides=",">
      <if test="siteId != null">
        site_id,
      </if>
      <if test="customerId != null">
        customer_id,
      </if>
      <if test="buildingId != null">
        building_id,
      </if>
      <if test="collectorNo != null">
        collector_no,
      </if>
      <if test="concentratorId != null">
        concentrator_id,
      </if>
      <if test="deviceType != null">
        device_type,
      </if>
      <if test="address != null">
        address,
      </if>
      <if test="status != null">
        `status`,
      </if>
      <if test="remark != null">
        remark,
      </if>
      <if test="dateCreate != null">
        date_create,
      </if>
      <if test="dateUpdate != null">
        date_update,
      </if>
      <if test="createBy != null">
        create_by,
      </if>
      <if test="updateBy != null">
        update_by,
      </if>
    </trim>
    <trim prefix="values (" suffix=")" suffixOverrides=",">
      <if test="siteId != null">
        #{siteId},
      </if>
      <if test="customerId != null">
        #{customerId},
      </if>
      <if test="buildingId != null">
        #{buildingId},
      </if>
      <if test="collectorNo != null">
        #{collectorNo},
      </if>
      <if test="concentratorId != null">
        #{concentratorId},
      </if>
      <if test="deviceType != null">
        #{deviceType},
      </if>
      <if test="address != null">
        #{address},
      </if>
      <if test="status != null">
        #{status},
      </if>
      <if test="remark != null">
        #{remark},
      </if>
      <if test="dateCreate != null">
        #{dateCreate},
      </if>
      <if test="dateUpdate != null">
        #{dateUpdate},
      </if>
      <if test="createBy != null">
        #{createBy},
      </if>
      <if test="updateBy != null">
        #{updateBy},
      </if>
    </trim>
  </insert>
  <update id="updateByPrimaryKeySelective" parameterType="com.bz.smart_city.entity.Collector">
    <!--@mbg.generated-->
    update sc_collector
    <set>
      <if test="siteId != null">
        site_id = #{siteId},
      </if>
      <if test="customerId != null">
        customer_id = #{customerId},
      </if>
      <if test="buildingId != null">
        building_id = #{buildingId},
      </if>
      <if test="collectorNo != null">
        collector_no = #{collectorNo},
      </if>
      <if test="concentratorId != null">
        concentrator_id = #{concentratorId},
      </if>
      <if test="deviceType != null">
        device_type = #{deviceType},
      </if>
      <if test="address != null">
        address = #{address},
      </if>
      <if test="status != null">
        `status` = #{status},
      </if>
      <if test="remark != null">
        remark = #{remark},
      </if>
      <if test="dateCreate != null">
        date_create = #{dateCreate},
      </if>
      <if test="dateUpdate != null">
        date_update = #{dateUpdate},
      </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 sc_collector
    (site_id, customer_id, building_id, collector_no, concentrator_id, device_type, address, 
      `status`, remark, date_create, date_update, create_by, update_by)
    values
    <foreach collection="list" item="item" separator=",">
      (#{item.siteId}, #{item.customerId}, #{item.buildingId}, #{item.collectorNo}, #{item.concentratorId}, 
        #{item.deviceType}, #{item.address}, #{item.status}, #{item.remark}, #{item.dateCreate}, 
        #{item.dateUpdate}, #{item.createBy}, #{item.updateBy})
    </foreach>
  </insert>

  <select id="getList" resultType="com.bz.smart_city.dto.CollectorDto">
    select
    soll.*,
    scon.serial_number,
    sb.name building_name,
    scu.customer_name,
    sb.province,
    sb.city,
    sb.region,
    sb.community
    from sc_collector soll
    left join sc_concentrator scon on(scon.id = soll.concentrator_id)
    left join sc_building sb on(sb.id = soll.building_id)
    left join sc_customer scu on(scu.id = soll.customer_id)
    where soll.status = 1
    <if test="siteId != null"> and soll.site_id = #{siteId}</if>
    <if test="concentratorId != null"> and soll.concentrator_id = #{concentratorId}</if>
    <if test="collectorNo != null and collectorNo != ''"> and soll.collector_no LIKE concat('%',#{collectorNo},'%')</if>
    <if test="serialNumber != null and serialNumber != ''"> and scon.serial_number LIKE concat('%',#{serialNumber},'%')</if>
    <if test="address != null and address != ''"> and soll.address LIKE concat('%',#{address},'%')</if>
    <if test="provinceId != null"> and sb.province = #{provinceId}</if>
    <if test="cityId != null"> and sb.city = #{cityId}</if>
    <if test="regionId != null"> and sb.region = #{regionId}</if>
    <if test="communityId != null"> and sb.community = #{communityId}</if>
    <if test="buildingId != null"> and sb.id = #{buildingId}</if>
    <if test="customerIds != null and customerIds.size() != 0">
      and soll.customer_id in <foreach collection="customerIds" item="item" open="(" separator="," close=")">#{item}</foreach>
    </if>
    order by soll.date_create desc
  </select>
  <select id="findList" resultType="com.bz.smart_city.entity.Collector">
    select id,collector_no from sc_collector where status = 1 and concentrator_id = #{concentratorId}
  </select>

  <select id="getAreaList" resultType="com.bz.smart_city.dto.BuildingSelectInfoDto">
    select
    b.id,
    b.name,
    b.province,
    b.city,
    b.region,
    b.community,
    b.longitude,
    b.latitude,
    t1.device_count
    from sc_building b
    right join (
    select soll.building_id,count(1) as device_count
    from sc_collector soll
    left join sc_concentrator scon on(scon.id = soll.concentrator_id)
    where soll.status = 1
    <if test="siteId != null"> and soll.site_id = #{siteId}</if>
    <if test="collectorNo != null and collectorNo != ''"> and soll.collector_no LIKE concat('%',#{collectorNo},'%')</if>
    <if test="serialNumber != null and serialNumber != ''"> and scon.serial_number LIKE concat('%',#{serialNumber},'%')</if>
    <if test="address != null and address != ''"> and soll.address LIKE concat('%',#{address},'%')</if>
    <if test="customerIds != null and customerIds.size() != 0">
      and soll.customer_id in <foreach collection="customerIds" item="item" open="(" separator="," close=")">#{item}</foreach>
    </if>
    GROUP BY soll.building_id
    ) t1 on (t1.building_id = b.id)
    where b.status = 1
  </select>

  <select id="countByConcentratorId" resultType="java.lang.Integer">
    select count(1) from sc_collector where status = 1 and concentrator_id = #{concentratorId}
  </select>

  <select id="findByCollectorNoUnique" resultType="int">
    select count(1) from sc_collector where status = 1 and concentrator_id = #{concentratorId} and collector_no = #{collectorNo}
    <if test="id != null"> and id != #{id}</if>
  </select>

  <select id="findName" resultMap="BaseResultMap">
    select id,collector_no from sc_collector where status = 1 and concentrator_id = #{concentratorId} and collector_no = #{collectorNo}
  </select>
</mapper>