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

import com.sky.ioc.config.RestTemplateConfig;
import com.sky.ioc.entity.domain.space.BuildingStation;
import com.sky.ioc.mapper.space.BuildingStationMapper;
import com.sky.ioc.service.space.BuildingStationService;
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 BuildingStationServiceImpl implements BuildingStationService {

    @Autowired
    BuildingStationMapper buildingStationMapper;

    @Autowired
    RestTemplateConfig restTemplateConfig;

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


    @Override
    public ReturnMsg getBuildingStationList() {
        // 创建RestTemplate
        RestTemplate restTemplate = restTemplateConfig.build();
        // 发送GET请求
        String url = restTemplateConfig.addBaseUrl(BUILDING_STATION_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){
                buildingStationMapper.delete(null);
                for (int i=0;i<list.size();i++){
                    LinkedHashMap json = (LinkedHashMap) list.get(i);
                    BuildingStation station = new BuildingStation();
                    station.setId((Integer) json.get("id"));
                    station.setCode(String.valueOf(json.get("code")));
                    if(json.get("officeId")!=null){
                        station.setDeptId(Integer.parseInt(String.valueOf(json.get("officeId"))));
                    }
                    if(json.get("roomId")!=null){
                        station.setRoomId(Integer.parseInt(String.valueOf(json.get("roomId"))));
                    }
                   // station.setDeptId(Integer.parseInt(String.valueOf(json.get("officeId"))));
                    station.setStatus(Integer.parseInt(String.valueOf(json.get("status"))));
                    station.setIsDel(Integer.parseInt(String.valueOf(json.get("delFlag"))));

                    station.setType(String.valueOf(json.get("type")));
                    station.setCreateTime(String.valueOf(json.get("createTime")));
                    station.setUpdateTime(String.valueOf(json.get("updateTime")));
                    station.setIsBlock(Integer.parseInt(String.valueOf(json.get("isBlock"))));
                    buildingStationMapper.insert(station);
                }
            }
            return ReturnMsg.ok();
        }else{
            log.info("获取工位数据："+message);
            return ReturnMsg.fail(message);
        }
    }
}
