<?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-10-18 17:29:31  -->
<mapper namespace="net.northking.cctp.scriptcase.db.dao.AtuScriptInfoDao">

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

  <resultMap id="DetailMap" type="net.northking.cctp.scriptcase.dto.atuScript.atuScriptInfo.AtuScriptInfoDetailDto">
      <!-- 脚本ID -->
    <id column="id" jdbcType="VARCHAR" property="id"/>
      <!-- 脚本目录ID -->
    <result column="group_id" jdbcType="VARCHAR" property="groupId"/>
        <!-- 脚本名称 -->
    <result column="script_name" jdbcType="VARCHAR" property="scriptName"/>
        <!-- 脚本类型 -->
    <result column="script_type" jdbcType="VARCHAR" property="scriptType"/>
        <!-- 脚本编号 -->
    <result column="script_code" jdbcType="VARCHAR" property="scriptCode"/>
        <!-- 最新版本ID -->
    <result column="latest_version_id" jdbcType="INTEGER" property="latestVersionId"/>
        <!-- 脚本地址 -->
    <result column="script_url" jdbcType="VARCHAR" property="scriptUrl"/>
        <!-- 脚本快照数据地址 -->
    <result column="data_url" jdbcType="VARCHAR" property="dataUrl"/>
        <!-- 是否删除 -->
    <result column="is_deleted" jdbcType="VARCHAR" property="isDeleted"/>
        <!-- 系统ID -->
    <result column="project_id" jdbcType="VARCHAR" property="projectId"/>
        <!-- 租户号 -->
    <result column="tenant_id" jdbcType="VARCHAR" property="tenantId"/>
        <!-- 创建人 -->
    <result column="created_by" jdbcType="VARCHAR" property="createdBy"/>
        <!-- 创建时间 -->
    <result column="created_time" jdbcType="TIMESTAMP" property="createdTime"/>
        <!-- 更新人 -->
    <result column="updated_by" jdbcType="VARCHAR" property="updatedBy"/>
        <!-- 更新时间 -->
    <result column="updated_time" jdbcType="TIMESTAMP" property="updatedTime"/>
    <!-- 应用ID -->
    <result column="app_id" jdbcType="VARCHAR" property="appId"/>
    <!-- 应用包名 -->
    <result column="app_package" jdbcType="VARCHAR" property="appPackage"/>
    <!-- 版本名称 -->
    <result column="version_name" jdbcType="VARCHAR" property="versionName"/>
    <!-- 分组 -->
    <result column="name_path" jdbcType="VARCHAR" property="namePath"/>
    <!-- 应用平台 -->
    <result column="platform" jdbcType="VARCHAR" property="platform"/>

    <result column="principal_id" jdbcType="VARCHAR" property="principalId"/>
    </resultMap>

  <select id="pagingQuery" resultMap="DetailMap" parameterType="net.northking.cctp.scriptcase.db.entity.AtuScriptInfo">
    select
    id
    ,group_id
    ,script_name
    ,script_type
    ,latest_version_id
    ,script_url
    ,data_url
    ,is_deleted
    ,project_id
    ,tenant_id
    ,created_by
    ,created_time
    ,updated_by
    ,updated_time
    ,app_id
    ,app_package
    ,platform
    from
    <include refid="Table_Name"/>
    <trim prefix="WHERE" prefixOverrides="AND" >
      <if test="id != null">
        AND id = #{id,jdbcType=VARCHAR}
      </if>
      <if test="groupId != null">
        AND group_id = #{groupId,jdbcType=VARCHAR}
      </if>
      <if test="scriptName != null">
        AND script_name = #{scriptName,jdbcType=VARCHAR}
      </if>
      <if test="scriptType != null">
        AND script_type = #{scriptType,jdbcType=VARCHAR}
      </if>
      <if test="latestVersionId != null">
        AND latest_version_id = #{latestVersionId,jdbcType=INTEGER}
      </if>
      <if test="scriptUrl != null">
        AND script_url = #{scriptUrl,jdbcType=VARCHAR}
      </if>
      <if test="dataUrl != null">
        AND data_url = #{dataUrl,jdbcType=VARCHAR}
      </if>
      <if test="isDeleted != null">
        AND is_deleted = #{isDeleted,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>
      <if test="createdBy != null">
        AND created_by = #{createdBy,jdbcType=VARCHAR}
      </if>
      <if test="createdTime != null">
        AND created_time = #{createdTime,jdbcType=TIMESTAMP}
      </if>
      <if test="updatedBy != null">
        AND updated_by = #{updatedBy,jdbcType=VARCHAR}
      </if>
      <if test="updatedTime != null">
        AND updated_time = #{updatedTime,jdbcType=TIMESTAMP}
      </if>
      <if test="appId != null">
        AND app_id = #{appId,jdbcType=VARCHAR}
      </if>
      <if test="appPackage != null">
        AND app_package = #{appPackage,jdbcType=VARCHAR}
      </if>
      <if test="platform != null">
        AND platform=#{platform,jdbcType=VARCHAR}
      </if>
    </trim>
  </select>
  <select id="countScriptByScriptName"  resultType="integer">
    select count(id) from
    <include refid="Table_Name"/>
    where is_deleted = '0' and script_name = #{scriptName,jdbcType=VARCHAR} and project_id = #{projectId,jdbcType=VARCHAR}
  </select>
  <select id="queryScriptList" parameterType="net.northking.cctp.common.http.QueryByPage" resultMap="DetailMap">
    select
    a.id
    ,a.group_id
    ,a.script_name
    ,a.script_type
    ,a.script_code
    ,a.latest_version_id
    ,a.script_url
    ,a.data_url
    ,a.is_deleted
    ,a.project_id
    ,a.tenant_id
    ,a.created_by
    ,a.created_time
    ,a.updated_by
    ,a.updated_time
    ,a.app_id
    ,a.app_package
    ,b.version_name
    ,a.platform
    ,a.principal_id
    ,c.name_path
    from ${defaultSchema}.atu_script_info a
    left join ${defaultSchema}.atu_script_version b
    on (a.id = b.script_id and a.latest_version_id = b.version_id)
    left join atu_script_group c
    on a.group_id = c.id
    <trim prefix="WHERE" prefixOverrides="AND" >
      <if test="query.projectIds != null and query.projectIds.size > 0">
        AND a.project_id IN
        <foreach collection="query.projectIds" open="(" index="index" item="item" close=")" separator=",">
          #{item,jdbcType=VARCHAR}
        </foreach>
      </if>
      <if test="query.groupIds != null and query.groupIds.size > 0">
        AND a.group_id IN
        <foreach collection="query.groupIds" open="(" index="index" item="item" close=")" separator=",">
          #{item,jdbcType=VARCHAR}
        </foreach>
      </if>
      <if test="query.keyword != null and query.keyword != ''">
        AND (LOCATE(#{query.keyword,jdbcType=VARCHAR},a.script_name) &gt; 0 or LOCATE(#{query.keyword},a.script_code) > 0)
      </if>
      <if test="query.scriptName != null and query.scriptName != ''">
        AND LOCATE(#{query.scriptName,jdbcType=VARCHAR},a.script_name) &gt; 0
      </if>
      <if test="query.creatorIds != null and query.creatorIds.size > 0">
        AND a.created_by IN
        <foreach collection="query.creatorIds" open="(" index="index" item="item" close=")" separator=",">
          #{item,jdbcType=VARCHAR}
        </foreach>
      </if>
      <if test="query.creatorId != null and query.creatorId != ''">
        AND a.created_by = #{query.creatorId}
      </if>
      <if test="query.isDeleted != null and query.isDeleted != ''">
        AND a.is_deleted = #{query.isDeleted,jdbcType=VARCHAR}
      </if>
      <if test="query.scriptType != null and query.scriptType != ''">
        AND LOCATE(a.script_type,#{query.scriptType,jdbcType=VARCHAR}) &gt; 0
      </if>
      <if test="query.groupId != null and query.groupId != ''">
        AND a.group_id = #{query.groupId,jdbcType=VARCHAR}
      </if>
      <if test="query.principalIds != null and query.principalIds.size > 0">
        AND a.principal_id IN
        <foreach collection="query.principalIds" open="(" index="index" item="item" close=")" separator=",">
          #{item,jdbcType=VARCHAR}
        </foreach>
      </if>
      <if test="query.principalId != null and query.principalId != '' ">
        AND a.principal_id =#{query.principalId}
      </if>
      <if test="query.updatedBys != null and query.updatedBys.size > 0">
        AND a.updated_by IN
        <foreach collection="query.updatedBys" open="(" index="index" item="item" close=")" separator=",">
          #{item,jdbcType=VARCHAR}
        </foreach>
      </if>
      <if test="query.updateBy != null and query.updateBy != ''">
        AND a.updated_by =#{query.updateBy}
      </if>

      <if test="query.startCreatedTime!=null">
        and a.created_time <![CDATA[>=]]> #{query.startCreatedTime}
      </if>
      <if test="query.endCreatedTime!=null">
        and a.created_time <![CDATA[<=]]> #{query.endCreatedTime}
      </if>
      <if test="query.startUpdatedTime!=null">
        and a.updated_time <![CDATA[>=]]> #{query.startUpdatedTime}
      </if>
      <if test="query.endUpdatedTime!=null">
        and a.updated_time <![CDATA[<=]]> #{query.endUpdatedTime}
      </if>
      <if test="query.scriptCode!=null and query.scriptCode!=''">
        and LOCATE(#{query.scriptCode},a.script_code) > 0
      </if>
      <if test="query.tenantId != null">
        AND a.tenant_id = #{query.tenantId,jdbcType=VARCHAR}
      </if>
    </trim>
    order by a.created_time desc
  </select>
  <update id="deleteScriptList" parameterType="net.northking.cctp.scriptcase.db.entity.AtuScriptInfo">
    update
    <include refid="Table_Name"/>
    <set>
      is_deleted = '1'
      <if test="script.updatedBy != null and script.updatedBy != ''">
        , updated_by = #{script.updatedBy}
      </if>
      <if test="script.updatedTime != null">
        , updated_time = #{script.updatedTime, jdbcType=TIMESTAMP}
      </if>
    </set>
    where id IN
    <foreach collection="list" open="(" index="index" item="item" close=")" separator=",">
      #{item,jdbcType=VARCHAR}
    </foreach>
  </update>
  <select id="findScriptCopyNum" resultMap="BaseResultMap">
    select
    <include refid="Base_Column_List"/>
    from
    <include refid="Table_Name"/>
    where project_id = #{projectId,jdbcType=VARCHAR} and script_name REGEXP #{scriptName,jdbcType=VARCHAR}
  </select>
  <select id="queryScriptByIds" resultType="net.northking.cctp.scriptcase.db.entity.AtuScriptInfo">
    select
    <include refid="Base_Column_List"/>
    from
    <include refid="Table_Name"/>
    where is_deleted = '0' and id IN
    <foreach collection="list" open="(" index="index" item="item" close=")" separator=",">
      #{item,jdbcType=VARCHAR}
    </foreach>
  </select>

  <select id="queryAppByScript" resultMap="BaseResultMap" parameterType="list">
    select
    app_package,platform
    from
    <include refid="Table_Name"/>
    where
    script_type in ('3','4')
    <if test="scriptIds!=null and scriptIds.size>0">
      and id IN
      <foreach collection="scriptIds" open="(" index="index" item="item" close=")" separator=",">
        #{item,jdbcType=VARCHAR}
      </foreach>
    </if>

    group by app_package,platform
    order by created_time
  </select>


  <select id="queryScriptInfoByCase" resultMap="BaseResultMap" parameterType="string">
    SELECT info.*
    FROM atu_script_info info
    LEFT JOIN atu_case_script_link link ON info.id = link.script_id
    WHERE link.id = #{caseId,jdbcType=VARCHAR}
  </select>

  <select id="getPublishScriptList" resultMap="DetailMap" parameterType="net.northking.cctp.scriptcase.dto.atuScript.atuScriptGroup.PublishScriptQueryDto">
    select
    a.id
    ,a.group_id
    ,a.script_name
    ,a.script_type
    ,a.latest_version_id
    ,a.script_url
    ,a.data_url
    ,a.is_deleted
    ,a.project_id
    ,a.tenant_id
    ,a.created_by
    ,a.created_time
    ,a.updated_by
    ,a.updated_time
    ,a.app_id
    ,a.app_package
    ,a.platform
    from ${defaultSchema}.atu_script_info a,${defaultSchema}.atu_script_group b
    where a.group_id = b.id and a.is_deleted = '0' and a.latest_version_id > 0
    <if test="scriptTypeList != null and scriptTypeList.size > 0">
      AND a.script_type IN
      <foreach collection="scriptTypeList" open="(" index="index" item="item" close=")" separator=",">
        #{item,jdbcType=VARCHAR}
      </foreach>
    </if>
    <if test="treeName != null and treeName != ''">
      AND LOCATE(#{treeName,jdbcType=VARCHAR},a.script_name) > 0
    </if>
    <if test="scriptName != null and scriptName != ''">
      AND LOCATE(#{scriptName,jdbcType=VARCHAR},a.script_name) &gt; 0
    </if>
    <if test="groupId != null and groupId != ''">
      AND a.group_id = #{groupId,jdbcType=VARCHAR}
    </if>
    <if test="projectId != null and projectId != ''">
      AND a.project_id = #{projectId,jdbcType=VARCHAR}
    </if>
    order by a.created_time desc
  </select>

  <select id="queryScriptDetailList" resultMap="DetailMap">
    select
    a.id
    ,a.script_name
    ,a.script_type
    ,a.latest_version_id
    ,b.url script_url
    ,a.app_package
    ,b.version_name
    ,a.platform
    from ${defaultSchema}.atu_script_info a
    left join ${defaultSchema}.atu_script_version b
    on (a.id = b.script_id and a.latest_version_id = b.version_id)
    WHERE id IN
    <foreach collection="scriptIdSet" item="scriptId" open="(" separator="," close=")">
      #{scriptId}
    </foreach>
    UNION ALL
    SELECT s.id, s.scene_name script_name, '5' script_type, v.version_id latest_version_id,
    v.url script_url, '' app_package, v.version_name, '' platform
    FROM ${defaultSchema}.atu_scene_info s
    LEFT JOIN ${defaultSchema}.atu_script_version v
    ON (v.script_id = s.id AND v.version_id = s.current_version_id)
    WHERE id IN
    <foreach collection="scriptIdSet" item="scriptId" open="(" separator="," close=")">
      #{scriptId}
    </foreach>
  </select>

    <select id="queryScriptPlatforms" resultType="java.lang.String">
      SELECT DISTINCT platform from atu_script_info where id in
      <foreach collection="scriptIdSet" item="scriptId" open="(" separator="," close=")">
        #{scriptId}
      </foreach>
      and platform is not NULL
    </select>
    <select id="selectManagerByProject" resultType="java.lang.String">
      SELECT principal_id FROM atu_script_info
      WHERE is_deleted = 0 and project_id = #{projectId}
      GROUP BY principal_id
      UNION
      SELECT principal_id FROM atu_scene_info
      WHERE is_deleted = 0 and project_id =#{projectId}
      GROUP BY principal_id
    </select>
    <select id="selectCreatorByProject" resultType="java.lang.String">
      SELECT created_by FROM atu_script_info
      WHERE is_deleted = 0 and project_id = #{projectId}
      GROUP BY created_by
      UNION
      SELECT principal_id FROM atu_scene_info
      WHERE is_deleted = 0 and project_id = #{projectId}
      GROUP BY created_by
    </select>
    <select id="selectUpdaterByProject" resultType="java.lang.String">
      SELECT updated_by FROM atu_script_info
      WHERE is_deleted = 0 and project_id = #{projectId}
      GROUP BY updated_by
      UNION
      SELECT updated_by FROM atu_scene_info
      WHERE is_deleted = 0 and project_id = #{projectId}
      GROUP BY updated_by
    </select>
  <select id="queryScriptVOList"
          resultType="com.hzbank.testteam.autotest.dependencies.baseDefineDependency.vo.ApiScriptOptionVO">
    select
      script.script_name as  label, script.id as value,script.is_deleted as disabled,script.script_code as scriptNo,
      script.script_type as  scriptType,script.project_id as systemNo,script.project_id as systemName,script.principal_id as managerId,
      script.principal_id as managerName
    FROM atu_script_info script
    where id in
    <foreach collection="scriptIdList" item="scriptId" open="(" separator="," close=")">
      #{scriptId}
    </foreach>
    UNION
    SELECT
      scene.scene_name as  label, scene.id as value,scene.is_deleted as disabled,scene.script_code as scriptNo,
      '-1' as  scriptType,scene.project_id as systemNo,scene.project_id as systemName,scene.principal_id as managerId,
      scene.principal_id as managerName
    FROM atu_scene_info scene
    where id in
    <foreach collection="scriptIdList" item="scriptId" open="(" separator="," close=")">
      #{scriptId}
    </foreach>

  </select>
  <select id="queryScriptVOByCodeAndSystemId"
          resultType="com.hzbank.testteam.autotest.dependencies.baseDefineDependency.vo.ApiScriptOptionVO">

    select
    script.script_name as  label, script.id as value,script.is_deleted as disabled,script.script_code as scriptNo,
    script.script_type as  scriptType,script.project_id as systemId,script.project_id as systemName,script.principal_id as managerId,
    script.principal_id as managerName
    FROM atu_script_info script
    where  is_deleted = '0'
    <if test="scriptCode != null and scriptCode != ''">
      AND  script.script_code =  #{scriptCode,jdbcType=VARCHAR}
    </if>
    <if test="systemId != null and systemId != ''">
      AND script.project_id = #{systemId,jdbcType=VARCHAR}
    </if>
    UNION
    select
      scene.scene_name as  label, scene.id as value,scene.is_deleted as disabled,scene.script_code as scriptNo,
      '-1' as  scriptType,scene.project_id as systemNo,scene.project_id as systemId,scene.principal_id as managerId,
      scene.principal_id as managerName
    FROM atu_scene_info scene
    where  is_deleted = '0'
    <if test="scriptCode != null and scriptCode != ''">
      AND  scene.script_code =  #{scriptCode,jdbcType=VARCHAR}
    </if>
    <if test="systemId != null and systemId != ''">
      AND scene.project_id = #{systemId,jdbcType=VARCHAR}
    </if>
  </select>

  <update id="updatePrincipal" parameterType="net.northking.cctp.scriptcase.dto.atuScript.atuScriptInfo.PrincipalUpdateDto">
    update
    <include refid="Table_Name"/>
      <set>
        principal_id = #{principalId, jdbcType=VARCHAR}
        <if test="updatedBy != null">
          , updated_by = #{updatedBy}
        </if>
        <if test="updatedTime != null">
          , updated_time = #{updatedTime,jdbcType=TIMESTAMP}
        </if>
      </set>
    where id in
    <foreach collection="scriptList" item="idItem" open="(" separator="," close=")">
      #{idItem, jdbcType=VARCHAR}
    </foreach>
  </update>
</mapper>