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

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.sky.ioc.config.RestTemplateConfig;
import com.sky.ioc.entity.domain.energy.EnergyDevice;
import com.sky.ioc.entity.domain.energy.EnergyDeviceType;
import com.sky.ioc.entity.domain.meeting.MeetingRoom;
import com.sky.ioc.mapper.energy.EnergyDeviceMapper;
import com.sky.ioc.mapper.energy.EnergyDeviceTypeMapper;
import com.sky.ioc.service.energy.EnergyDeviceService;
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 javax.annotation.Resource;
import java.util.ArrayList;
import java.util.LinkedHashMap;
import java.util.Map;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

@Service
@Slf4j
public class EnergyDeviceServiceImpl implements EnergyDeviceService {

    @Resource
    RestTemplateConfig restTemplateConfig;

    @Autowired
    EnergyDeviceMapper energyDeviceMapper;

    @Autowired
    EnergyDeviceTypeMapper energyDeviceTypeMapper;

    final static  String ENERGTY_DEVICE_URL="/api/energy/ENERGY/device/list?pageNum=1&pageSize=1000";

    final static  String WATER_HEATER_URL="/api/energy/ENERGY/device/controlWaterHeater";

    final static  String CONTROLAIRCONDITION_URL="/api/energy/ENERGY/device/controlAirCondition";

    final static  String LIGHT_URL="/api/energy/ENERGY/device/controlLight";

    @Override
    public ReturnMsg getDeviceList() {
        // 创建RestTemplate
        RestTemplate restTemplate = restTemplateConfig.build();
        // 发送GET请求
        String url = restTemplateConfig.addBaseUrl(ENERGTY_DEVICE_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){
                energyDeviceMapper.delete(null);
                for (int i=0;i<list.size();i++){
                    LinkedHashMap json = (LinkedHashMap) list.get(i);
                    EnergyDevice energyDevice = new EnergyDevice();
                    energyDevice.setId((Integer) json.get("id"));
                    energyDevice.setDeviceName((String) json.get("name"));
                    energyDevice.setDeviceLocation((String) json.get("location"));
                    energyDevice.setDeviceType(Integer.valueOf((String) json.get("deviceType")));
                    energyDevice.setUsage((String) json.get("usage"));
                    energyDevice.setGatewayCode((String) json.get("gatewayCode"));
                    energyDevice.setGatewayIp((String) json.get("gatewayIp"));
                    energyDevice.setLoopDetail((String) json.get("loopDetail"));
                    EnergyDeviceType energyDeviceType = energyDeviceTypeMapper.selectOne(new LambdaQueryWrapper<EnergyDeviceType>().eq(EnergyDeviceType::getCode,energyDevice.getDeviceType()));
                    if(energyDeviceType==null){
                        energyDeviceType = new EnergyDeviceType();
                        energyDeviceType.setCode(energyDevice.getDeviceType());
                        energyDeviceTypeMapper.insert(energyDeviceType);
                    }
                    energyDeviceMapper.insert(energyDevice);
                }
            }
            return ReturnMsg.ok();
        }else{
            log.info("获取能源设备数据："+message);
            return ReturnMsg.fail(message);
        }
    }

    @Override
    public ReturnMsg openWaterHeater(Integer deviceId, Integer open) {
        // 创建RestTemplate
        RestTemplate restTemplate = restTemplateConfig.build();
        // 发送GET请求
        String url = restTemplateConfig.addBaseUrl(CONTROLAIRCONDITION_URL+"?deviceId="+deviceId+"&open="+open);
        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){
            return ReturnMsg.ok();
        }else{
            log.info("热水器操作失败："+message);
            return ReturnMsg.fail(message);
        }
    }

    @Override
    public ReturnMsg openControlAirCondition(Integer deviceId, Double temperature, Double speed, Integer mode) {
        // 创建RestTemplate
        RestTemplate restTemplate = restTemplateConfig.build();
        // 发送GET请求
        String url = restTemplateConfig.addBaseUrl(WATER_HEATER_URL+"?deviceId="+deviceId+"&temperature="+temperature+"&speed="+speed+"&mode="+mode);
        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){
            return ReturnMsg.ok();
        }else{
            log.info("空调操作失败："+message);
            return ReturnMsg.fail(message);
        }
    }

    @Override
    public ReturnMsg openControlLight(Integer deviceId, Integer open) {
        // 创建RestTemplate
        RestTemplate restTemplate = restTemplateConfig.build();
        // 发送GET请求
        String url = restTemplateConfig.addBaseUrl(LIGHT_URL+"?deviceId="+deviceId+"&open="+open);
        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){
            return ReturnMsg.ok();
        }else{
            log.info("照明设备操作失败："+message);
            return ReturnMsg.fail(message);
        }
    }
}
