package com.sptg.gateway.controller;

import com.baomidou.mybatisplus.plugins.Page;
import com.sptg.common.core.beans.ResultBean;
import com.sptg.common.core.beans.ResultEnum;
import com.sptg.estate.api.dto.BuildingDto;
import com.sptg.estate.api.dto.ResidentialDto;
import com.sptg.estate.api.entity.Building;
import com.sptg.estate.api.entity.Residential;
import com.sptg.estate.api.feigns.BuildingFeign;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.util.Assert;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;

@Slf4j
@RestController
@RequiredArgsConstructor
@RequestMapping(value = "/grid/building")
public class BuildingController {

    private final BuildingFeign buildingFeign;

    /**
     * 功能描述:小区列表查询
     *
     * @return List<Residential>
     *
     *
     */
    @GetMapping(value = "/syncBuilding/{residentialId}")
    public List<Building> syncBuilding(@PathVariable("residentialId")Long residentialId){
        Page<Building> page = new Page<>();
        page.setCurrent(1);
        page.setSize(999999999);
        BuildingDto buildingDto = new BuildingDto();
        buildingDto.setResidentialId(residentialId);
        buildingDto.setPage(page);
        ResultBean<Page<Building>> pageResultBean = this.buildingFeign.showBuildingList(buildingDto);
        if(pageResultBean !=null){
            if(pageResultBean.getData()!=null){
                return pageResultBean.getData().getRecords();
            }
        }
        return null;
    }
    /**
     * 功能描述:小区列表查询
     * @param buildingDto 小区业务对象（含查询条件）
     * @param page 分页条件
     * @return ResultBean<Page<Building>>
     *
     *
     */
    @GetMapping(value = "/showBuildingList")
    public ResultBean<Page<Building>> showBuildingList(BuildingDto buildingDto, Page<Building> page) {
        // 暂时拿全部的数据
//        residentialDto.setOrgPosition("0/1/");
        buildingDto.setPage(page);
        return this.buildingFeign.showBuildingList(buildingDto);
    }

    @GetMapping(value = "/findBuildingById/{id}")
    public ResultBean<Building> findBuildingById(@PathVariable(value = "id") Long id){
        Building building = this.buildingFeign.findBuildingById(id);
        Assert.notNull(building,"楼栋不存在");
        return new ResultBean<>(ResultEnum.SUCCESS,building);
    }

}
