package com.sw.mes.sap.verify.closeOrderState;

import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import com.sw.mes.core.enums.FactoryModelWorkTypeEnum;
import com.sw.mes.sap.dto.jip.JipCloseOrderVerifyResult;
import com.sw.mes.sap.verify.VerifyWorkOrderState;
import com.sw.mes.yb.plan.entity.ProductionOrder;
import com.sw.mes.yb.plan.entity.ProductionSchedule;
import com.sw.mes.yb.plan.service.ProductionOrderService;
import com.sw.mes.yb.plan.service.ProductionScheduleService;

/**
 * 若是当前工单为拉晶工单，则生产排程状态为已完成
 * 
 * @author liru
 * @date 2024-04-17
 */
@Component("productionScheduleVerify")
public class ProductionScheduleVerify implements VerifyWorkOrderState {

	@Autowired
	private ProductionOrderService productionOrderService;
	@Autowired
	private ProductionScheduleService productionScheduleService;

	@Override
	public void executeVerify(JipCloseOrderVerifyResult result, String AUFNR, String WERKS) {
		ProductionOrder productionOrder = productionOrderService.findByAufnr(AUFNR.toString());

		if (productionOrder == null) {
			result.setStatus(false);
			result.setMsg("工单不存在,不允许关单");
			return;
		}

		// 判断是否是拉晶工单
		if (productionOrder.getWorkType() == FactoryModelWorkTypeEnum.INGOT.key) {
			// 已排程
			if (productionOrder.getScheduled() == 1) {
				List<ProductionSchedule> productionSchedules = productionScheduleService.findByOderId(productionOrder.getId());
				for (ProductionSchedule productionSchedule : productionSchedules) {
					// 验证排程状态
					if(productionSchedule.getStatus()!=2 && productionSchedule.getStatus()!=3) {
						result.setStatus(false);
						result.setMsg("排程状态非完结,不允许关单");
						break;
					}
				}
			}
		}
	}

}
