<?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 10:03:26  -->
<mapper namespace="net.northking.cctp.dw.db.dao.MtCaseExecPassRatioDao">

    <update id="caseExecPassRatio">
        INSERT INTO cctp_dw.mt_case_exec_pass_ratio ( dir_name, case_count, pass_count, case_pass_percent, project_id,
        tenant_id ) SELECT
        temp.dirName,
        temp.caseCount,
        temp.passCount,
        temp.casePassPercent,
        temp.projectId,
        temp.tenantId
        FROM
        (
        SELECT REPLACE
        ( d.name_path, 'TOP/', '' ) dirName,
        cast((SUM( CASE WHEN j.exec_status IN ( '2', '6' ) THEN 1 ELSE 0 END )+0) as signed) caseCount,
        cast((SUM( CASE j.exec_status WHEN '6' THEN 1 ELSE 0 END )+0) as signed) passCount,
        ROUND(
        cast((SUM( CASE j.exec_status WHEN '6' THEN 1 ELSE 0 END )+0) as signed)/ NULLIF( cast((SUM( CASE WHEN j.exec_status IN
        ( '2', '6' ) THEN 1 ELSE
        0 END )+0) as signed),0),
        4
        ) casePassPercent,
        j.project_id projectId,
        j.tenant_id tenantId
        FROM
        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
        WHERE
        temp.dirName IS NOT NULL
    </update>

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


</mapper>