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

    <resultMap id="BaseResultMap" type="com.zksy.property.domain.ASimplifiedHouseInfo">
            <id property="id" column="id" jdbcType="VARCHAR"/>
            <result property="assetType" column="asset_type" jdbcType="VARCHAR"/>
            <result property="building" column="building" jdbcType="VARCHAR"/>
            <result property="floor" column="floor" jdbcType="VARCHAR"/>
            <result property="houseName" column="house_name" jdbcType="VARCHAR"/>
            <result property="address" column="address" jdbcType="VARCHAR"/>
            <result property="status" column="status" jdbcType="VARCHAR"/>
            <result property="rentRange" column="rent_range" jdbcType="DECIMAL"/>
            <result property="createTime" column="create_time" jdbcType="TIMESTAMP"/>
            <result property="updateTime" column="update_time" jdbcType="TIMESTAMP"/>
    </resultMap>

    <sql id="Base_Column_List">
        id,asset_type,building,
        floor,house_name,address,
        status,rent_range,create_time,
        update_time
    </sql>
    <select id="getListPage" resultType="com.zksy.property.domain.vo.ASimplifiedHouseInfoVo">
        SELECT a.*, b.area
        FROM a_simplified_house_info a
        LEFT JOIN a_house_info_detail b ON a.id = b.simplified_house_id
        <where>
            <if test="entity.assetType != null and entity.assetType != ''">
                AND a.asset_type = #{entity.assetType}
            </if>
            <if test="entity.building != null and entity.building != ''">
                AND a.building LIKE CONCAT(#{entity.building}, '%')
            </if>
            <if test="entity.floor != null and entity.floor != ''">
                AND a.floor LIKE CONCAT(#{entity.floor}, '%')
            </if>
            <if test="entity.rentRangeMin != null">
                AND a.rent_range >= COALESCE(#{entity.rentRangeMin}, a.rent_range)
            </if>
            <if test="entity.rentRangeMax != null">
                AND a.rent_range &lt;= COALESCE(#{entity.rentRangeMax}, a.rent_range)
            </if>
            <if test="entity.areaMin != null">
                AND b.area >= COALESCE(#{entity.areaMin}, b.area)
            </if>
            <if test="entity.areaMax != null">
                AND b.area &lt;= COALESCE(#{entity.areaMax}, b.area)
            </if>
        </where>
        <if test="sortClause != null and sortClause != ''">
            ORDER BY ${sortClause}
        </if>
        <!-- 默认排序 -->
        <if test="sortClause == null or sortClause == ''">
            ORDER BY a.create_time DESC
        </if>
        <if test="offset != null and limit != null">
            LIMIT #{offset}, #{limit}
        </if>

    </select>
    <select id="getListPageTotal" resultType="java.lang.Long">
        SELECT count(*)
        FROM a_simplified_house_info a
        LEFT JOIN a_house_info_detail b ON a.id = b.simplified_house_id
        <where>
            <if test="entity.assetType != null and entity.assetType != ''">
                AND a.asset_type = #{entity.assetType}
            </if>
            <if test="entity.building != null and entity.building != ''">
                AND a.building = #{entity.building}
            </if>
            <if test="entity.floor != null and entity.floor != ''">
                AND a.floor LIKE CONCAT(#{entity.floor}, '%')
            </if>
            <if test="entity.rentRangeMin != null">
                AND a.rent_range >= COALESCE(#{entity.rentRangeMin}, a.rent_range)
            </if>
            <if test="entity.rentRangeMax != null">
                AND a.rent_range &lt;= COALESCE(#{entity.rentRangeMax}, a.rent_range)
            </if>
            <if test="entity.areaMin != null">
                AND b.area >= COALESCE(#{entity.areaMin}, b.area)
            </if>
            <if test="entity.areaMax != null">
                AND b.area &lt;= COALESCE(#{entity.areaMax}, b.area)
            </if>
        </where>
    </select>
    <select id="getArrears" resultType="java.util.Map">
        SELECT
        h.id AS id,
        h.house_name AS houseName,
        h.address AS address,
        c.contract_number AS contractNumber,
        c.contract_expiration_date AS contractExpirationDate,
        r.end_date AS endDate,
        NOW() AS nowAndDate
        FROM
        a_simplified_house_info h
        JOIN
        a_contract_info c ON h.id = c.simplified_house_id
        JOIN (
        -- 子查询：对每个合同，取最新一条“开始时间、结束时间非空”的收据
        SELECT
        contract_id,
        end_date,
        -- 按生成时间倒序，取第一条
        ROW_NUMBER() OVER (PARTITION BY contract_id ORDER BY generation_date DESC) AS rn
        FROM
        a_receipt_info
        WHERE
        start_date IS NOT NULL
        AND end_date IS NOT NULL
        ) r ON c.id = r.contract_id AND r.rn = 1
        WHERE
        h.status = '已租'
        AND c.contract_status = '有效'
        AND r.end_date &lt; c.contract_expiration_date AND r.end_date &lt; NOW()
        ORDER BY
        h.id;
    </select>
</mapper>
