package com.sptg.gateway.controller.estate;

import cn.hutool.json.JSONUtil;
import com.baomidou.mybatisplus.plugins.Page;
import com.sptg.common.core.beans.ResultBean;
import com.sptg.common.core.beans.ResultEnum;
import com.sptg.common.core.constant.OperationConstant;
import com.sptg.common.core.constant.SymbolConstant;
import com.sptg.common.core.constant.SysLogConstant;
import com.sptg.common.core.util.CommonUtil;
import com.sptg.common.core.util.WebUtils;
import com.sptg.common.security.annotation.Security;
import com.sptg.common.security.enums.DataFormatEnum;
import com.sptg.common.security.service.SecurityServiceInterface;
import com.sptg.estate.api.constant.EstateSecurityToken;
import com.sptg.estate.api.dto.*;
import com.sptg.estate.api.entity.*;
import com.sptg.estate.api.feigns.*;
import com.sptg.permissions.api.entity.Account;
import com.sptg.permissions.api.entity.Organization;
import com.sptg.permissions.api.feigns.OrgFeign;
import com.sptg.statistics.api.constant.SysLogMenuConstant;
import com.sptg.statistics.api.entity.SysOperationLog;
import com.sptg.statistics.api.feigns.SysOperationLogFeign;
import com.sptg.third.api.feigns.grid.GridEstateFeign;
import com.sptg.third.api.feigns.qinlin.QLinAuthFeign;
import lombok.RequiredArgsConstructor;
import org.springframework.beans.BeanUtils;
import org.springframework.core.ParameterizedTypeReference;
import org.springframework.http.HttpEntity;
import org.springframework.http.HttpHeaders;
import org.springframework.http.HttpMethod;
import org.springframework.http.ResponseEntity;
import org.springframework.util.Assert;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.client.RestTemplate;
import org.springframework.web.servlet.ModelAndView;

import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.net.URI;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * title: UnitController
 * package: com.sptg.gateway.controller.estate
 * description: 单元 网关控制器
 *
 *
 *
 */
@RestController
@RequestMapping("/estate/unit")
@RequiredArgsConstructor
public class UnitController {

    private final UnitFeign unitFeign;
    private final BuildingFeign buildingFeign;
    private final SecurityServiceInterface<Account> securityServiceInterface;
    private final OrgFeign orgFeign;
    private final ResidentialFeign residentialFeign;
    private final SysOperationLogFeign sysOperationLogFeign;
    private final GridEstateFeign gridEstateFeign;
    private final QLinAuthFeign qLinAuthFeign;
    private final RoomFeign roomFeign;
    private final FloorFeign floorFeign;

    /**
     * 功能描述:单元页面初始化
     *
     * @return ModelAndView
     */
    @GetMapping("/index/{buildingId}")
    @Security(token = EstateSecurityToken.ESTATE_UNIT_MANAGE,format = DataFormatEnum.VIEW)
    public ModelAndView index(@PathVariable(value = "buildingId")Long buildingId){
        return new ModelAndView("estate/unit-manage").addObject("buildingId", buildingId);
    }

    /**
     * 功能描述: 管理页面初始化
     *
     * @param id 必填
     * @return ModelAndView
     */
    @GetMapping(value = "/detail/{id}")
    @Security(token = EstateSecurityToken.ESTATE_RESIDENTIAL_UPDATE, format = DataFormatEnum.VIEW)
    public ModelAndView detail(@PathVariable(value = "id") Long id) {
        Unit unit = this.unitFeign.findUnitById(id);
        if (null == unit) {
            //  没有找到要的单元
            return new ModelAndView("common/404");
        }
        ModelAndView modelAndView = new ModelAndView("estate/unit-detail");
        modelAndView.addObject("unit", unit);
        return modelAndView;
    }

