<?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.MtCaseExecChartDao">


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

    <resultMap id="DetailMap" type="net.northking.cctp.dw.dto.MtCaseExecChartDetailDto">
        <!--  -->
        <id column="id" jdbcType="VARCHAR" property="id"/>
        <!-- 创建人 -->
        <result column="created_by" jdbcType="VARCHAR" property="createdBy"/>
        <!-- 租户id -->
        <result column="tenant_id" jdbcType="VARCHAR" property="tenantId"/>
        <!-- 创建时间 -->
        <result column="created_time" jdbcType="VARCHAR" property="createdTime"/>
        <!-- 未执行 -->
        <result column="no_exec" jdbcType="INTEGER" property="noExec"/>
        <!-- 无效 -->
        <result column="invalid" jdbcType="INTEGER" property="invalid"/>
        <!-- 失败 -->
        <result column="fail" jdbcType="INTEGER" property="fail"/>
        <!-- 阻塞 -->
        <result column="block" jdbcType="INTEGER" property="block"/>
        <!-- 挂起 -->
        <result column="hang" jdbcType="INTEGER" property="hang"/>
        <!-- 未完成 -->
        <result column="incomplete" jdbcType="INTEGER" property="incomplete"/>
        <!-- 通过 -->
        <result column="pass" jdbcType="INTEGER" property="pass"/>
        <!--  -->
        <result column="project_id" jdbcType="VARCHAR" property="projectId"/>
        <!-- 类型 -->
        <result column="statistics_type" jdbcType="INTEGER" property="statisticsType"/>
    </resultMap>

    <select id="pagingQuery" resultMap="DetailMap" parameterType="net.northking.cctp.dw.db.entity.MtCaseExecChart">
        select
        id
        ,created_by
        ,tenant_id
        ,created_time
        ,no_exec
        ,invalid
        ,fail
        ,block
        ,hang
        ,incomplete
        ,pass
        ,project_id
        ,statistics_type
        from
        <include refid="Table_Name"/>
        <trim prefix="WHERE" prefixOverrides="AND">
            <if test="id != null">
                AND id = #{id,jdbcType=VARCHAR}
            </if>
            <if test="createdBy != null">
                AND created_by = #{createdBy,jdbcType=VARCHAR}
            </if>
            <if test="tenantId != null">
                AND tenant_id = #{tenantId,jdbcType=VARCHAR}
            </if>
            <if test="createdTime != null">
                AND created_time = #{createdTime,jdbcType=VARCHAR}
            </if>
            <if test="noExec != null">
                AND no_exec = #{noExec,jdbcType=INTEGER}
            </if>
            <if test="invalid != null">
                AND invalid = #{invalid,jdbcType=INTEGER}
            </if>
            <if test="fail != null">
                AND fail = #{fail,jdbcType=INTEGER}
            </if>
            <if test="block != null">
                AND block = #{block,jdbcType=INTEGER}
            </if>
            <if test="hang != null">
                AND hang = #{hang,jdbcType=INTEGER}
            </if>
            <if test="incomplete != null">
                AND incomplete = #{incomplete,jdbcType=INTEGER}
            </if>
            <if test="pass != null">
                AND pass = #{pass,jdbcType=INTEGER}
            </if>
            <if test="projectId != null">
                AND project_id = #{projectId,jdbcType=VARCHAR}
            </if>
            <if test="statisticsType != null">
                AND statistics_type = #{statisticsType,jdbcType=INTEGER}
            </if>
        </trim>
    </select>

    <select id="listQuery" resultType="net.northking.cctp.dw.dto.MtCaseExecChartDetailDto">
        SELECT
        created_by createdBy,
        SUM(no_exec) noExec,
        SUM(invalid) invalid,
        SUM(fail) fail,
        SUM(block) block,
        SUM(hang) hang,
        SUM(incomplete) incomplete,
        SUM(pass) pass
        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>
            <if test="dateType!=null and dateType!=''">
                <!--        本日-->
                <if test="dateType=='1'.toString()">
                    and created_time=date_format(curdate(),'%Y-%m-%d')
                </if>
            </if>
            AND statistics_type=0
        </trim>
        GROUP BY created_by
    </select>

    <update id="syncExecNotice">
        INSERT INTO cctp_dw.mt_case_exec_chart ( id, created_by, tenant_id, created_time, no_exec, invalid, fail, block,
        hang, incomplete, pass, project_id, statistics_type ) SELECT
        temp.id,
        temp.userName,
        temp.tenantId,
        temp.createdTime,
        temp.no_exec,
        temp.invalid,
        temp.fail,
        temp.block,
        temp.hang,
        temp.incomplete,
        temp.pass,
        temp.projectId,
        temp.statisticsType
        FROM
        (
        SELECT
        UUID() id,
        0 AS statisticsType,
        mt.tenant_id tenantId,
        mt.project_id projectId,
        mt.updated_by userName,
        date_format( CURDATE(), '%Y-%m-%d' ) AS createdTime,
        cast((SUM( CASE exec_status WHEN 0 THEN 1 ELSE 0 END )+0) as signed) no_exec,
        cast((SUM( CASE exec_status WHEN 1 THEN 1 ELSE 0 END )+0) as signed) invalid,
        cast((SUM( CASE exec_status WHEN 2 THEN 1 ELSE 0 END )+0) as signed) fail,
        cast((SUM( CASE exec_status WHEN 3 THEN 1 ELSE 0 END )+0) as signed) block,
        cast((SUM( CASE exec_status WHEN 4 THEN 1 ELSE 0 END )+0) as signed) hang,
        cast((SUM( CASE exec_status WHEN 5 THEN 1 ELSE 0 END )+0) as signed) incomplete,
        cast((SUM( CASE exec_status WHEN 6 THEN 1 ELSE 0 END )+0) as signed) pass
        FROM
        cctp_dw.dw_manual_exec_job mt
        WHERE
        mt.updated_by IS NOT NULL
        and
        mt.project_id IN
        <foreach collection="projectIds" item="projectId" open="(" separator="," close=")">
            #{projectId, jdbcType=VARCHAR}
        </foreach>
        and  mt.tenant_id = #{tenantId}
        GROUP BY
        mt.updated_by,
        mt.project_id
        UNION

        SELECT
        UUID() id,
        1 AS statisticsType,
        mt.tenant_id tenantId,
        mt.project_id projectId,
        mt.updated_by userName,
        date_format( CURDATE(), '%Y-%m-%d' ) AS createdTime,
        cast((SUM( CASE exec_status WHEN 0 THEN 1 ELSE 0 END )+0) as signed) no_exec,
        cast((SUM( CASE exec_status WHEN 1 THEN 1 ELSE 0 END )+0) as signed) invalid,
        cast((SUM( CASE exec_status WHEN 2 THEN 1 ELSE 0 END )+0) as signed) fail,
        cast((SUM( CASE exec_status WHEN 3 THEN 1 ELSE 0 END )+0) as signed) block,
        cast((SUM( CASE exec_status WHEN 4 THEN 1 ELSE 0 END )+0) as signed) hang,
        cast((SUM( CASE exec_status WHEN 5 THEN 1 ELSE 0 END )+0) as signed) incomplete,
        cast((SUM( CASE exec_status WHEN 6 THEN 1 ELSE 0 END )+0) as signed) pass
        FROM
        cctp_dw.dw_manual_exec_job mt
        WHERE
        mt.updated_by IS NOT NULL
        and
        mt.project_id IN
        <foreach collection="projectIds" item="projectId" open="(" separator="," close=")">
            #{projectId, jdbcType=VARCHAR}
        </foreach>
        and  mt.tenant_id = #{tenantId}
        AND date_format( mt.updated_time, '%Y-%m-%d' )= date_format( curdate(), '%Y-%m-%d' )
        GROUP BY
        mt.updated_by,
        mt.project_id
        ) temp
    </update>

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


    <select id="queryByProjectId" resultType="net.northking.cctp.dw.db.entity.MtCaseExecChart">
        SELECT
        mc.created_time createdTime,
        mc.amount nonExec
        from
        mt_case_non_exec mc join cp_project_detail cp on mc.project_id=cp.project_id and cp.detail_type=3
        where
        mc.project_id=#{projectId}
        AND
        mc.created_time between date_format(cp.start_time,'%Y-%m-%d') and date_format(cp.end_time,'%Y-%m-%d')
        order by mc.created_time asc
    </select>
</mapper>