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

    <resultMap type="BankCardLendVo" id="BankCardLendResult">
        <result property="id"    column="id"    />
        <result property="name"    column="name"    />
        <result property="type"    column="type"    />
        <result property="code"    column="code"    />
        <result property="openingBank"    column="opening_bank"    />
        <result property="activationDate"    column="activation_date"    />
        <result property="billDate"    column="bill_date"    />
        <result property="payDate"    column="pay_date"    />
        <result property="delayPeriod"    column="delay_period"    />
        <result property="creditLimit"    column="credit_limit"    />
        <result property="effectiveDate"    column="effective_date"    />
        <result property="cvv"    column="cvv"    />
        <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="debitType"    column="debit_type"    />
        <result property="lendType"    column="lend_type"    />
    </resultMap>

    <sql id="selectBankCardLendVo">
        select
        a.name,
        a.type,
        a.credit_limit
        from
        bank_card_lend a
    </sql>

    <select id="selectBankCardLendList" parameterType="BankCardLendDto" resultMap="BankCardLendResult">
        <include refid="selectBankCardLendVo"/>
        <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="creditCardId != null "> and a.id = #{creditCardId}</if>
            <if test="lendType != null  and lendType != ''"> and a.lend_type = #{lendType}</if>
        </where>
        <!-- 数据范围过滤 -->
        ${params.dataScope}
    </select>

    <resultMap type="AccountsDealRecordVo" id="AccountsDealRecordResult">
        <result property="id"    column="id"    />
        <result property="name"    column="name"    />
         <result property="code"    column="code"    />
        <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>

    <select id="selectAccountsOutInList" parameterType="AccountsDealRecordDto" resultMap="AccountsDealRecordResult">
        select
        a.*
        from
        (
        select
        atr.id ,
        '13' as deal_category,
        '2' as deal_type,
        atr.commission as amount,
        atr.create_time ,
        atr.pos_id as account_id ,
        atr.create_by,
        atr.remark,
        p."name" as account_name ,
        p.code
        from
        accounts_transfer_record atr
        left join accounts p on
        p.id = atr.pos_id
        where
        atr."type" = '1'
        and atr.del_flag = '0'
        union
        select
        ihd.id ,
        case
        when ihd."type" = '2' then '14'
        when ihd."type" = '3' then '15'
        end as deal_category,
        '2' as deal_type,
        ihd.interest as amount,
        ihd.repayment_date as create_time,
        ihd.bank_card_lend_id as account_id ,
        ihd.create_by,
        ihd.remark,
        a."name" as account_name ,
        a.code
        from
        installment_history_detail ihd
        left join accounts a on
        a.id = ihd.bank_card_lend_id
        where
        ihd.del_flag = '0'
        union
        select
        adr.id ,
        adr.deal_category,
        adr.deal_type,
        adr.amount,
        adr.create_time ,
        adr.account_id ,
        adr.create_by,
        adr.remark,
        a."name" as account_name ,
        a.code
        from
        accounts_deal_record adr
        left join accounts a on
        a.id = adr.account_id
        where
        adr.del_flag = '0'
        and (
        adr.deal_category = '1'
        or adr.deal_category = '2'
        or adr.deal_category = '11'
        )
        ) a
        <where>
            1=1
            <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="dealCategory != null  and dealCategory != ''"> and a.deal_category = #{dealCategory}</if>
        </where>
        <!-- 数据范围过滤 -->
        ${params.dataScope}
        order by a.create_time desc
    </select>

</mapper>