<?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-20 09:48:21  -->
<mapper namespace="net.northking.cctp.dw.db.dao.BugLifeCycleChartDao">


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

    <resultMap id="DetailMap" type="net.northking.cctp.dw.dto.BugLifeCycleChartDetailDto">
        <!-- 主键 -->
        <id column="id" jdbcType="VARCHAR" property="id"/>
        <!-- 租户id -->
        <result column="tenant_id" jdbcType="VARCHAR" property="tenantId"/>
        <!-- 生命周期 -->
        <result column="life_time" jdbcType="VARCHAR" property="lifeTime"/>
        <!-- 数量 -->
        <result column="amount" jdbcType="INTEGER" property="amount"/>
        <!--  -->
        <result column="project_id" jdbcType="VARCHAR" property="projectId"/>
    </resultMap>

    <select id="pagingQuery" resultMap="DetailMap" parameterType="net.northking.cctp.dw.db.entity.BugLifeCycleChart">
        select
        id
        ,tenant_id
        ,life_time
        ,amount
        ,project_id
        from
        <include refid="Table_Name"/>
        <trim prefix="WHERE" prefixOverrides="AND">
            <if test="id != null">
                AND id = #{id,jdbcType=VARCHAR}
            </if>
            <if test="tenantId != null">
                AND tenant_id = #{tenantId,jdbcType=VARCHAR}
            </if>
            <if test="lifeTime != null">
                AND life_time = #{lifeTime,jdbcType=VARCHAR}
            </if>
            <if test="amount != null">
                AND amount = #{amount,jdbcType=INTEGER}
            </if>
            <if test="projectId != null">
                AND project_id = #{projectId,jdbcType=VARCHAR}
            </if>
        </trim>
    </select>

    <select id="listQuery" resultType="net.northking.cctp.dw.dto.BugLifeCycleChartDetailDto">
        SELECT
        id,
        tenant_id tenantId,
        project_id projectId,
        amount,
        life_time lifeTime
        FROM
        <include refid="Table_Name"/>
        where project_id=#{proId} and tenant_id=#{tenantId}
    </select>

    <insert id="insertBatch">
        insert ignore into
        bug_life_cycle_chart(id,tenant_id,project_id,life_time,amount)
        SELECT
        UUID() id,
        bm.tenant_id,
        bm.project_id,
        bm.life_time,
        COUNT(*) amount
        FROM
        (
        SELECT
        s.tenant_id,
        pro_id project_id,
        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 life_time
        FROM syn_bug_info s
        where
        s.bug_status IN(select bug_status_code from bug_template_status b where
        b.project_id in
        <foreach collection="projectIds" item="projectId" open="(" separator="," close=")">
            #{projectId, jdbcType=VARCHAR}
        </foreach>
        and b.is_end_status=1)
        and
        s.pro_id in
        <foreach collection="projectIds" item="projectId" open="(" separator="," close=")">
            #{projectId, jdbcType=VARCHAR}
        </foreach>
        and
        s.tenant_id=#{tenantId}
        and
        close_time is not null
        ) bm
        WHERE bm.life_time IS NOT NULL
        GROUP by bm.project_id,bm.life_time
    </insert>

    <select id="getProjectList" resultType="net.northking.cctp.dw.db.entity.BugLifeCycleChart">
        select
        tenant_id tenantId,
        pro_id projectId
        from
        syn_bug_info
        group by
        tenant_id,
        pro_id
    </select>

    <delete id="deleteByExampleByProjectIds" parameterType="net.northking.cctp.dw.db.entity.BugLifeCycleChart">
        DELETE FROM cctp_dw.bug_life_cycle_chart WHERE project_id IN
        <foreach collection="projectIds" item="projectId" open="(" separator="," close=")">
            #{projectId, jdbcType=VARCHAR}
        </foreach>
    </delete>

</mapper>