package org.jeecg.modules.jobXxl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.xxl.job.core.handler.annotation.XxlJob;
import org.jeecg.modules.plan.entity.Plan;
import org.jeecg.modules.plan.service.IPlanService;
import org.jeecg.modules.workOrder.entity.WorkOrder;
import org.jeecg.modules.workOrder.service.IWorkOrderService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.time.LocalDate;
import java.time.LocalDateTime;
import java.time.ZoneId;
import java.util.Date;
import java.util.List;

@Component
public class CreateWorkOrderJob {

    @Autowired
    private IPlanService planService;
    @Autowired
    private IWorkOrderService workOrderService;

    @XxlJob("CreateWorkOrderJob")
    public void executeCreateWorkOrder() {
        // 查询第二天需要执行的计划
        List<Plan> planList = planService.queryList(new LambdaQueryWrapper<Plan>().eq(Plan::getCalDay, LocalDate.now(ZoneId.of("Asia/Shanghai")).plusDays(1L)));
        // 遍历计划生成工单
        for (Plan plan : planList) {
            WorkOrder workOrder = new WorkOrder();
            workOrder.setPlanId(plan.getId());
            workOrder.setDeviceId(plan.getDeviceId());
            workOrder.setModelType(plan.getRapairRange());
            workOrder.setStatus(0);
            workOrder.setCreateTime(Date.from(LocalDateTime.now().plusDays(1L).atZone(ZoneId.of("Asia/Shanghai")).toInstant()));
            try {
                if (!workOrderService.addWorkOrder(workOrder)) {
                    System.out.println("**********计划生成工单失败**********"+plan.getId());
                }
            } catch (Exception e) {
                e.printStackTrace();
                System.out.println("**********计划生成工单失败**********"+plan.getId()+"*****"+e.getMessage());
            }
        }
    }
}
