<?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">
<mapper namespace="com.engine.hrmelog.mapper.ElogTableCheckerMapper">
    <insert id="recordVersion">
        insert into hrsa_elog_version (id, maintable, version) values (#{id},#{mainTable},#{version})
    </insert>
    <select id="getVersion" resultType="java.lang.Long">
        select version from  hrsa_elog_version where maintable = #{mainTable}
    </select>
    <update id="createMainTable">
        create table ${mainTable}
        (
            id              bigint comment 'ID',
            create_time     datetime default current_timestamp ,
            update_time     datetime default current_timestamp ,
            creator         bigint,
            delete_type     int,
            tenant_key      varchar(10),
            uuid            char(36),
            log_date        datetime default current_timestamp ,
            log_operator    varchar(50),
            operatorName    varchar(50),
            targetId        varchar(50),
            targetName      text,
            modulename      varchar(100),
            functionname    varchar(100),
            interfaceName   varchar(100),
            requesturl      varchar(200),
            requesturi      varchar(200),
            operateType     varchar(50),
            operatetypename varchar(100),
            operatedesc     varchar(1000),
            params          longtext,
            belongmainid    varchar(36),
            clientIp        varchar(50),
            groupid         varchar(50),
            device          varchar(200),
            groupNameLabel  varchar(500),
            redoService     varchar(200),
            redoContext     longtext,
            cancelService   varchar(200),
            cancelContext   longtext,
            totalruntime    bigint,
            mainruntime     bigint,
            log_result      varchar(100),
            fromterminal    varchar(100),
            resultdesc      text,
            old_content     varchar(1000),
            link_type       varchar(20),
            link_id         bigint,
            old_link_id     bigint,
            PRIMARY KEY (id)
        )
    </update>
    <update id="createMainTable" databaseId="oracle">

        create table ${mainTable}
        (
            id              number(*,0) not null primary key,
            create_time     date default sysdate,
            update_time     date default sysdate,
            creator         number(*,0),
            delete_type     number(*,0),
            tenant_key      varchar2(10),
            uuid            varchar2(36),
            log_date        date default sysdate,
            log_operator    varchar2(50),
            operatorName    varchar2(50),
            targetId        varchar2(50),
            targetName      varchar2(4000),
            modulename      varchar2(100),
            functionname    varchar2(100),
            interfaceName   varchar2(100),
            requesturl      varchar2(200),
            requesturi      varchar2(200),
            operateType     varchar2(50),
            operatetypename varchar2(100),
            operatedesc     varchar2(1000),
            params          clob,
            belongmainid    varchar2(36),
            clientIp        varchar2(200),
            groupid         varchar2(50),
            device          varchar2(200),
            groupNameLabel  varchar2(500),
            redoService     varchar2(200),
            redoContext     varchar2(4000),
            cancelService   varchar2(200),
            cancelContext   varchar2(4000),
            totalruntime    number(*,0),
            mainruntime     number(*,0),
            log_result      varchar2(4000),
            fromterminal    varchar2(100),
            resultdesc      varchar2(4000),
            old_content     varchar2(1000),
            link_type       varchar2(20),
            link_id         number(*,0),
            old_link_id     number(*,0)
        )
    </update>
    <update id="createMainTable" databaseId="sqlserver">
        create table ${mainTable}
        (
            id              bigint not null primary key,
            create_time     datetime default getdate(),
            update_time     datetime default getdate(),
            creator         bigint default '-1',
            delete_type     bigint    default 0,
            tenant_key      nvarchar(10),
            uuid            nvarchar(36),
            log_date        datetime default getdate(),
            device          nvarchar(500),
            log_operator    bigint default '-1',
            operatorname    nvarchar(100),
            targetid        bigint default '-1',
            targetname      nvarchar(3000),
            modulename      nvarchar(100),
            functionname    nvarchar(100),
            interfacename   nvarchar(100),
            requesturl      nvarchar(200),
            requesturi      nvarchar(200),
            operatetype     nvarchar(50),
            operatetypename nvarchar(100),
            operatedesc     nvarchar(3000),
            params          nvarchar(max),
            belongmainid    nvarchar(36),
            clientip        nvarchar(200),
            groupid         nvarchar(50),
            groupnamelabel  nvarchar(1000),
            redoservice     nvarchar(200),
            redocontext     nvarchar(3000),
            cancelservice   nvarchar(200),
            cancelcontext   nvarchar(3000),
            totalruntime    bigint default 0,
            mainruntime     bigint default 0,
            log_result      nvarchar(100),
            fromterminal    nvarchar(100),
            resultdesc      nvarchar(3000),
            old_content     nvarchar(3000),
            link_type       nvarchar(20),
            link_id         bigint default 0,
            old_link_id     bigint default 0
        )
    </update>
    <update id="createMainTable" databaseId="postgresql">
        create table ${mainTable}
        (
            id              int8 not null primary key,
            create_time     timestamp default current_timestamp,
            update_time     timestamp default current_timestamp,
            creator         int8,
            delete_type     int,
            tenant_key      varchar(10),
            uuid            varchar(36),
            log_date        timestamp default current_timestamp,
            log_operator    varchar(50),
            operatorName    varchar(50),
            targetId        varchar(50),
            targetName      text,
            modulename      varchar(100),
            functionname    varchar(100),
            interfaceName   varchar(100),
            requesturl      varchar(200),
            requesturi      varchar(200),
            operateType     varchar(50),
            operatetypename varchar(100),
            operatedesc     varchar(1000),
            params          text,
            belongmainid    varchar(36),
            clientIp        varchar(200),
            groupid         varchar(50),
            device          varchar(200),
            groupNameLabel  varchar(500),
            redoService     varchar(200),
            redoContext     text,
            cancelService   varchar(200),
            cancelContext   text,
            totalruntime    int4,
            mainruntime     int4,
            log_result      varchar(100),
            fromterminal    varchar(100),
            resultdesc      text,
            old_content     varchar(1000),
            link_type       varchar(20),
            link_id         int4,
            old_link_id     int4
        )
    </update>
    <update id="createDetailTable">
        create table ${detailTable}
        (
            id               bigint not null primary key,
            create_time      datetime default current_timestamp ,
            update_time      datetime default current_timestamp ,
            creator          bigint,
            delete_type      int,
            tenant_key       varchar(10),
            uuid             varchar(36),
            mainid           varchar(36),
            dataid           varchar(50),
            belongdataid     varchar(50),
            tableName        varchar(200),
            tablenamelabelid varchar(50),
            tablenamedesc    varchar(50),
            fieldName        varchar(200),
            fieldnamelabelid varchar(200),
            newValue         longtext,
            oldValue         longtext,
            newrealvalue     longtext,
            oldrealvalue     longtext,
            fieldDesc        varchar(200),
            showorder        int default 0,
            isdetail         int default 0
        )
    </update>
    <update id="createDetailTable" databaseId="postgresql">
        create table ${detailTable}
        (
            id               int8 not null primary key,
            create_time      timestamp default current_timestamp ,
            update_time      timestamp default current_timestamp ,
            creator          int8,
            delete_type      int8,
            tenant_key       varchar(10),
            uuid             varchar(36),
            mainid           varchar(36),
            dataid           varchar(50),
            belongdataid     varchar(50),
            tableName        varchar(200),
            tablenamelabelid varchar(50),
            tablenamedesc    varchar(50),
            fieldName        varchar(200),
            fieldnamelabelid varchar(200),
            newValue         text,
            oldValue         text,
            newrealvalue     text,
            oldrealvalue     text,
            fieldDesc        varchar(200),
            showorder        int8 default 0,
            isdetail         int8 default 0
        )
    </update>
    <update id="createDetailTable" databaseId="oracle">
        create table ${detailTable}
        (
            id               number(*,0) not null primary key,
            create_time      date default sysdate,
            update_time      date default sysdate,
            creator          number(*,0),
            delete_type      number(*,0),
            tenant_key       varchar2(10),
            uuid             varchar2(36),
            mainid           varchar2(36),
            dataid           varchar2(50),
            belongdataid     varchar2(50),
            tableName        varchar2(200),
            tablenamelabelid varchar2(50),
            tablenamedesc    varchar2(50),
            fieldName        varchar2(200),
            fieldnamelabelid varchar2(200),
            newValue         CLOB,
            oldValue         CLOB,
            newrealvalue     CLOB,
            oldrealvalue     CLOB,
            fieldDesc        varchar2(200),
            showorder        number(*,0) default 0,
            isdetail         number(*,0) default 0
        )
    </update>
    <update id="createDetailTable" databaseId="sqlserver">
        create table ${detailTable}
        (
            id bigint not null primary key,
            create_time     datetime default getdate(),
            update_time     datetime default getdate(),
            creator          bigint,
            delete_type      bigint,
            tenant_key       nvarchar(10),
            uuid             nvarchar(36),
            mainid           nvarchar(36),
            dataid           nvarchar(50),
            belongdataid     nvarchar(50),
            tableName        nvarchar(200),
            tablenamelabelid nvarchar(50),
            tablenamedesc    nvarchar(50),
            fieldName        nvarchar(200),
            fieldnamelabelid nvarchar(200),
            newValue         nvarchar(max),
            oldValue         nvarchar(max),
            newrealvalue     nvarchar(max),
            oldrealvalue     nvarchar(max),
            fieldDesc        varchar(200),
            showorder        bigint default 0,
            isdetail         bigint default 0
        )
    </update>


    <!-- 获取表结构 -->
    <select id="getTableStructure" resultType="com.engine.hrmelog.entity.dto.TableColumnBean" >
        SELECT
            COLUMN_NAME columnName,
            DATA_TYPE dataTypeStr,
            COLUMN_TYPE columnType,
            CHARACTER_MAXIMUM_LENGTH fieldLength,
            IS_NULLABLE isNullableStr,
            COLUMN_DEFAULT columnDefault,
            COLUMN_COMMENT columnComment
        FROM
            INFORMATION_SCHEMA.COLUMNS
        where
            table_name  = #{tableName}
          and TABLE_SCHEMA = (select database())
    </select>
    <select id="getTableStructure" resultType="com.engine.hrmelog.entity.dto.TableColumnBean" databaseId="postgresql">
        select *
        from pg_tables
        where tableowner = (select current_user)
          and tablename = #{tableName}
    </select>
    <select id="getTableStructure" resultType="com.engine.hrmelog.entity.dto.TableColumnBean" databaseId="oracle">
        SELECT
            COLUMN_NAME columnName
        FROM
            user_tab_columns
        where
            TABLE_NAME=UPPER(#{tableName})
    </select>
    <select id="getTableStructure" resultType="com.engine.hrmelog.entity.dto.TableColumnBean" databaseId="sqlserver">
        SELECT
            COLUMN_NAME columnName
        FROM
            INFORMATION_SCHEMA.COLUMNS
        where
            table_name  = #{tableName}
          and TABLE_catalog = (select DB_NAME())
    </select>

    <update id="createElogTable">
        ${createElogSql}
    </update>

    <!--查询表索引信息-->
    <select id="getTableIndex" resultType="java.util.Map">
        SELECT COLUMN_KEY
        FROM INFORMATION_SCHEMA.COLUMNS
        WHERE TABLE_NAME = #{tableName}
          AND COLUMN_NAME = #{columnName}
          AND TABLE_SCHEMA = (SELECT DATABASE()) LIMIT 1
    </select>
    <select id="getTableIndex" resultType="java.util.Map" databaseId="postgresql">
        SELECT COLUMN_KEY
        FROM INFORMATION_SCHEMA.COLUMNS
        WHERE TABLE_NAME = #{tableName}
          AND COLUMN_NAME = #{columnName}
          AND TABLE_SCHEMA = (SELECT current_database()) limit 1 offset 0
    </select>
    <select id="getTableIndex" resultType="java.util.Map" databaseId="oracle">
        SELECT *
        FROM user_ind_columns
        WHERE TABLE_NAME = UPPER(#{tableName})
          AND COLUMN_NAME = UPPER(#{columnName})

    </select>
    <select id="getTableIndex" resultType="java.util.Map" databaseId="sqlserver">
        SELECT a.name,
               c.name,
               d.name
        FROM sysindexes a
                 JOIN sysindexkeys b ON
                a.id = b.id
                AND a.indid = b.indid
                 JOIN sysobjects c ON
            b.id = c.id
                 JOIN syscolumns d ON
                b.id = d.id
                AND b.colid = d.colid
        WHERE a.indid NOT IN (0,
                              255)
          AND c.name = #{tableName}
          and d.name = #{columnName}

    </select>

    <!--查询表索引信息-->
    <select id="getDataBase" resultType="java.util.Map">
        SELECT DATABASE() as databasename
    </select>
    <select id="getDataBase" resultType="java.util.Map" databaseId="postgresql">
        SELECT current_database() as databasename
    </select>
    <select id="getDataBase" resultType="java.util.Map" databaseId="oracle">
        SELECT user as databasename
    </select>
    <select id="getDataBase" resultType="java.util.Map" databaseId="sqlserver">
        SELECT db_name() as databasename
    </select>

    <update id="createTableIndex">
        create index idx${id} on ${tableName} (${columnName})
    </update>

    <update id="createTableIndex" databaseId="oracle">
        create index idx${id} on ${tableName} (${columnName})
    </update>

    <update id="createTableIndex" databaseId="sqlserver">
        create index idx${id} on ${tableName} (${columnName})
    </update>

    <select id="getAllExistTables" resultType="java.lang.String">
        SELECT TABLE_NAME
        FROM INFORMATION_SCHEMA.TABLES
        WHERE TABLE_SCHEMA = (SELECT DATABASE())
        AND TABLE_NAME IN
        <foreach collection="tableNames" item="tableName" open="(" separator="," close=")">
            #{tableName}
        </foreach>
    </select>

    <select id="getAllExistTables" resultType="java.lang.String" databaseId="oracle">
        SELECT
        TABLE_NAME
        FROM
        USER_TABLES
        WHERE TABLE_NAME IN
        <foreach collection="tableNames" item="tableName" open="(" separator="," close=")">
            UPPER(#{tableName})
        </foreach>
    </select>

    <select id="getAllExistTables" resultType="java.lang.String" databaseId="sqlserver">
        SELECT table_name
        FROM INFORMATION_SCHEMA.TABLES
        WHERE TABLE_catalog = (select DB_NAME())
        AND table_name IN
        <foreach collection="tableNames" item="tableName" open="(" separator="," close=")">
            #{tableName}
        </foreach>
    </select>

    <select id="getAllExistTables" resultType="java.lang.String" databaseId="postgresql">
        SELECT tablename
        FROM pg_tables
        WHERE tableowner = (select current_user)
        AND tablename IN
        <foreach collection="tableNames" item="tableName" open="(" separator="," close=")">
            #{tableName}
        </foreach>
    </select>
</mapper>
