<?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 11:41:46  -->
<mapper namespace="net.northking.cctp.dw.db.dao.MtCaseExecOverallDao">

    <update id="caseExecOverall">
        INSERT INTO cctp_dw.mt_case_exec_overall ( dir_name, plan_exec, executed, pass, not_pass, unfinished, blocked_exec, case_exec_rate, case_pass_rate, project_id, tenant_id,invalid) SELECT
        temp.dirName,
        temp.planExec,
        temp.executed,
        temp.pass,
        temp.notPass,
        temp.unfinished,
        temp.blockedExec,
        temp.caseExecRate,
        temp.casePassRate,
        temp.projectId,
        temp.tenantId,
        temp.invalid
        FROM
        (SELECT
        REPLACE(d.name_path, 'TOP/', '') dirName,
        COUNT(*) planExec,
        CAST((SUM(CASE WHEN exec_status IN (2, 6) THEN 1 ELSE 0 END)+0) AS signed) executed,
        CAST((SUM(CASE exec_status WHEN 6 THEN 1 ELSE 0 END)+0) AS signed) pass,
        CAST((SUM(CASE WHEN exec_status != 6 THEN 1 ELSE 0 END)+0) AS signed) notPass,
        CAST((SUM(CASE exec_status WHEN 5 THEN 1 ELSE 0 END)+0) AS signed) unfinished,
        CAST((SUM(CASE WHEN exec_status IN (3, 4) THEN 1 ELSE 0 END)+0) AS signed) blockedExec,
        CONCAT(ROUND(IFNULL(SUM(CASE WHEN exec_status IN (2, 6) THEN 1 ELSE 0 END)/ NULLIF(SUM(CASE WHEN exec_status != 1 THEN 1 ELSE 0 END),0),0)* 100,2), '%' ) caseExecRate,
        CONCAT(ROUND(IFNULL(SUM(CASE exec_status WHEN 6 THEN 1 ELSE 0 END)/ NULLIF(SUM(CASE WHEN exec_status IN (2, 6) THEN 1 ELSE 0 END),0),0)* 100,2),'%') casePassRate,
        j.project_id projectId,
        j.tenant_id tenantId, CAST((SUM(CASE WHEN exec_status = 1 THEN 1 ELSE 0 END)+0) AS signed) invalid
        FROM
        cctp_dw.dw_manual_exec_job j
        LEFT JOIN (SELECT id, name_path FROM dw_manual_exec_dir WHERE project_id in
        <foreach collection="projectIds" item="projectId" open="(" separator="," close=")">
            #{projectId, jdbcType=VARCHAR}
        </foreach>
        AND tenant_id = #{tenantId}) d ON j.dir_id = d.id
        where
        j.project_id in
        <foreach collection="projectIds" item="projectId" open="(" separator="," close=")">
            #{projectId, jdbcType=VARCHAR}
        </foreach>
        and  j.tenant_id = #{tenantId}
        GROUP BY
        j.dir_id,
        j.project_id
        ) temp
    </update>

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