<?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.LogStatisticsMapper">

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

    <select id="getUnSubmittedUser"
            resultType="cn.iocoder.yudao.module.system.dal.dataobject.user.AdminUserDO">
        SELECT a.id,
               a.dept_id
        FROM system_users AS a
        LEFT JOIN work_log_use AS b ON a.id = b.use_user_id
        WHERE
          b.deleted = 0
          and b.form_id = #{formId}
          AND not EXISTS (SELECT id
                          FROM work_log_instance_ext
                          WHERE form_id = b.form_id
                            AND start_user_id = a.id
                            AND time = #{thisTime})
    </select>
    <select id="getStatistics"
            resultType="cn.iocoder.yudao.module.system.controller.admin.worklog.vo.statistics.LogStatisticsDetailsVO">
        SELECT
            a.id AS userId,
            a.nickname AS nickname,
            a.avatar AS avatar,
            a.dept_id AS deptId,
            c.`name` AS deptName,
            b.log_instance_ext_id AS logInstanceExtId,
            ifnull(b.`status`,3) AS `status`,
            d.create_time AS createTime
        FROM
            system_users AS a
                LEFT JOIN work_log_statistics AS b ON a.id = b.user_id
                LEFT JOIN system_dept AS c ON a.dept_id = c.id
                LEFT JOIN work_log_instance_ext AS d ON b.log_instance_ext_id = d.id
        <where>
            <if test="formId != null">
                AND b.form_id = #{formId}
            </if>
            <if test="userId != null">
                and a.id = #{userId}
            </if>
            <if test="deptId != null">
                and a.dept_id = #{deptId}
            </if>

            <if test="dateList != null and dateList.size() > 0">
                AND b.time IN
                <foreach collection="dateList" item="date" open="(" separator="," close=")">
                    #{date}
                </foreach>
            </if>
        </where>
    </select>
    <select id="getCurrentStatistics"
            resultType="cn.iocoder.yudao.module.system.controller.admin.worklog.vo.statistics.LogStatisticsDetailsVO">
        SELECT
            a.id AS userId,
            a.nickname AS nickname,
            a.avatar AS avatar,
            a.dept_id AS deptId,
            c.`name` AS deptName,
            IFNULL(d.`status`,3) AS status,
            d.id AS logInstanceExtId,
            d.create_time AS createTime
        FROM
            system_users AS a
                LEFT JOIN work_log_use AS b ON a.id = b.use_user_id
                LEFT JOIN system_dept AS c ON a.dept_id = c.id
                LEFT JOIN ( SELECT
                                id,start_user_id,create_time,`status`
                            FROM work_log_instance_ext
                            WHERE
                                form_id = #{formId}
                            <if test="dateList != null and dateList.size() > 0">
                                AND `time` IN
                                <foreach collection="dateList" item="date" open="(" separator="," close=")">
                                    #{date}
                                </foreach>
                            </if>) AS d ON d.start_user_id = b.use_user_id
        <where>
            b.deleted = 0
            <if test="formId != null">
                AND b.form_id = #{formId}
            </if>
        </where>
    </select>
    <select id="getMyCurrentStatistics"
            resultType="cn.iocoder.yudao.module.system.service.worklog.dto.LogUseVO">
        SELECT
        a.*,
        IFNULL( b.`status`, 3 ) as status
        FROM
        work_log_use AS a
        LEFT JOIN work_log_statistics AS b ON b.user_id = a.use_user_id
        <if test="dateList != null and dateList.size() > 0">
            AND b.`time` IN
            <foreach collection="dateList" item="date" open="(" separator="," close=")">
                #{date}
            </foreach>
        </if>
        <if test="formId != null">
            AND b.form_id = #{formId}
        </if>
        <where>
            a.deleted = 0
            <if test="userId != null">
                and a.use_user_id = #{userId}
            </if>
            <if test="deptId != null">
                and a.use_user_dept = #{deptId}
            </if>
        </where>
    </select>
    <select id="getNeedWriteHistory"
            resultType="cn.iocoder.yudao.module.system.controller.admin.worklog.vo.statistics.LogStatisticsModelVO">
        SELECT
        b.*,
        IF( a.STATUS = 1, 1, 0 ) AS onTimeNum,
        IF( a.STATUS = 2, 1, 0 ) AS lateNum,
        IF( a.STATUS = 3, 1, 0 ) AS unSubmittedNum,
        a.time,
        c.type,
        a.commit_time_range
        FROM
        work_log_statistics AS a
        LEFT JOIN work_log_form AS b ON a.form_id = b.id
        LEFT JOIN work_log_rule AS c ON a.rule_id = c.id
        <where>
            <if test="dto.formId != null">
                AND a.form_id = #{dto.formId}
            </if>
            <if test="dto.userId != null">
                AND a.user_id = #{dto.userId}
            </if>
            <if test="dateList != null and dateList.size() > 0">
                AND a.time IN
                <foreach collection="dateList" item="date" open="(" separator="," close=")">
                    #{date}
                </foreach>
            </if>
            <if test="dto.type != null">
                AND a.type = #{dto.type}
            </if>
        </where>
    </select>
    <select id="getStatisticsGroupByUser"
            resultType="cn.iocoder.yudao.module.system.controller.admin.worklog.vo.statistics.LogStatisticsDetailsVO">
        SELECT
            a.nickname as nickName,
            c.`name` as deptName,
            b.*
        FROM
            system_users AS a
                LEFT JOIN work_log_statistics AS b ON a.id = b.user_id
                LEFT JOIN system_dept AS c ON b.dept_id = c.id
        <where>
            b.deleted = 0
            <if test="dto.dateList != null and dto.dateList.size() > 0">
                and b.time IN
                <foreach collection="dto.dateList" item="date" separator="," open="(" close=")">
                    #{date}
                </foreach>
            </if>
            <if test="dto.nickName != null and dto.nickName != ''">
                and a.nickname like concat('%', #{dto.nickName}, '%')
            </if>
            <if test="dto.type != null">
                and b.type = #{dto.type}
            </if>
            <if test="dto.deptIds != null and dto.deptIds.size() > 0">
                AND b.dept_id IN
                <foreach collection="dto.deptIds" item="deptId" open="(" separator="," close=")">
                    #{deptId}
                </foreach>
            </if>
        </where>
    </select>
</mapper>
