<?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.mapper.Cms.CmsPageMapper">

    
    <sql id="includePage">
        p.id id,
        p.id page_id,
        pt.title title,
        COALESCE(pt.description, '') description,
        p.create_time create_time,
        p.update_time update_time
    </sql>

    <sql id="includePageDetail">
        p.id id,
        p.id page_id,
        p.create_time create_time,
        p.update_time update_time
    </sql>
    
    <sql id="includePageTranslation">
        id,
        page_id,
        language,
        title,
        description,
        content
    </sql>

    <resultMap id="resultMapPage" type="java.util.LinkedHashMap">
        <id property="id" column="id" jdbcType="BIGINT" />
        <result property="page_id" column="page_id" javaType="java.lang.Long"/>
        <result property="title" column="title" />
        <result property="description" column="description" />
        <result property="create_time" column="create_time" />
        <result property="update_time" column="update_time" />
    </resultMap>

    <resultMap id="resultMapPageDetail" type="java.util.LinkedHashMap">
        <id property="id" column="id" jdbcType="BIGINT" />
        <result property="page_id" column="page_id" javaType="java.lang.Long"/>
        <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" />
        </collection>
    </resultMap>

    <sql id="leftJoinTranslations">
        LEFT JOIN cms_page_translations pt ON p.id = pt.page_id
    </sql>



    <!-- 查 列表 -->
    <select id="queryPageList" resultMap="resultMapPage">
        SELECT
           <include refid="includePage" />
        FROM cms_article p
        <include refid="leftJoinTranslations" />
        WHERE pt.language = #{lang}
    </select>


    <!-- 查 详情 (带翻译 Array) (二次查询) -->
    <select id="queryPageDetail" resultMap="resultMapPageDetail">
        SELECT <include refid="includePageDetail" />
        FROM cms_page p
        WHERE p.page_sign = #{page_sign}
    </select>
    <!-- 查 翻译集合 (子查询) -->
    <select id="queryTranslationsById" resultType="java.util.LinkedHashMap">
        SELECT <include refid="includePageTranslation" />
        FROM cms_page_translations
        WHERE page_id = #{id}
    </select>


    <!-- 查 详情 (公共) (带翻译 Object) (关联查询) -->
    <select id="queryPageDetailPublic" resultMap="resultMapPage">
        SELECT <include refid="includePage" />, content
        FROM cms_page p
        <include refid="leftJoinTranslations" />
        WHERE p.page_sign = #{page_sign} AND pt.language = #{lang}
    </select>
    

    <!-- 改 -->
    <update id="updatePage" parameterType="com.backendsys.entity.Cms.CmsPageDTO"
        useGeneratedKeys="true" keyProperty="page_id">
        <foreach collection="translations" item="translation" separator=";">
            UPDATE cms_page_translations
            <set>
                title = #{translation.title},
                content = #{translation.content},
                <if test="translation.description != null and translation.description != ''">
                    description = #{translation.description}
                </if>
            </set>
            WHERE page_sign = ${page_sign} AND language = #{translation.language}
        </foreach>
    </update>

</mapper>