    /**
     * 功能描述:单元列表查询
     *
     * @param unitDto 单元业务对象（含查询条件）
     * @param page    分页条件
     * @return ResultBean<Page < Unit>>
     */
    @GetMapping(value = "/showUnitList")
    @Security(token = EstateSecurityToken.ESTATE_UNIT_MANAGE, format = DataFormatEnum.JSON)
    public ResultBean<Page<Unit>> showUnitList(UnitDto unitDto, Page<Unit> page) {
        //根据树形选择加载数据；如果没有选中ID，则根据当前账号查询
        Account onLineUser = this.securityServiceInterface.getOnLineUser();
        Organization userOrg = this.orgFeign.getOrgById(onLineUser.getOrgId());
        if (null == unitDto.getSelectTreeId() || null == unitDto.getSelectTreeType()) {
            if (userOrg.getOrgType() == OperationConstant.THREE) {
                unitDto.setEstatePosition(onLineUser.getOrgPosition());
            } else {
                unitDto.setOrgPosition(onLineUser.getOrgPosition());
            }
//            unitDto.setOrgPosition(onLineUser.getOrgPosition());
        } else {
            //1 机构 2 小区
            Organization organization = this.orgFeign.getOrgById(unitDto.getSelectTreeId());
            if (organization.getFlag() == OperationConstant.ONE) {
                //机构
                if (organization.getOrgType() == OperationConstant.THREE) {//物业
                    unitDto.setEstatePosition(organization.getPosition());
                } else {
                    unitDto.setOrgPosition(organization.getPosition());
                }
            }
            if (organization.getFlag() == OperationConstant.TWO) {
                //小区(则还要判断当前账号的身份)
                if (userOrg.getOrgType() == OperationConstant.THREE) {
                    //物业账号
                    unitDto.setEstatePosition(this.residentialFeign.findResidentialById(organization.getId()).getEstatePosition());
                } else {
                    //机构账号
                    unitDto.setOrgPosition(organization.getPosition());
                }
            }
        }
        unitDto.setPage(page);
        return this.unitFeign.showUnitList(unitDto);
    }

    /**
     * 功能描述:根据id获取单元
     *
     * @return ModelAndView
     */
    @GetMapping(value = "/findUnitById/{id}")
    @Security(token = EstateSecurityToken.ESTATE_UNIT_MANAGE, format = DataFormatEnum.JSON)
    public ResultBean<Unit> findUnitById(@PathVariable(value = "id") Long id) {
        Unit unit = this.unitFeign.findUnitById(id);
        Assert.notNull(unit, "单元不存在");
        return new ResultBean<>(ResultEnum.SUCCESS, unit);
    }

    /**
     * 功能描述:新增页面初始化
     *
     * @return ModelAndView
     */
    @GetMapping(value = "/add")
    @Security(token = EstateSecurityToken.ESTATE_UNIT_ADD, format = DataFormatEnum.VIEW)
    public ModelAndView add(String selectTreeId) {
        ModelAndView modelAndView = new ModelAndView("estate/unit-add");
        modelAndView.addObject("selectTreeId", selectTreeId);
        return modelAndView;
    }

    /**
     * 功能描述:新增页面初始化-带参数
     *
     * @return ModelAndView
     */
    @GetMapping(value = "/add/{residentialId}/{flag}")
    @Security(token = EstateSecurityToken.ESTATE_UNIT_ADD, format = DataFormatEnum.VIEW)
    public ModelAndView add(@PathVariable(value = "residentialId") Long residentialId, @PathVariable(value = "flag") Integer flag) {
        ModelAndView modelAndView = new ModelAndView("estate/unit-add");
        modelAndView.addObject("residentialId", residentialId);
        return modelAndView;
    }

    /**
     * 功能描述:新增单元
     *
     * @param unitDto 单元业务对象
     * @return ResultBean<Boolean>
     */
    @PostMapping(value = "/addUnit")
    @Security(token = EstateSecurityToken.ESTATE_UNIT_ADD, format = DataFormatEnum.JSON)
    public ResultBean<Boolean> addUnit(UnitDto unitDto) {
        ResultBean<Boolean> bean;
        Account onLineUser = this.securityServiceInterface.getOnLineUser();
        unitDto.setCreateAccountId(onLineUser.getId());

        Building building = buildingFeign.findBuildingById(unitDto.getBuildingId());
        if (building == null){
            return new ResultBean<>(ResultEnum.NOT_FIND);
        }
        unitDto.setResidentialId(building.getResidentialId());

        ResultBean<UnitDto> unitDtoResultBean = this.unitFeign.addUnit(unitDto);
        if (null != unitDtoResultBean) {
            unitDto.setId(unitDtoResultBean.getData().getId());
//            unitDto.setUnitNumber(SymbolConstant.B_NO + CommonUtil.autoZeroFilling(4, Integer.valueOf(unitDtoResultBean.getData().getUnitNumber())));
            bean = this.unitFeign.updateUnit(unitDto);
            /*---------------------------------------------------记录日志--------------------------------------------------------*/
            SysOperationLog sysOperationLog = new SysOperationLog(SysLogMenuConstant.ESTATE_UNIT_MANAGE, SysLogConstant.ADD_EVENT,
                    WebUtils.getIp(), unitDto, onLineUser, unitDto);
            sysOperationLogFeign.info(sysOperationLog);
            /*------------------------------------------------------------------------------------------------------------------*/

            /*---------------------------------------------------推送单元到网格--------------------------------------------------------*/
            ResultBean<String> stringResultBean = this.gridEstateFeign.gridUnitPush(unitDto);
            if (stringResultBean.getData() != null && stringResultBean.getData().equals("操作成功")) {
                SysOperationLog sysOperationLog1 = new SysOperationLog("推送单元数据到网格成功", SysLogConstant.ADD_EVENT,
                        WebUtils.getIp(), unitDto, onLineUser, unitDto);
                sysOperationLogFeign.info(sysOperationLog1);
            } else {
                SysOperationLog sysOperationLog1 = new SysOperationLog("推送单元数据到网格失败", SysLogConstant.ADD_EVENT,
                        WebUtils.getIp(), unitDto, onLineUser, unitDto);
                sysOperationLogFeign.info(sysOperationLog1);
            }
            return new ResultBean<>(ResultEnum.SUCCESS);
            /*------------------------------------------------------------------------------------------------------------------*/
        } else {
            bean = new ResultBean<>(ResultEnum.ERROR);
        }
        return bean;
    }

