<?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.transferDetail.mapper.TransferDetailMapper">

    <resultMap type="TransferDetailVO" id="TransferDetailResult" autoMapping="true" />

    <sql id="selectTransferDetailVo">
        select
            std.detail_id,
            std.detail_no,
            std.transfer_id,
            std.status,
            std.amount,
            std.remark,
            std.account_no,
            std.user_name,
            std.create_time,
            std.fail_reason,
            st.batch_no as transfer_batch_no
        from ss_transfer_detail std
            left join ss_transfer st on st.batch_id = std.transfer_id
    </sql>

    <sql id="searchCondition">
        <if test="query.detailId != null "> and std.detail_id = #{query.detailId}</if>
        <if test="query.detailNo != null  and query.detailNo != ''"> and std.detail_no like concat('%', #{query.detailNo}, '%')</if>
        <if test="query.transferId != null "> and std.transfer_id = #{query.transferId}</if>
        <if test="query.transferBatchNo != null and query.transferBatchNo != '' "> and st.batch_no like concat('%', #{query.transferBatchNo}, '%')</if>
        <if test="query.status != null  and query.status != ''"> and std.status = #{query.status}</if>
        <if test="query.remark != null  and query.remark != ''"> and std.remark like concat('%', #{query.remark}, '%')</if>
        <if test="query.accountNo != null  and query.accountNo != ''"> and std.account_no like concat('%', #{query.accountNo}, '%')</if>
        <if test="query.userName != null  and query.userName != ''"> and std.user_name like concat('%', #{query.userName}, '%')</if>
        <if test="query.transferIds != null and query.transferIds.size() > 0">
            and std.transfer_id in
            <foreach collection="query.transferIds" item="item" open="(" separator="," close=")">
                #{item}
            </foreach>
        </if>
    </sql>

    <select id="selectTransferDetailList" parameterType="TransferDetailQuery" resultMap="TransferDetailResult">
        <include refid="selectTransferDetailVo"/>
        <where>
            <include refid="searchCondition"/>
        </where>
    </select>

    <select id="selectTransferDetailByDetailId" parameterType="Long" resultMap="TransferDetailResult">
        <include refid="selectTransferDetailVo"/>
        where std.detail_id = #{detailId}
    </select>

    <insert id="insertTransferDetail" parameterType="TransferDetail" useGeneratedKeys="true" keyProperty="detailId">
        insert into ss_transfer_detail
        <trim prefix="(" suffix=")" suffixOverrides=",">
            <if test="detailNo != null and detailNo != ''">detail_no,</if>
            <if test="transferId != null">transfer_id,</if>
            <if test="status != null and status != ''">`status`,</if>
            <if test="amount != null">amount,</if>
            <if test="remark != null and remark != ''">remark,</if>
            <if test="accountNo != null and accountNo != ''">account_no,</if>
            <if test="userName != null">user_name,</if>
            <if test="createTime != null">create_time,</if>
            <if test="failReason != null">fail_reason,</if>
         </trim>
        <trim prefix="values (" suffix=")" suffixOverrides=",">
            <if test="detailNo != null and detailNo != ''">#{detailNo},</if>
            <if test="transferId != null">#{transferId},</if>
            <if test="status != null and status != ''">#{status},</if>
            <if test="amount != null">#{amount},</if>
            <if test="remark != null and remark != ''">#{remark},</if>
            <if test="accountNo != null and accountNo != ''">#{accountNo},</if>
            <if test="userName != null">#{userName},</if>
            <if test="createTime != null">#{createTime},</if>
            <if test="failReason != null">#{failReason},</if>
         </trim>
    </insert>


    <insert id="batchInsert">
        insert into ss_transfer_detail(
            detail_no,
            transfer_id,
            status,
            amount,
            remark,
            account_no,
            user_name,
            create_time,
            fail_reason
        )
        values
        <foreach collection="list" item="i" separator=",">
            <trim prefix="(" suffix=")" suffixOverrides=",">
                <if test="i.detailNo != null and i.detailNo != ''">#{i.detailNo},</if>
                <if test="i.detailNo == null or i.detailNo == ''">default,</if>
                <if test="i.transferId != null">#{i.transferId},</if>
                <if test="i.transferId == null">default,</if>
                <if test="i.status != null and i.status != ''">#{i.status},</if>
                <if test="i.status == null or i.status == ''">default,</if>
                <if test="i.amount != null">#{i.amount},</if>
                <if test="i.amount == null">default,</if>
                <if test="i.remark != null and i.remark != ''">#{i.remark},</if>
                <if test="i.remark == null or i.remark == ''">default,</if>
                <if test="i.accountNo != null and i.accountNo != ''">#{i.accountNo},</if>
                <if test="i.accountNo == null or i.accountNo == ''">default,</if>
                <if test="i.userName != null">#{i.userName},</if>
                <if test="i.userName == null">default,</if>
                <if test="i.createTime != null">#{i.createTime},</if>
                <if test="i.createTime == null">default,</if>
                <if test="i.failReason != null and i.failReason != ''">#{i.failReason},</if>
                <if test="i.failReason == null or i.failReason == ''">default,</if>
            </trim>
        </foreach>
    </insert>

    <update id="updateTransferDetail" parameterType="TransferDetail">
        update ss_transfer_detail
        <trim prefix="SET" suffixOverrides=",">
            <include refid="updateColumns"/>
        </trim>
        where detail_id = #{data.detailId}
    </update>

    <sql id="updateColumns">
        <if test="data.detailNo != null and data.detailNo != ''">detail_no = #{data.detailNo},</if>
        <if test="data.transferId != null">transfer_id = #{data.transferId},</if>
        <if test="data.status != null and data.status != ''">`status` = #{data.status},</if>
        <if test="data.amount != null">amount = #{data.amount},</if>
        <if test="data.remark != null and data.remark != ''">remark = #{data.remark},</if>
        <if test="data.accountNo != null and data.accountNo != ''">account_no = #{data.accountNo},</if>
        <if test="data.userName != null">user_name = #{data.userName},</if>
        <if test="data.createTime != null">create_time = #{data.createTime},</if>
        <if test="data.failReason != null and data.failReason != ''">fail_reason = #{data.failReason},</if>
    </sql>

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

    <delete id="deleteTransferDetailByDetailId" parameterType="Long">
        delete from ss_transfer_detail where detail_id = #{detailId}
    </delete>

    <delete id="deleteTransferDetailByDetailIds" parameterType="String">
        delete from ss_transfer_detail where detail_id in
        <foreach item="detailId" collection="array" open="(" separator="," close=")">
            #{detailId}
        </foreach>
    </delete>
</mapper>
