<?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-29 10:19:09  -->
<mapper namespace="net.northking.cctp.scriptcase.db.dao.AtuSceneInfoDao">

  <select id="findByPkScriptId" resultMap="BaseResultMap">
    select
    <include refid="Base_Column_List"/>
    from
    <include refid="Table_Name"/>
    <trim prefix="WHERE" prefixOverrides="AND" >
      AND id = #{0}
    </trim>
  </select>

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

  <resultMap id="DetailMap" type="net.northking.cctp.scriptcase.dto.sceneScript.AtuSceneInfoDetailDto">
      <!-- 场景脚本ID -->
    <id column="id" jdbcType="VARCHAR" property="id"/>
        <!-- 场景脚本名称 -->
    <result column="scene_name" jdbcType="VARCHAR" property="sceneName"/>
        <!-- 最新版本ID -->
    <result column="current_version_id" jdbcType="INTEGER" property="currentVersionId"/>
        <!-- 场景脚本地址 -->
    <result column="script_url" jdbcType="VARCHAR" property="scriptUrl"/>
        <!-- 场景脚本输入项数据存储的地址 -->
    <result column="data_url" jdbcType="VARCHAR" property="dataUrl"/>
        <!-- 系统ID -->
    <result column="project_id" jdbcType="VARCHAR" property="projectId"/>
        <!-- 租户号 -->
    <result column="tenant_id" jdbcType="VARCHAR" property="tenantId"/>
        <!-- 创建人Id -->
    <result column="created_by" jdbcType="VARCHAR" property="createdBy"/>
        <!-- 创建人名 -->
    <result column="created_Name" jdbcType="VARCHAR" property="createdName"/>
        <!-- 创建时间 -->
    <result column="created_time" jdbcType="TIMESTAMP" property="createdTime"/>
        <!-- 更新人 -->
    <result column="updated_by" jdbcType="VARCHAR" property="updatedBy"/>
        <!-- 更新人 -->
    <result column="updated_name" jdbcType="VARCHAR" property="updatedName"/>
        <!-- 更新时间 -->
    <result column="updated_time" jdbcType="TIMESTAMP" property="updatedTime"/>
        <!-- 是否删除 -->
    <result column="is_deleted" jdbcType="INTEGER" property="isDeleted"/>

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

    <result column="script_code" jdbcType="VARCHAR" property="scriptCode"/>

    <result column="version_name" jdbcType="VARCHAR" property="versionName"/>
    </resultMap>

  <select id="pagingQuery" resultMap="DetailMap" parameterType="net.northking.cctp.scriptcase.db.entity.AtuSceneInfo">
    select
    id
    ,scene_name
    ,script_code
    ,current_version_id
    ,script_url
    ,data_url
    ,project_id
    ,tenant_id
    ,created_by,
    '' as created_name,
    ,created_time
    ,updated_by
    ,updated_time
    ,is_deleted
    from
    <include refid="Table_Name"/>
    <trim prefix="WHERE" prefixOverrides="AND" >
      <if test="id != null">
        AND id = #{id,jdbcType=VARCHAR}
      </if>
      <if test="sceneName != null">
        AND scene_name = #{sceneName,jdbcType=VARCHAR}
      </if>
      <if test="currentVersionId != null">
        AND current_version_id = #{currentVersionId,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="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="isDeleted != null">
        AND is_deleted = #{isDeleted,jdbcType=INTEGER}
      </if>
    </trim>
  </select>

  <select id="queryList" resultMap="DetailMap" parameterType="net.northking.cctp.scriptcase.dto.sceneScript.AtuSceneScriptPagingQueryDto">
    select
    asi.id
    ,asi.scene_name
    ,asi.script_code
    ,asi.current_version_id
    ,asi.script_url
    ,asi.data_url
    ,asi.project_id
    ,asi.tenant_id
    ,asi.created_by
    ,'' as created_name
    ,asi.created_time
    ,asi.updated_by
    ,'' as updated_name
    ,asi.updated_time
    ,asi.is_deleted
    ,asi.principal_id
    ,asv.version_name
    from
    ${defaultSchema}.atu_scene_info asi left join
    ${defaultSchema}.atu_script_version asv
    on (asi.id = asv.script_id and asi.current_version_id = asv.version_id)
    <trim prefix="WHERE" prefixOverrides="AND" >
      <if test="tenantId != null and tenantId != '' ">
        AND asi.tenant_id = #{tenantId,jdbcType=VARCHAR}
      </if>
      <if test="projectIds != null and projectIds.size() > 0 ">
        and asi.project_id in
        <foreach collection="projectIds" open="(" item="item" index="index" separator="," close=")">
          #{item, jdbcType=VARCHAR}
        </foreach>
      </if>
      <if test="keyword != null and keyword != ''">
        AND (asi.scene_name like "%"#{keyword,jdbcType=VARCHAR}"%" or asi.script_code like "%"#{keyword,jdbcType=VARCHAR}"%")
      </if>
      <if test="sceneName != null and sceneName !=''">
        AND asi.scene_name like "%"#{sceneName,jdbcType=VARCHAR}"%"
      </if>
      <if test="createdByIds != null and createdByIds.size() > 0">
        and asi.created_by in
        <foreach collection="createdByIds" open="(" item="item" index="index" separator="," close=")">
          #{item, jdbcType=VARCHAR}
        </foreach>
      </if>
      <if test="creatorId != null and creatorId!=''">
        and asi.created_by =#{creatorId}
      </if>
      <if test="startCreatedTime!=null">
        and asi.created_time <![CDATA[>=]]> #{startCreatedTime}
      </if>
      <if test="endCreatedTime!=null">
        and asi.created_time <![CDATA[<=]]> #{endCreatedTime}
      </if>
      <if test="startUpdatedTime!=null">
        and asi.updated_time <![CDATA[>=]]> #{startUpdatedTime}
      </if>
      <if test="endUpdatedTime!=null">
        and asi.updated_time <![CDATA[<=]]> #{endUpdatedTime}
      </if>
      <if test="principalIds != null and principalIds.size > 0">
        AND asi.principal_id IN
        <foreach collection="principalIds" open="(" index="index" item="item" close=")" separator=",">
          #{item,jdbcType=VARCHAR}
        </foreach>
      </if>
      <if test="principalId != null and principalId !=''">
        AND asi.principal_id =#{principalId}
      </if>
      <if test="updatedBys != null and updatedBys.size > 0">
        AND asi.updated_by IN
        <foreach collection="updatedBys" open="(" index="index" item="item" close=")" separator=",">
          #{item,jdbcType=VARCHAR}
        </foreach>
      </if>
      <if test="updateBy != null and updateBy !=''">
        AND asi.updated_by =#{updateBy}
      </if>
        AND asi.is_deleted = 0
    </trim>
  </select>

  <select id="querySceneByList" resultMap="BaseResultMap" parameterType="list">
    select
    <include refid="Base_Column_List"/>
    from
    <include refid="Table_Name"/>
    where id in
    <foreach collection="ids" item="idItem" open="(" separator="," close=")">
      #{idItem, jdbcType=VARCHAR}
    </foreach>
  </select>
    <select id="selectManagerByProject" resultType="java.lang.String">
      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_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_scene_info
    WHERE is_deleted = 0 and project_id = #{projectId}
    GROUP BY updated_by
  </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>