<?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.frequency.bondrecord.mapper.BondRecordMapper">
    
    <resultMap type="com.ruoyi.frequency.bondrecord.entity.BondRecord" id="BondRecordResult">
        <result property="id"    column="id"    />
        <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="delFlag"    column="del_flag"    />
        <result property="userId"    column="user_id"    />
        <result property="amount"    column="amount"    />
        <result property="payType"    column="pay_type"    />
        <result property="aliBusinessId"    column="ali_business_id"    />
    </resultMap>

    <sql id="selectBondRecordVo">
        select id, create_by, create_time, update_by, update_time, del_flag, user_id, amount, pay_type, ali_business_id from t_bond_record
    </sql>

    <select id="selectBondRecordList" parameterType="com.ruoyi.frequency.bondrecord.entity.BondRecord" resultMap="BondRecordResult">
        <include refid="selectBondRecordVo"/>
        <where>  
            <if test="createTime != null "> and create_time = #{createTime}</if>
            <if test="userId != null "> and user_id = #{userId}</if>
            <if test="amount != null "> and amount = #{amount}</if>
            <if test="payType != null "> and pay_type = #{payType}</if>
            <if test="aliBusinessId != null  and aliBusinessId != ''"> and ali_business_id = #{aliBusinessId}</if>
        </where>
    </select>
    
    <select id="selectBondRecordById" parameterType="Long" resultMap="BondRecordResult">
        <include refid="selectBondRecordVo"/>
        where id = #{id}
    </select>
        
    <insert id="insertBondRecord" parameterType="com.ruoyi.frequency.bondrecord.entity.BondRecord">
        insert into t_bond_record
        <trim prefix="(" suffix=")" suffixOverrides=",">
            <if test="id != null">id,</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="delFlag != null">del_flag,</if>
            <if test="userId != null">user_id,</if>
            <if test="amount != null">amount,</if>
            <if test="payType != null">pay_type,</if>
            <if test="aliBusinessId != null">ali_business_id,</if>
         </trim>
        <trim prefix="values (" suffix=")" suffixOverrides=",">
            <if test="id != null">#{id},</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="delFlag != null">#{delFlag},</if>
            <if test="userId != null">#{userId},</if>
            <if test="amount != null">#{amount},</if>
            <if test="payType != null">#{payType},</if>
            <if test="aliBusinessId != null">#{aliBusinessId},</if>
         </trim>
    </insert>

    <update id="updateBondRecord" parameterType="com.ruoyi.frequency.bondrecord.entity.BondRecord">
        update t_bond_record
        <trim prefix="SET" suffixOverrides=",">
            <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="delFlag != null">del_flag = #{delFlag},</if>
            <if test="userId != null">user_id = #{userId},</if>
            <if test="amount != null">amount = #{amount},</if>
            <if test="payType != null">pay_type = #{payType},</if>
            <if test="aliBusinessId != null">ali_business_id = #{aliBusinessId},</if>
        </trim>
        where id = #{id}
    </update>

    <delete id="deleteBondRecordById" parameterType="Long">
        delete from t_bond_record where id = #{id}
    </delete>

    <delete id="deleteBondRecordByIds" parameterType="String">
        delete from t_bond_record where id in 
        <foreach item="id" collection="array" open="(" separator="," close=")">
            #{id}
        </foreach>
    </delete>

    <select id="bondRecordPage" resultType="com.ruoyi.frequency.bondrecord.entity.BondRecord">
        SELECT
            *
        FROM
            (
                SELECT
                    t2.id,
                    t2.object_id,
                    t2.create_by,
                    t2.create_time,
                    t2.update_by,
                    t2.update_time,
                    t2.del_flag,
                    t2.user_id,
                    t2.amount,
                    t2.current_amount,
                    t2.type,
                    NULL bank,
                    NULL subbranch,
                    NULL bank_card,
                    NULL process_time,
                    NULL transfer_voucher,
                    NULL STATUS,
                    NULL remarks,
                    NULL full_name,
                    NULL applyTime
                FROM
                    t_bond_record AS t
                        LEFT JOIN t_bond_amount_record AS t2 ON t.id = t2.object_id
                        AND t2.type = 1
                WHERE
                    t2.del_flag = 1 UNION ALL
                SELECT
                    t2.id,
                    t2.object_id,
                    t2.create_by,
                    t2.create_time,
                    t2.update_by,
                    t2.update_time,
                    t2.del_flag,
                    t2.user_id,
                    t2.amount,
                    t2.current_amount,
                    t2.type,
                    t.bank,
                    t.subbranch,
                    t.bank_card,
                    t.process_time,
                    t.transfer_voucher,
                    t.STATUS,
                    t.remarks,
                    t.full_name,
                    t.create_time applyTime
                FROM
                    t_bond_withdrawal_record AS t
                        LEFT JOIN t_bond_amount_record AS t2 ON t.id = t2.object_id
                        AND t2.type != 1
		AND ((
				t2.type = 2
				AND t.`status` = 2
				)
			OR ( t2.type = 3 AND t.`status` = 3 )
			OR t.`status` = 1
		)
                WHERE
                    t2.del_flag = 1
            ) tt
        WHERE
            tt.user_id = #{userVo.userId}
        ORDER BY
            tt.create_time DESC
    </select>

    <select id="bondRecordDetail" resultType="com.ruoyi.frequency.bondrecord.entity.BondRecord">
        SELECT
            t2.id,
            t2.create_by,
            t2.create_time,
            t2.update_by,
            t2.update_time,
            t2.del_flag,
            t2.user_id,
            t2.amount,
            t.pay_type,
            t.ali_business_id,
            t.pay_order_no,
            t.pay_status,
            t2.current_amount,
            t2.type,
            t3.bank,
            t3.subbranch,
            t3.bank_card,
            t3.transfer_voucher,
            t3.status,
            t3.remarks,
            t3.full_name,
            t3.process_time,
            t3.create_time applyTime,
            t3.refuse_reason
        FROM
            t_bond_amount_record AS t2
                LEFT JOIN t_bond_record AS t ON t.id = t2.object_id
                AND t2.type = 1
            LEFT JOIN t_bond_withdrawal_record AS t3 ON t3.id = t2.object_id
            AND t2.type != 1
        WHERE
            t2.id = #{id}
    </select>
</mapper>