<?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.DocumentDiscussionMapper">
    <resultMap type="com.salpa.subject.domain.MonitorDocumentDiscussion" id="MonitorDocumentDiscussionResult" >
        <id property="id" column="id" />
        <id property="userId" column="user_id" />
        <id property="projectId" column="project_id" />
        <result property="content" column="content" />
        <result property="tableName" column="table_name" />
        <result property="columnName" column="column_name" />
        <result property="createAt" column="create_at" />
        <result property="userName" column="user_name" />
        <result property="avatar" column="avatar" />
    </resultMap>

    <select id="getList" resultMap="MonitorDocumentDiscussionResult">
        SELECT m.id,m.content,m.user_id,m.project_id,m.table_name,m.column_name,m.create_at,
               u.user_name,u.avatar FROM monitor_document_discussion m LEFT JOIN sys_user u ON m.user_id = u.user_id WHERE project_id=#{projectId} and table_name=#{tableName}
        <if test="columnName !=null and columnName != ''">
            AND column_name = #{columnName}
        </if>
        ORDER BY create_at desc
    </select>
    <insert id="add" parameterType="MonitorDocumentDiscussion" useGeneratedKeys="true" keyProperty="id" >
        insert into monitor_document_discussion(
        <if test="id != null and id != 0">id,</if>
        <if test="content != null and content != ''">content,</if>
        <if test="userId != null and userId != ''">user_id,</if>
        <if test="projectId != null and projectId != ''">project_id,</if>
        <if test="tableName != null and tableName != ''">table_name,</if>
        <if test="columnName != null and columnName != ''">column_name,</if>
        create_at
        )values(
        <if test="content != null and content != ''">#{content},</if>
        <if test="userId != null and userId != ''">#{userId},</if>
        <if test="projectId != null and projectId != ''">#{projectId},</if>
        <if test="tableName != null and tableName != ''">#{tableName},</if>
        <if test="columnName != null and columnName != ''">#{columnName},</if>
        sysdate()
        )

    </insert>

    <select id="countDiscussionByTableName" resultType="map">
        SELECT column_name columnName,COUNT(*) count FROM monitor_document_discussion WHERE project_id = #{projectId} AND table_name = #{name} AND column_name != null GROUP BY column_name
    </select>

    <select id="discussionByProjectId" resultType="map">
        SELECT table_name tableName,COUNT(*) count FROM monitor_document_discussion WHERE project_id = #{projectId} and column_name is NULL GROUP BY table_name
    </select>

    <delete id="deleteComment">
        DELETE FROM monitor_document_discussion WHERE id = #{id}
    </delete>
</mapper>
