<?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.BCoDischargePlanMapper">
  <resultMap id="BaseResultMap" type="cn.cecep.talroad.domain.analysis.BCoDischargePlan">
    <!--@mbg.generated-->
    <!--@Table b_co_discharge_plan-->
    <result column="plan_id" jdbcType="INTEGER" property="planId" />
    <result column="factory_id" jdbcType="VARCHAR" property="factoryId" />
    <result column="plan_number" jdbcType="INTEGER" property="planNumber" />
    <result column="report_status" jdbcType="SMALLINT" property="reportStatus" />
    <result column="create_time" jdbcType="TIMESTAMP" property="createTime" />
    <result column="create_user" jdbcType="VARCHAR" property="createUser" />
    <result column="delete_status" jdbcType="SMALLINT" property="deleteStatus" />
    <result column="factory_name" jdbcType="VARCHAR" property="factoryName" />
    <result column="country_name" jdbcType="VARCHAR" property="countryName" />
    <result column="industry_category_name" jdbcType="VARCHAR" property="industryCategoryName" />
  </resultMap>
  <sql id="Base_Column_List">
    <!--@mbg.generated-->
    plan_id, factory_id, plan_number, report_status, create_time, create_user, delete_status, 
    factory_name, country_name, industry_category_name
  </sql>
  <insert id="batchInsert" parameterType="map">
    <!--@mbg.generated-->
    insert into b_co_discharge_plan
    (plan_id, factory_id, plan_number, report_status, create_time, create_user, delete_status, 
      factory_name, country_name, industry_category_name)
    values
    <foreach collection="list" item="item" separator=",">
      (#{item.planId,jdbcType=INTEGER}, #{item.factoryId,jdbcType=VARCHAR}, #{item.planNumber,jdbcType=INTEGER}, 
        #{item.reportStatus,jdbcType=SMALLINT}, #{item.createTime,jdbcType=TIMESTAMP}, 
        #{item.createUser,jdbcType=VARCHAR}, #{item.deleteStatus,jdbcType=SMALLINT}, #{item.factoryName,jdbcType=VARCHAR}, 
        #{item.countryName,jdbcType=VARCHAR}, #{item.industryCategoryName,jdbcType=VARCHAR}
        )
    </foreach>
  </insert>

  <select id="newPlanName" resultMap="BaseResultMap">
    SELECT
      pf.factory_name,
      pf.country_name,
      ii.NAME industry_category_name,
      pf.ID AS factory_id
    FROM
      pc_factory pf
        LEFT JOIN iot_industry ii ON ii.code = pf.industry_category
    WHERE pf.id=#{factoryId,jdbcType=VARCHAR}
    </select>

  <select id="selectCoAllInfo" resultType="cn.cecep.talroad.vo.analysis.CoPlanAllInfo">
    SELECT
        bpi.id,
        bpi.plan_id,
        bpi.type,
        bpi.name,
        bpi.device_id,
        bpi.plan_start_time,
        bpi.plan_end_time,
        bpi.remarks,
        bdp.create_time,
        bdp.create_user,
        bdp.factory_name,bdp.country_name,
        bdp.industry_category_name,
        COALESCE(bdsa.mon_status,'正常') status,
        COALESCE(bdsa.coal_gas_status,'0') statusSourceMQ
    FROM
      "b_co_discharge_plan_info" bpi
        LEFT JOIN b_co_discharge_plan bdp ON bdp.plan_id = bpi.plan_id
        LEFT JOIN bas_device_sal_analysis bdsa ON bdsa.device_id=bpi.device_id
        LEFT JOIN pc_factory pf ON bdp.factory_id=pf.id
    WHERE bdp.report_status=1
    <if test="info.factoryName != null and info.factoryName.trim()!=''">
      AND bdp.factory_name like  CONCAT ('%',#{info.factoryName,jdbcType=VARCHAR},'%')
    </if>
    <if test="info.industryCategoryName != null and info.industryCategoryName.trim()!=''">
      AND bdp.industry_category_name like  CONCAT ('%',#{info.industryCategoryName,jdbcType=VARCHAR},'%')
    </if>
    <if test="info.countryName != null and info.countryName.trim()!=''">
    AND bdp.country_name like  CONCAT ('%',#{info.countryName,jdbcType=VARCHAR},'%')
    </if>
    <if test="info.regionCodes != null and info.regionCodes.size() != 0">
      and pf.country_code IN
      <foreach collection="info.regionCodes" separator="," item="item" close=")" open="(">
        #{item,jdbcType=VARCHAR}
      </foreach>
    </if>
    <if test="info.status != null and info.status.trim()!=0">
      AND COALESCE(bdsa.coal_gas_status,'0'  ) !='0'
    </if>
    <if test="info.status != null and info.status.trim()==0">
      AND COALESCE(bdsa.coal_gas_status,'0'  ) ='0'
    </if>
    <if test="info.createStartTime != null">
      AND bdp.create_time &gt;= #{info.createStartTime,jdbcType=TIMESTAMP}
    </if>
    <if test="info.createEndTime != null">
      AND bdp.create_time &lt;= #{info.createEndTime,jdbcType=TIMESTAMP}
    </if>
  </select>
</mapper>