package com.steering.mes.mcp.heatTreatment;

import java.util.ArrayList;

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.AppearanceCheckResult;
import com.steering.mes.mcp.entity.AppearanceCheckResultD;


/**
 * 尺寸外观检查实绩
 * @author lzh 2015-7-13
 *
 */
public class AppearanceService extends DBCommon {

	/**
	 * 按计划号查询尺寸外观检查实绩
	 * @param planNo 计划号
	 * @return
	 */
	public CoreReturnObject getAppearanceResultById(String planNo){
		Object[] params = new Object[]{planNo};
		return this.getDataBySqlID("AppearanceService.GetAppearanceResultById", params);
	}
	
	/**
	 * 按计划号查询作业计划和尺寸外观检查实绩
	 * @param planNo
	 * @return
	 */
	public CoreReturnObject getHttPlanAndAppearanceResult(String planNo){
		CoreReturnObject core = this.getAppearanceResultById(planNo);
		CoreReturnSortObject ct = (CoreReturnSortObject) core.getResult();
		if(ct.getResult().size()>0){
			return this.getDataBySqlID("AppearanceService.GetHttPlanAndResultByID", new Object[]{planNo});
		}else{
			PlanService planService = new PlanService();
			return planService.getRclPlan(planNo);
		}
	}
	
	/**
	 * 保存尺寸外观检查实绩
	 * @param result 尺寸外观检查实绩
	 * @return
	 */
	public CoreReturnObject saveAppearanceResult(AppearanceCheckResult result,ArrayList<AppearanceCheckResultD> list){
		CoreReturnObject core = this.getAppearanceResultById(result.getPlanNo());
		CoreReturnSortObject ct = (CoreReturnSortObject) core.getResult();
		Object[] params = new Object[]{result.getODMax(),result.getODMin(),
				result.getOutOfRoundness(),result.getEndStraightness(),
				result.getFullLengthStraightness(),result.getCheckResult(),
				result.getCheckPieces(),result.getColShft(),result.getColGroup(),
				result.getColUser(),result.getColTime(),result.getPlanNo(),
				result.getPlineCode()};
		//结果表的增加和修改
		for (int i = 0; i < list.size(); i++) {
			AppearanceCheckResultD resultD = list.get(i);
    		Object[] param = new Object[]{resultD.getPipeNo(),
    				resultD.getEndAStraightness(),resultD.getEndAAvgOd(),
    				resultD.getEndAMaxOd(),resultD.getEndAMinOd(),
    				resultD.getEndAOvality(),resultD.getEndBStraightness(),
    				resultD.getEndBAvgOd(),resultD.getEndBMinOd(),resultD.getEndBMaxOd(),
    				resultD.getEndBOvality(),resultD.getPlanNo(),resultD.getSeqNo()};
    		CoreReturnObject coretsf = new CoreReturnObject();
    		coretsf = this.getAppearanceDByPlanNoAndSeqNo(resultD.getPlanNo(), resultD.getSeqNo());
    		CoreReturnSortObject ct1 = (CoreReturnSortObject) coretsf.getResult();
    		if(ct1.getResult().size()>0){
    			this.setDataBySqlID("AppearanceService.UpdateAppearanceD", param);
    		}else {
    			this.setDataBySqlID("AppearanceService.AddAppearanceD", param);
			}
		}
		//实绩表的增加和修改
		if(ct.getResult().size()>0){
			return this.setDataBySqlID("AppearanceService.UpdateAppearanceResult", params);
		}else{
			return this.setDataBySqlID("AppearanceService.AddAppearanceResult", params);
		}
	}
	
	/**
	 * 按计划号查询尺寸外观检查实绩结果表
	 * @param planNo
	 * @return
	 */
	public CoreReturnObject getAppearanceDById(String planNo){
		return this.getDataBySqlID("AppearanceService.GetAppearanceDById", new Object[]{planNo});
	}
	/**
	 * 按计划号和序号查询尺寸外观检查实绩结果表
	 * @param planNo
	 * @param seqNo
	 * @return
	 */
	public CoreReturnObject getAppearanceDByPlanNoAndSeqNo(String planNo,String seqNo){
		return this.getDataBySqlID("AppearanceService.GetAppearanceDByPlanNoAndSeqNo", 
				new Object[]{planNo,seqNo});
	}
}
