<?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.TestProjectMapper">

  <resultMap type="TestProject" id="TestProjectResult">
    <result property="id" column="id"/>
    <result property="serialNumber" column="serial_number"/>
    <result property="name" column="name"/>
    <result property="outline" column="outline"/>
    <result property="detail" column="detail"/>
    <result property="priority" column="priority"/>
    <result property="estimatedTime" column="estimated_time"/>
    <result property="source" column="source"/>
    <result property="type" column="type"/>
    <result property="status" column="status"/>
    <result property="manager" column="manager"/>
    <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="selectTestProjectVo">
    SELECT id,
           serial_number,
           name,
           outline,
           detail,
           priority,
           estimated_time,
           source,
           type,
           status,
           manager,
           create_time,
           update_time,
           version,
           del_flag
    FROM test_project
  </sql>

  <insert id="insertTestProject" parameterType="testProject" useGeneratedKeys="true" keyProperty="id">
    INSERT INTO test_project (
    <trim prefix="" suffix="" suffixOverrides=",">
      <if test="id != null">id,</if>
      <if test="serialNumber != null and serialNumber != ''">serial_number,</if>
      <if test="name != null and name != ''">name,</if>
      <if test="outline != null and outline != ''">outline,</if>
      <if test="detail != null and detail != ''">detail,</if>
      <if test="priority != null and priority != ''">priority,</if>
      <if test="estimatedTime != null">estimated_time,</if>
      <if test="source != null and source != ''">source,</if>
      <if test="type != null and type != ''">type,</if>
      <if test="status != null and status != ''">status,</if>
      <if test="manager != null and manager != ''">manager,</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>
    ) VALUES (
    <trim prefix="" suffix="" suffixOverrides=",">
      <if test="id != null">#{id},</if>
      <if test="serialNumber != null and serialNumber != ''">#{serialNumber},</if>
      <if test="name != null and name != ''">#{name},</if>
      <if test="outline != null and outline != ''">#{outline},</if>
      <if test="detail != null and detail != ''">#{detail},</if>
      <if test="priority != null and priority != ''">#{priority},</if>
      <if test="estimatedTime != null">#{estimatedTime},</if>
      <if test="source != null and source != ''">#{source},</if>
      <if test="type != null and type != ''">#{type},</if>
      <if test="status != null and status != ''">#{status},</if>
      <if test="manager != null and manager != ''">#{manager},</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="updateTestProject" parameterType="TestProject">
    update test_project
    <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="outline != null and outline != ''">outline = #{outline},</if>
      <if test="detail != null and detail != ''">detail = #{detail},</if>
      <if test="priority != null and priority != ''">priority = #{priority},</if>
      <if test="estimatedTime != null">estimated_time = #{estimatedTime},</if>
      <if test="source != null and source != ''">source = #{source},</if>
      <if test="type != null and type != ''">type = #{type},</if>
      <if test="status != null and status != ''">status = #{status},</if>
      <if test="manager != null and manager != ''">manager = #{manager},</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>

  <delete id="deleteTestProjectById">
    DELETE
    FROM test_project
    WHERE id = #{id}
  </delete>

  <select id="selectTestProjectList" parameterType="TestProjectListQO" resultType="TestProject">
    SELECT
    tp.id AS id,
    tp.serial_number AS serialNumber,
    tp.name ,
    tp.outline,
    tp.detail,
    tp.priority,
    tp.estimated_time AS estimatedTime,
    tp.source,
    tp.type,
    tp.status,
    su.user_name AS manager,
    tp.create_time AS createTime,
    tp.version,
    tp.del_flag
    FROM test_project tp
    LEFT JOIN sys_user su ON su.user_id = tp.manager
    where 1=1
      AND tp.del_flag = '0'
    <if test="serialNumber != null and serialNumber != ''">
      AND serial_number LIKE concat('%', #{serialNumber}, '%')
    </if>
    <if test="outline != null and outline != ''">
      AND outline LIKE concat('%', #{outline}, '%')
    </if>
    <if test="startCreateTime != null">
      AND DATE_FORMAT(IFNULL(tp.create_time,''),'%Y%m%d') <![CDATA[ >= ]]> DATE_FORMAT(#{startCreateTime},'%Y%m%d')
    </if>
    <if test="endCreateTime != null">
      AND DATE_FORMAT(IFNULL(tp.create_time,''),'%Y%m%d') <![CDATA[ <= ]]> DATE_FORMAT(#{endCreateTime},'%Y%m%d')
    </if>
    <if test="manager != null and manager != ''">
      AND manager = #{manager}
    </if>
    <if test="priority != null and priority != ''">
      AND priority = #{priority}
    </if>
    <if test="status !=null and status != ''">
      AND tp.status = #{status}
    </if>
  </select>

  <select id="selectTestProjectById" parameterType="Long" resultMap="TestProjectResult">
    <include refid="selectTestProjectVo"/>
    WHERE id = #{id}
  </select>
</mapper>