<?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.PmsProjectTestsheetIssuesMapper">
    
    <resultMap type="PmsProjectTestsheetIssues" id="PmsProjectTestsheetIssuesResult">
        <result property="id"    column="id"    />
        <result property="projectTestsheetCasesId"    column="project_testsheet_cases_id"    />
        <result property="projectIssueId"    column="project_issue_id"    />
    </resultMap>

    <sql id="selectPmsProjectTestsheetIssuesVo">
        select id, project_testsheet_cases_id, project_issue_id from pms_project_testsheet_issues
    </sql>

    <select id="selectPmsProjectTestsheetIssuesList" parameterType="PmsProjectTestsheetIssues" resultMap="PmsProjectTestsheetIssuesResult">
        <include refid="selectPmsProjectTestsheetIssuesVo"/>
        <where>  
            <if test="projectTestsheetCasesId != null "> and project_testsheet_cases_id = #{projectTestsheetCasesId}</if>
            <if test="projectIssueId != null "> and project_issue_id = #{projectIssueId}</if>
        </where>
    </select>
    
    <select id="selectPmsProjectTestsheetIssuesById" parameterType="Long" resultMap="PmsProjectTestsheetIssuesResult">
        <include refid="selectPmsProjectTestsheetIssuesVo"/>
        where id = #{id}
    </select>

    <select id="selectCountByTestsheetCasesId" parameterType="Long" resultType="java.lang.Integer">
        select count(*) from pms_project_testsheet_issues where project_testsheet_cases_id = #{id}
    </select>

    <insert id="insertPmsProjectTestsheetIssues" parameterType="PmsProjectTestsheetIssues" useGeneratedKeys="true" keyProperty="id">
        insert into pms_project_testsheet_issues
        <trim prefix="(" suffix=")" suffixOverrides=",">
            <if test="projectTestsheetCasesId != null">project_testsheet_cases_id,</if>
            <if test="projectIssueId != null">project_issue_id,</if>
         </trim>
        <trim prefix="values (" suffix=")" suffixOverrides=",">
            <if test="projectTestsheetCasesId != null">#{projectTestsheetCasesId},</if>
            <if test="projectIssueId != null">#{projectIssueId},</if>
         </trim>
    </insert>

    <update id="updatePmsProjectTestsheetIssues" parameterType="PmsProjectTestsheetIssues">
        update pms_project_testsheet_issues
        <trim prefix="SET" suffixOverrides=",">
            <if test="projectTestsheetCasesId != null">project_testsheet_cases_id = #{projectTestsheetCasesId},</if>
            <if test="projectIssueId != null">project_issue_id = #{projectIssueId},</if>
        </trim>
        where id = #{id}
    </update>

    <delete id="deletePmsProjectTestsheetIssuesById" parameterType="Long">
        delete from pms_project_testsheet_issues where id = #{id}
    </delete>

    <delete id="deletePmsProjectTestsheetIssuesByIds" parameterType="String">
        delete from pms_project_testsheet_issues where id in 
        <foreach item="id" collection="array" open="(" separator="," close=")">
            #{id}
        </foreach>
    </delete>

    <delete id="deleteByProjectTestsheetCasesIdAndIssueIds">
        delete from pms_project_testsheet_issues where project_testsheet_cases_id = #{projectTestsheetCasesId} and project_issue_id in
        <foreach item="id" collection="ids" open="(" separator="," close=")">
            #{id}
        </foreach>
    </delete>

    <delete id="deleteByProjectTestsheetCasesIds">
        delete from pms_project_testsheet_issues where project_testsheet_cases_id in
        <foreach item="id" collection="projectTestsheetCasesIds" open="(" separator="," close=")">
            #{id}
        </foreach>
    </delete>
    <delete id="deletePmsProjectTestsheetIssuesByIssueIds">
        delete from pms_project_testsheet_issues where pms_project_testsheet_issues.project_issue_id in
        <foreach item="id" collection="issueIds" open="(" separator="," close=")">
            #{id}
        </foreach>
    </delete>

    <select id="selectIssueIdsByTestSheetCaseIds" resultType="java.lang.Long">
        select project_issue_id
        from pms_project_testsheet_issues
        where project_testsheet_cases_id in
        <foreach item="projectTestSheetCasesId" collection="projectTestSheetCasesIds" open="(" separator="," close=")">
            #{projectTestSheetCasesId}
        </foreach>
    </select>
    <select id="getTestCaseExecutionResultStatisticsByIds"
            resultType="com.microservices.pms.project.domain.PmsProjectTestCaseExecutionStatistics">
        SELECT test_status AS testStatus, COUNT(*) AS statusCount
        FROM pms_project_testsheet_cases
        WHERE project_testsheet_id IN
        <foreach item="testSheetId" collection="testSheetIds" open="(" separator="," close=")">
            #{testSheetId}
        </foreach>
        GROUP BY test_status;
    </select>
    <select id="selectAllDistinctIssueId" resultType="java.lang.Long">
        select distinct (project_issue_id)
        from pms_project_testsheet_issues;
    </select>
</mapper>