<?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-24 11:21:08  -->
<mapper namespace="net.northking.cctp.dw.db.dao.BugOverallTendencyChartDao">


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

    <resultMap id="DetailMap" type="net.northking.cctp.dw.dto.BugOverallTendencyChartDetailDto">
        <!--  -->
        <id column="id" jdbcType="VARCHAR" property="id"/>
        <!-- 租户id -->
        <result column="tenant_id" jdbcType="VARCHAR" property="tenantId"/>
        <!-- 创建时间 -->
        <result column="created_time" jdbcType="VARCHAR" property="createdTime"/>
        <!-- 未解决 -->
        <result column="unsolved" jdbcType="INTEGER" property="unsolved"/>
        <!-- 已解决 -->
        <result column="solved" jdbcType="INTEGER" property="solved"/>
        <!-- 所有 -->
        <result column="all_amount" jdbcType="INTEGER" property="allAmount"/>
        <!-- 项目id -->
        <result column="project_id" jdbcType="VARCHAR" property="projectId"/>
        <!-- 记录类型：0跨项目的，1项目级的 -->
        <result column="record_type" jdbcType="INTEGER" property="recordType"/>
    </resultMap>

    <select id="pagingQuery" resultMap="DetailMap"
            parameterType="net.northking.cctp.dw.db.entity.BugOverallTendencyChart">
        select
        id
        ,tenant_id
        ,created_time
        ,unsolved
        ,solved
        ,all_amount
        ,project_id
        ,record_type
        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="createdTime != null">
                AND created_time = #{createdTime,jdbcType=VARCHAR}
            </if>
            <if test="unsolved != null">
                AND unsolved = #{unsolved,jdbcType=INTEGER}
            </if>
            <if test="solved != null">
                AND solved = #{solved,jdbcType=INTEGER}
            </if>
            <if test="allAmount != null">
                AND all_amount = #{allAmount,jdbcType=INTEGER}
            </if>
            <if test="projectId != null">
                AND project_id = #{projectId,jdbcType=VARCHAR}
            </if>
            <if test="recordType != null">
                AND record_type = #{recordType,jdbcType=INTEGER}
            </if>
        </trim>
    </select>

    <select id="listQuery" resultType="net.northking.cctp.dw.dto.BugOverallTendencyChartDetailDto">
        select
        created_time createdTime
        ,sum(unsolved) unsolved
        ,sum(solved) solved
        ,sum(all_amount) allAmount
        from
        <include refid="Table_Name"/>
        <trim prefix="WHERE" prefixOverrides="AND">
            <if test="tenantId != null">
                AND tenant_id = #{tenantId,jdbcType=VARCHAR}
            </if>
            <if test="startTime != null and endTime != null">
                AND created_time between #{startTime} and #{endTime}
            </if>
            <if test="proIdList != null and proIdList.size()>0">
                AND project_id in
                <foreach collection="proIdList" item="proId" open="(" close=")" separator=",">
                    #{proId}
                </foreach>
            </if>
        </trim>
        group by created_time
    </select>

    <insert id="insertBatch">
        insert into
        bug_overall_tendency_chart(id,project_id,created_time,solved,tenant_id,all_amount,unsolved)
        SELECT
        UUID() id,
        tmp.*,
        tmp1.all_amount,
        tmp1.all_amount-tmp.solved AS unsolved
        FROM
        (
        SELECT
        pro_id AS project_id,
        DATE_FORMAT(now(), '%Y-%m-%d') created_time,
        sum(case when s.bug_status=b.bug_status_code then 1 else 0 end) solved,
        s.tenant_id as tenant_id
        FROM
        syn_bug_info s join bug_template_status b
        on s.pro_id=b.project_id
        where
        DATE_FORMAT(updated_time, '%Y-%m-%d')&lt;=DATE_FORMAT(now(), '%Y-%m-%d')
        and is_end_status=1
        group by b.project_id
        )tmp
        join
        (
        select
        pro_id AS project_id,
        COUNT(*) all_amount
        FROM
        syn_bug_info
        where
        DATE_FORMAT(updated_time, '%Y-%m-%d')&lt;=DATE_FORMAT(now(), '%Y-%m-%d')
        group by pro_id
        )tmp1
        on
        tmp.project_id=tmp1.project_id
    </insert>

    <select id="getProjectList" resultType="net.northking.cctp.dw.dto.BugOverallTendencyChartDto">
        SELECT tenant_id AS tenantId, pro_id AS projectId FROM syn_bug_info GROUP BY tenant_id, pro_id
    </select>


    <insert id="insertBatchByProjectId" parameterType="net.northking.cctp.dw.dto.BugOverallTendencyChartDto">
        INSERT INTO bug_overall_tendency_chart ( id, project_id, created_time, solved, tenant_id, all_amount, unsolved )
        SELECT
        UUID() id,
        tmp.*,
        tmp1.all_amount,
        tmp1.all_amount - tmp.solved AS unsolved
        FROM
        (
        SELECT
        pro_id project_id,
        DATE_FORMAT( now(), '%Y-%m-%d' ) created_time,
        sum( CASE WHEN (syn.bug_status=tmp4.bug_status_code and syn.pro_id=tmp4.project_id) THEN 1 ELSE 0 END ) solved,
        syn.tenant_id
        FROM
        syn_bug_info syn
        left join
        (
        SELECT
        project_id,
        bug_status_code
        FROM
        bug_template_status
        WHERE
        is_end_status = 1
        AND
        project_id IN
        <foreach collection="projectIds" close=")" separator="," open="(" item="projectId">
            #{projectId}
        </foreach>
        )tmp4
        on tmp4.project_id=syn.pro_id
        WHERE
        DATE_FORMAT( updated_time, '%Y-%m-%d' )&lt;= DATE_FORMAT( now(), '%Y-%m-%d' )
        AND syn.tenant_id = #{tenantId}
        AND syn.pro_id IN
        <foreach collection="projectIds" close=")" separator="," open="(" item="projectId">
        #{projectId}
        </foreach>
        GROUP BY syn.pro_id
        ) tmp
        JOIN
        (
        SELECT
        pro_id AS project_id,
        COUNT(*) all_amount
        FROM
        syn_bug_info
        WHERE
        DATE_FORMAT( updated_time, '%Y-%m-%d' )&lt;= DATE_FORMAT( now(), '%Y-%m-%d' )
        AND tenant_id = #{tenantId}
        AND pro_id IN
        <foreach collection="projectIds" close=")" separator="," open="(" item="projectId">
            #{projectId}
        </foreach>
        GROUP BY pro_id
        ) tmp1 ON tmp.project_id = tmp1.project_id
    </insert>

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

    <select id="queryUnsolvedBugDataByProjectId" resultType="net.northking.cctp.dw.dto.BugOverallTendencyChartDetailDto">
        select
        created_time createdTime,
        unsolved
        from
        <include refid="Table_Name"/>
        where project_id=#{projectId}
        order by created_time asc
    </select>

    <select id="queryAllUnsolvedBugDataSortedByTenantId" resultType="net.northking.cctp.dw.db.entity.BugStatusStatistics">
        select
        project_id projectId
        ,unsolved amount
        from
        <include refid="Table_Name"/>
        <trim prefix="WHERE" prefixOverrides="AND">
            <if test="tenantId != null">
                AND tenant_id = #{tenantId,jdbcType=VARCHAR}
            </if>
            and created_time=date_format(now(),'%Y-%m-%d')
            and project_id in
            <foreach collection="projectIds" item="projectId" open="(" separator="," close=")">
                #{projectId}
            </foreach>
        </trim>
        order by unsolved desc
        limit 5
    </select>
</mapper>