package com.huimv.video.dhicc.timmer;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.huimv.video.dhicc.entity.SysTelecom;
import com.huimv.video.dhicc.entity.SysTelecomEvent;
import com.huimv.video.dhicc.entity.SysThreshold;
import com.huimv.video.dhicc.mapper.SysTelecomEventMapper;
import com.huimv.video.dhicc.mapper.SysTelecomMapper;
import com.huimv.video.dhicc.mapper.SysThresholdMapper;
import com.huimv.video.dhicc.service.ISysTelecomEventService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Component;

import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;
import java.util.List;
import java.util.stream.Collectors;


//主要用来生产烘干数据的 时间拿时间 --可以吧大华的代码赋值出来 自己改一改  应该是OK的
@Component
public class SysTelcomEnentTimmer {
    @Autowired
    private SysTelecomMapper sysTelecomMapper;
    @Autowired
    private SysTelecomEventMapper sysTelecomEventMapperMapper;
    @Autowired
    private ISysTelecomEventService sysTelecomEventService;

    @Autowired
    private SysThresholdMapper sysThresholdMapper;

    //检查烘干是否合格--找出时间 阈值直接计算
    // @Scheduled(cron = " 0/10 * * * * ? ")
    @Scheduled(cron = " 0 */59 * * * ? ")
    private void checkOk() {

        //拿对应牧场的阈值
        LambdaQueryWrapper<SysThreshold> wrapper1 = Wrappers.lambdaQuery();
        wrapper1.eq(SysThreshold::getFarmId, 1);
        SysThreshold sysThreshold = sysThresholdMapper.selectOne(wrapper1);//System.out.println(sysTelecoms);
        // System.out.println("yuzhi"+sysThreshold);

        SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        Calendar TimeDeviceId = Calendar.getInstance();
        TimeDeviceId.setTime(new Date());
        TimeDeviceId.add(Calendar.HOUR_OF_DAY, -30);
        Date began1time = TimeDeviceId.getTime();
        String startTime1 = format.format(began1time);
        String endTime1 = format.format(new Date());

        //拿事件
        LambdaQueryWrapper<SysTelecomEvent> wrapper = Wrappers.lambdaQuery();
        wrapper.eq(SysTelecomEvent::getFarmId, 1);
        wrapper.between(SysTelecomEvent::getEventTime, startTime1, endTime1);
        List<SysTelecomEvent> SysTelecomEvent = sysTelecomEventMapperMapper.selectList(wrapper);//System.out.println(sysTelecoms);
        // System.out.println("yuzhi"+SysTelecomEvent);

        for (SysTelecomEvent sysTelecomEvent : SysTelecomEvent) {
            // System.out.println("yuzhi"+sysTelecomEvent);
            Calendar eventTime = Calendar.getInstance();
            eventTime.setTime(sysTelecomEvent.getEventTime());
            //事件时间就是开始时间
            String EventStartTime = format.format(sysTelecomEvent.getEventTime());
            //一次烘干50度以上一般在8次左右  就是说40分钟  这里取一小时
            eventTime.add(Calendar.MINUTE, +60);
            Date EventEndTime1 = eventTime.getTime();
            String EventEndTime = format.format(EventEndTime1);

            //查第一次五十度事件之后的数据  统计持续在设定温度之上的事件
            LambdaQueryWrapper<SysTelecom> wrapper3 = Wrappers.lambdaQuery();
            wrapper3.eq(SysTelecom::getDeviceId, sysTelecomEvent.getDeviceId());
            wrapper3.eq(SysTelecom::getFarmId, 1);
            wrapper3.between(SysTelecom::getTimestamp, EventStartTime, EventEndTime);
            List<SysTelecom> sysTelecoms = sysTelecomMapper.selectList(wrapper3);//System.out.println(sysTelecoms);

            Integer count = 0;
            for (SysTelecom sysTelecom : sysTelecoms) {
                if (Double.parseDouble(sysTelecom.getTemp()) > Double.parseDouble(sysThreshold.getBenconValue())) {
                    count++;

                }
            }
//            if (Double.parseDouble(sysThreshold.getBenconValue()) < count * 5) {
            if (count >0 ) {
                System.out.println("高于阈值温度时间" + count * 5);
                //正确烘干为1

                sysTelecomEvent.setRemark("1");
            } else {
                sysTelecomEvent.setRemark("0");
            }
            System.out.println("存储" + sysTelecomEvent.getRemark());
            sysTelecomEventService.updateById(sysTelecomEvent);

            //System.out.println("事件时间=" + EventStartTime + sysTelecoms);
        }
    }

