<?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.test.test.mapper.TestPlanMapper">

  <resultMap type="TestPlan" id="TestPlanResult">
    <result property="id" column="id"/>
    <result property="serialNumber" column="serial_number"/>
    <result property="name" column="name"/>
    <result property="status" column="status"/>
    <result property="manager" column="manager"/>
    <result property="startTime" column="start_time"/>
    <result property="endTime" column="end_time"/>
    <result property="createTime" column="create_time"/>
    <result property="updateTime" column="update_time"/>
    <result property="version" column="version"/>
    <result property="delFlag" column="del_flag"/>
  </resultMap>

  <sql id="selectTestPlanVo">
    SELECT id,
           serial_number,
           name,
           status,
           manager,
           start_time,
           end_time,
           create_time,
           update_time,
           version,
           del_flag
    FROM test_plan
  </sql>

  <insert id="insertTestPlan" parameterType="testPlan">
    insert into test_plan
    <trim prefix="(" suffix=")" suffixOverrides=",">
      <if test="serialNumber != null and serialNumber != ''">serial_number,</if>
      <if test="name != null and name != ''">name,</if>
      <if test="status != null and status != ''">status,</if>
      <if test="manager != null and manager != ''">manager,</if>
      <if test="startTime != null">start_time,</if>
      <if test="endTime != null">end_time,</if>
      <if test="createTime != null">create_time,</if>
      <if test="updateTime != null">update_time,</if>
      <if test="version != null">version,</if>
      <if test="delFlag != null and delFlag != ''">del_flag,</if>
    </trim>
    <trim prefix="values (" suffix=")" suffixOverrides=",">
      <if test="serialNumber != null and serialNumber != ''">#{serialNumber},</if>
      <if test="name != null and name != ''">#{name},</if>
      <if test="status != null and status != ''">#{status},</if>
      <if test="manager != null and manager != ''">#{manager},</if>
      <if test="startTime != null">#{startTime},</if>
      <if test="endTime != null">#{endTime},</if>
      <if test="createTime != null">#{createTime},</if>
      <if test="updateTime != null">#{updateTime},</if>
      <if test="version != null">#{version},</if>
      <if test="delFlag != null and delFlag != ''">#{delFlag},</if>
    </trim>
  </insert>

  <update id="updateTestPlan" parameterType="TestPlan">
    update test_plan
    <trim prefix="SET" suffixOverrides=",">
      <if test="serialNumber != null and serialNumber != ''">serial_number = #{serialNumber},</if>
      <if test="name != null and name != ''">name = #{name},</if>
      <if test="status != null and status != ''">status = #{status},</if>
      <if test="manager != null and manager != ''">manager = #{manager},</if>
      <if test="startTime != null">start_time = #{startTime},</if>
      <if test="endTime != null">end_time = #{endTime},</if>
      <if test="createTime != null">create_time = #{createTime},</if>
      <if test="updateTime != null">update_time = #{updateTime},</if>
      <if test="version != null">version = #{version},</if>
      <if test="delFlag != null and delFlag != ''">del_flag = #{delFlag},</if>
    </trim>
    where id = #{id}
  </update>

  <select id="selectTestPlanList" parameterType="TestPlanListQO" resultType="TestPlanListVO">
    SELECT
    tp.id,
    tp.serial_number AS serialNumber,
    tp.name,
    tp.status,
    su.user_name AS manager,
    tp.start_time AS startTime,
    tp.end_time AS endTime,
    (
    (SELECT COUNT(1) FROM test_plan_case tpc
    WHERE tpc.plan_id = tp.id
    AND tpc.del_flag = '0'
    AND tpc.execute_result = '1'
    AND tpc.type = '0') /
    (SELECT COUNT(1) FROM test_plan_case tpc
    WHERE tpc.plan_id = tp.id
    AND tpc.del_flag = '0'
    AND tpc.type = '0') * 100
    ) AS smokeTest,
    (
    (SELECT COUNT(1) FROM test_plan_case tpc
    WHERE tpc.plan_id = tp.id
    AND tpc.del_flag = '0'
    AND tpc.execute_result = '1'
    AND tpc.type = '1') /
    (SELECT COUNT(1) FROM test_plan_case tpc
    WHERE tpc.plan_id = tp.id
    AND tpc.del_flag = '0'
    AND tpc.type = '1') * 100
    ) AS functionTest,
    (
    (SELECT COUNT(1) FROM test_plan_case tpc
    WHERE tpc.plan_id = tp.id
    AND tpc.del_flag = '0'
    AND tpc.execute_result = '1'
    AND tpc.type = '2') /
    (SELECT COUNT(1) FROM test_plan_case tpc
    WHERE tpc.plan_id = tp.id
    AND tpc.del_flag = '0'
    AND tpc.type = '2') * 100
    ) AS regressionTest,
    (
    (SELECT COUNT(1) FROM test_plan_case tpc
    WHERE tpc.plan_id = tp.id
    AND tpc.del_flag = '0'
    AND tpc.execute_result = '1'
    AND tpc.type = '3') /
    (SELECT COUNT(1) FROM test_plan_case tpc
    WHERE tpc.plan_id = tp.id
    AND tpc.del_flag = '0'
    AND tpc.type = '3') * 100
    ) AS preProductionTest,
    (
    (SELECT COUNT(1) FROM test_plan_case tpc
    WHERE tpc.plan_id = tp.id
    AND tpc.del_flag = '0'
    AND tpc.execute_result = '1'
    AND tpc.type = '4') /
    (SELECT COUNT(1) FROM test_plan_case tpc
    WHERE tpc.plan_id = tp.id
    AND tpc.del_flag = '0'
    AND tpc.type = '4') * 100
    ) AS productionTest,
    (SELECT COUNT(1) FROM test_plan_case tpc
    WHERE tpc.plan_id = tp.id
    AND tpc.del_flag = '0'
    AND tpc.type = '0') AS smokeTestPassNum,
    (SELECT COUNT(1) FROM test_plan_case tpc
    WHERE tpc.plan_id = tp.id
    AND tpc.del_flag = '0'
    AND tpc.type = '1') AS functionTestPassNum,
    (SELECT COUNT(1) FROM test_plan_case tpc
    WHERE tpc.plan_id = tp.id
    AND tpc.del_flag = '0'
    AND tpc.type = '2') AS regressionTestPassNum,
    (SELECT COUNT(1) FROM test_plan_case tpc
    WHERE tpc.plan_id = tp.id
    AND tpc.del_flag = '0'
    AND tpc.type = '3') AS preProductionTestPassNum,
    (SELECT COUNT(1) FROM test_plan_case tpc
    WHERE tpc.plan_id = tp.id
    AND tpc.del_flag = '0'
    AND tpc.type = '4') AS productionTestPassNum,
    tp.version,
    (
    SELECT COUNT(1) FROM test_plan_defect tpd
    WHERE tpd.plan_id = tp.id
    AND tpd.del_flag = '0'
    ) AS defectNum
    FROM test_plan tp
    LEFT JOIN sys_user su ON su.user_id = tp.manager
    where 1=1
    AND tp.del_flag = '0'
    <if test="status != null and status != ''">
      AND tp.status = #{status}
    </if>
    <if test="manager != null and manager != ''">
      AND tp.manager = #{manager}
    </if>
    <if test="name != null and name != ''">
      AND tp.name LIKE CONCAT('%', #{name}, '%')
    </if>
    <if test="startPlanTime != null">
      AND DATE_FORMAT(IFNULL(tp.start_time,''),'%Y%m%d') <![CDATA[ >= ]]> DATE_FORMAT(#{startPlanTime},'%Y%m%d')
    </if>
    <if test="endPlanTime != null">
      AND DATE_FORMAT(IFNULL(tp.start_time,''),'%Y%m%d') <![CDATA[ <= ]]> DATE_FORMAT(#{endPlanTime},'%Y%m%d')
    </if>
    <if test="startDeadline != null">
      AND DATE_FORMAT(IFNULL(tp.end_time,''),'%Y%m%d') <![CDATA[ >= ]]> DATE_FORMAT(#{startDeadline},'%Y%m%d')
    </if>
    <if test="endDeadline != null">
      AND DATE_FORMAT(IFNULL(tp.end_time,''),'%Y%m%d') <![CDATA[ <= ]]> DATE_FORMAT(#{endDeadline},'%Y%m%d')
    </if>
  </select>

  <select id="selectTestPlanById" parameterType="Long" resultMap="TestPlanResult">
    <include refid="selectTestPlanVo"/>
    where id = #{id}
  </select>

  <select id="selectPlanId" resultType="Long">
    select id from test_plan where serial_number = #{serialNumber} and del_flag = '0'
  </select>
</mapper>