package com.tofly.base.handler.task;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.tofly.base.em.IsHandlerEnum;
import com.tofly.base.service.messge.UsertaskService;
import com.tofly.base.util.DateTimeUtil;
import com.tofly.pojo.message.Usertask;
import com.xxl.job.core.biz.model.ReturnT;
import com.xxl.job.core.handler.annotation.XxlJob;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

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

//@Component
//@Slf4j
//public class UserTaskStateHandler {
//    @Autowired
//    private UsertaskService usertaskService;

//    @XxlJob("userTaskStateHandler")
//    public ReturnT<String> userTaskStateHandler(String param){
//        // 获取当前 未开始的待办任务
//        List<Usertask> list = usertaskService.list(new QueryWrapper<Usertask>().lambda().eq(Usertask::getIshandle, IsHandlerEnum.IS_HANDLER_NOT_STARTED.getCode()));
//        LocalDateTime now = LocalDateTime.now();
//        // 判断当前任务是否应该执行
//        List<Usertask> collect = list.stream().filter(info -> null!=info.getStartTime() &&  DateTimeUtil.fromLong2LocalDateTime(info.getStartTime().getTime()).isBefore(now)).collect(Collectors.toList());
//
//        // 获取当前 待完成的待办任务
//        List<Usertask> list1 = usertaskService.list(new QueryWrapper<Usertask>().lambda().eq(Usertask::getIshandle, IsHandlerEnum.IS_HANDLER_PENDING.getCode()));
//        // 判断当前任务是否超时
//        List<Usertask> collect1 = list1.stream().filter(info ->null!=info.getEndTime() && DateTimeUtil.fromLong2LocalDateTime(info.getEndTime().getTime()).isBefore(now)).collect(Collectors.toList());
//
//        if(collect.size()>0){
//            collect.forEach(v->v.setIshandle(IsHandlerEnum.IS_HANDLER_PENDING.getCode()));
//            usertaskService.updateBatchById(collect);
//            log.info("更新开始执行代办任务状态size :{}",collect.size());
//        }
//
//        if(collect1.size()>0){
//            // 判断若为抢维修则是超时待完成,巡检则为超时
//            collect1.stream().filter(f-> "1".equals(f.getTkty())).forEach(v->v.setIshandle(IsHandlerEnum.IS_HANDLER_TIMEOUT_EXPIRED.getCode()));
//            collect1.stream().filter(f-> !"1".equals(f.getTkty())).forEach(v->v.setIshandle(IsHandlerEnum.IS_HANDLER_TIMEOUT.getCode()));
//            usertaskService.updateBatchById(collect1);
//            log.info("更新超时代办任务状态size :{}",collect1.size());
//        }
//        return ReturnT.SUCCESS;
//    }
//}