    //每小时执行一次

    // @Scheduled(cron = " 0/5 * * * * ? ")
//     @Scheduled(cron = " 0/50 * * * * ? ")
    @Scheduled(cron = " 0 0 * * * ? ")
    private void updateBox() {
        //获得设备列表 查找烘干数据
        SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        LambdaQueryWrapper<SysTelecom> lambdaQueryWrapper =
                new LambdaQueryWrapper<>();
        lambdaQueryWrapper
                .select(SysTelecom::getDeviceId);
        List<String> collect = sysTelecomMapper.selectObjs(lambdaQueryWrapper).stream().map(o -> (String) o).collect(Collectors.toList());
        //System.out.println(collect);
        for (String deviceId : collect) {
            Calendar TimeDeviceId = Calendar.getInstance();
            TimeDeviceId.setTime(new Date());
            TimeDeviceId.add(Calendar.MINUTE, -300);
            Date began1time = TimeDeviceId.getTime();
            String startTime1 = format.format(began1time);
            String endTime1 = format.format(new Date());
            LambdaQueryWrapper<SysTelecom> wrapper = Wrappers.lambdaQuery();
            wrapper.eq(SysTelecom::getDeviceId, deviceId);
            wrapper.eq(SysTelecom::getFarmId, 1);
            wrapper.between(SysTelecom::getTimestamp, startTime1, endTime1);
            List<SysTelecom> sysTelecoms = sysTelecomMapper.selectList(wrapper);//System.out.println(sysTelecoms);
            for (SysTelecom sysTelecom : sysTelecoms) {
                if (Double.parseDouble(sysTelecom.getTemp()) > 50) {
//                    System.out.println("温度" + sysTelecom.getTemp() + sysTelecom.getDeviceId() + sysTelecom.getTimestamp());
                    //产生时间段
                    Calendar eventTime = Calendar.getInstance();
                    eventTime.setTime(sysTelecom.getTimestamp());
                    eventTime.add(Calendar.MINUTE, -60);
                    Date began = eventTime.getTime();
                    String EventStartTime = format.format(began);
                    eventTime.add(Calendar.MINUTE, +110);
                    Date EventEndTime1 = eventTime.getTime();
                    String EventEndTime = format.format(EventEndTime1);
                    //查询在大于50摄氏度这条记录的 前60分钟 后60分钟  时间里，有没有存储过报警事件  有就放弃  没有就加一条
                    int count = sysTelecomEventService.count(Wrappers.<SysTelecomEvent>lambdaQuery()
                            .eq(SysTelecomEvent::getDeviceId, sysTelecom.getDeviceId())
                            .eq(SysTelecomEvent::getFarmId, sysTelecom.getFarmId())
                            .between(SysTelecomEvent::getEventTime, EventStartTime, EventEndTime));
//                    System.out.println("插结果" + count);
                    if (count == 0) {
                        // System.out.println("保存了一条");
                        SysTelecomEvent sysTelecomEvent = new SysTelecomEvent();
                        sysTelecomEvent.setDeviceId(sysTelecom.getDeviceId());
                        sysTelecomEvent.setEventTime(sysTelecom.getTimestamp());
                        sysTelecomEvent.setEventId(sysTelecom.getId());
                        sysTelecomEvent.setFarmId(sysTelecom.getFarmId());
                        sysTelecomEvent.setRemark(sysTelecom.getTemp());
                        sysTelecomEventService.save(sysTelecomEvent);
                    }
                    // System.out.println("抛弃");
                }
            }
        }
    }
}
