package com.huimv.modular.timer.tasks;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.huimv.modular.collectorregister.entity.CollectorRegister;
import com.huimv.modular.collectorregister.mapper.CollectorRegisterMapper;
import com.huimv.modular.collectorregister.service.CollectorRegisterService;
import com.huimv.modular.heartbeatrecord.entity.HeartBeatRecord;
import com.huimv.modular.heartbeatrecord.mapper.HeartBeatRecordMapper;
import com.huimv.modular.timer.constant.ImportanteConfig;
import lombok.extern.slf4j.Slf4j;
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.Date;
import java.util.List;

/**
 * @Author Anchor
 * @Date 2021/8/14 17:46
 * @Version 1.0.1
 */

@Slf4j
@Component
public class CheckDeviceAble {

    //查找掉线的设备是否还掉线，让他上线 --对应的有一个让设备下线的
    //更新采集器温度
    @Autowired
    private CollectorRegisterService collectorRegisterService;

    @Autowired
    private HeartBeatRecordMapper heartBeatRecordDao ;

    @Autowired
    private CollectorRegisterMapper collectorRegisterMapper;

    @Autowired
    private CollectorRegisterMapper  collectorRegisterDao;

    @Scheduled(cron = "0/59 * * * * ?")
    public void  checkDeviceAble() {

        Date now = new Date();//end
        Date GapTimeBefor_gap = new Date(now.getTime() - ImportanteConfig.CHECK_DEVICE_UNABLE_GAP_TIME);//star
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        String format = sdf.format(GapTimeBefor_gap);
        String format1 = sdf.format(new Date());
        List<String> strings = collectorRegisterMapper.DeviceAble1(format, format1);
        
        LambdaUpdateWrapper<CollectorRegister> updateWrapper = Wrappers.lambdaUpdate();
        updateWrapper.in(CollectorRegister::getDeviceCode,strings)
                .set(CollectorRegister::getCanStatus,0);
        collectorRegisterMapper.update(null, updateWrapper);

        collectorRegisterMapper.updateTemp();
        
        
        
    }
}
