<?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 09:48:17  -->
<mapper namespace="net.northking.cctp.dw.db.dao.MidCaseDesignDao">

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

    <select id="caseDesignPie" resultType="net.northking.cctp.dw.dto.MidCaseDesignDetailDto">
        SELECT
        total
        ,completed
        ,designing
        ,d.plan_amount planAmount
        FROM mid_case_design i
        LEFT JOIN cp_project_detail d ON i.project_id = d.project_id
        WHERE d.detail_type=1
        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="countCaseDesignInfoData" parameterType="net.northking.cctp.dw.dto.DwCaseDesignInfoDto">
        INSERT INTO cctp_dw.mid_case_design ( tenant_id, project_id, created_time, total, completed, designing ) SELECT
        temp.tenantId,
        temp.projectId,
        temp.createdTime,
        temp.total,
        temp.completed,
        temp.designing
        FROM
        (
        SELECT
        tenant_id AS tenantId,
        project_id AS projectId,
        date_format(
        created_time,
        GET_FORMAT( DATE, 'jis' )) createdTime,
        cast((SUM( CASE case_status WHEN 1 THEN 1 ELSE 0 END )+0) as signed) completed,
        cast((SUM( CASE case_status WHEN 0 THEN 1 ELSE 0 END )+0) as signed) designing,
        COUNT( 1 ) total
        FROM
        cctp_dw.dw_case_design_info
        WHERE tenant_id = #{tenantId}
        AND project_id in
        <foreach collection="midProjectInfos" item="midProjectInfo" close=")" open="(" separator=",">
            #{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,
        completed
        from mid_case_design where project_id in
        <foreach collection="midProjectInfos" item="midProjectInfo" open="(" close=")" separator=",">
            #{midProjectInfo.projectId}
        </foreach>
    </select>

    <delete id="deleteByData" >
        delete
        from mid_case_design
        where project_id in
        <foreach collection="midProjectInfos" separator="," open="(" close=")" item="midProjectInfo">
            #{midProjectInfo.projectId}
        </foreach>
    </delete>
    <select id="getTestPointAmountAndCaseDesignAmountByProjectId" resultType="net.northking.cctp.dw.dto.ProjectDataVo">
        select
        tmp1.belong_module belongModule,
        tmp1.testPoint,
        tmp2.referCaseTestPoint
        from
        (
        select
        belong_module,
        count(1) testPoint
        from
        dw_test_point
        where project_id=#{projectId}
        group by belong_module
        )tmp1
        inner join
        (
        select
        belong_module,
        count(1) referCaseTestPoint
        from
        dw_test_point
        where
        project_id=#{projectId}
        and
        id in
        <foreach collection="testPointIds" item="testPointId" open="(" separator="," close=")">
            #{testPointId}
        </foreach>
        group by belong_module
        )tmp2
        on tmp1.belong_module=tmp2.belong_module
        order by tmp2.referCaseTestPoint desc limit 5
    </select>

    <select id="getTestPointDesignRateByProjectId" resultType="net.northking.cctp.dw.dto.ProjectDataVo">
        select
        tmp1.testPoints/(tmp2.days*(#{amount})) testPointDesignRate
        from
        (
                select
                project_id,
                sum(total) testPoints
                from
                mid_test_point
                where
                project_id=#{projectId}
        )tmp1 inner join
        (
                select
                project_id,
                datediff(end_time,start_time)+1 days
                from
                cp_project_detail
                where
                project_id=#{projectId}
                and
                detail_type =2
        )tmp2
        on tmp1.project_id=tmp2.project_id
    </select>

    <select id="getCaseDesignRateByProjectId" resultType="net.northking.cctp.dw.dto.ProjectDataVo">
        select
        tmp1.caseDesigns/(tmp2.days*(#{amount})) caseDesignRate
        from
        (
            select
            project_id,
            sum(total) caseDesigns
            from
            mid_case_design
            where
            project_id=#{projectId}
        )tmp1 inner join
        (
            select
            project_id,
            datediff(end_time,start_time)+1 days
            from
            cp_project_detail
            where
            project_id=#{projectId}
            and
            detail_type =1
        )tmp2
        on tmp1.project_id=tmp2.project_id
    </select>
</mapper>