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

    <!--弃用-->
    <select id="createOverallTestReportProgress" resultType="net.northking.cctp.dw.db.entity.MtTestChart">
        SELECT DISTINCT
        4 as testType,
        cast(date(CURDATE()) AS char) as recordDate,
        c.tenant_id tenantId,
        c.project_id projectId,
        1/WORKDAYSONEYEARS(p.start_time,p.production_time)*
        WORKDAYSONEYEARS(if(CURDATE() >= p.start_time, p.start_time, CURDATE()),
        if(CURDATE() >= p.production_time, p.production_time, CURDATE())) as percentPlan,
        ROUND((case
        when CURDATE() &lt; d.start_time AND d.start_time &lt; d1.end_time
        then c.percent_actual*0.22
        when d.start_time &lt;= CURDATE() AND CURDATE() &lt; d1.end_time
        then c.percent_actual*0.22+c1.percent_actual*0.25
        when d1.end_time &lt;= CURDATE() AND CURDATE() &lt; d2.start_time
        then c1.percent_actual*0.25
        when d2.start_time &lt;= CURDATE() AND CURDATE() &lt; d.end_time
        then c.percent_actual*0.22+c1.percent_actual*0.25+c2.percent_actual*0.53
        when d.end_time &lt;= CURDATE() AND CURDATE() &lt; d2.end_time
        then c2.percent_actual*0.53
        END),4) AS percentActual
        FROM mt_test_chart c
        LEFT JOIN mt_test_chart c1 ON c.project_id = c1.project_id
        LEFT JOIN mt_test_chart c2 ON c.project_id = c2.project_id
        LEFT JOIN cp_project_detail d ON c.project_id = d.pro_id and c.test_type = d.detail_type
        LEFT JOIN cp_project_detail d1 ON c.project_id = d1.pro_id and c1.test_type = d1.detail_type
        LEFT JOIN cp_project_detail d2 ON c.project_id = d2.pro_id and c2.test_type = d2.detail_type
        LEFT JOIN mid_project_info p ON c.project_id = p.project_id
        WHERE c.test_type = 1 AND c1.test_type = 2 AND c2.test_type = 3
        AND date(CURDATE()) BETWEEN date(p.start_time) AND date(p.production_time)
        GROUP BY c.project_id
    </select>

    <insert id="addByBatch" parameterType="net.northking.cctp.dw.db.entity.MtTestChart">
        insert into
        <include refid="Table_Name"/>
        <trim prefix="(" suffix=")" suffixOverrides=",">
            tenant_id,
            record_date,
            total_plan,
            total_actual,
            percent_plan,
            percent_actual,
            test_type,
            project_id,
        </trim>
        values
        <foreach collection="list" item="item" index="index" separator=",">
            <trim prefix="(" suffix=")" suffixOverrides=",">
                #{item.tenantId, jdbcType=VARCHAR},
                #{item.recordDate, jdbcType=VARCHAR},
                #{item.totalPlan, jdbcType=INTEGER},
                #{item.totalActual, jdbcType=INTEGER},
                #{item.percentPlan, jdbcType=REAL},
                #{item.percentActual, jdbcType=REAL},
                #{item.testType, jdbcType=INTEGER},
                #{item.projectId, jdbcType=VARCHAR},
            </trim>
        </foreach>
    </insert>

    <select id="queryByProIdAndDate" resultMap="BaseResultMap">
        select
        *
        from
        <include refid="Table_Name"/>
        where project_id = #{projectId,jdbcType=VARCHAR}
        AND test_type = #{testType, jdbcType=INTEGER}
        AND record_date BETWEEN date(#{startTime}) AND date(#{endTime})
    </select>

    <select id="queryToday" resultMap="BaseResultMap">
        SELECT
        percent_actual
        ,test_type
        ,project_id
        FROM
        <include refid="Table_Name"/>
        WHERE project_id in
        <foreach collection="midProjectInfos" item="midProjectInfo" open="(" close=")" separator=",">
            #{midProjectInfo.projectId}
        </foreach>
        AND record_date = date(now())
        AND test_type != 4
    </select>

    <delete id="queryTodayDeleted">
        DELETE FROM <include refid="Table_Name"/>
        WHERE project_id in
        <foreach collection="midProjectInfos" item="midProjectInfo" open="(" close=")" separator=",">
            #{midProjectInfo.projectId}
        </foreach>
        AND record_date = date(now())
        AND test_type = 4
    </delete>

    <delete id="deleteProgress">
        delete from
        <include refid="Table_Name"/>
        where test_type != 4 and record_date = date(now()) and tenant_id=#{tenantId}
    </delete>

    <select id="queryChartProgress" parameterType="net.northking.cctp.dw.dto.ChartProgressQueryDto"
            resultType="net.northking.cctp.dw.dto.MtTestChartDto">
<!--        <if test="type!=4">-->
        SELECT
        id
        ,tenant_id tenantId
        ,record_date recordDate
        ,total_plan totalPlan
        ,total_actual totalActual
        ,percent_plan percentPlan
        ,percent_actual percentActual
        ,test_type testType
        ,project_id projectId
        FROM mt_test_chart
        WHERE project_id = #{projectId} AND test_type = ${type} ORDER BY record_date
<!--        </if>-->
<!--        <if test="type==4">-->
<!--            SELECT-->
<!--            id-->
<!--            ,tenant_id tenantId-->
<!--            ,record_date recordDate-->
<!--            ,total_plan totalPlan-->
<!--            ,total_actual totalActual-->
<!--            ,percent_plan percentPlan-->
<!--            ,percent_actual percentActual-->
<!--            ,test_type testType-->
<!--            ,project_id projectId-->
<!--            FROM mt_test_chart-->
<!--            WHERE project_id = #{projectId} and test_type != ${type}  ORDER BY record_date-->
<!--        </if>-->
    </select>

    <select id="findMaxDateGroupByProId" resultType="net.northking.cctp.dw.db.entity.MtTestChart">
        SELECT
        project_id projectId,
        max(record_date) recordDate
        from mt_test_chart
        where test_type = #{testType}
        group by project_id
    </select>
    <select id="getProjectIds" resultType="net.northking.cctp.dw.dto.ProjectDataVo">
        select
        project_id projectId
        from
        mt_test_chart
        where
        date_format(record_date,'%Y-%m-%d')=date_format(NOW(),'%Y-%m-%d')
        and
        percent_actual &lt;100.0
        and
        test_type=4
        and
        tenant_id=#{tenantId}
    </select>

</mapper>