<?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.zksy.property.mapper.ARefundMapper">

    <resultMap id="BaseResultMap" type="com.zksy.property.domain.ARefund">
            <id property="id" column="id" jdbcType="VARCHAR"/>
            <result property="contractId" column="contract_id" jdbcType="VARCHAR"/>
            <result property="unit" column="unit" jdbcType="VARCHAR"/>
            <result property="tenant" column="tenant" jdbcType="VARCHAR"/>
            <result property="deposit" column="deposit" jdbcType="DECIMAL"/>
            <result property="rent" column="rent" jdbcType="DECIMAL"/>
            <result property="propertyFee" column="property_fee" jdbcType="DECIMAL"/>
            <result property="totalAmount" column="total_amount" jdbcType="DECIMAL"/>
            <result property="generationDate" column="generation_date" jdbcType="DATE" javaType="java.time.LocalDate"/>
            <result property="attachmentUrl" column="attachment_url" jdbcType="VARCHAR"/>
            <result property="createTime" column="create_time" jdbcType="TIMESTAMP" javaType="java.time.LocalDateTime"/>
            <result property="updateTime" column="update_time" jdbcType="TIMESTAMP" javaType="java.time.LocalDateTime"/>
    </resultMap>

    <resultMap id="VoResultMap" type="com.zksy.property.domain.vo.ARefundVo">
        <id property="id" column="id" jdbcType="VARCHAR"/>
        <result property="contractId" column="contract_id" jdbcType="VARCHAR"/>
        <result property="unit" column="unit" jdbcType="VARCHAR"/>
        <result property="tenant" column="tenant" jdbcType="VARCHAR"/>
        <result property="deposit" column="deposit" jdbcType="DECIMAL"/>
        <result property="rent" column="rent" jdbcType="DECIMAL"/>
        <result property="propertyFee" column="property_fee" jdbcType="DECIMAL"/>
        <result property="totalAmount" column="total_amount" jdbcType="DECIMAL"/>
        <result property="generationDate" column="generation_date" jdbcType="DATE" javaType="java.time.LocalDate"/>
        <result property="attachmentUrl" column="attachment_url" jdbcType="VARCHAR"/>
        <result property="createTime" column="create_time" jdbcType="TIMESTAMP" javaType="java.time.LocalDateTime"/>
        <result property="updateTime" column="update_time" jdbcType="TIMESTAMP" javaType="java.time.LocalDateTime"/>
        <result property="contractNumber" column="contract_number" jdbcType="VARCHAR"/>
    </resultMap>

    <sql id="Base_Column_List">
        id,contract_id,unit,tenant,
        deposit,rent,property_fee,
        total_amount,generation_date,attachment_url,
        create_time,update_time
    </sql>

    <select id="selectRefundWithContractByPage" resultMap="VoResultMap">
        SELECT
            r.id,
            r.contract_id,
            r.unit,
            r.tenant,
            r.deposit,
            r.rent,
            r.property_fee,
            r.total_amount,
            r.generation_date,
            r.attachment_url,
            r.create_time,
            r.update_time,
            c.contract_number
        FROM a_refund r
        LEFT JOIN a_contract_info c ON r.contract_id = c.id
        <where>
            <if test="unit != null and unit != ''">
                AND r.unit LIKE CONCAT('%', #{unit}, '%')
            </if>
            <if test="tenant != null and tenant != ''">
                AND r.tenant LIKE CONCAT('%', #{tenant}, '%')
            </if>
            <if test="contractNumber != null and contractNumber != ''">
                AND c.contract_number LIKE CONCAT('%', #{contractNumber}, '%')
            </if>
        </where>
        ORDER BY r.create_time DESC
    </select>
</mapper>
