package com.huimv.admin.jinghongtimer;

import cn.hutool.core.date.DateTime;
import cn.hutool.core.date.DateUtil;
import cn.hutool.core.util.ObjectUtil;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.huimv.admin.entity.EnergyData;
import com.huimv.admin.entity.EnergyEnvDevice;
import com.huimv.admin.entity.EnvData;
import com.huimv.admin.entity.EnvDevice;
import com.huimv.admin.jinghongtimer.Vo.*;
import com.huimv.admin.mapper.EnergyDataMapper;
import com.huimv.admin.mapper.EnergyEnvDeviceMapper;
import com.huimv.admin.mapper.EnvDataMapper;
import com.huimv.admin.mapper.EnvDeviceMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Configuration;
import org.springframework.http.HttpEntity;
import org.springframework.http.HttpHeaders;
import org.springframework.http.MediaType;
import org.springframework.http.ResponseEntity;
import org.springframework.scheduling.annotation.EnableScheduling;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.util.LinkedMultiValueMap;
import org.springframework.util.MultiValueMap;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.client.RestTemplate;

import java.io.IOException;
import java.util.Date;
import java.util.List;


@RestController
@RequestMapping("/test")
public class EnvTimer {
    @Autowired
    private EnvDataMapper envDataMapper;
    @Autowired
    private EnvDeviceMapper envDeviceMapper;
    @GetMapping("/test3")
    public Object test3(@RequestParam String verify) throws IOException {
        System.out.println(verify);
        return verify;
    }
    @PostMapping("/test3")
    public Object test3( @RequestBody JingHongWenShi data) throws IOException {
        System.out.println("test3");
        System.out.println(data.toString());
        JingHongWenShi2 data1 = data.getData();
        String deviceId = data1.getDeviceId();

        EnvDevice envDevice = envDeviceMapper.selectOne(new QueryWrapper<EnvDevice>().eq("device_code", deviceId));

        EnvData envData = new EnvData();
        envData.setDeviceId(envDevice.getDeviceCode());
        envData.setCreateTime(new Date());
        envData.setFarmId(envDevice.getFarmId());
        envData.setEnvHum(getHum(data1));
        envData.setEnvTemp(getTem(data1));
        envData.setUnitId(envDevice.getUnitId());
//        envData.setReceiveDate();
        envDataMapper.insert(envData);
        if("12004001250111000003".equals(deviceId)){
            EnvData envData12 = new EnvData();
            envData12.setDeviceId("12004001250111000012");
            envData12.setCreateTime(new Date());
            envData12.setFarmId(30);
            envData12.setEnvHum(getHum(data1));
            envData12.setEnvTemp(getTem(data1));
            envData12.setUnitId(198);
//        envData.setReceiveDate();
            envDataMapper.insert(envData12);
        }
        return data;
    }

    private String getHum(JingHongWenShi2 data1) {
        List<JingHongWenShi3> dataPoints = data1.getDataPoints();
        for (JingHongWenShi3 dataPoint : dataPoints) {
            if ("湿度".equals(dataPoint.getVariableName())){
                return dataPoint.getValue();
            }
        }
        return "0";
    }
    private String getTem(JingHongWenShi2 data1) {
        List<JingHongWenShi3> dataPoints = data1.getDataPoints();
        for (JingHongWenShi3 dataPoint : dataPoints) {
            if ("温度".equals(dataPoint.getVariableName())){
                return dataPoint.getValue();
            }
        }
        return "0";
    }


}
