<?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="jnpf.memberLog.mapper.WorkLogMapper">

    <!-- 新增日志评论 -->
    <insert id="addCommentReply">
        insert into ftb_log_comment (F_Id, F_Member_Log_Id, F_Content, F_Pic, F_File, F_Pid, F_Path, F_Root_Id, F_CreatorUserId, F_LastModifyUserId)
        values (#{id}, #{memberLogId}, #{content}, #{pic}, #{file}, #{pid}, #{path}, #{rootId}, #{creatorUserId}, #{lastModifyUserId})
    </insert>
    <!-- 新增点赞记录 -->
    <insert id="addAssociationRecord">
        insert into ftb_log_member_log_association (F_Id, F_Member_Log_Id, F_User_Id, F_User_Type, F_Type, F_Read)
        values (#{associationId}, #{logId}, #{userId}, #{userType}, #{type}, #{read})
    </insert>
    <!-- 批量新增转发信息 -->
    <insert id="addAssociationRecordBatch">
        insert into ftb_log_member_log_association (F_Id, F_Member_Log_Id, F_User_Id, F_User_Type, F_Type, F_Read)
        values
        <foreach collection="list" index="index" item="item" separator=",">
            (#{item.associationId}, #{item.logId}, #{item.userId}, #{item.userType}, #{item.type}, #{item.read})
        </foreach>
    </insert>
    <!-- 删除点赞记录 -->
    <delete id="deleteLikeRecord">
        delete from ftb_log_member_log_association where F_Id = #{associationId}
    </delete>
    <!-- 删除回复/评论 -->
    <delete id="deleteLogCommentByPath">
        update ftb_log_comment
        set
            F_EnabledMark = 1,
            F_DeleteTime = now(),
            F_DeleteUserId = #{userId}
        where find_in_set(#{commentId}, F_Path)
    </delete>
    <!-- 删除评论, 不删下级 -->
    <delete id="deleteLogCommentById">
        update ftb_log_comment
        set
            F_EnabledMark = 1,
            F_DeleteTime = now(),
            F_DeleteUserId = #{userId}
        where F_Id = #{commentId}
    </delete>
    <!-- 查询日志主表 -->
    <select id="getLog" resultType="jnpf.model.memberLog.vo.MiniMemberLogVo">
        select F_Id logId, F_Log_Template_Id logTemplateId, F_Title title, F_Log_Type logType, F_Pic pic, F_File file, F_CreatorUserId creatorUserId
        from ftb_log_member_log
        where F_Id = #{logId}
    </select>
    <!-- 查询评论列表 -->
    <select id="getCommentList" resultType="jnpf.model.memberLog.vo.LogCommentVo">
        select F_Id commentId, F_Pid pid, F_Root_Id rootId, F_Content content, F_Pic pic, F_File file, F_CreatorTime creatorTime, F_CreatorUserId creatorUserId,
        (select count(*) from ftb_log_comment where F_Root_Id = lc.F_Id) childNum,
        (select F_CreatorUserId from ftb_log_comment where F_Id = lc.F_Pid) replyUserId
        from ftb_log_comment lc
        where F_Member_Log_Id = #{logId} and F_Root_Id = #{commentId} and F_EnabledMark = 0
        order by F_CreatorTime desc
    </select>
    <!-- 查询日志评论 -->
    <select id="getLogComment" resultType="jnpf.model.memberLog.po.LogComment">
        select F_Id id, F_Member_Log_Id memberLogId, F_Content content, F_Pic pic, F_File file, F_Pid pid, F_Path `path`, F_Root_Id rootId,
               F_CreatorTime creatorTime, F_CreatorUserId creatorUserId, F_LastModifyTime lastModifyTime, F_LastModifyUserId lastModifyUserId
        from ftb_log_comment
        where F_Id = #{id}
    </select>
    <!-- 查询成员日志信息关联 -->
    <select id="getLogAssociation" resultType="jnpf.model.memberLog.vo.LogAssociationVo">
        select F_Id associationId, F_Member_Log_Id logId, F_User_Id userId, F_User_Type userType, F_Type `type`, F_Read `read`
        from ftb_log_member_log_association
        where F_Member_Log_Id = #{logId} and F_User_Id = #{userId} and F_Type = #{type}
    </select>
    <!-- 根据日期统计用户写日志数量 -->
    <select id="getLogDataStat" resultType="jnpf.model.memberLog.vo.LogDataVo">
        select t.creatorTime, t.creatorUserId, count(*) num
        from (
          select DATE_FORMAT(F_CreatorTime, '%Y-%m-%d') creatorTime, F_CreatorUserId creatorUserId
          from ftb_log_member_log
          where
          <![CDATA[
            DATE_FORMAT(F_CreatorTime, '%Y-%m-%d') >= DATE_FORMAT(#{queryDto.beginDate}, '%Y-%m-%d')
            and DATE_FORMAT(F_CreatorTime, '%Y-%m-%d') <= DATE_FORMAT(#{queryDto.endDate}, '%Y-%m-%d')
          ]]>
          <if test='"0" != queryDto.organizeId and "" != queryDto.organizeId'>
              and F_Organize_Id = #{queryDto.organizeId}
          </if>
          <if test='"0" != queryDto.positionId and "" != queryDto.positionId'>
              and F_Position_Id = #{queryDto.positionId}
          </if>
          <if test="'全部' != queryDto.queryType">
              and F_Log_Type = #{queryDto.queryType}
          </if>
          <if test="null != userIds and userIds.size > 0">
              and F_CreatorUserId in
              <foreach collection="userIds" index="index" item="item" open="(" separator="," close=")">
                  #{item}
              </foreach>
          </if>
        ) t
        group by t.creatorTime, t.creatorUserId
        order by t.creatorTime asc
    </select>
    <!-- 查询与我相关的全部日志列表 -->
    <select id="getAllLogList" resultType="jnpf.model.memberLog.vo.MiniLogVo">
        select t.logId, t.title, t.organizeName, t.positionName, t.userId, t.creatorTime, t.readNum, t.enabledMark from (
             <include refid="myLogList"/> and lml.F_EnabledMark = 0
             union
             select lml.F_Id logId, lml.F_Title title, lml.F_Organize_Name organizeName, lml.F_Position_Name positionName, lml.F_CreatorUserId userId, lml.F_CreatorTime creatorTime, lml.F_EnabledMark enabledMark,
            (
                select concat_ws(
                   '/',
                   (select count(distinct F_User_Id) from ftb_log_member_log_association where F_Member_Log_Id = lml.F_Id and F_User_Type = 1 and F_Type in (1, 2) and F_Read = 1),
                   (select count(distinct F_User_Id) from ftb_log_member_log_association where F_Member_Log_Id = lml.F_Id and F_User_Type = 1 and F_Type in (1, 2))
               )
            ) readNum
             from ftb_log_member_log lml where lml.F_Id in (
                 select F_Member_Log_Id from ftb_log_member_log_association where F_User_Id = #{userId} and F_User_Type = 1 and F_Type in (1, 2)
             ) and lml.F_EnabledMark = 0
        ) t where 1 = 1
        <include refid="dateQuery"/>
        order by t.creatorTime desc
    </select>
    <!-- 我发布的日志列表 -->
    <select id="getMyLogList" resultType="jnpf.model.memberLog.vo.MiniLogVo">
        select t.logId, t.title, t.organizeName, t.positionName, t.userId, t.creatorTime, t.readNum, t.enabledMark from (
            <include refid="myLogList"/> and lml.F_EnabledMark = 0
        ) t where 1 = 1
        <include refid="dateQuery"/>
        order by t.creatorTime desc
    </select>
    <!-- 查询与我部门相关的日志列表 -->
    <select id="getMyOrganizeLogList" resultType="jnpf.model.memberLog.vo.MiniLogVo">
        select t.logId, t.title, t.organizeName, t.positionName, t.userId, t.creatorTime, t.readNum, t.enabledMark from (
            select lml.F_Id logId, lml.F_Title title, lml.F_Organize_Name organizeName, lml.F_Position_Name positionName, lml.F_CreatorUserId userId, lml.F_CreatorTime creatorTime, lml.F_EnabledMark enabledMark,
            (
               select concat_ws(
                  '/',
                  (select count(distinct F_User_Id) from ftb_log_member_log_association where F_Member_Log_Id = lml.F_Id and F_User_Type = 1 and F_Type in (1, 2) and F_Read = 1),
                  (select count(distinct F_User_Id) from ftb_log_member_log_association where F_Member_Log_Id = lml.F_Id and F_User_Type = 1 and F_Type in (1, 2))
              )
             ) readNum
            from ftb_log_member_log lml where lml.F_CreatorUserId = #{userId} and lml.F_EnabledMark = 0 and lml.F_Id in (
                select F_Member_Log_Id from ftb_log_member_log_association where F_User_Type = 1 and F_Type in (1, 2) and F_User_Id in
                <foreach collection="orgUserIds" index="index" item="item" open="(" separator="," close=")">
                    #{item}
                </foreach>
            )
            union
            select lml.F_Id logId, lml.F_Title title, lml.F_Organize_Name organizeName, lml.F_Position_Name positionName, lml.F_CreatorUserId userId, lml.F_CreatorTime creatorTime, lml.F_EnabledMark enabledMark,
            (
               select concat_ws(
                  '/',
                  (select count(distinct F_User_Id) from ftb_log_member_log_association where F_Member_Log_Id = lml.F_Id and F_User_Type = 1 and F_Type in (1, 2) and F_Read = 1),
                  (select count(distinct F_User_Id) from ftb_log_member_log_association where F_Member_Log_Id = lml.F_Id and F_User_Type = 1 and F_Type in (1, 2))
              )
             ) readNum
            from ftb_log_member_log lml where lml.F_EnabledMark = 0 and lml.F_CreatorUserId in
            <foreach collection="orgUserIds" index="index" item="item" open="(" separator="," close=")">
                #{item}
            </foreach>
            and lml.F_Id in (
                select F_Member_Log_Id from ftb_log_member_log_association where F_User_Type = 1 and F_Type in (1, 2) and F_User_Id = #{userId}
            )
        ) t where 1 = 1
        <include refid="dateQuery"/>
        order by t.creatorTime desc
    </select>
    <!-- 可查看的字段列表 -->
    <select id="getViewableFieldList" resultType="jnpf.model.memberLog.vo.LogMemberFieldVo">
        select lmf.F_Id id, lmf.F_Member_Log_Id memberLogId, lmf.F_Field_Id fieldId, lmf.F_Field field, lmf.f_Code `code`, lmf.F_Sort sort, lmf.F_Content content
        from ftb_log_member_field lmf
        left join ftb_log_template_field ltf on lmf.F_Field_Id = ltf.F_Id
        where ltf.F_List_Display = 1 and lmf.F_Member_Log_Id in
        <foreach collection="logIds" index="index" item="item" open="(" separator="," close=")">
            #{item}
        </foreach>
    </select>
    <!-- 查询日志列表 -->
    <select id="getWebLogList" resultType="jnpf.model.memberLog.vo.WebLogVo">
        select t.logId, t.logType, t.title, t.organizeName, t.positionName, t.creatorUserId, t.creatorTime, t.enabledMark, t.replyNum, t.likeNum from (
             select lml.F_Id logId, lml.F_Log_Type logType, lml.F_Title title, lml.F_Organize_Name organizeName, lml.F_Position_Name positionName,
                lml.F_CreatorUserId creatorUserId, lml.F_CreatorTime creatorTime, lml.F_EnabledMark enabledMark,
                (select count(*) from ftb_log_comment where F_Member_Log_Id = lml.F_Id and F_EnabledMark = 0) replyNum,
                (select count(*) from ftb_log_member_log_association where F_Member_Log_Id = lml.F_Id and F_User_Type = 1 and F_Type = 4) likeNum,
                (
                    select group_concat(lmf.F_Content) from ftb_log_member_field lmf
                    left join ftb_log_template_field ltf on lmf.F_Field_Id = ltf.F_Id
                    where ltf.F_List_Display = 1 and lmf.F_Member_Log_Id = lml.F_Id
                ) content
             from ftb_log_member_log lml
         ) t
        where 1 = 1
        <include refid="dateQuery"/>
        <if test="'全部' != queryType">
            and t.logType = #{queryType}
        </if>
        <if test="null != content and '' != content">
            and instr(ifnull(t.content,""), #{content}) > 0
        </if>
        <if test="null != userIds and userIds.size > 0">
            and t.creatorUserId in
            <foreach collection="userIds" index="index" item="item" open="(" separator="," close=")">
                #{item}
            </foreach>
        </if>
        order by t.creatorTime desc
    </select>
    <select id="getWebLogListByAuth" resultType="jnpf.model.memberLog.vo.WebLogVo">
        select t.logId, t.logType, t.title, t.organizeName, t.positionName, t.creatorUserId, t.creatorTime, t.enabledMark, t.replyNum, t.likeNum from (
            select lml.F_Id logId, lml.F_Log_Type logType, lml.F_Title title, lml.F_Organize_Name organizeName, lml.F_Position_Name positionName,
                 lml.F_CreatorUserId creatorUserId, lml.F_CreatorTime creatorTime, lml.F_EnabledMark enabledMark,
                 (select count(*) from ftb_log_comment where F_Member_Log_Id = lml.F_Id and F_EnabledMark = 0) replyNum,
                 (select count(*) from ftb_log_member_log_association where F_Member_Log_Id = lml.F_Id and F_User_Type = 1 and F_Type = 4) likeNum,
                 (
                     select group_concat(lmf.F_Content) from ftb_log_member_field lmf
                     left join ftb_log_template_field ltf on lmf.F_Field_Id = ltf.F_Id
                     where ltf.F_List_Display = 1 and lmf.F_Member_Log_Id = lml.F_Id
                 ) content
            from ftb_log_member_log lml
        <where>
        <if test="userIds != null and userIds.size() > 0" >
            lml.F_CreatorUserId in
            <foreach collection="userIds" index="index" item="item" open="(" separator="," close=")">
                #{item}
            </foreach>

        </if>
        </where>
          ) t
        where 1 = 1
        <include refid="dateQuery"/>
        <if test="'全部' != queryType">
            and t.logType = #{queryType}
        </if>
        <if test="null != content and '' != content">
            and t.content  LIKE CONCAT('%', #{content}, '%')
        </if>
        order by t.creatorTime desc
    </select>
    <!-- 根据条件查询日志列表 -->
    <select id="getLogListByCondition" resultType="jnpf.model.memberLog.vo.MiniLogVo">
        select t.logId, t.title, t.organizeName, t.positionName, t.userId, t.creatorTime, t.readNum, t.enabledMark, t.content from (
           select lml.F_Id logId, lml.F_Title title, lml.F_Organize_Name organizeName, lml.F_Position_Name positionName,
           lml.F_CreatorUserId userId, lml.F_CreatorTime creatorTime, lml.F_EnabledMark enabledMark,
           (
               select group_concat(lmf.F_Content) from ftb_log_member_field lmf
               left join ftb_log_template_field ltf on lmf.F_Field_Id = ltf.F_Id
               where ltf.F_List_Display = 1 and lmf.F_Member_Log_Id = lml.F_Id
           ) content,
           (
               select concat_ws(
                  '/',
                  (select count(distinct F_User_Id) from ftb_log_member_log_association where F_Member_Log_Id = lml.F_Id and F_User_Type = 1 and F_Type in (1, 2) and F_Read = 1),
                  (select count(distinct F_User_Id) from ftb_log_member_log_association where F_Member_Log_Id = lml.F_Id and F_User_Type = 1 and F_Type in (1, 2))
              )
           ) readNum
           from ftb_log_member_log lml where lml.F_CreatorUserId = #{userId} and lml.F_EnabledMark = 0
           union
           select lml.F_Id logId, lml.F_Title title, lml.F_Organize_Name organizeName, lml.F_Position_Name positionName,
           lml.F_CreatorUserId userId, lml.F_CreatorTime creatorTime, lml.F_EnabledMark enabledMark,
           (
               select group_concat(lmf.F_Content) from ftb_log_member_field lmf
               left join ftb_log_template_field ltf on lmf.F_Field_Id = ltf.F_Id
               where ltf.F_List_Display = 1 and lmf.F_Member_Log_Id = lml.F_Id
           ) content,
           (
               select concat_ws(
                  '/',
                  (select count(distinct F_User_Id) from ftb_log_member_log_association where F_Member_Log_Id = lml.F_Id and F_User_Type = 1 and F_Type in (1, 2) and F_Read = 1),
                  (select count(distinct F_User_Id) from ftb_log_member_log_association where F_Member_Log_Id = lml.F_Id and F_User_Type = 1 and F_Type in (1, 2))
              )
           ) readNum
           from ftb_log_member_log lml where lml.F_Id in (
               select F_Member_Log_Id from ftb_log_member_log_association where F_User_Id = #{userId} and F_User_Type = 1 and F_Type in (1, 2)
           ) and lml.F_EnabledMark = 0
        ) t where 1 = 1
        <if test="null != content and '' != content">
            and t.content  LIKE CONCAT('%', #{content}, '%')
        </if>
        <if test="userIds != null and userIds.size() > 0" >
            and t.userId in
            <foreach collection="userIds" index="index" item="item" open="(" separator="," close=")">
                #{item}
            </foreach>
        </if>
        order by t.creatorTime desc
    </select>
    <!-- 日志列表 - 指定人员查询 -->
    <select id="getLogListByUser" resultType="jnpf.model.memberLog.vo.MiniLogVo">
        select * from (
            <include refid="myLogList"/>
        ) t where 1 = 1
        <include refid="dateQuery"/>
    </select>
    <!-- 查询评论/回复详情 -->
    <select id="getLogCommentVo" resultType="jnpf.model.memberLog.vo.LogCommentVo">
        select F_Id commentId, F_Pid pid, F_Path `path`, F_Root_Id rootId, F_Content content, F_Pic pic, F_File file, F_CreatorTime creatorTime, F_CreatorUserId creatorUserId,
        (select count(*) from ftb_log_comment where F_Root_Id = lc.F_Id) childNum,
        (select F_CreatorUserId from ftb_log_comment where F_Id = lc.F_Pid) replyUserId
        from ftb_log_comment lc
        where F_Id = #{id}
    </select>
    <!-- 评论总条数 -->
    <select id="getCommentTotalNum" resultType="java.lang.Integer">
        select count(*) from ftb_log_comment where F_Member_Log_Id = #{logId} and F_EnabledMark = 0
    </select>
    <sql id="myLogList">
        select lml.F_Id logId, lml.F_Title title, lml.F_Organize_Name organizeName, lml.F_Position_Name positionName, lml.F_CreatorUserId userId, lml.F_CreatorTime creatorTime, lml.F_EnabledMark enabledMark,
       (
           select concat_ws(
              '/',
              (select count(distinct F_User_Id) from ftb_log_member_log_association where F_Member_Log_Id = lml.F_Id and F_User_Type = 1 and F_Type in (1, 2) and F_Read = 1),
              (select count(distinct F_User_Id) from ftb_log_member_log_association where F_Member_Log_Id = lml.F_Id and F_User_Type = 1 and F_Type in (1, 2))
          )
       ) readNum
        from ftb_log_member_log lml where lml.F_CreatorUserId = #{userId}
    </sql>
    <sql id="dateQuery">
        <if test="null != beginDate and '' != beginDate">
            <![CDATA[
                and DATE_FORMAT(t.creatorTime, '%Y-%m-%d') >= DATE_FORMAT(#{beginDate}, '%Y-%m-%d')
            ]]>
        </if>
        <if test="null != endDate and '' != endDate">
            <![CDATA[
                and DATE_FORMAT(t.creatorTime, '%Y-%m-%d') <= DATE_FORMAT(#{endDate}, '%Y-%m-%d')
            ]]>
        </if>
    </sql>
</mapper>