<?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:37:20  -->
<mapper namespace="net.northking.cctp.dw.db.dao.CaseDesignCompleteDetailsDao">

    <update id="caseDesignComplete" parameterType="net.northking.cctp.dw.dto.DwCaseDesignInfoDto">
        INSERT INTO cctp_dw.case_design_complete_details ( id, receiver_name, login_no, designering, complete,
        date_type, tenant_id, project_id ) SELECT
        temp_table.id,
        temp_table.receiverName,
        temp_table.receiverName,
        temp_table.designering,
        temp_table.complete,
        temp_table.dateType,
        temp_table.tenantId,
        temp_table.projectId
        FROM
        (
        SELECT
        UUID_SHORT() AS id,
        a.tenant_id tenantId,
        a.created_by AS receiverName,
        CAST(SUM( CASE a.case_STATUS WHEN 0 THEN 1 ELSE 0 END ) AS signed) AS designering,
        CAST(SUM( CASE a.case_STATUS WHEN 1 THEN 1 ELSE 0 END ) AS signed) AS complete,
        0 AS dateType,
        a.project_id projectId
        FROM
        dw_case_design_info a
        WHERE
        DATE( a.created_time ) = CURDATE()
        AND a.tenant_id = #{tenantId}
        AND a.project_id IN
        <foreach collection="projectIds" item="projectId" open="(" separator="," close=")">
            #{projectId, jdbcType=VARCHAR}
        </foreach>
        GROUP BY
        a.created_by,
        a.project_id
        UNION ALL
        SELECT
        UUID_SHORT() AS id,
        a.tenant_id tenantId,
        a.created_by AS receiverName,
        CAST(SUM( CASE a.case_STATUS WHEN 0 THEN 1 ELSE 0 END ) AS signed) AS designering,
        CAST(SUM( CASE a.case_STATUS WHEN 1 THEN 1 ELSE 0 END ) AS signed) AS complete,
        1 AS dateType,
        a.project_id projectId
        FROM
        dw_case_design_info a
        WHERE
        DATE( a.created_time ) BETWEEN date(#{lastSat}) AND CURDATE()
        AND a.tenant_id = #{tenantId}
        AND a.project_id IN
        <foreach collection="projectIds" item="projectId" open="(" separator="," close=")">
            #{projectId, jdbcType=VARCHAR}
        </foreach>
        GROUP BY
        a.created_by,
        a.project_id
        UNION ALL
        SELECT
        UUID_SHORT() AS id,
        a.tenant_id tenantId,
        a.created_by AS receiverName,
        CAST(SUM( CASE a.case_STATUS WHEN 0 THEN 1 ELSE 0 END ) AS signed) AS designering,
        CAST(SUM( CASE a.case_STATUS WHEN 1 THEN 1 ELSE 0 END ) AS signed) AS complete,
        2 AS dateType,
        a.project_id projectId
        FROM
        dw_case_design_info a
        WHERE
        a.tenant_id = #{tenantId}
        AND a.project_id IN
        <foreach collection="projectIds" item="projectId" open="(" separator="," close=")">
            #{projectId, jdbcType=VARCHAR}
        </foreach>
        GROUP BY
        a.created_by,
        a.project_id
        ) temp_table
    </update>

    <delete id="deleteCompleteByProjectIds" parameterType="net.northking.cctp.dw.dto.DwCaseDesignInfoDto">
        DELETE FROM cctp_dw.case_design_complete_details WHERE project_id IN
        <foreach collection="projectIds" item="projectId" open="(" separator="," close=")">
            #{projectId, jdbcType=VARCHAR}
        </foreach>
    </delete>


</mapper>