<?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-14 18:32:57  -->
<mapper namespace="net.northking.cctp.dw.db.dao.MidTestPointDao">

    <select id="createTestPointProgress" resultType="net.northking.cctp.dw.db.entity.MtTestChart">
        SELECT
        date_format(CURDATE(),GET_FORMAT(DATE,'jis')) as recordDate,
        d.tenant_id tenantId,
        d.project_id projectId,
        2 testType,
        sum(p.total) AS totalActual,
        d.plan_amount as totalPlan,
        if(sum(p.total)/d.plan_amount>1,100,sum(p.total)*100/d.plan_amount) 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_test_point p ON p.project_id = d.project_id
        WHERE d.detail_type = 2
        AND DATE(NOW()) >= DATE_SUB(date(d.start_time), INTERVAL 1 DAY)
        and d.tenant_id=#{tenantId}
        GROUP BY p.project_id
    </select>

    <select id="testPointPie" resultType="net.northking.cctp.dw.dto.MidTestPointDetailDto">
        SELECT
        p.*,
        d.plan_amount planAmount
        FROM mid_test_point p
        LEFT JOIN cp_project_detail d ON p.project_id = d.project_id
        WHERE d.detail_type = 2
        AND d.project_id = #{projectId}
        AND date(created_time) BETWEEN IFNULL(date(d.start_time),created_time) AND IFNULL(date(d.end_time),created_time)
    </select>

    <update id="countTestPointData" >
        INSERT INTO cctp_dw.mid_test_point ( tenant_id, project_id, completed, uncompleted, total, created_time ) SELECT
        temp.tenantId,
        temp.projectId,
        temp.completed,
        temp.uncompleted,
        temp.total,
        temp.createdTime
        FROM
        (
        SELECT
        tenant_id AS tenantId,
        project_id AS projectId,
        date_format(
        created_time,
        GET_FORMAT( DATE, 'jis' )) createdTime,
        cast((SUM( CASE point_status WHEN 1 THEN 1 ELSE 0 END )+0) as signed) completed,
        cast((SUM( CASE point_status WHEN 0 THEN 1 ELSE 0 END )+0) as signed) uncompleted,
        COUNT( 1 ) total
        FROM
        cctp_dw.dw_test_point
        WHERE
        tenant_id=#{tenantId}
        and
        project_id in
        <foreach collection="midProjectInfos" separator="," close=")" open="(" item="midProjectInfo">
            #{midProjectInfo.projectId}
        </foreach>
        GROUP BY
        date_format(
        created_time,
        GET_FORMAT( DATE, 'jis' )),project_id) temp
    </update>

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

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