    /**
     * 功能描述: 编辑页面初始化
     *
     * @param id 必填
     * @return ModelAndView
     */
    @GetMapping(value = "/edit/{id}")
    @Security(token = EstateSecurityToken.ESTATE_UNIT_UPDATE, format = DataFormatEnum.VIEW)
    public ModelAndView edit(@PathVariable(value = "id") Long id) {
        Unit unit = this.unitFeign.findUnitById(id);
        if (null == unit) {
            //  没有找到要修改的单元
            return new ModelAndView("common/404");
        }
        ModelAndView modelAndView = new ModelAndView("estate/unit-edit");
        modelAndView.addObject("unit", unit);
        return modelAndView;
    }

    /**
     * 功能描述:编辑单元
     *
     * @param unitDto 单元业务对象
     * @return ResultBean<Boolean>
     */
    @PostMapping(value = "/updateUnit")
    @Security(token = EstateSecurityToken.ESTATE_UNIT_UPDATE, format = DataFormatEnum.JSON)
    public ResultBean<Boolean> updateUnit(UnitDto unitDto) {
        if (null == unitDto || null == unitDto.getId()) {
            //  参数异常
            return new ResultBean<>(ResultEnum.PARAMETER_ANOMALY);
        }
        Unit oldObj = this.unitFeign.findUnitById(unitDto.getId());
        ResultBean<Boolean> resultBean = this.unitFeign.updateUnit(unitDto);
        if (null != resultBean.getData() && resultBean.getData()) {
            Account onLineUser = this.securityServiceInterface.getOnLineUser();
            /*---------------------------------------------------记录日志--------------------------------------------------------*/
            Unit newObj = this.unitFeign.findUnitById(unitDto.getId());
            SysOperationLog sysOperationLog = new SysOperationLog(SysLogMenuConstant.ESTATE_UNIT_MANAGE, SysLogConstant.ALTER_EVENT,
                    WebUtils.getIp(), unitDto, onLineUser, oldObj, newObj);
            sysOperationLogFeign.info(sysOperationLog);
            /*------------------------------------------------------------------------------------------------------------------*/
        }
        return resultBean;
    }

    /**
     * 功能描述:删除单元
     *
     * @param unitDto 单元业务对象
     * @return ResultBean<Boolean>
     */
    @PostMapping(value = "/deleteUnit")
    @Security(token = EstateSecurityToken.ESTATE_UNIT_DELETE, format = DataFormatEnum.JSON)
    public ResultBean<Boolean> deleteUnit(UnitDto unitDto) {
        if (null == unitDto || null == unitDto.getId()) {
            //  参数异常
            return new ResultBean<>(ResultEnum.PARAMETER_ANOMALY);
        }
        Unit oldObj = this.unitFeign.findUnitById(unitDto.getId());
        unitDto.setStatus(OperationConstant.MINUS_ONE);
        ResultBean<Boolean> resultBean = this.unitFeign.deleteUnit(unitDto);
        if (null != resultBean.getData() && resultBean.getData()) {
            Account onLineUser = this.securityServiceInterface.getOnLineUser();
            /*---------------------------------------------------记录日志--------------------------------------------------------*/
            SysOperationLog sysOperationLog = new SysOperationLog(SysLogMenuConstant.ESTATE_UNIT_MANAGE, SysLogConstant.DELETE_EVENT,
                    WebUtils.getIp(), unitDto, onLineUser, oldObj);
            sysOperationLogFeign.info(sysOperationLog);
            /*------------------------------------------------------------------------------------------------------------------*/
        }
        return resultBean;
    }

