package com.sw.mes.yb.production.service.impl;

import java.util.List;

import com.sw.mes.base.global.GlobalSwitch;
import com.sw.mes.base.global.GlobalSwitchNamespace;
import com.sw.mes.mqtt.service.NotificationService;
import com.sw.mes.yb.ipc.service.impl.MaterialOperationClickServiceImpl;
import com.sw.mes.yb.mcs.service.YbMesToMcsService;
import org.quartz.SchedulerException;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import com.sw.mes.mmcs.service.MMcsInterfaceService;
import com.sw.mes.quartz.entity.SysJob;
import com.sw.mes.quartz.service.SysJobService;
import com.sw.mes.yb.production.dto.AgainTask.AgainTaskDto;
import com.sw.mes.yb.production.dto.LoadingTask.LoadingTaskDto;
import com.sw.mes.yb.production.entity.AgainTask;
import com.sw.mes.yb.production.entity.AgainTaskDetail;
import com.sw.mes.yb.production.entity.LoadingTask;
import com.sw.mes.yb.production.notification.AgainTaskDetailMessage;
import com.sw.mes.yb.production.notification.AgainTaskMessage;
import com.sw.mes.yb.production.service.AgainTaskDetailService;
import com.sw.mes.yb.production.service.AgainTaskService;
import com.sw.mes.yb.production.service.DelayStartService;
import com.sw.mes.yb.production.service.LoadingTaskService;

@Component("delayStartTask")
public class DelayStartServiceImpl implements DelayStartService {

	@Autowired
	private SysJobService sysJobService;

	@Autowired
	private NotificationService notificationService;

	@Autowired
	private LoadingTaskService loadingTaskService;
	@Autowired
	private AgainTaskService againTaskService;
	@Autowired
	private AgainTaskDetailService againTaskDetailService;

	@Autowired
	private YbMesToMcsService mesToMcsService;


	/**
	 * 装料延时任务处理
	 * @param lotno
	 * @param id
	 */
	@Override
	public void zlTask(String lotno, Long id) {
		LoadingTask task = loadingTaskService.get(id);
		if (task.getRawStatus().equals(LoadingTaskDto.rawStatus_6)) {
			task.setRawStatus(LoadingTaskDto.rawStatus_0);
			loadingTaskService.saveOrUpdate(task);
			if (GlobalSwitch.enable(GlobalSwitchNamespace.NAMESPACE_AUTOMATION, "CanningMaterial", false)) {
				//下发生产任务接口 装罐料下发
				mesToMcsService.issueMoInterface(task);
			}
		}

		// 删除定时任务
		List<SysJob> sysJobs = sysJobService.findByJobName("zlTask:"+lotno + "-" + id);
		for (SysJob sysJob : sysJobs) {
			try {
				sysJobService.deleteJob(sysJob);
			} catch (SchedulerException e) {
				e.printStackTrace();
			}
		}

	}

	/**
	 * 复投延时任务处理
	 * @param lotno
	 * @param id
	 */
	@Override
	public void ftTask(String lotno, Long id) {
		AgainTask task = againTaskService.get(id);
		if (task.getRawStatus().equals(AgainTaskDto.rawStatus_6)) {
			task.setRawStatus(AgainTaskDto.rawStatus_0);
			againTaskService.saveOrUpdate(task);
		}

		//发送通知
		//发送MQTT通知
		AgainTaskMessage message = new AgainTaskMessage();
		message.setArea(task.getArea());
		message.setEquipmentCode(task.getEquipmentCode());
		message.setId(task.getId());
		message.setLotNo(task.getLotNo());
		message.setSeries(task.getSeries());
		notificationService.sendNotification(message);

		// 删除定时任务
		List<SysJob> sysJobs = sysJobService.findByJobName("ftTask:"+lotno + "-" + id);
		for (SysJob sysJob : sysJobs) {
			try {
				sysJobService.deleteJob(sysJob);
			} catch (SchedulerException e) {
				e.printStackTrace();
			}
		}
	}

	/**
	 * 灌料延时任务处理
	 * @param lotno
	 * @param id
	 */
	@Override
	public void glTask(String lotno, Long id) {
		AgainTaskDetail task = againTaskDetailService.get(id);
		if (task.getRawStatus().equals(AgainTaskDto.rawStatus_6)) {
			task.setRawStatus(AgainTaskDto.rawStatus_0);
			againTaskDetailService.saveOrUpdate(task);
			if (GlobalSwitch.enable(GlobalSwitchNamespace.NAMESPACE_AUTOMATION, "CanningMaterial", false)) {
				//下发生产任务接口 装罐料下发
				mesToMcsService.issueMoInterface(task);
			}
		}

		//发送通知
		//发送MQTT通知
        //MaterialOperationClickServiceImpl.sendAgainTaskDetailMessage(task, notificationService);

        // 删除定时任务
		List<SysJob> sysJobs = sysJobService.findByJobName("glTask:"+lotno + "-" + id);
		for (SysJob sysJob : sysJobs) {
			try {
				sysJobService.deleteJob(sysJob);
			} catch (SchedulerException e) {
				e.printStackTrace();
			}
		}
	}



}
