<?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.business.notice.mapper.NoticeMapper">

    <!-- 通用查询映射结果 -->
    <resultMap id="noticeResultMap" type="org.springblade.modules.business.notice.entity.NoticeEntity">
        <result column="release_time" property="releaseTime"/>
        <result column="category" property="category"/>
        <result column="title" property="title"/>
        <result column="content" property="content"/>
        <result column="public_state" property="publicState"/>
        <result column="sys_msg" property="sysMsg"/>
    </resultMap>

    <resultMap id="noticeResultVOMap" type="org.springblade.modules.business.notice.vo.NoticeVO">
        <result column="release_time" property="releaseTime"/>
        <result column="category" property="category"/>
        <result column="title" property="title"/>
        <result column="content" property="content"/>
        <result column="public_state" property="publicState"/>
        <result column="dept_name" property="releaseDeptName"/>
        <result column="sys_msg" property="sysMsg"/>

    </resultMap>

    <select id="selectList" resultMap="noticeResultVOMap">
        SELECT
        n.*,d.dept_name
        FROM
        blade_notice n
        LEFT JOIN blade_dept d on d.id=n.create_dept
        WHERE
        n.is_deleted = 0
        <if test="noticeDTO.category!=null and noticeDTO.category==0">
            and (n.create_dept = #{noticeDTO.createDept} or n.sys_msg=1)
        </if>
        <if test="noticeDTO.category!=null and noticeDTO.category==1">
            and ((n.create_dept = #{noticeDTO.createDept} or (n.category = 0 and n.public_state=1)) or n.sys_msg=1)
        </if>
        <if test="noticeDTO.category!=null and noticeDTO.category==2">
            and ((n.create_dept = #{noticeDTO.createDept} or (n.category = 1 and n.public_state=1)) or n.sys_msg=1)
        </if>
        <if test="noticeDTO.title!=null">
            and n.title like concat(concat('%', #{noticeDTO.title}), '%')
        </if>
        <if test="noticeDTO.viewDate!=null">
            and n.release_time > #{noticeDTO.viewDate}
        </if>
        order by n.release_time desc
    </select>

    <select id="selectPage" resultMap="noticeResultVOMap">
        SELECT
        n.*,d.dept_name
        FROM
        blade_notice n
        LEFT JOIN blade_dept d on d.id=n.create_dept
        WHERE
        n.is_deleted = 0
        <if test="noticeDTO.category!=null and noticeDTO.category==0">
            and (n.create_dept = #{noticeDTO.createDept} or n.sys_msg=1)
        </if>
        <if test="noticeDTO.category!=null and noticeDTO.category==1">
            and ((n.create_dept = #{noticeDTO.createDept} or (n.category = 0 and n.public_state=1)) or n.sys_msg=1)
        </if>
        <if test="noticeDTO.category!=null and noticeDTO.category==2">
            and ((n.create_dept = #{noticeDTO.createDept} or (n.category = 1 and n.public_state=1)) or n.sys_msg=1)
        </if>
        <if test="noticeDTO.title!=null">
            and n.title like concat(concat('%', #{noticeDTO.title}), '%')
        </if>
        <if test="noticeDTO.viewDate!=null">
            and n.release_time > #{noticeDTO.viewDate}
        </if>
        order by n.release_time desc
    </select>

    <select id="unviewNoticeCount" resultType="java.lang.Long">
        SELECT
        count(*)
        FROM
        blade_notice n
        WHERE
        n.is_deleted = 0
        <if test="noticeDTO.category!=null and noticeDTO.category==0">
            and (n.create_dept = #{noticeDTO.createDept} or n.sys_msg=1)
        </if>
        <if test="noticeDTO.category!=null and noticeDTO.category==1">
            and ((n.create_dept = #{noticeDTO.createDept} or (n.category = 0 and n.public_state=1)) or n.sys_msg=1)
        </if>
        <if test="noticeDTO.category!=null and noticeDTO.category==2">
            and ((n.create_dept = #{noticeDTO.createDept} or (n.category = 1 and n.public_state=1)) or n.sys_msg=1)
        </if>
        <if test="noticeDTO.viewDate!=null">
            and n.release_time > #{noticeDTO.viewDate}
        </if>
    </select>
</mapper>
