<?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="net.educoder.ecsonar.dao.IssuesDao">


    <select id="getPageIssuesCount" resultType="java.lang.Integer">
        select
        count(1)
        from issues i
        inner join rules r on i.rule_id =r.id
        inner join projects p on p.uuid =i.component_uuid
        where i.project_uuid = #{projectUuid,jdbcType=VARCHAR}
        and p.root_uuid  =#{projectUuid,jdbcType=VARCHAR} and p.uuid !=#{projectUuid,jdbcType=VARCHAR} and p."scope" ='FIL'
        <if test="issueType != null">
            and i.issue_type=#{issueType,jdbcType=INTEGER}
        </if>
        <if test="severity != null">
            and i.severity=#{severity,jdbcType=VARCHAR}
        </if>
    </select>


    <select id="getPageIssues" resultType="net.educoder.ecsonar.model.Issues">
        select
        i.id,i.kee,i.rule_id ruleId,i.severity,i.status,i.project_uuid projectUuid,i.issue_type issueType,i.locations,r.name,r.description,p.path,p.uuid,i.message
        from issues i
        inner join rules r on i.rule_id =r.id
        inner join projects p on p.uuid =i.component_uuid
        where i.project_uuid = #{projectUuid,jdbcType=VARCHAR}
        and p.root_uuid  =#{projectUuid,jdbcType=VARCHAR} and p.uuid !=#{projectUuid,jdbcType=VARCHAR} and p."scope" ='FIL'
        <if test="issueType != null">
            and i.issue_type=#{issueType,jdbcType=INTEGER}
        </if>
        <if test="severity != null">
            and i.severity=#{severity,jdbcType=VARCHAR}
        </if>
        limit #{pageSize} offset #{start}
    </select>

    <select id="queryDegree" resultType="net.educoder.ecsonar.model.dto.DegreeDTO">
        select
        (select count(1) from issues i  where project_uuid =#{projectUuid} and issue_type=#{issueType}) total,
        (select count(1) from issues i  where project_uuid =#{projectUuid} and issue_type=#{issueType} and severity = 'MAJOR') major,
        (select count(1) from issues i  where project_uuid =#{projectUuid} and issue_type=#{issueType} and severity = 'MINOR')  minor,
        (select count(1) from issues i  where project_uuid =#{projectUuid} and issue_type=#{issueType} and severity = 'BLOCKER')  blocker,
        (select count(1) from issues i  where project_uuid =#{projectUuid} and issue_type=#{issueType} and severity = 'CRITICAL')  critical,
        (select text_value from project_measures i  where component_uuid =#{projectUuid}  and metric_id = 80 order by id desc limit 1) levelStr
    </select>


</mapper>
