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

import com.sky.ioc.config.RestTemplateConfig;
import com.sky.ioc.entity.domain.space.Building;
import com.sky.ioc.mapper.space.BuildingMapper;
import com.sky.ioc.service.space.BuildingService;
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.List;
import java.util.Map;
import java.util.concurrent.ThreadLocalRandom;

@Service
@Slf4j
public class BuildingServiceImpl implements BuildingService {

    @Autowired
    RestTemplateConfig restTemplateConfig;

    @Autowired
    BuildingMapper buildingMapper;

    final static  String BUILDING_URL="/api/space/SPACE/buildingArea/list";

    @Override
    public ReturnMsg getBuildigData() {
        String url = restTemplateConfig.addBaseUrl(BUILDING_URL)+"?pageNum=1&pageSize=1000";
        // 创建RestTemplate
        RestTemplate restTemplate = restTemplateConfig.build();
        // 发送GET请求
        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){
            ThreadLocalRandom random = ThreadLocalRandom.current();
            ArrayList list = (ArrayList) body.get("rows");
            if(list!=null&&list.size()>0){
                buildingMapper.delete(null);
                for (int i=0;i<list.size();i++){
                    LinkedHashMap json = (LinkedHashMap) list.get(i);
                    Building building = new Building();
                    building.setId((Integer) json.get("id"));
                    building.setType(String.valueOf(json.get("buildingType")));
                    building.setName(String.valueOf(json.get("name")));
                    if(json.get("buildingArea")!=null){
                        building.setBuildingArea(String.valueOf(json.get("buildingArea")));
                    }
                    if(json.get("usableArea")!=null){
                        building.setUseableArea(String.valueOf(json.get("usableArea")));
                    }
                    if(json.get("enterTime")!=null){
                        building.setEnterTime(String.valueOf(json.get("enterTime")));
                    }
                    buildingMapper.insert(building);
                }
            }
            return ReturnMsg.ok();

        }else{
            log.info("获取碳配额数据："+message);
            return  ReturnMsg.fail(message);
        }
    }

    @Override
    public ReturnMsg listBuilding() {
        List<Building> list = buildingMapper.selectList(null);
        return ReturnMsg.ok(list);
    }
}
