<?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.BMaintenancePlanInfoMapper">
  <resultMap id="BaseResultMap" type="cn.cecep.talroad.domain.analysis.BMaintenancePlanInfo">
    <!--@mbg.generated-->
    <!--@Table b_maintenance_plan_info-->
    <id column="id" jdbcType="VARCHAR" property="id" />
    <result column="plan_id" jdbcType="INTEGER" property="planId" />
    <result column="type" jdbcType="VARCHAR" property="type" />
    <result column="name" jdbcType="VARCHAR" property="name" />
    <result column="device_id" jdbcType="VARCHAR" property="deviceId" />
    <result column="plan_start_time" jdbcType="TIMESTAMP" property="planStartTime" />
    <result column="plan_end_time" jdbcType="TIMESTAMP" property="planEndTime" />
    <result column="remarks" jdbcType="VARCHAR" property="remarks" />
    <result column="create_time" jdbcType="TIMESTAMP" property="createTime" />
    <result column="update_time" jdbcType="TIMESTAMP" property="updateTime" />
  </resultMap>
  <sql id="Base_Column_List">
    <!--@mbg.generated-->
    id, plan_id, "type", "name", device_id, plan_start_time, plan_end_time, remarks, 
    create_time, update_time
  </sql>
  <update id="updateBatch" parameterType="java.util.List">
    <!--@mbg.generated-->
    update b_maintenance_plan_info
    <trim prefix="set" suffixOverrides=",">
      <trim prefix="plan_id = case" suffix="end,">
        <foreach collection="list" index="index" item="item">
          when id = #{item.id,jdbcType=VARCHAR} then #{item.planId,jdbcType=INTEGER}
        </foreach>
      </trim>
      <trim prefix="&quot;type&quot; = case" suffix="end,">
        <foreach collection="list" index="index" item="item">
          when id = #{item.id,jdbcType=VARCHAR} then #{item.type,jdbcType=VARCHAR}
        </foreach>
      </trim>
      <trim prefix="&quot;name&quot; = case" suffix="end,">
        <foreach collection="list" index="index" item="item">
          when id = #{item.id,jdbcType=VARCHAR} then #{item.name,jdbcType=VARCHAR}
        </foreach>
      </trim>
      <trim prefix="device_id = case" suffix="end,">
        <foreach collection="list" index="index" item="item">
          when id = #{item.id,jdbcType=VARCHAR} then #{item.deviceId,jdbcType=VARCHAR}
        </foreach>
      </trim>
      <trim prefix="plan_start_time = case" suffix="end,">
        <foreach collection="list" index="index" item="item">
          when id = #{item.id,jdbcType=VARCHAR} then #{item.planStartTime,jdbcType=TIMESTAMP}
        </foreach>
      </trim>
      <trim prefix="plan_end_time = case" suffix="end,">
        <foreach collection="list" index="index" item="item">
          when id = #{item.id,jdbcType=VARCHAR} then #{item.planEndTime,jdbcType=TIMESTAMP}
        </foreach>
      </trim>
      <trim prefix="remarks = case" suffix="end,">
        <foreach collection="list" index="index" item="item">
          when id = #{item.id,jdbcType=VARCHAR} then #{item.remarks,jdbcType=VARCHAR}
        </foreach>
      </trim>
      <trim prefix="create_time = case" suffix="end,">
        <foreach collection="list" index="index" item="item">
          when id = #{item.id,jdbcType=VARCHAR} then #{item.createTime,jdbcType=TIMESTAMP}
        </foreach>
      </trim>
      <trim prefix="update_time = case" suffix="end,">
        <foreach collection="list" index="index" item="item">
          when id = #{item.id,jdbcType=VARCHAR} then #{item.updateTime,jdbcType=TIMESTAMP}
        </foreach>
      </trim>
    </trim>
    where id in
    <foreach close=")" collection="list" item="item" open="(" separator=", ">
      #{item.id,jdbcType=VARCHAR}
    </foreach>
  </update>
  <update id="updateBatchSelective" parameterType="java.util.List">
    <!--@mbg.generated-->
    update b_maintenance_plan_info
    <trim prefix="set" suffixOverrides=",">
      <trim prefix="plan_id = case" suffix="end,">
        <foreach collection="list" index="index" item="item">
          <if test="item.planId != null">
            when id = #{item.id,jdbcType=VARCHAR} then #{item.planId,jdbcType=INTEGER}
          </if>
        </foreach>
      </trim>
      <trim prefix="&quot;type&quot; = case" suffix="end,">
        <foreach collection="list" index="index" item="item">
          <if test="item.type != null">
            when id = #{item.id,jdbcType=VARCHAR} then #{item.type,jdbcType=VARCHAR}
          </if>
        </foreach>
      </trim>
      <trim prefix="&quot;name&quot; = case" suffix="end,">
        <foreach collection="list" index="index" item="item">
          <if test="item.name != null">
            when id = #{item.id,jdbcType=VARCHAR} then #{item.name,jdbcType=VARCHAR}
          </if>
        </foreach>
      </trim>
      <trim prefix="device_id = case" suffix="end,">
        <foreach collection="list" index="index" item="item">
          <if test="item.deviceId != null">
            when id = #{item.id,jdbcType=VARCHAR} then #{item.deviceId,jdbcType=VARCHAR}
          </if>
        </foreach>
      </trim>
      <trim prefix="plan_start_time = case" suffix="end,">
        <foreach collection="list" index="index" item="item">
          <if test="item.planStartTime != null">
            when id = #{item.id,jdbcType=VARCHAR} then #{item.planStartTime,jdbcType=TIMESTAMP}
          </if>
        </foreach>
      </trim>
      <trim prefix="plan_end_time = case" suffix="end,">
        <foreach collection="list" index="index" item="item">
          <if test="item.planEndTime != null">
            when id = #{item.id,jdbcType=VARCHAR} then #{item.planEndTime,jdbcType=TIMESTAMP}
          </if>
        </foreach>
      </trim>
      <trim prefix="remarks = case" suffix="end,">
        <foreach collection="list" index="index" item="item">
          <if test="item.remarks != null">
            when id = #{item.id,jdbcType=VARCHAR} then #{item.remarks,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=TIMESTAMP}
          </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=TIMESTAMP}
          </if>
        </foreach>
      </trim>
    </trim>
    where id in
    <foreach close=")" collection="list" item="item" open="(" separator=", ">
      #{item.id,jdbcType=VARCHAR}
    </foreach>
  </update>
  <insert id="batchInsert" parameterType="map">
    <!--@mbg.generated-->
    insert into b_maintenance_plan_info
    (id, plan_id, "type", "name", device_id, plan_start_time, plan_end_time, remarks, 
      create_time, update_time)
    values
    <foreach collection="list" item="item" separator=",">
      (#{item.id,jdbcType=VARCHAR}, #{item.planId,jdbcType=INTEGER}, #{item.type,jdbcType=VARCHAR}, 
        #{item.name,jdbcType=VARCHAR}, #{item.deviceId,jdbcType=VARCHAR}, #{item.planStartTime,jdbcType=TIMESTAMP}, 
        #{item.planEndTime,jdbcType=TIMESTAMP}, #{item.remarks,jdbcType=VARCHAR}, #{item.createTime,jdbcType=TIMESTAMP}, 
        #{item.updateTime,jdbcType=TIMESTAMP})
    </foreach>
  </insert>

  <sql id="device_List">
    SELECT
      bdpi.ID,
      bd.industry,
      pf.industry_category,
      bdpi.plan_start_time,
      bdpi.plan_end_time,
      bdpi.remarks,
      bd.work_step AS TYPE,
      bd.NAME,
      bd.device_id
    FROM
      bas_device bd
        LEFT JOIN b_maintenance_plan_info bdpi ON bd.device_id = bdpi.device_id
        AND bdpi.plan_id =#{planId,jdbcType=INTEGER}
        LEFT JOIN pc_factory pf ON bd.factory_id = pf.ID
    WHERE
      pf.industry_category = bd.industry
      AND bd.factory_id = #{factoryId,jdbcType=VARCHAR}
      AND (CASE bd.industry
             WHEN 'GT' THEN bd.work_step IN ('烧结', '球团', '高炉', '转炉', '炉窑', '发电')
             WHEN 'JH' THEN bd.work_step IN ('焦化', '干熄焦')
             WHEN 'SN' THEN bd.work_step IN ('水泥窑')
             WHEN 'DL' THEN bd.work_step IN ('锅炉','发电机组')
             WHEN 'FS' THEN bd.work_step IN ('焚烧炉')
             WHEN 'BL' THEN bd.work_step IN ('玻璃生产线')
             ELSE FALSE
      END)
    ORDER BY
      bdpi.ID
  </sql>

  <select id="selectAllDevice" resultMap="BaseResultMap">
    <include refid="device_List"/>
  </select>

  <select id="infoPageList" resultMap="BaseResultMap">
    <include refid="device_List"/>
    <if test="deviceType != null and deviceType !=''">
      AND bd.work_step LIKE CONCAT('%', #{deviceType}, '%')
    </if>
    <if test="deviceName != null and deviceName !=''">
      AND bd.NAME LIKE CONCAT('%', #{deviceName}, '%')
    </if>
  </select>
</mapper>