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

    <resultMap type="InstallmentHistoryVo" id="InstallmentHistoryResult">
        <result property="id"    column="id"    />
        <result property="name"    column="name"    />
        <result property="type"    column="type"    />
        <result property="code"    column="code"    />
        <result property="bankCardLendId"    column="bank_card_lend_id"    />
        <result property="installmentAmount"    column="installment_amount"    />
        <result property="installmentDate"    column="installment_date"    />
        <result property="period"    column="period"    />
        <result property="repaidPeriod"    column="repaid_period"    />
        <result property="totalInterest"    column="total_interest"    />
        <result property="interestRate"    column="interest_rate"    />
        <result property="dueDate"    column="due_date"    />
        <result property="closeDate"    column="close_date"    />
        <result property="state"    column="state"    />
        <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="balance"    column="balance"    />
        <result property="bankName"    column="bank_name"    />
        <result property="bankCode"    column="bank_code"    />
        <result property="repaymentDate"    column="repayment_date"    />
        <result property="calculateInterestRate"    column="calculate_interest_rate"    />
        <result property="outstandingInterest"    column="outstanding_interest"    />
        <result property="outstandingPrincipal"    column="outstanding_principal"    />
    </resultMap>

    <sql id="selectInstallmentHistoryVo">
        select
        a.id,
        a.name,
        a.type,
        a.code,
        a.bank_card_lend_id,
        a.installment_amount,
        a.installment_date,
        a.period,
        a.calculate_interest_rate,
        (
        select
        CASE
        WHEN sum(ihd.current_amount) is  null THEN 0
        ELSE sum(ihd.current_amount)
        END
        from
        installment_history_detail ihd
        where
        ihd.installment_history_id = a.id
        and ihd.posting_state = '0'
        and ihd.del_flag='0'
        ) as balance,
        (
        select
        CASE
        WHEN sum(ihd.interest) is  null THEN 0
        ELSE sum(ihd.interest)
        END
        from
        installment_history_detail ihd
        where
        ihd.installment_history_id = a.id
        and ihd.posting_state = '0'
        and ihd.del_flag='0'
        ) as outstanding_interest,
        (
        select
        CASE
        WHEN sum(ihd.principal) is  null THEN 0
        ELSE sum(ihd.principal)
        END
        from
        installment_history_detail ihd
        where
        ihd.installment_history_id = a.id
        and ihd.posting_state = '0'
        and ihd.del_flag='0'
        ) as outstanding_principal,
        (
        select
        min(d.repayment_date)
        from
        installment_history_detail d
        where
        d.installment_history_id = a.id
        and d.posting_state = '0')
        as repayment_date,
        a.repaid_period,
        a.total_interest,
        a.interest_rate,
        a.due_date,
        a.close_date,
        a.state,
        a.create_by,
        a.create_time,
        a.update_by,
        a.update_time,
        a.del_flag,
        a.remark,
        bc."name" as bank_name,
        bc.code as bank_code
        from
        installment_history a
        left join bank_card_lend bc on
        bc.id = a.bank_card_lend_id
    </sql>

    <select id="selectInstallmentHistoryList" parameterType="InstallmentHistoryDto" resultMap="InstallmentHistoryResult">
        <include refid="selectInstallmentHistoryVo"/>
        <where>
            a.del_flag='0'
            <if test="name != null  and name != ''"> and a.name like '%'|| #{name}||'%'</if>
            <if test="bankCardLendId != null  and bankCardLendId != ''"> and a.bank_card_lend_id = #{bankCardLendId}</if>
            <if test="state != null  and state != ''"> and a.state = #{state}</if>
            <if test="type != null  and type != ''"> and a.type = #{type}</if>
            <if test="startMonth!=null and startMonth !=''">
                and to_char(a.installment_date, 'yyyy-MM')>=#{startMonth}
            </if>
            <if test="endMonth!=null and endMonth !=''">
                and #{endMonth}>=to_char(a.installment_date, 'yyyy-MM')
            </if>
        </where>
        <!-- 数据范围过滤 -->
        ${params.dataScope}
        order by a.installment_date desc
    </select>

    <select id="selectInstallmentHistoryById" parameterType="Long" resultMap="InstallmentHistoryResult">
        <include refid="selectInstallmentHistoryVo"/>
        where a.id = #{id}
    </select>

    <insert id="insertInstallmentHistory" parameterType="InstallmentHistory">
        insert into installment_history
        <trim prefix="(" suffix=")" suffixOverrides=",">
            <if test="id != null">id,</if>
            <if test="name != null">name,</if>
            <if test="type != null">type,</if>
            <if test="code != null">code,</if>
            <if test="bankCardLendId != null and bankCardLendId != ''">bank_card_lend_id,</if>
            <if test="installmentAmount != null and installmentAmount != ''">installment_amount,</if>
            <if test="installmentDate != null">installment_date,</if>
            <if test="period != null">period,</if>
            <if test="repaidPeriod != null">repaid_period,</if>
            <if test="totalInterest != null">total_interest,</if>
            <if test="interestRate != null">interest_rate,</if>
            <if test="dueDate != null">due_date,</if>
            <if test="closeDate != null">close_date,</if>
            <if test="state != null and state != ''">state,</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="balance != null">balance,</if>
            <if test="calculateInterestRate != null">calculate_interest_rate,</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="code != null">#{code},</if>
            <if test="bankCardLendId != null and bankCardLendId != ''">#{bankCardLendId},</if>
            <if test="installmentAmount != null and installmentAmount != ''">#{installmentAmount},</if>
            <if test="installmentDate != null">#{installmentDate},</if>
            <if test="period != null">#{period},</if>
            <if test="repaidPeriod != null">#{repaidPeriod},</if>
            <if test="totalInterest != null">#{totalInterest},</if>
            <if test="interestRate != null">#{interestRate},</if>
            <if test="dueDate != null">#{dueDate},</if>
            <if test="closeDate != null">#{closeDate},</if>
            <if test="state != null and state != ''">#{state},</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="balance != null">#{balance},</if>
            <if test="calculateInterestRate != null">#{calculateInterestRate},</if>
         </trim>
    </insert>

    <update id="updateInstallmentHistory" parameterType="InstallmentHistory">
        update installment_history
        <trim prefix="SET" suffixOverrides=",">
            <if test="name != null">name = #{name},</if>
            <if test="type != null">type = #{type},</if>
            <if test="code != null">code = #{code},</if>
            <if test="bankCardLendId != null and bankCardLendId != ''">bank_card_lend_id = #{bankCardLendId},</if>
            <if test="installmentAmount != null and installmentAmount != ''">installment_amount = #{installmentAmount},</if>
            <if test="installmentDate != null">installment_date = #{installmentDate},</if>
            <if test="period != null">period = #{period},</if>
            <if test="repaidPeriod != null">repaid_period = #{repaidPeriod},</if>
            <if test="totalInterest != null">total_interest = #{totalInterest},</if>
            <if test="interestRate != null">interest_rate = #{interestRate},</if>
            <if test="dueDate != null">due_date = #{dueDate},</if>
            <if test="closeDate != null">close_date = #{closeDate},</if>
            <if test="state != null and state != ''">state = #{state},</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="balance != null">balance = #{balance},</if>
            <if test="calculateInterestRate != null">calculate_interest_rate = #{calculateInterestRate},</if>
        </trim>
        where id = #{id}
    </update>

    <delete id="deleteInstallmentHistoryById" parameterType="Long">
        delete from installment_history where id = #{id}
    </delete>

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

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