package com.steering.mes.mcp.coup;

import java.util.ArrayList;

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

import com.steering.comm.db.DBCommon;
import com.steering.mes.mcp.entity.CoupProcessPlan;
import com.steering.mes.mcp.entity.InspectResult;
import com.steering.mes.mcp.entity.ThreadInspectSelfCheck;
import com.steering.mes.mcp.entity.ThreadingResult;
import com.steering.mes.mcp.entity.ThreadingSelfcheck;
/**
 * 螺纹检验实绩
 * @author lzh520
 *
 */
public class ThreadInspectResultService extends DBCommon{
	/**
     * 查询简单作业计划
     * @param CoupProcessPlan 接箍加工计划实体类
     * @return CoreReturnObject
     */
    public CoreReturnObject getSinglePlan(CoupProcessPlan coupProcessPlan) {
        return this.getDataBySqlID(
                "ThreadInspectResultService.ProcessPlan.SingleQuery",
                new Object[] { coupProcessPlan.getBeginTime(),
                		coupProcessPlan.getEndTime(), coupProcessPlan.getLotNo(),
                		coupProcessPlan.getJudgeStoveNo(),coupProcessPlan.getBatchNo() });
    }
    
    /**
     * 查询加工计划
     * @param planNO
     * @return
     */
    public CoreReturnObject getCoupProcessPlan(String  planNO){
    	Object[] pamar = new Object[]{ planNO};
    	CoreReturnObject core = new CoreReturnObject();
    	core = this.getDataBySqlID("ThreadInspectResultService.ProcessPlan.GetThreadInspectResult", pamar);
    	CoreReturnSortObject ct = (CoreReturnSortObject) core.getResult();
    	if(ct.getResult().size()>0){
    		return this.getDataBySqlID(
    				"ThreadInspectResultService.ProcessPlan.GetThreadingResultAndCoupPlan", pamar);
    	}else
    	{
    		return this.getDataBySqlID(
        			"ThreadInspectResultService.ProcessPlan.CoupProcessPlan",pamar);
    	}
    }
    
    /**
     * 新增或修改螺纹检验实绩
     * @param result 螺纹检验实绩
     * @return
     */
    public CoreReturnObject saveThreadInspectResult(InspectResult  result) {

    	CoreReturnObject core = new CoreReturnObject();
    	CoreReturnObject core2 = new CoreReturnObject();
    	
    	Object[] pamar = new Object[]{ result.getInspectPieces()== null?0:result.getInspectPieces(),
    			result.getQualiriedPcs()== null?0:result.getQualiriedPcs(),
    			result.getRepairedPcs()== null?0:result.getRepairedPcs(),
    			result.getRepairedReason1(),result.getRepairedPcs1()== null? 0 :result.getRepairedPcs1(),
    			result.getRepairedReason2(),result.getRepairedPcs2()== null? 0 :result.getRepairedPcs2(),
    			result.getRepairedReason3(),result.getRepairedPcs3()== null? 0 :result.getRepairedPcs3(),
    			result.getRepairedReason4(),result.getRepairedPcs4()== null? 0 :result.getRepairedPcs4(),
    			result.getColShft(),result.getColGroup(),
    			result.getColUser(),result.getColTime(),
    			result.getPlineCode(),result.getPlanNO()};
    	
    	core = this.getThreadInspectResult(result.getPlanNO());
    	CoreReturnSortObject ct = (CoreReturnSortObject) core.getResult();
		if(ct.getResult().size()>0){
			//修改
			core2 = this.setDataBySqlID("ThreadInspectResultService.ProcessPlan.UpdateThreadInspectResult", pamar);
		}else{
			//新增
			core2 = this.setDataBySqlID("ThreadInspectResultService.ProcessPlan.AddThreadInspectResult",pamar);
		}
		return core2;
    }
    
    /**
     * 保存自检记录
     * @param list
     * @return
     */
    public CoreReturnObject saveThreadInspectSelfcheck(ArrayList<ThreadInspectSelfCheck> list){
    	CoreReturnObject core = new CoreReturnObject();
    	for (int i = 0; i < list.size(); i++) {
    		ThreadInspectSelfCheck tisc = list.get(i);
    		Object[] params = new Object[]{tisc.getTaper(),tisc.getLead(),
    				tisc.getHeight(),tisc.getPdiameterOvality(),
    				tisc.getBrokenThread(),tisc.getAl(),tisc.getRunOut(),tisc.getChamfer(),
    				tisc.getThreadForm(),tisc.getResult(),tisc.getPlanNo(),tisc.getSeqNo()};
    		CoreReturnObject coretsf = new CoreReturnObject();
    		coretsf = this.getDataBySqlID("ThreadInspectResultService.ProcessPlan.GetThreadingSelfcheckBySeq", 
    				new Object[]{tisc.getPlanNo(),tisc.getSeqNo()});
    		CoreReturnSortObject ct = (CoreReturnSortObject) coretsf.getResult();
    		if(ct.getResult().size()>0){
    			core= this.setDataBySqlID("ThreadInspectResultService.ProcessPlan.UpdateThreadingSelfcheck", params);
    		}else {
    			core= this.setDataBySqlID("ThreadInspectResultService.ProcessPlan.AddThreadingSelfcheck", params);
			}
		}
    	return core;
    }
    
    /**
     * 查询单个螺纹检验实绩
     * @param planNO
     * @return
     */
    public CoreReturnObject getThreadInspectResult(String  planNO){
    	return this.getDataBySqlID("ThreadInspectResultService.ProcessPlan.GetThreadInspectResult",new Object[]{planNO} );
    }
    
    /**
     * 查询自检记录
     * @param planNO
     * @return
     */
    public CoreReturnObject getThreadInspectSelfcheck(String  planNO){
    	return this.getDataBySqlID("ThreadInspectResultService.ProcessPlan.GetThreadingSelfcheck",new Object[]{planNO} );
    }
}
