<?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.workReport.mapper.WorkReportMapper">


    <select id="selectPage" resultType="cn.iocoder.yudao.module.workReport.vo.WorkReportRespVO">
        SELECT wr.*, su.nickname as userName, su.avatar as avatar
        FROM work_report wr
        LEFT JOIN system_users su ON su.id = wr.user_id
        <where>
            wr.deleted = 0
            <if test="pageReqVO.reportTopic != null">
                AND wr.report_topic LIKE CONCAT('%', #{pageReqVO.reportTopic}, '%')
            </if>
            <if test="pageReqVO.reportTime != null">
                AND wr.report_time BETWEEN #{pageReqVO.reportTime[0]} AND #{pageReqVO.reportTime[1]}
            </if>
            <if test="pageReqVO.createTime != null">
                AND wr.create_time BETWEEN #{pageReqVO.createTime[0]} AND #{pageReqVO.createTime[1]}
            </if>
            <if test="pageReqVO.userName != null and pageReqVO.userName != ''">
                AND su.nickname LIKE CONCAT('%', #{pageReqVO.userName}, '%')
            </if>
            <if test="pageReqVO.servicePackageId != null and pageReqVO.servicePackageId != ''">
                AND wr.service_package_id = #{pageReqVO.servicePackageId}
            </if>
            <if test="pageReqVO.topicOrUserName != null and pageReqVO.topicOrUserName != ''">
                AND (wr.report_topic LIKE CONCAT('%', #{pageReqVO.topicOrUserName}, '%')
                OR su.nickname LIKE CONCAT('%', #{pageReqVO.topicOrUserName}, '%'))
            </if>
            <if test="pageReqVO.userId != null">
                AND (wr.user_id = #{pageReqVO.userId}  -- 查询自己的汇报
                OR FIND_IN_SET(#{pageReqVO.userId}, wr.report_to) > 0)  -- 查询汇报给自己的汇报
            </if>
        </where>
        ORDER BY wr.create_time DESC
    </select>
    <select id="queryReportCount" resultType="java.lang.Long" parameterType="java.lang.Long">
        SELECT COUNT(*)
        FROM work_report wr
        LEFT JOIN system_users su ON su.id = wr.user_id
        <where>
            wr.deleted = 0
            <if test="pageReqVO.reportTopic != null">
                AND wr.report_topic LIKE CONCAT('%', #{pageReqVO.reportTopic}, '%')
            </if>
            <if test="pageReqVO.reportTime != null">
                AND wr.report_time BETWEEN #{pageReqVO.reportTime[0]} AND #{pageReqVO.reportTime[1]}
            </if>
            <if test="pageReqVO.createTime != null">
                AND wr.create_time BETWEEN #{pageReqVO.createTime[0]} AND #{pageReqVO.createTime[1]}
            </if>
            <if test="pageReqVO.userName != null and pageReqVO.userName != ''">
                AND su.nickname LIKE CONCAT('%', #{pageReqVO.userName}, '%')
            </if>
            <if test="pageReqVO.servicePackageId != null and pageReqVO.servicePackageId != ''">
                AND wr.service_package_id = #{pageReqVO.servicePackageId}
            </if>
            <if test="pageReqVO.topicOrUserName != null and pageReqVO.topicOrUserName != ''">
                AND (wr.report_topic LIKE CONCAT('%', #{pageReqVO.topicOrUserName}, '%')
                OR su.nickname LIKE CONCAT('%', #{pageReqVO.topicOrUserName}, '%'))
            </if>
            <if test="pageReqVO.userId != null">
                AND (wr.user_id = #{pageReqVO.userId}  -- 查询自己的汇报
                OR FIND_IN_SET(#{pageReqVO.userId}, wr.report_to) > 0)  -- 查询汇报给自己的汇报
            </if>
        </where>
    </select>

    <select id="selectByIdOne" resultType="cn.iocoder.yudao.module.workReport.vo.WorkReportRespVO">
        SELECT wr.*, su.nickname as userName, su.avatar as avatar
        FROM work_report wr
        LEFT JOIN system_users su ON su.id = wr.user_id
        <where>
            wr.deleted = 0 and wr.id = #{pageReqVO.id}
        </where>
    </select>

</mapper>