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


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

    <resultMap id="DetailMap" type="net.northking.cctp.dw.dto.atu.DwAtuScriptInfoDetailDto">
        <!--  -->
        <id column="id" jdbcType="VARCHAR" property="id"/>
        <!--  -->
        <result column="script_type" jdbcType="INTEGER" property="scriptType"/>
        <!--  -->
        <result column="script_name" jdbcType="VARCHAR" property="scriptName"/>
        <!--  -->
        <result column="tenant_id" jdbcType="VARCHAR" property="tenantId"/>
        <!--  -->
        <result column="project_id" jdbcType="VARCHAR" property="projectId"/>
        <!--  -->
        <result column="is_deleted" jdbcType="INTEGER" property="isDeleted"/>
    </resultMap>

    <select id="pagingQuery" resultMap="DetailMap" parameterType="net.northking.cctp.dw.db.entity.DwAtuScriptInfo">
        select
        id
        ,script_type
        ,script_name
        ,tenant_id
        ,project_id
        ,is_deleted
        from
        <include refid="Table_Name"/>
        <trim prefix="WHERE" prefixOverrides="AND">
            <if test="id != null">
                AND id = #{id,jdbcType=VARCHAR}
            </if>
            <if test="scriptType != null">
                AND script_type = #{scriptType,jdbcType=INTEGER}
            </if>
            <if test="scriptName != null">
                AND script_name = #{scriptName,jdbcType=VARCHAR}
            </if>
            <if test="tenantId != null">
                AND tenant_id = #{tenantId,jdbcType=VARCHAR}
            </if>
            <if test="projectId != null">
                AND project_id = #{projectId,jdbcType=VARCHAR}
            </if>
            <if test="isDeleted != null">
                AND is_deleted = #{isDeleted,jdbcType=INTEGER}
            </if>
        </trim>
    </select>
    <select id="countByGroup" resultType="net.northking.cctp.dw.dto.atu.DwAtuScriptInfoDetailDto">
        select
        count(dsi.script_type) mount,
        dsi.script_type `type`,
        dsiv.created_by createdBy
        from
        <include refid="Table_Name"/>
        dsi
        inner join dw_atu_script_version dsiv
        on dsi.id=dsiv.script_id
        where
        dsi.project_id in
        <foreach collection="projectIds" item="projectId" open="(" separator="," close=")">
            #{projectId}
        </foreach>
        <if test="startTime!=null and endTime!=null">
            and
            dsiv.created_time between date_format(#{startTime},"%Y-%m-%d") and date_format(#{endTime},"%Y-%m-%d")
        </if>
        and
        dsi.is_deleted=0
        and
        dsiv.version_id=1
        group by dsiv.created_by,dsi.script_type
    </select>
    <select id="pageQuery" resultType="net.northking.cctp.dw.dto.atu.DwAtuScriptInfoDto">
        SELECT
        DISTINCT
        script.script_manager_oper_code as snowWolfName,
        script.script_manager as name,
        script.script_manager_company as companyName,
        script.project_name as projectName,
        script.script_type as scriptType,
        script.script_code as scriptCode,
        script.script_name as scriptName,
        script.id as scriptId,
        script.script_manager_id as userId,
        task.min_time as firstExecutionTime
        FROM
        dw_atu_script_info script
        LEFT JOIN dw_atu_plan_script_link link ON script.id = link.script_id
        INNER JOIN
        ( SELECT
        a.script_id,
        min( a.start_time ) AS min_time,
        b.plan_id
        FROM
        dw_atu_plan_task a,
        dw_atu_plan_batch b
        WHERE
        a.`status` = '2'
        AND a.batch_id = b.id
        AND b.`status` = 2
        GROUP BY
        a.script_id ) task
        ON task.script_id = link.script_id
        AND script.id = task.script_id
        LEFT JOIN dw_atu_plan_info plan ON plan.id = task.plan_id
        AND link.plan_id = plan.id
        where 1=1
        <if test="name!=null">
            and script.script_manager_id = #{name}
        </if>
        <if test="projectName!=null">
            and script.project_id = #{projectName}
        </if>
        <if test="startTime!=null">
            and task.min_time <![CDATA[>=]]> #{startTime}
        </if>
        <if test="endTime!=null">
            and task.min_time <![CDATA[<=]]> #{endTime}
        </if>
        <if test="companyName!=null">
            and script.script_manager_company = #{companyName}
        </if>

        order by firstExecutionTime desc
    </select>


    <!--getScriptExecution 报表 lwz-->
    <select id="getScriptExecution" resultType="net.northking.cctp.dw.dto.atu.DwAtuScriptExecutionDataDto">
        select
        DISTINCT
        snowWolfName,managerId,name,companyName,projectName,scriptType,
        SUM(total) totalSum,SUM(yes) successNum,sum(noyes) failNum
        from (
        select
        b.script_id
        ,b.batch_id
        ,b.start_time
        ,b.end_time
        ,b.total
        ,b.yes
        ,b.noyes
        ,script.script_type
        ,
        CASE
        WHEN script.script_type = '5' THEN
        '场景'
        WHEN (
        script.script_type <![CDATA[>=]]> 0
        AND script.script_type <![CDATA[<=]]> 4
        ) THEN
        '一般'
        ELSE
        NULL
        END AS scriptType
        ,batch.trigger_type
        ,script.project_name as projectName

        ,CASE batch.trigger_type
        WHEN 1 THEN script.script_manager_oper_code
        ELSE batch.manager_oper_code
        END
        as snowWolfName
        ,CASE batch.trigger_type
        WHEN 1 THEN script.script_manager
        ELSE batch.executed_name
        END
        as `name`
        ,CASE batch.trigger_type
        WHEN 1 THEN script.script_manager_id
        ELSE batch.executed_by
        END
        as `managerId`
        ,CASE batch.trigger_type
        WHEN 1 THEN script.script_manager_company
        ELSE batch.manager_company
        END
        as `companyName`
        from (
        select
        a.script_id,
        a.batch_id,
        a.start_time,a.end_time,
        count(1) as total,
        sum(case when LENGTH(REPLACE(a.newstatus,"2",""))=0 then 1 else 0 end)  as yes,
        (count(1) - sum(case when LENGTH(REPLACE(a.newstatus,"2",""))=0 then 1 else 0 end))as noyes
        from (
        select batch_id,script_id,start_time,end_time,REPLACE(GROUP_CONCAT(status),",","")as newstatus from dw_atu_plan_task group by batch_id,script_id) a
        GROUP BY a.script_id,a.batch_id
        ) b
        INNER JOIN dw_atu_script_info script on script.id=b.script_id
        INNER JOIN dw_atu_plan_batch batch on batch.id=b.batch_id and batch.`status` IN (2, 3)
        INNER JOIN dw_atu_plan_info plan on plan.id=batch.plan_id and plan.is_deleted = 0
        where 1=1
        <if test="projectName!=null and projectName!=''">
            AND script.project_id = #{projectName}
        </if>
        <if test="startTime!=null and startTime!=''">
            AND b.start_time <![CDATA[>=]]> DATE_FORMAT(#{startTime},'%Y-%m-%d 00:00:00')
        </if>
        <if test="endTime!=null and endTime!='' ">
            AND b.start_time <![CDATA[<=]]> DATE_FORMAT(#{endTime},'%Y-%m-%d 23:59:59')
        </if>
        ORDER BY b.end_time DESC
        ) temp
        where 1=1
        <if test="name!=null and name!='' ">
            AND temp.managerId = #{name}
        </if>

        <if test="companyName!=null and companyName!='' ">
            AND temp.companyName = #{companyName}
        </if>
        GROUP BY snowWolfName,name,projectName,scriptType
    </select>
    <select id="projectOverviewQuery" resultType="net.northking.cctp.dw.dto.ProjectScriptNumDto">
        SELECT
        count(*) AS scriptNum
        <if test="userId==null or userId==''">
            ,(
            CASE
            script_type
            WHEN 0 THEN
            '自定义脚本'
            WHEN 1 THEN
            'PC端B/S脚本'
            WHEN 2 THEN
            'PC端C/S脚本'
            WHEN 3 THEN
            '移动端Android脚本'
            WHEN 4 THEN
            '移动端IOS脚本'
            WHEN 5 THEN
            '场景脚本'
            END
            ) AS scriptType,
            script_type AS scriptTypeCode
        </if>
        FROM
        dw_atu_script_info
        where project_id=#{projectId} and is_deleted=0 and script_type!=5
        <if test="userId!=null and userId!=''">
            and script_manager_id =#{userId}
        </if>
        <if test="userId==null or userId==''">
            GROUP BY script_type
        </if>
    </select>
    <select id="getSenceScriptNum" resultType="java.lang.Integer">
        select count(*) from dw_atu_script_info where script_type='5' and is_deleted=0
        <if test="projectId!=null and projectId!=''">
            and project_id=#{projectId}
        </if>
        <if test="userId!=null and userId!=''">
            and script_manager_id =#{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="getScriptNum" resultType="java.lang.Integer">
        select count(*) from dw_atu_script_info where script_type!='5' and is_deleted=0
        <if test="userId!=null and userId!=''">
            and script_manager_id =#{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="getModifyInfo" resultType="net.northking.cctp.dw.dto.ModifyInfoDto">
        SELECT
        projectId,
        scriptOrPlanName,
        type,
        updatedTime,
        is_deleted,
        id
        FROM
        (
        SELECT
        id as id,
        project_id AS projectId,
        script_name AS scriptOrPlanName,
        (case script_type when 5 then '场景脚本'
        else '一般脚本' end) AS type,
        updated_time AS updatedTime,
        updated_by as userId,
        is_deleted
        FROM
        dw_atu_script_info UNION ALL
        SELECT
        id as id,
        project_id AS projectId,
        plan_name AS scriptOrPlanName,
        '测试计划' AS type,
        updated_time AS updatedTime,
        updated_by as userId,
        is_deleted
        FROM
        dw_atu_plan_info
        ) a
        where a.userId=#{userId} and a.is_deleted=0
        <if test="projectIdList != null and projectIdList.size > 0">
            and a.projectId in
            <foreach collection="projectIdList" open="(" index="index" item="item" close=")" separator=",">
                #{item,jdbcType=VARCHAR}
            </foreach>
        </if>
        ORDER BY a.updatedTime DESC limit 0,10
    </select>
    <select id="queryScriptDetail" resultType="net.northking.cctp.dw.dto.atu.DwAtuScriptInfoDto">
        SELECT
        DISTINCT
        script.id as scriptId,
        script.script_code as scriptCode,
        script.script_name as scriptName,
        script.script_type as scriptType,
        script.script_manager_id as userId,
        script.project_id projectId,
        script.created_time createdTime
        FROM
        dw_atu_script_info script
        LEFT JOIN dw_atu_plan_script_link link ON script.id = link.script_id
        INNER JOIN
        ( SELECT
        a.script_id,
        min( a.start_time ) AS min_time,
        b.plan_id
        FROM
        dw_atu_plan_task a,
        dw_atu_plan_batch b
        WHERE
        a.`status` = '2'
        AND a.batch_id = b.id
        AND b.`status` = 2
        GROUP BY
        a.script_id ) task
        ON task.script_id = link.script_id
        AND script.id = task.script_id
        LEFT JOIN dw_atu_plan_info plan ON plan.id = task.plan_id
        AND link.plan_id = plan.id
        where 1=1
        <if test="name!=null">
            and script.script_manager_id = #{name}
        </if>
        <if test="projectName!=null">
            and script.project_id = #{projectName}
        </if>
        <if test="startTime!=null">
            and DATE_FORMAT(task.min_time,'%y-%m-%d') <![CDATA[>=]]> DATE_FORMAT(#{startTime},'%y-%m-%d')
        </if>
        <if test="endTime!=null">
            and DATE_FORMAT(task.min_time,'%y-%m-%d') <![CDATA[<=]]> DATE_FORMAT(#{endTime},'%y-%m-%d')
        </if>
        <if test="companyName!=null">
            and script.script_manager_company = #{companyName}
        </if>
    </select>
</mapper>