<?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="net.lab1024.smartadmin.module.business.notice.dao.NoticeDao">

    <resultMap id="NoticeEntity" type="net.lab1024.smartadmin.module.business.notice.domain.entity.NoticeEntity"></resultMap>

    <resultMap id="NoticeDTO" type="net.lab1024.smartadmin.module.business.notice.domain.dto.NoticeVO"></resultMap>

    <resultMap id="NoticeDetailDTO" type="net.lab1024.smartadmin.module.business.notice.domain.dto.NoticeDetailVO"></resultMap>

    <resultMap id="NoticeReceiveDTO" type="net.lab1024.smartadmin.module.business.notice.domain.dto.NoticeReceiveDTO"></resultMap>


    <resultMap id="NoticeReadCountDTO" type="net.lab1024.smartadmin.module.business.notice.domain.dto.NoticeReadCountDTO"></resultMap>


    <select id="queryByPage" resultMap="NoticeDTO">
        select
            n.id,
            n.title,
            n.create_user,
            e.actual_name as createUserName,
            n.send_status,
            n.create_time
        from t_notice n
        left join t_employee e on e.id = n.create_user
        <where>
            <if test="queryDTO.startDate != null and queryDTO.startDate != ''">
                AND DATE_FORMAT(n.create_time, '%Y-%m-%d') &gt;= #{queryDTO.startDate}
            </if>
            <if test="queryDTO.endDate != null and queryDTO.endDate != ''">
                AND DATE_FORMAT(n.create_time, '%Y-%m-%d') &lt;= #{queryDTO.endDate}
            </if>
            <if test="queryDTO.title != null and queryDTO.title != ''">
                AND INSTR(n.title,#{queryDTO.title})
            </if>
            <if test="queryDTO.deleted != null">
                AND n.deleted = #{queryDTO.deleted}
            </if>
        </where>
        ORDER by n.create_time desc
    </select>



    <select id="queryUnreadByPage" resultMap="NoticeDTO">
        select
        n.id,
        n.title,
        n.send_status,
        n.create_user,
        e.actual_name as createUserName,
        n.create_time
        from t_notice n
        left join t_employee e on e.id = n.create_user
        where n.id not in (select notice_id from t_notice_receive_record nrr where nrr.employee_id = #{employeeId})
        and n.send_status = #{sendStatus}
        ORDER by n.create_time desc
    </select>


    <select id="queryReceiveByPage" resultMap="NoticeReceiveDTO">
       select
        n.id,
        n.title,
        n.create_user,
        n.send_status,
        e.actual_name as createUserName,
        n.create_time,
        nrr.create_time as receiveTime
        from t_notice n
        left join t_employee e on e.id = n.create_user
        left join t_notice_receive_record nrr on nrr.notice_id=n.id and nrr.employee_id=#{queryDTO.employeeId}
        <where>
            <if test="queryDTO.startDate != null and queryDTO.startDate != ''">
                AND DATE_FORMAT(n.create_time, '%Y-%m-%d') &gt;= #{queryDTO.startDate}
            </if>
            <if test="queryDTO.endDate != null and queryDTO.endDate != ''">
                AND DATE_FORMAT(n.create_time, '%Y-%m-%d') &lt;= #{queryDTO.endDate}
            </if>
            <if test="queryDTO.title != null and queryDTO.title != ''">
                AND INSTR(n.title,#{queryDTO.title})
            </if>
            <if test="queryDTO.sendStatus != null">
                AND n.send_status = #{queryDTO.sendStatus}
            </if>
        </where>
        ORDER by n.create_time desc
    </select>




    <select id="detail" resultMap="NoticeDetailDTO">
        select
        n.id,
        n.title,
        n.content,
        n.send_status,
        n.create_user,
        e.actual_name as createUserName,
        n.create_time,
        n.update_time
        from t_notice n
        left join t_employee e on e.id = n.create_user
        where n.id = #{id}
    </select>



    <update id="logicDeleteById">
        UPDATE t_notice
        set deleted = #{deletedFlag}
        WHERE id =#{id}
    </update>


    <delete id="logicDeleteByIds">
        UPDATE t_notice set deleted = #{deletedFlag} where id in
        <foreach collection="idList" open="(" close=")" separator="," item="item">
            #{item}
        </foreach>
    </delete>


    <select id="noticeCount" resultType="integer">
        select
        count(1)
        from t_notice where send_status = #{sendStatus}
    </select>

    <select id="readCount" resultMap="NoticeReadCountDTO">
        SELECT
        nrr.employee_id,
        count(1) as readCount
        from t_notice_receive_record nrr
        where nrr.employee_id in
        <foreach collection="employeeIds" open="(" close=")" separator="," item="item">
            #{item}
        </foreach>
        GROUP BY nrr.employee_id
    </select>



    <select id="noticeUnreadCount" resultType="integer">
        select
        count(1)
        from t_notice n
        where n.id not in (select msg_id from t_notice_receive_record nrr where nrr.employee_id = #{employeeId})
         and n.send_status = #{sendStatus}
    </select>


</mapper>