<?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-15 10:09:06  -->
<mapper namespace="net.northking.cctp.dw.db.dao.MidManualExecJobDao">

    <select id="execJobPie" resultType="net.northking.cctp.dw.dto.MidManualExecJobDetailDto">
        SELECT
        total
        ,non_exec nonExec
        ,invalid
        ,fail
        ,blocked
        ,hang_up hangUp
        ,unfinished
        ,pass
        ,d.plan_amount planAmount
        FROM
        mid_manual_exec_job j
        LEFT JOIN cp_project_detail d ON j.project_id = d.project_id
        WHERE d.detail_type = 3
        AND j.project_id = #{projectId}
        AND date(created_time) BETWEEN IFNULL(date(d.start_time),created_time) AND IFNULL(date(d.end_time),created_time)
    </select>

    <select id="createExecJobProgress" resultType="net.northking.cctp.dw.db.entity.MtTestChart">
        SELECT
        d.tenant_id tenantId,
        d.project_id projectId,
        3 as testType,
        DATE_FORMAT(CURDATE(),GET_FORMAT(DATE, 'JIS')) recordDate,
        SUM(j.fail + j.pass) AS totalActual,
        sum(j.total)-sum(j.invalid) AS totalPlan,
        if(SUM(j.fail + j.pass)/(SUM(j.total)-SUM(j.invalid))>1,100,SUM(j.fail +
        j.pass)*100/(SUM(j.total)-SUM(j.invalid))) AS percentActual,
        IFNULL(100/WORKDAYSONEYEARS(d.start_time,d.end_time)*
        WORKDAYSONEYEARS(if(CURDATE() >= d.start_time, d.start_time, d.end_time),
        if(CURDATE() >= d.end_time, d.end_time, CURDATE())),0) AS percentPlan
        FROM cp_project_detail d
        LEFT JOIN mid_manual_exec_job j ON j.project_id = d.project_id
        WHERE d.detail_type=3
        AND DATE(NOW()) >= DATE_SUB(date(d.start_time), INTERVAL 1 DAY)
        and d.tenant_id=#{tenantId}
        GROUP BY j.project_id
    </select>

    <update id="countExecJobData">
        INSERT INTO cctp_dw.mid_manual_exec_job ( tenant_id, project_id, created_time, total, non_exec, invalid, fail, blocked, hang_up, unfinished, pass ) SELECT
        temp.tenantId,
        temp.projectId,
        temp.createdTime,
        temp.total,
        temp.nonExec,
        temp.invalid,
        temp.fail,
        temp.blocked,
        temp.hangUp,
        temp.unfinished,
        temp.pass
        FROM
        (
        SELECT
        tenant_id AS tenantId,
        project_id AS projectId,
        date_format(
        updated_time,
        GET_FORMAT( DATE, 'jis' )) createdTime,
        cast((SUM( CASE exec_status WHEN 0 THEN 1 ELSE 0 END )+0) as signed) nonExec,
        cast((SUM( CASE exec_status WHEN 1 THEN 1 ELSE 0 END )+0) as signed) invalid,
        cast((SUM( CASE exec_status WHEN 2 THEN 1 ELSE 0 END )+0) as signed) fail,
        cast((SUM( CASE exec_status WHEN 3 THEN 1 ELSE 0 END )+0) as signed) blocked,
        cast((SUM( CASE exec_status WHEN 4 THEN 1 ELSE 0 END )+0) as signed) hangUp,
        cast((SUM( CASE exec_status WHEN 5 THEN 1 ELSE 0 END )+0) as signed) unfinished,
        cast((SUM( CASE exec_status WHEN 6 THEN 1 ELSE 0 END )+0) as signed) pass,
        COUNT( 1 ) total
        FROM
        cctp_dw.dw_manual_exec_job
        WHERE
        project_id in
        <foreach collection="midProjectInfos" separator="," close=")" open="(" item="midProjectInfo">
            #{midProjectInfo.projectId}
        </foreach>
        and
        tenant_id=#{tenantId}
        GROUP BY
        date_format(
        updated_time,
        GET_FORMAT( DATE, 'jis' )),project_id) temp
    </update>

    <select id="findByTenantId" resultMap="BaseResultMap">
        select
        project_id
        ,total
        ,invalid
        ,fail
        ,pass
        from mid_manual_exec_job where project_id in
        <foreach collection="midProjectInfos" separator="," close=")" open="(" item="midProjectInfo">
            #{midProjectInfo.projectId}
        </foreach>
    </select>

    <select id="findProIdList" resultType="net.northking.cctp.dw.db.entity.DwManualExecJob">
        select
        tenant_id tenantId,
        project_id projectId
        from
        dw_manual_exec_job
        group by
        tenant_id,
        project_id
    </select>

    <delete id="deleteByData">
        delete
        from mid_manual_exec_job
        where project_id in
        <foreach collection="midProjectInfos" item="midProjectInfo" open="(" close=")" separator=",">
            #{midProjectInfo.projectId}
        </foreach>
    </delete>

    <select id="getCaseBugFindRateByTenantId" resultType="net.northking.cctp.dw.dto.ProjectDataVo">
        select
        tmp.bug_num/tmp1.amount caseBugFindRate
        from
        (
        select
        tenant_id,
        sum(case when case_num &gt;0 then 1 else 0 end) bug_num
        from
        syn_bug_info
        where
        tenant_id=#{tenantId}
        and
        pro_id in
        <foreach collection="projectIds" close=")" separator="," open="(" item="projectId">
            #{projectId}
        </foreach>
        )tmp
        inner join
        (
        select
        tenant_id,
        sum(total)-sum(invalid) amount
        from
        mid_manual_exec_job
        where
        tenant_id=#{tenantId}
        and
        project_id in
        <foreach collection="projectIds" close=")" separator="," open="(" item="projectId">
            #{projectId}
        </foreach>
        )tmp1
        on tmp.tenant_id=tmp1.tenant_id
    </select>

    <select id="getCaseBugFindRateByProjectId" resultType="net.northking.cctp.dw.dto.ProjectDataVo">
        select
        tmp.bug_num/tmp1.amount caseBugFindRate
        from
        (
        select
        pro_id,
        sum(case when case_num &gt;0 then 1 else 0 end) bug_num
        from
        syn_bug_info
        where
        pro_id=#{projectId}
        )tmp
        inner join
        (
        select
        project_id,
        sum(total)-sum(invalid) amount
        from
        mid_manual_exec_job
        where
        project_id=#{projectId}
        )tmp1
        on tmp.pro_id=tmp1.project_id
    </select>
</mapper>