<?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.PlanOperationRecordMapper">
  <select id="filterCount" parameterType="map" resultType="java.lang.Integer">
    SELECT
      count(1)
    FROM
      c_plan_operation_record cpor
    LEFT JOIN c_plan_detail cpd ON
      cpor.plan_id = cpd.id
    <where>
      <if test="planId != null">
        AND cpor.plan_id = #{planId}
      </if>
      <if test="planName != null and planName.length > 0">
        AND cpd.plan_name LIKE CONCAT('%', #{planName},'%')
      </if>
      <if test="classifyId != null and classifyId.size > 0">
        AND cpd.classify_id IN
        <foreach collection="classifyId" item="item" separator="," open="(" close=")">
          #{item}
        </foreach>
      </if>
      <if test="planPattern != null">
        AND cpor.plan_pattern = #{planPattern}
      </if>
      <if test="executionType != null">
        AND cpor.execution_type LIKE #{executionType}
      </if>
      <if test="startTimeLeft != null">
        AND cpor.start_time <![CDATA[ >= ]]> #{startTimeLeft}
      </if>
      <if test="startTimeRight != null">
        AND cpor.start_time <![CDATA[ < ]]> #{startTimeRight}
      </if>
    </where>
  </select>
  
  <select id="filterList" parameterType="map" resultType="map">
    SELECT
      cpor.id
      , cpor.batch_no batchNo
      , cpor.plan_id planId
      , cpor.start_time startTime
      , cpor.end_time endTime
      , cpor.status
      , cpor.equipment_code equipmentCode
      , cpor.equipment_name equipmentName
      , cpor.execution_type executionType
      , cpor.plan_pattern planPattern
      , cpor.start_user_name startUserName
      , cpd.plan_name planName
      , cpor.equipment_id equipmentId
      , cpor.fire_equipment_id fireEquipmentId
      , (SELECT classify_name FROM c_plan_classify_tree cpct WHERE id = cpd.classify_id) classifyName
      , cpd.plan_range planRange
      , TIMESTAMPDIFF(SECOND, cpor.start_time, cpor.end_time) spendTime
    FROM
      c_plan_operation_record cpor
    LEFT JOIN c_plan_detail cpd ON
      cpor.plan_id = cpd.id
    <where>
      <if test="planId != null">
        AND cpor.plan_id = #{planId}
      </if>
      <if test="planName != null and planName.length > 0">
        AND cpd.plan_name LIKE CONCAT('%', #{planName},'%')
      </if>
      <if test="classifyId != null and classifyId.size > 0">
        AND cpd.classify_id IN
        <foreach collection="classifyId" item="item" separator="," open="(" close=")">
          #{item}
        </foreach>
      </if>
      <if test="planPattern != null">
        AND cpor.plan_pattern = #{planPattern}
      </if>
      <if test="executionType != null">
        AND cpor.execution_type LIKE #{executionType}
      </if>
      <if test="startTimeLeft != null">
        AND cpor.start_time <![CDATA[ >= ]]> #{startTimeLeft}
      </if>
      <if test="startTimeRight != null">
        AND cpor.start_time <![CDATA[ < ]]> #{startTimeRight}
      </if>
    </where>
    ORDER BY cpor.start_time DESC
    LIMIT #{start}, #{size}
  </select>
</mapper>