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

    <resultMap type="com.ruoyi.oa.domain.OaReportSummary" id="OaReportSummaryResult">
        <result property="summaryId" column="summary_id"/>
        <result property="reportTitle" column="report_title"/>
        <result property="reportDate" column="report_date"/>
        <result property="reporter" column="reporter"/>
        <result property="projectId" column="project_id"/>
        <result property="createBy" column="create_by"/>
        <result property="createTime" column="create_time"/>
        <result property="updateBy" column="update_by"/>
        <result property="updateTime" column="update_time"/>
        <result property="delFlag" column="del_flag"/>
        <result property="remark" column="remark"/>
        <result property="type" column="type"/>
        <result property="status" column="status"/>
    </resultMap>

    <select id="selectVoPageWithProject" resultType="com.ruoyi.oa.domain.vo.OaReportSummaryVo">
        SELECT
        s.summary_id as summaryId,
        s.report_title AS reportTitle,
        s.report_date AS reportDate,
        s.reporter,
        s.project_id AS projectId,
        s.remark,
        s.status,
        s.type,
        p.project_name AS projectName,
        d.latest_create_time AS lastUpdateTime
        FROM oa_report_summary s
        LEFT JOIN sys_oa_project p ON s.project_id = p.project_id
        LEFT JOIN (
        SELECT
        summary_id,
        create_time AS latest_create_time,
        ROW_NUMBER() OVER (PARTITION BY summary_id ORDER BY create_time DESC) AS rn
        FROM oa_report_detail
        ) d ON s.summary_id = d.summary_id AND d.rn = 1
        <where>
            <if test="bo.reportTitle != null and bo.reportTitle != ''">
                AND s.report_title LIKE CONCAT('%', #{bo.reportTitle}, '%')
            </if>
            <if test="bo.reportDate != null">
                AND s.report_date = #{bo.reportDate}
            </if>
            <if test="bo.reporter != null and bo.reporter != ''">
                AND s.reporter LIKE CONCAT('%', #{bo.reporter}, '%')
            </if>
            <if test="bo.projectId != null">
                AND s.project_id = #{bo.projectId}
            </if>
            <if test="bo.type != null">
                AND s.type = #{bo.type}
            </if>
                AND s.del_flag = 0
        </where>
        ORDER BY
        COALESCE(d.latest_create_time, s.report_date) DESC,
        s.report_date DESC
    </select>


</mapper>