    /**
     * 功能描述:批量删除单元
     *
     * @param ids 选中id集合
     * @return ResultBean<Boolean>
     */
    @PostMapping(value = "/deleteUnitList")
    @Security(token = EstateSecurityToken.ESTATE_UNIT_DELETE, format = DataFormatEnum.JSON)
    public ResultBean<Boolean> deleteUnitList(@RequestBody List<String> ids) {
        if (null == ids || ids.size() <= 0) {
            //  参数异常
            return new ResultBean<>(ResultEnum.PARAMETER_ANOMALY);
        }
        UnitDto unitDto = new UnitDto();
        unitDto.setIds(ids);
        ResultBean<Boolean> resultBean = this.unitFeign.deleteUnitList(unitDto);
        if (null != resultBean.getData() && resultBean.getData()) {
            Account onLineUser = this.securityServiceInterface.getOnLineUser();
            /*---------------------------------------------------记录日志--------------------------------------------------------*/
            SysOperationLog sysOperationLog = new SysOperationLog(SysLogMenuConstant.ESTATE_UNIT_MANAGE, SysLogConstant.DELETE_EVENT,
                    WebUtils.getIp(), unitDto, onLineUser);
            sysOperationLogFeign.info(sysOperationLog);
            /*------------------------------------------------------------------------------------------------------------------*/
        }
        return resultBean;
    }

    /**
     * 功能描述:导入单元
     *
     * @param flag 2 单元
     * @return ModelAndView
     */
    @RequestMapping(value = "/indexImportExcel/{flag}")
    @Security(token = EstateSecurityToken.ESTATE_UNIT_IMPORT, format = DataFormatEnum.VIEW)
    public ModelAndView indexImportExcel(@PathVariable(value = "flag") String flag) {
        ModelAndView modelAndView = new ModelAndView("common/import-excel");
        modelAndView.addObject("flag", flag);
        return modelAndView;
    }

    /**
     * 功能描述:导出单元
     *
     * @param response
     */
    @RequestMapping(value = "/exportUnit")
    @Security(token = EstateSecurityToken.ESTATE_UNIT_EXPORT)
    public void exportUnit(HttpServletResponse response) {
        try {
            response.sendRedirect("/common/excel/exportExcel/2");
        } catch (IOException e) {
            e.printStackTrace();
        }
    }


    /* -------------------------------------------------- 公共方法,不做权限限制 -------------------------------------------------- */
    /* -------------------------------------------------------------------------------------------------------------------------- */

    /**
     * 功能描述:根据父节点查找单元
     *
     * @param unitDto 单元业务对象
     * @return ResultBean<Unit>
     */
    @GetMapping(value = "/findListByPid")
    @Security(format = DataFormatEnum.JSON)
    public ResultBean<Unit> findListByPid(UnitDto unitDto) {
        if (null == unitDto || null == unitDto.getOrgPid()) {
            //  参数异常
            return new ResultBean<>(ResultEnum.PARAMETER_ANOMALY);
        }
        return this.unitFeign.findListByPid(unitDto);
    }


    /**
     * 功能描述:根据位置查询的所有单元
     *
     * @param dto 单元业务对象
     * @return ResultBean<Residential>
     */
    @GetMapping(value = "/findListByPosition")
    @Security(format = DataFormatEnum.JSON)
    public ResultBean<Unit> findListByPosition(UnitDto dto) {
        Account onLineUser = this.securityServiceInterface.getOnLineUser();
        Organization userOrg = this.orgFeign.getOrgById(onLineUser.getOrgId());
        //
        if (StringUtils.isEmpty(dto.getFrom()) || !"device".equals(dto.getFrom())) {
            if (null == dto.getSelectTreeId()) {
                if (userOrg.getOrgType() == OperationConstant.THREE) {
                    dto.setEstatePosition(onLineUser.getOrgPosition());
                } else {
                    dto.setOrgPosition(onLineUser.getOrgPosition());
                }
            } else {
                Organization organization = this.orgFeign.getOrgById(dto.getSelectTreeId());
                if (organization.getOrgType() == OperationConstant.THREE) {
                    dto.setEstatePosition(organization.getPosition());
                } else {
                    dto.setOrgPosition(organization.getPosition());
                }
            }
        }
        return this.unitFeign.findListByPosition(dto);
    }

