<?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.srs.routine.mapper.NotificationManagementMapper">

    <resultMap type="NotificationManagement" id="NotificationManagementResult">
        <result property="id"    column="id"    />
        <result property="title"    column="title"    />
        <result property="content"    column="content"    />
        <result property="gradeIds"    column="grade_ids"    />
        <result property="gradeName"    column="grade_name"    />
        <result property="sender"    column="sender"    />
        <result property="createBy"    column="create_by"    />
        <result property="createTime"    column="create_time"    />
        <result property="updateBy"    column="update_by"    />
        <result property="updateTime"    column="update_time"    />
    </resultMap>

    <resultMap type="SrsGrade" id="GradeResult">
        <result property="gradeId"    column="grade_id"    />
        <result property="gradeName"    column="grade_name"    />
        <result property="gradeCode"    column="grade_code"    />
        <result property="status"    column="status"    />
        <result property="createBy"    column="create_by"    />
        <result property="createTime"    column="create_time"    />
        <result property="updateBy"    column="update_by"    />
        <result property="updateTime"    column="update_time"    />
    </resultMap>

    <sql id="selectNotificationManagementVo">
        select id, title, content, grade_ids, sender, create_by, create_time, update_by, update_time from srs_notification
    </sql>

    <sql id="selectGradeVo">
        select grade_id, grade_name, grade_code, status, create_by, create_time, update_by, update_time from srs_grade
    </sql>

    <select id="selectNotificationManagementList" parameterType="NotificationManagement" resultMap="NotificationManagementResult">
        <include refid="selectNotificationManagementVo"/>
        <where>
            <if test="title != null and title != ''"> and title like concat('%', #{title}, '%')</if>
            <if test="content != null  and content != ''"> and content like concat('%', #{content}, '%')</if>
            <if test="gradeIds != null and gradeIds != ''"> and grade_ids = #{gradeIds}</if>
            <if test="sender != null"> and sender = #{sender}</if>
        </where>
        order by create_time desc
    </select>

    <!-- 查询当前用户发送的通知列表 -->
    <select id="selectNotificationManagementListBySender" resultMap="NotificationManagementResult">
        select n.id, n.title, n.content, n.grade_ids, n.sender, n.create_by, n.create_time, n.update_by, n.update_time,
        GROUP_CONCAT(g.grade_name SEPARATOR ',') as grade_name
        from srs_notification n
        left join srs_grade g on FIND_IN_SET(g.grade_id, n.grade_ids) > 0
        <where>
            n.sender = #{senderId}
            <if test="notification.title != null and notification.title != ''"> and n.title like concat('%', #{notification.title}, '%')</if>
            <if test="notification.content != null  and notification.content != ''"> and n.content like concat('%', #{notification.content}, '%')</if>
            <if test="notification.gradeIds != null and notification.gradeIds != ''"> and n.grade_ids = #{notification.gradeIds}</if>
        </where>
        group by n.id, n.title, n.content, n.grade_ids, n.sender, n.create_by, n.create_time, n.update_by, n.update_time
        order by n.create_time desc
    </select>


    <select id="selectNotificationManagementById" parameterType="Long" resultMap="NotificationManagementResult">
        <include refid="selectNotificationManagementVo"/>
        where id = #{id}
    </select>

<!--    插入数据到通知管理表-->
    <insert id="insertNotificationManagement" parameterType="NotificationManagement" useGeneratedKeys="true" keyProperty="id">
        insert into srs_notification
        <trim prefix="(" suffix=")" suffixOverrides=",">
            <if test="title != null">title,</if>
            <if test="content != null">content,</if>
            <if test="gradeIds != null">grade_ids,</if>
            <if test="sender != null">sender,</if>
            <if test="createBy != null">create_by,</if>
            <if test="createTime != null">create_time,</if>
            <if test="updateBy != null">update_by,</if>
            <if test="updateTime != null">update_time,</if>
        </trim>
        <trim prefix="values (" suffix=")" suffixOverrides=",">
            <if test="title != null">#{title},</if>
            <if test="content != null">#{content},</if>
            <if test="gradeIds != null">#{gradeIds},</if>
            <if test="sender != null">#{sender},</if>
            <if test="createBy != null">#{createBy},</if>
            <if test="createTime != null">#{createTime},</if>
            <if test="updateBy != null">#{updateBy},</if>
            <if test="updateTime != null">#{updateTime},</if>
        </trim>
    </insert>

    <update id="updateNotificationManagement" parameterType="NotificationManagement">
        update srs_notification
        <trim prefix="SET" suffixOverrides=",">
            <if test="title != null">title = #{title},</if>
            <if test="content != null">content = #{content},</if>
            <if test="gradeIds != null">grade_ids = #{gradeIds},</if>
            <if test="sender != null">sender = #{sender},</if>
            <if test="createBy != null">create_by = #{createBy},</if>
            <if test="createTime != null">create_time = #{createTime},</if>
            <if test="updateBy != null">update_by = #{updateBy},</if>
            <if test="updateTime != null">update_time = #{updateTime},</if>
        </trim>
        where id = #{id}
    </update>

    <delete id="deleteNotificationManagementById" parameterType="Long">
        delete from srs_notification where id = #{id}
    </delete>

    <delete id="deleteNotificationManagementByIds" parameterType="String">
        delete from srs_notification where id in
        <foreach item="id" collection="array" open="(" separator="," close=")">
            #{id}
        </foreach>
    </delete>

    <!-- 查询年级列表 -->
    <select id="selectGradeList" resultMap="GradeResult">
        <include refid="selectGradeVo"/>
        where status = '0'
        order by grade_code desc
    </select>

    <!-- 根据年级ID列表查询学生用户ID -->
    <select id="selectStudentIdsByGrades" resultType="Long">
        SELECT DISTINCT u.user_id
        FROM srs_student ss
        left JOIN srs_class sc ON ss.class_id = sc.class_id
        left JOIN sys_user u ON ss.stu_no = u.user_name
        WHERE u.user_id IS NOT NULL
        AND sc.grade_id IN
        <foreach item="gradeId" collection="gradeIds" open="(" separator="," close=")">
            #{gradeId}
        </foreach>
    </select>

    <!-- 根据年级ID列表查询学生学号列表 -->
    <select id="selectStudentNosByGrades" resultType="String">
        SELECT DISTINCT ss.stu_no
        FROM srs_student ss
        left JOIN srs_class sc ON ss.class_id = sc.class_id
        WHERE ss.stu_no IS NOT NULL
        AND sc.grade_id IN
        <foreach item="gradeId" collection="gradeIds" open="(" separator="," close=")">
            #{gradeId}
        </foreach>
    </select>

    <!-- 批量插入通知记录到cph_msg表 -->
    <insert id="batchInsertNotification" parameterType="java.util.List">
        insert into cph_msg (sender, receiver, content, create_time)
        values
        <foreach collection="list" item="item" separator=",">
            (#{item.sender}, #{item.receiver}, #{item.content}, #{item.createTime})
        </foreach>
    </insert>

    <!-- 批量删除cph_msg表数据 -->
    <delete id="batchDeleteCphMsg" parameterType="java.util.List">
        delete from cph_msg where (content, sender, create_time) in
        <foreach collection="list" item="item" open="(" separator="," close=")">
            (#{item.content}, #{item.sender}, #{item.createTime})
        </foreach>
    </delete>

    <!-- 批量更新cph_msg表数据 -->
    <update id="batchUpdateCphMsg" parameterType="java.util.Map">
        update cph_msg
        set content = #{newList[0].content}
        where content = #{oldList[0].content}
          and sender = #{oldList[0].sender}
          and create_time = #{oldList[0].createTime}
    </update>

</mapper>
