<?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.DocumentDescriptionMapper">

    <update id="updateDescription" parameterType="com.salpa.subject.domain.MonitorDocumentDescription">
        UPDATE monitor_document_description
        <set>
            <if test="content != null and content != ''">content = #{content},</if>
            <if test="updateBy != null and updateBy != ''">update_by = #{updateBy}</if>
        </set>
        WHERE table_name = #{tableName} AND project_id = #{projectId}
        <if test="columnName != null and columnName != ''">
            AND column_name = #{columnName}
        </if>
        <if test="columnName == null or columnName == ''">
            AND column_name is NULL
        </if>
    </update>

    <select id="exists" resultType="int">
        SELECT COUNT(*) FROM monitor_document_description
        WHERE table_name = #{tableName} AND project_id = #{projectId}
        <if test="columnName != null and columnName != ''">
            AND column_name = #{columnName}
        </if>
        <if test="columnName == null or columnName == ''">
            AND column_name is NULL
        </if>
    </select>

    <insert id="addDescription">
        insert into monitor_document_description(
        <if test="projectId != null">project_id,</if>
        <if test="tableName != null and tableName != ''">table_name,</if>
        <if test="columnName != null and columnName != ''">column_name,</if>
        <if test="content != null and content != ''">content</if>
        )values(
        <if test="projectId != null">#{projectId},</if>
        <if test="tableName != null and tableName != ''">#{tableName},</if>
        <if test="columnName != null and columnName != ''">#{columnName},</if>
        <if test="content != null and content != ''">#{content}</if>
        )
    </insert>


    <select id="selectDescriptionByTableName" resultType="map">
        SELECT column_name columnName,content FROM monitor_document_description WHERE project_id = #{projectId} AND table_name = #{name} AND column_name != null
    </select>

    <select id="selectById" resultType="map">
        SELECT table_name tableName,content FROM monitor_document_description WHERE project_id = #{projectId} and column_name is NULL
    </select>

</mapper>
