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

import java.text.SimpleDateFormat;
import java.util.Date;

import com.sw.mes.base.factory.init.entity.Equipment;
import com.sw.mes.core.context.BaseContextHandler;
import com.sw.mes.core.utils.BeanUtil;
import com.sw.mes.core.utils.DateUtil;
import com.sw.mes.outside.dto.DismantlingDto;
import com.sw.mes.outside.service.EapService;
import com.sw.mes.yb.production.entity.ProcessWorkingHours;
import com.sw.mes.yb.production.service.ProcessWorkingHoursService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;


import com.sw.mes.base.factory.init.service.EquipmentService;
import com.sw.mes.yb.ipc.dto.ButtonClickDto;
import com.sw.mes.yb.ipc.mapping.ButtonType;
import com.sw.mes.yb.ipc.service.IBaseButtonHandler;
import com.sw.mes.yb.production.entity.EquipmentWorkHoursLog;
import com.sw.mes.yb.production.service.EquipmentExtendStateService;
import com.sw.mes.yb.production.service.EquipmentWorkHoursLogService;
import org.springframework.util.ObjectUtils;

/**
 * 工时操作 点击处理
 *
 * @author 李儒
 *
 */
@Service
@Transactional
public class WorkHoursClickServiceImpl implements IBaseButtonHandler {

	@Autowired
	private EquipmentService equipmentService;
	@Autowired
	private EquipmentExtendStateService equipmentExtendStateService;
	@Autowired
	private EquipmentWorkHoursLogService equipmentWorkHoursLogService;
	@Autowired
	private ProcessWorkingHoursService processWorkingHoursService;
	@Autowired
	private EapService eapService;

	@Override
	public Object execute(ButtonClickDto dto) throws Exception {

		if (dto.getButtonName().equals("拆清开始")) {
			Equipment equipment = equipmentService.findByCode(dto.getEquipmentCode());
			if (ObjectUtils.isEmpty(equipment)){
				throw new RuntimeException("未查询到编号【"+dto.getEquipmentCode()+"】的设备");
			}
			ProcessWorkingHours workingHours = new ProcessWorkingHours();
			workingHours.setStepName("拆清工时");
			workingHours.setLotNo(dto.getLotNo());
			workingHours.setEquipmentCode(dto.getEquipmentCode());
			Date now = new Date();
			workingHours.setStartTime(now);
			workingHours.setArea(equipment.getArea());
			workingHours.setSeries(equipment.getSeries());
			workingHours.setWorkshop(equipment.getFactoryModel().getName());
			workingHours.setDistrict(equipment.getFactoryModel().getName().replace("片区",""));
			workingHours.setStartOperatorUser(BaseContextHandler.getUsername());
			processWorkingHoursService.saveOrUpdate(workingHours);

			DismantlingDto dismantlingDto = new DismantlingDto();
			dismantlingDto.setDeviceCode(dto.getEquipmentCode());
			eapService.dismantlingStart(dismantlingDto);

			return "操作:拆清开始";
		} else if (dto.getButtonName().equals("拆清结束")) {
			ProcessWorkingHours workingHours = processWorkingHoursService.findByLotNoAndStep(dto.getLotNo(), "拆清工时");
			if (ObjectUtils.isEmpty(workingHours)){
				throw new RuntimeException("未查询到炉次号【"+dto.getLotNo()+"】的拆请记录");
			}
			Date now = new Date();
			workingHours.setEndTime(now);
			workingHours.setTimeLong(now.getTime() - workingHours.getStartTime().getTime());
			workingHours.setEndOperatorUser(BaseContextHandler.getUsername());
			return "操作:拆清结束";
		}else if (dto.getButtonName().equals("吊料合炉盖开始")) {
			return "操作:吊料合炉盖开始";
		}

		return "未知操作:"+dto.getButtonName();
	}

	@Override
	public void writeWorkHours(ButtonClickDto dto) {
		if(ButtonType.isCalculationWorkHours(dto.getButtonName())) {
			EquipmentWorkHoursLog log = new EquipmentWorkHoursLog();
			BeanUtil.copyProperties(dto, log);

			String changePath = "";

			//获取上次操作
			EquipmentWorkHoursLog preLog = equipmentWorkHoursLogService.getByEquipmentCodeLastOperation(dto.getEquipmentCode());
			if(preLog!=null) {
				changePath = preLog.getButtonName();
				log.setStartTime(preLog.getEndTime());
				log.setWorkStep(ButtonType.getType(preLog.getButtonName()));
			}

			changePath = changePath + "->" + dto.getButtonName();

			log.setChangePath(changePath);
			log.setEndTime(new Date());

			if(log.getStartTime()!=null && log.getEndTime()!=null) {
				log.setTimeLong(log.getEndTime().getTime() - log.getStartTime().getTime());
			}else {
				log.setTimeLong(0L);
			}

			equipmentWorkHoursLogService.saveOrUpdate(log);
		}
	}

}
