package com.steering.mes.mcp.heatTreatment;

import CoreFS.SA06.CoreReturnObject;
import CoreFS.SA06.CoreReturnSortObject;

import com.steering.comm.db.DBCommon;
import com.steering.mes.mcp.common.PlanService;
import com.steering.mes.mcp.entity.HttLoading;

/**
 * 热处理上料实绩
 * @author lzh 2015-7-20
 *
 */
public class HttLoadingService extends DBCommon{

	/**
	 * 根据计划号查询热处理计划和上料实绩
	 * @param planNo 计划号
	 * @return
	 */
	public CoreReturnObject getHttPlanAndLoadResult(String planNo){
		Object[] params = new Object[]{planNo};
		CoreReturnObject core = new CoreReturnObject();
		core = this.getLoadResult(planNo);
		CoreReturnSortObject ct = (CoreReturnSortObject) core.getResult();
		if(ct.getResult().size()>0){
			return this.getDataBySqlID("HttLoadingService.GetHttPlanAndResultByID", params);
		}else{
			PlanService planService = new PlanService();
			return planService.getRclPlan(planNo);
		}
	}
	/**
	 * 保存
	 * @param load
	 * @return
	 */
	public CoreReturnObject saveHttLoadingResult(HttLoading load ){
		CoreReturnObject core = new CoreReturnObject();
		CoreReturnObject coreMess = new CoreReturnObject();
		core = this.getLoadResult(load.getRclBatchId());
		CoreReturnSortObject ct = (CoreReturnSortObject) core.getResult();
		Object[] params = new Object[]{load.getLoadPieces(),load.getLoadShift(),
				load.getLoadGroup(),load.getLoadUser(),load.getLoadTime(),
				load.getOrderNo(),load.getOraderSeq(),load.getOrderQuantity(),
				load.getRclBatchId()};
		if(ct.getResult().size()>0){
			HeatTreatmentService httService = new HeatTreatmentService();
			CoreReturnObject core1 = httService.getHeatTreatmentResult(load.getRclBatchId());
			CoreReturnSortObject ct1 = (CoreReturnSortObject) core1.getResult();
			if(ct1.getResult().size()>0){
				coreMess.setV_errMsg("此计划已到热处理，不可更改");
				return coreMess;
			}
			//修改
			 this.setDataBySqlID("HttLoadingService.UpdateLoadResult", params);
			coreMess.setV_errMsg("上料成功。");
			return coreMess;
		}else{
			//新增
			this.setDataBySqlID("HttLoadingService.AddLoadResult", params);
			coreMess.setV_errMsg("上料成功。");
			return coreMess;
		}
	}
	
	/**
	 * 撤销上料
	 * @param planNo
	 * @return
	 */
	public CoreReturnObject returnHttLoadingResult(String planNo){
		HeatTreatmentService httService = new HeatTreatmentService();
		CoreReturnObject core = httService.getHeatTreatmentResult(planNo);
		CoreReturnSortObject ct = (CoreReturnSortObject) core.getResult();
		CoreReturnObject core2 = new CoreReturnObject();
		if(ct.getResult().size()>0){
			core2.setV_errMsg("撤销失败，此计划已到热处理实绩。");
		}else{
		   this.setDataBySqlID("HttLoadingService.ReturnHttLoadingResult", new Object[]{planNo});
		   core2.setV_errMsg("撤销成功");
		}
		return core2;
	}
	
	/**
	 * 根据计划号查询热处理上料实绩
	 * @param planNo 计划号
	 * @return
	 */
	public CoreReturnObject getLoadResult(String planNo){
		return this.getDataBySqlID("HttLoadingService.GetLoadResultByID", new Object[]{planNo});
	}
}
