<?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.salpa.subject.mapper.DocumentMapper">

    <resultMap type="com.salpa.subject.domain.MonitorDatabaseDocument" id="MonitorDatabaseDocumentResult">
        <id     property="id"            column="id"             />
        <id     property="projectId"            column="project_id"             />
        <result property="databaseName"          column="database_name"           />
        <result property="schemaName"   column="schema_name"    />
        <result property="productName"              column="product_name"                />
        <result property="productVersion"             column="product_version"              />
        <result property="documentVersion"             column="version"              />
        <result property="archive"             column="is_archive"              />
        <result property="createAt"             column="create_at"              />
        <result property="updateAt"             column="update_at"              />
    </resultMap>

    <resultMap type="com.salpa.subject.domain.data.DatabaseDocumentResponse" id="DatabaseDocumentResponse">
        <id     property="id"            column="id"             />
        <result property="databaseName"          column="database_name"           />
        <result property="schemaName"   column="schema_name"    />
        <result property="productName"              column="product_name"                />
        <result property="productVersion"             column="product_version"              />
        <result property="documentVersion"             column="version"              />
        <result property="createAt"             column="create_at"              />
    </resultMap>

    <resultMap type="com.salpa.subject.domain.MonitorTableDocument" id="MonitorTableDocumentResult">
        <id     property="id"            column="id"             />
        <id     property="databaseDocumentId"            column="database_document_id"             />
        <result property="name"          column="name"           />
        <result property="type"   column="type"    />
        <result property="comment"              column="comment"                />
        <result property="createAt"             column="create_at"              />
    </resultMap>

    <resultMap type="com.salpa.subject.domain.data.TableDocumentResponse" id="TableDocumentResponse">
        <id     property="id"            column="id"             />
        <id     property="databaseDocumentId"            column="database_document_id"             />
        <result property="name"          column="name"           />
        <result property="type"   column="type"    />
        <result property="comment"              column="comment"                />
        <result property="createAt"             column="create_at"              />
    </resultMap>

    <resultMap type="com.salpa.subject.domain.MonitorProjectSyncTask" id="MonitorProjectSyncTaskResult">
        <id     property="id"            column="id"             />
        <id     property="projectId"            column="project_id"             />
        <result property="userId"          column="user_id"           />
        <result property="status"   column="status"    />
        <result property="result"              column="result"                />
        <result property="runAt"             column="run_at"              />
        <result property="updateAt"             column="update_at"              />
        <result property="createAt"             column="create_at"              />
    </resultMap>

    <select id="getVersionsByProjectId" resultType="map">
        SELECT id databaseDocumentId,version,create_at createAt
        FROM monitor_database_document WHERE project_id = #{projectId}
    </select>

    <select id="getMaxVersion" resultType="long">
        SELECT MAX(version) version FROM monitor_database_document WHERE project_id = #{projectId} AND is_archive = 0
    </select>

    <select id="getDocumentVersionById" resultType="long">
        SELECT version FROM monitor_database_document WHERE id = #{docId}
    </select>

    <select id="uniqueDocumentByProjectId" resultType="int">
        SELECT count(*) FROM monitor_database_document WHERE project_id = #{projectId}
    </select>

    <delete id="deleteDocumentById">
        DELETE FROM monitor_database_document WHERE id = #{docId}
    </delete>

    <select id="getSimpleByProjectId" resultMap="MonitorDatabaseDocumentResult">
        SELECT id,project_id,database_name,schema_name,product_name,product_version,version,is_archive,create_at,update_at
        FROM monitor_database_document WHERE project_id = #{projectId} AND version = #{version}
    </select>

    <select id="getByProjectId" resultMap="DatabaseDocumentResponse">
        SELECT id,database_name,schema_name,product_name,product_version,version,create_at
        FROM monitor_database_document WHERE project_id = #{projectId} AND version = #{version}
    </select>

    <select id="getTableDocument" resultMap="MonitorTableDocumentResult">
        SELECT id,database_document_id,name,type,comment,create_at
        FROM monitor_table_document WHERE database_document_id = #{id}
        <if test="search != null and search != ''">
            AND name like concat('%', #{search}, '%')
        </if>
    </select>

    <select id="getTableById" resultMap="MonitorTableDocumentResult">
        SELECT id,database_document_id,name,type,comment,create_at
        FROM monitor_table_document WHERE id = #{id}
    </select>

    <select id="getTableDocumentById" resultType="map">
        SELECT t.id,t.database_document_id databaseDocumentId,t.name,t.comment,d.content
        FROM monitor_table_document t LEFT JOIN monitor_document_description d ON t.name = d.table_name WHERE t.database_document_id = #{projectId}
        <if test="search != null and search != ''">
            AND t.name like concat('%', #{search}, '%') OR t.comment like concat('%', #{search}, '%') OR d.content like concat('%', #{search}, '%')
        </if>
    </select>

    <select id="getTable" resultType="map">
        SELECT name,comment,type,create_at
        FROM monitor_table_document WHERE id = #{id}
    </select>

    <select id="getTableByIds" resultMap="MonitorTableDocumentResult">
        SELECT id,database_document_id,name,type,comment,create_at
        FROM monitor_table_document WHERE 1=1
        AND id in
        <foreach collection="list" open="(" separator="," close=")" item="id">
            #{id}
        </foreach>
    </select>

    <select id="selectNotArchivedByProjectId" resultMap="MonitorDatabaseDocumentResult">
        SELECT id,project_id,database_name,schema_name,product_name,product_version,version,is_archive,create_at
        FROM monitor_database_document WHERE project_id = #{projectId} AND is_archive = 0
    </select>

    <select id="getTableDetails" resultMap="TableDocumentResponse">
        SELECT id,database_document_id,name,type,comment,create_at
        FROM monitor_table_document WHERE database_document_id = #{databaseDocumentId}
    </select>

    <select id="getSyncTaskList" resultMap="MonitorProjectSyncTaskResult">
        SELECT id,project_id,user_id,status,result,run_at,create_at
        FROM monitor_project_sync_task WHERE 1=1
        <if test="list != null and list.length != 0">
            AND id in
            <foreach collection="list" separator="," open="(" close=")" item="id">
                #{id}
            </foreach>
        </if>
    </select>

    <insert id="createSyncTask" parameterType="MonitorProjectSyncTask" useGeneratedKeys="true" keyProperty="id">
        insert into monitor_project_sync_task(
        <if test="projectId != null">project_id,</if>
        <if test="userId != null">user_id,</if>
        <if test="status != null and status != ''">status</if>
        )values(
        <if test="projectId != null">#{projectId},</if>
        <if test="userId != null">#{userId},</if>
        <if test="status != null and status != ''">#{status}</if>
        )
    </insert>

    <insert id="addDatabaseDocument" parameterType="MonitorDatabaseDocument" useGeneratedKeys="true" keyProperty="id">
        insert into monitor_database_document(
        <if test="projectId != null">project_id,</if>
        <if test="databaseName != null and databaseName != ''">database_name,</if>
        <if test="schemaName != null and schemaName != ''">`schema_name`,</if>
        <if test="productName != null and productName != ''">product_name,</if>
        <if test="productVersion != null and productVersion != ''">product_version,</if>
        <if test="documentVersion != null">version</if>
        )values(
        <if test="projectId != null">#{projectId},</if>
        <if test="databaseName != null and databaseName != ''">#{databaseName},</if>
        <if test="schemaName != null and schemaName != ''">#{schemaName},</if>
        <if test="productName != null and productName != ''">#{productName},</if>
        <if test="productVersion != null and productVersion != ''">#{productVersion},</if>
        <if test="documentVersion != null">#{documentVersion}</if>
        )
    </insert>

    <insert id="insertTableDocumentAndReturnId" parameterType="MonitorTableDocument" useGeneratedKeys="true" keyProperty="id">
        insert into monitor_table_document(
        <if test="databaseDocumentId != null">database_document_id,</if>
        <if test="name != null and name != ''">`name`,</if>
        <if test="comment != null and comment != ''">comment,</if>
        <if test="type != null and type != ''">`type`</if>
        )values(
        <if test="databaseDocumentId != null">#{databaseDocumentId},</if>
        <if test="name != null and name != ''">#{name},</if>
        <if test="comment != null and comment != ''">#{comment},</if>
        <if test="type != null and type != ''">#{type}</if>
        )
    </insert>

    <update id="updateStatusAndResultById">
        UPDATE monitor_project_sync_task SET status = "CANCELED",result = "主动取消"
        WHERE id = #{taskId}
    </update>

    <update id="updateIsArchiveById">
        UPDATE monitor_database_document SET is_archive = #{archive}
        WHERE id = #{id}
    </update>

    <update id="updateIsArchive">
        UPDATE monitor_database_document SET is_archive = 0
        WHERE project_id = #{projectId} AND version = #{version}
    </update>

    <update id="updateSyncTask">
        UPDATE monitor_project_sync_task SET run_at = sysdate() WHERE id = #{syncTask}
    </update>

    <select id="getSyncTaskStatus" resultType="string">
        SELECT status FROM monitor_project_sync_task WHERE id = #{taskId}
    </select>

    <select id="selectStatusById" resultType="string">
        SELECT status FROM monitor_project_sync_task WHERE id = #{taskId}
    </select>
</mapper>