<?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.iocoder.yudao.module.system.dal.mysql.comment.WorkLogCommentMapper">

    <!--
        一般情况下，尽可能使用 Mapper 进行 CRUD 增删改查即可。
        无法满足的场景，例如说多表关联查询，才使用 XML 编写 SQL。
        代码生成器暂时只生成 Mapper XML 文件本身，更多推荐 MybatisX 快速开发插件来生成查询。
        文档可见：https://www.iocoder.cn/MyBatis/x-plugins/
     -->

    <select id="queryCommentPageList"
            resultType="cn.iocoder.yudao.module.system.controller.app.comment.vo.CommentPageListVO">
        SELECT a.id,
        a.work_log_id AS workLogId,
        a.user_id AS userId,
        c.avatar AS avatar,
        c.nickname AS userName,
        a.comment_user_id AS commentUserId,
        d.avatar AS commentAvatar,
        d.nickname AS commentUserName,
        a.COMMENT AS COMMENT,
        a.type AS type,
        e.COMMENT AS replyComment,
        <if test="dto.isShowWorkLogContent != null and dto.isShowWorkLogContent == 1">
            b.name as workLogName,
            b.form_variables AS workLogContent,
            b.form_id as workFormId,
            b.create_time as workLogTime,
        </if>
        a.create_time AS createTime
        FROM work_log_comment AS a
        <if test="dto.isShowWorkLogContent != null and dto.isShowWorkLogContent == 1">
            LEFT JOIN work_log_instance_ext AS b ON a.work_log_id = b.id
        </if>
        LEFT JOIN system_users AS c ON a.user_id = c.id
        LEFT JOIN system_users AS d ON a.comment_user_id = d.id
        LEFT JOIN work_log_comment AS e ON a.comment_id = e.id
        <if test="dto.isCommentOnMyBlog != null and dto.isCommentOnMyBlog == 1">
            LEFT JOIN work_log_instance_ext AS f ON f.id = a.work_log_id
        </if>
        <where>
            <if test="dto.workLogId != null">
                and a.work_log_id = #{dto.workLogId}
            </if>
            <if test="dto.type != null">
                and a.type = #{dto.type}
            </if>
            <if test="dto.isCommentOnMyBlog != null and dto.isCommentOnMyBlog == 1">
                <if test="dto.userId != null">
                    AND f.start_user_id = #{dto.userId}
                </if>
            </if>
            <if test="dto.isCommentOnMyBlog == null or dto.isCommentOnMyBlog == 0">
                <if test="dto.userId != null">
                    and (a.user_id = #{dto.userId} or a.comment_user_id = #{dto.userId})
                </if>
            </if>
        </where>
        ORDER BY a.create_time DESC
    </select>
    <select id="selectCountReceiveNum" resultType="java.lang.Integer">
        SELECT count(a.id)
        FROM work_log_comment AS a
                 LEFT JOIN work_log_instance_ext AS f ON f.id = a.work_log_id
        WHERE a.type = 0
          AND f.start_user_id = #{userId}
    </select>
</mapper>
