<?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="cn.ey.biz.module.system.dal.mapper.i18n.I18nMapper">

    <!-- 自定义ORM映射规则 -->
    <resultMap id="i18nDO" type="cn.ey.biz.module.system.controller.admin.i18n.vo.I18nRespVO">
        <result column="type" property="type"/>
        <result column="key" property="key"/>
        <!-- 当关联属性为集合时用collection标签来描述关联属性
             ofType是关联属性集合中装的实体类的全类名 -->
        <collection property="langs" column="{key=key,type=type}" ofType="cn.ey.biz.module.system.controller.admin.i18n.vo.I18nBaseVO" select="selectLangs">
            <result property="lang" column="lang" />
            <result property="label" column="label" />
        </collection>
        <collection property="appIds" column="key" ofType="String" select="selectApps">
            <result column="app" />
        </collection>
    </resultMap>

    <select id="selectPage" resultMap="i18nDO">
        SELECT
        DISTINCT
        a2.id,
        a1.type,
        a1.key
        FROM  infra_i18n_app a2
        LEFT JOIN infra_i18n a1 ON  a2.i18n_key = a1.key AND a2.deleted = 0
        WHERE
        a1.deleted = 0
        AND a1.type = 'label'
        <if test="c.app != null and c.app != ''">
            and a2.app = #{c.app}
        </if>
        <if test="c.key != null and c.key != ''">
            and a1.key like concat('%', #{c.key},'%')
        </if>
        <if test="c.lang != null and c.lang != ''">
            and a1.lang = #{c.lang}
        </if>
    </select>

    <select id="selectLangs" resultType="cn.ey.biz.module.system.controller.admin.i18n.vo.I18nBaseVO">
       SELECT
            a1.lang,
            a1.label
        FROM
            infra_i18n a1
        WHERE a1.key = #{key} AND a1.type = #{type}
            AND a1.deleted = 0
    </select>

    <select id="selectApps" resultType="String">
         SELECT
            ia.app
         FROM infra_i18n_app ia where ia.i18n_key = #{key}  AND ia.deleted = 0
    </select>

    <select id="getI18nByApp" resultType="cn.ey.biz.module.system.dal.dataobject.i18n.I18nDO">
        SELECT
        i.*
        FROM
            infra_i18n i
            LEFT JOIN infra_i18n_app ia ON i.KEY = ia.i18n_key
        WHERE
            i.deleted = 0
            AND ia.deleted = 0
            AND ia.app = #{app}
            AND i.lang = #{acceptLanguage}
    </select>
</mapper>

