<?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.UserCollectMapper">
    
    <resultMap type="UserCollect" id="UserCollectResult">
        <result property="id"    column="id"    />
        <result property="userId"    column="user_id"    />
        <result property="collectId"    column="collect_id"    />
        <result property="collectName"    column="collect_name"    />
        <result property="createTime"    column="create_time"    />
        <result property="updateTime"    column="update_time"    />
        <result property="collectType"    column="collect_type"    />
    </resultMap>

    <sql id="selectUserCollectVo">
        select id, user_id, collect_id, collect_name, create_time, update_time, collect_type from user_collect
    </sql>

    <select id="selectUserCollectList" parameterType="UserCollect" resultMap="UserCollectResult">
        <include refid="selectUserCollectVo"/>
        <where>  
            <if test="userId != null "> and user_id = #{userId}</if>
            <if test="collectId != null "> and collect_id = #{collectId}</if>
            <if test="collectName != null  and collectName != ''"> and collect_name like concat('%', #{collectName}, '%')</if>
            <if test="collectType != null  and collectType != ''"> and collect_type = #{collectType}</if>
        </where>
    </select>
    
    <select id="selectUserCollectById" parameterType="Long" resultMap="UserCollectResult">
        <include refid="selectUserCollectVo"/>
        where id = #{id}
    </select>

    <insert id="insertUserCollect" parameterType="UserCollect">
        insert into user_collect
        <trim prefix="(" suffix=")" suffixOverrides=",">
            <if test="id != null">id,</if>
            <if test="userId != null">user_id,</if>
            <if test="collectId != null">collect_id,</if>
            <if test="collectName != null">collect_name,</if>
            <if test="createTime != null">create_time,</if>
            <if test="updateTime != null">update_time,</if>
            <if test="collectType != null">collect_type,</if>
         </trim>
        <trim prefix="values (" suffix=")" suffixOverrides=",">
            <if test="id != null">#{id},</if>
            <if test="userId != null">#{userId},</if>
            <if test="collectId != null">#{collectId},</if>
            <if test="collectName != null">#{collectName},</if>
            <if test="createTime != null">#{createTime},</if>
            <if test="updateTime != null">#{updateTime},</if>
            <if test="collectType != null">#{collectType},</if>
         </trim>
    </insert>

    <update id="updateUserCollect" parameterType="UserCollect">
        update user_collect
        <trim prefix="SET" suffixOverrides=",">
            <if test="userId != null">user_id = #{userId},</if>
            <if test="collectId != null">collect_id = #{collectId},</if>
            <if test="collectName != null">collect_name = #{collectName},</if>
            <if test="createTime != null">create_time = #{createTime},</if>
            <if test="updateTime != null">update_time = #{updateTime},</if>
            <if test="collectType != null">collect_type = #{collectType},</if>
        </trim>
        where id = #{id}
    </update>

    <delete id="deleteUserCollectById" parameterType="Long">
        delete from user_collect where id = #{id}
    </delete>

    <delete id="deleteUserCollectByIds" parameterType="String">
        delete from user_collect where id in 
        <foreach item="id" collection="array" open="(" separator="," close=")">
            #{id}
        </foreach>
    </delete>

    <select id="selectUserCollectAndUserId" resultMap="UserCollectResult">
        SELECT uc.* FROM music_collect mc LEFT JOIN user_collect uc on uc.collect_id=mc.collect_id WHERE mc.music_id=#{musicId} and uc.user_id=#{userId}
    </select>
</mapper>