<?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">
<!--  Copyright (c) 京北方信息技术股份有限公司 Corporation 2021 . All rights reserved. -->
<!--  Mybatis数据库持久层（业务层映射文件）：缺陷密度分布图 -->
<!--  version: 1.0  -->
<!--  author:  maven-cctp-plugin    -->
<!--  date:  2021-12-17 15:58:25  -->
<mapper namespace="net.northking.cctp.dw.db.dao.BugDensityChartDao">
    <insert id="addByBatch" parameterType="net.northking.cctp.dw.db.entity.BugDensityChart">
        insert into
        <include refid="Table_Name"/>
        <trim prefix="(" suffix=")" suffixOverrides=",">
            user_name,
            login_no,
            bug_num,
            exec_count,
            bug_density,
            project_id,
            tenant_id,
        </trim>
        values
        <foreach collection="list" item="item" index="index" separator=",">
            <trim prefix="(" suffix=")" suffixOverrides=",">
                #{item.userName, jdbcType=VARCHAR},
                #{item.loginNo, jdbcType=VARCHAR},
                #{item.bugNum, jdbcType=REAL},
                #{item.execCount, jdbcType=INTEGER},
                #{item.bugDensity, jdbcType=REAL},
                #{item.projectId, jdbcType=VARCHAR},
                #{item.tenantId, jdbcType=VARCHAR},
            </trim>
        </foreach>
    </insert>
    <insert id="insertBatch">
        INSERT INTO
        bug_density_chart(tenant_id,project_id,user_name,login_no,exec_count,bug_density,bug_num)
        SELECT
        tmp.tenant_id,
        tmp.project_id,
        tmp.user_name,
        tmp.login_no,
        tmp.exec_count,
        CONVERT((IFNULL(tmp.a*5,0)+ IFNULL(tmp.b*3,0)+ IFNULL(tmp.c,0)+ IFNULL(tmp.d*0.3,0)+ IFNULL(tmp.e*0.1,0))/IFNULL(tmp.exec_count,0), DECIMAL(10,2)) bug_density,
        CONVERT((IFNULL(tmp.a*5,0)+ IFNULL(tmp.b*3,0)+ IFNULL(tmp.c,0)+ IFNULL(tmp.d*0.3,0)+ IFNULL(tmp.e*0.1,0)), DECIMAL(10,2)) bug_num
        FROM
        (
        select
        mv.tenant_id,
        mv.project_id,
        mv.created_by user_name,
        mv.created_by login_no, cast((SUM(mv.valid_exec)+0) as signed) exec_count, cast((SUM(mu.grade_a)+0) as signed)a, cast((SUM(mu.grade_b)+0) as signed)b, cast((SUM(mu.grade_c)+0) as signed)c, cast((SUM(mu.grade_d)+0) as signed)d, cast((SUM(mu.grade_e)+0)as signed)e
        FROM
        mid_valid_exec_job mv
        LEFT JOIN
        mid_user_bug_grade mu ON mv.project_id=mu.project_id AND mv.created_by=mu.created_by
        WHERE
        mv.tenant_id=#{tenantId}
        and
        mv.project_id in
        <foreach collection="dataList" item="data" open="(" close=")" separator=",">
            #{data.projectId}
        </foreach>
        GROUP BY mv.created_by,mv.project_id
        )tmp
        WHERE tmp.exec_count!=0
    </insert>
    <select id="getProjectList" resultType="net.northking.cctp.dw.db.entity.BugDensityChart">
<!--        select-->
<!--        tenant_id tenantId,-->
<!--        project_id projectId-->
<!--        from-->
<!--        bug_density_chart-->
<!--        group by-->
<!--        tenant_id,-->
<!--        project_id-->
        select
          tenant_id tenantId,
          pro_id projectId
        from
        syn_bug_info
        group by
          tenant_id,
        pro_id
    </select>
    <delete id="deleteByData">
        delete
        from bug_density_chart
        where
        tenant_id=#{tenantId}
        and
        project_id in
        <foreach collection="dataList" item="data" open="(" close=")" separator=",">
            #{data.projectId}
        </foreach>
    </delete>
    <select id="queryBugDensityByTenantId" resultType="net.northking.cctp.dw.dto.ProjectDataVo">
     select
        tmp1.system_id systemId,
        tmp1.bug_num/tmp3.valid_count bugDensity
        from
        (
        select
        system_id,
        sum(case when bug_grade='A' then 5 when bug_grade='B' then 3 when bug_grade='C' then 1 when bug_grade='D' then 0.3 when bug_grade='E' then 0.1 else 0 end ) bug_num
        from
        syn_bug_info
        where
        tenant_id=#{tenantId}
        and
        pro_id in
        <foreach collection="projectIds" close=")" separator="," open="(" item="projectId">
            #{projectId}
        </foreach>
        group by system_id
        )tmp1
        inner join
        (
        select
        tmp2.system,
        sum(case when tmp2.exec_status not in(0,1) then 1 else 0 end) valid_count
        from
            (
            select
            di.system,
            dj.*
            from
            dw_case_design_info di inner join dw_manual_exec_job dj
            on di.id=dj.case_id
            where di.tenant_id=#{tenantId}
            and di.project_id in
            <foreach collection="projectIds" close=")" separator="," open="(" item="projectId">
                #{projectId}
            </foreach>
            )tmp2
        group by tmp2.system
        )tmp3
        on tmp1.system_id=tmp3.system
        where tmp3.valid_count>0
        order by bugDensity desc
    </select>
</mapper>