<?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.aidex.system.mapper.SysNoticeMapper">
    <sql id="sysNoticeColumns">
        a.id as "id",
        a.notice_title as "noticeTitle",
        a.notice_type as "noticeType",
		cast(a.notice_content as char) AS "noticeContent",
		cast(a.notice_content_html as char) AS "noticeContentHtml",
		a.status AS "status",
		a.create_by AS "createBy",
		a.create_time AS "createTime",
		a.update_by AS "updateBy",
		a.update_time AS "updateTime",
		a.update_ip AS "updateIp",
		a.remark AS "remark",
		a.version AS "version",
        a.topping AS "topping",
        a.carousel_img_url AS "carouselImgUrl",
        a.carousel AS "carousel"
	</sql>

    <sql id="sysNoticeJoins">

    </sql>

    <sql id="sysNoticeOrderBy">
			order by topping desc, a.create_time desc
	</sql>

    <select id="get" resultType="SysNotice">
        SELECT
        <include refid="sysNoticeColumns"/>
        FROM sys_notice a
        where id = #{id}
    </select>

    <select id="findList" resultType="SysNotice">
        SELECT
        <include refid="sysNoticeColumns"/>,
        su.name AS "createByName"
        FROM sys_notice a
        left join sys_user su
        on su.id = a.create_by
        <include refid="sysNoticeJoins"/>
        where a.del_flag = #{DEL_FLAG_NORMAL}
        <if test="noticeTitle != null and noticeTitle != ''">
            AND a.notice_title like concat('%', #{noticeTitle}, '%')
        </if>
        <if test="noticeType != null and noticeType != ''">
            AND a.notice_type = #{noticeType}
        </if>
        <if test="carousel != null and carousel != ''">
            AND a.carousel = #{carousel}
        </if>
        <if test="createBy != null and createBy != ''">
            AND su.name like concat('%', #{createBy}, '%')
        </if>
        <include refid="sysNoticeOrderBy"/>
    </select>

    <insert id="insert" parameterType="SysNotice">
        insert into sys_notice (
            id,
			notice_title,
			notice_type,
			notice_content,
			notice_content_html,
			status,
            topping,
            carousel,
            carousel_img_url,
			remark,
 			create_by,
			create_dept,
			create_time,
			update_by,
			update_time,
			update_ip,
			del_flag,
			version
 		)values(
 		    #{id},
			#{noticeTitle},
			#{noticeType},
			#{noticeContent},
			#{noticeContentHtml},
			#{status},
            #{topping},
            #{carousel},
            #{carouselImgUrl},
			#{remark},
 			#{createBy},
			#{createDept},
			#{createTime},
			#{updateBy},
			#{updateTime},
			#{updateIp},
			#{DEL_FLAG_NORMAL},
			1
		)
    </insert>

    <update id="update" parameterType="SysNotice">
        update sys_notice
        <set>
            <if test="noticeTitle != null and noticeTitle != ''">
                notice_title = #{noticeTitle},
            </if>
            <if test="noticeType != null and noticeType != ''">
                notice_type = #{noticeType},
            </if>
            <if test="topping != null and topping != ''">
                topping = #{topping},
            </if>
            <if test="carousel != null and carousel != ''">
                carousel = #{carousel},
            </if>
            <if test="carouselImgUrl != null and carouselImgUrl != ''">
                carousel_img_url = #{carouselImgUrl},
            </if>
            <if test="noticeContent != null">
                notice_content = #{noticeContent},
            </if>
            <if test="noticeContentHtml != null">
                notice_content_html = #{noticeContentHtml},
            </if>
            <if test="status != null and status != ''">
                status = #{status},
            </if>
            <if test="updateBy != null and updateBy != ''">
                update_by = #{updateBy},
            </if>
            <if test="updateTime != null">
                update_time = #{updateTime},
            </if>
            <if test="updateIp != null and updateIp != ''">
                update_ip = #{updateIp},
            </if>
            version = version + 1
        </set>
        where id = #{id}
    </update>

    <delete id="delete">
		UPDATE sys_notice SET
			del_flag = #{DEL_FLAG_DELETE}
		WHERE id = #{id}
	</delete>

    <update id="deleteNoticeByIds" parameterType="java.util.Map">
        UPDATE sys_notice SET
        del_flag = #{DEL_FLAG_DELETE}
        WHERE id in
        <foreach collection="noticeIds" item="ids" open="(" separator="," close=")">
            #{ids}
        </foreach>
    </update>


    <select id="getNoticeListByUserId" resultType="SysNotice">
        SELECT
        id,
        notice_title as 'noticeTitle'
        FROM
        `sys_notice` a
        WHERE
        NOT EXISTS (
        SELECT
        1
        FROM
        sys_notice_user_read t
        WHERE
        a.id = t.notice_id
        AND t.user_id = #{userId}
        )
        AND a.del_flag = #{DEL_FLAG_NORMAL}
        <include refid="sysNoticeOrderBy"/>
    </select>

    <select id="findNoticeByUserList" resultType="SysNotice">
        SELECT
        <include refid="sysNoticeColumns"/>,
        su.name AS "createByName",
        r.is_read AS "isRead"
        FROM sys_notice a
        left join sys_user su
        on su.id = a.create_by
        LEFT JOIN sys_notice_user_read r ON a.id = r.notice_id and r.user_id = #{userId}
        where a.del_flag = #{DEL_FLAG_NORMAL} and (r.del_flag =  #{DEL_FLAG_NORMAL} or r.id is null)
        <if test="noticeTitle != null and noticeTitle != ''">
            AND a.notice_title like concat('%', #{noticeTitle}, '%')
        </if>
        <if test="noticeType != null and noticeType != ''">
            AND a.notice_type = #{noticeType}
        </if>
        <if test="createBy != null and createBy != ''">
            AND su.name like concat('%', #{createBy}, '%')
        </if>
        <include refid="sysNoticeOrderBy"/>
    </select>

</mapper>
