package com.ruoyi.traceability.Task;

import com.alibaba.fastjson.JSONArray;
import com.ruoyi.common.core.text.Convert;
import com.ruoyi.common.json.JSONObject;
import com.ruoyi.traceability.domain.ProLogrecord;
import com.ruoyi.traceability.service.IProLogrecordService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.text.SimpleDateFormat;
import java.util.*;
import java.util.stream.Collectors;
import java.util.stream.Stream;

/**
 * 定时清除机台日志-清除两天前的数据
 */
@Component("DeleteMachineLog")
public class DeleteMachineLog {

    @Autowired
    private IProLogrecordService proLogrecordService;

    public void RunTask(){
        try {
            Calendar calendar = Calendar.getInstance();
            calendar.add(Calendar.DATE,-2);

            List<ProLogrecord> logrecords = proLogrecordService.selectProLogrecordList(new ProLogrecord());

            List<ProLogrecord> collect = logrecords.stream().filter(x -> x.getRecordTime().before(calendar.getTime())).collect(Collectors.toList());

            if(collect != null && collect.size() > 0){
                Optional<ProLogrecord> max = collect.stream().max(Comparator.comparingLong(ProLogrecord::getId));
                ProLogrecord proLogrecord = max.get();
                proLogrecordService.deleteProLogrecordById(proLogrecord.getId(),true);
            }
        }catch (Exception ex){
            System.out.println("定时任务执行异常:"+ex.getMessage());
        }


    }
}