    /**
     * 功能描述: 根据小区Id差单元列表
     *
     * @param xqId 小区Id,机构表的Id
     * @return ResultBean<Unit>
     */
    @GetMapping(value = "/getListByXqId/{xqId}")
    @Security(format = DataFormatEnum.JSON)
    public ResultBean<Unit> getListByXqId(@PathVariable(value = "xqId") Long xqId) {
        List<Unit> unitList = this.unitFeign.getListByXqId(xqId);
        return new ResultBean<>(ResultEnum.SUCCESS, unitList);
    }

    /**
     * 功能描述: 根据楼栋Id差单元列表
     *
     * @param buildingId 楼栋Id
     * @return ResultBean<Unit>
     */
    @GetMapping(value = "/getListByBuildingId/{buildingId}")
    @Security(format = DataFormatEnum.JSON)
    public ResultBean<Unit> getListByBuildingId(@PathVariable(value = "buildingId") Long buildingId) {
        UnitDto unitDto = new UnitDto();
        unitDto.setBuildingId(buildingId);
        List<Unit> unitList = this.unitFeign.getListByLikeName(unitDto);
        return new ResultBean<>(ResultEnum.SUCCESS, unitList);
    }

    /**
     * 功能描述: 根据名称模糊查询
     *
     * @param dto
     * @return ResultBean<Unit>
     */
    @GetMapping(value = "/getListByLikeName")
    @Security(format = DataFormatEnum.JSON)
    public ResultBean<Unit> getListByLikeName(UnitDto dto) {
        Account onLineUser = this.securityServiceInterface.getOnLineUser();
        Organization userOrg = this.orgFeign.getOrgById(onLineUser.getOrgId());
        if (null == dto.getSelectTreeId()) {
            if (userOrg.getOrgType() == OperationConstant.THREE) {
                dto.setEstatePosition(onLineUser.getOrgPosition());
            } else {
                dto.setOrgPosition(onLineUser.getOrgPosition());
            }
        } else {
            Organization organization = this.orgFeign.getOrgById(dto.getSelectTreeId());
            if (organization.getOrgType() == OperationConstant.THREE) {
                dto.setEstatePosition(organization.getPosition());
            } else {
                dto.setOrgPosition(organization.getPosition());
            }
        }
        return new ResultBean<>(ResultEnum.SUCCESS, this.unitFeign.getListByLikeName(dto));
    }


    /**
     * 同步亲邻单元数据
     *
     * @param residentialId
     * @return
     */
    @PostMapping(value = "/syncUnit/{id}")
    @Security(token = "estate:residential:sync")
    public ResultBean sync(@PathVariable(value = "id") Long residentialId) {

        Residential residential = this.residentialFeign.findResidentialById(residentialId);
        if (null == residential) {
            return new ResultBean(ResultEnum.FAILURE);
        }
        BuildingDto buildingDto = new BuildingDto();
        buildingDto.setResidentialId(residentialId);
        List<Building> buildingList = this.buildingFeign.getListByCondition(buildingDto);
        this.syncUnit(residential,buildingList);
        return new ResultBean(ResultEnum.SUCCESS);
    }

