<?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.invest.mapper.AccountsTransferRecordMapper">

    <resultMap type="AccountsTransferRecordVo" id="AccountsTransferRecordResult">
        <result property="id"    column="id"    />
        <result property="name"    column="name"    />
        <result property="type"    column="type"    />
        <result property="outAccountId"    column="out_account_id"    />
        <result property="inAccountId"    column="in_account_id"    />
        <result property="posId"    column="pos_id"    />
        <result property="commission"    column="commission"    />
        <result property="amount"    column="amount"    />
        <result property="actualAmount"    column="actual_amount"    />
        <result property="dealType"    column="deal_type"    />
        <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="remark"    column="remark"    />
        <result property="posName"    column="pos_name"    />
        <result property="outAccountName"    column="out_account_name"    />
        <result property="inAccountName"    column="in_account_name"    />
        <result property="merchantName"    column="merchant_name"    />
    </resultMap>

    <sql id="selectAccountsTransferRecordVo">
        select
        a.id,
        a.name,
        a.type,
        a.out_account_id,
        a.in_account_id,
        a.pos_id,
        a.commission,
        a.amount,
        a.actual_amount,
        a.deal_type,
        a.create_by,
        a.create_time,
        a.update_by,
        a.update_time,
        a.del_flag,
        a.remark,
        pm."name" as pos_name,
        pm.merchant_name,
        CONCAT(a3."name", '（', right(a3.code, 4), '）') as out_account_name,
        CONCAT(a4."name", '（', right(a4.code, 4), '）') as in_account_name
        from
        accounts_transfer_record a
        left join pos_machine pm on
        pm.id = a.pos_id
        left join accounts a3 on
        a3.id = a.out_account_id
        left join accounts a4 on
        a4.id = a.in_account_id

    </sql>

    <select id="selectAccountsTransferRecordList" parameterType="AccountsTransferRecordDto" resultMap="AccountsTransferRecordResult">
        <include refid="selectAccountsTransferRecordVo"/>
        <where>
            a.del_flag='0'
            <if test="name != null  and name != ''"> and a.name like '%'|| #{name}||'%'</if>
            <if test="type != null  and type != ''"> and a.type = #{type}</if>
            <if test="inAccountId != null "> and a.in_account_id = #{inAccountId}</if>
            <if test="outAccountId != null "> and a.out_account_id = #{outAccountId}</if>
            <if test="accountId != null "> and (a.out_account_id = #{accountId} or a.in_account_id = #{accountId} or a.pos_id = #{accountId})</if>
            <if test="posId != null "> and a.pos_id = #{posId}</if>
            <if test="dealType != null  and dealType != ''"> and a.deal_type = #{dealType}</if>
            <if test="endTime!=null and endTime !=''">
                and #{endTime}>=to_char(a.create_time, 'yyyy-MM-dd')
            </if>
            <if test="startTime!=null and startTime !=''">
                and to_char(a.create_time, 'yyyy-MM-dd')>=#{startTime}
            </if>
        </where>
        <!-- 数据范围过滤 -->
        ${params.dataScope}
        order by a.create_time desc
    </select>

    <select id="selectAccountsTransferRecordById" parameterType="Long" resultMap="AccountsTransferRecordResult">
        <include refid="selectAccountsTransferRecordVo"/>
        where a.id = #{id}
    </select>

    <insert id="insertAccountsTransferRecord" parameterType="AccountsTransferRecord">
        insert into accounts_transfer_record
        <trim prefix="(" suffix=")" suffixOverrides=",">
            <if test="id != null">id,</if>
            <if test="name != null">name,</if>
            <if test="type != null">type,</if>
            <if test="outAccountId != null">out_account_id,</if>
            <if test="inAccountId != null">in_account_id,</if>
            <if test="posId != null">pos_id,</if>
            <if test="commission != null">commission,</if>
            <if test="amount != null">amount,</if>
            <if test="actualAmount != null">actual_amount,</if>
            <if test="dealType != null">deal_type,</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="remark != null">remark,</if>
         </trim>
        <trim prefix="values (" suffix=")" suffixOverrides=",">
            <if test="id != null">#{id},</if>
            <if test="name != null">#{name},</if>
            <if test="type != null">#{type},</if>
            <if test="outAccountId != null">#{outAccountId},</if>
            <if test="inAccountId != null">#{inAccountId},</if>
            <if test="posId != null">#{posId},</if>
            <if test="commission != null">#{commission},</if>
            <if test="amount != null">#{amount},</if>
            <if test="actualAmount != null">#{actualAmount},</if>
            <if test="dealType != null">#{dealType},</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="remark != null">#{remark},</if>
         </trim>
    </insert>

    <update id="updateAccountsTransferRecord" parameterType="AccountsTransferRecord">
        update accounts_transfer_record
        <trim prefix="SET" suffixOverrides=",">
            <if test="name != null">name = #{name},</if>
            <if test="type != null">type = #{type},</if>
            <if test="outAccountId != null">out_account_id = #{outAccountId},</if>
            <if test="inAccountId != null">in_account_id = #{inAccountId},</if>
            <if test="posId != null">pos_id = #{posId},</if>
            <if test="commission != null">commission = #{commission},</if>
            <if test="amount != null">amount = #{amount},</if>
            <if test="actualAmount != null">actual_amount = #{actualAmount},</if>
            <if test="dealType != null">deal_type = #{dealType},</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="delFlag != null">del_flag = #{delFlag},</if>
            <if test="remark != null">remark = #{remark},</if>
        </trim>
        where id = #{id}
    </update>

    <delete id="deleteAccountsTransferRecordById" parameterType="Long">
        delete from accounts_transfer_record where id = #{id}
    </delete>

    <delete id="deleteAccountsTransferRecordByIds" parameterType="String">
        delete from accounts_transfer_record where id in
        <foreach item="id" collection="array" open="(" separator="," close=")">
            #{id}
        </foreach>
    </delete>
    <update id="removeAccountsTransferRecordById" parameterType="Long">
         update accounts_transfer_record set del_flag='1' where id = #{id}
    </update>

    <update id="removeAccountsTransferRecordByIds" parameterType="String">
        update accounts_transfer_record set del_flag='1' where id in
        <foreach item="id" collection="array" open="(" separator="," close=")">
            #{id}
        </foreach>
    </update>
</mapper>
