<?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-15 14:58:27  -->
<mapper namespace="net.northking.cctp.dw.db.dao.BugGradeStatisticsDao">


    <!-- 请在本文件内添加自定义SQL语句 -->

    <resultMap id="DetailMap" type="net.northking.cctp.dw.dto.BugGradeStatisticsDetailDto">
        <!--  -->
        <id column="id" jdbcType="VARCHAR" property="id"/>
        <!-- 分配人 -->
        <result column="receiver_name" jdbcType="VARCHAR" property="receiverName"/>
        <!-- 登陆帐号 -->
        <result column="login_no" jdbcType="VARCHAR" property="loginNo"/>
        <!-- 模块 -->
        <result column="module_id" jdbcType="VARCHAR" property="moduleId"/>
        <!-- 类型 -->
        <result column="record_type" jdbcType="CHAR" property="type"/>
        <!-- 致命性错误-A类 -->
        <result column="A" jdbcType="INTEGER" property="a"/>
        <!-- 严重性错误-B类 -->
        <result column="B" jdbcType="INTEGER" property="b"/>
        <!-- 一般性错误-C类 -->
        <result column="C" jdbcType="INTEGER" property="c"/>
        <!-- 警告性错误-D类 -->
        <result column="D" jdbcType="INTEGER" property="d"/>
        <!-- 建议性错误-E类 -->
        <result column="E" jdbcType="INTEGER" property="e"/>
        <!-- 项目id -->
        <result column="project_id" jdbcType="VARCHAR" property="projectId"/>
        <!-- 创建时间 -->
        <result column="created_time" jdbcType="TIMESTAMP" property="createdTime"/>
        <!-- 租户id -->
        <result column="tenant_id" jdbcType="VARCHAR" property="tenantId"/>
    </resultMap>

    <select id="pagingQuery" resultMap="DetailMap" parameterType="net.northking.cctp.dw.db.entity.BugGradeStatistics">
        select
        id
        ,receiver_name
        ,login_no
        ,module_id
        ,record_type
        ,A
        ,B
        ,C
        ,D
        ,E
        ,project_id
        ,created_time
        ,tenant_id
        from
        <include refid="Table_Name"/>
        <trim prefix="WHERE" prefixOverrides="AND">
            <if test="id != null">
                AND id = #{id,jdbcType=VARCHAR}
            </if>
            <if test="receiverName != null">
                AND receiver_name = #{receiverName,jdbcType=VARCHAR}
            </if>
            <if test="loginNo != null">
                AND login_no = #{loginNo,jdbcType=VARCHAR}
            </if>
            <if test="moduleId != null">
                AND module_id = #{moduleId,jdbcType=VARCHAR}
            </if>
            <if test="type != null">
                AND record_type = #{type,jdbcType=CHAR}
            </if>
            <if test="a != null">
                AND A = #{a,jdbcType=INTEGER}
            </if>
            <if test="b != null">
                AND B = #{b,jdbcType=INTEGER}
            </if>
            <if test="c != null">
                AND C = #{c,jdbcType=INTEGER}
            </if>
            <if test="d != null">
                AND D = #{d,jdbcType=INTEGER}
            </if>
            <if test="e != null">
                AND E = #{e,jdbcType=INTEGER}
            </if>
            <if test="projectId != null">
                AND project_id = #{projectId,jdbcType=VARCHAR}
            </if>
            <if test="createdTime != null">
                AND created_time = #{createdTime,jdbcType=TIMESTAMP}
            </if>
            <if test="tenantId != null">
                AND tenant_id = #{tenantId,jdbcType=VARCHAR}
            </if>
        </trim>
    </select>

    <select id="listQuery" resultType="net.northking.cctp.dw.dto.BugGradeStatisticsDetailDto">
        SELECT
        <if test="recordType=='0'.toString()">
            receiver_name receiverName,
            login_no loginNo,
        </if>
        <if test="recordType=='1'.toString()">
            module_id moduleId,
        </if>
        sum(A) A,
        sum(B) B,
        sum(C) C,
        sum(D) D,
        sum(E) E
        FROM
        <include refid="Table_Name"/>
        <trim prefix="WHERE" prefixOverrides="AND">
            <if test="tenantId != null">
                AND tenant_id = #{tenantId,jdbcType=VARCHAR}
            </if>
            <if test="proId != null">
                AND project_id = #{proId,jdbcType=VARCHAR}
            </if>
            <if test="startTime != null and endTime !=null and startTime != '' and endTime !=''">
                AND date_format(created_time,'%Y-%m-%d') between #{startTime} and #{endTime}
            </if>
            <if test="dateType!=null and dateType!=''">
                <!--        本周-->
                <if test="dateType=='0'.toString()">
                    and date_format(created_time,'%Y-%m-%d') between date_format(DATE_SUB(CURDATE(),INTERVAL
                    DAYOFWEEK(curdate()) DAY),'%Y-%m-%d') and date_format(curdate(),'%Y-%m-%d')
                </if>
                <!--        本日-->
                <if test="dateType=='1'.toString()">
                    and date_format(created_time,'%Y-%m-%d')=date_format(curdate(),'%Y-%m-%d')
                </if>
            </if>
            AND record_type=#{recordType}
        </trim>
        GROUP BY
        <if test="recordType=='0'.toString()">
            receiver_name
        </if>
        <if test="recordType=='1'.toString()">
            module_id
        </if>
    </select>

    <insert id="insertBatch">
        insert into
        bug_grade_statistics
        (id,receiver_name,login_no,module_id,record_type,a,b,c,d,e,created_time,tenant_id,project_id)
        select
        UUID() id,
        tmp.*
        from
        (
        (
        SELECT
        b.created_by receiver_name,
        b.created_by login_no,
        null module_id,
        0 record_type,
        cast((SUM(case bug_grade when 'A' then 1 ELSE 0 END)+0) as signed)a,
        cast((SUM(case bug_grade when 'B' then 1 ELSE 0 END)+0) as signed)b,
        cast((SUM(case bug_grade when 'C' then 1 ELSE 0 END)+0) as signed)c,
        cast((SUM(case bug_grade when 'D' then 1 ELSE 0 END)+0) as signed)d,
        cast((SUM(case bug_grade when 'E' then 1 ELSE 0 END)+0) as signed)e,
        date_format(curdate(),'%Y-%m-%d') created_time,
        b.tenant_id tenant_id,
        b.pro_id project_id
        FROM syn_bug_info b
        WHERE
        b.tenant_id=#{tenantId}
        and
        b.pro_id in
        <foreach collection="dataList" open="(" separator="," close=")" item="synBugInfo">
            #{synBugInfo.proId}
        </foreach>
        GROUP BY b.created_by,b.pro_id
        )
        union
        (
        SELECT
        b.created_by receiver_name,
        b.created_by login_no,
        null module_id,
        2 record_type,
        cast((SUM(case bug_grade when 'A' then 1 ELSE 0 END)+0) as signed)a,
        cast((SUM(case bug_grade when 'B' then 1 ELSE 0 END)+0) as signed)b,
        cast((SUM(case bug_grade when 'C' then 1 ELSE 0 END)+0) as signed)c,
        cast((SUM(case bug_grade when 'D' then 1 ELSE 0 END)+0) as signed)d,
        cast((SUM(case bug_grade when 'E' then 1 ELSE 0 END)+0) as signed)e,
        date_format(curdate(),'%Y-%m-%d') created_time,
        b.tenant_id tenant_id,
        b.pro_id project_id
        FROM syn_bug_info b
        where
        b.tenant_id=#{tenantId}
        and
        b.pro_id in
        <foreach collection="dataList" open="(" separator="," close=")" item="synBugInfo">
            #{synBugInfo.proId}
        </foreach>
        and
        date_format(b.created_time,'%Y-%m-%d')=date_format(curdate(),'%Y-%m-%d')
        GROUP BY b.created_by,b.pro_id
        )
        union
        (
        SELECT
        null receiver_name,
        b.created_by login_no,
        b.module_id module_id,
        1 record_type,
        cast((SUM(case bug_grade when 'A' then 1 ELSE 0 END)+0) as signed)a,
        cast((SUM(case bug_grade when 'B' then 1 ELSE 0 END)+0) as signed)b,
        cast((SUM(case bug_grade when 'C' then 1 ELSE 0 END)+0) as signed)c,
        cast((SUM(case bug_grade when 'D' then 1 ELSE 0 END)+0) as signed)d,
        cast((SUM(case bug_grade when 'E' then 1 ELSE 0 END)+0) as signed)e,
        date_format(curdate(),'%Y-%m-%d') created_time,
        b.tenant_id tenant_id,
        b.pro_id project_id
        FROM syn_bug_info b
        WHERE
        b.tenant_id=#{tenantId}
        and
        b.pro_id in
        <foreach collection="dataList" open="(" separator="," close=")" item="synBugInfo">
            #{synBugInfo.proId}
        </foreach>
        GROUP BY module_id,b.pro_id
        )
        union
        (
        SELECT
        b.created_by receiver_name,
        b.created_by login_no,
        null module_id,
        3 record_type,
        cast((SUM(case bug_grade when 'A' then 1 ELSE 0 END)+0) as signed)a,
        cast((SUM(case bug_grade when 'B' then 1 ELSE 0 END)+0) as signed)b,
        cast((SUM(case bug_grade when 'C' then 1 ELSE 0 END)+0) as signed)c,
        cast((SUM(case bug_grade when 'D' then 1 ELSE 0 END)+0) as signed)d,
        cast((SUM(case bug_grade when 'E' then 1 ELSE 0 END)+0) as signed)e,
        REPLACE(SUBDATE(CURDATE(),if(DATE_FORMAT(CURDATE(),'%w')=6,0,DATE_FORMAT(CURDATE(),'%w')+1)),"-","") as
        created_time,
        b.tenant_id tenant_id,
        b.pro_id project_id
        FROM syn_bug_info b
        where
        (date(created_time) BETWEEN SUBDATE(CURDATE(),if(DATE_FORMAT(CURDATE(),'%w')=6,0,DATE_FORMAT(CURDATE(),'%w')+1))
        AND CURDATE())
        AND
        b.tenant_id=#{tenantId}
        and
        b.pro_id in
        <foreach collection="dataList" open="(" separator="," close=")" item="synBugInfo">
            #{synBugInfo.proId}
        </foreach>
        GROUP BY b.created_by,b.pro_id
        )
        )tmp
    </insert>
    <delete id="deleteData">
        delete from
        bug_grade_statistics
        where
        project_id in
        <foreach collection="dataList" open="(" separator="," close=")" item="synBugInfo">
            #{synBugInfo.proId}
        </foreach>
        and
        tenant_id=#{tenantId}
        and (
        (created_time=REPLACE(SUBDATE(CURDATE(),if(DATE_FORMAT(CURDATE(),'%w')=6,0,DATE_FORMAT(CURDATE(),'%w')+1)),"-","")
        and record_type in('3'))
        or
        (created_time=date_format(NOW(),'%Y-%m-%d') and record_type not in('3')) )
    </delete>
</mapper>