<?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.backendsys.modules.cms.article.dao.ArticleDao">

    <sql id="includeArticle">
        a.id id,
        a.id article_id,
        a.uid uid,
        COALESCE(a.thumb, '') thumb,
        uf.user_id user_id,
        COALESCE(uf.nickname, '') user_nickname,
        COALESCE(ac.id, -1) category_id,
        COALESCE(ac.category_name, '') category_name,
        at.title title,
        COALESCE(at.description, '') description,
        a.status status,
        a.is_top is_top,
        a.create_time create_time,
        a.update_time update_time
    </sql>

    <sql id="includeArticleDetail">
        a.id id,
        a.id article_id,
        a.uid uid,
        COALESCE(a.thumb, '') thumb,
        uf.user_id user_id,
        COALESCE(uf.nickname, '') user_nickname,
        ac.id category_id,
        ac.category_name category_name,
        a.status status,
        a.is_top is_top,
        a.create_time create_time,
        a.update_time update_time
    </sql>
    <sql id="includeArticleTranslation">
        id,
        article_id,
        language,
        title,
        description,
        content,
        COALESCE(meta_keyword, '') meta_keyword,
        COALESCE(meta_description, '') meta_description
    </sql>
    <sql id="leftJoinCategory">
        LEFT JOIN cms_article_category_i18n ac ON ac.article_category_id = a.category_id AND ac.language = #{lang}
    </sql>
    <sql id="leftJoinUser">
        LEFT JOIN sys_user_info uf ON a.user_id = uf.user_id
    </sql>
    <sql id="leftJoinTranslations">
        LEFT JOIN cms_article_i18n at ON a.id = at.article_id
    </sql>

    <resultMap id="resultMapArticle" type="java.util.LinkedHashMap">
        <id property="id" column="id" jdbcType="BIGINT" />
        <result property="article_id" column="article_id" javaType="java.lang.Long" />
        <result property="uid" column="uid" />
        <result property="thumb" column="thumb" />
        <result property="user_id" column="user_id" javaType="java.lang.Long" />
        <result property="user_nickname" column="user_nickname" />
        <result property="category_id" column="category_id" javaType="java.lang.Long" />
        <result property="category_name" column="category_name" />
        <result property="title" column="title" />
        <result property="description" column="description" />
        <result property="status" column="status" javaType="java.lang.Integer" />
        <result property="is_top" column="is_top" javaType="java.lang.Integer" />
        <result property="create_time" column="create_time" />
        <result property="update_time" column="update_time" />
    </resultMap>

    <resultMap id="resultMapArticleDetail" type="java.util.LinkedHashMap">
        <id property="id" column="id" jdbcType="BIGINT" />
        <result property="article_id" column="article_id" javaType="java.lang.Long" />
        <result property="uid" column="uid" />
        <result property="thumb" column="thumb" />
        <result property="user_id" column="user_id" javaType="java.lang.Long" />
        <result property="user_nickname" column="user_nickname" />
        <result property="category_id" column="category_id" javaType="java.lang.Long" />
        <result property="category_name" column="category_name" />
        <result property="status" column="status" javaType="java.lang.Integer" />
        <result property="is_top" column="is_top" javaType="java.lang.Integer" />
        <result property="create_time" column="create_time" />
        <result property="update_time" column="update_time" />
        <collection property="translations" javaType="java.util.List"
            select="queryTranslationsById" column="id">
            <id property="id" column="id" />
            <result property="title" column="title" />
            <result property="content" column="content" />
            <result property="description" column="description" />
            <result property="meta_keyword" column="meta_keyword" />
            <result property="meta_description" column="meta_description" />
        </collection>
    </resultMap>


    <!-- 查 列表 -->
    <select id="selectArticleList" resultMap="resultMapArticle">
        SELECT <include refid="includeArticle" /> FROM cms_article a
        <include refid="leftJoinCategory" />
        <include refid="leftJoinUser" />
        <include refid="leftJoinTranslations" />
        <where>
            at.language = #{lang}
            <if test="title != null and title != ''">
                AND at.title LIKE CONCAT('%', #{title}, '%')
            </if>
            <if test="uid != null and uid != ''">
                AND a.uid = #{uid}
            </if>
            <if test="user_id != null and user_id != ''">
                AND a.user_id = #{user_id}
            </if>
            <if test="category_id != null and category_id != ''">
                AND a.category_id = #{category_id}
            </if>
            <if test="status != null and status != ''">
                AND a.status = #{status}
            </if>
            <if test="create_time != null and create_time != ''">
                AND a.create_time >= #{create_time}
            </if>
        </where>
        ORDER BY a.is_top = 1 DESC, a.create_time DESC
    </select>

    <!-- 查 详情 -->
    <select id="selectArticleDetail" resultMap="resultMapArticleDetail">
        SELECT <include refid="includeArticleDetail" />
        FROM cms_article a
        <include refid="leftJoinCategory" />
        <include refid="leftJoinUser" />
        <where>
            <if test="uid != null and uid != ''">
                AND a.uid = #{uid}
            </if>
            <if test="article_id != null and article_id != ''">
                AND a.id = #{article_id}
            </if>
        </where>
    </select>

    <!-- 查 翻译详情 (子查询) -->
    <select id="queryTranslationsById" resultType="java.util.LinkedHashMap">
        SELECT <include refid="includeArticleTranslation" />
        FROM cms_article_i18n
        WHERE article_id = #{id}
    </select>

    <!-- 查 详情 (公共) (带翻译 Object) (关联查询) -->
<!--    <select id="queryArticleDetailPublic" resultMap="resultMapArticle">-->
<!--        SELECT <include refid="includeArticle" />, content-->
<!--        FROM cms_article a-->
<!--        <include refid="leftJoinCategory" />-->
<!--        <include refid="leftJoinUser" />-->
<!--        <include refid="leftJoinTranslations" />-->
<!--        WHERE a.uid = #{uid} AND at.language = #{lang}-->
<!--    </select>-->

</mapper>