package com.ruoyi.quartz.task;

import com.baomidou.mybatisplus.core.conditions.Wrapper;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
import com.ruoyi.common.utils.DateUtils;
import com.ruoyi.doubledefense.domain.CheckTaskInventory;
import com.ruoyi.doubledefense.domain.CheckTaskInventoryRecord;
import com.ruoyi.doubledefense.service.ICheckTaskInventoryRecordService;
import com.ruoyi.doubledefense.service.ICheckTaskInventoryService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import org.springframework.util.CollectionUtils;

import java.util.Date;
import java.util.List;
import java.util.Map;
import java.util.Set;
import java.util.stream.Collectors;

/**
 * 定时清理巡检任务清单
 *
 * @author ruoyi
 */
@Slf4j
@Component("checkTaskInventoryRecordTask")
public class CheckTaskInventoryRecordTask {
    @Autowired
    private ICheckTaskInventoryService checkTaskInventoryService;
    @Autowired
    private ICheckTaskInventoryRecordService checkTaskInventoryRecordService;

    /**
     * 定时清理巡检任务清单
     * 定时检查漏检任务清单
     * <p>
     */
    public void clearCheckTaskInventoryRecord() {
        log.info("【巡检定时器】检查漏检、清理已完成漏检开始：{}", DateUtils.getTime());


        //1、查询漏检的任务清单：状态等于未检且结束时间已过
//        checkTaskInventory = new CheckTaskInventory();
//        checkTaskInventory.setStatus(0l);
//        checkTaskInventory.setEndDateLt(new Date());
//        checkTaskInventoryList = checkTaskInventoryService.selectCheckTaskInventoryList(checkTaskInventory);
        LambdaQueryWrapper<CheckTaskInventory> queryWrapper = new LambdaQueryWrapper();
        queryWrapper.eq(CheckTaskInventory::getStatus, 0l);
        queryWrapper.le(CheckTaskInventory::getEndDate, new Date());// <=
        List<CheckTaskInventory> missedCheckTaskInventoryList = checkTaskInventoryService.list(queryWrapper);
        if (!CollectionUtils.isEmpty(missedCheckTaskInventoryList)) {
            List<String> recordIdList = missedCheckTaskInventoryList.stream().map(CheckTaskInventory::getId).collect(Collectors.toList());
            //2.1、更新任务清单表
            LambdaUpdateWrapper<CheckTaskInventory> updateWrapper = new LambdaUpdateWrapper<>();
            updateWrapper.set(CheckTaskInventory::getStatus, 2l);
            updateWrapper.in(CheckTaskInventory::getId, recordIdList);
            boolean updateCount = checkTaskInventoryService.update(updateWrapper);
            log.info("【巡检定时器】检查漏检更新任务清单条数：{}", updateCount);

            //2.2、更新任务清单表，与记录表保持一致
            LambdaUpdateWrapper<CheckTaskInventoryRecord> recordUpdateWrapper = new LambdaUpdateWrapper<>();
            recordUpdateWrapper.set(CheckTaskInventoryRecord::getStatus, 2l);
            recordUpdateWrapper.in(CheckTaskInventoryRecord::getId, recordIdList);
            boolean updateCountRecord = checkTaskInventoryRecordService.update(recordUpdateWrapper);
            log.info("【巡检定时器】检查漏检更新任务清单记录条数：{}", updateCountRecord);
        }

        log.info("【巡检定时器】检查漏检结束：{}", DateUtils.getTime());

        //1、查询任务清单：已完成状态的
        CheckTaskInventory checkTaskInventory = new CheckTaskInventory();
        checkTaskInventory.setStatus(1l);
        checkTaskInventory.setEndDateLt(new Date());
        List<CheckTaskInventory> checkTaskInventoryList1 = checkTaskInventoryService.selectCheckTaskInventoryList(checkTaskInventory);

        checkTaskInventory = new CheckTaskInventory();
        checkTaskInventory.setStatus(2l);
        checkTaskInventory.setEndDateLt(new Date());
        List<CheckTaskInventory> checkTaskInventoryList2 = checkTaskInventoryService.selectCheckTaskInventoryList(checkTaskInventory);
        checkTaskInventoryList2.addAll(checkTaskInventoryList1);

        //2、遍历删除任务清单：Mapper批量删除接口
        int deleteCheckTaskInventoryByIds = 0;
        if (!checkTaskInventoryList2.isEmpty()) {
//            Map<String, CheckTaskInventory> checkTaskInventoryMap = checkTaskInventoryList2.stream()
//                    .collect(Collectors.toMap(CheckTaskInventory::getId, a -> a, (k1, k2) -> k1));
//            Set<String> keySet = checkTaskInventoryMap.keySet();
//            String[] ids = keySet.toArray(new String[keySet.size()]);

            List<String> checkTaskInventoryIdList = checkTaskInventoryList2.stream().map(CheckTaskInventory::getId).collect(Collectors.toList());
            String[] ids = checkTaskInventoryIdList.toArray(new String[checkTaskInventoryIdList.size()]);
            deleteCheckTaskInventoryByIds += checkTaskInventoryService.deleteCheckTaskInventoryByIds(ids);
        }
        log.info("【巡检定时器】清理巡检任务清单条数：{}", deleteCheckTaskInventoryByIds);


        log.info("【巡检定时器】检查漏检、清理已完成漏检结束：{}", DateUtils.getTime());
    }


}
