package com.sptg.gateway.controller;

import cn.hutool.core.date.DateUtil;
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.ResidentialDto;
import com.sptg.estate.api.dto.RoomDto;
import com.sptg.estate.api.dto.UnitDto;
import com.sptg.estate.api.entity.*;
import com.sptg.estate.api.feigns.*;
import com.sptg.gateway.util.DataCheck;
import com.sptg.permissions.api.entity.Conf;
import com.sptg.permissions.api.entity.Organization;
import com.sptg.permissions.api.feigns.ApiDockingConfFeign;
import com.sptg.permissions.api.feigns.ConfFeign;
import com.sptg.permissions.api.feigns.OrgFeign;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang.StringUtils;
import org.springframework.util.Assert;
import org.springframework.web.bind.annotation.*;

import java.util.*;

/**
 * title: EstateController
 * package: com.sptg.gateway.controller
 * description: 小区管理控制器--查询小区信息接口
 *
 *
 *
 */
@Slf4j
@RestController
@RequiredArgsConstructor
@RequestMapping(value = "/grid/estate")
public class EstateController {

    private final OrgFeign orgFeign;
    private final ResidentialFeign residentialFeign;
    private final UnitFeign unitFeign;
    private final RoomFeign roomFeign;
    private final MemberAndUserFeign memberAndUserFeign;
    private final UserFeign userFeign;
    private final ApiDockingConfFeign apiDockingConfFeign;
    private final ConfFeign confFeign;


    /**
     * 功能描述:小区列表查询
     *
     * @return List<Residential>
     *
     *
     */
    @GetMapping(value = "/syncResidential")
    public List<Residential> syncResidential(){
        Page<Residential> page = new Page<>();
        page.setCurrent(1);
        page.setSize(99999);
        ResidentialDto residentialDto = new ResidentialDto();
        residentialDto.setPage(page);
        ResultBean<Page<Residential>> pageResultBean = this.residentialFeign.showResidentialList(residentialDto);
        if(pageResultBean !=null){
            if(pageResultBean.getData()!=null){
                return pageResultBean.getData().getRecords();
            }
        }
        return null;
    }


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

    /**
     * 功能描述:小区列表查询
     * @param residentialDto 小区业务对象（含查询条件）
     * @return ResultBean<Page<Residential>>
     *
     *
     */
    @PostMapping(value = "/updateResidential")
    public ResultBean<Boolean> updateResidential(@RequestBody ResidentialDto residentialDto) {
        System.out.println();
        return this.residentialFeign.updateResidential(residentialDto);
    }

    @GetMapping(value = "/findResidentialById/{id}")
    public ResultBean<Residential> findResidentialById(@PathVariable(value = "id") Long id){
        Residential residential = this.residentialFeign.findResidentialById(id);
        Assert.notNull(residential,"小区不存在");
        return new ResultBean<>(ResultEnum.SUCCESS,residential);
    }

    /**
     * 功能描述:获取小区下拉数据
     * @return ModelAndView
     *
     */
    @GetMapping(value = "/getResidentialList")
    public List<Residential> getResidentialList(ResidentialDto residentialDto){
        residentialDto.setOrgPosition("0/1/");
        ResultBean<Residential> listByCondition = this.residentialFeign.getListByCondition(residentialDto);
        return listByCondition.getList();
    }

    /**
     * 功能描述:获取单元下拉数据
     * @return ModelAndView
     *
     */
    @GetMapping(value = "/getUnitList")
    public List<Unit> getUnitList(UnitDto unitDto){
        unitDto.setOrgPosition("0/1/");
        ResultBean<Unit> listByPosition = this.unitFeign.findListByPosition(unitDto);
        return listByPosition.getList();
    }

    /**
     * 功能描述:获取房间下拉数据
     * @return ModelAndView
     *
     */
    @GetMapping(value = "/getRoomList")
    public List<Room> getRoomList(RoomDto roomDto){
        roomDto.setOrgPosition("0/1/");
        List<Room> listByCondition = this.roomFeign.getListByCondition(roomDto);
        return listByCondition;
    }

    /**
     * 功能描述:房间列表查询
     * @param roomDto 房间业务对象（含查询条件）
     * @param page 分页条件
     * @return ResultBean<Page<Room>>
     *
     */
    @GetMapping(value = "/showRoomList")
    public ResultBean<Page<Room>> showRoomList(RoomDto roomDto, Page<Room> page) {
        // 暂时获取所有数据
        roomDto.setOrgPosition("0/1/");
        roomDto.setPage(page);
        return this.roomFeign.showRoomList(roomDto);
    }

    /**
     * 功能描述:根据id获取房间
     * @return ModelAndView
     *
     */
    @GetMapping(value = "/findRoomById/{id}")
    public ResultBean<Room> findRoomById(@PathVariable(value = "id") Long id){
        Room room = this.roomFeign.findRoomById(id);
        return new ResultBean<>(ResultEnum.SUCCESS,room);
    }


    /**
     * 功能描述: 网格推送社区信息到社区，社区新增社区机构和社区实体
     * @return ModelAndView
     *
     */
//    @GetMapping(value = "/agencySync")
//    public ResultBean<Boolean> agencySync(@RequestBody List<A> id){
//        Room room = this.roomFeign.findRoomById(id);
//        return new ResultBean<>(ResultEnum.SUCCESS);
//    }

}
