<?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.hanserwei.hannote.search.biz.domain.mapper.SelectMapper">

    <select id="selectEsNoteIndexData" resultType="map" parameterType="map">
        select n.id,
               n.title,
               n.topic_name                                    as topic,
               n.type,
               SUBSTRING_INDEX(n.img_uris, ',', 1)             AS cover,
               DATE_FORMAT(n.create_time, '%Y-%m-%d %H:%i:%s') AS create_time,
               DATE_FORMAT(n.update_time, '%Y-%m-%d %H:%i:%s') AS update_time,
               u.nickname                                      AS creator_nickname,
               u.avatar                                        AS creator_avatar,
               IFNULL(nc.like_total, 0)                        as like_total,
               IFNULL(nc.collect_total, 0)                     as collect_total,
               IFNULL(nc.comment_total, 0)                     as comment_total
        from t_note n
                 left join t_user u on n.creator_id = u.id
                 left join t_note_count nc on n.id = nc.note_id
        where n.visible = 0
          and n.`status` = 1
        <if test="noteId != null">
            and n.id = #{noteId}
        </if>
        <if test="userId != null">
            and u.id = #{userId}
        </if>
        <if test="noteId != null">
            limit 1
        </if>
    </select>

    <select id="selectEsUserIndexData" resultType="map" parameterType="map">
        select u.id,
               u.nickname,
               u.avatar,
               u.han_note_id,
               IFNULL(uc.note_total, 0) as note_total,
               IFNULL(uc.fans_total, 0) as fans_total
        from t_user u
                 left join t_user_count uc on u.id = uc.user_id
        where u.`status` = 0
          and u.is_deleted = 0
        <if test="userId != null">
            and u.id = #{userId}
        </if>
    </select>
</mapper>