<?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-23 17:23:02  -->
<mapper namespace="net.northking.cctp.scriptcase.db.dao.AtuBusinessNoDao">


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

  <resultMap id="DetailMap" type="net.northking.cctp.scriptcase.dto.component.AtuBusinessNoDetailDto">
      <!-- 主键 -->
    <id column="id" jdbcType="VARCHAR" property="id"/>
                            <!-- 编号数字 -->
    <result column="no_value" jdbcType="BIGINT" property="noValue"/>
        <!-- 编号类型 -->
    <result column="no_type" jdbcType="VARCHAR" property="noType"/>
        <!-- 系统id -->
    <result column="project_id" jdbcType="VARCHAR" property="projectId"/>
        <!-- 创建人 -->
    <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"/>
    </resultMap>

  <select id="pagingQuery" resultMap="DetailMap" parameterType="net.northking.cctp.scriptcase.db.entity.AtuBusinessNo">
    select
    id
    ,no_value
    ,no_type
    ,project_id
    ,created_by
    ,created_time
    ,updated_by
    ,updated_time
    ,tenant_id
    from
    <include refid="Table_Name"/>
    <trim prefix="WHERE" prefixOverrides="AND" >
      <if test="id != null">
        AND id = #{id,jdbcType=VARCHAR}
      </if>
      <if test="noValue != null">
        AND no_value = #{noValue,jdbcType=BIGINT}
      </if>
      <if test="noType != null">
        AND no_type = #{noType,jdbcType=VARCHAR}
      </if>
      <if test="projectId != null">
        AND project_id = #{projectId,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="tenantId != null">
        AND tenant_id = #{tenantId,jdbcType=VARCHAR}
      </if>
    </trim>
  </select>

  <insert id="updateComTreeNo">
    INSERT INTO atu_business_no (id,no_value,no_type,tenant_id,project_id)
    SELECT re.id,re.no_value,re.no_type,re.tenant_id,re.project_id FROM
    (SELECT (case when abn.id IS NULL then tab.id ELSE abn.id END) AS id,order_seq AS no_value,"BUS_COMPONENT_TREE" as no_type,tab.tenant_id,tab.project_id
    FROM (SELECT REPLACE(MD5(UUID()),"-","") as id,tenant_id,project_id, MAX(order_seq) AS order_seq
    FROM bus_component_tree
    WHERE LENGTH(order_seq)>0
    GROUP BY tenant_id,project_id) tab
    LEFT JOIN
    atu_business_no abn
    ON tab.tenant_id = abn.tenant_id AND tab.project_id=abn.project_id AND abn.no_type = 'BUS_COMPONENT_TREE'
    WHERE   tab.order_seq>abn.no_value OR abn.no_value IS null)re
    ON DUPLICATE KEY UPDATE no_value=VALUES(no_value)
  </insert>
</mapper>