<?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.branch.mapper.TBranchStudyMapper">
    
    <resultMap type="TBranchStudy" id="TBranchStudyResult">
        <result property="studyId"    column="study_id"    />
        <result property="studyTime"    column="study_time"    />
        <result property="studyVenue"    column="study_venue"    />
        <result property="lecturer"    column="lecturer"    />
        <result property="recorder"    column="recorder"    />
        <result property="participants"    column="participants"    />
        <result property="absentees"    column="absentees"    />
        <result property="contents"    column="contents"    />
        <result property="remarks"    column="remarks"    />
        <result property="delFlag"    column="del_flag"    />
        <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="deptId"       column="dept_id"      />
    </resultMap>

    <sql id="selectTBranchStudyVo">
        select
            u.study_id,
            u.study_time,
            u.study_venue,
            u.lecturer,
            u.recorder,
            u.participants,
            u.absentees,
            u.contents,
            u.remarks,
            u.del_flag,
            u.create_by,
            u.create_time,
            u.update_by,
            u.update_time,
            u.dept_id
        from t_branch_study u
        left join sys_dept d on u.dept_id = d.dept_id
    </sql>

    <select id="selectTBranchStudyList" parameterType="TBranchStudy" resultMap="TBranchStudyResult">
        <include refid="selectTBranchStudyVo"/>
        <where>  
            <if test="studyId != null "> and u.study_id = #{studyId}</if>
            <if test="studyTime != null "> and u.study_time = #{studyTime}</if>
            <if test="studyVenue != null  and studyVenue != ''"> and u.study_venue = #{studyVenue}</if>
            <if test="lecturer != null  and lecturer != ''"> and u.lecturer = #{lecturer}</if>
            <if test="recorder != null  and recorder != ''"> and u.recorder = #{recorder}</if>
            <if test="participants != null  and participants != ''"> and u.participants = #{participants}</if>
            <if test="absentees != null  and absentees != ''"> and u.absentees = #{absentees}</if>
            <if test="contents != null  and contents != ''"> and u.contents = #{contents}</if>
            <if test="remarks != null  and remarks != ''"> and u.remarks = #{remarks}</if>
            <if test="deptId != null and deptId != 0">
                AND (u.dept_id = #{deptId} OR u.dept_id IN ( SELECT t.dept_id FROM sys_dept t WHERE find_in_set(#{deptId}, ancestors) <![CDATA[ <> ]]> 0 )))
            </if>
            and u.del_flag = 0
        </where>
        <!-- 数据范围过滤 -->
        ${params.dataScope}
    </select>
    
    <select id="selectTBranchStudyByStudyId" parameterType="Long" resultMap="TBranchStudyResult">
        <include refid="selectTBranchStudyVo"/>
        where u.study_id = #{studyId}
        and u.del_flag = 0
    </select>
        
    <insert id="insertTBranchStudy" parameterType="TBranchStudy">
        <selectKey keyProperty="studyId" resultType="long" order="BEFORE">
            SELECT seq_t_branch_study.NEXTVAL as studyId FROM DUAL
        </selectKey>
        insert into t_branch_study
        <trim prefix="(" suffix=")" suffixOverrides=",">
            <if test="studyId != null">study_id,</if>
            <if test="studyTime != null">study_time,</if>
            <if test="studyVenue != null">study_venue,</if>
            <if test="lecturer != null">lecturer,</if>
            <if test="recorder != null">recorder,</if>
            <if test="participants != null">participants,</if>
            <if test="absentees != null">absentees,</if>
            <if test="contents != null">contents,</if>
            <if test="remarks != null">remarks,</if>
            <if test="delFlag != null">del_flag,</if>
            <if test="createBy != null">create_by,</if>
            <if test="createTime != null">create_time,</if>
            <if test="updateBy != null">update_by,</if>
            <if test="updateTime != null">update_time,</if>
            <if test="deptId != null and deptId != 0">dept_id,</if>
         </trim>
        <trim prefix="values (" suffix=")" suffixOverrides=",">
            <if test="studyId != null">#{studyId},</if>
            <if test="studyTime != null">#{studyTime},</if>
            <if test="studyVenue != null">#{studyVenue},</if>
            <if test="lecturer != null">#{lecturer},</if>
            <if test="recorder != null">#{recorder},</if>
            <if test="participants != null">#{participants},</if>
            <if test="absentees != null">#{absentees},</if>
            <if test="contents != null">#{contents},</if>
            <if test="remarks != null">#{remarks},</if>
            <if test="delFlag != null">#{delFlag},</if>
            <if test="createBy != null">#{createBy},</if>
            <if test="createTime != null">#{createTime},</if>
            <if test="updateBy != null">#{updateBy},</if>
            <if test="updateTime != null">#{updateTime},</if>
            <if test="deptId != null and deptId != ''">#{deptId},</if>
         </trim>
    </insert>

    <update id="updateTBranchStudy" parameterType="TBranchStudy">
        update t_branch_study
        <trim prefix="SET" suffixOverrides=",">
            <if test="studyTime != null">study_time = #{studyTime},</if>
            <if test="studyVenue != null">study_venue = #{studyVenue},</if>
            <if test="lecturer != null">lecturer = #{lecturer},</if>
            <if test="recorder != null">recorder = #{recorder},</if>
            <if test="participants != null">participants = #{participants},</if>
            <if test="absentees != null">absentees = #{absentees},</if>
            <if test="contents != null">contents = #{contents},</if>
            <if test="remarks != null">remarks = #{remarks},</if>
            <if test="delFlag != null">del_flag = #{delFlag},</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="deptId != null and deptId != 0">dept_id = #{deptId},</if>
        </trim>
        where study_id = #{studyId}
    </update>

    <update id="deleteTBranchStudyByStudyId" parameterType="Long">
        update t_branch_study set del_flag = 2 where study_id = #{studyId}
    </update>

    <update id="deleteTBranchStudyByStudyIds" parameterType="String">
        update t_branch_study set del_flag = 2 where study_id in
        <foreach item="studyId" collection="array" open="(" separator="," close=")">
            #{studyId}
        </foreach>
    </update>
</mapper>