<?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.TBranchPlanMapper">
    
    <resultMap type="TBranchPlan" id="TBranchPlanResult">
        <result property="planId"    column="plan_id"    />
        <result property="planTitle"    column="plan_title"    />
        <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="selectTBranchPlanVo">
        select u.plan_id, u.plan_title, u.del_flag, u.create_by, u.create_time, u.update_by, u.update_time, u.dept_id from t_branch_plan u left join sys_dept d on u.dept_id = d.dept_id
    </sql>

    <select id="selectTBranchPlanList" parameterType="TBranchPlan" resultMap="TBranchPlanResult">
        <include refid="selectTBranchPlanVo"/>
        <where>  
            <if test="planId != null "> and u.plan_id = #{planId}</if>
            <if test="planTitle != null  and planTitle != ''"> and u.plan_title like concat('%', #{planTitle}, '%')</if>
            <if test="deptId != null "> and u.dept_id = #{deptId}</if>
            and u.del_flag = 0
        </where>
        <!-- 数据范围过滤 -->
        ${params.dataScope}
    </select>
    
    <select id="selectTBranchPlanByPlanId" parameterType="Long" resultMap="TBranchPlanResult">
        <include refid="selectTBranchPlanVo"/>
        where u.plan_id = #{planId}
        and u.del_flag = 0
    </select>
        
    <insert id="insertTBranchPlan" parameterType="TBranchPlan">
        <selectKey keyProperty="planId" resultType="long" order="BEFORE">
            SELECT seq_t_branch_plan.NEXTVAL as planId FROM DUAL
        </selectKey>
        insert into t_branch_plan
        <trim prefix="(" suffix=")" suffixOverrides=",">
            <if test="planId != null">plan_id,</if>
            <if test="planTitle != null">plan_title,</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">dept_id,</if>
         </trim>
        <trim prefix="values (" suffix=")" suffixOverrides=",">
            <if test="planId != null">#{planId},</if>
            <if test="planTitle != null">#{planTitle},</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">#{deptId},</if>
         </trim>
    </insert>

    <update id="updateTBranchPlan" parameterType="TBranchPlan">
        update t_branch_plan
        <trim prefix="SET" suffixOverrides=",">
            <if test="planTitle != null">plan_title = #{planTitle},</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">dept_id = #{deptId},</if>
        </trim>
        where plan_id = #{planId}
    </update>

    <update id="deleteTBranchPlanByPlanId" parameterType="Long">
        update t_branch_plan set del_flag = 2 where plan_id = #{planId}
    </update>

    <update id="deleteTBranchPlanByPlanIds" parameterType="String">
        update t_branch_plan set del_flag = 2 where plan_id in
        <foreach item="planId" collection="array" open="(" separator="," close=")">
            #{planId}
        </foreach>
    </update>
</mapper>