<?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.m2pool.manage.mapper.ManageDocumentsMapper">


    <select id="getListDataByPage" resultType="com.m2pool.manage.dto.ManageDocumentDto">
SELECT
    id,
    title,
    sub_title as subTitle,
    content,
    create_user as createUser,
    update_user as updateUser,
    update_time as updateTime,
    create_time as createTime,
    type,
    child_type as childType,
    article_url as articleUrl,
    title_url as titleUrl,
    coin
FROM
    manage_documents
        <where>
            del = false
            <choose>
                <when test="type != null">
                    AND type = #{type}
                    <if test="keyword != null">
                        AND (<include refid="keywordSearch"/>)
                    </if>
                </when>
                <when test="keyword != null">
                    AND (<include refid="keywordSearch"/>)
                </when>
            </choose>

        </where>
    ORDER BY id DESC
    </select>


    <sql id="keywordSearch">
        title like CONCAT('%',#{keyword},'%')
        OR sub_title like CONCAT('%',#{keyword},'%')
        OR content like CONCAT('%',#{keyword},'%')
    </sql>


    <sql id="keywordSearchEn">
        title_en like CONCAT('%',#{keyword},'%')
        OR sub_title_en like CONCAT('%',#{keyword},'%')
        OR content_en like CONCAT('%',#{keyword},'%')
    </sql>


    <select id="getListDataEnByPage" resultType="com.m2pool.manage.dto.ManageDocumentDto">
        SELECT
        id,
        title_en as `title`,
        sub_title_en as subTitle,
        content_en as content,
        create_user as createUser,
        update_user as updateUser,
        update_time as updateTime,
        create_time as createTime,
        type,
        child_type as childType,
        article_url as articleUrl,
        title_url as titleUrl,
        coin
        FROM
        manage_documents
        <where>
            del = false
            <choose>
                <when test="type != null">
                    AND type = #{type}
                    <if test="keyword != null">
                        AND (<include refid="keywordSearchEn"/>)
                    </if>
                </when>
                <when test="keyword != null">
                    AND (<include refid="keywordSearchEn"/>)
                </when>
            </choose>

        </where>
        ORDER BY id DESC
    </select>
    <select id="findCatalogueList" resultType="com.m2pool.manage.dto.ManageCatalogueDto">
        select
            type,
            case when #{lang} = 'zh' then description else description_en end as description
        from
            manage_documents_type
        where del = false

    </select>
    <select id="findCatalogueDocumentList" resultType="com.m2pool.manage.dto.ManageCatalogueDocumentDto">
        select
            id,
            case when #{lang} = 'zh' then title else title_en end as title,
            case when #{lang} = 'zh' then sub_title else sub_title_en end as subTitle,
            create_user as createUser,
            update_user as updateUser,
            update_time as updateTime,
            create_time as createTime,
            type,
            child_type as childType,
            article_url as articleUrl,
            title_url as titleUrl,
            coin
        from
            manage_documents
        <where>
            del = false and child_type = #{childType}
            <if test="type != null">
                AND type = #{type}
            </if>
        </where>


        order by id desc
    </select>
</mapper>
