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.toolkit.Wrappers;
import com.huimv.modular.alarmrecord.service.AlarmRecordService;
import com.huimv.modular.alarmvalueset.mapper.AlarmValueSetMapper;
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.eartagdata.entity.EartagData;
import com.huimv.modular.eartagdata.mapper.EartagDataMapper;
import com.huimv.modular.heartbeatrecord.entity.HeartBeatRecord;
import com.huimv.modular.heartbeatrecord.mapper.HeartBeatRecordMapper;
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.util.List;

@Slf4j
@Component
public class UpdateDeviceLastSendTime {

    @Autowired
    private HeartBeatRecordMapper   heartBeatRecordMapper   ;


    @Autowired
    private CollectorRegisterMapper collectorRegisterMapper    ;


    @Autowired
    private CollectorRegisterService collectorRegisterService     ;

    //更新设备最后一次上传时间
    @Scheduled(cron = "0/59 * * * * ?")
    public void checkDeviceAble() {


        LambdaQueryWrapper<CollectorRegister> wrapper49 = Wrappers.lambdaQuery();
        List<CollectorRegister> list = collectorRegisterMapper.selectList(wrapper49);

        for (CollectorRegister collectorRegister : list) {
            LambdaQueryWrapper<HeartBeatRecord> wrapper42 = Wrappers.lambdaQuery();
            wrapper42.eq(HeartBeatRecord::getDeviceCode,collectorRegister.getDeviceCode()).orderByDesc(HeartBeatRecord::getId).last("limit 1");
            HeartBeatRecord heartBeatRecord = heartBeatRecordMapper.selectOne(wrapper42);
            if(heartBeatRecord!=null){
                collectorRegister.setLastTime(heartBeatRecord.getCreateTime());
                collectorRegisterService.updateById(collectorRegister);
            }

        }
    }
}



















