<?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.dgtly.system.mapper.SysNoticeMapper">
    
    <resultMap type="SysNotice" id="SysNoticeResult">
        <result property="noticeId"       column="notice_id"       />
        <result property="companyId"    column="company_id"    />
        <result property="noticeTitle"    column="notice_title"    />
        <result property="noticeType"     column="notice_type"     />
        <result property="noticeContent"  column="notice_content"  />
        <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"     />
        <result property="remark"         column="remark"          />
        <result property="fmzFileUrl"         column="fmz_file_url"          />
    </resultMap>
    
    <sql id="selectNoticeVo">
        select notice_id,company_id,notice_title, notice_type, notice_content, status, create_by, fmz_file_url, create_time, update_by, update_time, remark
		from sys_notice
    </sql>

    <sql id="selectNotice">
        select sn.notice_id,
        su.company_id,
        sn.notice_title,
        sn.notice_type,
        sn.notice_content,
        sn.status,
        su.user_name create_by,
        sn.create_time,
        sn.update_by,
        sn.update_time,
        sn.remark,
        sn.fmz_file_url,
        sn.create_time
		from sys_notice sn
		left join sys_user su on su.user_id = sn.create_by
		left join sys_dept sd on sd.dept_id = su.dept_id
    </sql>
    
    <select id="selectNoticeById" parameterType="Long" resultMap="SysNoticeResult">
        <include refid="selectNoticeVo"/>
        where notice_id = #{noticeId}
    </select>
    
    <select id="selectNoticeList" parameterType="SysNotice" resultMap="SysNoticeResult">
        <include refid="selectNoticeVo"/>
        <where>
			<if test="noticeTitle != null and noticeTitle != ''">
				AND notice_title like concat('%', #{noticeTitle}, '%')
			</if>
            <if test="companyId != null and companyId != 0">
                AND company_id = #{companyId}
            </if>
			<if test="noticeType != null and noticeType != ''">
				AND notice_type = #{noticeType}
			</if>
			<if test="createBy != null and createBy != ''">
				AND create_by like concat('%', #{createBy}, '%')
			</if>
		</where>
    </select>

    <select id="selectNoticeListByRole" parameterType="SysNotice" resultMap="SysNoticeResult">
        <include refid="selectNotice"/>
        <where>
            <if test="noticeTitle != null and noticeTitle != ''">
                AND sn.notice_title like concat('%', #{noticeTitle}, '%')
            </if>
            <if test="companyId != null and companyId != 0">
                AND company_id = #{companyId}
            </if>
            <if test="noticeType != null and noticeType != ''">
                AND sn.notice_type = #{noticeType}
            </if>
            <if test="createBy != null and createBy != ''">
                AND su.user_name like concat('%', #{createBy}, '%')
            </if>
            ${params.dataScope}
        </where>
    </select>



    <select id="selectNoticeTop3ByRole" parameterType="SysNotice" resultMap="SysNoticeResult">
        <include refid="selectNoticeVo"/>
        <where>
            <if test="noticeTitle != null and noticeTitle != ''">
                AND notice_title like concat('%', #{noticeTitle}, '%')
            </if>
            <if test="companyId != null and companyId != 0">
                AND company_id = #{companyId}
            </if>
            <if test="noticeType != null and noticeType != ''">
                AND notice_type = #{noticeType}
            </if>
            <if test="createBy != null and createBy != ''">
                AND create_by like concat('%', #{createBy}, '%')
            </if>
            ORDER BY create_time desc
            <if test="limitFlag != null and limitFlag != ''">
                LIMIT 0,3
            </if>

        </where>
    </select>

    
    <insert id="insertNotice" parameterType="SysNotice">
        insert into sys_notice (
            <if test="companyId != null and companyId != 0">company_id,</if>
			<if test="noticeTitle != null and noticeTitle != '' ">notice_title, </if>
			<if test="noticeType != null and noticeType != '' ">notice_type, </if>
			<if test="noticeContent != null and noticeContent != '' ">notice_content, </if>
			<if test="status != null and status != '' ">status, </if>
			<if test="remark != null and remark != ''">remark,</if>
 			<if test="createBy != null and createBy != ''">create_by,</if>
            <if test="fmzFileUrl != null and fmzFileUrl != ''">fmz_file_url,</if>
 			create_time
 		)values(
            <if test="companyId != null and companyId != 0">#{companyId},</if>
			<if test="noticeTitle != null and noticeTitle != ''">#{noticeTitle}, </if>
			<if test="noticeType != null and noticeType != ''">#{noticeType}, </if>
			<if test="noticeContent != null and noticeContent != ''">#{noticeContent}, </if>
			<if test="status != null and status != ''">#{status}, </if>
			<if test="remark != null and remark != ''">#{remark},</if>
 			<if test="createBy != null and createBy != ''">#{createBy},</if>
            <if test="fmzFileUrl != null and fmzFileUrl != ''">#{fmzFileUrl},</if>
             sysdate()
		)
    </insert>
	 
    <update id="updateNotice" 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="noticeContent != null">notice_content = #{noticeContent}, </if>
            <if test="status != null and status != ''">status = #{status}, </if>
            <if test="fmzFileUrl != null and fmzFileUrl != ''">fmz_file_url = #{fmzFileUrl}, </if>
            <if test="updateBy != null and updateBy != ''">update_by = #{updateBy},</if>
 			update_time = sysdate()
        </set>
        where notice_id = #{noticeId}
    </update>
	
    <delete id="deleteNoticeByIds" parameterType="String">
        delete from sys_notice where notice_id in 
        <foreach item="noticeId" collection="array" open="(" separator="," close=")">
            #{noticeId}
        </foreach>
    </delete>
    
</mapper>