<?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.microservices.pms.document.mapper.PmsDocumentUserFavoriteMapper">
    
    <resultMap type="PmsDocumentUserFavorite" id="PmsDocumentUserFavoriteResult">
        <result property="id"    column="id"    />
        <result property="userId"    column="user_id"    />
        <result property="pmsDocumentId"    column="pms_document_id"    />
        <result property="createTime"    column="create_time"    />
    </resultMap>

    <sql id="selectPmsDocumentUserFavoriteVo">
        select id, user_id, pms_document_id, create_time from pms_document_user_favorite
    </sql>

    <select id="selectPmsDocumentUserFavoriteList" parameterType="PmsDocumentUserFavorite" resultMap="PmsDocumentUserFavoriteResult">
        <include refid="selectPmsDocumentUserFavoriteVo"/>
        <where>  
            <if test="userId != null "> and user_id = #{userId}</if>
            <if test="pmsDocumentId != null "> and pms_document_id = #{pmsDocumentId}</if>
        </where>
    </select>
    <select id="isDocumentInFavorites" resultType="java.lang.Boolean">
        SELECT COUNT(*) > 0 AS hasFavorited
        FROM pms_document_user_favorite
        WHERE user_id = #{userId} AND pms_document_id = #{documentId}
    </select>

    <insert id="insertPmsDocumentUserFavorite" parameterType="PmsDocumentUserFavorite" useGeneratedKeys="true" keyProperty="id">
        insert into pms_document_user_favorite
        <trim prefix="(" suffix=")" suffixOverrides=",">
            <if test="userId != null">user_id,</if>
            <if test="pmsDocumentId != null">pms_document_id,</if>
            <if test="createTime != null">create_time,</if>
         </trim>
        <trim prefix="values (" suffix=")" suffixOverrides=",">
            <if test="userId != null">#{userId},</if>
            <if test="pmsDocumentId != null">#{pmsDocumentId},</if>
            <if test="createTime != null">#{createTime},</if>
         </trim>
    </insert>


    <delete id="deletePmsDocumentUserFavoriteByDocId">
        delete from pms_document_user_favorite
               where user_id = #{userId} and pms_document_id = #{documentId}
    </delete>
</mapper>