<?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.zsc.edu.gateway.modules.message.repo.UserNoticeRepository">
    <resultMap id="userNoticeMap" type="com.zsc.edu.gateway.modules.message.vo.UserNoticeVo">
        <id column="notice_id" jdbcType="BIGINT" property="noticeId"/>
        <result column="is_read" jdbcType="BOOLEAN" property="isRead"/>
        <result column="read_time" jdbcType="TIMESTAMP" property="readTime"/>
        <result column="username" jdbcType="VARCHAR" property="username"/>
        <result column="type" jdbcType="INTEGER" property="type"/>
        <result column="system" jdbcType="BOOLEAN" property="system"/>
        <result column="email" jdbcType="BOOLEAN" property="email"/>
        <result column="sms" jdbcType="BOOLEAN" property="sms"/>
        <result column="html" jdbcType="BOOLEAN" property="html"/>
        <result column="title" jdbcType="VARCHAR" property="title"/>
        <result column="content" jdbcType="VARCHAR" property="content"/>
        <result column="create_by" jdbcType="VARCHAR" property="createBy"/>
        <result column="update_by" jdbcType="VARCHAR" property="updateBy"/>
        <result column="create_time" jdbcType="DATE" property="createTime"/>
        <result column="update_time" jdbcType="DATE" property="updateTime"/>
        <result column="remark" jdbcType="VARCHAR" property="remark"/>
    </resultMap>

    <resultMap id="AdminNoticeMap" type="com.zsc.edu.gateway.modules.message.vo.AdminNoticeVo">
        <id column="id" jdbcType="BIGINT" property="id"/>
        <result column="type" jdbcType="INTEGER" property="type"/>
        <result column="system" jdbcType="BOOLEAN" property="system"/>
        <result column="email" jdbcType="BOOLEAN" property="email"/>
        <result column="sms" jdbcType="BOOLEAN" property="sms"/>
        <result column="html" jdbcType="BOOLEAN" property="html"/>
        <result column="title" jdbcType="VARCHAR" property="title"/>
        <result column="content" jdbcType="VARCHAR" property="content"/>
        <result column="remark" jdbcType="VARCHAR" property="remark"/>
        <result column="user_count" jdbcType="INTEGER" property="userCount"/>
        <result column="read_count" jdbcType="INTEGER" property="readCount"/>
    </resultMap>
    <select id="selectByNoticeIdAndUserId" resultType="com.zsc.edu.gateway.modules.message.vo.UserNoticeVo"
            resultMap="userNoticeMap">
        select sun.*,sn.*,su.username,su.address,su.avatar,su.name
        from sys_user_notice sun
        left join sys_user su on sun.user_id = su.id
        left join sys_notice sn on sn.id = sun.notice_id
        <where>
            sun.notice_id=#{noticeId}
            and sun.user_id=#{userId}
        </where>
    </select>

    <select id="page" resultType="com.zsc.edu.gateway.modules.message.vo.UserNoticeVo" resultMap="userNoticeMap">
        select sun.*,sn.*,su.username,su.address,su.avatar,su.name
        FROM
        sys_user su
        LEFT JOIN
        sys_user_notice sun ON sun.user_id = su.id
        LEFT JOIN
        sys_notice sn ON sn.id = sun.notice_id
        <where>
            <if test="query.userId != null">
                AND sun.user_id = #{query.userId}
            </if>
            <if test="query.title != null and query.title != ''">
                AND sn.title LIKE CONCAT('%', #{query.title}, '%')
            </if>
            <if test="query.type != null">
                AND sn.type = #{query.type}
            </if>
            <if test="query.name != null and query.name != ''">
                AND su.username LIKE CONCAT('%', #{query.name}, '%')
            </if>
            <if test="query.system != null">
                AND sn.system = #{query.system}
            </if>
            <if test="query.isRead != null">
                AND sun.is_read = #{query.isRead}
            </if>
            <if test="query.createAtBegin != null and query.createAtEnd != null">
                AND sn.create_time BETWEEN #{query.createAtBegin} AND #{query.createAtEnd}
            </if>
        </where>
        ORDER BY
        sn.create_time DESC
    </select>

    <select id="pageAdmin" resultMap="AdminNoticeMap">
        SELECT
        sn.*, -- 获取消息详情
        COUNT(DISTINCT sun.user_id) AS user_count, -- 统计用户的关联数量
        SUM(CASE WHEN sun.is_read = true THEN 1 ELSE 0 END) AS read_count -- 统计已读数量
        FROM
        sys_user su
        LEFT JOIN
        sys_user_notice sun ON sun.user_id = su.id
        LEFT JOIN
        sys_notice sn ON sn.id = sun.notice_id
        <where>
            <if test="query.userId != null">
                AND sun.user_id = #{query.userId}
            </if>
            <if test="query.title != null and query.title != ''">
                AND sn.title LIKE CONCAT('%', #{query.title}, '%')
            </if>
            <if test="query.createAtBegin != null and query.createAtEnd != null">
                AND sn.create_time BETWEEN #{query.createAtBegin} AND #{query.createAtEnd}
            </if>
        </where>
        GROUP BY
        sn.id
    </select>

</mapper>