<?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.TBranchActivityBudgetMapper">
    
    <resultMap type="TBranchActivityBudget" id="TBranchActivityBudgetResult">
        <result property="budgetId"    column="budget_id"    />
        <result property="noticeId"    column="notice_id"    />
        <result property="budgetItem"    column="budget_item"    />
        <result property="budgetAmount"    column="budget_amount"    />
        <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="selectTBranchActivityBudgetVo">
        select u.budget_id, u.notice_id, u.budget_item, u.budget_amount, u.del_flag, u.create_by, u.create_time, u.update_by, u.update_time, u.dept_id from t_branch_activity_budget u left join sys_dept d on u.dept_id = d.dept_id
    </sql>

    <select id="selectTBranchActivityBudgetList" parameterType="TBranchActivityBudget" resultMap="TBranchActivityBudgetResult">
        <include refid="selectTBranchActivityBudgetVo"/>
        <where>  
            <if test="budgetId != null "> and u.budget_id = #{budgetId}</if>
            <if test="noticeId != null "> and u.notice_id = #{noticeId}</if>
            <if test="budgetItem != null  and budgetItem != ''"> and u.budget_item = #{budgetItem}</if>
            <if test="budgetAmount != null "> and u.budget_amount = #{budgetAmount}</if>
            <if test="deptId != null "> and u.dept_id = #{deptId}</if>
            and u.del_flag = 0
        </where>
        <!-- 数据范围过滤 -->
        ${params.dataScope}
    </select>
    
    <select id="selectTBranchActivityBudgetByBudgetId" parameterType="Long" resultMap="TBranchActivityBudgetResult">
        <include refid="selectTBranchActivityBudgetVo"/>
        where u.budget_id = #{budgetId}
        and u.del_flag = 0
    </select>
        
    <insert id="insertTBranchActivityBudget" parameterType="TBranchActivityBudget">
        <selectKey keyProperty="budgetId" resultType="long" order="BEFORE">
            SELECT seq_t_branch_activity_budget.NEXTVAL as budgetId FROM DUAL
        </selectKey>
        insert into t_branch_activity_budget
        <trim prefix="(" suffix=")" suffixOverrides=",">
            <if test="budgetId != null">budget_id,</if>
            <if test="noticeId != null">notice_id,</if>
            <if test="budgetItem != null">budget_item,</if>
            <if test="budgetAmount != null">budget_amount,</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="budgetId != null">#{budgetId},</if>
            <if test="noticeId != null">#{noticeId},</if>
            <if test="budgetItem != null">#{budgetItem},</if>
            <if test="budgetAmount != null">#{budgetAmount},</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="updateTBranchActivityBudget" parameterType="TBranchActivityBudget">
        update t_branch_activity_budget
        <trim prefix="SET" suffixOverrides=",">
            <if test="noticeId != null">notice_id = #{noticeId},</if>
            <if test="budgetItem != null">budget_item = #{budgetItem},</if>
            <if test="budgetAmount != null">budget_amount = #{budgetAmount},</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 budget_id = #{budgetId}
    </update>

    <update id="deleteTBranchActivityBudgetByBudgetId" parameterType="Long">
        update t_branch_activity_budget set del_flag = 2 where budget_id = #{budgetId}
    </update>

    <update id="deleteTBranchActivityBudgetByNoticeId" parameterType="Long">
        update t_branch_activity_budget set del_flag = 2 where notice_id = #{noticeId}
    </update>

    <update id="deleteTBranchActivityBudgetByBudgetIds" parameterType="String">
        update t_branch_activity_budget set del_flag = 2 where budget_id in
        <foreach item="budgetId" collection="array" open="(" separator="," close=")">
            #{budgetId}
        </foreach>
    </update>
</mapper>