<?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.banner.dao.BannerDao">

    <sql id="includeBanner">
        b.id id,
        b.id banner_id,
        b.type,
        bt.title title,
        COALESCE(bt.link, '') link,
        bt.image image,
        bt.image_thumb image_thumb,
        b.sort,
        b.status,
        b.create_time,
        b.update_time
    </sql>
    <sql id="includeBannerDetail">
        b.id id,
        b.id banner_id,
        b.type,
        b.sort,
        b.status,
        b.create_time,
        b.update_time
    </sql>
    <sql id="includeBannerTranslation">
        id,
        banner_id,
        language,
        title,
        COALESCE(link, '') link,
        image,
        image_thumb
    </sql>

    <sql id="leftJoinTranslations">
        LEFT JOIN cms_banner_i18n bt ON b.id = bt.banner_id
    </sql>

    <resultMap id="resultMapBanner" type="java.util.LinkedHashMap">
        <id property="id" column="id" jdbcType="BIGINT" />
        <result property="banner_id" column="banner_id" javaType="java.lang.Long" />
        <result property="type" column="type" javaType="java.lang.Integer" />
        <result property="title" column="title" />
        <result property="link" column="link" />
        <result property="image" column="image" />
        <result property="image_thumb" column="image_thumb" />
        <result property="sort" column="sort" javaType="java.lang.Integer" />
        <result property="status" column="status" javaType="java.lang.Integer" />
        <result property="create_time" column="create_time" />
        <result property="update_time" column="update_time" />
    </resultMap>

    <resultMap id="resultMapBannerDetail" type="java.util.LinkedHashMap">
        <id property="id" column="id" jdbcType="BIGINT" />
        <result property="banner_id" column="banner_id" javaType="java.lang.Long" />
        <result property="title" column="title" />
        <result property="link" column="link" />
        <result property="image" column="image" />
        <result property="image_thumb" column="image_thumb" />
        <result property="type" column="type" javaType="java.lang.Integer" />
        <result property="sort" column="sort" javaType="java.lang.Integer" />
        <result property="status" column="status" 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">
            <result property="title" column="title" />
            <result property="link" column="link" />
            <result property="image" column="image" />
            <result property="image_thumb" column="image_thumb" />
        </collection>
    </resultMap>


    <!-- 查 列表 -->
    <select id="selectBannerList" resultMap="resultMapBanner">
        SELECT <include refid="includeBanner" />
        FROM cms_banner b
        <include refid="leftJoinTranslations" />
        <where>
            bt.language = #{lang}
            <if test="title != null and title != ''">
                AND bt.title LIKE CONCAT('%', #{title}, '%')
            </if>
            <if test="status != null and status != ''">
                AND b.status = #{status}
            </if>
        </where>
    </select>

    <!-- 查 详情 -->
    <select id="selectBannerDetail" resultMap="resultMapBannerDetail">
        SELECT <include refid="includeBannerDetail" />
        FROM cms_banner b
        <where>
            <if test="banner_id != null and banner_id != ''">
                AND b.id = #{banner_id}
            </if>
        </where>
    </select>

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

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

</mapper>