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

import com.sky.ioc.config.RestTemplateConfig;
import com.sky.ioc.entity.domain.space.BuildingFloor;
import com.sky.ioc.mapper.space.BuildingFloorMapper;
import com.sky.ioc.service.space.BuildingFloorService;
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 BuildingFloorServiceImpl implements BuildingFloorService {
    @Autowired
    BuildingFloorMapper buildingFloorMapper;

    @Autowired
    RestTemplateConfig restTemplateConfig;

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


    @Override
    public ReturnMsg getBuildingFloorList() {
        // 创建RestTemplate
        RestTemplate restTemplate = restTemplateConfig.build();
        // 发送GET请求
        String url = restTemplateConfig.addBaseUrl(BUILDING_FLOOR_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){
                buildingFloorMapper.delete(null);
                for (int i=0;i<list.size();i++){
                    LinkedHashMap json = (LinkedHashMap) list.get(i);
                    BuildingFloor floor = new BuildingFloor();
                    floor.setId((Integer) json.get("id"));
                    floor.setName((String) json.get("name"));
                    floor.setBuildingId(Integer.parseInt(String.valueOf(json.get("buildingId"))));
                    floor.setCode(String.valueOf(json.get("code")));
                    String floorcode = String.valueOf(json.get("code"));
                    if(floorcode.startsWith("B")){
                        floor.setSortCode(Integer.parseInt("-"+floorcode.substring(1)));
                    }else if(floorcode.startsWith("A")){
                        floor.setSortCode(Integer.parseInt(floorcode.substring(1,floorcode.length()-1)));
                    }else if(floorcode.length()>1){
                        floor.setSortCode(Integer.parseInt(floorcode.substring(0,floorcode.length()-1)));
                    }

                    floor.setIsDel(Integer.parseInt(String.valueOf(json.get("delFlag"))));
                    floor.setCreateTime(String.valueOf(json.get("createTime")));
                    floor.setUpdateTime(String.valueOf(json.get("updateTime")));
                    buildingFloorMapper.insert(floor);
                }
            }
            return ReturnMsg.ok();
        }else{
            log.info("获取楼层数据："+message);
            return ReturnMsg.fail(message);
        }
    }
}
