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

    <resultMap type="InstallmentHistoryDetailVo" id="InstallmentHistoryDetailResult">
        <result property="id"    column="id"    />
        <result property="installmentHistoryId"    column="installment_history_id"    />
        <result property="bankCardLendId"    column="bank_card_lend_id"    />
        <result property="currentAmount"    column="current_amount"    />
        <result property="repaymentDate"    column="repayment_date"    />
        <result property="principal"    column="principal"    />
        <result property="interest"    column="interest"    />
        <result property="postingState"    column="posting_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="type"    column="type"    />
        <result property="periods"    column="periods"    />
        <result property="bankCardLendName"    column="bank_card_lend_name"    />
    </resultMap>

    <sql id="selectInstallmentHistoryDetailVo">
        select
        a.id,
        a.installment_history_id,
        a.bank_card_lend_id,
        a.current_amount,
        a.repayment_date,
        a.principal,
        a.interest,
        a.posting_state,
        a.create_by,
        a.create_time,
        a.update_by,
        a.update_time,
        a.del_flag,
        a.remark,
        a.type,
        a.periods,
        CONCAT(bcl."name",
        '（',
        right(bcl.code,
        4),
        '）') as bank_card_lend_name
        from
        installment_history_detail a
        left join bank_card_lend bcl on
        bcl.id = a.bank_card_lend_id
    </sql>

    <select id="selectInstallmentHistoryDetailList" parameterType="InstallmentHistoryDetailDto" resultMap="InstallmentHistoryDetailResult">
        <include refid="selectInstallmentHistoryDetailVo"/>
        <where>
            a.del_flag='0'
            <if test="installmentHistoryId != null "> and a.installment_history_id = #{installmentHistoryId}</if>
            <if test="bankCardLendId != null "> and a.bank_card_lend_id = #{bankCardLendId}</if>
            <if test="type != null  and type != ''"> and a.type = #{type}</if>
            <if test="periods != null "> and a.periods = #{periods}</if>
            <if test="state != null  and state != ''"> and a.posting_state = #{state}</if>
            <if test="repaymentMonth!=null and repaymentMonth !=''">
                and #{repaymentMonth}=to_char(a.repayment_date, 'yyyy-MM')
            </if>
            <if test="repaymentDate!=null and repaymentDate !=''">
                and #{repaymentDate}>=to_char(a.repayment_date, 'yyyy-MM-dd')
            </if>
            <if test="repaymentEqualDate!=null and repaymentEqualDate !=''">
                and #{repaymentEqualDate}=to_char(a.repayment_date, 'yyyy-MM-dd')
            </if>
            <if test="startDate!=null and startDate !=''">
                and to_char(a.repayment_date, 'yyyy-MM-dd')>=#{startDate}
            </if>
            <if test="endDate!=null and endDate !=''">
                and #{endDate}>=to_char(a.repayment_date, 'yyyy-MM-dd')
            </if>
        </where>
        <!-- 数据范围过滤 -->
        ${params.dataScope}
        order by a.repayment_date desc
    </select>

    <select id="selectInstallmentHistoryDetailById" parameterType="Long" resultMap="InstallmentHistoryDetailResult">
        <include refid="selectInstallmentHistoryDetailVo"/>
        where a.id = #{id}
    </select>

    <insert id="insertInstallmentHistoryDetail" parameterType="InstallmentHistoryDetail">
        insert into installment_history_detail
        <trim prefix="(" suffix=")" suffixOverrides=",">
            <if test="id != null">id,</if>
            <if test="installmentHistoryId != null">installment_history_id,</if>
            <if test="bankCardLendId != null">bank_card_lend_id,</if>
            <if test="currentAmount != null and currentAmount != ''">current_amount,</if>
            <if test="repaymentDate != null">repayment_date,</if>
            <if test="principal != null">principal,</if>
            <if test="interest != null">interest,</if>
            <if test="postingState != null and postingState != ''">posting_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="type != null">type,</if>
            <if test="periods != null">periods,</if>
         </trim>
        <trim prefix="values (" suffix=")" suffixOverrides=",">
            <if test="id != null">#{id},</if>
            <if test="installmentHistoryId != null">#{installmentHistoryId},</if>
            <if test="bankCardLendId != null">#{bankCardLendId},</if>
            <if test="currentAmount != null and currentAmount != ''">#{currentAmount},</if>
            <if test="repaymentDate != null">#{repaymentDate},</if>
            <if test="principal != null">#{principal},</if>
            <if test="interest != null">#{interest},</if>
            <if test="postingState != null and postingState != ''">#{postingState},</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="type != null">#{type},</if>
            <if test="periods != null">#{periods},</if>
         </trim>
    </insert>

    <update id="updateInstallmentHistoryDetail" parameterType="InstallmentHistoryDetail">
        update installment_history_detail
        <trim prefix="SET" suffixOverrides=",">
            <if test="installmentHistoryId != null">installment_history_id = #{installmentHistoryId},</if>
            <if test="bankCardLendId != null">bank_card_lend_id = #{bankCardLendId},</if>
            <if test="currentAmount != null and currentAmount != ''">current_amount = #{currentAmount},</if>
            <if test="repaymentDate != null">repayment_date = #{repaymentDate},</if>
            <if test="principal != null">principal = #{principal},</if>
            <if test="interest != null">interest = #{interest},</if>
            <if test="postingState != null and postingState != ''">posting_state = #{postingState},</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="type != null">type = #{type},</if>
            <if test="periods != null">periods = #{periods},</if>
        </trim>
        where id = #{id}
    </update>

    <delete id="deleteInstallmentHistoryDetailById" parameterType="Long">
        delete from installment_history_detail where id = #{id}
    </delete>

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

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