<?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.browserecord.mapper.BrowseRecordMapper">

    <resultMap type="com.ruoyi.frequency.browserecord.entity.BrowseRecord" id="BrowseRecordResult">
        <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="objectId"    column="object_id"    />
        <result property="userId"    column="user_id"    />
        <result property="userType"    column="user_type"    />
        <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="selectBrowseRecordVo">
        select
            t.id ,
            t.create_by ,
            t.create_time ,
            t.update_by ,
            t.update_time ,
            t.del_flag ,
            t.type ,
            t.object_id ,
            t.user_id ,
            t.user_type ,
            u.user_type,
            u.id_no,
            u.mobile,
            u.avatar,
            u.nickname
        from t_browse_record AS t
                 LEFT JOIN t_user AS u ON t.user_id = u.id
    </sql>

    <select id="selectBrowseRecordList" parameterType="com.ruoyi.frequency.browserecord.entity.BrowseRecord" resultMap="BrowseRecordResult">
        <include refid="selectBrowseRecordVo"/>
        <where> AND t.del_flag != 2
            <if test="type != null "> and t.type = #{type}</if>
            <if test="objectId != null "> and t.object_id = #{objectId}</if>
            <if test="userId != null "> and t.user_id = #{userId}</if>
            <if test="userType != null "> and t.user_type = #{userType}</if>
            <if test="delFlag != null and delFlag != ''"> and t.del_flag = #{delFlag}</if>
        </where>
        order by t.create_time desc
    </select>

    <select id="selectBrowseRecordById" parameterType="Long" resultMap="BrowseRecordResult">
        <include refid="selectBrowseRecordVo"/>
        where t.id = #{id}
    </select>

    <insert id="insertBrowseRecord" parameterType="com.ruoyi.frequency.browserecord.entity.BrowseRecord">
        insert into t_browse_record
        <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="objectId != null">object_id,</if>
            <if test="userId != null">user_id,</if>
            <if test="userType != null">user_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="type != null">#{type},</if>
            <if test="objectId != null">#{objectId},</if>
            <if test="userId != null">#{userId},</if>
            <if test="userType != null">#{userType},</if>
        </trim>
    </insert>

    <update id="updateBrowseRecord" parameterType="com.ruoyi.frequency.browserecord.entity.BrowseRecord">
        update t_browse_record
        <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="objectId != null">object_id = #{objectId},</if>
            <if test="userId != null">user_id = #{userId},</if>
            <if test="userType != null">user_type = #{userType},</if>
        </trim>
        where id = #{id}
    </update>

    <select id="worksBrowsePage" resultType="com.ruoyi.controller.resp.WorksPageResp">
        SELECT
        t.id,
        c.update_time create_time,
        t.works_type_id,
        t.title,
        t.images,
        t.price,
        t.user_type,
        t.store_id,
        t.source,
        t.order_id,
        t.like_num,
        t.step_num,
        t.comment_num,
        t.collect_num,
        t.browse_num,
        t.remark
        FROM
        t_browse_record AS c
        LEFT JOIN t_works t on c.object_id = t.id
        WHERE
        c.del_flag = 1
        AND c.user_type = #{req.userType}
        AND c.user_id = #{req.userId}
        AND c.type = #{req.type}
        <if test="req.typeIds!=null and req.typeIds.size()>0">
            AND t.works_type_id IN
            <foreach collection="req.typeIds" index="index" item="i" open="("
                     separator="," close=")">
                #{i}
            </foreach>
        </if>
        ORDER BY
        c.update_time DESC
    </select>


    <select id="postBrowsePage" resultType="com.ruoyi.post.controller.resp.PostPageResp">
        SELECT
        t.id,
        1 type,
        c.update_time create_time,
        t.user_id,
        t.user_type,
        t.title,
        t.cover_image,
        t.video_url,
        t.share_num,
        t.like_num,
        t.comment_num,
        t.collect_num,
        t.browse_num,
        NULL isBuy,
        NULL price,
        CASE WHEN fl.id is null THEN 1 ELSE 2 END isLike,
        CASE WHEN col.id is null THEN 1 ELSE 2 END isCollect,
        CASE
        WHEN #{userVo.userType} = t.user_type
        AND #{userVo.userId} = t.user_id THEN
        2 ELSE 1
        END isSelf,
        t.result,
        t.address

        FROM
        t_browse_record AS c
        LEFT JOIN t_post t on c.object_id = t.id
        LEFT JOIN t_collect col on col.object_id = t.id and col.del_flag = 1  AND col.type = 2  AND col.user_id = #{userVo.userId}
        AND col.user_type = #{userVo.userType}
        LEFT JOIN t_post_like fl on fl.object_id = t.id AND fl.type = 1 AND fl.user_id = #{userVo.userId} AND fl.user_type = #{userVo.userType}
        AND fl.del_flag = 1
        WHERE
        c.del_flag = 1
        AND c.user_type = #{req.userType}
        AND c.user_id = #{req.userId}
        AND c.type = #{req.type}
        <if test="req.typeId!=null and req.typeId!=''">
            AND t.post_type_id = #{req.typeId}
        </if>
        ORDER BY
        c.update_time DESC
    </select>
</mapper>