<?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="com.yeejoin.amos.fas.business.dao.mapper.PlanDetailMapper">

  <update id="updatePlanStatusByIdList" parameterType="list">
    UPDATE c_plan_detail SET status = #{status} WHERE id IN
      <foreach collection="idList" separator="," item="id" open="(" close=")">
        #{id}
      </foreach>
  </update>

  <update id="updateIsDeleteByIdList" parameterType="list">
    UPDATE c_plan_detail SET is_delete = #{isDelete} WHERE id IN
      <foreach collection="idList" separator="," item="id" open="(" close=")">
        #{id}
      </foreach>
  </update>

  <select id="filterCount" parameterType="string" resultType="int">
    SELECT
      count(1)
    FROM
      c_plan_detail cpd
    <where>
      <if test="planName != null and planName.length > 0">
        AND plan_name LIKE concat('%', #{planName}, '%')
      </if>
      <if test="classifyId != null and classifyId.size > 0">
        AND classify_id IN
        <foreach collection="classifyId" separator="," item="cid" open="(" close=")">
          #{cid}
        </foreach>
      </if>
      <if test="planRange != null and planRange.size > 0">
        AND plan_range IN
        <foreach collection="planRange" separator="," item="pr" open="(" close=")">
          #{pr}
        </foreach>
      </if>
      <if test="editOrgName != null and editOrgName.length > 0">
        AND edit_org_name LIKE concat('%', #{editOrgName}, '%')
      </if>
      <if test="implementationTimeLeft != null">
        AND implementation_time <![CDATA[ >= ]]> #{implementationTimeLeft}
      </if>
      <if test="implementationTimeRight != null">
        AND implementation_time <![CDATA[ < ]]> #{implementationTimeRight}
      </if>
      AND is_delete = 0
    </where>
  </select>

  <select id="filterList" parameterType="string" resultType="com.yeejoin.amos.fas.business.vo.PlanDetailVo">
    SELECT
    cpd.id
    , cpd.create_date createDate
    , plan_name planName
    , code
    , classify_id classifyId
    , plan_range planRange
    , edit_org_name editOrgName
    , edition
    , implementation_time implementationTime
    , cpd.remark
    , status
    , creator
    , reviser
    , update_time updateTime
    , org_code orgCode
    , cpd.is_delete isDelete
    , input_time inputTime
    , (SELECT classify_name FROM c_plan_classify_tree cpct WHERE id = cpd.classify_id) classifyName
    , (SELECT count(1) FROM c_plan_operation_record cpor WHERE plan_id = cpd.id) executionTimes
    ,fpp.picture
    FROM
    c_plan_detail cpd
    left join c_plan_equipment as cpe on cpd.id = cpe.plan_id and cpe.is_delete = 0
    left join f_preplan_picture as fpp on fpp.equipment_id  = cpe.fire_equipment_id and fpp.`type` =5
    <where>
      <if test="planName != null and planName.length > 0">
        AND cpd.plan_name LIKE concat('%', #{cpd.planName}, '%')
      </if>
      <if test="classifyId != null and classifyId.size > 0">
        AND cpd.classify_id IN
        <foreach collection="classifyId" separator="," item="cid" open="(" close=")">
          #{cid}
        </foreach>
      </if>
      <if test="planRange != null and planRange.size > 0">
        AND cpd.plan_range IN
        <foreach collection="planRange" separator="," item="pr" open="(" close=")">
          #{pr}
        </foreach>
      </if>
      <if test="editOrgName != null and editOrgName.length > 0">
        AND cpd.edit_org_name LIKE concat('%', #{editOrgName}, '%')
      </if>
      <if test="implementationTimeLeft != null">
        AND cpd.implementation_time <![CDATA[ >= ]]> #{implementationTimeLeft}
      </if>
      <if test="implementationTimeRight != null">
        AND cpd.implementation_time <![CDATA[ < ]]> #{implementationTimeRight}
      </if>
      AND cpd.is_delete = 0
    </where>
    ORDER BY cpd.create_date ASC
    LIMIT #{start}, #{size}
  </select>


  <select id="getRuleIdByEquipment" parameterType="java.lang.Long" resultType="java.lang.String">
  SELECT
		rule_id
    FROM
    c_plan_detail cpd
		LEFT JOIN c_plan_rule rule ON  rule.plan_id = cpd.id
		LEFT JOIN c_plan_equipment equip ON  equip.plan_id = cpd.id
		LEFT JOIN  f_equipment equipment ON equipment.id =  equip.fire_equipment_id
    where
       equipment.id = #{id}
       AND equip.is_delete != 1
       AND rule.is_delete  !=1
  </select>
</mapper>