<?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.ruoyi.generator.mapper.community.CommunityArticleMapper">

    <resultMap type="com.ruoyi.generator.vo.CommunityArticleVo" id="CommunityArticleVoResult">
        <id property="id" column="article_id"/>
        <result property="userId" column="article_user_id"/>
        <result property="title" column="article_title"/>
        <result property="content" column="article_content"/>
        <result property="createBy" column="article_create_by"/>
        <result property="createTime" column="article_create_time"/>
        <result property="updateBy" column="article_update_by"/>
        <result property="updateTime" column="article_update_time"/>
        <result property="remark" column="remark"/>
        <result property="userName" column="user_name"/>
        <result property="nickName" column="nick_name"/>
        <result property="email" column="email"/>
        <result property="avatar" column="avatar"/>
        <result property="pageViews" column="pageViews"/>
        <result column="images" property="imageList" javaType="java.util.List" typeHandler="com.ruoyi.common.utils.bean.JSONTypeHandler"/>
        <result column="comments" property="comments" typeHandler="com.ruoyi.common.utils.bean.JSONTypeHandler"/>
    </resultMap>

    <resultMap type="CommunityArticleImages" id="CommunityArticleImagesResult">
        <id property="id" column="image_id"/>
        <result property="articleId" column="images_article_id"/>
        <result property="imageUrl" column="image_url"/>
        <result property="createBy" column="images_create_by"/>
        <result property="createTime" column="images_create_time"/>
        <result property="updateBy" column="images_update_by"/>
        <result property="updateTime" column="images_update_time"/>
    </resultMap>

    <resultMap type="SysUser" id="SysUserResult">
        <id property="userId" column="user_id"/>
        <result property="deptId" column="dept_id"/>
        <result property="phonenumber" column="phonenumber"/>
        <result property="sex" column="sex"/>
        <result property="password" column="password"/>
        <result property="userName" column="user_name"/>
        <result property="nickName" column="nick_name"/>
        <result property="email" column="email"/>
        <result property="avatar" column="avatar"/>
        <result property="status" column="status"/>
        <result property="createBy" column="create_by"/>
        <result property="createTime" column="create_time"/>
        <result property="updateBy" column="update_by"/>
        <result property="updateTime" column="update_time"/>
        <result property="remark" column="remark"/>
    </resultMap>

    <resultMap type="com.ruoyi.generator.vo.CommunityArticleCommentVo" id="CommunityCommentsResult">
        <id property="id" column="comment_id"/>
        <result property="articleId" column="comment_article_id"/>
        <result property="userId" column="comment_user_id"/>
        <result property="content" column="comment_content"/>
        <result property="createBy" column="comment_create_by"/>
        <result property="createTime" column="comment_create_time"/>
        <result property="updateBy" column="comment_update_by"/>
        <result property="updateTime" column="comment_update_time"/>
        <result property="remark" column="comment_remark"/>
    </resultMap>

    <resultMap id="CommunityCollectResult" type="CommunityArticleCollect">
        <id property="id" column="collect_id"/>
        <result property="articleId" column="article_id"/>
        <result property="userId" column="user_id"/>
        <result property="createBy" column="create_by"/>
        <result property="createTime" column="create_time"/>
        <result property="updateBy" column="update_by"/>
        <result property="updateTime" column="update_time"/>
    </resultMap>

    <resultMap id="CommunityLikeResult" type="CommunityLike">
        <id property="id" column="like_id"/>
        <result property="articleId" column="article_id"/>
        <result property="userId" column="user_id"/>
        <result property="createBy" column="create_by"/>
        <result property="createTime" column="create_time"/>
        <result property="updateBy" column="update_by"/>
        <result property="updateTime" column="update_time"/>
    </resultMap>

    <sql id="selectCommunityArticleVo">
        select id as article_id,
               user_id,
               title,
               content,
               create_by,
               create_time,
               update_by,
               update_time,
               remark
        from community_article
    </sql>

    <select id="selectCommunityArticleList" parameterType="CommunityArticle" resultMap="CommunityArticleVoResult">
        select
        a.id as article_id,
        a.user_id as article_user_id,
        a.title as article_title,
        a.is_comment as article_is_comment,
        a.content as article_content,
        a.page_views as pageViews,
        a.create_by as article_create_by,
        a.create_time as article_create_time,
        a.update_by as article_update_by,
        a.update_time as article_update_time,
        COALESCE((
        SELECT
        JSON_ARRAYAGG(
        CASE
        WHEN b.id IS NOT NULL THEN
        JSON_OBJECT(
        'id', IFNULL(b.id,''),
        'articleId', IFNULL(b.article_id,''),
        'imageUrl', IFNULL(b.image_url,''),
        'createBy', IFNULL(b.create_by,''),
        'createTime', IFNULL(DATE_FORMAT(b.create_time,'%Y-%m-%d %H:%i:%s'),''),
        'updateBy', IFNULL(b.update_by,''),
        'updateTime', IFNULL(DATE_FORMAT(b.update_time,'%Y-%m-%d %H:%i:%s'),'')
        )
        ELSE NULL
        END
        )
        FROM
        community_article_images b
        WHERE
        b.article_id = a.id
        ), '[]') AS images,
        c.user_name as user_name,
        c.nick_name as nick_name,
        c.email as email,
        c.avatar as avatar
        # COALESCE((
        # SELECT
        # JSON_ARRAYAGG(
        # CASE
        # WHEN d.id IS NOT NULL THEN
        # JSON_OBJECT(
        # 'id', IFNULL(d.id,''),
        # 'articleId', IFNULL(d.article_id,''),
        # 'userId', IFNULL(d.user_id,''),
        # 'content', IFNULL(d.content,''),
        # 'createBy', IFNULL(d.create_by,''),
        # 'createTime', IFNULL(DATE_FORMAT(d.create_time,'%Y-%m-%d %H:%i:%s'),''),
        # 'updateBy', IFNULL(d.update_by,''),
        # 'updateTime', IFNULL(DATE_FORMAT(d.update_time,'%Y-%m-%d %H:%i:%s'),'')
        # )
        # ELSE NULL
        # END
        # )
        # FROM
        # community_article_comment d
        # WHERE
        # d.article_id = a.id and d.is_delete != 1 order by d.create_time desc
        # ), '[]') AS comments
        from
        community_article a
        left join sys_user c on a.user_id = c.user_id
        left join community_article_circle e on e.article_id = a.id
        left join community_article_tag f on f.article_id = a.id
        <where>
            <if test="communityArticle.id != null and communityArticle.id != ''">
                AND a.id = #{communityArticle.id}
            </if>
            <if test="communityArticle.title != null and communityArticle.title != ''">
                AND a.title like concat('%', #{communityArticle.title}, '%')
            </if>
            <if test="circleIds != null and circleIds.size() > 0">
                and e.circle_id IN
                <foreach collection="circleIds" item="circleId" index="index" open="(" close=")" separator=",">
                    #{circleId}
                </foreach>
            </if>
            <if test="communityArticle.userId != null and communityArticle.userId != ''">
                AND a.user_id = #{communityArticle.userId}
            </if>
            <if test="communityArticle.userIds != null and communityArticle.userIds.size > 0">
                AND a.user_id in
                <foreach collection="communityArticle.userIds" item="userId" index="index" open="(" close=")" separator=",">
                    #{userId}
                </foreach>
            </if>
            and (is_delete != 1 or is_delete is null)
        </where>
        group by a.id,a.create_time
        <if test="searchType == 1">
            order by a.create_time desc
        </if>

        <if test="searchType == 2">
            order by a.page_views desc
        </if>
        limit #{offset},#{limit}
    </select>

    <select id="selectCommunityArticleCount" parameterType="CommunityArticle" resultType="int">
        SELECT COUNT(DISTINCT a.id)
        FROM community_article a
        LEFT JOIN sys_user c ON a.user_id = c.user_id
        LEFT JOIN community_article_circle e ON e.article_id = a.id
        LEFT JOIN community_article_tag f ON f.article_id = a.id
        <where>
            <if test="communityArticle.id != null and communityArticle.id != ''">
                AND a.id = #{communityArticle.id}
            </if>
            <if test="communityArticle.title != null and communityArticle.title != ''">
                AND a.title LIKE CONCAT('%', #{communityArticle.title}, '%')
            </if>
            <if test="circleIds != null and circleIds.size() > 0">
                AND e.circle_id IN
                <foreach collection="circleIds" item="circleId" open="(" close=")" separator=",">
                    #{circleId}
                </foreach>
            </if>
            <if test="communityArticle.userId != null and communityArticle.userId != ''">
                AND a.user_id = #{communityArticle.userId}
            </if>
            <if test="communityArticle.userIds != null and communityArticle.userIds.size > 0">
                AND a.user_id IN
                <foreach collection="communityArticle.userIds" item="userId" open="(" close=")" separator=",">
                    #{userId}
                </foreach>
            </if>
            AND (a.is_delete != 1 OR a.is_delete IS NULL)
        </where>
    </select>


    <select id="selectCommunityArticleCollectById" resultMap="CommunityCollectResult">
        select collect_id, article_id, user_id, create_by, create_time, update_by, update_time from
        community_article_collect
        <where>
            <if test="userId != null and userId != ''">
                user_id = #{userId}
            </if>
            <if test="articleId != null and articleId != ''">
                and article_id = #{articleId}
            </if>
        </where>
    </select>

    <!--点赞-->
    <select id="selectCommunityArticleLikeById" resultMap="CommunityLikeResult">
        select id as like_id, article_id, user_id, create_by, create_time, update_by, update_time from
        community_like
        <where>
            <if test="userId != null and userId != ''">
                user_id = #{userId}
            </if>
            <if test="articleId != null and articleId != ''">
                and article_id = #{articleId}
            </if>
        </where>
    </select>

    <insert id="insertCommunityLikeById" parameterType="communityLike">
        insert into community_like (article_id, user_id, create_by, create_time, update_by, update_time)
        values (#{articleId}, #{userId}, #{userId}, now(), #{userId}, now());
    </insert>

    <delete id="deleteCommunityLikeById" parameterType="communityLike">
        delete
        from community_like
        where user_id = #{userId}
          and article_id = #{articleId};
    </delete>

    <!--收藏-->
    <select id="selectCommunityCollectById" resultMap="CommunityCollectResult" parameterType="communityArticleCollect">
        select id as collect_id, article_id, user_id, create_by, create_time, update_by, update_time from
        community_article_collect
        <where>
            <if test="userId != null and userId != ''">
                user_id = #{userId}
            </if>
            <if test="articleId != null and articleId != ''">
                and article_id = #{articleId}
            </if>
        </where>
    </select>

    <insert id="insertCommunityCollectById" parameterType="communityArticleCollect">
        insert into community_article_collect (article_id, user_id, create_by, create_time, update_by, update_time)
        values (#{articleId}, #{userId}, #{userId}, now(), #{userId}, now());
    </insert>

    <delete id="deleteCommunityCollectById" parameterType="communityArticleCollect">
        delete
        from community_article_collect
        where user_id = #{userId}
          and article_id = #{articleId};
    </delete>
</mapper>