<?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.analysis.SRaEmissionsStatisticsMapper">
  <resultMap id="BaseResultMap" type="cn.cecep.talroad.domain.analysis.SRaEmissionsStatistics">
    <!--@mbg.generated-->
    <!--@Table s_ra_emissions_statistics-->
    <id column="id" jdbcType="VARCHAR" property="id" />
    <result column="factory_id" jdbcType="VARCHAR" property="factoryId" />
    <result column="factory_name" jdbcType="VARCHAR" property="factoryName" />
    <result column="industry_id" jdbcType="VARCHAR" property="industryId" />
    <result column="industry_name" jdbcType="VARCHAR" property="industryName" />
    <result column="region_code" jdbcType="VARCHAR" property="regionCode" />
    <result column="region_name" jdbcType="VARCHAR" property="regionName" />
    <result column="problem_id" jdbcType="VARCHAR" property="problemId" />
    <result column="problem_name" jdbcType="VARCHAR" property="problemName" />
    <result column="problem_value" jdbcType="BIGINT" property="problemValue" />
    <result column="problem_time" jdbcType="TIMESTAMP" property="problemTime" />
  </resultMap>
  <sql id="Base_Column_List">
    <!--@mbg.generated-->
    id, factory_id, factory_name, industry_id, industry_name, region_code, region_name, problem_id,
    problem_name, problem_value, problem_time
  </sql>

  <select id="selectListAll" resultType="cn.cecep.talroad.vo.query.analysis.SREStatisticsQuery">
    SELECT
    SUM ( problem_value ) AS problem_value,
    <if test="type != null and type != '' and type=='trend'">
      problem_name ,
      to_char(problem_time, 'YYYY-MM-DD') AS  problemTimeStr
    </if>
    <if test="type != null and type != '' and type=='top'">
       factory_name
    </if>
    <if test="type != null and type != '' and type=='industry'">
      problem_name ,
      industry_name
    </if>
    FROM
      "s_ra_emissions_statistics"
    <where>
      <if test="problemTimeStart != null">
        AND problem_time &gt;= #{problemTimeStart,jdbcType=TIMESTAMP}
      </if>
      <if test="problemTimeEnd != null">
        AND problem_time &lt;= #{problemTimeEnd,jdbcType=TIMESTAMP}
      </if>
    </where>
    GROUP BY
    <if test="type != null and type != '' and type=='trend'">
      problem_name,
      to_char(problem_time, 'YYYY-MM-DD')
    </if>
    <if test="type != null and type != '' and type=='top'">
      factory_name
        ORDER BY problem_value DESC
      LIMIT 10
    </if>
    <if test="type != null and type != '' and type=='industry'">
      problem_name,
      industry_name
    </if>
    </select>

  <select id="selectFactoryProblemGroup" resultType="cn.cecep.talroad.domain.vo.ProblemGroupTimeCountVO">

    select t1.name,t1.dateTime,count(*) as totalCount
    from
      (select prpt.name, to_char(mapf.analyse_time,'YYYY-MM-DD')as dateTime, count(*)
       from
         m_am_problem_fi mapf
           inner join pc_ra_problem_type prpt on substring(mapf.problem_type,1,3) = prpt.id
           inner join pc_factory fc on mapf.factory_id = fc.id
          <if test="regionCodes != null and regionCodes.size() != 0">
            and fc.country_code IN
            <foreach collection="regionCodes" separator="," item="item" close=")" open="(">
              #{item,jdbcType=VARCHAR}
            </foreach>
          </if>
       where mapf.analyse_time  is not null
         and mapf.analyse_time >= #{problemTimeStart} and mapf.analyse_time &lt; #{problemTimeEnd}
       group by
         prpt."name",
         mapf.analyse_time
      )t1
    group by  t1."name",t1.dateTime
    order by
      t1.dateTime,t1."name"
  </select>

  <select id="selectProblemType" resultType="java.lang.String">
    select
      prpt.name
    from  pc_ra_problem_type prpt  where prpt.parent_id is null
  </select>

  <select id="selectTop10Factory" resultType="cn.cecep.talroad.vo.analysis.FactoryTop10ProblemCountVO">
    select
      count(*) as problemCount,
      pf.factory_name
    from m_am_problem_fi mapf
           inner join pc_factory pf on mapf.factory_id  = pf.id
          <if test="regionCodes != null and regionCodes.size() != 0">
            and pf.country_code IN
            <foreach collection="regionCodes" separator="," item="item" close=")" open="(">
              #{item,jdbcType=VARCHAR}
            </foreach>
          </if>
    where mapf.analyse_time >= #{problemTimeStart} and mapf.analyse_time &lt; #{problemTimeEnd}
    group by pf.factory_name
    order by problemCount desc
      limit 10
  </select>


  <select id="selectFactoryIndustryProblemGroup" resultType="cn.cecep.talroad.vo.analysis.FactoryIndustryProblemCountVO">
    select
      count(*) as totalCount,
      pf.industry_permission_name  ,
      prpt."name"
    from m_am_problem_fi mapf
           left join pc_factory pf on mapf.factory_id  = pf.id
           inner join (select count(*) as problemCount,
                              pf.industry_permission_name
                       from  m_am_problem_fi mapf
                               left join pc_factory pf on mapf.factory_id  = pf.id
                              <if test="regionCodes != null and regionCodes.size() != 0">
                                and pf.country_code IN
                                <foreach collection="regionCodes" separator="," item="item" close=")" open="(">
                                  #{item,jdbcType=VARCHAR}
                                </foreach>
                              </if>
                       where
                         pf.industry_permission_name  is not null
                          and mapf.analyse_time >= #{problemTimeStart} and mapf.analyse_time &lt; #{problemTimeEnd}
                       group by pf.industry_permission_name
                       order by problemCount desc
                         limit 10) t1 on pf.industry_permission_name  = t1.industry_permission_name
           inner join pc_ra_problem_type prpt on substring(mapf.problem_type,1,3) = prpt.id
    where mapf.analyse_time >= #{problemTimeStart} and mapf.analyse_time &lt; #{problemTimeEnd}
    group by pf.industry_permission_name,prpt."name"
    order by pf.industry_permission_name,prpt."name"
  </select>

  <select id="selectTop10FactoryIndustry" resultType="cn.cecep.talroad.vo.analysis.FactoryIndustryProblemCountVO">
    select count(*) as totalCount,
           pf.industry_permission_name
    from  m_am_problem_fi mapf
            left join pc_factory pf on mapf.factory_id  = pf.id
            <if test="regionCodes != null and regionCodes.size() != 0">
              and pf.country_code IN
              <foreach collection="regionCodes" separator="," item="item" close=")" open="(">
                #{item,jdbcType=VARCHAR}
              </foreach>
            </if>
    where
      pf.industry_permission_name  is not null
      and mapf.analyse_time >= #{problemTimeStart} and mapf.analyse_time &lt; #{problemTimeEnd}
    group by pf.industry_permission_name
    order by totalCount desc
      limit 10
  </select>
</mapper>