    private void syncUnit(Residential residential,List<Building> buildingList){
        int addResult = 0;
        int updateResult = 0;

        for (int j = 0; j < buildingList.size(); j++) {
            Building building = buildingList.get(j);
            RestTemplate restTemplate = new RestTemplate();
            ResponseEntity responseEntity = null;
            HttpHeaders headers = new HttpHeaders();
            headers.add(HttpHeaders.CONTENT_TYPE, "text/plain");
            headers.add(HttpHeaders.HOST, "qiotapi.qinlinkeji.com");
            headers.add("Cookie", "acw_tc=2760822c16041156819701506e1f0df2b6797f5010bb8a47e5c8d932fa360e");
            Map<String, Object> map = new HashMap<>();
            map.put("communityId", residential.getOldId());
            map.put("id", buildingList.get(j).getOldId());
            map.put("nodeType", 1);
            map.put("requestSource", 2);

            HttpEntity requestEntity = new HttpEntity<>(JSONUtil.toJsonStr(map), headers);
            ParameterizedTypeReference<Object> reference = new ParameterizedTypeReference<Object>() {
            };
            String sessionId = qLinAuthFeign.qLinAuth();
            String url = "https://qiotapi.qinlinkeji.com/admin/building/house/findChildList?sessionId="+sessionId;
            URI uri = URI.create(url);
            responseEntity = restTemplate.exchange(uri, HttpMethod.POST, requestEntity, reference);


            Map map1 = (Map) responseEntity.getBody();
            List<Map> list = (List<Map>) map1.get("data");

            for (int i = 0; i < list.size(); i++) {
                Unit unit = new Unit();

                Integer integer = (Integer) list.get(i).get("id");
                unit.setOldId(integer.toString());
                unit.setName((String) list.get(i).get("label"));
                unit.setBuildingId(building.getId());
                unit.setBuildingName(buildingList.get(j).getName());
                unit.setResidentialId(residential.getId());
                unit.setResidentialName(residential.getName());

                Unit unit1 = this.unitFeign.getByOldId(unit.getOldId());
                if (unit1 != null) {
                    unit.setId(unit1.getId());
                    boolean result1 = this.unitFeign.updateEntity(unit);
                    if(result1){
                        updateResult = 1 + updateResult;
                    }

                } else {
                    int result1 = this.unitFeign.addUnitBySync(unit);
                    if(result1 ==1){
                        building.setUnitNumber(building.getUnitNumber()+1);
                        this.buildingFeign.updateEntity(building);
                        residential.setUnitNumber(residential.getUnitNumber()+1);
                        this.residentialFeign.updateResidentialEntity(residential);
                    }
                    addResult = result1 + addResult;
                }
            }
            System.out.println("楼栋id为" + buildingList.get(j).getOldId() + "的单元：" + list);

        }
        System.out.println("单元：" + "   add:" + addResult + "      update:" + updateResult);
    }

    @PostMapping("/clearFloor/{unitId}")
    @Security(token = EstateSecurityToken.ESTATE_UNIT_DELETE)
    public ResultBean clearFloor(@PathVariable("unitId") Long unitId) {
        Unit unit = unitFeign.findUnitById(unitId);
        Long residentialId = unit.getResidentialId();
        Long buildingId = unit.getBuildingId();
        boolean b = false;

        //进行删除房屋信息处理
        RoomDto roomDto = new RoomDto();
        roomDto.setUnitId(unitId);
        List<Room> rooms = roomFeign.getListByCondition(roomDto);
        if (rooms.size() > 0) {
            List<Room> roomList = new ArrayList<>();
            rooms.forEach(room -> {
                Room room1 = new Room();
                BeanUtils.copyProperties(room, room1);
                room1.setStatus(OperationConstant.MINUS_ONE);
                roomList.add(room1);
            });
            b = roomFeign.updateRoomList(roomList);
        }

        //进行删除楼层信息处理
        FloorDto floorDto = new FloorDto();
        floorDto.setUnitId(unitId);
        List<Floor> floors = floorFeign.getListByCondition(floorDto);
        if (floors.size() > 0) {
            List<Floor> floorList = new ArrayList<>();
            floors.forEach(floor -> {
                Floor floor1 = new Floor();
                BeanUtils.copyProperties(floor, floor1);
                floor1.setStatus(OperationConstant.MINUS_ONE);
                floorList.add(floor1);
            });
            b = floorFeign.updateFloorList(floorList);
            if (b) {
                Unit unit1 = new Unit();
                unit1.setId(unitId);
                unit1.setFloorNumber(0);
                unit1.setRoomNumber(0);
                unitFeign.updateEntity(unit1);

                Residential residential = residentialFeign.findResidentialById(residentialId);
                Integer floorNumber = residential.getFloorNumber() - floors.size();
                residential.setFloorNumber(floorNumber);
                Integer roomNumber = residential.getRoomNumber() - rooms.size();
                residential.setRoomNumber(roomNumber);
                residentialFeign.updateEntity(residential);

                Building building = buildingFeign.findBuildingById(buildingId);
                Integer floorNum = building.getFloorNumber() - floors.size();
                building.setFloorNumber(floorNum);
                Integer roomNum = building.getRoomNumber() - rooms.size();
                building.setRoomNumber(roomNum);
                buildingFeign.updateEntity(building);
            }
        }
        return new ResultBean(ResultEnum.SUCCESS, b);
    }
}