<?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.navigation.dao.NavigationDao">

    <sql id="includeNavigation">
        n.id id,
        n.id navigation_id,
        n.parent_id parent_id,
        n.uid uid,
        nt.navigation_name navigation_name,
        COALESCE(nt.link, '') link,
        n.sort sort,
        n.is_blank is_blank
    </sql>

    <sql id="includeNavigationDetail">
        n.id id,
        n.id navigation_id,
        n.uid uid,
        n.parent_id parent_id,
        n.sort sort,
        n.is_blank is_blank
    </sql>

    <sql id="includeNavigationTranslation">
        id, navigation_id, language, navigation_name, link
    </sql>

    <resultMap id="resultMapNavigation" type="java.util.LinkedHashMap">
        <id property="id" column="id" jdbcType="BIGINT" />
        <result property="navigation_id" column="navigation_id" javaType="java.lang.Long" />
        <result property="parent_id" column="parent_id" javaType="java.lang.Long" />
        <result property="uid" column="uid" />
        <result property="navigation_name" column="navigation_name" />
        <result property="link" column="link" />
        <result property="sort" column="sort" javaType="java.lang.Integer" />
        <result property="is_blank" column="is_blank" javaType="java.lang.Integer" />
    </resultMap>

    <resultMap id="resultMapNavigationDetail" type="java.util.LinkedHashMap">
        <id property="id" column="id" jdbcType="BIGINT" />
        <result property="navigation_id" column="navigation_id" javaType="java.lang.Long" />
        <result property="parent_id" column="parent_id" javaType="java.lang.Long" />
        <result property="uid" column="uid" />
        <result property="navigation_name" column="navigation_name" />
        <result property="link" column="link" />
        <result property="sort" column="sort" javaType="java.lang.Integer" />
        <result property="is_blank" column="is_blank" javaType="java.lang.Integer" />
        <collection property="translations" javaType="java.util.List"
            select="queryTranslationsById" column="id">
            <id property="id" column="id" />
            <result property="navigation_name" column="navigation_name" />
            <result property="link" column="link" />
        </collection>
    </resultMap>

    <sql id="leftJoinTranslations">
        LEFT JOIN cms_navigation_i18n nt ON n.id = nt.navigation_id
    </sql>

    <!-- 查 列表 -->
    <select id="selectNavigationList" resultMap="resultMapNavigation">
        SELECT
        <include refid="includeNavigation" />
        FROM cms_navigation n
        <include refid="leftJoinTranslations" />
        <where>
            nt.language = #{lang}
            <if test="navigation_name != null and navigation_name != ''">
                AND nt.navigation_name LIKE CONCAT('%', #{navigation_name}, '%')
            </if>
        </where>
        ORDER BY n.sort DESC
    </select>


    <!-- 查 详情 (带翻译 Array) (二次查询) -->
    <select id="selectNavigationDetail" resultMap="resultMapNavigationDetail">
        SELECT <include refid="includeNavigationDetail" />
        FROM cms_navigation n
        <where>
            <if test="uid != null and uid != ''">
                AND n.uid = #{uid}
            </if>
            <if test="navigation_id != null and navigation_id != ''">
                AND n.id = #{navigation_id}
            </if>
        </where>
    </select>

    <!-- 查 翻译集合 (子查询) -->
    <select id="queryTranslationsById" resultType="java.util.LinkedHashMap">
        SELECT <include refid="includeNavigationTranslation" />
        FROM cms_navigation_i18n
        WHERE navigation_id = #{id}
    </select>

    <!-- 查 详情 (公共) (带翻译 Object) (关联查询) -->
<!--    <select id="queryNavigationDetailPublic" resultMap="resultMapNavigation">-->
<!--        SELECT <include refid="includeNavigation" />-->
<!--        FROM cms_navigation n-->
<!--        <include refid="leftJoinTranslations" />-->
<!--        WHERE n.uid = #{uid} AND nt.language = #{lang}-->
<!--    </select>-->

    <!-- 创建 -->
<!--    <insert id="insertNavigation" parameterType="com.backendsys.modules.cms.navigation.entity."-->
<!--        useGeneratedKeys="true" keyProperty="navigation_id">-->
<!--        INSERT INTO cms_navigation (-->
<!--        <if test="parent_id != null and parent_id != ''">, parent_id</if>-->
<!--        <if test="sort != null and sort != ''">, sort</if>-->
<!--        <if test="is_blank != null and is_blank != ''">, is_blank</if>-->
<!--        )-->
<!--        VALUES (-->
<!--        <if test="parent_id != null and parent_id != ''">, #{parent_id}</if>-->
<!--        <if test="sort != null and sort != ''">, #{sort}</if>-->
<!--        <if test="is_blank != null and is_blank != ''">, #{is_blank}</if>-->
<!--        );-->
<!--    </insert>-->
    <!-- 创建翻译 -->
<!--    <insert id="insertNavigationTranslations" parameterType="com.backendcms.entity.Cms.CmsNavigationI18nDTO.TranslationsDTO">-->
<!--        INSERT INTO cms_navigation_i18n (navigation_id, language, navigation_name, link)-->
<!--        VALUES-->
<!--        <foreach collection="translations" item="translation" separator=",">-->
<!--            (#{translation.navigation_id}, #{translation.language}, #{translation.navigation_name}, #{translation.link})-->
<!--        </foreach>-->
<!--    </insert>-->

    <!-- 编辑 -->
<!--    <update id="updateNavigationTranslations" parameterType="com.backendcms.entity.Cms.CmsNavigationI18nDTO.CmsNavigationI18nDTO"-->
<!--            useGeneratedKeys="true" keyProperty="navigation_id">-->
<!--        UPDATE cms_navigation-->
<!--        SET-->
<!--        <trim suffixOverrides="," suffix=" ">-->
<!--            <if test="parent_id != null and parent_id != ''">parent_id = #{parent_id},</if>-->
<!--            <if test="sort != null and sort != ''">sort = #{sort},</if>-->
<!--            <if test="is_blank != null and is_blank != ''">is_blank = #{is_blank},</if>-->
<!--        </trim>-->
<!--        WHERE id = #{navigation_id};-->

<!--        <foreach collection="translations" item="translation" separator=";">-->
<!--            UPDATE cms_navigation_i18n-->
<!--            SET-->
<!--            <trim suffixOverrides="," suffix=" ">-->
<!--                navigation_name = #{translation.navigation_name},-->
<!--                <if test="translation.link != null and translation.link != ''">-->
<!--                    link = #{translation.link},-->
<!--                </if>-->
<!--            </trim>-->
<!--            WHERE navigation_id = ${navigation_id} AND language = #{translation.language}-->
<!--        </foreach>-->

<!--    </update>-->

    <!-- 删除 -->
    <delete id="deleteNavigation" parameterType="java.lang.Long">
        DELETE FROM cms_navigation WHERE id = #{navigation_id};
        DELETE FROM cms_navigation_i18n WHERE navigation_id = #{navigation_id};
    </delete>

    <!-- 删除 (批量) -->
    <delete id="deleteNavigationBatch" parameterType="java.lang.Long">
        DELETE FROM cms_navigation WHERE id IN
        <foreach collection="ids" item="id" open="(" separator="," close=")">
            #{id}
        </foreach>;

        DELETE FROM cms_navigation_i18n WHERE navigation_id IN
        <foreach collection="ids" item="id" open="(" separator="," close=")">
            #{id}
        </foreach>;
    </delete>

</mapper>