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

/**
 * 接箍车丝实绩业务类
 * @author siy 2015/05/28
 * @version v1.0
 */
public class CoupThreadingService extends DBCommon {
	
    /**
     * 查询简单作业计划
     * @param CoupProcessPlan 接箍加工计划实体类
     * @return CoreReturnObject
     */
    public CoreReturnObject getSinglePlan(CoupProcessPlan coupProcessPlan) {
        return this.getDataBySqlID(
                "CoupThreadingService.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("CoupThreadingService.ProcessPlan.GetThreadingResult", pamar);
    	CoreReturnSortObject ct = (CoreReturnSortObject) core.getResult();
    	if(ct.getResult().size()>0){
    		return this.getDataBySqlID(
    				"CoupThreadingService.ProcessPlan.GetThreadingResultAndCoupPlan", pamar);
    	}else
    	{
    		return this.getDataBySqlID(
        			"CoupThreadingService.ProcessPlan.CoupProcessPlan",pamar);
    	}
    }
    
    /**
     * 新增或修改车丝实绩
     * @param result 车丝实绩
     * @return
     */
    public CoreReturnObject addThreadingResult(ThreadingResult  result,ArrayList<ThreadingSelfcheck> list) {

    	CoreReturnObject core = new CoreReturnObject();
    	CoreReturnObject core2 = new CoreReturnObject();
    	CoupThreadingService coup = new CoupThreadingService();
    	Object[] pamar = new Object[]{ result.getMachineNo(),
    			result.getProducedPcs(),result.getQualiriedPcs(),
    			result.getRejectedPieces(),result.getRejectedReason(),
    			result.getProducer(),result.getProducerTime(),
    			result.getColShft(),result.getColGroup(),
    			result.getColUser(),result.getColTime(),
    			result.getPlineCode(),result.getPlanNo()};
    	
    	core = coup.getThreadingResult(result.getPlanNo());
    	CoreReturnSortObject ct = (CoreReturnSortObject) core.getResult();
    	//System.out.println(ct.getResult().size());
    	
    	for (int i = 0; i < list.size(); i++) {
    		ThreadingSelfcheck tsf = list.get(i);
    		Object[] params = new Object[]{tsf.getLead(),tsf.getHeight(),tsf.getTaper(),
    				tsf.getPitchDimeter(),tsf.getPlanNo(),tsf.getSeqNo()};
    		CoreReturnObject coretsf = new CoreReturnObject();
    		coretsf = this.getDataBySqlID("CoupThreadingService.ProcessPlan.GetThreadingSelfcheckByPlan", 
    				new Object[]{tsf.getPlanNo(),tsf.getSeqNo()});
    		CoreReturnSortObject ct1 = (CoreReturnSortObject) coretsf.getResult();
    		if(ct1.getResult().size()>0){
    			this.setDataBySqlID("CoupThreadingService.ProcessPlan.UpdateThreadingSelfcheck", params);
    		}else {
    			this.setDataBySqlID("CoupThreadingService.ProcessPlan.AddThreadingSelfcheck", params);
			}
		}
    	
		if(ct.getResult().size()>0){
			//修改
			core2 = this.setDataBySqlID("CoupThreadingService.ProcessPlan.UpdateThreadingResult", pamar);
		}else{
			//新增
			core2 = this.setDataBySqlID("CoupThreadingService.ProcessPlan.AddThreadingResult",pamar);
		}
		
		return core2;
    }
    
    /**
     * 查询单个车丝实绩
     * @param planNO
     * @return
     */
    public CoreReturnObject getThreadingResult(String  planNO){
    	return this.getDataBySqlID("CoupThreadingService.ProcessPlan.GetThreadingResult",new Object[]{planNO} );
    }
    
    /**
     * 查询自检记录
     * @param planNO
     * @return
     */
    public CoreReturnObject getThreadingSelfcheck(String  planNO){
    	return this.getDataBySqlID("CoupThreadingService.ProcessPlan.GetThreadingSelfcheck",new Object[]{planNO} );
    }

    
}
