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.UnitDto;
import com.sptg.estate.api.entity.Building;
import com.sptg.estate.api.entity.Unit;
import com.sptg.estate.api.feigns.UnitFeign;
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/unit")
public class UnitController {

    private final UnitFeign unitFeign;

    @GetMapping(value = "/syncUnit/{residentialId}")
    public List<Unit> syncUnit(@PathVariable("residentialId")Long residentialId){
        Page<Unit> page = new Page<>();
        page.setCurrent(1);
        page.setSize(999999999);
        UnitDto unitDto = new UnitDto();
        unitDto.setResidentialId(residentialId);
        unitDto.setPage(page);
        ResultBean<Page<Unit>> pageResultBean = this.unitFeign.showUnitList(unitDto);
        if(pageResultBean !=null){
            if(pageResultBean.getData()!=null){
                return pageResultBean.getData().getRecords();
            }
        }
        return null;
    }

    /**
     * 功能描述:单元列表查询
     * @param unitDto 单元业务对象（含查询条件）
     * @param page 分页条件
     * @return ResultBean<Page<Unit>>
     *
     *
     */
    @GetMapping(value = "/showUnitList")
    public ResultBean<Page<Unit>> showUnitList(UnitDto unitDto, Page<Unit> page) {
        // 暂时拿全部的数据
//        residentialDto.setOrgPosition("0/1/");
        unitDto.setPage(page);
        return this.unitFeign.showUnitList(unitDto);
    }

    @GetMapping(value = "/findUnitById/{id}")
    public ResultBean<Unit> findUnitById(@PathVariable(value = "id") Long id){
        Unit unit = this.unitFeign.findUnitById(id);
        Assert.notNull(unit,"单元不存在");
        return new ResultBean<>(ResultEnum.SUCCESS,unit);
    }

}
