<?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.pnkx.mapper.PxArticleMapper">

    <resultMap type="com.pnkx.domain.vo.PxArticleVo" id="PxArticleResult">
        <result property="id" column="id"/>
        <result property="title" column="title"/>
        <result property="cover" column="cover"/>
        <result property="content" column="content"/>
        <result property="richText" column="rich_text"/>
        <result property="tag" column="tag"/>
        <result property="type" column="type"/>
        <result property="typeName" column="type_name"/>
        <result property="open" column="open"/>
        <result property="version" column="version"/>
        <result property="likeNumber" column="like_number"/>
        <result property="createBy" column="create_by"/>
        <result property="nickName" column="nick_name"/>
        <result property="createTime" column="create_time"/>
        <result property="updateBy" column="update_by"/>
        <result property="updateTime" column="update_time"/>
        <result property="remark" column="remark"/>
        <result property="leaveMessageNumber" column="leaveMessageNumber"/>
    </resultMap>

    <sql id="selectPxArticleVo">
        SELECT a.id,
               a.title,
               a.cover,
               a.content,
               a.rich_text,
               a.tag,
               a.type,
               d.dict_label type_name,
               a.state,
            `open`,
            a.version,
            u.nick_name,
            a.create_by,
            a.create_time,
            a.update_by,
            a.update_time,
            a.remark,
            a.visits visitsNumber,
            (select count(*) from px_leave_message where article_id = a.id) leaveMessageNumber,
            (select count(*) from px_like_record where item_id = a.id) like_number
        FROM
            px_article a
            LEFT JOIN sys_user u
        on a.create_by = u.user_id
            LEFT JOIN sys_dict_data d ON a.type = d.dict_value
            AND dict_type = 'px_article_type'
    </sql>
    <sql id="selectPxArticleNotContentVo">
        SELECT a.id,
               a.title,
               a.cover,
               a.tag,
               a.type,
               d.dict_label type_name,
               a.state,
            `open`,
            a.version,
            u.nick_name,
            a.create_by,
            a.create_time,
            a.update_by,
            a.update_time,
            a.remark,
            a.visits visitsNumber,
            (select count(*) from px_leave_message where article_id = a.id) leaveMessageNumber,
            (select count(*) from px_like_record where item_id = a.id) like_number
        FROM
            px_article a
            LEFT JOIN sys_user u
        on a.create_by = u.user_id
            LEFT JOIN sys_dict_data d ON a.type = d.dict_value
            AND dict_type = 'px_article_type'
    </sql>

    <select id="selectPxArticleList" parameterType="com.pnkx.domain.vo.PxArticleVo" resultMap="PxArticleResult">
        <include refid="selectPxArticleVo"/>
        <where>
            <if test="title != null  and title != ''">and title like concat('%', #{title}, '%')</if>
            <if test="richText != null  and richText != ''">and rich_text like concat('%', #{richText}, '%')</if>
            <if test="type != null  and type != ''">and `type` = #{type}</if>
            <if test="open != null  and type != ''">and `open` = #{open}</if>
            <if test="createBy != null  and createBy != ''">and a.create_by like concat('%', #{createBy}, '%')</if>
            <if test="params.beginCreateTime != null and params.beginCreateTime != '' and params.endCreateTime != null and params.endCreateTime != ''">
                and create_time between #{params.beginCreateTime} and #{params.endCreateTime}
            </if>
            <if test="searchValue != null  and searchValue != ''">
                and title like concat('%', #{searchValue}, '%')
                or rich_text like concat('%', #{searchValue}, '%')
                or content like concat('%', #{searchValue}, '%')
                or `type` like concat('%', #{searchValue}, '%')
            </if>
        </where>
        order by create_time desc
    </select>

    <select id="getArticleById" parameterType="String" resultMap="PxArticleResult">
        <include refid="selectPxArticleVo"/>
        where id = #{id}
    </select>

    <!--校验字典项标签、键值唯一性-->
    <select id="dictDataCheckUniqueness" resultType="java.lang.Integer">
        SELECT
        count(*)
        FROM
        `sys_dict_data`
        WHERE
        dict_type = #{dictType}
        <if test="dictLabel != '' and dictLabel != null">
            AND dict_label = #{dictLabel}
        </if>
        <if test="dictValue != '' and dictValue != null">
            AND dict_value = #{dictValue}
        </if>
    </select>

    <select id="selectPxArticleNotContent" parameterType="com.pnkx.domain.vo.PxArticleVo"
            resultMap="PxArticleResult">
        <include refid="selectPxArticleNotContentVo"/>
        <where>
            <if test="open != null  and open != ''">and `open` = #{open}</if>
            <if test="state != null  and state != ''">and `state` = #{state}</if>
            <if test="title != null  and title != ''">and title like concat('%', #{title}, '%')</if>
            <if test="richText != null  and richText != ''">and rich_text like concat('%', #{richText}, '%')</if>
            <if test="type != null  and type != ''">and type = #{type}</if>
            <if test="createBy != null  and createBy != ''">and a.create_by like concat('%', #{createBy}, '%')</if>
            <if test="params.beginCreateTime != null and params.beginCreateTime != '' and params.endCreateTime != null and params.endCreateTime != ''">
                and create_time between #{params.beginCreateTime} and #{params.endCreateTime}
            </if>
            <if test="searchValue != null  and searchValue != ''">
                and title like concat('%', #{search}, '%')
                or rich_text like concat('%', #{search}, '%')
                or content like concat('%', #{search}, '%')
                or `type` like concat('%', #{search}, '%')
            </if>
        </where>
        order by create_time desc
    </select>

    <insert id="insertPxArticle" parameterType="com.pnkx.domain.vo.PxArticleVo"
            useGeneratedKeys="true" keyProperty="id">
        insert into px_article
        <trim prefix="(" suffix=")" suffixOverrides=",">
            <if test="id != null">id,</if>
            <if test="title != null and title != ''">title,</if>
            <if test="content != null">content,</if>
            <if test="cover != null">cover,</if>
            <if test="richText != null and richText != ''">rich_text,</if>
            <if test="tag != null and tag != ''">tag,</if>
            <if test="type != null and type != ''">`type`,</if>
            <if test="version != null">version,</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="remark != null">remark,</if>
            <if test="open != null">`open`,</if>
            <if test="state != null">`state`,</if>
        </trim>
        <trim prefix="values (" suffix=")" suffixOverrides=",">
            <if test="id != null">#{id},</if>
            <if test="title != null and title != ''">#{title},</if>
            <if test="content != null">#{content},</if>
            <if test="cover != null">#{cover},</if>
            <if test="richText != null and richText != ''">#{richText},</if>
            <if test="tag != null and tag != ''">#{tag},</if>
            <if test="type != null and type != ''">#{type},</if>
            <if test="version != null">#{version},</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="remark != null">#{remark},</if>
            <if test="open != null">#{open},</if>
            <if test="state != null">#{state},</if>
        </trim>
    </insert>

    <update id="updatePxArticle" parameterType="com.pnkx.domain.vo.PxArticleVo">
        update px_article
        <trim prefix="SET" suffixOverrides=",">
            <if test="title != null and title != ''">title = #{title},</if>
            <if test="content != null">content = #{content},</if>
            <if test="cover != null">cover = #{cover},</if>
            <if test="richText != null and richText != ''">rich_text = #{richText},</if>
            <if test="tag != null and tag != ''">tag = #{tag},</if>
            <if test="type != null and type != ''">`type` = #{type},</if>
            <if test="open != null and open != ''">`open` = #{open},</if>
            <if test="state != null and state != ''">`state` = #{state},</if>
            <if test="version != null">version = #{version},</if>
            <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="remark != null">remark = #{remark},</if>
        </trim>
        where id = #{id}
    </update>

    <delete id="deletePxArticleById" parameterType="String">
        delete
        from px_article
        where id = #{id}
    </delete>

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

    <!--获取最热文章-->
    <select id="getHotArticle" resultType="map">
        SELECT a.id,
               title,
               (SELECT count(1) FROM px_leave_message WHERE article_id = a.id) leaveMessageNumber,
               (SELECT count(1) FROM px_like_record WHERE item_id = a.id) likeNumber,
                a.visits visitsNumber
        FROM px_article a
        where `open` = '1' and a.state = '1'
        ORDER BY (leaveMessageNumber*50 + likeNumber*10 + visitsNumber) DESC
            limit 5
    </select>

    <!-- 查询上一篇文章 -->
    <select id="getLastArticleById" resultType="com.pnkx.domain.po.PxArticle">
        <include refid="selectPxArticleVo"/>
        where id &gt; #{id} order by create_time desc limit 1
    </select>

    <!-- 查询下一篇文章 -->
    <select id="getNextArticleById" resultType="com.pnkx.domain.po.PxArticle">
        <include refid="selectPxArticleVo"/>
        where id &lt; #{id} order by create_time desc limit 1
    </select>

    <!--更新访问次数-->
    <update id="updateVisits" parameterType="string">
        update px_article
        set visits = visits + 1
        where id = #{articleId}
    </update>

    <select id="selectPxArticleNumberByType" parameterType="string" resultType="int">
        select count(*) from px_article where type = #{type}
    </select>

    <!--获取文章标签列表-->
    <select id="getLabelList" resultType="string">
        select DISTINCT tag from px_article where tag is not null and tag != ''
    </select>
</mapper>
