<?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.BaseControlConcentStandardsMapper">
  <resultMap id="BaseResultMap" type="cn.cecep.talroad.domain.BaseControlConcentStandards">
    <!--@mbg.generated-->
    <!--@Table base_control_concent_standards-->
    <id column="id" jdbcType="VARCHAR" property="id" />
    <result column="factory_id" jdbcType="VARCHAR" property="factoryId" />
    <result column="pollutant_type" jdbcType="VARCHAR" property="pollutantType" />
    <result column="control_standard" jdbcType="NUMERIC" property="controlStandard" />
    <result column="include_calculations" jdbcType="VARCHAR" property="includeCalculations" />
    <result column="create_time" jdbcType="DATE" property="createTime" />
    <result column="update_time" jdbcType="DATE" property="updateTime" />
    <result column="outlet_id" jdbcType="VARCHAR" property="outletId" />
  </resultMap>
  <sql id="Base_Column_List">
    <!--@mbg.generated-->
    id, factory_id, pollutant_type, control_standard, include_calculations, create_time, 
    update_time, outlet_id
  </sql>
  <select id="selectByPrimaryKey" parameterType="java.lang.String" resultMap="BaseResultMap">
    <!--@mbg.generated-->
    select 
    <include refid="Base_Column_List" />
    from base_control_concent_standards
    where id = #{id,jdbcType=VARCHAR}
  </select>
  <delete id="deleteByPrimaryKey" parameterType="java.lang.String">
    <!--@mbg.generated-->
    delete from base_control_concent_standards
    where id = #{id,jdbcType=VARCHAR}
  </delete>
  <insert id="insert" parameterType="cn.cecep.talroad.domain.BaseControlConcentStandards">
    <!--@mbg.generated-->
    insert into base_control_concent_standards (id, factory_id, pollutant_type, 
      control_standard, include_calculations, create_time, 
      update_time, outlet_id)
    values (#{id,jdbcType=VARCHAR}, #{factoryId,jdbcType=VARCHAR}, #{pollutantType,jdbcType=VARCHAR}, 
      #{controlStandard,jdbcType=NUMERIC}, #{includeCalculations,jdbcType=VARCHAR}, #{createTime,jdbcType=DATE}, 
      #{updateTime,jdbcType=DATE}, #{outletId,jdbcType=VARCHAR})
  </insert>
  <insert id="insertSelective" parameterType="cn.cecep.talroad.domain.BaseControlConcentStandards">
    <!--@mbg.generated-->
    insert into base_control_concent_standards
    <trim prefix="(" suffix=")" suffixOverrides=",">
      <if test="id != null">
        id,
      </if>
      <if test="factoryId != null">
        factory_id,
      </if>
      <if test="pollutantType != null">
        pollutant_type,
      </if>
      <if test="controlStandard != null">
        control_standard,
      </if>
      <if test="includeCalculations != null">
        include_calculations,
      </if>
      <if test="createTime != null">
        create_time,
      </if>
      <if test="updateTime != null">
        update_time,
      </if>
      <if test="outletId != null">
        outlet_id,
      </if>
    </trim>
    <trim prefix="values (" suffix=")" suffixOverrides=",">
      <if test="id != null">
        #{id,jdbcType=VARCHAR},
      </if>
      <if test="factoryId != null">
        #{factoryId,jdbcType=VARCHAR},
      </if>
      <if test="pollutantType != null">
        #{pollutantType,jdbcType=VARCHAR},
      </if>
      <if test="controlStandard != null">
        #{controlStandard,jdbcType=NUMERIC},
      </if>
      <if test="includeCalculations != null">
        #{includeCalculations,jdbcType=VARCHAR},
      </if>
      <if test="createTime != null">
        #{createTime,jdbcType=DATE},
      </if>
      <if test="updateTime != null">
        #{updateTime,jdbcType=DATE},
      </if>
      <if test="outletId != null">
        #{outletId,jdbcType=VARCHAR},
      </if>
    </trim>
  </insert>
  <update id="updateByPrimaryKeySelective" parameterType="cn.cecep.talroad.domain.BaseControlConcentStandards">
    <!--@mbg.generated-->
    update base_control_concent_standards
    <set>
      <if test="factoryId != null">
        factory_id = #{factoryId,jdbcType=VARCHAR},
      </if>
      <if test="pollutantType != null">
        pollutant_type = #{pollutantType,jdbcType=VARCHAR},
      </if>
      <if test="controlStandard != null">
        control_standard = #{controlStandard,jdbcType=NUMERIC},
      </if>
      <if test="includeCalculations != null">
        include_calculations = #{includeCalculations,jdbcType=VARCHAR},
      </if>
      <if test="createTime != null">
        create_time = #{createTime,jdbcType=DATE},
      </if>
      <if test="updateTime != null">
        update_time = #{updateTime,jdbcType=DATE},
      </if>
      <if test="outletId != null">
        outlet_id = #{outletId,jdbcType=VARCHAR},
      </if>
    </set>
    where id = #{id,jdbcType=VARCHAR}
  </update>
  <update id="updateByPrimaryKey" parameterType="cn.cecep.talroad.domain.BaseControlConcentStandards">
    <!--@mbg.generated-->
    update base_control_concent_standards
    set factory_id = #{factoryId,jdbcType=VARCHAR},
      pollutant_type = #{pollutantType,jdbcType=VARCHAR},
      control_standard = #{controlStandard,jdbcType=NUMERIC},
      include_calculations = #{includeCalculations,jdbcType=VARCHAR},
      create_time = #{createTime,jdbcType=DATE},
      update_time = #{updateTime,jdbcType=DATE},
      outlet_id = #{outletId,jdbcType=VARCHAR}
    where id = #{id,jdbcType=VARCHAR}
  </update>
  <update id="updateBatchSelective" parameterType="java.util.List">
    <!--@mbg.generated-->
    update base_control_concent_standards
    <trim prefix="set" suffixOverrides=",">
      <trim prefix="factory_id = case" suffix="end,">
        <foreach collection="list" index="index" item="item">
          <if test="item.factoryId != null">
            when id = #{item.id,jdbcType=VARCHAR} then #{item.factoryId,jdbcType=VARCHAR}
          </if>
        </foreach>
      </trim>
      <trim prefix="pollutant_type = case" suffix="end,">
        <foreach collection="list" index="index" item="item">
          <if test="item.pollutantType != null">
            when id = #{item.id,jdbcType=VARCHAR} then #{item.pollutantType,jdbcType=VARCHAR}
          </if>
        </foreach>
      </trim>
      <trim prefix="control_standard = case" suffix="end,">
        <foreach collection="list" index="index" item="item">
          <if test="item.controlStandard != null">
            when id = #{item.id,jdbcType=VARCHAR} then #{item.controlStandard,jdbcType=NUMERIC}
          </if>
        </foreach>
      </trim>
      <trim prefix="include_calculations = case" suffix="end,">
        <foreach collection="list" index="index" item="item">
          <if test="item.includeCalculations != null">
            when id = #{item.id,jdbcType=VARCHAR} then #{item.includeCalculations,jdbcType=VARCHAR}
          </if>
        </foreach>
      </trim>
      <trim prefix="create_time = case" suffix="end,">
        <foreach collection="list" index="index" item="item">
          <if test="item.createTime != null">
            when id = #{item.id,jdbcType=VARCHAR} then #{item.createTime,jdbcType=DATE}
          </if>
        </foreach>
      </trim>
      <trim prefix="update_time = case" suffix="end,">
        <foreach collection="list" index="index" item="item">
          <if test="item.updateTime != null">
            when id = #{item.id,jdbcType=VARCHAR} then #{item.updateTime,jdbcType=DATE}
          </if>
        </foreach>
      </trim>
      <trim prefix="outlet_id = case" suffix="end,">
        <foreach collection="list" index="index" item="item">
          <if test="item.outletId != null">
            when id = #{item.id,jdbcType=VARCHAR} then #{item.outletId,jdbcType=VARCHAR}
          </if>
        </foreach>
      </trim>
      <trim prefix="control_points = case" suffix="end,">
        <foreach collection="list" index="index" item="item">
          <if test="item.controlPoints != null">
            when id = #{item.id,jdbcType=VARCHAR} then #{item.controlPoints}
          </if>
        </foreach>
      </trim>
    </trim>
    where id in
    <foreach close=")" collection="list" item="item" open="(" separator=", ">
      #{item.id,jdbcType=VARCHAR}
    </foreach>
  </update>
    <update id="updateAdviceEmissionAvg">
      <foreach collection="standards" separator=";" item="item">
        UPDATE base_control_concent_standards SET advice_emission_avg = #{item.adviceEmissionAvg} WHERE id = #{item.id}
      </foreach>
    </update>
    <insert id="batchInsert" parameterType="map">
    <!--@mbg.generated-->
    insert into base_control_concent_standards
    (id, factory_id, pollutant_type, control_standard, include_calculations, create_time, 
      update_time, outlet_id,control_points)
    values
    <foreach collection="list" item="item" separator=",">
      (#{item.id,jdbcType=VARCHAR}, #{item.factoryId,jdbcType=VARCHAR}, #{item.pollutantType,jdbcType=VARCHAR}, 
        #{item.controlStandard,jdbcType=NUMERIC}, #{item.includeCalculations,jdbcType=VARCHAR}, 
        #{item.createTime,jdbcType=DATE}, #{item.updateTime,jdbcType=DATE}, #{item.outletId,jdbcType=VARCHAR},#{item.controlPoints}
        )
    </foreach>
<!--    ON CONFLICT(factory_id, pollutant_type,outlet_id) do update set-->
<!--    include_calculations = excluded.include_calculations,-->
<!--    control_standard = excluded.control_standard,update_time=now()-->

  </insert>

  <select id="selectListPage" resultType="cn.cecep.talroad.vo.BaseControlConcentStandardsVo">
    SELECT
    plf.factory_id,
    bccs.id,
    pf.factory_name,
    pf.country_name,
    plv1.arguments_name AS industryCategoryName,
    plv1.arguments_code AS industryCategoryCode,
    po.monitoring_point_name,
    po.id outletId,
    pp.poll_code AS pollutantType,
    pp.poll_name,
    bccs.control_standard,
    bccs."include_calculations",
    bccs.control_points
    FROM
    <!--获取位大气监管企业的名单-->
    ( SELECT "id", factory_id FROM pc_label_factory WHERE label_id = '502' AND label_value = '1' ) plf
    <!--关联企业表-->
    INNER JOIN pc_factory pf ON plf.factory_id = pf."id"
    <!--关联排口，获取监测点位名称等信息-->
    INNER JOIN (
        SELECT
        t1."id",
        t1.monitoring_point_name,
        t1.factory_id
        FROM
        pc_outlet t1

        WHERE
        t1.pollutant_type = '4'
        AND t1.pollutant_outlet_type='1'
        AND t1.is_status='1'
        AND t1.monitoring_point_name not like CONCAT( '%','入口','%')
        AND t1.monitoring_point_name not like CONCAT( '%','进口','%')
    ) po ON po.factory_id = pf."id"
    <!--关联标签获取行业-->
    LEFT JOIN ( SELECT factory_id, label_value FROM pc_label_factory WHERE label_id = '501' ) plf1 ON plf1.factory_id = pf."id"
    LEFT JOIN ( SELECT * FROM pc_label_value WHERE label_id = '501' ) plv1 ON plf1.label_value = plv1.arguments_code
    <!--关联点位实际监测污染物 关联污染物种类-->
    LEFT JOIN ( SELECT po.ID, po.poll_name, po.poll_code,pfp.outlet_id FROM pc_pollutant  as po INNER JOIN
    pc_factory_pollutant as pfp  on pfp.pollutant_id=po.poll_code WHERE is_gas_type = '1' and pfp.is_status='1' ) pp ON pp.outlet_id = po.id
    <!--关联用户配置表-->
    LEFT JOIN base_control_concent_standards bccs ON bccs.factory_id = pf."id" AND bccs.outlet_id = po.id AND bccs.pollutant_type = pp.poll_code
    <where>
    <if test="query.factoryName != null and query.factoryName.trim() != ''">
        AND  pf.factory_name LIKE CONCAT( '%',#{query.factoryName,jdbcType=VARCHAR},'%')
      </if>
      <if test="query.monitoringPointName != null and query.monitoringPointName.trim()!=''">
        AND  po.monitoring_point_name = #{query.monitoringPointName,jdbcType=VARCHAR}
      </if>
      <if test="query.pollutantType != null and query.pollutantType.trim() != ''">
        AND  pp.poll_code = #{query.pollutantType,jdbcType=VARCHAR}
      </if>
      <if test="query.countryCode != null and query.countryCode.trim() != ''">
        AND pf.country_code = #{query.countryCode,jdbcType=VARCHAR}
      </if>
      <if test="query.industryCategoryCode != null and query.industryCategoryCode.trim() != ''">
        AND  plv1.arguments_code = #{query.industryCategoryCode,jdbcType=VARCHAR}
      </if>
      <if test="query.includeCalculations != null and query.includeCalculations.trim() != ''">
        AND  bccs."include_calculations" = #{query.includeCalculations,jdbcType=VARCHAR}
      </if>
      <if test="query.controlPoints != null and query.controlPoints.trim() != ''">
        AND  bccs."control_points" = #{query.controlPoints,jdbcType=VARCHAR}
      </if>
    </where>
    ORDER BY
      pf.factory_name,
      po.id,
      pp.poll_code
  </select>

  <select id="selectWarningStandards" resultType="cn.cecep.talroad.domain.EarlyWarning">
    SELECT
      plf.factory_id,
      pf.factory_name,
      pf.country_name,
      pf.country_code,
      plv1.arguments_name AS industryName,
      plv1.arguments_code AS industryCode,
      po.monitoring_point_name as stationName,
      po.id  as stationId,
      pp.poll_code AS pollCode,
      pp.poll_name,
      bccs.control_standard
    FROM
      ( SELECT "id", factory_id FROM pc_label_factory WHERE label_id = '502' AND label_value = '1' ) plf
        INNER JOIN pc_factory pf ON plf.factory_id = pf."id"
        INNER JOIN (
        SELECT
          t1."id",
          t1.monitoring_point_name,
          t1.factory_id
        FROM
          pc_outlet t1

        WHERE
          t1.pollutant_type = '4'
          AND t1.pollutant_outlet_type='1'
      ) po ON po.factory_id = pf."id"
        LEFT JOIN ( SELECT factory_id, label_value FROM pc_label_factory WHERE label_id = '501' ) plf1 ON plf1.factory_id = pf."id"
        LEFT JOIN ( SELECT * FROM pc_label_value WHERE label_id = '501' ) plv1 ON plf1.label_value = plv1.arguments_code
        LEFT JOIN ( SELECT ID, poll_name, poll_code FROM pc_pollutant WHERE is_gas_type = '1' ) pp ON 1 = 1
        inner JOIN base_control_concent_standards bccs ON bccs.factory_id = pf."id" AND bccs.outlet_id = po.id AND bccs.pollutant_type = pp.poll_code
    where bccs.include_calculations='1'
  </select>

  <delete id="deletePollutant">
        DELETE from pc_factory_pollutant where pollutant_id=#{code} and outlet_id in (
          SELECT

            po.id outletId

          FROM

            ( SELECT "id", factory_id FROM pc_label_factory WHERE label_id = '502' AND label_value = '1' ) plf

              INNER JOIN pc_factory pf ON plf.factory_id = pf."id"

              INNER JOIN (
              SELECT
                t1."id",
                t1.monitoring_point_name,
                t1.factory_id
              FROM
                pc_outlet t1

              WHERE
                t1.pollutant_type = '4'
                AND t1.pollutant_outlet_type='1'
                AND t1.is_status='1'
                AND t1.monitoring_point_name not like CONCAT( '%','入口','%')
                AND t1.monitoring_point_name not like CONCAT( '%','进口','%')
            ) po ON po.factory_id = pf."id"

              LEFT JOIN ( SELECT factory_id, label_value FROM pc_label_factory WHERE label_id = '501' ) plf1 ON plf1.factory_id = pf."id"
              LEFT JOIN ( SELECT * FROM pc_label_value WHERE label_id = '501' ) plv1 ON plf1.label_value = plv1.arguments_code

        )
  </delete>

  <select id="addPollutant" resultType="cn.cecep.talroad.domain.PcFactoryPollutant">
    SELECT
    po.id outletId,
    po.factory_id factoryId
    FROM

    ( SELECT "id", factory_id FROM pc_label_factory WHERE label_id = '502' AND label_value = '1' ) plf

    INNER JOIN pc_factory pf ON plf.factory_id = pf."id"

    INNER JOIN (
    SELECT
    t1."id",
    t1.monitoring_point_name,
    t1.factory_id
    FROM
    pc_outlet t1

    WHERE
    t1.pollutant_type = '4'
    AND t1.pollutant_outlet_type='1'
    AND t1.is_status='1'
    AND t1.monitoring_point_name not like CONCAT( '%','入口','%')
    AND t1.monitoring_point_name not like CONCAT( '%','进口','%')
    <if test="type==2">
      AND t1.monitoring_point_name  not like CONCAT( '%','单烟尘','%')
    </if>
    ) po ON po.factory_id = pf."id"

    LEFT JOIN ( SELECT factory_id, label_value FROM pc_label_factory WHERE label_id = '501' ) plf1 ON plf1.factory_id = pf."id"
    LEFT JOIN ( SELECT * FROM pc_label_value WHERE label_id = '501' ) plv1 ON plf1.label_value = plv1.arguments_code

  </select>
</mapper>