<?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.system.mapper.TblContractSettlementMapper">

    <resultMap type="TblContractSettlement" id="TblContractSettlementResult">
        <result property="id" column="id"/>
        <result property="contractId" column="contract_id"/>
        <result property="settleType" column="settle_type"/>
        <result property="settleDate" column="settle_date"/>
        <result property="amount" column="amount"/>
        <result property="payMode" column="pay_mode"/>
        <result property="remark" column="remark"/>
        <result property="warnFlag" column="warn_flag"/>
        <result property="delFlag" column="del_flag"/>
        <result property="createBy" column="create_by"/>
        <result property="createTime" column="create_time"/>
        <result property="updateBy" column="update_by"/>
        <result property="updateTime" column="update_time"/>
    </resultMap>
    <resultMap type="CntSettleSummaryModel" id="CntSettleSummaryResult">
        <result property="contractId" column="contract_id"/>
        <result property="payedAmount" column="payed_amount"/>
        <result property="matureAmount" column="mature_amount"/>
        <result property="matureDate" column="mature_date"/>
    </resultMap>

    <sql id="selectTblContractSettlementVo">
        select id,
               contract_id,
               settle_type,
               settle_date,
               amount,
               pay_mode,
               warn_flag,
               remark,
               del_flag,
               create_by,
               create_time,
               update_by,
               update_time
        from tbl_contract_settlement
    </sql>

    <select id="selectTblContractSettlementList" parameterType="TblContractSettlement"
            resultMap="TblContractSettlementResult">
        <include refid="selectTblContractSettlementVo"/>
        <where>
            <if test="contractId != null  and contractId != ''">and contract_id = #{contractId}</if>
            <if test="settleType != null  and settleType != ''">and settle_type = #{settleType}</if>
            <if test="settleDate != null ">and settle_date = #{settleDate}</if>
            <if test="amount != null ">and amount = #{amount}</if>
            <if test="payMode != null  and payMode != ''">and pay_mode = #{payMode}</if>
            and (del_flag is null or del_flag = '0')
        </where>
    </select>

    <select id="selectTblContractSettlementById" parameterType="String" resultMap="TblContractSettlementResult">
        <include refid="selectTblContractSettlementVo"/>
        where id = #{id}
    </select>

    <insert id="insertTblContractSettlement" parameterType="TblContractSettlement">
        insert into tbl_contract_settlement
        <trim prefix="(" suffix=")" suffixOverrides=",">
            <if test="id != null">id,</if>
            <if test="contractId != null and contractId != ''">contract_id,</if>
            <if test="settleType != null and settleType != ''">settle_type,</if>
            <if test="settleDate != null">settle_date,</if>
            <if test="amount != null">amount,</if>
            <if test="payMode != null and payMode != ''">pay_mode,</if>
            <if test="warnFlag != null">warn_flag,</if>
            <if test="remark != null">remark,</if>
            <if test="delFlag != null">del_flag,</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>
        </trim>
        <trim prefix="values (" suffix=")" suffixOverrides=",">
            <if test="id != null">#{id},</if>
            <if test="contractId != null and contractId != ''">#{contractId},</if>
            <if test="settleType != null and settleType != ''">#{settleType},</if>
            <if test="settleDate != null">#{settleDate},</if>
            <if test="amount != null">#{amount},</if>
            <if test="payMode != null and payMode != ''">#{payMode},</if>
            <if test="warnFlag != null">#{warnFlag},</if>
            <if test="remark != null">#{remark},</if>
            <if test="delFlag != null">#{delFlag},</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>
        </trim>
    </insert>

    <insert id="insertTblContractSettlementList" parameterType="Arraylist">
        insert into
        tbl_contract_settlement(id,contract_id,settle_type,settle_date,amount,pay_mode,warn_flag,remark,create_by,create_time) values
        <foreach collection="list" item="item" index="index" separator=",">
            (
            (select REPLACE(UUID(),'-','')),
            #{item.contractId},
            #{item.settleType},
            #{item.settleDate},
            #{item.amount},
            #{item.payMode},
            #{item.warnFlag},
            #{item.remark},
            #{item.createBy},
            #{item.createTime}
            )
        </foreach>
    </insert>

    <update id="updateTblContractSettlement" parameterType="TblContractSettlement">
        update tbl_contract_settlement
        <trim prefix="SET" suffixOverrides=",">
            <if test="contractId != null and contractId != ''">contract_id = #{contractId},</if>
            <if test="settleType != null and settleType != ''">settle_type = #{settleType},</if>
            <if test="settleDate != null">settle_date = #{settleDate},</if>
            <if test="amount != null">amount = #{amount},</if>
            <if test="payMode != null and payMode != ''">pay_mode = #{payMode},</if>
            <if test="warnFlag != null">warn_flag = #{warnFlag},</if>
            <if test="remark != null">remark = #{remark},</if>
            <if test="delFlag != null">del_flag = #{delFlag},</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>
        </trim>
        where id = #{id}
    </update>

    <delete id="deleteTblContractSettlementById" parameterType="String">
        update tbl_contract_settlement set del_flag = '2'
        where id = #{id}
    </delete>

    <delete id="deleteTblContractSettlementByIds" parameterType="String">
        update tbl_contract_settlement set del_flag = '2'
        where id in
        <foreach item="id" collection="array" open="(" separator="," close=")">
            #{id}
        </foreach>
    </delete>

    <select id="selectContractSettleSummary" parameterType="String" resultMap="CntSettleSummaryResult">
        select case when mature.contract_id is not null then mature.contract_id else payed.contract_id end contract_id,
        payed.payed_amount,
        mature.mature_amount,
        mature.mature_date
        from (select contract_id,sum(amount) payed_amount from tbl_contract_settlement
        <where>
            settle_type = '1'
            and (del_flag is null or del_flag = '0')
            and contract_id in
            <foreach item="id" collection="array" open="(" separator="," close=")">
                #{id}
            </foreach>
        </where>
        group by contract_id) payed
        left join
        (
        select t.contract_id,
        substring_index(group_concat( t.amount order by t.settle_date asc),',',1) mature_amount,
        substring_index(group_concat( t.settle_date order by t.settle_date asc),',',1) mature_date
        from (
        select ord.contract_id, ord.amount, ord.settle_date
        from tbl_contract_settlement ord
        <where>
            settle_type = '0'
            and (warn_flag is null or warn_flag = '1')
            and (del_flag is null or del_flag = '0')
            and contract_id in
            <foreach item="id" collection="array" open="(" separator="," close=")">
                #{id}
            </foreach>
        </where>
        order by contract_id asc , settle_date asc
        ) t group by contract_id
        ) mature
        on payed.contract_id = mature.contract_id
    </select>
    <select  id="selectContractPayedSummary" parameterType="String" resultMap="CntSettleSummaryResult">
        select contract_id,sum(amount) payed_amount from tbl_contract_settlement
        <where>
            settle_type = '1'
            and (del_flag is null or del_flag = '0')
            and contract_id in
            <foreach item="id" collection="array" open="(" separator="," close=")">
                #{id}
            </foreach>
        </where>
        group by contract_id
    </select>
    <select id="selectContractPlanSettleSummary" parameterType="String" resultMap="CntSettleSummaryResult">
        select t.contract_id,
        substring_index(group_concat( t.amount order by t.settle_date asc),',',1) mature_amount,
        substring_index(group_concat( t.settle_date order by t.settle_date asc),',',1) mature_date
        from (
        select ord.contract_id, ord.amount, ord.settle_date
        from tbl_contract_settlement ord
        <where>
            settle_type = '0'
            and (warn_flag is null or warn_flag = '1')
            and (del_flag is null or del_flag = '0')
            and contract_id in
            <foreach item="id" collection="array" open="(" separator="," close=")">
                #{id}
            </foreach>
        </where>
        order by contract_id asc , settle_date asc
        ) t group by contract_id
    </select>
</mapper>