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

import com.baomidou.mybatisplus.core.conditions.Wrapper;
import com.baomidou.mybatisplus.core.mapper.BaseMapper;
import com.baomidou.mybatisplus.core.toolkit.Constants;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.yuchen.saas.park.api.entity.property.PropertyHouses;
import com.yuchen.saas.park.api.vo.property.PropertyHouseParentInfoVo;
import com.yuchen.saas.park.api.vo.property.PropertyHousesListVo;
import org.apache.ibatis.annotations.Param;
import org.apache.ibatis.annotations.Select;

import java.util.List;

public interface PropertyHousesMapper extends BaseMapper<PropertyHouses> {

    @Select("select p.id as park_id, p.park_name,b.id as building_id,b.building_name,h.*" +
            " from property_houses h" +
            " left join property_buildings b on h.building_id = b.id" +
            " left join pmg_bat_park_info p on h.park_id = p.id" +
            " ${ew.customSqlSegment} order by h.sort asc,h.create_time desc")
    Page<PropertyHousesListVo> propertyHousesPage(Page<PropertyHousesListVo> page, @Param(Constants.WRAPPER) Wrapper<String> wrapper);

    /**
     * 根据房源id查询园区楼栋信息
     * @param houseIdList
     * @return
     */
    @Select("SELECT p.id as park_id,p.park_name,b.id as building_id,b.building_name,h.id as house_id,h.house_name,h.resource_code" +
            " ,h.house_name,h.construction_area,h.use_area" +
            " from property_houses h" +
            " LEFT JOIN property_buildings b on h.building_id = b.id" +
            " LEFT JOIN pmg_bat_park_info p on h.park_id = p.id" +
            " where h.id in (${houseIdList})")
    List<PropertyHouseParentInfoVo> searchHouseParentInfoList(@Param("houseIdList") String houseIdList);

    @Select(" select a.*" +
            " from property_houses a" +
            " left join" +
            " property_user_house b on a.id=b.house_id" +
            " where a.is_auto_deduction=${isAutoDeduction} and b.is_invalid=0 and b.user_detail_id=${userDetailId}")
    Page<PropertyHouses> listByBindingUser(Page<PropertyHouses> page,  @Param("userDetailId")Long userDetailId,@Param("isAutoDeduction")Integer isAutoDeduction);

    @Select(" select a.*" +
            " from property_houses a" +
            " left join" +
            " property_enterprise_address b on a.id=b.house_id" +
            " where a.is_auto_deduction=${isAutoDeduction} and b.is_valid=0 and b.dept_id=${deptId}")
    Page<PropertyHouses> listByBindingEnterprise(Page<PropertyHouses> page,  @Param("deptId")Long deptId,@Param("isAutoDeduction")Integer isAutoDeduction);

}
