<?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.worklog.LogInstanceMapper">

    <!--
        一般情况下，尽可能使用 Mapper 进行 CRUD 增删改查即可。
        无法满足的场景，例如说多表关联查询，才使用 XML 编写 SQL。
        代码生成器暂时只生成 Mapper XML 文件本身，更多推荐 MybatisX 快速开发插件来生成查询。
        文档可见：https://www.iocoder.cn/MyBatis/x-plugins/
     -->
    <select id="selectRaedUser" resultType="cn.iocoder.yudao.module.system.service.worklog.dto.LogReadUserRespDTO">
        SELECT
            DISTINCT result.user_id, result.dept_id
        FROM
        (
        SELECT
        user.id,
        user.dept_id,
        user.nickname,
        read_user.*,
        CASE
        read_user.data_scope
        WHEN 1 THEN
        'all' -- 全部数据权限

        WHEN 2 THEN
        read_user.data_scope_dept_ids --  指定部门数据权限

        WHEN 3 THEN
        user.dept_id --  本部门数据权限

        WHEN 4 THEN
        (
        SELECT
        GROUP_CONCAT( id SEPARATOR ', ' ) AS ids
        FROM
        (
        SELECT
        t1.id,
        t1.NAME,
        IF
        ( find_in_set( parent_id, @pids ) > 0, @pids := concat( @pids, ',', id ), 0 ) AS ischild
        FROM
        ( SELECT id, parent_id, NAME FROM system_dept t ORDER BY parent_id, id ) t1,
        ( SELECT @pids := user.dept_id ) t2
        ) t3
        WHERE
        ischild != 0
        OR id = user.dept_id
        ) -- 	read_user.data_scope_dept_ids   -- 本部门及以下数据权限

        END AS ids
        FROM
        system_users user
        LEFT JOIN (
        SELECT
        u_role.user_id,
        u_role.role_id,
        role_id.data_scope,
        role_id.data_scope_dept_ids
        FROM
        system_user_role u_role
        LEFT JOIN (
        SELECT
        role.id, role.data_scope, role.data_scope_dept_ids
        FROM
        (
            SELECT role_id
            FROM system_role_menu a
            LEFT JOIN system_menu role ON a.menu_id = role.id
            WHERE
                role.permission = 'system:view-log:query'
                AND a.deleted = 0
                AND a.role_id NOT IN (1,101,162)
        )  menu
        LEFT JOIN	system_role role on role.id  =  menu.role_id
        ) role_id ON u_role.role_id = role_id.id
        WHERE u_role.deleted = 0
        ORDER BY
        u_role.role_id
        ) read_user ON user.id = read_user.user_id
        WHERE
        user.deleted = 0
        AND user.status = 0
        AND read_user.data_scope != 5
        ) result
        WHERE
        result.user_id != #{userId}
        and
        ( ( LOCATE(#{deptId},result.ids) and result.data_scope = 4 )
        or ( LOCATE(#{deptId},result.ids) and result.data_scope = 2 )
        or ( result.dept_id = #{deptId} and result.data_scope = 3 )
        or  result.data_scope = 1 )
    </select>

    <select id="getMyNextOrUp"
            resultType="cn.iocoder.yudao.module.system.controller.admin.worklog.vo.loginstance.LogInstanceRespVO">
        SELECT
            t.id,
            t.name
        FROM
            work_log_instance_ext AS t
            INNER JOIN (
                SELECT
                    a.time,
                    a.create_time
                FROM
                    work_log_instance_ext a
                WHERE
                    a.id = #{id}
            ) tt ON
                <if test="type == 1">
                    ( t.time &gt; tt.time )
                    OR ( t.time = tt.time  AND tt.create_time &lt; t.create_time )
                </if>
                <if test="type == 0">
                    ( t.time &lt; tt.time )
                    OR ( t.time = tt.time  AND tt.create_time &gt; t.create_time )
                </if>
        <where>
            t.deleted = 0
            AND t.start_user_id = #{userId}
            <if test="reqVO.type != null">
                and t.type = #{reqVO.type}
            </if>
            <if test="reqVO.deptId != null">
                and t.dept_id = #{reqVO.deptId}
            </if>
            <if test="reqVO.startUserId != null">
                and t.start_user_id = #{reqVO.startUserId}
            </if>
            <if test="reqVO.createTime != null and reqVO.createTime.length > 0">
                <if test="reqVO.createTime[0] != null">
                    and t.create_time &gt;= #{reqVO.createTime[0]}
                </if>
                <if test="reqVO.createTime[1] != null">
                    and t.create_time &lt;= #{reqVO.createTime[1]}
                </if>
            </if>
            <if test="reqVO.isBoss != null">
                and t.start_user_id in
                <foreach collection="userIds" item="userId" open="(" close=")" separator=",">
                    #{userId}
                </foreach>
            </if>
        </where>
        <if test="type == 0">
            ORDER BY
                t.time DESC,
                t.create_time DESC
        </if>
        <if test="type == 1">
            ORDER BY
                t.time,
                t.create_time
        </if>
        limit 1
    </select>

    <select id="selectMyPageResult" resultType="cn.iocoder.yudao.module.system.controller.admin.worklog.vo.loginstance.LogInstanceRespVO">
        SELECT
        a.*,
        COALESCE(c.readCount, 0) as readCount,
        COALESCE(d.unreadCount, 0) as unReadCount,
        COALESCE(b.comment, 0) as comment
        FROM
        work_log_instance_ext as a
        LEFT JOIN (SELECT log_instance_id, COUNT(log_instance_id) AS readCount FROM work_log_read where read_status = 1 AND deleted = 0 GROUP BY log_instance_id ) AS c ON a.id = c.log_instance_id
        LEFT JOIN (SELECT log_instance_id, COUNT(log_instance_id) AS unReadCount FROM work_log_read where read_status = 0 AND deleted = 0 GROUP BY log_instance_id ) AS d ON a.id = d.log_instance_id
        LEFT JOIN (SELECT work_log_id, COUNT(work_log_id) AS comment FROM work_log_comment where deleted = 0  GROUP BY work_log_id) AS b ON a.id = b.work_log_id
        <where>
            a.deleted = 0
            and a.start_user_id = #{userId}
            <if test="reqVO.formId != null">
                and a.form_id = #{reqVO.formId}
            </if>
            <if test="reqVO.createTime != null and reqVO.createTime.length > 0">
                <if test="reqVO.createTime[0] != null">
                    and a.time &gt;= #{reqVO.createTime[0]}
                </if>
                <if test="reqVO.createTime[1] != null">
                    and a.time &lt;= #{reqVO.createTime[1]}
                </if>
            </if>
        </where>
        GROUP BY a.id
        ORDER BY
        a.time DESC,
        a.create_time DESC
    </select>
</mapper>