<?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.like.mapper.LikeMapper">

    <resultMap type="com.ruoyi.frequency.like.entity.Like" id="LikeResult">
        <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="type"    column="type"    />
        <result property="userType"    column="user_type"    />
        <result property="userId"    column="user_id"    />
        <result property="linkId"    column="link_id"    />
        <association property="userInfo"    column="user_id" javaType="com.ruoyi.vo.UserInfo" resultMap="userInfo" />
    </resultMap>

    <resultMap id="userInfo" type="com.ruoyi.vo.UserInfo">
        <id     property="userId"    column="user_id"     />
        <result property="userType"  column="user_type"   />
        <result property="idNo"  column="id_no"   />
        <result property="nickname"  column="nickname"   />
        <result property="avatar" column="avatar"   />
        <result property="mobile" column="mobile"   />
    </resultMap>


    <sql id="selectLikeVo">
        select
            t.id ,
            t.create_by ,
            t.create_time ,
            t.update_by ,
            t.update_time ,
            t.del_flag ,
            t.type ,
            t.user_type ,
            t.user_id ,
            t.link_id ,
            u.user_type,
            u.id_no,
            u.mobile,
            u.avatar,
            u.nickname
        from t_like AS t
                 LEFT JOIN t_user AS u ON t.user_id = u.id
    </sql>

    <select id="selectLikeList" parameterType="com.ruoyi.frequency.like.entity.Like" resultMap="LikeResult">
        <include refid="selectLikeVo"/>
        <where> AND t.del_flag != 2
            <if test="type != null "> and t.type = #{type}</if>
            <if test="userType != null "> and t.user_type = #{userType}</if>
            <if test="userId != null "> and t.user_id = #{userId}</if>
            <if test="linkId != null "> and t.link_id = #{linkId}</if>
            <if test="delFlag != null and delFlag != ''"> and t.del_flag = #{delFlag}</if>
        </where>
        order by t.create_time desc
    </select>

    <select id="selectLikeById" parameterType="Long" resultMap="LikeResult">
        <include refid="selectLikeVo"/>
        where t.id = #{id}
    </select>

    <insert id="insertLike" parameterType="com.ruoyi.frequency.like.entity.Like">
        insert into t_like
        <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="type != null">type,</if>
            <if test="userType != null">user_type,</if>
            <if test="userId != null">user_id,</if>
            <if test="linkId != null">link_id,</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="type != null">#{type},</if>
            <if test="userType != null">#{userType},</if>
            <if test="userId != null">#{userId},</if>
            <if test="linkId != null">#{linkId},</if>
        </trim>
    </insert>

    <update id="updateLike" parameterType="com.ruoyi.frequency.like.entity.Like">
        update t_like
        <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="type != null">type = #{type},</if>
            <if test="userType != null">user_type = #{userType},</if>
            <if test="userId != null">user_id = #{userId},</if>
            <if test="linkId != null">link_id = #{linkId},</if>
        </trim>
        where id = #{id}
    </update>

    <update id="like" parameterType="com.ruoyi.frequency.like.entity.Like">
        insert into t_like
        (id, create_by, create_time, update_by, update_time, type, user_type, user_id, link_id)
        values
            (#{req.id},#{req.updateBy},#{req.updateTime},#{req.updateBy},#{req.updateTime},#{req.type},#{userVo.userType}, #{userVo.userId}, #{req.linkId})
            ON DUPLICATE  KEY UPDATE
                                  del_flag = IF( del_flag = 1, 3, 1 ),
                                  update_time = #{req.updateTime},
                                  update_by = #{req.updateBy};
    </update>
</mapper>