<?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 2022 . All rights reserved. -->
<!--  Mybatis数据库持久层（业务层映射文件）：脚本信息同步表 -->
<!--  version: 1.0  -->
<!--  author:  maven-cctp-plugin    -->
<!--  date:  2022-11-25 11:15:19  -->
<mapper namespace="net.northking.cctp.dw.db.dao.DwAtuPlanInfoDao">


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

    <resultMap id="DetailMap" type="net.northking.cctp.dw.dto.atu.DwAtuPlanExecutionDataDto">
        <!--  -->
        <id column="id" jdbcType="VARCHAR" property="id"/>
        <!--  -->
        <!--  -->
        <result column="plan_id" jdbcType="VARCHAR" property="planId"/>
        <!--  -->
        <result column="project_id" jdbcType="VARCHAR" property="projectId"/>
        <!--  -->
    </resultMap>


    <select id="pageQuery" resultType="net.northking.cctp.dw.dto.atu.DwAtuPlanExecutionDataDto">
        select
        DISTINCT
        e.planId,
        e.envId,
        e.tag,
        e.planName,
        e.projectId,
        e.projectName,
        e.planManagerId,
        e.planManager,
        e.successNum,
        e.failNum 	,
        e.created_time
        from (
        select
        d.plan_id planId,
        d.env_id envId,
        d.env_name tag,
        d.plan_name planName,
        d.project_id projectId,
        d.project_name projectName,
        d.created_by planManagerId,
        d.creator planManager,
        sum(d.successNum)successNum,
        SUM(d.failNum) failNum,
        d.created_time
        from (
        SELECT
        c.id,
        c.plan_id,
        c.env_id,
        c.start_time,
        c.end_time,
        c.env_name,
        c.plan_name,
        c.project_id,
        c.project_name,
        c.created_by,
        c.creator,
        sum(
        CASE
        WHEN LENGTH(
        REPLACE (c.success_rate, "100%", "")
        ) = 0 THEN
        1
        ELSE
        0
        END
        ) AS successNum,
        (
        count(1) - sum(
        CASE
        WHEN LENGTH(
        REPLACE (c.success_rate, "100%", "")
        ) = 0 THEN
        1
        ELSE
        0
        END
        )
        ) failNum,
        c.created_time
        FROM
        (
        SELECT
        batch.id,
        batch.plan_id,
        batch.start_time,
        batch.end_time,
        batch.`status`,
        batch.success_rate,
        task.env_id,
        link.env_name,
        info.plan_name,
        info.project_id,
        info.project_name,
        info.created_by,
        info.creator,
        info.is_deleted,
        info.created_time
        FROM
        dw_atu_plan_batch batch
        INNER JOIN (
        SELECT
        task.batch_id,
        task.env_id
        FROM
        dw_atu_plan_task task
        GROUP BY
        batch_id
        ) task ON task.batch_id = batch.id
        INNER JOIN dw_atu_plan_env_link link ON link.env_id = task.env_id
        AND link.plan_id = batch.plan_id
        INNER JOIN dw_atu_plan_info info ON info.id = batch.plan_id
        AND info.is_deleted = 0
        WHERE
        batch.`status` IN (2, 3)
        GROUP BY
        batch.id,
        batch.plan_id,
        batch.`status`,
        batch.success_rate,
        task.env_id,
        info.project_id

        ) c
        Where 1=1
        <if test="projectId!=null and projectId !=''">
            AND c.project_id = #{projectId}
        </if>
        <if test="planId!=null and planId!='' ">
            AND c.plan_id = #{planId}
        </if>
        <if test="envId!=null and envId!='' ">
            AND c.env_id = #{envId}
        </if>
        <if test="planManager!=null and planManager!='' ">
            AND c.created_by = #{planManager}
        </if>
        <if test="startTime!=null and startTime!=''">
            AND c.start_time <![CDATA[>=]]> DATE_FORMAT(#{startTime},'%Y-%m-%d 00:00:00')
        </if>
        <if test="endTime!=null and endTime!='' ">
            AND c.start_time <![CDATA[<=]]> DATE_FORMAT(#{endTime},'%Y-%m-%d 23:59:59')
        </if>
        GROUP BY
        c.id,
        c.plan_id,
        c.env_id,
        c.project_id
        ) d
        GROUP BY
        d.plan_id,
        d.project_id,
        d.created_by,
        d.env_id ) e
        left join (
        SELECT
        team.id AS teamId,
        team.NAME AS testGroup,
        team.manager_id AS testGroupLeader,
        member.member_id AS userId
        FROM
        ${platform}.clt_test_team team,
        ${platform}.clt_test_team_member member
        WHERE
        team.id = member.test_team_id
        AND team.enabled = 1
        )  testTeam on testTeam.userId = e.planManagerId where 1=1
        <if test="testGroup!= null and testGroup.size() != 0">
            and testTeam.teamId in
            <foreach collection="testGroup" open="(" close=")" separator="," item="tg">
                #{tg}
            </foreach>
        </if>
        order by e.created_time desc
    </select>

    <select id="getPlanList" resultType="net.northking.cctp.dw.dto.atu.DwOptionDto">
        select DISTINCT id as value,plan_name as label from dw_atu_plan_info where is_deleted=0
    </select>

    <select id="getNameByPlanId" resultType="net.northking.cctp.dw.db.entity.DwAtuPlanInfo">
        select DISTINCT id,plan_name planName from dw_atu_plan_info where id =#{planId}
    </select>

    <select id="getFirstDateAndSum" resultType="net.northking.cctp.dw.dto.atu.DwDataAndSumDto">
        SELECT
        pb.end_time AS successTime,
        pb.success_num successNum,
        task.env_id
        FROM
        dw_atu_plan_batch pb
        LEFT JOIN (select batch_id,env_id from dw_atu_plan_task GROUP BY batch_id)
        task ON pb.id = task.batch_id
        WHERE pb.success_rate = '100%'
        AND task.env_id=#{envId} and pb.plan_id=#{planId}
        <if test="startTime!=null and startTime!=''">
            AND pb.end_time <![CDATA[>=]]> DATE_FORMAT(#{startTime},'%Y-%m-%d 00:00:00')
        </if>
        <if test="endTime!=null and endTime!='' ">
            AND pb.end_time <![CDATA[<=]]> DATE_FORMAT(#{endTime},'%Y-%m-%d 23:59:59')
        </if>
        ORDER BY
        pb.end_time
        LIMIT 1
    </select>

    <select id="getEndDateAndSum" resultType="net.northking.cctp.dw.dto.atu.DwDataAndSumDto">
        SELECT
        pb.end_time AS successTime,
        pb.success_num successNum,
        task.env_id
        FROM
        dw_atu_plan_batch pb
        LEFT JOIN (select batch_id,env_id from dw_atu_plan_task GROUP BY batch_id)
        task ON pb.id = task.batch_id
        WHERE pb.success_rate = '100%'
        AND task.env_id=#{envId} and pb.plan_id=#{planId}
        <if test="startTime!=null and startTime!=''">
            AND pb.end_time <![CDATA[>=]]> DATE_FORMAT(#{startTime},'%Y-%m-%d 00:00:00')
        </if>
        <if test="endTime!=null and endTime!='' ">
            AND pb.end_time <![CDATA[<=]]> DATE_FORMAT(#{endTime},'%Y-%m-%d 23:59:59')
        </if>
        ORDER BY
        pb.end_time DESC
        LIMIT 1
    </select>
    <select id="getPlanNum" resultType="java.lang.Integer">
        select count(*) from dw_atu_plan_info where 1=1 and is_deleted=0
        <if test="projectId!=null and projectId!=''">
            and project_id =#{projectId}
        </if>
        <if test="userId!=null and userId!=''">
            and created_by=#{userId}
        </if>
        <if test="queryToday==1">
            <if test="todayStart!=null">
                and created_time <![CDATA[>=]]> #{todayStart}
            </if>
            <if test="todayEnd!=null">
                and created_time <![CDATA[<=]]> #{todayEnd}
            </if>

        </if>
    </select>
    <select id="failPlanQuery" resultType="net.northking.cctp.dw.dto.FailPlanInfoDto">
        select
        id as planId,
        plan_name as planName,
        project_id as projectId
        from dw_atu_plan_info
        where 1=1 and is_deleted=0
        <if test="projectIdList != null and projectIdList.size > 0">
            and project_id in
            <foreach collection="projectIdList" open="(" index="index" item="item" close=")" separator=",">
                #{item,jdbcType=VARCHAR}
            </foreach>
        </if>
    </select>
    <select id="planQuery" resultType="net.northking.cctp.dw.dto.PlanQueryRspDto">
        select
        has_corn as hasCorn,
        cron_expr as cronExpr
        from dw_atu_plan_info
        where has_corn='1' and is_deleted=0 and is_enabled=1
    </select>
</mapper>