<?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 14:58:27  -->
<mapper namespace="net.northking.cctp.dw.db.dao.MtTestPointStatisticsDao">


    <!-- 请在本文件内添加自定义SQL语句 -->

    <resultMap id="DetailMap" type="net.northking.cctp.dw.dto.MtTestPointStatisticsDetailDto">
        <!--  -->
        <id column="id" jdbcType="VARCHAR" property="id"/>
        <!-- 测试点创建人 -->
        <result column="user_name" jdbcType="VARCHAR" property="userName"/>
        <!-- 登陆账号 -->
        <result column="login_no" jdbcType="VARCHAR" property="loginNo"/>
        <!-- 测试点数量 -->
        <result column="test_point_count" jdbcType="INTEGER" property="testPointCount"/>
        <!-- 创建时间 -->
        <result column="created_time" jdbcType="VARCHAR" property="createdTime"/>
        <!--  -->
        <result column="project_id" jdbcType="VARCHAR" property="projectId"/>
        <!-- 租户id -->
        <result column="tenant_id" jdbcType="VARCHAR" property="tenantId"/>
    </resultMap>

    <select id="pagingQuery" resultMap="DetailMap"
            parameterType="net.northking.cctp.dw.db.entity.MtTestPointStatistics">
        select
        id
        ,user_name
        ,login_no
        ,test_point_count
        ,created_time
        ,project_id
        ,tenant_id
        from
        <include refid="Table_Name"/>
        <trim prefix="WHERE" prefixOverrides="AND">
            <if test="id != null">
                AND id = #{id,jdbcType=VARCHAR}
            </if>
            <if test="userName != null">
                AND user_name = #{userName,jdbcType=VARCHAR}
            </if>
            <if test="loginNo != null">
                AND login_no = #{loginNo,jdbcType=VARCHAR}
            </if>
            <if test="testPointCount != null">
                AND test_point_count = #{testPointCount,jdbcType=INTEGER}
            </if>
            <if test="createdTime != null">
                AND created_time = #{createdTime,jdbcType=VARCHAR}
            </if>
            <if test="projectId != null">
                AND project_id = #{projectId,jdbcType=VARCHAR}
            </if>
            <if test="tenantId != null">
                AND tenant_id = #{tenantId,jdbcType=VARCHAR}
            </if>
        </trim>
    </select>

    <select id="listQuery" resultType="net.northking.cctp.dw.dto.MtTestPointStatisticsDetailDto">
        select
        user_name userName,
        sum(test_point_count) testPointCount
        FROM
        <include refid="Table_Name"/>
        <trim prefix="WHERE" prefixOverrides="AND">
            <if test="tenantId != null">
                AND tenant_id = #{tenantId,jdbcType=VARCHAR}
            </if>
            <if test="proId != null">
                AND project_id = #{proId,jdbcType=VARCHAR}
            </if>
            <if test="startTime != null and endTime !=null and startTime != '' and endTime !=''">
                AND created_time between #{startTime} and #{endTime}
            </if>
        </trim>
        GROUP BY user_name
    </select>

    <update id="syncPointNotice">
        INSERT INTO cctp_dw.mt_test_point_statistics ( id, user_name, login_no, test_point_count, created_time, project_id, tenant_id ) SELECT
        temp.id,
        temp.createdBy,
        temp.createdBy,
        temp.testPointCount,
        temp.createdTime,
        temp.projectId,
        temp.tenantId
        FROM
        (
        SELECT
        UUID() id,
        COUNT( 1 ) testPointCount,
        date_format( curdate(), '%Y-%m-%d' ) createdTime,
        p.tenant_id tenantId,
        p.project_id projectId,
        p.created_by createdBy
        FROM
        cctp_dw.dw_test_point p
        WHERE
        date_format( p.created_time, '%Y-%m-%d' )= date_format( curdate(), '%Y-%m-%d' )
        and
        p.project_id in
        <foreach collection="projectIds" item="projectId" open="(" separator="," close=")">
            #{projectId, jdbcType=VARCHAR}
        </foreach>
        and tenant_id = #{tenantId}
        GROUP BY
        p.created_by,
        p.project_id
        ) temp
    </update>

    <delete id="deleteByExampleByProjectIds" parameterType="net.northking.cctp.dw.db.entity.MtTestPointStatistics">
        DELETE FROM cctp_dw.mt_test_point_statistics WHERE project_id IN
        <foreach collection="projectIds" item="projectId" open="(" separator="," close=")">
            #{projectId, jdbcType=VARCHAR}
        </foreach>
        AND created_time=#{createdTime}
    </delete>




</mapper>