<?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.ss.transfer.mapper.TransferMapper">

    <resultMap type="TransferVO" id="TransferResult" autoMapping="true" />

    <sql id="selectTransferVo">
        select
            st.batch_id,
            st.batch_no,
            st.account_type,
            st.bst_type,
            st.bst_id,
            st.status,
            st.batch_name,
            st.batch_remark,
            st.create_time,
            st.total_amount,
            st.close_reason,
            st.update_time
        from ss_transfer st
    </sql>

    <sql id="searchCondition">
        <if test="query.batchId != null "> and st.batch_id = #{query.batchId}</if>
        <if test="query.batchNo != null  and query.batchNo != ''"> and st.batch_no = #{query.batchNo}</if>
        <if test="query.accountType != null  and query.accountType != ''"> and st.account_type = #{query.accountType}</if>
        <if test="query.bstType != null  and query.bstType != ''"> and st.bst_type = #{query.bstType}</if>
        <if test="query.bstId != null "> and st.bst_id = #{query.bstId}</if>
        <if test="query.status != null  and query.status != ''"> and st.status = #{query.status}</if>
        <if test="query.batchName != null  and query.batchName != ''"> and st.batch_name like concat('%', #{query.batchName}, '%')</if>
        <if test="query.batchRemark != null  and query.batchRemark != ''"> and st.batch_remark like concat('%', #{query.batchRemark}, '%')</if>
    </sql>

    <select id="selectTransferList" parameterType="TransferQuery" resultMap="TransferResult">
        <include refid="selectTransferVo"/>
        <where>
            <include refid="searchCondition"/>
        </where>
    </select>

    <select id="selectTransferByBatchId" parameterType="Long" resultMap="TransferResult">
        <include refid="selectTransferVo"/>
        where st.batch_id = #{batchId}
    </select>

    <select id="selectByBatchNo" resultMap="TransferResult">
        <include refid="selectTransferVo"/>
        where st.batch_no = #{batchNo}
    </select>

    <insert id="insertTransfer" parameterType="Transfer" useGeneratedKeys="true" keyProperty="batchId">
        <selectKey keyProperty="batchId" order="AFTER" resultType="Long">
            SELECT LAST_INSERT_ID();
        </selectKey>
        insert into ss_transfer
        <trim prefix="(" suffix=")" suffixOverrides=",">
            <if test="batchNo != null and batchNo != ''">batch_no,</if>
            <if test="accountType != null and accountType != ''">account_type,</if>
            <if test="bstType != null and bstType != ''">bst_type,</if>
            <if test="bstId != null">bst_id,</if>
            <if test="status != null and status != ''">`status`,</if>
            <if test="batchName != null and batchName != ''">batch_name,</if>
            <if test="batchRemark != null and batchRemark != ''">batch_remark,</if>
            <if test="createTime != null">create_time,</if>
            <if test="totalAmount != null">total_amount,</if>
            <if test="closeReason != null and closeReason != ''">close_reason,</if>
            <if test="updateTime != null">update_time,</if>
         </trim>
        <trim prefix="values (" suffix=")" suffixOverrides=",">
            <if test="batchNo != null and batchNo != ''">#{batchNo},</if>
            <if test="accountType != null and accountType != ''">#{accountType},</if>
            <if test="bstType != null and bstType != ''">#{bstType},</if>
            <if test="bstId != null">#{bstId},</if>
            <if test="status != null and status != ''">#{status},</if>
            <if test="batchName != null and batchName != ''">#{batchName},</if>
            <if test="batchRemark != null and batchRemark != ''">#{batchRemark},</if>
            <if test="createTime != null">#{createTime},</if>
            <if test="totalAmount != null">#{totalAmount},</if>
            <if test="closeReason != null and closeReason != ''">#{closeReason},</if>
            <if test="updateTime != null">#{updateTime},</if>
         </trim>
    </insert>

    <update id="updateTransfer" parameterType="Transfer">
        update ss_transfer
        <trim prefix="SET" suffixOverrides=",">
            <include refid="updateColumns"/>
        </trim>
        where batch_id = #{data.batchId}
    </update>

    <sql id="updateColumns">
        <if test="data.batchNo != null and data.batchNo != ''">batch_no = #{data.batchNo},</if>
        <if test="data.accountType != null and data.accountType != ''">account_type = #{data.accountType},</if>
        <if test="data.bstType != null and data.bstType != ''">bst_type = #{data.bstType},</if>
        <if test="data.bstId != null">bst_id = #{data.bstId},</if>
        <if test="data.status != null and data.status != ''">`status` = #{data.status},</if>
        <if test="data.batchName != null and data.batchName != ''">batch_name = #{data.batchName},</if>
        <if test="data.batchRemark != null and data.batchRemark != ''">batch_remark = #{data.batchRemark},</if>
        <if test="data.createTime != null">create_time = #{data.createTime},</if>
        <if test="data.totalAmount != null">total_amount = #{data.totalAmount},</if>
        <if test="data.closeReason != null and data.closeReason != ''">close_reason = #{data.closeReason},</if>
        <if test="data.updateTime != null">update_time = #{data.updateTime},</if>
    </sql>

    <update id="updateByQuery">
        update ss_transfer st
        <trim prefix="SET" suffixOverrides=",">
            <include refid="updateColumns"/>
        </trim>
        <where>
            <include refid="searchCondition"/>
        </where>
    </update>

    <delete id="deleteTransferByBatchId" parameterType="Long">
        delete from ss_transfer where batch_id = #{batchId}
    </delete>

    <delete id="deleteTransferByBatchIds" parameterType="String">
        delete from ss_transfer where batch_id in
        <foreach item="batchId" collection="array" open="(" separator="," close=")">
            #{batchId}
        </foreach>
    </delete>
</mapper>
