<?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.frequency.recommend.mapper.RecommendMapper">

    <resultMap type="com.ruoyi.frequency.recommend.entity.Recommend" id="RecommendResult">
        <result property="id"    column="id"    />
        <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="delFlag"    column="del_flag"    />
        <result property="objectId"    column="object_id"    />
        <result property="sort"    column="sort"    />
        <result property="type"    column="type"    />
    </resultMap>

    <sql id="selectRecommendVo">
        select id, create_by, create_time, update_by, update_time, del_flag, object_id, sort, type from t_recommend
    </sql>

    <select id="selectRecommendList" parameterType="com.ruoyi.frequency.recommend.entity.Recommend" resultMap="RecommendResult">
        <include refid="selectRecommendVo"/>
        <where>
            <if test="objectId != null "> and object_id = #{objectId}</if>
            <if test="sort != null "> and sort = #{sort}</if>
            <if test="type != null "> and type = #{type}</if>
        </where>
    </select>

    <select id="selectRecommendById" parameterType="Long" resultMap="RecommendResult">
        <include refid="selectRecommendVo"/>
        where id = #{id}
    </select>

    <insert id="insertRecommend" parameterType="com.ruoyi.frequency.recommend.entity.Recommend">
        insert into t_recommend
        <trim prefix="(" suffix=")" suffixOverrides=",">
            <if test="id != null">id,</if>
            <if test="createBy != null">create_by,</if>
            <if test="createTime != null">create_time,</if>
            <if test="updateBy != null">update_by,</if>
            <if test="updateTime != null">update_time,</if>
            <if test="delFlag != null">del_flag,</if>
            <if test="objectId != null">object_id,</if>
            <if test="sort != null">sort,</if>
            <if test="type != null">type,</if>
         </trim>
        <trim prefix="values (" suffix=")" suffixOverrides=",">
            <if test="id != null">#{id},</if>
            <if test="createBy != null">#{createBy},</if>
            <if test="createTime != null">#{createTime},</if>
            <if test="updateBy != null">#{updateBy},</if>
            <if test="updateTime != null">#{updateTime},</if>
            <if test="delFlag != null">#{delFlag},</if>
            <if test="objectId != null">#{objectId},</if>
            <if test="sort != null">#{sort},</if>
            <if test="type != null">#{type},</if>
         </trim>
    </insert>

    <update id="updateRecommend" parameterType="com.ruoyi.frequency.recommend.entity.Recommend">
        update t_recommend
        <trim prefix="SET" suffixOverrides=",">
            <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="delFlag != null">del_flag = #{delFlag},</if>
            <if test="objectId != null">object_id = #{objectId},</if>
            <if test="sort != null">sort = #{sort},</if>
            <if test="type != null">type = #{type},</if>
        </trim>
        where id = #{id}
    </update>

    <delete id="deleteRecommendById" parameterType="Long">
        delete from t_recommend where id = #{id}
    </delete>

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

    <update id="topping">
        insert into t_recommend
        (id, create_by, create_time, update_by, update_time, type, object_id)
        values
            (#{req.id},#{req.updateBy},#{req.updateTime},#{req.updateBy},#{req.updateTime},#{req.type}, #{req.objectId})
        ON DUPLICATE  KEY UPDATE
                              del_flag = IF( del_flag = 1, 3, 1 ),
                              update_time = #{req.updateTime},
                              update_by = #{req.updateBy};
    </update>
</mapper>
