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

    <resultMap type="TblContract" id="TblContractResult">
        <result property="id" column="id"/>
        <result property="name" column="name"/>
        <result property="leaderId" column="leader_id"/>
        <result property="leader" column="leader"/>
        <result property="buyers" column="buyers"/>
        <result property="sellers" column="sellers"/>
        <result property="signDate" column="sign_date"/>
        <result property="startDate" column="start_date"/>
        <result property="endDate" column="end_date"/>
        <result property="amount" column="amount"/>
        <result property="payMode" column="pay_mode"/>
        <result property="remark" column="remark"/>
        <result property="payStatus" column="pay_status"/>
        <result property="status" column="status"/>
        <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="ContractSettleIndexModel" id="TblContractSettleResult">
        <result property="id" column="id"/>
        <result property="name" column="name"/>
        <result property="leaderId" column="leader_id"/>
        <result property="leader" column="leader"/>
        <result property="buyers" column="buyers"/>
        <result property="sellers" column="sellers"/>
        <result property="signDate" column="sign_date"/>
        <result property="startDate" column="start_date"/>
        <result property="endDate" column="end_date"/>
        <result property="amount" column="amount"/>
        <result property="payMode" column="pay_mode"/>
        <result property="remark" column="remark"/>
        <result property="payStatus" column="pay_status"/>
        <result property="status" column="status"/>
        <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"/>
        <result property="payedAmount" column="payed_amount"/>
        <result property="matureAmount" column="mature_amount"/>
        <result property="matureDate" column="mature_date"/>
    </resultMap>

    <sql id="selectTblContractVo">
        select id,
               name,
               leader_id,
               leader,
               buyers,
               sellers,
               sign_date,
               start_date,
               end_date,
               amount,
               pay_mode,
               remark,
               pay_status,
               status,
               del_flag,
               create_by,
               create_time,
               update_by,
               update_time
        from tbl_contract
    </sql>

    <select id="selectTblContractList" parameterType="TblContract" resultMap="TblContractResult">
        <include refid="selectTblContractVo"/>
        <where>
            <if test="name != null  and name != ''">and name like concat('%', #{name}, '%')</if>
            <if test="leaderId != null ">and leader_id = #{leaderId}</if>
            <if test="leader != null  and leader != ''">and leader = #{leader}</if>
            <if test="buyers != null  and buyers != ''">and buyers = #{buyers}</if>
            <if test="sellers != null  and sellers != ''">and sellers = #{sellers}</if>
            <if test="signDate != null ">and sign_date = #{signDate}</if>
            <if test="startDate != null ">and start_date = #{startDate}</if>
            <if test="endDate != null ">and end_date = #{endDate}</if>
            <if test="amount != null ">and amount = #{amount}</if>
            <if test="payMode != null  and payMode != ''">and pay_mode = #{payMode}</if>
            <if test="payStatus != null ">and pay_status = #{payStatus}</if>
            <if test="status != null  and status != ''">and status = #{status}</if>
            and (del_flag is null or del_flag = '0')
            order by create_time desc
        </where>
    </select>

    <select id="selectTblContractSettleList" parameterType="TblContract" resultMap="TblContractSettleResult">
        select
        id,
        name,
        leader_id,
        leader,
        buyers,
        sellers,
        sign_date,
        start_date,
        end_date,
        amount,
        pay_mode,
        remark,
        pay_status,
        status,
        del_flag,
        create_by,
        create_time,
        update_by,
        update_time,
        payed_amount,
        mature_amount,
        mature_date
        from tbl_contract cnt
        left join
        (
        select contract_id,sum(amount) payed_amount from tbl_contract_settlement
        where settle_type = '1'
        and (del_flag is null or del_flag = '0')
        group by contract_id
        ) payed
        on cnt.id = payed.contract_id
        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')
        order by contract_id asc , settle_date asc
        ) t group by contract_id
        ) plan
        on cnt.id = plan.contract_id
        <where>
            <if test="name != null  and name != ''">and cnt.name like concat('%', #{name}, '%')</if>
            <if test="leaderId != null ">and cnt.leader_id = #{leaderId}</if>
            <if test="leader != null  and leader != ''">and cnt.leader = #{leader}</if>
            <if test="buyers != null  and buyers != ''">and cnt.buyers = #{buyers}</if>
            <if test="sellers != null  and sellers != ''">and cnt.sellers = #{sellers}</if>
            <if test="signDate != null ">and cnt.sign_date = #{signDate}</if>
            <if test="startDate != null ">and cnt.start_date = #{startDate}</if>
            <if test="endDate != null ">and cnt.end_date = #{endDate}</if>
            <if test="amount != null ">and cnt.amount = #{amount}</if>
            <if test="payMode != null  and payMode != ''">and cnt.pay_mode = #{payMode}</if>
            <if test="payStatus != null ">and cnt.pay_status = #{payStatus}</if>
            <if test="status != null  and status != ''">and cnt.status = #{status}</if>
            and (cnt.del_flag is null or cnt.del_flag = '0')
        </where>
    </select>

    <select id="selectTblContractById" parameterType="String" resultMap="TblContractResult">
        <include refid="selectTblContractVo"/>
        where id = #{id}
    </select>

    <insert id="insertTblContract" parameterType="TblContract">
        insert into tbl_contract
        <trim prefix="(" suffix=")" suffixOverrides=",">
            id,
            <if test="name != null and name != ''">name,</if>
            <if test="leaderId != null">leader_id,</if>
            <if test="leader != null">leader,</if>
            <if test="buyers != null and buyers != ''">buyers,</if>
            <if test="sellers != null and sellers != ''">sellers,</if>
            <if test="signDate != null">sign_date,</if>
            <if test="startDate != null">start_date,</if>
            <if test="endDate != null">end_date,</if>
            <if test="amount != null">amount,</if>
            <if test="payMode != null and payMode != ''">pay_mode,</if>
            <if test="remark != null">remark,</if>
            <if test="payStatus != null">pay_status,</if>
            <if test="status != null">status,</if>
            del_flag,
            <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=",">
            (select REPLACE(UUID(),'-','')),
            <if test="name != null and name != ''">#{name},</if>
            <if test="leaderId != null">#{leaderId},</if>
            <if test="leader != null">#{leader},</if>
            <if test="buyers != null and buyers != ''">#{buyers},</if>
            <if test="sellers != null and sellers != ''">#{sellers},</if>
            <if test="signDate != null">#{signDate},</if>
            <if test="startDate != null">#{startDate},</if>
            <if test="endDate != null">#{endDate},</if>
            <if test="amount != null">#{amount},</if>
            <if test="payMode != null and payMode != ''">#{payMode},</if>
            <if test="remark != null">#{remark},</if>
            <if test="payStatus != null">#{payStatus},</if>
            <if test="status != null">#{status},</if>
            0,
            <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>

    <update id="updateTblContract" parameterType="TblContract">
        update tbl_contract
        <trim prefix="SET" suffixOverrides=",">
            <if test="name != null and name != ''">name = #{name},</if>
            <if test="leaderId != null">leader_id = #{leaderId},</if>
            <if test="leader != null">leader = #{leader},</if>
            <if test="buyers != null and buyers != ''">buyers = #{buyers},</if>
            <if test="sellers != null and sellers != ''">sellers = #{sellers},</if>
            <if test="signDate != null">sign_date = #{signDate},</if>
            <if test="startDate != null">start_date = #{startDate},</if>
            <if test="endDate != null">end_date = #{endDate},</if>
            <if test="amount != null">amount = #{amount},</if>
            <if test="payMode != null and payMode != ''">pay_mode = #{payMode},</if>
            <if test="remark != null">remark = #{remark},</if>
            <if test="payStatus != null">pay_status = #{payStatus},</if>
            <if test="status != null">status = #{status},</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="deleteTblContractById" parameterType="String">
        update tbl_contract
        set del_flag = '2'
        where id = #{id}
    </delete>

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

</mapper>