<?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.oa.mapper.OaProjectReportMapper">

    <resultMap type="com.ruoyi.oa.domain.OaProjectReport" id="OaProjectReportResult">
        <result property="reportId" column="report_id"/>
        <result property="userId" column="user_id"/>
        <result property="workPlace" column="work_place"/>
        <result property="projectId" column="project_id"/>
        <result property="content" column="content"/>
        <result property="createTime" column="create_time"/>
        <result property="createBy" column="create_by"/>
        <result property="updateTime" column="update_time"/>
        <result property="updateBy" column="update_by"/>
        <result property="delFlag" column="del_flag"/>
        <result property="remark" column="remark"/>
    </resultMap>


    <select id="selectVoPagePlus" resultType="com.ruoyi.oa.domain.vo.OaProjectReportVo">
        select opr.report_id,
               opr.user_id,
               is_trip,
               work_place,
               opr.project_id,
               content,
               opr.create_time,
               opr.create_by,
               opr.update_time,
               opr.update_by,
               opr.del_flag,
               opr.remark,
               op.project_name,
               op.project_num,
               op.project_code,
               su.nick_name,
               opr.work_type,
               sd.dept_name
        from oa_project_report opr
        left join sys_oa_project op on opr.project_id = op.project_id
        left join sys_user su on su.user_id = opr.user_id
        left join sys_dept sd on su.dept_id = sd.dept_id
            ${ew.getCustomSqlSegment}
    </select>

    <select id="selectVoByIdPlus" resultType="com.ruoyi.oa.domain.vo.OaProjectReportVo">
        select opr.report_id,
               opr.user_id,
               is_trip,
               work_place,
               opr.project_id,
               content,
               opr.create_time,
               opr.create_by,
               opr.update_time,
               opr.update_by,
               opr.del_flag,
               opr.remark,
               op.project_name,
               op.project_num,
               op.project_code,
               opr.work_type,
               su.nick_name,
               sd.dept_name
        from oa_project_report opr
                 left join sys_oa_project op on opr.project_id = op.project_id
                 left join sys_user su on su.user_id = opr.user_id
                 left join sys_dept sd on su.dept_id = sd.dept_id
            where report_id = #{reportId}
    </select>

    <!-- 查询 content 中包含 “完成” 的记录数，按时间过滤 -->
    <select id="countWithCompleted" parameterType="java.util.Date" resultType="Long">
        SELECT COUNT(*)
        FROM oa_project_report
        WHERE del_flag = 0
          AND DATE_FORMAT(create_time, '%Y-%m') = DATE_FORMAT(#{createTime}, '%Y-%m')
    </select>



    <!-- 查询 content 中包含 “未完成” 或 “等待” 或 “待” 的记录数，按时间过滤 -->
    <select id="countWithPending" parameterType="java.util.Date" resultType="Long">
        SELECT COUNT(*)
        FROM oa_project_report
        WHERE del_flag = 0
          AND (
            content LIKE CONCAT('%', '未完成', '%')
                OR content LIKE CONCAT('%', '等待', '%')
                OR content LIKE CONCAT('%', '待', '%')
            )
          AND create_time &gt;= #{createTime}
    </select>
    <!-- 按天统计报工数 -->
    <select id="selectReportTrend" resultType="com.ruoyi.oa.domain.vo.ProjectReportTrendVo">
        SELECT
            DATE(create_time) AS date,
            COUNT(*)               AS count
        FROM oa_project_report r
        WHERE create_time
          AND DATE(r.create_time) BETWEEN
            DATE_FORMAT(#{start}, '%Y-%m-%d')
            AND DATE_FORMAT(#{end},   '%Y-%m-%d')
          AND del_flag = 0
        GROUP BY DATE(create_time)
        ORDER BY DATE(create_time)
    </select>

    <!-- 各项目报工分布 -->
    <select id="selectProjectDistribution" resultType="com.ruoyi.oa.domain.vo.ProjectReportPieVo">
        SELECT
            p.project_name AS projectName,
            COUNT(r.report_id)  AS value
        FROM oa_project_report r
                 JOIN sys_oa_project   p ON r.project_id = p.project_id
        WHERE r.del_flag = 0
          AND DATE(r.create_time) BETWEEN
            DATE_FORMAT(#{start}, '%Y-%m-%d')
            AND DATE_FORMAT(#{end},   '%Y-%m-%d')
        GROUP BY p.project_name
        ORDER BY value DESC
    </select>


    <!-- 查询每个 user_id 的报工次数，并返回 nickName, deptName, count -->
    <select id="selectRankData"
            resultType="com.ruoyi.oa.domain.vo.OaProjectReportVo">
        SELECT
            u.nick_name   AS nickName,
            d.dept_name   AS deptName,
            COUNT(r.report_id) AS count
        FROM oa_project_report r
                 INNER JOIN sys_user u
                            ON r.user_id = u.user_id
                 INNER JOIN sys_dept d
                            ON u.dept_id = d.dept_id
        WHERE r.del_flag = 0
          AND DATE(r.create_time) BETWEEN
            DATE_FORMAT(#{start}, '%Y-%m-%d')
            AND DATE_FORMAT(#{end},   '%Y-%m-%d')
        GROUP BY r.user_id
    </select>


    <select id="getProjects"
            parameterType="map"
            resultType="com.ruoyi.oa.domain.vo.OaProjectReportVo">
        SELECT
            opr.project_id                                      AS projectId,
            COUNT(opr.report_id)                                AS reportCount,
            proj.project_name                                   AS projectName,
            proj.project_status                                AS projectStatus,
            proj.project_num                                  AS projectNum,
            opr.work_type AS workType,
            proj.project_code                                   AS projectCode,
            TIMESTAMPDIFF(
                    DAY,
                    CURDATE(),                                        -- 从今天开始
                    DATE(proj.finish_time)                               -- 到项目结束日期（仅取日期部分）
            )                                                   AS remainTime
        FROM oa_project_report opr
                 INNER JOIN sys_oa_project proj
                            ON opr.project_id = proj.project_id
        WHERE opr.del_flag = 0
          AND DATE(opr.create_time)
            BETWEEN DATE_FORMAT(#{start}, '%Y-%m-%d')
            AND DATE_FORMAT(#{end},   '%Y-%m-%d')
        GROUP BY opr.project_id,workType
    </select>

    <select id="getClearList" resultType="com.ruoyi.oa.domain.vo.OaProjectReportVo">
        select opr.report_id,
               opr.user_id,
               is_trip,
               work_place,
               opr.project_id,
               content,
               opr.create_time,
               opr.create_by,
               opr.update_time,
               opr.update_by,
               opr.del_flag,
               opr.remark,
               op.project_name,
               op.project_num,
               op.project_code,
               su.nick_name,
               opr.work_type,
               sd.dept_name
        from oa_project_report opr
                 left join sys_oa_project op on opr.project_id = op.project_id
                 left join sys_user su on su.user_id = opr.user_id
                 left join sys_dept sd on su.dept_id = sd.dept_id
        WHERE opr.del_flag = 0
          AND DATE(opr.create_time)
            BETWEEN DATE_FORMAT(#{start}, '%Y-%m-%d')
            AND DATE_FORMAT(#{end},   '%Y-%m-%d')
        ORDER BY opr.create_time DESC
        LIMIT 20
    </select>

    <select id="getSummaryData" resultType="com.ruoyi.oa.domain.vo.OaProjectReportVo">
        SELECT
        u.nick_name                                   AS nickName,
        d.dept_name                                   AS deptName,
        -- 国内出差次数：仅统计标记为出差且 work_type=0 的记录
        SUM(CASE WHEN r.is_trip = 1 AND r.work_type = 0 THEN 1 ELSE 0 END)
        AS inWorkNum,
        -- 国外出差次数：仅统计标记为出差且 work_type=1 的记录
        SUM(CASE WHEN r.is_trip = 1 AND r.work_type = 1 THEN 1 ELSE 0 END)
        AS outWorkNum,
        -- 报工总次数：统计所有未删除记录
        COUNT(1)                                      AS reportCount
        FROM oa_project_report r
        JOIN sys_user          u ON r.user_id = u.user_id
        LEFT JOIN sys_dept     d ON u.dept_id = d.dept_id
        WHERE r.del_flag = 0
          AND DATE(r.create_time)
            BETWEEN DATE_FORMAT(#{start}, '%Y-%m-%d')
            AND DATE_FORMAT(#{end},   '%Y-%m-%d')
        GROUP BY u.user_id, u.nick_name, d.dept_name
        ORDER BY u.nick_name
    </select>
</mapper>
