package com.yuchen.saas.park.service.mapper.property;

import com.baomidou.mybatisplus.core.mapper.BaseMapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.yuchen.saas.park.api.dto.property.PropertyHouseResidentQueryDto;
import com.yuchen.saas.park.api.entity.property.PropertyHouseResident;
import com.yuchen.saas.park.api.vo.property.PropertyHouseResidentListVo;
import com.yuchen.saas.park.api.vo.property.PropertyTenantOwnerVo;
import org.apache.ibatis.annotations.Param;
import org.apache.ibatis.annotations.Select;

import java.util.List;

/**
 * @Author 蔡文强
 * @Date 2023/11/28 10:56
 * @Description 房产住户表 Mapper 接口
 */
public interface PropertyHouseResidentMapper extends BaseMapper<PropertyHouseResident> {
    Page<PropertyHouseResidentListVo> getHouseResidentPageList(Page<PropertyHouseResidentListVo> page, @Param("queryDto") PropertyHouseResidentQueryDto queryDto);
    List<PropertyHouseResidentListVo> getHouseResidentList(@Param("queryDto") PropertyHouseResidentQueryDto queryDto);

    /**
     * 根据房源id查询租客或业主，优先获取租客
     * @param houseId
     * @return
     */
    @Select("SELECT * from (" +
            " SELECT house_id,customer_archives_id,0 as is_owner,start_date from property_house_resident where relation=9 and resident_status=1 and is_deleted=0 and (end_date is null or end_date > NOW())" +
            " UNION " +
            " SELECT house_id,customer_archives_id,1 as is_owner,null as start_date from property_house_owner where is_deleted=0" +
            " ) t where t.house_id=#{houseId} ORDER BY t.is_owner limit 1")
    PropertyTenantOwnerVo getTenantOrOwnerInfo(@Param("houseId") Long houseId);

}
