<?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-09-29 11:15:28  -->
<mapper namespace="net.northking.cctp.scriptcase.db.dao.MdBusComponentInfoDao">


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

  <resultMap id="DetailMap" type="net.northking.cctp.scriptcase.dto.component.MdBusComponentInfoDetailDto">
      <!-- 主键 -->
    <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="project_id" jdbcType="VARCHAR" property="projectId"/>
        <!-- 脚本内容 -->
    <result column="script_content" jdbcType="VARCHAR" property="scriptContent"/>
        <!-- 是否推荐使用0-推荐1-不推荐 -->
    <result column="is_deprecated" jdbcType="INTEGER" property="isDeprecated"/>
        <!-- 组件复杂度 -->
    <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="principal_id" jdbcType="VARCHAR" property="principalId"/>
    <result column="shared_flag" jdbcType="INTEGER" property="sharedFlag"/>
    </resultMap>

  <select id="pagingQuery" resultMap="DetailMap" parameterType="net.northking.cctp.scriptcase.dto.component.MdBusComponentInfoQueryDto">
    select
    md_bus_component_info.id
    ,md_bus_component_info.com_name
    ,md_bus_component_info.com_type
    ,md_bus_component_info.memo
    ,md_bus_component_info.project_id
    ,md_bus_component_info.script_content
    ,md_bus_component_info.is_deprecated
    ,md_bus_component_info.complexity
    ,md_bus_component_info.is_deleted
    ,md_bus_component_info.com_version
    ,md_bus_component_info.tree_id
    ,md_bus_component_info.created_by
    ,md_bus_component_info.created_time
    ,md_bus_component_info.updated_by
    ,md_bus_component_info.updated_time
    ,md_bus_component_info.tenant_id
    ,md_bus_component_info.principal_id
    ,md_bus_component_info.shared_flag
    ,library_bus_com_info.created_time shared_time
    ,library_bus_com_info.created_by sharedBy
    from
    <include refid="Table_Name"/>
    LEFT JOIN library_bus_com_info ON library_bus_com_info.id = md_bus_component_info.id
    <trim prefix="WHERE" prefixOverrides="AND" >
      <if test="comName != null">
        AND LOCATE(#{comName},md_bus_component_info.com_name)>0
      </if>
      <if test="comType != null">
        AND md_bus_component_info.com_type = #{comType,jdbcType=INTEGER}
      </if>
      <if test="projectId != null">
        AND md_bus_component_info.project_id = #{projectId,jdbcType=VARCHAR}
      </if>
      <if test="tenantId != null">
        AND md_bus_component_info.tenant_id = #{tenantId,jdbcType=VARCHAR}
      </if>
      <if test="sharedFlag != null">
        AND md_bus_component_info.shared_flag = #{sharedFlag,jdbcType=VARCHAR}
      </if>
      <if test="userIds != null and userIds.size > 0">
        AND md_bus_component_info.created_by IN
        <foreach collection="userIds" item="userId" open="(" separator="," close=")">
          #{userId, jdbcType=VARCHAR}
        </foreach>
      </if>

      <if test="creatorId != null and creatorId !=''">
        AND md_bus_component_info.created_by =#{creatorId}
      </if>

      <if test="projectList != null and projectList.size > 0">
        AND md_bus_component_info.project_id IN
        <foreach collection="projectList" item="projectId" open="(" separator="," close=")">
          #{projectId, jdbcType=VARCHAR}
        </foreach>
      </if>
      <if test="treeIds != null and treeIds.size > 0">
        AND md_bus_component_info.tree_id IN
        <foreach collection="treeIds" item="treeId" open="(" separator="," close=")">
          #{treeId, jdbcType=VARCHAR}
        </foreach>
      </if>
      <if test="startCreatedTime!=null">
        and md_bus_component_info.created_time <![CDATA[>=]]> #{startCreatedTime}
      </if>
      <if test="endCreatedTime!=null">
        and md_bus_component_info.created_time <![CDATA[<=]]> #{endCreatedTime}
      </if>
      <if test="startUpdatedTime!=null">
        and md_bus_component_info.updated_time <![CDATA[>=]]> #{startUpdatedTime}
      </if>
      <if test="endUpdatedTime!=null">
        and md_bus_component_info.updated_time <![CDATA[<=]]> #{endUpdatedTime}
      </if>
      <if test="principalIds != null and principalIds.size > 0">
        AND md_bus_component_info.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 md_bus_component_info.principal_id =#{principalId}
      </if>

      <if test="updatedBys != null and updatedBys.size > 0">
        AND md_bus_component_info.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 md_bus_component_info.updated_by =#{updateBy}
      </if>

      and md_bus_component_info.is_deleted = 0
    </trim>
  </select>

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

  <select id="deleteByIdList" parameterType="java.util.List" resultType="java.lang.String">
    SET @upid := 0;
    update
    <include refid="Table_Name"/>
    set is_deleted = 1
    where is_deleted = 0
    and id in
    <foreach collection="list" item="idItem" open="(" separator="," close=")">
      #{idItem, jdbcType=VARCHAR}
    </foreach>
    AND (SELECT @upid := CONCAT_WS(',',id,@upid)) IS NOT null;
    SELECT @upid;
  </select>

  <select id="getMaxCopyComName" resultType="java.lang.String">
    select com_name
    from <include refid="Table_Name"/>
    where tenant_id = #{tenantId,jdbcType=VARCHAR}
    AND project_id = #{projectId,jdbcType=VARCHAR}
    AND tree_id=#{treeId,jdbcType=VARCHAR}
    AND is_deleted = 0
    AND com_name REGEXP #{comName,jdbcType=VARCHAR}
    order by LENGTH(com_name) DESC,com_name desc
    limit 0,1
  </select>

  <update id="updateInfoByIds" parameterType="net.northking.cctp.scriptcase.db.entity.MdBusComponentInfo">
    update
    <include refid="Table_Name"/>
    <set>
      updated_time = #{info.updatedTime, jdbcType=TIMESTAMP}
      <if test="info.principalId != null">
        , principal_id = #{info.principalId}
      </if>
      <if test="info.isDeleted != null">
        , is_deleted = #{info.is_deleted}
      </if>
      <if test="info.sharedFlag != null">
        , shared_flag = #{info.sharedFlag}
      </if>
      <if test="info.updatedBy != null">
        , updated_by = #{info.updatedBy}
      </if>
    </set>
    where tenant_id = #{info.tenantId,jdbcType=VARCHAR}
    and id in
    <foreach collection="ids" item="id" open="(" separator="," close=")">
      #{id, jdbcType=VARCHAR}
    </foreach>
    and is_deleted = 0
  </update>

  <select id="queryComListByType" resultType="net.northking.cctp.scriptcase.dto.component.MdBusComponentInfoDetailDto">
    select
    info.id
    ,com_name
    ,com_type
    ,memo
    ,project_id
    ,script_content
    ,is_deprecated
    ,complexity
    ,is_deleted
    ,com_version
    ,tree_id
    ,param.param_json
    ,param.com_json
    FROM md_bus_component_info info
    LEFT JOIN md_component_param param ON info.id=param.id
    WHERE is_deleted=0
    AND info.project_id = #{com.projectId,jdbcType=VARCHAR}
    AND 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="queryComInfoByIdList" resultType="net.northking.cctp.scriptcase.dto.component.MdBusComponentInfoDetailDto">
    select
    info.id
    ,com_name
    ,com_type
    ,memo
    ,project_id
    ,script_content
    ,is_deprecated
    ,com_version
    ,tree_id
    ,param.param_json
    ,param.com_json
    FROM md_bus_component_info info
    LEFT JOIN md_component_param param ON info.id=param.id
    WHERE is_deleted=0
    AND info.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 md_bus_component_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 md_bus_component_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 md_bus_component_info
    WHERE is_deleted = 0 and project_id =  #{projectId}
    GROUP BY updated_by
  </select>
</mapper>