<?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.ruoyi.system.mapper.CommentStarsMapper">

    <resultMap type="CommentStars" id="CommentStarsResult">
        <result property="starsId"    column="stars_id"    />
        <result property="targetId"    column="target_id"    />
        <result property="targetType"    column="target_type"    />
        <result property="targetTitle"    column="target_title"    />
        <result property="userId"    column="user_id"    />
        <result property="nickName"    column="nick_name"    />
        <result property="avatar"    column="avatar"    />
        <result property="delFlag"    column="del_flag"    />
        <result property="createBy"    column="create_by"    />
        <result property="createTime"    column="create_time"    />
        <result property="updateBy"    column="update_by"    />
        <result property="updateTime"    column="update_time"    />
        <result property="remark"    column="remark"    />
    </resultMap>

    <sql id="selectCommentStarsVo">
        select stars_id, target_id, target_type, target_title, user_id, nick_name, avatar, del_flag, create_by, create_time, update_by, update_time, remark from comment_stars
    </sql>

    <select id="selectCommentStarsList" parameterType="CommentStars" resultMap="CommentStarsResult">
        <include refid="selectCommentStarsVo"/>
        <where>
            and del_flag = 'N'
            <if test="targetId != null "> and target_id = #{targetId}</if>
            <if test="targetType != null  and targetType != ''"> and target_type = #{targetType}</if>
            <if test="targetTitle != null  and targetTitle != ''"> and target_title = #{targetTitle}</if>
            <if test="userId != null "> and user_id = #{userId}</if>
            <if test="nickName != null  and nickName != ''"> and nick_name like concat('%', #{nickName}, '%')</if>
            <if test="avatar != null  and avatar != ''"> and avatar = #{avatar}</if>
        </where>
        order by create_time DESC
    </select>
    
    <select id="selectCommentStarsByStarsId" parameterType="Long" resultMap="CommentStarsResult">
        <include refid="selectCommentStarsVo"/>
        where stars_id = #{starsId}
    </select>

    <insert id="insertCommentStars" parameterType="CommentStars" useGeneratedKeys="true" keyProperty="starsId">
        insert into comment_stars
        <trim prefix="(" suffix=")" suffixOverrides=",">
            <if test="targetId != null">target_id,</if>
            <if test="targetType != null and targetType != ''">target_type,</if>
            <if test="targetTitle != null">target_title,</if>
            <if test="userId != null">user_id,</if>
            <if test="nickName != null">nick_name,</if>
            <if test="avatar != null">avatar,</if>
            <if test="delFlag != null">del_flag,</if>
            <if test="createBy != null">create_by,</if>
            <if test="updateBy != null">update_by,</if>
            <if test="updateTime != null">update_time,</if>
            <if test="remark != null">remark,</if>
            create_time
        </trim>
        <trim prefix="values (" suffix=")" suffixOverrides=",">
            <if test="targetId != null">#{targetId},</if>
            <if test="targetType != null and targetType != ''">#{targetType},</if>
            <if test="targetTitle != null">#{targetTitle},</if>
            <if test="userId != null">#{userId},</if>
            <if test="nickName != null">#{nickName},</if>
            <if test="avatar != null">#{avatar},</if>
            <if test="delFlag != null">#{delFlag},</if>
            <if test="createBy != null">#{createBy},</if>
            <if test="updateBy != null">#{updateBy},</if>
            <if test="updateTime != null">#{updateTime},</if>
            <if test="remark != null">#{remark},</if>
            sysdate()
        </trim>
    </insert>

    <update id="updateCommentStars" parameterType="CommentStars">
        update comment_stars
        <trim prefix="SET" suffixOverrides=",">
            <if test="targetId != null">target_id = #{targetId},</if>
            <if test="targetType != null and targetType != ''">target_type = #{targetType},</if>
            <if test="targetTitle != null">target_title = #{targetTitle},</if>
            <if test="userId != null">user_id = #{userId},</if>
            <if test="nickName != null">nick_name = #{nickName},</if>
            <if test="avatar != null">avatar = #{avatar},</if>
            <if test="delFlag != null">del_flag = #{delFlag},</if>
            <if test="createBy != null">create_by = #{createBy},</if>
            <if test="createTime != null">create_time = #{createTime},</if>
            <if test="updateBy != null">update_by = #{updateBy},</if>
            <if test="updateTime != null">update_time = #{updateTime},</if>
            <if test="remark != null">remark = #{remark},</if>
        </trim>
        where stars_id = #{starsId}
    </update>
    <update id="updateCommentStarsByUserIdAndCommunityId" parameterType="CommentStars">
        update comment_stars set del_flag = 'Y'
        where user_id = #{userId} and target_id = #{targetId} and target_type = #{targetType}
    </update>

    <delete id="deleteCommentStarsByStarsId" parameterType="Long">
        delete from comment_stars where stars_id = #{starsId}
    </delete>

    <delete id="deleteCommentStarsByStarsIds" parameterType="String">
        delete from comment_stars where stars_id in 
        <foreach item="starsId" collection="array" open="(" separator="," close=")">
            #{starsId}
        </foreach>
    </delete>
    <delete id="deleteCommentStarsByCommunityIds" parameterType="String">
        delete from comment_stars where target_type='1' and   target_id in
        <foreach item="communityIds" collection="array" open="(" separator="," close=")">
            #{targetId}
        </foreach>
    </delete>
</mapper>