<?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.XysArticleMapper">

    <resultMap type="XysArticle" id="XysArticleResult">
        <result property="id" column="id"/>
        <result property="title" column="title"/>
        <result property="coverImg" column="cover_img"/>
        <result property="contentImg" column="content_img"/>
        <result property="description" column="description"/>
        <result property="content" column="content"/>
        <result property="articleCat" column="article_cat"/>
        <result property="sort" column="sort"/>
        <result property="createTime" column="create_time"/>
        <result property="createBy" column="create_by"/>
        <result property="updateTime" column="update_time"/>
        <result property="updateBy" column="update_by"/>
        <result property="remark" column="remark"/>
        <result property="delFlag" column="del_flag"/>
    </resultMap>

    <sql id="selectXysArticleVo">
        select id,
               title,
               cover_img,
               content_img,
               description,
               content,
               article_cat,
               sort,
               create_time,
               create_by,
               update_time,
               update_by,
               remark,
               del_flag
        from xys_article
    </sql>

    <select id="selectHomeArticleList" parameterType="XysArticle" resultMap="XysArticleResult">
        select id,title,description,create_time from xys_article
        <where>
            1=1 and article_cat = 7
            <if test="title != null  and title != ''">and title = #{title}</if>
            <if test="articleCat != null ">and article_cat = #{articleCat}</if>
        </where>
        order by create_time desc
        limit 3
    </select>

    <select id="selectXysArticleList" parameterType="XysArticle" resultMap="XysArticleResult">
        select a.*,c.title as catName from xys_article a left join xys_category c on a.article_cat = c.id
        <where>
            <if test="title != null  and title != ''">and a.title = #{title}</if>
            <if test="articleCat != null ">and a.article_cat = #{articleCat}</if>
        </where>
        order by sort asc,create_time desc
    </select>

    <select id="selectXysArticleById" parameterType="Long" resultMap="XysArticleResult">
        <include refid="selectXysArticleVo"/>
        where id = #{id}
    </select>

    <insert id="insertXysArticle" parameterType="XysArticle" useGeneratedKeys="true" keyProperty="id">
        insert into xys_article
        <trim prefix="(" suffix=")" suffixOverrides=",">
            <if test="title != null">title,</if>
            <if test="coverImg != null">cover_img,</if>
            <if test="contentImg != null">content_img,</if>
            <if test="content != null">content,</if>
            <if test="description != null">description,</if>
            <if test="articleCat != null">article_cat,</if>
            <if test="sort != null">sort,</if>
            <if test="createTime != null">create_time,</if>
            <if test="createBy != null">create_by,</if>
            <if test="updateTime != null">update_time,</if>
            <if test="updateBy != null">update_by,</if>
            <if test="remark != null">remark,</if>
            <if test="delFlag != null">del_flag,</if>
        </trim>
        <trim prefix="values (" suffix=")" suffixOverrides=",">
            <if test="title != null">#{title},</if>
            <if test="coverImg != null">#{coverImg},</if>
            <if test="contentImg != null">#{contentImg},</if>
            <if test="content != null">#{content},</if>
            <if test="description != null">#{description},</if>
            <if test="articleCat != null">#{articleCat},</if>
            <if test="sort != null">#{sort},</if>
            <if test="createTime != null">#{createTime},</if>
            <if test="createBy != null">#{createBy},</if>
            <if test="updateTime != null">#{updateTime},</if>
            <if test="updateBy != null">#{updateBy},</if>
            <if test="remark != null">#{remark},</if>
            <if test="delFlag != null">#{delFlag},</if>
        </trim>
    </insert>

    <update id="updateXysArticle" parameterType="XysArticle">
        update xys_article
        <trim prefix="SET" suffixOverrides=",">
            <if test="title != null">title = #{title},</if>
            <if test="coverImg != null">cover_img = #{coverImg},</if>
            <if test="contentImg != null">content_img = #{contentImg},</if>
            <if test="content != null">content = #{content},</if>
            <if test="description != null">description = #{description},</if>
            <if test="articleCat != null">article_cat = #{articleCat},</if>
            <if test="sort != null">sort = #{sort},</if>
            <if test="createTime != null">create_time = #{createTime},</if>
            <if test="createBy != null">create_by = #{createBy},</if>
            <if test="updateTime != null">update_time = #{updateTime},</if>
            <if test="updateBy != null">update_by = #{updateBy},</if>
            <if test="remark != null">remark = #{remark},</if>
            <if test="delFlag != null">del_flag = #{delFlag},</if>
        </trim>
        where id = #{id}
    </update>

    <delete id="deleteXysArticleById" parameterType="Long">
        delete
        from xys_article
        where id = #{id}
    </delete>

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

    <select id="getNext" resultMap="XysArticleResult">
        select id,title from xys_article
        <where>
            <if test="id != null">and id &gt; #{id}</if>
            <if test="articleCat != null ">and article_cat = #{articleCat}</if>
        </where>
        limit 1
    </select>

    <select id="getUp" resultMap="XysArticleResult">
        select id,title from xys_article
        <where>
            <if test="id != null">and id &lt; #{id}</if>
            <if test="articleCat != null ">and article_cat = #{articleCat}</if>
        </where>
        ORDER BY create_time desc
        limit 1
    </select>

</mapper>
