<?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.cyksj.mapper.mini.ArticleMapper">
    <resultMap id="recommendArticle" type="com.cyksj.model.views.ArticleDetailView">
        <id column="id" property="id"></id>
        <collection property="others" ofType="com.cyksj.model.views.ArticleDetailView" column="{id=id}"
                    select="selectOtherRecommendArticles"/>
    </resultMap>

    <resultMap id="recommendArticleBack" type="com.cyksj.model.entity.Article">
        <id column="id" property="id"></id>
        <collection property="otherArticleIds" ofType="java.lang.Long" column="{id=id}"
                    select="selectOtherRecommendArticlesBack"/>
    </resultMap>

    <select id="getArticleDetailById" resultMap="recommendArticle">
        select a.id,
               a.name,
               a.banner,
               a.content,
               a.author,
               a.head_img_url,
               a.img,
               a.dz_num,
               a.col_num,
               a.update_time as releaseTime
        from `article` a
        where id = #{articleId}
          and status is true
          and deleted is true limit 1
    </select>

    <select id="selectOtherRecommendArticles" resultType="com.cyksj.model.views.ArticleDetailView">
        select a.id,
               a.name,
               a.banner,
               a.content,
               a.author,
               a.head_img_url,
               a.img,
               a.dz_num,
               a.col_num
        from `article_recommend` ar
                 left join `article` a on a.id = ar.article_id
        where a.status is true
          and a.deleted is true
          and recommend_id = #{id}
    </select>

    <select id="getArticleDetail" resultMap="recommendArticleBack">
        select *
        from article
        where id = #{articleId}
          and deleted is true limit 1
    </select>

    <select id="selectOtherRecommendArticlesBack" resultType="java.lang.Long">
        select ar.id
        from `article_recommend` ar
                 left join `article` a on a.id = ar.article_id
        where a.status is true
          and a.deleted is true
          and recommend_id = #{id}
    </select>
</mapper>