<?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.manage.dao.Power_NoticeMapper">
    <resultMap id="BaseResultMap" type="com.manage.vo.Power_NoticeVo">
        <id column="notice_id" property="noticeId" jdbcType="INTEGER"/>
        <result column="sys_id" property="sysId" jdbcType="INTEGER"/>
        <result column="notice_type_flag" property="noticeTypeFlag" jdbcType="VARCHAR"/>
        <result column="notice_type_name" property="noticeTypeName" jdbcType="VARCHAR"/>
        <result column="notice_title" property="noticeTitle" jdbcType="VARCHAR"/>
        <result column="notice_content" property="noticeContent" jdbcType="VARCHAR"/>
        <result column="notice_send" property="noticeSend" jdbcType="VARCHAR"/>
        <result column="notice_date" property="noticeDate" jdbcType="CHAR"/>
        <result column="notice_receive" property="noticeReceive" jdbcType="VARCHAR"/>
        <result column="effective" property="effective" jdbcType="INTEGER"/>
        <result column="read_flag" property="readFlag" jdbcType="INTEGER"/>
        <result column="parent_id" property="parentId" jdbcType="INTEGER"/>
        <result column="remark" property="remark" jdbcType="VARCHAR"/>
    </resultMap>
    <sql id="Base_Column_List">
    notice_id, sys_id, notice_type_flag, notice_type_name, notice_title, notice_content, 
    notice_send, notice_date, notice_receive, effective, read_flag, parent_id, remark
  </sql>
    <select id="selectByPrimaryKey" resultMap="BaseResultMap" parameterType="java.lang.Integer">
        select
        <include refid="Base_Column_List"/>
        from power_notice
        where notice_id = #{noticeId,jdbcType=INTEGER}
    </select>
    <delete id="deleteByPrimaryKey" parameterType="java.lang.Integer">
    delete from power_notice
    where notice_id = #{noticeId,jdbcType=INTEGER}
  </delete>
    <insert id="insert" parameterType="com.manage.entity.Power_Notice" useGeneratedKeys="true" keyProperty="noticeId">
    insert into power_notice (notice_id, sys_id, notice_type_flag, 
      notice_type_name, notice_title, notice_content, 
      notice_send, notice_date, notice_receive, 
      effective, read_flag, parent_id, 
      remark)
    values (#{noticeId,jdbcType=INTEGER}, #{sysId,jdbcType=INTEGER}, #{noticeTypeFlag,jdbcType=VARCHAR}, 
      #{noticeTypeName,jdbcType=VARCHAR}, #{noticeTitle,jdbcType=VARCHAR}, #{noticeContent,jdbcType=VARCHAR}, 
      #{noticeSend,jdbcType=VARCHAR}, #{noticeDate,jdbcType=CHAR}, #{noticeReceive,jdbcType=VARCHAR}, 
      #{effective,jdbcType=INTEGER}, #{readFlag,jdbcType=INTEGER}, #{parentId,jdbcType=INTEGER}, 
      #{remark,jdbcType=VARCHAR})
  </insert>
    <insert id="insertSelective" parameterType="com.manage.entity.Power_Notice">
        insert into power_notice
        <trim prefix="(" suffix=")" suffixOverrides=",">
            <if test="noticeId != null">
                notice_id,
            </if>
            <if test="sysId != null">
                sys_id,
            </if>
            <if test="noticeTypeFlag != null">
                notice_type_flag,
            </if>
            <if test="noticeTypeName != null">
                notice_type_name,
            </if>
            <if test="noticeTitle != null">
                notice_title,
            </if>
            <if test="noticeContent != null">
                notice_content,
            </if>
            <if test="noticeSend != null">
                notice_send,
            </if>
            <if test="noticeDate != null">
                notice_date,
            </if>
            <if test="noticeReceive != null">
                notice_receive,
            </if>
            <if test="effective != null">
                effective,
            </if>
            <if test="readFlag != null">
                read_flag,
            </if>
            <if test="parentId != null">
                parent_id,
            </if>
            <if test="remark != null">
                remark,
            </if>
        </trim>
        <trim prefix="values (" suffix=")" suffixOverrides=",">
            <if test="noticeId != null">
                #{noticeId,jdbcType=INTEGER},
            </if>
            <if test="sysId != null">
                #{sysId,jdbcType=INTEGER},
            </if>
            <if test="noticeTypeFlag != null">
                #{noticeTypeFlag,jdbcType=VARCHAR},
            </if>
            <if test="noticeTypeName != null">
                #{noticeTypeName,jdbcType=VARCHAR},
            </if>
            <if test="noticeTitle != null">
                #{noticeTitle,jdbcType=VARCHAR},
            </if>
            <if test="noticeContent != null">
                #{noticeContent,jdbcType=VARCHAR},
            </if>
            <if test="noticeSend != null">
                #{noticeSend,jdbcType=VARCHAR},
            </if>
            <if test="noticeDate != null">
                #{noticeDate,jdbcType=CHAR},
            </if>
            <if test="noticeReceive != null">
                #{noticeReceive,jdbcType=VARCHAR},
            </if>
            <if test="effective != null">
                #{effective,jdbcType=INTEGER},
            </if>
            <if test="readFlag != null">
                #{readFlag,jdbcType=INTEGER},
            </if>
            <if test="parentId != null">
                #{parentId,jdbcType=INTEGER},
            </if>
            <if test="remark != null">
                #{remark,jdbcType=VARCHAR},
            </if>
        </trim>
    </insert>
    <update id="updateByPrimaryKeySelective" parameterType="com.manage.entity.Power_Notice">
        update power_notice
        <set>
            <if test="sysId != null">
                sys_id = #{sysId,jdbcType=INTEGER},
            </if>
            <if test="noticeTypeFlag != null">
                notice_type_flag = #{noticeTypeFlag,jdbcType=VARCHAR},
            </if>
            <if test="noticeTypeName != null">
                notice_type_name = #{noticeTypeName,jdbcType=VARCHAR},
            </if>
            <if test="noticeTitle != null">
                notice_title = #{noticeTitle,jdbcType=VARCHAR},
            </if>
            <if test="noticeContent != null">
                notice_content = #{noticeContent,jdbcType=VARCHAR},
            </if>
            <if test="noticeSend != null">
                notice_send = #{noticeSend,jdbcType=VARCHAR},
            </if>
            <if test="noticeDate != null">
                notice_date = #{noticeDate,jdbcType=CHAR},
            </if>
            <if test="noticeReceive != null">
                notice_receive = #{noticeReceive,jdbcType=VARCHAR},
            </if>
            <if test="effective != null">
                effective = #{effective,jdbcType=INTEGER},
            </if>
            <if test="readFlag != null">
                read_flag = #{readFlag,jdbcType=INTEGER},
            </if>
            <if test="parentId != null">
                parent_id = #{parentId,jdbcType=INTEGER},
            </if>
            <if test="remark != null">
                remark = #{remark,jdbcType=VARCHAR},
            </if>
        </set>
        where notice_id = #{noticeId,jdbcType=INTEGER}
    </update>
    <update id="updateByPrimaryKey" parameterType="com.manage.entity.Power_Notice">
    update power_notice
    set sys_id = #{sysId,jdbcType=INTEGER},
      notice_type_flag = #{noticeTypeFlag,jdbcType=VARCHAR},
      notice_type_name = #{noticeTypeName,jdbcType=VARCHAR},
      notice_title = #{noticeTitle,jdbcType=VARCHAR},
      notice_content = #{noticeContent,jdbcType=VARCHAR},
      notice_send = #{noticeSend,jdbcType=VARCHAR},
      notice_date = #{noticeDate,jdbcType=CHAR},
      notice_receive = #{noticeReceive,jdbcType=VARCHAR},
      effective = #{effective,jdbcType=INTEGER},
      read_flag = #{readFlag,jdbcType=INTEGER},
      parent_id = #{parentId,jdbcType=INTEGER},
      remark = #{remark,jdbcType=VARCHAR}
    where notice_id = #{noticeId,jdbcType=INTEGER}
  </update>
    <select id="selectAllByUserIdOrRoleIdAndSysIdOrSysFlag" resultMap="BaseResultMap">
        SELECT
        power_notice.notice_id,
        power_sys_dict.parent_id sys_id,
        power_notice1.notice_type_flag,
        power_notice1.notice_type_name,
        power_notice.notice_title,
        power_notice.notice_content,
        power_user.user_name notice_send,
        power_notice.notice_date,
        IF (
        power_notice.notice_receive = 'all',
        'all',
        power_user1.user_name
        ) notice_receive,
        power_notice.effective,
        power_notice.read_flag,
        power_notice.parent_id,
        power_notice.remark
        FROM
        power_notice
        LEFT JOIN power_notice AS power_notice1 ON power_notice.parent_id = power_notice1.notice_id
        INNER JOIN power_sys_dict ON power_notice1.notice_type_flag = power_sys_dict.sys_flag
        LEFT JOIN power_user ON power_notice.notice_send = power_user.user_id
        LEFT JOIN power_user power_user1 ON power_notice.notice_receive = power_user1.user_id
        WHERE
        power_notice.notice_type_flag IS NULL
        <if test="roleId != 0 and roleId != -100 and userId != null">
            AND (
            power_notice.notice_send = ${userId}
            OR power_notice.notice_receive = ${userId}
            OR (
            power_notice.notice_receive = 'all'
            )
            AND power_notice1.notice_type_flag IN (
            SELECT
            power_sys_dict.sys_flag
            FROM
            power_sys_dict
            INNER JOIN power_user ON FIND_IN_SET(
            power_sys_dict.dept_id,
            power_user.dept_id
            )
            AND power_user.user_id = ${userId}
            UNION ALL
            SELECT
            power_sys_dict.sys_flag
            FROM
            power_sys_dict
            LEFT JOIN power_user_dict ON power_user_dict.dict_id = power_sys_dict.parent_id
            AND power_user_dict.user_id = ${userId}
            WHERE
            power_sys_dict.sys_type = '权限系统'
            )
            )
        </if>
        <if test="sysId != null and sysId != ''">
            AND power_sys_dict.parent_id = ${sysId}
        </if>
        <if test="sysFlag != null and sysFlag != ''">
            AND power_notice1.notice_type_flag = #{sysFlag}
        </if>
        <!--SELECT
            power_notice.notice_id,
            power_sys_dict.parent_id sys_id,
            power_notice.notice_type_flag,
            power_notice.notice_type_name,
            power_notice.notice_title,
            power_notice.notice_content,
            power_notice.notice_send,
            power_notice.notice_date,
            power_notice.notice_receive,
            power_notice.effective,
            power_notice.read_flag,
            power_notice.parent_id,
            power_notice.remark
            FROM
            power_notice
            LEFT JOIN power_sys_dict ON power_notice.notice_type_flag = power_sys_dict.sys_flag
            WHERE
            power_notice.notice_type_flag IS NOT NULL
            <if test="sysId != null">
                AND power_sys_dict.parent_id = ${sysId}
            </if>
            <if test="sysFlag != null and sysFlag != ''">
                AND power_notice.notice_type_flag = #{sysFlag}
            </if>
            UNION ALL
            SELECT
            power_notice.notice_id,
            power_sys_dict.parent_id sys_id,
            power_notice.notice_type_flag,
            power_notice.notice_type_name,
            power_notice.notice_title,
            power_notice.notice_content,
            power_notice.notice_send,
            power_notice.notice_date,
            power_notice.notice_receive,
            power_notice.effective,
            power_notice.read_flag,
            power_notice.parent_id,
            power_notice.remark
            FROM
            (
            SELECT
            power_notice.notice_id,
            power_notice.sys_id,
            power_notice.notice_type_flag,
            power_notice.notice_type_name,
            power_notice.notice_title,
            power_notice.notice_content,
            power_notice.notice_send,
            power_notice.notice_date,
            power_notice.notice_receive,
            power_notice.effective,
            power_notice.read_flag,
            power_notice.parent_id,
            power_notice.remark
            FROM
            (
            SELECT
            power_notice.notice_id,
            power_notice.sys_id,
            power_notice.notice_type_flag,
            power_notice.notice_type_name,
            power_notice.notice_title,
            power_notice.notice_content,
            power_notice.sender notice_send,
            power_notice.notice_date,
            power_notice.receiver notice_receive,
            power_notice.effective,
            power_notice.read_flag,
            power_notice.parent_id,
            power_notice.remark,
            power_user.user_id,
            power_user.role_id
            FROM
            (
            SELECT
            power_notice.notice_id,
            power_notice.sys_id,
            power_notice1.notice_type_flag,
            power_notice1.notice_type_name,
            power_notice.notice_title,
            power_notice.notice_content,
            power_notice.notice_send,
            power_notice.notice_date,
            power_notice.notice_receive,
            power_notice.effective,
            power_notice.read_flag,
            power_notice.parent_id,
            power_notice.remark,
            power_user1.user_name sender,
            power_user2.user_name receiver
            FROM
            power_notice
            LEFT JOIN power_notice AS power_notice1 ON power_notice.parent_id = power_notice1.notice_id
            LEFT JOIN power_user power_user1 ON power_notice.notice_send = power_user1.user_id
            LEFT JOIN power_user power_user2 ON power_notice.notice_receive = power_user2.user_id
            WHERE
            power_notice.notice_type_flag IS NULL
            AND power_notice.effective != 0
            AND power_user1.role_id != 0
            AND power_user1.role_id != - 100
            AND power_user2.role_id != 0
            AND power_user2.role_id != - 100
            ) power_notice
            INNER JOIN power_user ON power_notice.notice_send = power_user.user_id
            OR power_notice.notice_receive = power_user.user_id
            UNION ALL
            SELECT
            power_notice.notice_id,
            power_notice.sys_id,
            power_notice.notice_type_flag,
            power_notice.notice_type_name,
            power_notice.notice_title,
            power_notice.notice_content,
            power_notice.notice_send,
            power_notice.notice_date,
            power_notice.notice_receive,
            power_notice.effective,
            power_notice.read_flag,
            power_notice.parent_id,
            power_notice.remark,
            power_user.user_id,
            power_user.role_id
            FROM
            power_user,
            (
            SELECT
            power_notice.notice_id,
            power_notice.sys_id,
            power_notice.notice_title,
            power_notice.notice_content,
            power_user1.user_name notice_send,
            power_notice.notice_date,
            power_user2.user_name notice_receive,
            power_notice.effective,
            power_notice.read_flag,
            power_notice.parent_id,
            power_notice.remark,
            power_notice1.notice_type_flag,
            power_notice1.notice_type_name
            FROM
            power_notice
            LEFT JOIN power_notice power_notice1 ON power_notice.parent_id = power_notice1.notice_id
            LEFT JOIN power_user power_user1 ON power_notice.notice_send = power_user1.user_id
            LEFT JOIN power_user power_user2 ON power_notice.notice_receive = power_user2.user_id
            WHERE
            power_notice.notice_type_flag IS NULL
            ) power_notice
            ) power_notice
            <where>
                <if test="sysFlag != null and sysFlag != ''">
                    AND power_notice.notice_type_flag = #{sysFlag}
                </if>
                <if test="userId != null">
                    AND power_notice.user_id = ${userId}
                </if>
                <if test="roleId != null">
                    AND power_notice.roleId = ${roleId}
                </if>
            </where>
            ) power_notice
            LEFT JOIN power_sys_dict ON power_notice.notice_type_flag = power_sys_dict.sys_flag
            <where>
                <if test="sysId != null">
                    AND power_sys_dict.parent_id = ${sysId}
                </if>
            </where>-->
    </select>
    <select id="checkTypeSysFlagOrTypeSysName" resultType="com.manage.entity.Power_Notice">
        SELECT
        notice_id
        FROM
        power_notice
        <where>
            <if test="noticeTypeFlag != null and noticeTypeFlag != ''">
                AND notice_type_flag = #{noticeTypeFlag}
            </if>
            <if test="noticeTypeName != null and noticeTypeName != ''">
                AND notice_type_name = #{noticeTypeName}
            </if>
        </where>
    </select>
    <!--查询类别集合-->
    <select id="selectNoticeTypeList" resultMap="BaseResultMap">
        SELECT
        notice_id,
        notice_type_flag,
        notice_type_name
        FROM
        power_notice
        WHERE
        notice_type_flag IS NOT NULL
        <if test="noticeTypeFlag != '' and noticeTypeFlag != null">
            AND notice_type_flag = #{noticeTypeFlag}
        </if>
        <if test="noticeId != null">
            AND notice_id = ${noticeId}
        </if>
    </select>
    <!--查询系统管理员的系统列表-->
    <select id="selectSysByAdmin" resultMap="BaseResultMap">
        select power_notice.notice_id,a.sys_flag notice_type_flag,power_notice.notice_type_name,a.sys_name remark from
        (SELECT
        sys_flag,
        sys_name
        FROM
        power_sys_dict
        WHERE
        sys_flag IS NOT NULL
        <if test="roleId != null and roleId != 0 and roleId != -100 and userId != null">
            AND sys_flag IN (
            SELECT
            power_sys_dict.sys_flag
            FROM
            power_sys_dict
            INNER JOIN power_user ON FIND_IN_SET(
            power_sys_dict.dept_id,
            power_user.dept_id
            )
            AND power_user.user_id = ${userId}
            UNION ALL
            SELECT
            power_sys_dict.sys_flag
            FROM
            power_sys_dict
            LEFT JOIN power_user_dict ON power_user_dict.dict_id = power_sys_dict.parent_id
            AND power_user_dict.user_id = ${userId}
            WHERE
            power_sys_dict.sys_type = '权限系统'
            )
        </if>
        AND dict_status = 1
        <if test="roleId != null and roleId != 0">
            and parent_id = (select dict_id from power_user_dict where user_id = ${userId})
        </if>
        GROUP BY sys_flag) a
        INNER JOIN power_notice on a.sys_flag = power_notice.notice_type_flag
        <where>
            <if test="record.noticeId != null">
                and power_notice.notice_id = ${record.noticeId}
            </if>
            <if test="record.noticeTypeFlag != null and record.noticeTypeFlag != ''">
                and power_notice.notice_type_flag = #{record.noticeTypeFlag}
            </if>
        </where>
    </select>
    <!--查询非系统管理员的系统列表-->
    <select id="selectSysByNotAdmin" resultMap="BaseResultMap">
        SELECT
        power_notice.notice_id,
        a.sys_flag notice_type_flag,
        power_notice.notice_type_name,
        a.sys_name remark
        FROM
        (
        SELECT
        sys_flag,
        sys_name
        FROM
        power_sys_dict
        <if test="isRoleManage != null">
            LEFT JOIN power_user ON FIND_IN_SET(
            power_sys_dict.dept_id,
            power_user.dept_id
            )
        </if>
        WHERE
        sys_flag IS NOT NULL
        AND dict_status = 1
        AND parent_id IN (
        SELECT
        dict_id
        FROM
        power_user_dict
        WHERE
        user_id = ${userId}
        )
        <if test="isRoleManage != null">
            AND power_user.user_id = ${userId}
            OR power_sys_dict.sys_type = '权限系统'
        </if>
        GROUP BY
        sys_flag
        ) a
        INNER JOIN power_notice ON a.sys_flag = power_notice.notice_type_flag
    </select>
    <!--查询按权限查-->
    <select id="selectAll" resultMap="BaseResultMap">
        SELECT
        power_notice.notice_id,
        power_notice1.notice_type_flag,
        power_notice1.notice_type_name,
        power_notice.notice_title,
        power_notice.notice_content,
        power_user.user_name notice_send,
        power_notice.notice_date,
        IF (
        power_notice.notice_receive = 'all',
        'all',
        power_user1.user_name
        ) notice_receive,
        power_notice.effective,
        power_notice.read_flag,
        power_notice.parent_id,
        power_notice.remark
        FROM
        power_notice
        LEFT JOIN power_notice AS power_notice1 ON power_notice.parent_id = power_notice1.notice_id
        INNER JOIN power_sys_dict ON power_notice1.notice_type_flag = power_sys_dict.sys_flag
        LEFT JOIN power_user ON power_notice.notice_send = power_user.user_id
        LEFT JOIN power_user power_user1 ON power_notice.notice_receive = power_user1.user_id
        WHERE
        power_notice.notice_type_flag IS NULL
        <if test="roleId != 0 and roleId != -100 and userId != null">
            AND (
            power_notice.notice_send = ${userId}
            OR power_notice.notice_receive = ${userId}
            OR (
            power_notice.notice_receive = 'all'
            )
            AND power_notice1.notice_type_flag IN (
            SELECT
            power_sys_dict.sys_flag
            FROM
            power_sys_dict
            INNER JOIN power_user ON FIND_IN_SET(
            power_sys_dict.dept_id,
            power_user.dept_id
            )
            AND power_user.user_id = ${userId}
            UNION ALL
            SELECT
            power_sys_dict.sys_flag
            FROM
            power_sys_dict
            LEFT JOIN power_user_dict ON power_user_dict.dict_id = power_sys_dict.parent_id
            AND power_user_dict.user_id = ${userId}
            WHERE
            power_sys_dict.sys_type = '权限系统'
            )
            )
        </if>
        <if test="record.noticeTypeFlag != null and record.noticeTypeFlag != ''">
            AND power_notice1.notice_type_flag = #{record.noticeTypeFlag}
        </if>
        <if test="record.parentId != null">
            AND power_notice.parent_id = ${record.parentId}
        </if>
        <if test="record.effective != null">
            AND power_notice.effective = ${record.effective}
        </if>
        <if test="record.noticeDateStart != null and record.noticeDateStart != ''">
            AND notice_date >= #{record.noticeDateStart}
        </if>
        <if test="record.noticeDateEnd != null and record.noticeDateEnd != ''">
            AND notice_date &lt;= #{record.noticeDateEnd}
        </if>
        <if test="record.noticeSend != null and record.noticeSend != ''">
            AND notice_send in (SELECT user_id FROM Power_User WHERE user_name LIKE '%${record.noticeSend}%')
        </if>
        <if test="record.noticeReceive != null and record.noticeReceive != ''">
            AND notice_receive in (SELECT user_id FROM Power_User WHERE user_name LIKE '%${record.noticeReceive}%')
        </if>
    </select>
    <select id="selectNoticeListByParentId" resultMap="BaseResultMap">
        select power_notice.notice_id, power_notice.notice_title,
        power_notice.notice_content,power_notice.parent_id,power_user.user_name
        notice_send, power_notice.notice_date, if(power_notice.notice_receive='all','all',power_user1.user_name)
        notice_receive, power_notice.effective, power_notice.read_flag from power_notice
        left join power_user
        on power_user.user_id = power_notice.notice_send
        left join power_user power_user1
        on power_user1.user_id = power_notice.notice_receive
        where parent_id = ${record.parentId}
        <if test="userId != null">
            and (notice_send='${userId}' or notice_receive='${userId}' or notice_receive='all')
        </if>
        <if test="record.effective != null">
            AND power_notice.effective = ${record.effective}
        </if>
        <if test="record.noticeSend != null and record.noticeSend != ''">
            AND notice_send in (select user_id from power_user where user_name LIKE '%${record.noticeSend}%')
        </if>
        <if test="record.noticeReceive != null and record.noticeReceive != ''">
            AND (notice_receive in (select user_id from power_user where user_name LIKE '%${record.noticeReceive}%') OR
            notice_receive = 'all')
        </if>
        <if test="record.noticeDateStart != null and record.noticeDateStart != ''">
            AND notice_date >= #{record.noticeDateStart}
        </if>
        <if test="record.noticeDateEnd != null and record.noticeDateEnd != ''">
            AND notice_date &lt;= #{record.noticeDateEnd}
        </if>
    </select>
    <select id="selectALlByPower" resultMap="BaseResultMap">
        select power_notice.notice_id,a.sys_flag
        notice_type_flag,power_notice.notice_type_name,notice_title,notice_content,parent_id,notice_send,notice_receive,notice_date,read_flag,effective
        from (SELECT
        sys_flag,
        sys_name
        FROM
        power_sys_dict
        WHERE
        sys_flag IS NOT NULL
        <if test="roleId != null and roleId != 0 and roleId != -100 and userId != null">
            AND sys_flag IN (
            SELECT
            power_sys_dict.sys_flag
            FROM
            power_sys_dict
            INNER JOIN power_user ON FIND_IN_SET(
            power_sys_dict.dept_id,
            power_user.dept_id
            )
            AND power_user.user_id = ${userId}
            UNION ALL
            SELECT
            power_sys_dict.sys_flag
            FROM
            power_sys_dict
            LEFT JOIN power_user_dict ON power_user_dict.dict_id = power_sys_dict.parent_id
            AND power_user_dict.user_id = ${userId}
            WHERE
            power_sys_dict.sys_type = '权限系统'
            )
        </if>
        AND dict_status = 1
        <if test="roleId != null and roleId != 0">
            and parent_id = (select dict_id from power_user_dict where user_id = ${userId})
        </if>
        GROUP BY sys_flag) a
        INNER JOIN power_notice on a.sys_flag = power_notice.notice_type_flag
        <where>
            <if test="record.noticeId != null">
                and power_notice.notice_id = ${record.noticeId}
            </if>
            <if test="record.noticeTypeFlag != null and record.noticeTypeFlag != ''">
                and power_notice.notice_type_flag = #{record.noticeTypeFlag}
            </if>
        </where>
        UNION ALL
        (select power_notice.notice_id,
        power_notice.notice_type_flag,power_notice.notice_type_name,power_notice.notice_title,
        power_notice.notice_content,power_notice.parent_id,power_user.user_name notice_send,
        if(power_notice.notice_receive='all','all',power_user1.user_name) notice_receive, power_notice.notice_date,
        power_notice.read_flag,power_notice.effective from power_notice
        left join power_user
        on power_user.user_id = power_notice.notice_send
        left join power_user power_user1
        on power_user1.user_id = power_notice.notice_receive
        where notice_type_flag IS NULL
        <if test="userId != null and roleId != -100">
            and (notice_send='${userId}' or notice_receive='${userId}' or notice_receive='all')
        </if>
        <if test="record.effective != null">
            AND power_notice.effective = ${record.effective}
        </if>
        <if test="record.noticeSend != null and record.noticeSend != ''">
            AND notice_send in (select user_id from power_user where user_name LIKE '%${record.noticeSend}%')
        </if>
        <if test="record.noticeReceive != null and record.noticeReceive != ''">
            AND (notice_receive in (select user_id from power_user where user_name LIKE '%${record.noticeReceive}%') OR
            notice_receive = 'all')
        </if>
        <if test="record.noticeDateStart != null and record.noticeDateStart != ''">
            AND notice_date >= #{record.noticeDateStart}
        </if>
        <if test="record.noticeDateEnd != null and record.noticeDateEnd != ''">
            AND notice_date &lt;= #{record.noticeDateEnd}
        </if>
        AND power_notice.parent_id IN (select power_notice.notice_id from (SELECT
        sys_flag,
        sys_name
        FROM
        power_sys_dict
        WHERE
        sys_flag IS NOT NULL
        <if test="roleId != null and roleId != 0 and roleId != -100 and userId != null">
            AND sys_flag IN (
            SELECT
            power_sys_dict.sys_flag
            FROM
            power_sys_dict
            INNER JOIN power_user ON FIND_IN_SET(
            power_sys_dict.dept_id,
            power_user.dept_id
            )
            AND power_user.user_id = ${userId}
            UNION ALL
            SELECT
            power_sys_dict.sys_flag
            FROM
            power_sys_dict
            LEFT JOIN power_user_dict ON power_user_dict.dict_id = power_sys_dict.parent_id
            AND power_user_dict.user_id = ${userId}
            WHERE
            power_sys_dict.sys_type = '权限系统'
            )
        </if>
        AND dict_status = 1
        <if test="roleId != null and roleId != 0">
            and parent_id = (select dict_id from power_user_dict where user_id = ${userId})
        </if>
        GROUP BY sys_flag) a
        INNER JOIN power_notice on a.sys_flag = power_notice.notice_type_flag
        <where>
            <if test="record.noticeId != null">
                and power_notice.notice_id = ${record.noticeId}
            </if>
            <if test="record.noticeTypeFlag != null and record.noticeTypeFlag != ''">
                and power_notice.notice_type_flag = #{record.noticeTypeFlag}
            </if>
        </where>
        )
        ORDER BY notice_type_flag desc,notice_date desc
        <if test="record.offset != null and record.limit != null">
            LIMIT #{record.offset},#{record.limit}
        </if>)
    </select>
    <select id="selectALlByPowerWithExport" resultMap="BaseResultMap">
        select power_notice.notice_id,
        power_notice.notice_type_flag,power_notice1.notice_type_name,power_notice.notice_title,
        power_notice.notice_content,power_notice.parent_id,power_user.user_name notice_send,
        if(power_notice.notice_receive='all','all',power_user1.user_name) notice_receive, power_notice.notice_date,
        power_notice.read_flag,power_notice.effective from power_notice
        left join power_user
        on power_user.user_id = power_notice.notice_send
        left join power_user power_user1
        on power_user1.user_id = power_notice.notice_receive
        left join power_notice power_notice1
        on power_notice1.notice_id = power_notice.parent_id
        where power_notice.notice_type_flag IS NULL
        <if test="userId != null and roleId != -100">
            and (power_notice.notice_send='${userId}' or power_notice.notice_receive='${userId}' or
            power_notice.notice_receive='all')
        </if>
        <if test="record.effective != null">
            AND power_notice.power_notice.effective = ${record.effective}
        </if>
        <if test="record.noticeSend != null and record.noticeSend != ''">
            AND power_notice.notice_send in (select user_id from power_user where user_name LIKE
            '%${record.noticeSend}%')
        </if>
        <if test="record.noticeReceive != null and record.noticeReceive != ''">
            AND (power_notice.notice_receive in (select user_id from power_user where user_name LIKE
            '%${record.noticeReceive}%') OR power_notice.notice_receive = 'all')
        </if>
        <if test="record.noticeDateStart != null and record.noticeDateStart != ''">
            AND power_notice.notice_date >= #{record.noticeDateStart}
        </if>
        <if test="record.noticeDateEnd != null and record.noticeDateEnd != ''">
            AND power_notice.notice_date &lt;= #{record.noticeDateEnd}
        </if>
        AND power_notice.parent_id IN (select power_notice.notice_id from (SELECT
        sys_flag,
        sys_name
        FROM
        power_sys_dict
        WHERE
        sys_flag IS NOT NULL
        <if test="roleId != null and roleId != 0 and roleId != -100 and userId != null">
            AND sys_flag IN (
            SELECT
            power_sys_dict.sys_flag
            FROM
            power_sys_dict
            INNER JOIN power_user ON FIND_IN_SET(
            power_sys_dict.dept_id,
            power_user.dept_id
            )
            AND power_user.user_id = ${userId}
            UNION ALL
            SELECT
            power_sys_dict.sys_flag
            FROM
            power_sys_dict
            LEFT JOIN power_user_dict ON power_user_dict.dict_id = power_sys_dict.parent_id
            AND power_user_dict.user_id = ${userId}
            WHERE
            power_sys_dict.sys_type = '权限系统'
            )
        </if>
        AND dict_status = 1
        <if test="roleId != null and roleId != 0">
            and parent_id = (select dict_id from power_user_dict where user_id = ${userId})
        </if>
        GROUP BY sys_flag) a
        INNER JOIN power_notice on a.sys_flag = power_notice.notice_type_flag
        <where>
            <if test="record.noticeId != null">
                and power_notice.notice_id = ${record.noticeId}
            </if>
            <if test="record.noticeTypeFlag != null and record.noticeTypeFlag != ''">
                and power_notice.notice_type_flag = #{record.noticeTypeFlag}
            </if>
        </where>
        )
        ORDER BY power_notice.notice_type_flag desc,power_notice.notice_date desc
    </select>
    <select id="getTotal" resultMap="BaseResultMap">
        <!--select power_notice.notice_id,a.sys_flag notice_type_flag,power_notice.notice_type_name,notice_title,notice_content,parent_id,notice_send,notice_receive,notice_date,read_flag,effective from (SELECT
        sys_flag,
        sys_name
        FROM
        power_sys_dict
        WHERE
        sys_flag IS NOT NULL
        <if test="roleId != null and roleId != 0 and roleId != -100 and userId != null">
            AND sys_flag IN (
            SELECT
            power_sys_dict.sys_flag
            FROM
            power_sys_dict
            INNER JOIN power_user ON FIND_IN_SET(
            power_sys_dict.dept_id,
            power_user.dept_id
            )
            AND power_user.user_id = ${userId}
            UNION ALL
            SELECT
            power_sys_dict.sys_flag
            FROM
            power_sys_dict
            LEFT JOIN power_user_dict ON power_user_dict.dict_id = power_sys_dict.parent_id
            AND power_user_dict.user_id = ${userId}
            WHERE
            power_sys_dict.sys_type = '权限系统'
            )
        </if>
        AND dict_status = 1
        <if test="roleId != null and roleId != 0">
            and parent_id = (select dict_id from power_user_dict where user_id = ${userId})
        </if>
        GROUP BY sys_flag) a
        INNER JOIN power_notice on a.sys_flag = power_notice.notice_type_flag
        <where>
            <if test="record.noticeId != null">
                and power_notice.notice_id = ${record.noticeId}
            </if>
            <if test="record.noticeTypeFlag != null and record.noticeTypeFlag != ''">
                and power_notice.notice_type_flag = #{record.noticeTypeFlag}
            </if>
        </where>
        UNION ALL-->
        select power_notice.notice_id,
        power_notice.notice_type_flag,power_notice.notice_type_name,power_notice.notice_title,
        power_notice.notice_content,power_notice.parent_id,power_user.user_name notice_send,
        if(power_notice.notice_receive='all','all',power_user1.user_name) notice_receive, power_notice.notice_date,
        power_notice.read_flag,power_notice.effective from power_notice
        left join power_user
        on power_user.user_id = power_notice.notice_send
        left join power_user power_user1
        on power_user1.user_id = power_notice.notice_receive
        where notice_type_flag IS NULL
        <if test="userId != null and roleId != -100">
            and (notice_send='${userId}' or notice_receive='${userId}' or notice_receive='all')
        </if>
        <if test="record.effective != null">
            AND power_notice.effective = ${record.effective}
        </if>
        <if test="record.noticeSend != null and record.noticeSend != ''">
            AND notice_send in (select user_id from power_user where user_name LIKE '%${record.noticeSend}%')
        </if>
        <if test="record.noticeReceive != null and record.noticeReceive != ''">
            AND (notice_receive in (select user_id from power_user where user_name LIKE '%${record.noticeReceive}%') OR
            notice_receive = 'all')
        </if>
        <if test="record.noticeDateStart != null and record.noticeDateStart != ''">
            AND notice_date >= #{record.noticeDateStart}
        </if>
        <if test="record.noticeDateEnd != null and record.noticeDateEnd != ''">
            AND notice_date &lt;= #{record.noticeDateEnd}
        </if>
        AND parent_id IN (select power_notice.notice_id from (SELECT
        sys_flag,
        sys_name
        FROM
        power_sys_dict
        WHERE
        sys_flag IS NOT NULL
        <if test="roleId != null and roleId != 0 and roleId != -100 and userId != null">
            AND sys_flag IN (
            SELECT
            power_sys_dict.sys_flag
            FROM
            power_sys_dict
            INNER JOIN power_user ON FIND_IN_SET(
            power_sys_dict.dept_id,
            power_user.dept_id
            )
            AND power_user.user_id = ${userId}
            UNION ALL
            SELECT
            power_sys_dict.sys_flag
            FROM
            power_sys_dict
            LEFT JOIN power_user_dict ON power_user_dict.dict_id = power_sys_dict.parent_id
            AND power_user_dict.user_id = ${userId}
            WHERE
            power_sys_dict.sys_type = '权限系统'
            )
        </if>
        AND dict_status = 1
        <if test="roleId != null and roleId != 0">
            and parent_id = (select dict_id from power_user_dict where user_id = ${userId})
        </if>
        GROUP BY sys_flag) a
        INNER JOIN power_notice on a.sys_flag = power_notice.notice_type_flag
        <where>
            <if test="record.noticeId != null">
                and power_notice.notice_id = ${record.noticeId}
            </if>
            <if test="record.noticeTypeFlag != null and record.noticeTypeFlag != ''">
                and power_notice.notice_type_flag = #{record.noticeTypeFlag}
            </if>
        </where>
        )
    </select>

    <!--按noticeId集合查，按选中id导出-->
    <select id="selectAllByIds" resultMap="BaseResultMap">
        SELECT
            power_notice.notice_id,
            power_notice1.notice_type_name,
            power_notice.notice_title,
            power_notice.notice_content,
            power_user.user_name notice_send, power_notice.notice_date,
            if(power_notice.notice_receive='all','all',power_user1.user_name) notice_receive,
            power_notice.notice_date,
            power_notice.effective,
            power_notice.read_flag
        FROM
            power_notice
        LEFT JOIN power_notice power_notice1 ON power_notice.parent_id = power_notice1.notice_id
        LEFT JOIN power_user ON power_user.user_id = power_notice.notice_send
        LEFT JOIN power_user power_user1 ON power_user1.user_id = power_notice.notice_receive
        WHERE
            power_notice.notice_id IN (${noticeIds})
    </select>

    <!--按用户id查询未读通知数量-->
    <select id="getUnReadCount" resultType="java.lang.Integer">
        SELECT
        count(1)
        FROM
        power_notice
        WHERE
        notice_type_flag IS NULL
        AND read_flag = 0
        AND effective = 1
        <if test="userId != null">
            AND (
            notice_receive = 'all' OR notice_receive = #{userId}
            )
            AND parent_id IN (
            SELECT
            notice_id
            FROM
            power_notice
            WHERE
            notice_type_flag IN (
            SELECT
            sys_flag
            FROM
            power_sys_dict
            WHERE
            dept_id IN (
            SELECT
            substring_index(
            substring_index(
            power_user.dept_id,
            ',',
            b.help_topic_id + 1
            ),
            ',' ,- 1
            ) AS dept_id
            FROM
            power_user
            JOIN mysql.help_topic b ON b.help_topic_id &lt; (
            length(power_user.dept_id) - length(
            REPLACE (power_user.dept_id, ',', '')
            ) + 1
            )
            WHERE
            power_user.user_id = #{userId}
            )
            GROUP BY
            sys_flag
            )
            OR notice_type_flag = 'power'
            )
        </if>
    </select>
    <!--批量新增-->
    <insert id="simpleInsert" useGeneratedKeys="true" keyProperty="noticeId">
        INSERT INTO power_notice(
            sys_id, notice_type_flag,
      notice_type_name, notice_title, notice_content,
      notice_send, notice_date, notice_receive,
      effective, read_flag, parent_id,
      remark
        )
        VALUES
        <foreach collection="list" item="record" separator=",">
            (
            #{record.sysId,jdbcType=INTEGER}, #{record.noticeTypeFlag,jdbcType=VARCHAR},
            #{record.noticeTypeName,jdbcType=VARCHAR}, #{record.noticeTitle,jdbcType=VARCHAR}, #{record.noticeContent,jdbcType=VARCHAR},
            #{record.noticeSend,jdbcType=VARCHAR}, #{record.noticeDate,jdbcType=CHAR}, #{record.noticeReceive,jdbcType=VARCHAR},
            #{record.effective,jdbcType=INTEGER}, #{record.readFlag,jdbcType=INTEGER}, #{record.parentId,jdbcType=INTEGER},
            #{record.remark,jdbcType=VARCHAR}
            )
        </foreach>
    </insert>
</mapper>