<?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-12-06 10:53:48  -->
<mapper namespace="net.northking.cctp.scriptcase.db.dao.LibraryBusComInfoDao">


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

  <resultMap id="DetailMap" type="net.northking.cctp.scriptcase.dto.comLib.LibraryBusComInfoDetailDto">
      <!-- 主键 -->
    <id column="id" jdbcType="VARCHAR" property="id"/>
                                      <!-- 组件名称 -->
    <result column="com_name" jdbcType="VARCHAR" property="comName"/>
        <!-- 组件类型1-接口，2-pcbs，3-pccs，4-安卓，5-ios -->
    <result column="com_type" jdbcType="INTEGER" property="comType"/>
        <!-- 说明 -->
    <result column="memo" jdbcType="VARCHAR" property="memo"/>
        <!-- 脚本内容 -->
    <result column="script_content" jdbcType="VARCHAR" property="scriptContent"/>
        <!-- 组件复杂度 -->
    <result column="complexity" jdbcType="INTEGER" property="complexity"/>
        <!-- 逻辑删除0-可用1-已删除 -->
    <result column="is_deleted" jdbcType="INTEGER" property="isDeleted"/>
        <!-- 组件版本号 -->
    <result column="com_version" jdbcType="VARCHAR" property="comVersion"/>
        <!-- 目录树id -->
    <result column="tree_id" jdbcType="VARCHAR" property="treeId"/>
        <!-- 创建人 -->
    <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="tenant_id" jdbcType="VARCHAR" property="tenantId"/>
    <result column="lib_status" jdbcType="INTEGER" property="libStatus"/>
    <result column="project_id" jdbcType="VARCHAR" property="projectId"/>
    </resultMap>

  <select id="pagingQuery" resultMap="DetailMap" parameterType="net.northking.cctp.scriptcase.dto.comLib.LibraryBusComInfoQueryDto">
    select
    a.id
    ,a.com_name
    ,a.com_type
    ,a.memo
    ,a.project_id
    ,a.script_content
    ,a.is_deprecated
    ,a.script_content
    ,a.complexity
    ,a.is_deleted
    ,a.com_version
    ,b.tree_id
    ,b.created_by
    ,b.created_time
    ,a.updated_by
    ,a.updated_time
    ,a.tenant_id
    from
    <include refid="Table_Name"/> b
    left join md_bus_component_info a  on a.id=b.id
    <trim prefix="WHERE" prefixOverrides="AND" >
      <if test="comName != null">
        AND LOCATE(#{comName},a.com_name)>0
      </if>
      <if test="comType != null">
        AND a.com_type = #{comType,jdbcType=INTEGER}
      </if>
      <if test="tenantId != null">
        AND a.tenant_id = #{tenantId,jdbcType=INTEGER}
      </if>
      <if test="userIds != null and userIds.size > 0">
        AND b.created_by IN
        <foreach collection="userIds" item="userId" open="(" separator="," close=")">
          #{userId, jdbcType=VARCHAR}
        </foreach>
      </if>
      <if test="treeIds != null and treeIds.size > 0">
        AND b.tree_id IN
        <foreach collection="treeIds" item="treeId" open="(" separator="," close=")">
          #{treeId, jdbcType=VARCHAR}
        </foreach>
      </if>
      and a.is_deleted = 0
    </trim>
  </select>

  <select id="queryCountByTreeCom" resultType="java.lang.Integer" parameterType="net.northking.cctp.scriptcase.db.entity.LibraryBusComInfo">
    select count(*) from
    <include refid="Table_Name"/>
    where tenant_id = #{tenantId,jdbcType=VARCHAR}
    AND tree_id=#{treeId,jdbcType=VARCHAR}
    AND is_deleted = 0
  </select>

  <select id="checkRepeatName" resultType="java.lang.String">
    select com_name from <include refid="Table_Name"/>
    where tenant_id = #{lib.tenantId,jdbcType=VARCHAR}
    AND tree_id=#{lib.treeId,jdbcType=VARCHAR}
<!--    and project_id=#{lib.projectId,jdbcType=VARCHAR}-->
    <if test="nameList != null">
      AND com_name in
      <foreach collection="nameList" item="nameItem" open="(" separator="," close=")">
        #{nameItem, jdbcType=VARCHAR}
      </foreach>
    </if>
  </select>

  <select id="updateByIdList" parameterType="java.util.List" resultType="java.lang.String">
    SET @upid := 0;
    update
    <include refid="Table_Name"/>
    set is_deleted =  #{lib.isDeleted,jdbcType=VARCHAR}
    ,lib_status = 1
    ,updated_by =  #{lib.updatedBy,jdbcType=VARCHAR}
    ,updated_time =  #{lib.updatedTime,jdbcType=TIMESTAMP}
    where tenant_id = #{lib.tenantId,jdbcType=VARCHAR}
    <if test="lib.libStatus != null">
      AND lib_status = #{lib.libStatus,jdbcType=INTEGER}
    </if>
    AND is_deleted = 0
    and id in
    <foreach collection="ids" item="id" open="(" separator="," close=")">
      #{id, jdbcType=VARCHAR}
    </foreach>
    AND (SELECT @upid := CONCAT_WS(',',id,@upid)) IS NOT null;
    SELECT @upid;
  </select>

  <select id="queryComListByType" resultType="net.northking.cctp.scriptcase.dto.comLib.LibraryBusComInfoDetailDto">
    select
    info.id
    ,param.param_json
    ,param.com_json
    FROM library_bus_com_info info
    LEFT JOIN md_component_param param ON info.id=param.id
    WHERE info.tenant_id = #{com.tenantId,jdbcType=VARCHAR}
    AND com_type IN
    <foreach collection="types" item="idItem" open="(" separator="," close=")">
      #{idItem, jdbcType=VARCHAR}
    </foreach>
    <if test="com.treeId != null">
      AND tree_id = #{com.treeId,jdbcType=INTEGER}
    </if>
    <if test="com.comName != null">
      AND LOCATE(#{com.comName},com_name)>0
    </if>
    order by com_name
  </select>
    <select id="selectCreator" resultType="java.lang.String">
      SELECT created_by FROM library_bus_com_info
      WHERE is_deleted = 0 AND tenant_id = #{tenantId}
      GROUP BY created_by
    </select>
</mapper>