<?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.kexue.skills.mapper.CmsContentLikeMapper">
    
    <resultMap type="com.kexue.skills.entity.CmsContentLike" id="CmsContentLikeResult">
        <result property="likeId" column="like_id"/>
        <result property="userId" column="user_id"/>
        <result property="userName" column="user_name"/>
        <result property="contentId" column="content_id"/>
        <result property="contentTitle" column="content_title"/>
        <result property="likeTime" column="like_time"/>
        <result property="deleteFlag" column="delete_flag"/>
    </resultMap>
    
    <insert id="insert" parameterType="com.kexue.skills.entity.CmsContentLike">
        INSERT INTO cms_content_like (
            user_id, user_name, content_id, content_title, like_time, delete_flag
        ) VALUES (
            #{userId}, #{userName}, #{contentId}, #{contentTitle}, NOW(), #{deleteFlag, jdbcType=INTEGER}
        )
    </insert>
    
    <update id="update" parameterType="com.kexue.skills.entity.CmsContentLike">
        UPDATE cms_content_like 
        <set>
            <if test="userId != null">user_id = #{userId},</if>
            <if test="userName != null and userName != ''">user_name = #{userName},</if>
            <if test="contentId != null">content_id = #{contentId},</if>
            <if test="contentTitle != null and contentTitle != ''">content_title = #{contentTitle},</if>
            <if test="likeTime != null">like_time = #{likeTime},</if>
            <if test="deleteFlag != null">delete_flag = #{deleteFlag},</if>
        </set>
        WHERE like_id = #{likeId}
    </update>
    
    <delete id="deleteById" parameterType="Long">
        DELETE FROM cms_content_like WHERE like_id = #{likeId}
    </delete>
    
    <select id="queryById" parameterType="Long" resultMap="CmsContentLikeResult">
        SELECT * FROM cms_content_like WHERE like_id = #{likeId}
    </select>
    
    <select id="queryByUserIdAndContentId" resultMap="CmsContentLikeResult">
        SELECT * FROM cms_content_like 
        WHERE user_id = #{userId} AND content_id = #{contentId} AND delete_flag = 0
    </select>
    
    <select id="queryRecentLikesByUserId" resultType="Long">
        SELECT content_id FROM cms_content_like 
        WHERE user_id = #{userId} AND delete_flag = 0
        ORDER BY like_time DESC
        LIMIT #{limit}
    </select>
    
    <select id="countByContentId" parameterType="Long" resultType="int">
        SELECT COUNT(*) FROM cms_content_like 
        WHERE content_id = #{contentId} AND delete_flag = 0
    </select>
</mapper>