package com.ygtx.gxt.task;

import com.ygtx.gxt.service.IGxtOrderPersonService;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.scheduling.annotation.EnableScheduling;
import org.springframework.stereotype.Component;

@EnableScheduling
@Component("workOrderPutAsideTimeTask")
public class WorkOrderPutAsideTimeTask {

    private static final Logger log = LoggerFactory.getLogger(WorkOrderPutAsideTimeTask.class);

    @Autowired
    private IGxtOrderPersonService orderPersonService;


    public void processWorkOrderPutAsideTime() {
        log.info("开始执行挂起时长统计任务");
        try {
            Integer workOrderFlowRecordsSize = orderPersonService.workOrderFlowRecordsSize();
            Integer repairOrderFlowRecordsSize = orderPersonService.repairOrderFlowRecordsSize();
            Integer workOrderFlowRecordsSizeNext = orderPersonService.selectWorkOrderFlowRecordsSize();
            Integer repairOrderFlowRecordsSizeNext = orderPersonService.selectRepairOrderFlowRecordsSize();

            if (workOrderFlowRecordsSize != null && workOrderFlowRecordsSizeNext != null && !workOrderFlowRecordsSizeNext.equals(workOrderFlowRecordsSize)) {
                orderPersonService.delWorkOrderFlowRecordsNext();
                orderPersonService.delWorkOrderFlowRecords();
                orderPersonService.addWorkOrderFlowRecords();
                orderPersonService.addWorkOrderFlowRecordsNext();
            }

            if (repairOrderFlowRecordsSize != null && repairOrderFlowRecordsSizeNext != null && !repairOrderFlowRecordsSizeNext.equals(repairOrderFlowRecordsSize)) {
                orderPersonService.delRepairOrderFlowRecordsNext();
                orderPersonService.delRepairOrderFlowRecords();
                orderPersonService.addRepairOrderFlowRecords();
                orderPersonService.addRepairOrderFlowRecordsNext();
            }
            log.info("挂起时长统计任务执行完成，原有{}个维保挂起数据，{}个维修挂起数据，实际 {} 个维保挂起数据，{} 个维修挂起数据",
                    workOrderFlowRecordsSizeNext, repairOrderFlowRecordsSizeNext, workOrderFlowRecordsSize, repairOrderFlowRecordsSize);
        }catch (Exception err) {
            log.error("执行执行挂起时长统计任务时发生异常", err);
        }
    }

}
