<?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.fs.store.mapper.FsHomeArticleViewMapper">

    <resultMap type="FsHomeArticleView" id="FsHomeArticleViewResult">
        <result property="viewId"    column="view_id"    />
        <result property="articleId"    column="article_id"    />
        <result property="userId"    column="user_id"    />
        <result property="createTime"    column="create_time"    />
    </resultMap>

    <sql id="selectFsHomeArticleViewVo">
        select view_id, article_id, user_id, create_time from fs_home_article_view
    </sql>

    <select id="selectFsHomeArticleViewList" resultType="FsHomeArticleView">
        SELECT
        ha.article_id,
        ha.title,
        u.user_id,
        u.avatar,
        u.phone,
        u.nickname,
        v.create_time
        FROM
        fs_home_article_view v
        LEFT JOIN fs_user u ON u.user_id = v.user_id
        LEFT JOIN fs_home_article ha ON ha.article_id = v.article_id
        <where>
            <if test="title != null">
                AND ha.title like concat('%', #{title}, '%')
            </if>
            <if test="phone != null">
                AND u.phone like concat('%', #{phone}, '%')
            </if>
        </where>
        ORDER BY v.create_time
    </select>

    <select id="selectFsHomeArticleViewById" parameterType="Long" resultMap="FsHomeArticleViewResult">
        <include refid="selectFsHomeArticleViewVo"/>
        where view_id = #{viewId}
    </select>

    <insert id="insertFsHomeArticleView" parameterType="FsHomeArticleView" useGeneratedKeys="true" keyProperty="viewId">
        insert into fs_home_article_view
        <trim prefix="(" suffix=")" suffixOverrides=",">
            <if test="articleId != null">article_id,</if>
            <if test="userId != null">user_id,</if>
            <if test="createTime != null">create_time,</if>
         </trim>
        <trim prefix="values (" suffix=")" suffixOverrides=",">
            <if test="articleId != null">#{articleId},</if>
            <if test="userId != null">#{userId},</if>
            <if test="createTime != null">#{createTime},</if>
         </trim>
    </insert>

    <update id="updateFsHomeArticleView" parameterType="FsHomeArticleView">
        update fs_home_article_view
        <trim prefix="SET" suffixOverrides=",">
            <if test="articleId != null">article_id = #{articleId},</if>
            <if test="userId != null">user_id = #{userId},</if>
            <if test="createTime != null">create_time = #{createTime},</if>
        </trim>
        where view_id = #{viewId}
    </update>

    <delete id="deleteFsHomeArticleViewById" parameterType="Long">
        delete from fs_home_article_view where view_id = #{viewId}
    </delete>

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

    <select id="selectHomeArticleViewsList" resultType="HomeArticleViewsListVO">
        SELECT
        u.user_id,
        u.avatar,
        u.nickname
        FROM
        fs_home_article_view v
        LEFT JOIN fs_user u ON u.user_id = v.user_id
        <where>
            <if test="param.articleId != null">
                AND v.article_id = #{param.articleId}
            </if>
        </where>
        ORDER BY
        v.view_id DESC
    </select>

</mapper>
