package com.sky.ioc.service.space.impl;

import com.sky.ioc.config.RestTemplateConfig;
import com.sky.ioc.entity.domain.space.BuildingRoom;
import com.sky.ioc.mapper.space.BuildingRoomMapper;
import com.sky.ioc.service.space.BuildingRoomService;
import com.sky.ioc.tool.ReturnMsg;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.ResponseEntity;
import org.springframework.stereotype.Service;
import org.springframework.web.client.RestTemplate;

import java.util.ArrayList;
import java.util.LinkedHashMap;
import java.util.Map;

@Service
@Slf4j
public class BuildingRoomServiceImpl implements BuildingRoomService {

    @Autowired
    BuildingRoomMapper buildingRoomMapper;

    @Autowired
    RestTemplateConfig restTemplateConfig;

    final static  String BUILDING_ROOM_URL="/api/space/SPACE/roomArea/list?pageNum=1&pageSize=90000";


    @Override
    public ReturnMsg getBuildingRoomList() {
        // 创建RestTemplate
        RestTemplate restTemplate = restTemplateConfig.build();
        // 发送GET请求
        String url = restTemplateConfig.addBaseUrl(BUILDING_ROOM_URL);
        ResponseEntity<Map> responseEntity = restTemplate.getForEntity(url, Map.class);
        // 获取响应对象里的 body 对象
        Map<String, Object> body = responseEntity.getBody();
        // 获取状态码
        Integer code = (Integer)body.get("code");
        // 获取响应信息
        String message = (String)body.get("msg");
        if(code==200){
            ArrayList list = (ArrayList) body.get("rows");
            if(list!=null&&list.size()>0){
                buildingRoomMapper.delete(null);
                for (int i=0;i<list.size();i++){
                    LinkedHashMap json = (LinkedHashMap) list.get(i);
                    BuildingRoom room = new BuildingRoom();
                    room.setId((Integer) json.get("id"));
                    room.setName((String) json.get("name"));
                    room.setFloorId(Integer.parseInt(String.valueOf(json.get("floorId"))));
                    room.setBuildingId(Integer.parseInt(String.valueOf(json.get("buildingId"))));
                    room.setBuildingArea(String.valueOf(json.get("buildingArea")));
                    room.setUseableArea(String.valueOf(json.get("usableArea")));
                    room.setPublicArea(String.valueOf(json.get("publicArea")));
                    room.setCode(String.valueOf(json.get("code")));
                    room.setDeptId(Integer.parseInt(String.valueOf(json.get("officeId"))));
                    room.setStatus(Integer.parseInt(String.valueOf(json.get("status"))));
                    room.setIsDel(Integer.parseInt(String.valueOf(json.get("delFlag"))));
                    buildingRoomMapper.insert(room);
                }
            }
            return ReturnMsg.ok();
        }else{
            log.info("获取楼层房间数据："+message);
            return ReturnMsg.fail(message);
        }
    }
}
