package com.huimv.modular.timer.tasks;//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.entity.DeviceStatusEntity;
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.ParseException;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import org.springframework.beans.factory.annotation.Autowired;
/**
 * @Author Anchor
 * @Date 2021/8/14 15:10
 * @Version 1.0.1
 */
@Slf4j
@Component
public class CheckDeviceUnable {


    @Autowired
    private CollectorRegisterService collectorRegisterService;
    @Autowired
    private HeartBeatRecordMapper heartBeatRecordMapper;
    @Autowired
    private CollectorRegisterMapper collectorRegisterMapper;


    //写一个sql应该能解决 目前没时间   后面增加   这个代码也太垃圾了    虽然用了SQL 还是好垃圾
    @Scheduled(cron = "0/59 * * * * ?")
    private void checkUnadleDevice() throws ParseException {
        System.out.println("zhixing");
        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.DeviceUnAble1(format, format1);


        LambdaUpdateWrapper<CollectorRegister> updateWrapper = Wrappers.lambdaUpdate();
        updateWrapper.in(CollectorRegister::getDeviceCode,strings)
                .set(CollectorRegister::getCanStatus,1);
        collectorRegisterMapper.update(null, updateWrapper);

    }
}

