package digital.laboratory.platform.imr.task;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import digital.laboratory.platform.imr.entity.SampleStorage;
import digital.laboratory.platform.imr.mapper.SampleStorageMapper;
import lombok.RequiredArgsConstructor;
import org.springframework.context.annotation.Configuration;
import org.springframework.scheduling.annotation.EnableScheduling;
import org.springframework.scheduling.annotation.Scheduled;

import java.time.LocalDateTime;
import java.util.List;

@Configuration
@EnableScheduling
@RequiredArgsConstructor
public class DestructionWarning {


    private final SampleStorageMapper storageMapper;


    //测试定时器
  @Scheduled(cron = "0 0/1 * * * ?")//60秒执行一次
  public void testTimer(){

      //获取取当前时间+7>=存储期限的sample
      LocalDateTime localDateTime = LocalDateTime.now().plusDays(7L);//当前时间+7天
      //查询数据库有哪些数据是符合该条件的
      QueryWrapper<SampleStorage> queryWrapper = new QueryWrapper<>();
      queryWrapper.le("storage_date",localDateTime);
      queryWrapper.eq("early_warning",0);//只能是未销毁预警状态的样本
      List<SampleStorage> sampleStorages = storageMapper.selectList(queryWrapper);
      if(!sampleStorages.isEmpty()){
          System.out.println("定时任务启动");
          for (SampleStorage sample:sampleStorages) {
              sample.setEarlyWarning(1);//销毁预警值;该样本待申请销毁
              storageMapper.updateById(sample);
          }
      }
  }
}
