<?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.ruoyi.system.mapper.ZxNoticeMapper">
    
    <resultMap type="ZxNotice" id="ZxNoticeResult">
        <result property="zxNoticeId"    column="zx_notice_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="issuer"    column="issuer"    />
        <result property="issuerDept"    column="issuer_dept"    />
        <result property="issuerDeptId"    column="issuer_dept_id"    />
        <result property="issuerTime"    column="issuer_time"    />
        <result property="type"    column="type"    />
        <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="updateTime"    column="update_time"    />
        <result property="remark"    column="remark"    />
    </resultMap>

    <resultMap id="ZxNoticeZxUserNoticeResult" type="ZxNotice" extends="ZxNoticeResult">
        <collection property="zxUserNoticeList" notNullColumn="sub_user_id" javaType="java.util.List" resultMap="ZxUserNoticeResult" />
        <collection property="zxFjList" notNullColumn="fj_source_id" javaType="java.util.List" resultMap="ZxFjResult" />

    </resultMap>

    <resultMap type="ZxUserNotice" id="ZxUserNoticeResult">
        <result property="userId"    column="sub_user_id"    />
        <result property="userName"    column="sub_user_name"    />
        <result property="zxNoticeId"    column="sub_zx_notice_id"    />
        <result property="zxNoticeType"    column="sub_zx_notice_type"    />
        <result property="createTime"    column="sub_create_time"    />
    </resultMap>

    <resultMap type="ZxFj" id="ZxFjResult">
        <result property="id"    column="fj_id"    />
        <result property="mainId"    column="fj_main_id"    />
        <result property="sourceId"    column="fj_source_id"    />
        <result property="name"    column="fj_name"    />
        <result property="url"    column="fj_url"    />
        <result property="type"    column="fj_type"    />
        <result property="stytle"    column="fj_stytle"    />
        <result property="remark"    column="fj_remark"    />
    </resultMap>

    <sql id="selectZxNoticeVo">
        select zx_notice_id, notice_title, notice_type, notice_content, status, issuer, issuer_dept, issuer_dept_id, issuer_time, create_by, create_time, update_by, update_time, remark from zx_notice
    </sql>

    <select id="selectZxNoticeList" parameterType="ZxNotice" resultMap="ZxNoticeZxUserNoticeResult">
            select * from ( select a.zx_notice_id, a.notice_title, a.notice_type, a.notice_content, a.status, a.issuer,
            a.issuer_dept, a.issuer_dept_id, a.issuer_time, a.create_by, a.create_time, a.update_by, a.update_time,
            a.remark,
            b.user_id as sub_user_id,
            b.zx_notice_type  as type,
            f.id as fj_id,f.main_id as fj_main_id, f.source_id as fj_source_id, f.name as fj_name, f.url as fj_url, f.type as fj_type,f.stytle as fj_stytle
            from zx_notice a
            left join zx_user_notice b on b.zx_notice_id = a.zx_notice_id
            left join zx_fj f on f.source_id = a.zx_notice_id
            <where>
                f.type = '3'
            <if test="userId != null">and b.user_id = #{userId}</if>
            <if test="zxNoticeId != null">and a.zx_notice_id = #{zxNoticeId}</if>
            <if test="noticeTitle != null  and noticeTitle != ''">and a.notice_title = #{noticeTitle}</if>
            <if test="noticeType != null  and noticeType != ''"> and a.notice_type = #{noticeType}</if>
            <if test="noticeContent != null  and noticeContent != ''">and a.notice_content =#{noticeContent}</if>
            <if test="status != null  and status != ''"> and a.status = #{status}</if>
            <if test="issuer != null  and issuer != ''"> and a.issuer = #{issuer}</if>
            <if test="issuerDept != null  and issuerDept != ''">and a.issuer_dept =#{issuerDept}</if>
            <if test="issuerDeptId != null ">and a.issuer_dept_id= #{issuerDeptId}</if>
            <if test="issuerTime != null ">and a.issuer_time =#{issuerTime}</if>
            </where>
        ) s
        <where>
            <if test="type != null and type != ''">and s.type =#{type}</if>
        </where>
        order by s.issuer_time DESC
    </select>
    
    <select id="selectZxNoticeByZxNoticeId" parameterType="Long" resultMap="ZxNoticeZxUserNoticeResult">
        select a.zx_notice_id, a.notice_title, a.notice_type, a.notice_content, a.status, a.issuer, a.issuer_dept, a.issuer_dept_id, a.issuer_time, a.create_by, a.create_time, a.update_by, a.update_time, a.remark,
        b.user_id as sub_user_id, b.user_name as sub_user_name,b.zx_notice_type as sub_zx_notice_type, b.zx_notice_id as sub_zx_notice_id, b.create_time as sub_create_time,
        f.id as fj_id,f.main_id as fj_main_id, f.source_id as fj_source_id, f.name as fj_name, f.url as fj_url, f.type as fj_type,f.stytle as fj_stytle
        from zx_notice a
        left join zx_user_notice b on b.zx_notice_id = a.zx_notice_id
        left join zx_fj f on f.source_id = a.zx_notice_id
        where a.zx_notice_id = #{zxNoticeId} and f.type = '3'
    </select>
    <select id="selectZxUserNoticeByUserIdAndNoticeId" resultType="java.lang.Integer">
        select count(1) from zx_user_notice where user_id = #{userId} and zx_notice_id =#{zxNoticeId}
    </select>

    <insert id="insertZxNotice" parameterType="ZxNotice" useGeneratedKeys="true" keyProperty="zxNoticeId">
        insert into zx_notice
        <trim prefix="(" suffix=")" suffixOverrides=",">
            <if test="noticeTitle != null and noticeTitle != ''">notice_title,</if>
            <if test="noticeType != null and noticeType != ''">notice_type,</if>
            <if test="noticeContent != null">notice_content,</if>
            <if test="status != null">status,</if>
            <if test="issuer != null">issuer,</if>
            <if test="issuerDept != null">issuer_dept,</if>
            <if test="issuerDeptId != null">issuer_dept_id,</if>
            <if test="issuerTime != null">issuer_time,</if>
            <if test="createBy != null">create_by,</if>
            <if test="updateBy != null">update_by,</if>
            <if test="updateTime != null">update_time,</if>
            <if test="remark != null">remark,</if>
            create_time
         </trim>
        <trim prefix="values (" suffix=")" suffixOverrides=",">
            <if test="noticeTitle != null and noticeTitle != ''">#{noticeTitle},</if>
            <if test="noticeType != null and noticeType != ''">#{noticeType},</if>
            <if test="noticeContent != null">#{noticeContent},</if>
            <if test="status != null">#{status},</if>
            <if test="issuer != null">#{issuer},</if>
            <if test="issuerDept != null">#{issuerDept},</if>
            <if test="issuerDeptId != null">#{issuerDeptId},</if>
            <if test="issuerTime != null">#{issuerTime},</if>
            <if test="createBy != null">#{createBy},</if>
            <if test="updateBy != null">#{updateBy},</if>
            <if test="updateTime != null">#{updateTime},</if>
            <if test="remark != null">#{remark},</if>
            sysdate()
         </trim>
    </insert>

    <update id="updateZxNotice" parameterType="ZxNotice">
        update zx_notice
        <trim prefix="SET" suffixOverrides=",">
            <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">status = #{status},</if>
            <if test="issuer != null">issuer = #{issuer},</if>
            <if test="issuerDept != null">issuer_dept = #{issuerDept},</if>
            <if test="issuerDeptId != null">issuer_dept_id = #{issuerDeptId},</if>
            <if test="issuerTime != null">issuer_time = #{issuerTime},</if>
            <if test="createBy != null">create_by = #{createBy},</if>
            <if test="createTime != null">create_time = #{createTime},</if>
            <if test="updateBy != null">update_by = #{updateBy},</if>
            <if test="updateTime != null">update_time = #{updateTime},</if>
            <if test="remark != null">remark = #{remark},</if>
        </trim>
        where zx_notice_id = #{zxNoticeId}
    </update>
    <update id="updateZxNoticeUser" parameterType="ZxUserNotice">
        update zx_user_notice set zx_notice_type = #{zxNoticeType} where user_id = #{userId} and zx_notice_id =#{zxNoticeId}
    </update>

    <delete id="deleteZxNoticeByZxNoticeId" parameterType="Long">
        delete from zx_notice where zx_notice_id = #{zxNoticeId}
    </delete>

    <delete id="deleteZxNoticeByZxNoticeIds" parameterType="String">
        delete from zx_notice where zx_notice_id in 
        <foreach item="zxNoticeId" collection="array" open="(" separator="," close=")">
            #{zxNoticeId}
        </foreach>
    </delete>
    
    <delete id="deleteZxUserNoticeByZxNoticeIds" parameterType="String">
        delete from zx_user_notice where zx_notice_id in 
        <foreach item="zxNoticeId" collection="array" open="(" separator="," close=")">
            #{zxNoticeId}
        </foreach>
    </delete>

    <delete id="deleteZxUserNoticeByZxNoticeId" parameterType="Long">
        delete from zx_user_notice where zx_notice_id = #{zxNoticeId}
    </delete>

    <insert id="batchZxUserNotice">
        insert into zx_user_notice( user_id, user_name, zx_notice_id, create_time) values
		<foreach item="item" index="index" collection="list" separator=",">
            ( #{item.userId}, #{item.userName}, #{item.zxNoticeId}, sysdate())
        </foreach>
    </insert>

    <insert id="insterZxUserNotice" parameterType="zxUserNotice">
        insert into zx_user_notice
        <trim prefix="(" suffix=")" suffixOverrides=",">
            <if test="userId != null and userId != ''">user_id,</if>
            <if test="userName != null and userName != ''">user_name,</if>
            <if test="zxNoticeId != null">zx_notice_id,</if>
            <if test="zxNoticeType != null">zx_notice_type,</if>
            create_time
        </trim>
        <trim prefix="values (" suffix=")" suffixOverrides=",">
            <if test="userId != null and userId != ''">#{userId},</if>
            <if test="userName != null and userName != ''">#{userName},</if>
            <if test="zxNoticeId != null">#{zxNoticeId},</if>
            <if test="zxNoticeType != null">#{zxNoticeType},</if>
            sysdate()
        </trim>
    </insert>
</mapper>