<?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.BugStatusStatisticsDao">


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

    <resultMap id="DetailMap" type="net.northking.cctp.dw.dto.BugStatusStatisticsDetailDto">
        <!--  -->
        <id column="id" jdbcType="VARCHAR" property="id"/>
        <!-- 分配人 -->
        <result column="receiver_name" jdbcType="VARCHAR" property="receiverName"/>
        <!-- 模块 -->
        <result column="module_id" jdbcType="VARCHAR" property="moduleId"/>
        <!-- 类型 -->
        <result column="record_type" jdbcType="CHAR" property="type"/>
        <!-- 项目id -->
        <result column="project_id" jdbcType="VARCHAR" property="projectId"/>
        <!-- 缺陷状态 -->
        <result column="bug_status" jdbcType="VARCHAR" property="bugStatus"/>
        <!-- 缺陷状态码 -->
        <result column="bug_status_code" jdbcType="VARCHAR" property="bugStatusCode"/>
        <!-- 数量 -->
        <result column="amount" jdbcType="INTEGER" property="amount"/>
        <!-- 创建时间 -->
        <result column="created_time" jdbcType="VARCHAR" property="createdTime"/>
        <!-- 租户id -->
        <result column="tenant_id" jdbcType="VARCHAR" property="tenantId"/>
    </resultMap>

    <select id="pagingQuery" resultMap="DetailMap" parameterType="net.northking.cctp.dw.db.entity.BugStatusStatistics">
        select
        id
        ,receiver_name
        ,module_id
        ,record_type
        ,project_id
        ,bug_status
        ,bug_status_code
        ,amount
        ,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="moduleId != null">
                AND module_id = #{moduleId,jdbcType=VARCHAR}
            </if>
            <if test="type != null">
                AND record_type = #{type,jdbcType=CHAR}
            </if>
            <if test="projectId != null">
                AND project_id = #{projectId,jdbcType=VARCHAR}
            </if>
            <if test="bugStatus != null">
                AND bug_status = #{bugStatus,jdbcType=VARCHAR}
            </if>
            <if test="bugStatusCode != null">
                AND bug_status_code = #{bugStatusCode,jdbcType=VARCHAR}
            </if>
            <if test="amount != null">
                AND amount = #{amount,jdbcType=INTEGER}
            </if>
            <if test="createdTime != null">
                AND created_time = #{createdTime,jdbcType=VARCHAR}
            </if>
            <if test="tenantId != null">
                AND tenant_id = #{tenantId,jdbcType=VARCHAR}
            </if>
        </trim>
    </select>

    <select id="listQuery" resultType="net.northking.cctp.dw.dto.BugStatusStatisticsDetailDto">
        SELECT
        <if test="recordType=='0'.toString()">
            receiver_name receiverName,
        </if>
        <if test="recordType=='1'.toString()">
            module_id moduleId,
        </if>
        bug_status_code bugStatusCode,
        sum(amount) amount,
        bug_status bugStatus
        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 created_time between #{startTime} and #{endTime}
            </if>
            <if test="dateType!=null and dateType!=''">
                <!--        本周-->
                <if test="dateType=='0'.toString()">
                    and created_time 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 created_time=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>
        bug_status_code
    </select>

    <insert id="insertBatch">
        insert into
        bug_status_statistics(id,receiver_name,module_id,record_type,bug_status_code,amount,created_time,tenant_id,project_id,bug_status)
        select
        tmp.*
        from
        (
        <!--        项目级按人员-->
        (
        SELECT
        UUID() id,
        created_by receiver_name,
        null module_id,
        0 record_type,
        syn_bug_info.bug_status bug_status_code,
        COUNT(bug_status) AS amount,
        date_format(now(),'%Y-%m-%d') as created_time,
        tenant_id,
        pro_id project_id,
        case bug_status
        <foreach collection="dictItems" item="item">
            when #{item.itemCode} then #{item.itemName}
        </foreach>
        end as bug_status
        FROM
        syn_bug_info
        where
        tenant_id=#{tenantId}
        and
        pro_id in
        <foreach collection="dataList" item="synBugInfo" open="(" separator="," close=")">
            #{synBugInfo.proId}
        </foreach>
        <!--date_format(updated_time,'%Y-%m-%d')=date_format(now(),'%Y-%m-%d') and-->
        GROUP BY created_by,syn_bug_info.bug_status,pro_id
        )
        <!--        项目级按模块-->
        union
        (
        SELECT
        UUID() id,
        null receiver_name,
        module_id,
        1 record_type,
        syn_bug_info.bug_status bug_status_code,
        COUNT(bug_status) AS amount,
        date_format(now(),'%Y-%m-%d') as created_time,
        tenant_id,
        pro_id project_id,
        case bug_status
        <foreach collection="dictItems" item="item">
            when #{item.itemCode} then #{item.itemName}
        </foreach>
        end as bug_status
        from
        syn_bug_info
        where <!--date_format(updated_time,'%Y-%m-%d')=date_format(now(),'%Y-%m-%d') and-->
        tenant_id=#{tenantId}
        and
        pro_id in
        <foreach collection="dataList" item="synBugInfo" open="(" separator="," close=")">
            #{synBugInfo.proId}
        </foreach>
        GROUP BY module_id,syn_bug_info.bug_status,pro_id
        )
        <!--        每日按人员-->
        union
        (
        SELECT
        UUID() id,
        created_by receiver_name,
        null module_id,
        2 record_type,
        syn_bug_info.bug_status bug_status_code,
        COUNT(bug_status) AS amount,
        date_format(now(),'%Y-%m-%d') as created_time,
        tenant_id,
        pro_id project_id,
        case bug_status
        <foreach collection="dictItems" item="item">
            when #{item.itemCode} then #{item.itemName}
        </foreach>
        end as bug_status
        FROM
        syn_bug_info
        where
        date_format(created_time,'%Y-%m-%d')=date_format(now(),'%Y-%m-%d')
        and
        tenant_id=#{tenantId}
        and
        pro_id in
        <foreach collection="dataList" item="synBugInfo" open="(" separator="," close=")">
            #{synBugInfo.proId}
        </foreach>
        GROUP BY created_by,syn_bug_info.bug_status,pro_id
        )
        <!--        每日按模块-->
        union
        (
        SELECT
        UUID() id,
        null receiver_name,
        module_id,
        3 record_type,
        syn_bug_info.bug_status bug_status_code,
        COUNT(bug_status) AS amount,
        date_format(now(),'%Y-%m-%d') as created_time,
        tenant_id,
        pro_id project_id,
        case bug_status
        <foreach collection="dictItems" item="item">
            when #{item.itemCode} then #{item.itemName}
        </foreach>
        end as bug_status
        from
        syn_bug_info
        where date_format(created_time,'%Y-%m-%d')=date_format(now(),'%Y-%m-%d')
        and
        tenant_id=#{tenantId}
        and
        pro_id in
        <foreach collection="dataList" item="synBugInfo" open="(" separator="," close=")">
            #{synBugInfo.proId}
        </foreach>
        GROUP BY module_id,syn_bug_info.bug_status,pro_id
        )
        )tmp
        where tmp.bug_status is not null
    </insert>
    <insert id="insertBatchWeekData">
        insert into
        bug_status_statistics(id,receiver_name,module_id,record_type,bug_status_code,amount,created_time,tenant_id,project_id,bug_status)
        select
        tmp.*
        from
        (
        <!--每周按人员 -->
        (
        SELECT
        UUID() id,
        created_by receiver_name,
        null module_id,
        4 record_type,
        syn_bug_info.bug_status bug_status_code,
        COUNT(bug_status) AS amount,
        REPLACE(SUBDATE(CURDATE(),if(DATE_FORMAT(CURDATE(),'%w')=6,0,DATE_FORMAT(CURDATE(),'%w')+1)),"-","") as
        created_time,
        tenant_id,
        pro_id project_id,
        case bug_status
        <foreach collection="dictItems" item="item">
            when #{item.itemCode} then #{item.itemName}
        </foreach>
        end as bug_status
        FROM
        syn_bug_info
        where
        (date(created_time) BETWEEN SUBDATE(CURDATE(),if(DATE_FORMAT(CURDATE(),'%w')=6,0,DATE_FORMAT(CURDATE(),'%w')+1))
        AND CURDATE())
        and
        tenant_id=#{tenantId}
        and
        pro_id in
        <foreach collection="dataList" item="synBugInfo" open="(" separator="," close=")">
            #{synBugInfo.proId}
        </foreach>
        GROUP BY created_by,syn_bug_info.bug_status,pro_id
        )
        <!--   每周按模块-->
        union
        (
        SELECT
        UUID() id,
        null receiver_name,
        module_id,
        5 record_type,
        syn_bug_info.bug_status bug_status_code,
        COUNT(bug_status) AS amount,
        REPLACE(SUBDATE(CURDATE(),if(DATE_FORMAT(CURDATE(),'%w')=6,0,DATE_FORMAT(CURDATE(),'%w')+1)),"-","") as
        created_time,
        tenant_id,
        pro_id project_id,
        case bug_status
        <foreach collection="dictItems" item="item">
            when #{item.itemCode} then #{item.itemName}
        </foreach>
        end as bug_status
        from
        syn_bug_info
        where ((date(created_time) BETWEEN
        SUBDATE(CURDATE(),if(DATE_FORMAT(CURDATE(),'%w')=6,0,DATE_FORMAT(CURDATE(),'%w')+1)) AND CURDATE()))
        and
        tenant_id=#{tenantId}
        and
        pro_id in
        <foreach collection="dataList" item="synBugInfo" open="(" separator="," close=")">
            #{synBugInfo.proId}
        </foreach>
        GROUP BY module_id,syn_bug_info.bug_status,pro_id
        )
        )tmp
        where tmp.bug_status is not null
    </insert>

    <delete id="deleteData">
        delete from
        bug_status_statistics
        where
        project_id in
        <foreach collection="dataList" item="synBugInfo" open="(" separator="," close=")">
            #{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('4','5'))
    or
     (created_time=date_format(NOW(),'%Y-%m-%d') and record_type not in('4','5'))
    )
    </delete>

    <select id="queryUnresolvedBugModuleData" resultType="net.northking.cctp.dw.db.entity.BugStatusStatistics">
        select
        module_id moduleId,
        SUM(amount)-SUM(CASE WHEN bug_status_code IN
        <foreach collection="endStatuss" item="status" open="(" close=")" separator="," index="i">
            #{status,jdbcType=VARCHAR}
        </foreach>
        THEN amount ELSE 0 END) amount
        from
        bug_status_statistics
        where
        record_type='1'
        and
        created_time=date_format(NOW(),'%Y-%m-%d')
        and
        project_id=#{projectId}
        group by
        module_id
    </select>

    <select id="queryTop5TypeBugDataByTenantId" resultType="net.northking.cctp.dw.db.entity.BugStatusStatistics">
            select
            bug_type bugStatus,
            count(bug_type) amount
            from
            syn_bug_info
            where
            tenant_id=#{tenantId}
            and
            pro_id in
            <foreach collection="projectIds" close=")" separator="," open="(" item="projectId">
                #{projectId}
            </foreach>
            group by
            bug_type
            order by amount desc
            limit 5
    </select>
</mapper>