<?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 2022 . All rights reserved. -->
<!--  Mybatis数据库持久层（业务层映射文件）：缺陷管理 -->
<!--  version: 1.0  -->
<!--  author:  maven-cctp-plugin    -->
<!--  date:  2022-01-28 13:45:44  -->
<mapper namespace="net.northking.cctp.dw.db.dao.SynBugInfoDao">

    <select id="findProIdList" resultType="net.northking.cctp.dw.db.entity.SynBugInfo">
        select
        tenant_id tenantId,
        pro_id proId
        from
        <include refid="Table_Name"></include>
        group by
        tenant_id,
        pro_id
    </select>

    <select id="findBugLifeCycleList" resultType="net.northking.cctp.dw.dto.BugLifeCycleChartAddDto">
        SELECT
        bm.*,
        COUNT(*) amount
        FROM
        (SELECT
        UUID() id,
        tenant_id tenantId,
        pro_id projectId,
        case
        when
        (TIMESTAMPDIFF(SECOND,created_time,close_time)/(60*60*24))&lt;=1 then '1d'
        when
        (TIMESTAMPDIFF(SECOND,created_time,close_time)/(60*60*24))&lt;=2 AND
        (TIMESTAMPDIFF(SECOND,created_time,close_time)/(60*60*24))>1 then '2d'
        when
        (TIMESTAMPDIFF(SECOND,created_time,close_time)/(60*60*24))&lt;=3 AND
        (TIMESTAMPDIFF(SECOND,created_time,close_time)/(60*60*24))>2 then '3d'
        when
        (TIMESTAMPDIFF(SECOND,created_time,close_time)/(60*60*24))&lt;=4 AND
        (TIMESTAMPDIFF(SECOND,created_time,close_time)/(60*60*24))>3 then '4d'
        when
        (TIMESTAMPDIFF(SECOND,created_time,close_time)/(60*60*24))&lt;=5 AND
        (TIMESTAMPDIFF(SECOND,created_time,close_time)/(60*60*24))>4 then '5d'
        when
        (TIMESTAMPDIFF(SECOND,created_time,close_time)/(60*60*24))&lt;=6 AND
        (TIMESTAMPDIFF(SECOND,created_time,close_time)/(60*60*24))>5 then '6d'
        when
        (TIMESTAMPDIFF(SECOND,created_time,close_time)/(60*60*24))&lt;=7 AND
        (TIMESTAMPDIFF(SECOND,created_time,close_time)/(60*60*24))>6 then '1w'
        when
        (TIMESTAMPDIFF(SECOND,created_time,close_time)/(60*60*24))&lt;=14 AND
        (TIMESTAMPDIFF(SECOND,created_time,close_time)/(60*60*24))>7 then '2w'
        when
        (TIMESTAMPDIFF(SECOND,created_time,close_time)/(60*60*24))&lt;=21 AND
        (TIMESTAMPDIFF(SECOND,created_time,close_time)/(60*60*24))>14 then '3w'
        when
        (TIMESTAMPDIFF(SECOND,created_time,close_time)/(60*60*24))&lt;=28 AND
        (TIMESTAMPDIFF(SECOND,created_time,close_time)/(60*60*24))>21 then '4w'
        when
        (TIMESTAMPDIFF(SECOND,created_time,close_time)/(60*60*24))>28 then '>4w'
        END lifeTime
        FROM cctp_test_bug.bug_management
        where
        pro_id=#{proId}
        and
        bug_status in
        <foreach collection="endStatusList" item="status" open="(" close=")" separator=",">
            #{status,jdbcType=VARCHAR}
        </foreach>
        and
        close_time is not null
        ) bm
        WHERE bm.lifeTime IS NOT NULL
        GROUP BY bm.lifeTime
    </select>

    <select id="queryBugTendencyMessage" resultType="net.northking.cctp.dw.dto.BugOverallTendencyChartAddDto">
        SELECT
        tmp.*,
        tmp.solved+tmp.unsolved AS allAmount
        FROM
        (
        SELECT UUID() id,
        pro_id AS projectId,
        DATE_FORMAT(now(), '%Y-%m-%d') createdTime,
        SUM(CASE WHEN bug_status IN
        <foreach collection="statusList" item="status" open="(" close=")" separator="," index="i">
            #{status,jdbcType=VARCHAR}
        </foreach>
        THEN 1 ELSE 0 END) solved,
        SUM(CASE WHEN bug_status NOT IN
        <foreach collection="statusList" item="status" open="(" close=")" separator="," index="i">
            #{status,jdbcType=VARCHAR}
        </foreach>
        THEN 1 ELSE 0 END) unsolved,
        tenant_id as tenantId
        FROM
        <include refid="Table_Name"/>
        <trim prefix="WHERE" prefixOverrides="AND">
            <if test="proId != null and proId != ''">
                AND pro_id=#{proId}
            </if>
            and DATE_FORMAT(updated_time, '%Y-%m-%d')&lt;=DATE_FORMAT(now(), '%Y-%m-%d')
        </trim>
        )tmp
    </select>
    <select id="findAllGroup" resultType="net.northking.cctp.dw.dto.BugLegacyGradeGroupDto">
        SELECT
        tenant_id tenantId,
        pro_id proId,
        module_id moduleId,
        bug_grade bugGrade,
        bug_status bugStatus,
        COUNT(*) sum
        FROM syn_bug_info
        GROUP BY pro_id,module_id,bug_grade,bug_status
        ORDER BY pro_id,module_id,bug_grade
    </select>

    <insert id="insertBugInfoByBatch" parameterType="java.util.List">
        insert into
        <include refid="Table_Name"/>
        <trim prefix="(" suffix=")" suffixOverrides=",">
            bug_id,
            bug_grade,
            bug_type,
            bug_from,
            emergency,
            bug_status,
            receiver,
            repair_time,
            repair_user,
            close_time,
            system_id,
            round_id,
            module_id,
            pro_id,
            created_by,
            created_time,
            updated_by,
            updated_time,
            tenant_id,
            case_num,
        </trim>
        values
        <foreach collection="list" item="item" index="index" separator=",">
            <trim prefix="(" suffix=")" suffixOverrides=",">
                #{item.bugId, jdbcType=VARCHAR},
                #{item.bugGrade, jdbcType=CHAR},
                #{item.bugType, jdbcType=VARCHAR},
                #{item.bugFrom, jdbcType=VARCHAR},
                #{item.emergency, jdbcType=VARCHAR},
                #{item.bugStatus, jdbcType=VARCHAR},
                #{item.receiver, jdbcType=VARCHAR},
                #{item.repairTime, jdbcType=TIMESTAMP},
                #{item.repairUser, jdbcType=VARCHAR},
                #{item.closeTime, jdbcType=TIMESTAMP},
                #{item.systemId, jdbcType=VARCHAR},
                #{item.roundId, jdbcType=VARCHAR},
                #{item.moduleId, jdbcType=VARCHAR},
                #{item.proId, jdbcType=VARCHAR},
                #{item.createdBy, jdbcType=VARCHAR},
                #{item.createdTime, jdbcType=TIMESTAMP},
                #{item.updatedBy, jdbcType=VARCHAR},
                #{item.updatedTime, jdbcType=TIMESTAMP},
                #{item.tenantId, jdbcType=VARCHAR},
                #{item.caseNum, jdbcType=INTEGER},
            </trim>
        </foreach>
    </insert>

    <select id="findTenantIdList" resultType="java.lang.String">
        select
        distinct tenant_id
        from
        <include refid="Table_Name"></include>
    </select>

    <select id="count" resultType="java.lang.Long">
        select
        count(*)
        from
        syn_bug_info
        where
        tenant_id=#{tenantId}
        and
        pro_id in
        <foreach collection="projectIds" item="projectId" open="(" separator="," close=")">
            #{projectId}
        </foreach>
    </select>
</mapper>