<?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.AccountsDealRecordMapper">

    <resultMap type="AccountsDealRecordVo" id="AccountsDealRecordResult">
        <result property="id"    column="id"    />
        <result property="name"    column="name"    />
        <result property="type"    column="type"    />
        <result property="accountId"    column="account_id"    />
        <result property="amount"    column="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="dealCategory"    column="deal_category"    />
        <result property="accountName"    column="account_name"    />
        <result property="transferRecordId"    column="transfer_record_id"    />
        <result property="currentBalance"    column="current_balance"    />
    </resultMap>

    <sql id="selectAccountsDealRecordVo">
        select
        a.id,
        a.name,
        a.type,
        a.account_id,
        a.amount,
        a.transfer_record_id,
        a.deal_type,
        a.create_by,
        a.create_time,
        a.update_by,
        a.update_time,
        a.del_flag,
        a.remark,
        a.deal_category,
        a.current_balance,
        CONCAT(a2."name", '（', right(a2.code, 4), '）') as account_name
        from
        accounts_deal_record a
        left join accounts a2 on a2.id =a.account_id

    </sql>

    <select id="selectAccountsDealRecordList" parameterType="AccountsDealRecordDto" resultMap="AccountsDealRecordResult">
        <include refid="selectAccountsDealRecordVo"/>
        <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="accountId != null  and accountId != ''"> and a.account_id = #{accountId}</if>
            <if test="dealType != null  and dealType != ''"> and a.deal_type = #{dealType}</if>
            <if test="dealCategory != null  and dealCategory != ''"> and a.deal_category = #{dealCategory}</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>
            <if test="endDateTime!=null">
                and #{endDateTime}>=a.create_time
            </if>
            <if test="startDateTime!=null">
                and a.create_time>=#{startDateTime}
            </if>
            <if test="creditBill!=null">
                and a.deal_category!=#{creditBill}
            </if>

        </where>
        <!-- 数据范围过滤 -->
        ${params.dataScope}
        order by a.create_time desc
    </select>

    <select id="selectAccountsDealRecordById" parameterType="Long" resultMap="AccountsDealRecordResult">
        <include refid="selectAccountsDealRecordVo"/>
        where a.id = #{id}
    </select>

    <insert id="insertAccountsDealRecord" parameterType="AccountsDealRecord">
        insert into accounts_deal_record
        <trim prefix="(" suffix=")" suffixOverrides=",">
            <if test="id != null">id,</if>
            <if test="name != null and name != ''">name,</if>
            <if test="type != null and type != ''">type,</if>
            <if test="accountId != null and accountId != ''">account_id,</if>
            <if test="amount != null">amount,</if>
            <if test="dealType != null and dealType != ''">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>
            <if test="dealCategory != null and dealCategory != ''">deal_category,</if>
            <if test="transferRecordId != null and transferRecordId != ''">transfer_record_id,</if>
            <if test="currentBalance != null">current_balance,</if>
         </trim>
        <trim prefix="values (" suffix=")" suffixOverrides=",">
            <if test="id != null">#{id},</if>
            <if test="name != null and name != ''">#{name},</if>
            <if test="type != null and type != ''">#{type},</if>
            <if test="accountId != null and accountId != ''">#{accountId},</if>
            <if test="amount != null">#{amount},</if>
            <if test="dealType != null and dealType != ''">#{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>
            <if test="dealCategory != null and dealCategory != ''">#{dealCategory},</if>
            <if test="transferRecordId != null and transferRecordId != ''">#{transferRecordId},</if>
            <if test="currentBalance != null">#{currentBalance},</if>
         </trim>
    </insert>

    <update id="updateAccountsDealRecord" parameterType="AccountsDealRecord">
        update accounts_deal_record
        <trim prefix="SET" suffixOverrides=",">
            <if test="name != null and name != ''">name = #{name},</if>
            <if test="type != null and type != ''">type = #{type},</if>
            <if test="accountId != null and accountId != ''">account_id = #{accountId},</if>
            <if test="amount != null">amount = #{amount},</if>
            <if test="dealType != null and dealType != ''">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>
            <if test="dealCategory != null and dealCategory != ''">deal_category = #{dealCategory},</if>
            <if test="transferRecordId != null and transferRecordId != ''">transfer_record_id = #{transferRecordId},</if>
            <if test="currentBalance != null ">current_balance = #{currentBalance},</if>
        </trim>
        where id = #{id}
    </update>

    <delete id="deleteAccountsDealRecordById" parameterType="Long">
        delete from accounts_deal_record where id = #{id}
    </delete>

    <delete id="deleteAccountsDealRecordByTransferId" parameterType="Long">
        delete from accounts_deal_record where transfer_record_id = #{id}
    </delete>

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

    <update id="removeAccountsDealRecordByIds" parameterType="String">
        update accounts_deal_record set del_flag='1' where id in
        <foreach item="id" collection="array" open="(" separator="," close=")">
            #{id}
        </foreach>
    </update>
    <update id="batchPlusCurrentBalance" >
        update accounts_deal_record
        <trim prefix="SET" suffixOverrides=",">
            <if test="currentBalance != null ">current_balance = current_balance+#{currentBalance},</if>
        </trim>
        where account_id = #{accountId}
        <if test="startDateTime!=null">
            and create_time>=#{startDateTime}
        </if>
    </update>

    <update id="batchMinusCurrentBalance" >
        update accounts_deal_record
        <trim prefix="SET" suffixOverrides=",">
            <if test="currentBalance != null ">current_balance = current_balance-#{currentBalance},</if>
        </trim>
        where account_id = #{accountId}
        <if test="startDateTime!=null">
            and create_time>=#{startDateTime}
        </if>
    </update>

    <select id="getLastOneAccountsDealRecordByMap"  resultMap="AccountsDealRecordResult">
        <include refid="selectAccountsDealRecordVo"/>
        where a.account_id = #{accountId}
        and  a.del_flag='0'
        <if test="startDateTime!=null">
            and #{startDateTime}>=a.create_time
        </if>
        order by  a.create_time desc
        limit 1
    </select>
</mapper>
