<?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.product.mapper.PmsProductModuleMapper">
    
    <resultMap type="PmsProductModule" id="PmsProductModuleResult">
        <result property="id"    column="id"    />
        <result property="moduleName"    column="module_name"    />
        <result property="pmsProductIdentifier"    column="pms_product_identifier"    />
        <result property="parentId"    column="parent_id"    />
        <result property="sort"    column="sort"    />
        <result property="updateTime"    column="update_time"    />
        <result property="updateBy"    column="update_by"    />
        <result property="createTime"    column="create_time"    />
        <result property="createBy"    column="create_by"    />
        <result property="delFlag"    column="del_flag"    />
        <result property="requirementCount"    column="requirementCount"    />
        <result property="type" column="type"/>
        <result property="pmsProjectId" column="pms_project_id"/>
    </resultMap>

    <sql id="selectPmsProductModuleVo">
        select id,
               module_name,
               pms_product_identifier,
               parent_id,
               sort,
               update_time,
               update_by,
               create_time,
               create_by,
               del_flag,
               type,
               pms_project_id
        from pms_product_module
    </sql>

    <sql id="selectPmsProductModuleAliasVo">
        select m.id
             , m.module_name
             , m.pms_product_identifier
             , m.parent_id
             , m.sort
             , m.update_time
             , m.update_by
             , m.create_time
             , m.create_by
             , m.del_flag
             , m.type
             , m.pms_project_id
                 ,(
                    SELECT COUNT(*)
                    FROM pms_product_requirement r
                    WHERE r.pms_product_module_id = m.id
                ) AS requirementCount
             , (SELECT COUNT(*)
                FROM pms_project_testcase t
                WHERE t.pms_module_id = m.id) AS testCaseCount
        from pms_product_module as m
    </sql>

    <select id="selectPmsProductModuleList" parameterType="PmsProductModule" resultMap="PmsProductModuleResult">
        <include refid="selectPmsProductModuleAliasVo"/>
        <where>  
            <if test="moduleName != null  and moduleName != ''"> and m.module_name like concat('%', #{moduleName}, '%')</if>
            <if test="pmsProductIdentifier != null "> and m.pms_product_identifier = #{pmsProductIdentifier}</if>
            <if test="parentId != null "> and m.parent_id = #{parentId}</if>
            <if test="pmsProjectId != null ">and m.pms_project_id = #{pmsProjectId}</if>
            <if test="sort != null "> and m.sort = #{sort}</if>
            <if test="type != null ">and m.type = #{type}</if>
        </where>
    </select>

    <select id="selectPmsProductModuleForTestsheetList" parameterType="PmsProductModule" resultMap="PmsProductModuleResult">
        select m.id
        , m.module_name
        , m.pms_product_identifier
        , m.parent_id
        , m.sort
        , m.update_time
        , m.update_by
        , m.create_time
        , m.create_by
        , m.del_flag
        , m.type
        , m.pms_project_id
        ,(
        SELECT COUNT(*)
        FROM pms_product_requirement r
        WHERE r.pms_product_module_id = m.id
        ) AS requirementCount
        , (SELECT COUNT(*)
        FROM pms_project_testcase t
        WHERE t.pms_module_id = m.id) AS testCaseCount
        , (SELECT COUNT(*)
        FROM pms_project_testsheet_cases tsc left join pms_project_testcase tc on tsc.project_testcase_id = tc.id
        WHERE tc.pms_module_id = m.id and tsc.project_testsheet_id = #{params.projectTestsheetId}) AS testsheetCasesCount
        from pms_product_module as m
        <where>
            <if test="moduleName != null  and moduleName != ''"> and m.module_name like concat('%', #{moduleName}, '%')</if>
            <if test="pmsProductIdentifier != null "> and m.pms_product_identifier = #{pmsProductIdentifier}</if>
            <if test="parentId != null "> and m.parent_id = #{parentId}</if>
            <if test="pmsProjectId != null ">and m.pms_project_id = #{pmsProjectId}</if>
            <if test="sort != null "> and m.sort = #{sort}</if>
            <if test="type != null ">and m.type = #{type}</if>
        </where>
    </select>
    
    <select id="selectPmsProductModuleById" parameterType="Long" resultMap="PmsProductModuleResult">
        <include refid="selectPmsProductModuleVo"/>
        where id = #{id}
    </select>
    <select id="selectPmsProductModuleCountByNameAndProductIdentifier" resultType="java.lang.Integer">
        select count(0) from pms_product_module
        <where>
            module_name = #{moduleName}
            and pms_product_identifier=#{productIdentifier}
        </where>
    </select>
    <select id="selectPmsProductModuleCountByNameAndProjectId" resultType="java.lang.Integer">
        select count(0) from pms_product_module
        <where>
            module_name = #{moduleName}
            and pms_project_id=#{pmsProjectId}
        </where>
    </select>
    <select id="selectModuleStatisticsByTestCaseIds"
            resultType="com.microservices.pms.project.domain.PmsProjectModuleStatistics">
        SELECT
        m.module_name AS moduleName,
        COUNT(tc.id) AS caseCount,
        COUNT(tsi.id) AS issueCount
        FROM
        pms_product_module m
        JOIN
        pms_project_testcase tc ON m.id = tc.`pms_module_id`
        LEFT JOIN
        pms_project_testsheet_cases tsc ON tc.id = tsc.`project_testcase_id`
        LEFT JOIN
        pms_project_testsheet_issues tsi ON tsc.id = tsi.`project_testsheet_cases_id`
        WHERE tc.id IN
        <foreach item="testCaseId" collection="testCaseIds" open="(" separator="," close=")">
            #{testCaseId}
        </foreach>
        AND tsc.project_testsheet_id IN
        <foreach item="testSheetId" collection="testSheetIds" open="(" separator="," close=")">
            #{testSheetId}
        </foreach>
        GROUP BY
        m.module_name
        UNION
        SELECT
        '无所属模块' AS moduleName,
        COUNT(tc.id) AS caseCount,
        COUNT(tsi.id) AS issueCount
        FROM
        pms_product_module m
        RIGHT JOIN
        pms_project_testcase tc ON m.id = tc.`pms_module_id`
        LEFT JOIN
        pms_project_testsheet_cases tsc ON tc.id = tsc.`project_testcase_id`
        LEFT JOIN
        pms_project_testsheet_issues tsi ON tsc.id = tsi.`project_testsheet_cases_id`
        WHERE tc.`pms_module_id` IS NULL
        AND tc.id IN
        <foreach item="testCaseId" collection="testCaseIds" open="(" separator="," close=")">
            #{testCaseId}
        </foreach>
        AND tsc.project_testsheet_id IN
        <foreach item="testSheetId" collection="testSheetIds" open="(" separator="," close=")">
            #{testSheetId}
        </foreach>
        GROUP BY
        moduleName;
    </select>
    <select id="selectPmsProductModuleListByNameAndProductIdentifier" resultMap="PmsProductModuleResult">
        <include refid="selectPmsProductModuleVo"/>
        <where>
            module_name = #{moduleName}
            and pms_product_identifier=#{pmsProductIdentifier}
        </where>
    </select>
    <select id="selectPmsProductModuleListByNameAndProjectId" resultMap="PmsProductModuleResult">
        <include refid="selectPmsProductModuleVo"/>
        <where>
            module_name = #{moduleName}
            and pms_project_id=#{pmsProjectId}
        </where>
    </select>

    <insert id="insertPmsProductModule" parameterType="PmsProductModule" useGeneratedKeys="true" keyProperty="id">
        insert into pms_product_module
        <trim prefix="(" suffix=")" suffixOverrides=",">
            <if test="moduleName != null and moduleName != ''">module_name,</if>
            <if test="pmsProductIdentifier != null">pms_product_identifier,</if>
            <if test="parentId != null">parent_id,</if>
            <if test="sort != null">sort,</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>
            <if test="delFlag != null">del_flag,</if>
            <if test="type != null">type,</if>
            <if test="pmsProjectId != null">pms_project_id,</if>
         </trim>
        <trim prefix="values (" suffix=")" suffixOverrides=",">
            <if test="moduleName != null and moduleName != ''">#{moduleName},</if>
            <if test="pmsProductIdentifier != null">#{pmsProductIdentifier},</if>
            <if test="parentId != null">#{parentId},</if>
            <if test="sort != null">#{sort},</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>
            <if test="delFlag != null">#{delFlag},</if>
            <if test="type != null">#{type},</if>
            <if test="pmsProjectId != null">#{pmsProjectId},</if>
         </trim>
    </insert>

    <update id="updatePmsProductModule" parameterType="PmsProductModule">
        update pms_product_module
        <trim prefix="SET" suffixOverrides=",">
            <if test="moduleName != null and moduleName != ''">module_name = #{moduleName},</if>
            <if test="pmsProductIdentifier != null">pms_product_identifier = #{pmsProductIdentifier},</if>
            <if test="parentId != null">parent_id = #{parentId},</if>
            <if test="sort != null">sort = #{sort},</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>
            <if test="delFlag != null">del_flag = #{delFlag},</if>
            <if test="type != null">type = #{type},</if>
            <if test="pmsProjectId != null">pms_project_id = #{pmsProjectId},</if>
        </trim>
        where id = #{id}
    </update>

    <delete id="deletePmsProductModuleById" parameterType="Long">
        delete from pms_product_module where id = #{id}
    </delete>

    <delete id="deletePmsProductModuleByIds" parameterType="String">
        delete from pms_product_module where id in 
        <foreach item="id" collection="array" open="(" separator="," close=")">
            #{id}
        </foreach>
    </delete>
    <delete id="deletePmsProductModuleByProductIdentifier">
        delete
        from pms_product_module
        where pms_product_identifier = #{productIdentifier}
    </delete>
</mapper>