<?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.microservices.pms.project.mapper.PmsProjectTestsheetMapper">
    
    <resultMap type="PmsProjectTestsheet" id="PmsProjectTestsheetResult">
        <result property="id"    column="id"    />
        <result property="pmsProjectId"    column="pms_project_id"    />
        <result property="name"    column="name"    />
        <result property="assigneeId"    column="assignee_id"    />
        <result property="beginTime"    column="begin_time"    />
        <result property="endTime"    column="end_time"    />
        <result property="projectSprintId"    column="project_sprint_id"    />
        <result property="status"    column="status"    />
        <result property="updateTime"    column="update_time"    />
        <result property="updateBy"    column="update_by"    />
        <result property="createTime"    column="create_time"    />
        <result property="createBy"    column="create_by"    />
    </resultMap>

    <sql id="selectPmsProjectTestsheetVo">
        select id, pms_project_id, name, assignee_id, begin_time, end_time, project_sprint_id, status, update_time, update_by, create_time, create_by from pms_project_testsheet
    </sql>

    <select id="selectPmsProjectTestsheetList" parameterType="PmsProjectTestsheet" resultMap="PmsProjectTestsheetResult">
        <include refid="selectPmsProjectTestsheetVo"/>
        <where>  
            <if test="pmsProjectId != null "> and pms_project_id = #{pmsProjectId}</if>
            <if test="name != null  and name != ''"> and name like concat('%', #{name}, '%')</if>
            <if test="assigneeId != null "> and assignee_id = #{assigneeId}</if>
            <if test="beginTime != null "> and begin_time = #{beginTime}</if>
            <if test="endTime != null "> and end_time = #{endTime}</if>
            <if test="projectSprintId != null "> and project_sprint_id = #{projectSprintId}</if>
            <if test="status != null "> and status = #{status}</if>
        </where>
        order by create_time desc
    </select>
    
    <select id="selectPmsProjectTestsheetById" parameterType="Long" resultMap="PmsProjectTestsheetResult">
        <include refid="selectPmsProjectTestsheetVo"/>
        where id = #{id}
    </select>

    <select id="selectPmsProjectTestsheetByIds"
            resultMap="PmsProjectTestsheetResult">
        <include refid="selectPmsProjectTestsheetVo"/>
        where id in
        <foreach collection="pmsProjectTestSheetIds.split(',')" item="pmsProjectTestSheetIds" open="(" close=")" separator=",">
            #{pmsProjectTestSheetIds}
        </foreach>
    </select>
    <select id="selectCountBySprintId" resultType="java.lang.Integer">
        select count(*) from pms_project_testsheet where project_sprint_id = #{sprintId}
    </select>
    <select id="selectNewTestSheetCurrentNaturalWeekCount" resultType="java.lang.Integer">
        select count(*)
        from pms_project_testsheet
        where pms_project_id = #{projectId}
          AND create_time <![CDATA[ >= ]]> STR_TO_DATE(DATE_FORMAT(NOW(), '%Y-%V-1'), '%Y-%U-%w')
          AND create_time  <![CDATA[ <= ]]> STR_TO_DATE(DATE_FORMAT(NOW(), '%Y-%V-1'), '%Y-%U-%w') + INTERVAL 6 DAY
    </select>
    <select id="selectCompleteTestSheetCurrentNaturalWeekCount" resultType="java.lang.Integer">
        select count(*)
        from pms_project_testsheet
        where pms_project_id = #{projectId}
          AND status = 2
          AND update_time <![CDATA[ >= ]]> STR_TO_DATE(DATE_FORMAT(NOW(), '%Y-%V-1'), '%Y-%U-%w')
          AND update_time  <![CDATA[ <= ]]> STR_TO_DATE(DATE_FORMAT(NOW(), '%Y-%V-1'), '%Y-%U-%w') + INTERVAL 6 DAY
    </select>

    <insert id="insertPmsProjectTestsheet" parameterType="PmsProjectTestsheet" useGeneratedKeys="true" keyProperty="id">
        insert into pms_project_testsheet
        <trim prefix="(" suffix=")" suffixOverrides=",">
            <if test="pmsProjectId != null">pms_project_id,</if>
            <if test="name != null">name,</if>
            <if test="assigneeId != null">assignee_id,</if>
            <if test="beginTime != null">begin_time,</if>
            <if test="endTime != null">end_time,</if>
            <if test="projectSprintId != null">project_sprint_id,</if>
            <if test="status != null">status,</if>
            <if test="updateTime != null">update_time,</if>
            <if test="updateBy != null">update_by,</if>
            <if test="createTime != null">create_time,</if>
            <if test="createBy != null">create_by,</if>
         </trim>
        <trim prefix="values (" suffix=")" suffixOverrides=",">
            <if test="pmsProjectId != null">#{pmsProjectId},</if>
            <if test="name != null">#{name},</if>
            <if test="assigneeId != null">#{assigneeId},</if>
            <if test="beginTime != null">#{beginTime},</if>
            <if test="endTime != null">#{endTime},</if>
            <if test="projectSprintId != null">#{projectSprintId},</if>
            <if test="status != null">#{status},</if>
            <if test="updateTime != null">#{updateTime},</if>
            <if test="updateBy != null">#{updateBy},</if>
            <if test="createTime != null">#{createTime},</if>
            <if test="createBy != null">#{createBy},</if>
         </trim>
    </insert>

    <update id="updatePmsProjectTestsheet" parameterType="PmsProjectTestsheet">
        update pms_project_testsheet
        <trim prefix="SET" suffixOverrides=",">
            <if test="pmsProjectId != null">pms_project_id = #{pmsProjectId},</if>
            <if test="name != null">name = #{name},</if>
            <if test="assigneeId != null">assignee_id = #{assigneeId},</if>
            <if test="beginTime != null">begin_time = #{beginTime},</if>
            <if test="endTime != null">end_time = #{endTime},</if>
            <if test="projectSprintId != null">project_sprint_id = #{projectSprintId},</if>
            <if test="status != null">status = #{status},</if>
            <if test="updateTime != null">update_time = #{updateTime},</if>
            <if test="updateBy != null">update_by = #{updateBy},</if>
            <if test="createTime != null">create_time = #{createTime},</if>
            <if test="createBy != null">create_by = #{createBy},</if>
        </trim>
        where id = #{id}
    </update>

    <delete id="deletePmsProjectTestsheetById" parameterType="Long">
        delete from pms_project_testsheet where id = #{id}
    </delete>

    <delete id="deletePmsProjectTestsheetByIds" parameterType="String">
        delete from pms_project_testsheet where id in 
        <foreach item="id" collection="array" open="(" separator="," close=")">
            #{id}
        </foreach>
    </delete>
    <delete id="deletePmsProjectTestsheetByProjectId">
        delete from pms_project_testsheet where pms_project_id = #{projectId}
    </delete>
</mapper>