<?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.TBranchDuesMapper">
    
    <resultMap type="TBranchDues" id="TBranchDuesResult">
        <result property="duesId"    column="dues_id"    />
        <result property="duesTime"    column="dues_time"    />
        <result property="duesItem"    column="dues_item"    />
        <result property="duesInOut"    column="dues_in_out"    />
        <result property="duesType"    column="dues_type"    />
        <result property="duesAmount"    column="dues_amount"    />
        <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="selectTBranchDuesVo">
        select u.dues_id, u.dues_time, u.dues_item, u.dues_in_out, u.dues_type, u.dues_amount, u.remarks, u.del_flag, u.create_by, u.create_time, u.update_by, u.update_time, u.dept_id from t_branch_dues u left join sys_dept d on u.dept_id = d.dept_id
    </sql>

    <select id="selectTBranchDuesList" parameterType="TBranchDues" resultMap="TBranchDuesResult">
        <include refid="selectTBranchDuesVo"/>
        <where>  
            <if test="duesId != null "> and u.dues_id = #{duesId}</if>
            <if test="duesYear != null "> and extract(year from u.dues_time) = #{duesYear}</if>
            <if test="duesTime != null "> and u.dues_time = #{duesTime}</if>
            <if test="duesItem != null  and duesItem != ''"> and u.dues_item like concat('%', #{duesItem}, '%')</if>
            <if test="duesInOut != null  and duesInOut != ''"> and u.dues_in_out = #{duesInOut}</if>
            <if test="duesType != null  and duesType != ''"> and u.dues_type = #{duesType}</if>
            <if test="duesAmount != null "> and u.dues_amount = #{duesAmount}</if>
            <if test="remarks != null  and remarks != ''"> and u.remarks like concat('%', #{remarks}, '%')</if>
            <if test="deptId != null "> and u.dept_id = #{deptId}</if>
            and u.del_flag = 0
        </where>
        <!-- 数据范围过滤 -->
        ${params.dataScope}
    </select>
    
    <select id="selectTBranchDuesByDuesId" parameterType="Long" resultMap="TBranchDuesResult">
        <include refid="selectTBranchDuesVo"/>
        where u.dues_id = #{duesId}
        and u.del_flag = 0
    </select>
        
    <insert id="insertTBranchDues" parameterType="TBranchDues">
        <selectKey keyProperty="duesId" resultType="long" order="BEFORE">
            SELECT seq_t_branch_dues.NEXTVAL as duesId FROM DUAL
        </selectKey>
        insert into t_branch_dues
        <trim prefix="(" suffix=")" suffixOverrides=",">
            <if test="duesId != null">dues_id,</if>
            <if test="duesTime != null">dues_time,</if>
            <if test="duesItem != null">dues_item,</if>
            <if test="duesInOut != null">dues_in_out,</if>
            <if test="duesType != null">dues_type,</if>
            <if test="duesAmount != null">dues_amount,</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">dept_id,</if>
         </trim>
        <trim prefix="values (" suffix=")" suffixOverrides=",">
            <if test="duesId != null">#{duesId},</if>
            <if test="duesTime != null">#{duesTime},</if>
            <if test="duesItem != null">#{duesItem},</if>
            <if test="duesInOut != null">#{duesInOut},</if>
            <if test="duesType != null">#{duesType},</if>
            <if test="duesAmount != null">#{duesAmount},</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">#{deptId},</if>
         </trim>
    </insert>

    <update id="updateTBranchDues" parameterType="TBranchDues">
        update t_branch_dues
        <trim prefix="SET" suffixOverrides=",">
            <if test="duesTime != null">dues_time = #{duesTime},</if>
            <if test="duesItem != null">dues_item = #{duesItem},</if>
            <if test="duesInOut != null">dues_in_out = #{duesInOut},</if>
            <if test="duesType != null">dues_type = #{duesType},</if>
            <if test="duesAmount != null">dues_amount = #{duesAmount},</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">dept_id = #{deptId},</if>
        </trim>
        where dues_id = #{duesId}
    </update>

    <update id="deleteTBranchDuesByDuesId" parameterType="Long">
        update t_branch_dues set del_flag = 2 where dues_id = #{duesId}
    </update>

    <update id="deleteTBranchDuesByDuesIds" parameterType="String">
        update t_branch_dues set del_flag = 2 where dues_id in
        <foreach item="duesId" collection="array" open="(" separator="," close=")">
            #{duesId}
        </foreach>
    </update>
</mapper>