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

    <resultMap type="com.microservices.pms.project.domain.PmsProjectSprint" id="PmsProjectSprintResult">
        <result property="id"    column="id"    />
        <result property="sprintName"    column="sprint_name"    />
        <result property="sprintAssigneeId"    column="sprint_assignee_id"    />
        <result property="pmsProjectId"    column="pms_project_id"    />
        <result property="sprintDescription"    column="sprint_description"    />
        <result property="timeScale"    column="time_scale"    />
        <result property="status"    column="status"    />
        <result property="startTime"    column="start_time"    />
        <result property="endTime"    column="end_time"    />
        <result property="isTop"    column="is_top"    />
        <result property="createTime"    column="create_time"    />
        <result property="updateTime"    column="update_time"    />
        <result property="isDeleted"    column="is_deleted"    />
        <result property="reservedField1"    column="reserved_field_1"    />
        <result property="reservedField2"    column="reserved_field_2"    />
        <result property="reservedField3"    column="reserved_field_3"    />
    </resultMap>

    <sql id="selectPmsProjectSprintVo">
        select id, sprint_name, sprint_assignee_id, pms_project_id, sprint_description, time_scale, status, start_time, end_time, is_top, create_time, update_time, is_deleted, reserved_field_1, reserved_field_2, reserved_field_3 from pms_project_sprint
    </sql>

    <select id="selectPmsProjectSprintList" parameterType="PmsProjectSprint" resultMap="PmsProjectSprintResult">
        <include refid="selectPmsProjectSprintVo"/>
        <where>  
            <if test="sprintName != null  and sprintName != ''"> and sprint_name like concat('%', #{sprintName}, '%')</if>
            <if test="sprintAssigneeId != null "> and sprint_assignee_id = #{sprintAssigneeId}</if>
            <if test="pmsProjectId != null "> and pms_project_id = #{pmsProjectId}</if>
            <if test="sprintDescription != null  and sprintDescription != ''"> and sprint_description = #{sprintDescription}</if>
            <if test="timeScale != null "> and time_scale = #{timeScale}</if>
            <if test="status != null  and status != ''"> and status = #{status}</if>
            <if test="startTime != null "> and start_time = #{startTime}</if>
            <if test="endTime != null "> and end_time = #{endTime}</if>
            <if test="isTop != null "> and is_top = #{isTop}</if>
            <if test="createTime != null "> and create_time = #{createTime}</if>
            <if test="updateTime != null "> and update_time = #{updateTime}</if>
            <if test="isDeleted != null "> and is_deleted = #{isDeleted}</if>
            <if test="reservedField1 != null  and reservedField1 != ''"> and reserved_field_1 = #{reservedField1}</if>
            <if test="reservedField2 != null  and reservedField2 != ''"> and reserved_field_2 = #{reservedField2}</if>
            <if test="reservedField3 != null  and reservedField3 != ''"> and reserved_field_3 = #{reservedField3}</if>
        </where>
    </select>
    
    <select id="selectPmsProjectSprintById" parameterType="Long" resultMap="PmsProjectSprintResult">
        <include refid="selectPmsProjectSprintVo"/>
        where id = #{id}
    </select>

    <sql id="selectWhereBySearch">
        <where>
            <if test="sprintName != null  and sprintName != ''">and sprint_name like concat('%', #{sprintName}, '%')
            </if>
            <if test="statusIds != null  and statusIds != ''">and status in
            <foreach item="statusIds" collection="statusIds.split(',')" open="(" separator="," close=")">
                #{statusIds}
            </foreach>
            </if>
            <if test="pmsProjectIds != null  and pmsProjectIds != ''">and pms_project_id in
                <foreach item="pmsProjectId" collection="pmsProjectIds.split(',')" open="(" separator="," close=")">
                    #{pmsProjectId}
                </foreach>
            </if>

            <if test="pmsProjectId != null  and pmsProjectId != ''">and pms_project_id = #{pmsProjectId}</if>
            <if test="sprintAssigneeId != null  and sprintAssigneeId != ''">and sprint_assignee_id = #{sprintAssigneeId}</if>
            <!-- 数据范围过滤 -->
            ${params.dataScope}
        </where>
    </sql>

    <select id="selectPmsProjectSprintListBySearchInput"
            parameterType="com.microservices.pms.project.domain.vo.PmsProjectSprintSearchVo"
            resultMap="PmsProjectSprintResult">
        <include refid="selectPmsProjectSprintVo"/>
        <include refid="selectWhereBySearch"/>
        order by
        CASE
        WHEN status IN ('0', '1') THEN 0
        ELSE 1
        END, status,
        update_time desc
    </select>

    <select id="selectPmsProjectSprintCount" resultType="java.lang.Integer">
        select count(*)
        from pms_project_sprint
        <include refid="selectWhereBySearch"/>
    </select>

    <insert id="insertPmsProjectSprint" parameterType="PmsProjectSprint" useGeneratedKeys="true" keyProperty="id">
        insert into pms_project_sprint
        <trim prefix="(" suffix=")" suffixOverrides=",">
            <if test="sprintName != null and sprintName != ''">sprint_name,</if>
            <if test="sprintAssigneeId != null">sprint_assignee_id,</if>
            <if test="pmsProjectId != null">pms_project_id,</if>
            <if test="sprintDescription != null and sprintDescription != ''">sprint_description,</if>
            <if test="timeScale != null">time_scale,</if>
            <if test="status != null">status,</if>
            <if test="startTime != null">start_time,</if>
            <if test="endTime != null">end_time,</if>
            <if test="isTop != null">is_top,</if>
            <if test="createTime != null">create_time,</if>
            <if test="updateTime != null">update_time,</if>
            <if test="isDeleted != null">is_deleted,</if>
            <if test="reservedField1 != null">reserved_field_1,</if>
            <if test="reservedField2 != null">reserved_field_2,</if>
            <if test="reservedField3 != null">reserved_field_3,</if>
         </trim>
        <trim prefix="values (" suffix=")" suffixOverrides=",">
            <if test="sprintName != null and sprintName != ''">#{sprintName},</if>
            <if test="sprintAssigneeId != null">#{sprintAssigneeId},</if>
            <if test="pmsProjectId != null">#{pmsProjectId},</if>
            <if test="sprintDescription != null and sprintDescription != ''">#{sprintDescription},</if>
            <if test="timeScale != null">#{timeScale},</if>
            <if test="status != null">#{status},</if>
            <if test="startTime != null">#{startTime},</if>
            <if test="endTime != null">#{endTime},</if>
            <if test="isTop != null">#{isTop},</if>
            <if test="createTime != null">#{createTime},</if>
            <if test="updateTime != null">#{updateTime},</if>
            <if test="isDeleted != null">#{isDeleted},</if>
            <if test="reservedField1 != null">#{reservedField1},</if>
            <if test="reservedField2 != null">#{reservedField2},</if>
            <if test="reservedField3 != null">#{reservedField3},</if>
         </trim>
    </insert>

    <update id="updatePmsProjectSprint" parameterType="PmsProjectSprint">
        update pms_project_sprint
        <trim prefix="SET" suffixOverrides=",">
            <if test="sprintName != null and sprintName != ''">sprint_name = #{sprintName},</if>
            <if test="sprintAssigneeId != null">sprint_assignee_id = #{sprintAssigneeId},</if>
            <if test="pmsProjectId != null">pms_project_id = #{pmsProjectId},</if>
            <if test="sprintDescription != null and sprintDescription != ''">sprint_description = #{sprintDescription},</if>
            <if test="timeScale != null">time_scale = #{timeScale},</if>
            <if test="status != null">status = #{status},</if>
            <if test="startTime != null">start_time = #{startTime},</if>
            <if test="endTime != null">end_time = #{endTime},</if>
            <if test="isTop != null">is_top = #{isTop},</if>
            <if test="createTime != null">create_time = #{createTime},</if>
            <if test="updateTime != null">update_time = #{updateTime},</if>
            <if test="isDeleted != null">is_deleted = #{isDeleted},</if>
            <if test="reservedField1 != null">reserved_field_1 = #{reservedField1},</if>
            <if test="reservedField2 != null">reserved_field_2 = #{reservedField2},</if>
            <if test="reservedField3 != null">reserved_field_3 = #{reservedField3},</if>
        </trim>
        where id = #{id}
    </update>

    <delete id="deletePmsProjectSprintById" parameterType="Long">
        delete from pms_project_sprint where id = #{id}
    </delete>

    <delete id="deletePmsProjectSprintByIds" parameterType="String">
        delete from pms_project_sprint where id in 
        <foreach item="id" collection="array" open="(" separator="," close=")">
            #{id}
        </foreach>
    </delete>
    <delete id="deletePmsProjectSprintByProjectId">
        delete from pms_project_sprint where pms_project_id = #{projectId}
    </delete>

    <select id="selectPmsProjectSprintBYIds" resultMap="PmsProjectSprintResult">
        <include refid="selectPmsProjectSprintVo"/>
        where id in
        <foreach item="testSheetLinkSprintId" collection="testSheetLinkSprintIds" open="(" separator="," close=")">
            #{testSheetLinkSprintId}
        </foreach>
    </select>
    <select id="selectLatestSprintByProjectIdAndNameAndAssigneeId"
            resultMap="PmsProjectSprintResult">
        <include refid="selectPmsProjectSprintVo"/>
        where pms_project_id = #{pmsProjectId}
        and sprint_name = #{sprintName}
        and sprint_assignee_id = #{sprintAssigneeId}
        order by id desc
        limit 1
    </select>
    <select id="selectOpeningSprintCount" resultType="java.lang.Integer">
        select count(*)
        from pms_project_sprint
        where pms_project_id = #{pmProjectId}
        and status IN ('0', '1')
    </select>
    <select id="selectOpeningSprintLastWeekIterationCount" resultType="java.lang.Integer">
        select count(*)
        from pms_project_sprint
        where pms_project_id = #{pmProjectId}
        and status IN ('0', '1')
        and start_time >= DATE_SUB(NOW(), INTERVAL 1 WEEK)
    </select>
    <select id="selectPmsProjectSprintStatisticsByProjectId"
            resultType="com.microservices.pms.project.domain.vo.PmsProjectSprintStatisticsVo">
        SELECT
            (
                SELECT COUNT(*)
                FROM pms_project_sprint
                WHERE pms_project_id = #{pmProjectId}
            ) AS all_sprint_count,
            (
                SELECT COUNT(*)
                FROM pms_project_sprint
                WHERE pms_project_id = #{pmProjectId} AND status = 0
            ) AS `todo_sprint_count`,
            (
                SELECT COUNT(*)
                FROM pms_project_sprint
                WHERE pms_project_id = #{pmProjectId} AND status = 1
            ) AS `doing_sprint_count`,
            (
                SELECT COUNT(*)
                FROM pms_project_sprint
                WHERE pms_project_id = #{pmProjectId} AND status = 2
            ) AS `closed_sprint_count`
    </select>
    <select id="selectNewSprintCurrentNaturalWeekCount" resultType="java.lang.Integer">
        select count(*)
        from pms_project_sprint
        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="selectCompleteSprintCurrentNaturalWeekCount" resultType="java.lang.Integer">
        select count(*)
        from pms_project_sprint
        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>
</mapper>