<?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="org.springblade.modules.desk.mapper.NoticeMapper">

    <!-- 通用查询映射结果 -->
    <resultMap id="noticeResultMap" type="org.springblade.modules.desk.entity.Notice">
        <result column="id" property="id"/>
        <result column="create_user" property="createUser"/>
        <result column="create_time" property="createTime"/>
        <result column="update_user" property="updateUser"/>
        <result column="update_time" property="updateTime"/>
        <result column="status" property="status"/>
        <result column="is_deleted" property="isDeleted"/>
        <result column="release_time" property="releaseTime"/>
        <result column="title" property="title"/>
        <result column="content" property="content"/>

        <result column="type" property="type"/>
        <result column="dept_id" property="deptId"/>
        <result column="role_id" property="roleId"/>
        <result column="target_ids" property="targetIds"/>
        <result column="read_ids" property="readIds"/>
        <result column="business_type" property="businessType"/>
    </resultMap>

    <!-- 通用查询映射结果 -->
    <resultMap id="noticeVOResultMap" type="org.springblade.modules.desk.vo.NoticeVO">
        <result column="id" property="id"/>
        <result column="create_user" property="createUser"/>
        <result column="create_time" property="createTime"/>
        <result column="update_user" property="updateUser"/>
        <result column="update_time" property="updateTime"/>
        <result column="status" property="status"/>
        <result column="is_deleted" property="isDeleted"/>
        <result column="release_time" property="releaseTime"/>
        <result column="title" property="title"/>
        <result column="content" property="content"/>
    </resultMap>

    <select id="topList" resultMap="noticeResultMap">
        select * from blade_notice limit #{number}
    </select>

    <select id="selectMyNoticePage" resultMap="noticeVOResultMap">
        SELECT
        n.*,
        d.dict_value AS categoryName
        FROM
        blade_notice n
        LEFT JOIN ( SELECT * FROM blade_dict WHERE CODE = 'notice' ) d ON n.category = d.dict_key
        WHERE
        n.is_deleted = 0 and n.tenant_id = #{notice.tenantId}
        <if test="notice.currentUserId!=null">
            AND n.target_ids like '%${notice.currentUserId}%'
        </if>

        <if test="notice.title!=null">
            and n.title like concat(concat('%', #{notice.title}), '%')
        </if>
        <if test="notice.category!=null">
            and n.category = #{notice.category}
        </if>
        <if test="notice.hadRead!=null">
            <if test="notice.hadRead==0">
                and ( n.read_ids not like '%${notice.currentUserId}%' or n.read_ids is null )
            </if>
            <if test="notice.hadRead==1">
                and n.read_ids like '%${notice.currentUserId}%'
            </if>
        </if>
        <if test="notice.id!=null">
            and n.id = #{notice.id}
        </if>
        ORDER BY n.create_time DESC
    </select>

    <select id="selectNoticePage" resultMap="noticeVOResultMap">
        select n.* from blade_notice n where n.is_deleted = 0 and n.create_user = #{notice.createUser}
        <if test="notice.title!=null">
            and n.title like concat(concat('%', #{notice.title}), '%')
        </if>
        <if test="notice.category!=null">
            and n.category = #{notice.category}
        </if>
        ORDER BY n.create_time DESC
    </select>
</mapper>
