<?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.xjrsoft.module.oa.mapper.NewsRelationMapper">
    <insert id="insertAllTeaStaff" parameterType="java.lang.Long">
        insert into xjr_oa_news_relation (id, news_id, user_id, create_date, read_mark)
            (select
                 UUID(),
                     #{newsId},
                     t.id,
                     NOW(),
                     0
             from xjr_user t
                      left join xjr_user_role_relation t3 on t3.user_id = t.id
                      LEFT JOIN xjr_role t4 ON t4.id = t3.role_id
             where t.delete_mark = 0 and t4.id = 2)
    </insert>

    <insert id="insertDeptUser">
        insert into xjr_oa_news_relation (id, news_id, user_id, create_date, read_mark)
        (select
        UUID(),
        #{newsId},
        t.id,
        NOW(),
        0
        from xjr_user t
        left join xjr_user_dept_relation t1 on t1.user_id = t.id
        LEFT JOIN xjr_department t2 ON t2.id = t1.dept_id
        left join xjr_user_role_relation t3 on t3.user_id = t.id
        LEFT JOIN xjr_role t4 ON t4.id = t3.role_id
        where t.delete_mark = 0 and t4.id = 2
        <if test="deptIdList != null and deptIdList.size() > 0">
            and t2.id in
            <foreach item="deptId" index="index" collection="deptIdList" open="(" close=")"
                     separator=",">
                #{deptId}
            </foreach>
        </if>)
    </insert>

    <insert id="insertClassUser">
        insert into xjr_oa_news_relation (id, news_id, user_id, create_date, read_mark)
        (select
        UUID(),
        #{newsId},
        t.id,
        NOW(),
        0
        from xjr_user t
        inner join base_student t3 on t.id = t3.user_id
        left join base_student_school_roll t1 on t1.user_id = t.id
        LEFT JOIN base_class t2 ON t2.id = t1.class_id
        where t.delete_mark = 0
        <if test="classIdList != null and classIdList.size() > 0">
            and t2.id in
            <foreach item="classId" index="index" collection="classIdList" open="(" close=")"
                     separator=",">
                #{classId}
            </foreach>
        </if>)
    </insert>

    <insert id="insertPersonl">
        insert into xjr_oa_news_relation (id, news_id, user_id, create_date, read_mark)
        (select
        UUID(),
        #{newsId},
        t.id,
        NOW(),
        0
        from xjr_user t
        where t.delete_mark = 0
        <if test="userIdList != null and userIdList.size() > 0">
            and t.id in
            <foreach item="userId" index="index" collection="userIdList" open="(" close=")"
                     separator=",">
                #{userId}
            </foreach>
        </if>)
    </insert>

</mapper>