package com.tofly.gpsboot.handler.plan;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.tofly.gpsboot.common.system.OrderStateEnum;
import com.tofly.gpsboot.common.system.PlanStateEnum;
import com.tofly.gpsboot.service.maintain.RepairorderService;
import com.tofly.gpsboot.service.paln.task.PlanService;
import com.tofly.gpsboot.util.DateTimeUtil;
import com.tofly.pojo.maintain.Repairorder;
import com.tofly.pojo.plan.Plan;
import com.xxl.job.core.biz.model.ReturnT;
import com.xxl.job.core.handler.annotation.XxlJob;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

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

/**
 * 抢维修超时处理
 * @author BZ-DJJ
 */
@Component
public class RepairOrderTimeOutHandler {

    @Autowired
    private RepairorderService repairorderService;

    @XxlJob("repairOrderTimeOutHandler")
    public ReturnT<String> repairOrderTimeOutHandler(String param) {
        // 判断当前抢维修计划状态处于 在 2:未开始 , 3:正在执行
        List<Repairorder> list = repairorderService.list(new QueryWrapper<Repairorder>().lambda()
                .eq(Repairorder::getState, OrderStateEnum.ORDER_STATE_NOT_START.getCode())
                .or()
                .eq(Repairorder::getState, OrderStateEnum.ORDER_STATE_BEING_PERFORMED.getCode()));
        // 判断计划结束时间是否小于当前时间,若小于状态则改为 9 超时
        LocalDateTime now = LocalDateTime.now();
        list.forEach(info ->{
            LocalDateTime dateTime = DateTimeUtil.fromLong2LocalDateTime(info.getEndTime().getTime());
            if(!now.isBefore(dateTime)){
                info.setState(OrderStateEnum.ORDER_STATE_OVERTIME.getCode());
            }
        });
        repairorderService.updateBatchById(list);
        return ReturnT.SUCCESS;
    }

}
