<?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-17 12:02:41  -->
<mapper namespace="net.northking.cctp.dw.db.dao.MtCaseExecTrendChartDao">


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

    <resultMap id="DetailMap" type="net.northking.cctp.dw.dto.MtCaseExecTrendChartDetailDto">
        <!-- 主键id -->
        <id column="id" jdbcType="VARCHAR" property="id"/>
        <!-- 租户id -->
        <result column="tenant_id" jdbcType="VARCHAR" property="tenantId"/>
        <!-- 执行用例总数量 -->
        <result column="case_exec_all_amount" jdbcType="INTEGER" property="caseExecAllAmount"/>
        <!-- 已执行用例数量 -->
        <result column="case_exec_amount" jdbcType="INTEGER" property="caseExecAmount"/>
        <!-- 未执行用例数量 -->
        <result column="case_no_exec_amount" jdbcType="INTEGER" property="caseNoExecAmount"/>
        <!-- 创建时间 -->
        <result column="created_time" jdbcType="VARCHAR" property="createdTime"/>
        <!--  -->
        <result column="project_id" jdbcType="VARCHAR" property="projectId"/>
    </resultMap>

    <select id="pagingQuery" resultMap="DetailMap" parameterType="net.northking.cctp.dw.db.entity.MtCaseExecTrendChart">
        select
        id
        ,tenant_id
        ,case_exec_all_amount
        ,case_exec_amount
        ,case_no_exec_amount
        ,created_time
        ,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="caseExecAllAmount != null">
                AND case_exec_all_amount = #{caseExecAllAmount,jdbcType=INTEGER}
            </if>
            <if test="caseExecAmount != null">
                AND case_exec_amount = #{caseExecAmount,jdbcType=INTEGER}
            </if>
            <if test="caseNoExecAmount != null">
                AND case_no_exec_amount = #{caseNoExecAmount,jdbcType=INTEGER}
            </if>
            <if test="createdTime != null">
                AND created_time = #{createdTime,jdbcType=VARCHAR}
            </if>
            <if test="projectId != null">
                AND project_id = #{projectId,jdbcType=VARCHAR}
            </if>
        </trim>
    </select>

    <select id="listQuery" resultType="net.northking.cctp.dw.dto.MtCaseExecTrendChartDetailDto">
        select
        case_exec_all_amount caseExecAllAmount
        ,case_exec_amount caseExecAmount
        ,case_no_exec_amount caseNoExecAmount
        ,created_time createdTime
        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 startTime != '' and endTime != ''">
                AND created_time between #{startTime} and #{endTime}
            </if>
            <if test="proId != null">
                AND project_id = #{proId,jdbcType=VARCHAR}
            </if>
        </trim>
        order by created_time asc
    </select>

    <update id="syncExecTendencyNotice">
        INSERT INTO cctp_dw.mt_case_exec_trend_chart ( id, tenant_id, case_exec_all_amount, case_exec_amount,
        case_no_exec_amount, created_time, project_id ) SELECT
        temp.id,
        temp.tenantId,
        temp.caseExecAllAmount,
        temp.caseExecAmount,
        temp.caseNoExecAmount,
        temp.createdTime,
        temp.projectId
        FROM
        (
        SELECT
        tmp.*,
        tmp.caseExecAllAmount - tmp.caseExecAmount AS caseNoExecAmount
        FROM
        (
        SELECT
        UUID() id,
        date_format( now(), '%Y-%m-%d' ) AS createdTime,
        tenant_id tenantId,
        project_id projectId,
        cast((SUM( CASE WHEN exec_status = 2 OR exec_status = 6 THEN 1 ELSE 0 END )+0) as signed) caseExecAmount,
        cast((SUM( CASE WHEN exec_status != 1 THEN 1 ELSE 0 END ) +0) as signed) caseExecAllAmount
        FROM
        cctp_dw.dw_manual_exec_job
        WHERE
        date_format( IFNULL( updated_time, created_time ), '%Y-%m-%d' )&lt;= date_format( now(), '%Y-%m-%d' )
        and project_id in
        <foreach collection="projectIds" item="projectId" open="(" separator="," close=")">
            #{projectId, jdbcType=VARCHAR}
        </foreach>
        and tenant_id = #{tenantId}
        GROUP BY project_id
        ) tmp
        ) temp
    </update>

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