<?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="cn.cecep.talroad.mapper.BasDeviceSiteMapper">
  <resultMap id="BaseResultMap" type="cn.cecep.talroad.domain.BasDeviceSite">
    <!--@mbg.generated-->
    <!--@Table bas_device_site-->
    <id column="id" jdbcType="VARCHAR" property="id" />
    <result column="device_id" jdbcType="VARCHAR" property="deviceId" />
    <result column="name" jdbcType="VARCHAR" property="name" />
    <result column="point_lng" jdbcType="VARCHAR" property="pointLng" />
    <result column="point_lat" jdbcType="VARCHAR" property="pointLat" />
    <result column="status" jdbcType="VARCHAR" property="status" />
    <result column="type" jdbcType="VARCHAR" property="type" />
    <result column="equip_name" jdbcType="VARCHAR" property="equipName" />
    <result column="factory_id" jdbcType="VARCHAR" property="factoryId" />
    <result column="normal_value" jdbcType="VARCHAR" property="normalValue" />
    <result column="min_value" jdbcType="VARCHAR" property="minValue" />
    <result column="device_name" jdbcType="VARCHAR" property="deviceName" />
    <result column="is_stat" jdbcType="VARCHAR" property="isStat" />
  </resultMap>

  <resultMap id="BaseResultMapFactory" type="cn.cecep.talroad.vo.export.BasDeviceSiteExport">
    <!--@mbg.generated-->
    <!--@Table bas_device_site-->
    <id column="id" jdbcType="VARCHAR" property="id" />
    <result column="device_id" jdbcType="VARCHAR" property="deviceId" />
    <result column="factory_id" jdbcType="VARCHAR" property="factoryId" />
    <result column="name" jdbcType="VARCHAR" property="name" />
    <result column="point_lng" jdbcType="VARCHAR" property="pointLng" />
    <result column="point_lat" jdbcType="VARCHAR" property="pointLat" />
    <result column="device_name" jdbcType="VARCHAR" property="deviceName" />
    <result column="device_ype" jdbcType="VARCHAR" property="deviceType" />
  </resultMap>
  <sql id="Base_Column_List">
    <!--@mbg.generated-->
    id, device_id, "name", point_lng, point_lat, status, "type", equip_name, factory_id,normal_value,min_value,is_stat
  </sql>

  <select id="selectMapPoints" resultType="cn.cecep.talroad.vo.MapPointVo">
    select
      bds.id pointCode,
      bds."name" pointName,
      bds.point_lng ,
      bds.point_lat ,
      bds.status pointStatus
    from
      bas_device bd
        inner join bas_device_site bds on bd.device_id  = bds.device_id
    where
      bd.factory_id = #{factoryId}
    and bds."name"  is not null and bds.point_lng  is not null and bds.point_lat  is not  null and bds.point_lng!='' and bds.point_lat !=''
  </select>

  <select id="selectDeviceNameById" parameterType="cn.cecep.talroad.vo.export.BasDeviceSiteExport" resultMap="BaseResultMapFactory">
    select   mam.id as id,mam.name , mam.factory_id as factory_id,mam.device_id as device_id,mam.point_lng,mam.point_lat,poc.name as device_name, poc.work_step as device_type
    from  bas_device_site mam
            left join bas_device poc on   poc.device_id = mam.device_id
      ${ew.customSqlSegment}
  </select>

  <select id="selectBasDeviceSiteExportList" parameterType="cn.cecep.talroad.vo.export.BasDeviceSiteExport" resultMap="BaseResultMapFactory">
    select   mam.id as id,mam.name , mam.factory_id as factory_id,mam.device_id as device_id,mam.point_lng,mam.point_lat,poc.name as device_name, poc.work_step as device_type
    from  bas_device_site mam
            left join bas_device poc on   poc.device_id = mam.device_id
      ${ew.customSqlSegment}
  </select>


  <select id="selectBasDeviceSiteExportListT" parameterType="cn.cecep.talroad.vo.export.BasDeviceSiteExport" resultMap="BaseResultMapFactory">
    select   mam.id as id,mam.name , mam.factory_id as factory_id,mam.device_id as device_id,mam.point_lng,mam.point_lat,poc.name as device_name, poc.work_step as device_type
    from  bas_device_site mam
            left join bas_device poc on   poc.device_id = mam.device_id
      ${ew.customSqlSegment}
  </select>

  <select id="selectBasDeviceList" parameterType="cn.cecep.talroad.vo.export.BasDeviceSiteExport" resultMap="BaseResultMapFactory">
    select    factory_id,pollutant_name as device_name, pollutant_code as device_id
    from   bas_device
      ${ew.customSqlSegment}
  </select>

  <select id="getByExport" parameterType="cn.cecep.talroad.vo.export.BasDeviceSiteExport" resultMap="BaseResultMapFactory">
    select   mam.id as id,mam.name , mam.factory_id as factory_id,mam.device_id as device_id,mam.point_lng,mam.point_lat,poc.name as device_name, poc.work_step as device_type
    from  bas_device_site mam
            left join bas_device poc on   poc.device_id = mam.device_id
    where mam.id =#{id}
  </select>

  <select id="selectExportBasDeviceList" resultType="cn.cecep.talroad.vo.BasDeviceSiteExcelVo">
    SELECT
    bds."name" name,
    bds.type type,
    bd.name deviceName,
    bds.normal_value normalValue
    FROM
    bas_device bd
    inner join bas_device_site bds on bd.device_id  = bds.device_id
    <where>
      <if test="name != null and name!=''">and  bds.name like concat('%', #{name}, '%')</if>
      <if test="factoryId != null ">and  bds.factory_id = #{factoryId}</if>
      <if test="type != null and type!=''">and  bds.type = #{type}</if>
      <if test="deviceName != null and deviceName!=''">and  bd.name = #{deviceName}</if>
    </where>
  </select>
    <select id="selectCountOverview" resultType="cn.cecep.talroad.vo.BasDeviceSiteVo$CountOverview">
      SELECT
        count(*) as count,
        count(CASE WHEN t1.type = '1' THEN 1 END) as scCount,
        count(CASE WHEN t1.type = '2' THEN 1 END) as zlCount
      FROM bas_device_site t1
      INNER JOIN pc_factory t3 ON t1.factory_id = t3.id
      <where>
        AND t3.country_code IN
        <foreach collection="regionCodeList" open="(" separator="," close=")" item="item">
          #{item}
        </foreach>
      </where>
    </select>
  <select id="selectScatterChart" resultType="java.util.Map">
    <if test='type == "1"'>
      SELECT
        t3.country_code as code,
        t3.country_name as name,
        count(*) as count
      FROM bas_device_site t1
      INNER JOIN pc_factory t3 ON t1.factory_id = t3.id
      WHERE t3.country_code IN
      <foreach collection="regionCodeList" open="(" separator="," close=")" item="item">
        #{item}
      </foreach>
      GROUP BY t3.country_code, t3.country_name
      ORDER BY t3.country_code
    </if>
    <if test='type == "2"'>
      SELECT
        t4.indus_code as code,
        t4.indus_name as name,
        count(*) as count
      FROM bas_device_site t1
      INNER JOIN pc_factory t3 ON t1.factory_id = t3.id
      INNER JOIN pc_industry t4 ON t4.indus_code = t3.industry_category
      <where>
        AND t3.country_code IN
        <foreach collection="regionCodeList" open="(" separator="," close=")" item="item">
          #{item}
        </foreach>
      </where>
      GROUP BY t4.indus_code, t4.indus_name
      ORDER BY t4.indus_code
    </if>
  </select>

  <select id="selectDevicePoints" resultType="cn.cecep.talroad.domain.BasDeviceSite">
    select
      bds.id pointCode,
      bds.device_id deviceId,
      bds."name" pointName,
      bds.point_lng ,
      bds.point_lat ,
      bds.status pointStatus
    from
      bas_device bd
        inner join bas_device_site bds on bd.device_id  = bds.device_id
    where
      bd.factory_id = #{factoryId}
      and bds."name"  is not null and bds.point_lng  is not null and bds.point_lat  is not  null
  </select>
</mapper>
