package org.jeecg.modules.job;

import lombok.extern.slf4j.Slf4j;
import org.jeecg.modules.bigScreen.model.ReceiveStatus;
import org.jeecg.modules.device.entity.BuDevice;
import org.jeecg.modules.device.model.DeviceRiskAnalysis;
import org.jeecg.modules.device.service.IBuDeviceOnlineService;
import org.jeecg.modules.device.service.IBuDeviceService;
import org.jeecg.modules.energy.entity.BuElectric;
import org.jeecg.modules.energy.service.IBuElectricService;
import org.jeecg.modules.sensor.entity.BuSensor;
import org.jeecg.modules.sensor.entity.BuSensorType;
import org.jeecg.modules.sensor.entity.BuStatus;
import org.jeecg.modules.sensor.service.IBuSensorService;
import org.jeecg.modules.sensor.service.IBuSensorTypeService;
import org.jeecg.modules.sensor.service.IBuStatusService;
import org.quartz.Job;
import org.quartz.JobExecutionContext;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.client.RestTemplate;
import java.text.DecimalFormat;
import java.text.SimpleDateFormat;
import java.util.*;
import java.util.concurrent.TimeUnit;
import java.util.stream.Collectors;

@Slf4j
public class DeviceDataUploadJob implements Job {
    @Autowired
    private IBuDeviceService buDeviceService;
    @Autowired
    private IBuSensorService buSensorService;
    @Autowired
    private IBuStatusService buStatusService;
    @Autowired
    private IBuDeviceOnlineService buDeviceOnlineService;
    @Autowired
    private IBuSensorTypeService buSensorTypeService;
    @Autowired
    private IBuElectricService buElectricService;
    @Autowired
    private RestTemplate restTemplate;


    @Override
    public void execute(JobExecutionContext jobExecutionContext) {
        List<BuDevice> list1 = buDeviceService.list();
//        List<BuDevice> list = list1.stream().filter(a -> !a.getTypeId().equals("DJLC")).collect(Collectors.toList());
        for (BuDevice buDevice : list1){
            ReceiveStatus receiveStatus = new ReceiveStatus();
            receiveStatus.setSbllid(buDevice.getDeviceCode());//传入设备唯一编号list.get(i).getDeviceCode()
            SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
            receiveStatus.setCreatetime(sdf.format(new Date()));//传入上报时间
            receiveStatus.setSelftesttime(sdf.format(new Date()));//开机自检时间
            receiveStatus.setStarttime(sdf.format(new Date()));//开机时间
            receiveStatus.setFaulttime("");//故障发生时间
            List<BuSensor> buSensorElectric = buSensorService.getByCheckName(buDevice.getId(), "%电能%");
            BuStatus buStatus1 = buStatusService.getDataBySensorId(buSensorElectric.get(0).getId());
            if (buStatus1.getSensorStatus() == 0) {
                receiveStatus.setDevicestatus("关机");
            } else if(buStatus1.getSensorStatus() == 1){
                receiveStatus.setDevicestatus("待机");
            } else {
                receiveStatus.setDevicestatus("工作");
            }
            Calendar c = Calendar.getInstance();
            DeviceRiskAnalysis dayRunTime = buDeviceOnlineService.getDayRunTime(String.valueOf(c.get(Calendar.YEAR)),
                    String.valueOf(c.get(Calendar.MONTH) + 1), String.valueOf(c.get(Calendar.DAY_OF_MONTH)), buDevice.getId());
            if (dayRunTime == null) {
                receiveStatus.setRunningtime("0");//传入设备当天累计运行小时
            } else {
                double runTime = Double.parseDouble(dayRunTime.getRunTime()) / 60 / 60;
                DecimalFormat df = new DecimalFormat("0.00");
                receiveStatus.setRunningtime(df.format(runTime));//传入设备当天累计运行小时
            }
            BuSensorType buSensorType = buSensorTypeService.getById(buSensorElectric.get(0).getTypeId());
            String tableName = buSensorType.getTableHead() + "_online";
            List<BuElectric> buElectric = buElectricService.getBySensorId(tableName, buSensorElectric.get(0).getId());
            if (buElectric.get(0).getUdy()!= null) {
                receiveStatus.setVoltage(buElectric.get(0).getUdy() + "V");//传入设备当前电压
                receiveStatus.setElectricity(buElectric.get(0).getUdl() + "A");//传入设备当前电流
            }else {
                receiveStatus.setVoltage("");//传入设备当前电压
                receiveStatus.setElectricity("");//传入设备当前电流
            }
            receiveStatus.setRunningstatus("正常");
            //String url = "http://192.168.88.11:9797/emis/digitapi/zjyxzt/" + buDevice.getDeviceToken();
//            String url = "http://10.106.39.19:9797/emis/digitapi/zjyxzt/" + buDevice.getDeviceToken();
            String url = "http://127.0.0.1:8081/jeecg-boot/bigScreen/getTestMethod";
            ResultJson jsonData = restTemplate.postForObject(url, receiveStatus, ResultJson.class);
            if (jsonData == null) {
                log.info("与设备数字化平台接口通讯错误:" + jsonData.getMessage());
                return;
            } else {
                if (jsonData.getCode() != 0) {
                    log.info("与设备数字化平台接口调用状态异常！" + "错误消息为：" + jsonData.getMessage());
//                    log.info("设备编号:" +  buDevice.getDeviceCode());
//                    log.info("上传数据:" + receiveStatus);
//                    log.info("返回数据：" + jsonData);
                    System.out.println("设备编号:" + buDevice.getDeviceCode());
                    System.out.println("上传数据:" + receiveStatus);
                    System.out.println("返回数据:" + jsonData);
                } else {
                    log.info("与设备数字化平台接口调用状态正常！" + "消息为：" + jsonData.getMessage());
//                    log.info("设备编号:" +  buDevice.getDeviceCode());
//                    log.info("上传数据:" + receiveStatus);
//                    log.info("返回数据：" + jsonData);
                    System.out.println("设备编号:" + buDevice.getDeviceCode());
                    System.out.println("上传数据:" + receiveStatus);
                    System.out.println("返回数据:" + jsonData);
                }
            }
            try {
                TimeUnit.SECONDS.sleep(3);
//                        Thread.sleep(1000);//睡眠10秒
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
        }
    }
}
