package com.steering.mes.mcp.coup;



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


import com.steering.comm.db.DBCommon;
import com.steering.mes.mcp.entity.CoupCutPlan;
import com.steering.mes.mcp.entity.CutResult;

/**
 * 接箍切割实绩业务类
 * @author siy 2015/05/28
 * @version v1.0
 */
public class CoupCutService extends DBCommon {
	//CoupCutService coup = new CoupCutService();
    /**
     * 查询简单作业计划
     * @param coupCutPlan 作业计划实体类
     * @return CoreReturnObject
     */
    public CoreReturnObject getSinglePlan(CoupCutPlan coupCutPlan) {
        return this.getDataBySqlID(
                "CoupCutService.CutPlan.SingleQuery",
                new Object[] { coupCutPlan.getBeginTime(),
                        coupCutPlan.getEndTime(), 
                        coupCutPlan.getJudgeStoveNo(),coupCutPlan.getBatchNo() });
    }
    
    /**
     * 查询单个详细作业计划
     * @param planNO 计划号
     * @return
     */ 
    public CoreReturnObject getCoupPlan(String  planNO) {
    	Object[] pamar = new Object[]{ planNO };
    	CoreReturnObject cor = new CoreReturnObject();
    	cor = this.getDataBySqlID(
                "CoupCutService.CutPlan.GetCutResultByPlanNo",pamar);
    	CoreReturnSortObject ct = (CoreReturnSortObject) cor.getResult();
    	if(ct.getResult().size()>0){
    		return this.getDataBySqlID("CoupCutService.CutPlan.GetCutResultAndCoupPlan", pamar);
    	}else{
    	return this.getDataBySqlID(
                "CoupCutService.CutPlan.CoupPlan",pamar);
    	}
    }
    
    /**
     * 查询单个作业计划的管号队列
     * @param planNO 计划号
     * @return
     */
    public CoreReturnObject getPipeNO(String judgeStoveNo ,String batchNo) {
    	Object[] pamar = new Object[]{ judgeStoveNo, batchNo};
    	return this.getDataBySqlID(
                "CoupCutService.CutPlan.SelectPipeNO",pamar);
    }
    
    /**
     * 更新单个作业计划的管号状态
     * @param planNO 计划号
     * @param cutpieces 支数
     * @return
     */
    public CoreReturnObject updatePipeNO(String  planNO,String cutpieces) {
    	Object[] pamar = new Object[]{ planNO, cutpieces };
    	CoupCutService coup = new CoupCutService();
    	coup.retrunPipeNO(planNO);
    		return this.setDataBySqlID(
                "CoupCutService.CutPlan.UpdatePipeNO",pamar);
    }
    /**
     * 还原管号队列状态
     * @param planNO
     * @return
     */
    public CoreReturnObject retrunPipeNO(String  planNO) {
    	Object[] pamar = new Object[]{ planNO };
    		return this.setDataBySqlID(
                "CoupCutService.CutPlan.RetrunPipeNO",pamar);
    }
    
    /**
     * 查询切割实绩
     * @param planNO
     * @return
     */
    public CoreReturnObject getCutResultByPlanNo(String  planNO) {
    	Object[] pamar = new Object[]{ planNO };
    	return this.getDataBySqlID(
                "CoupCutService.CutPlan.GetCutResultByPlanNo",pamar);
    }
    /**
     * 新增或修改切割实绩
     * @param result 切割实绩
     * @return
     */
    public CoreReturnObject addCutResult(CutResult  result) {
    	
    	CoreReturnObject cor = new CoreReturnObject();
    	CoreReturnObject cor2 = new CoreReturnObject();
    	CoupCutService coup = new CoupCutService();
    	//查询切割实绩
    	cor = coup.getCutResultByPlanNo(result.getPlanNo());
    	CoreReturnSortObject rt = (CoreReturnSortObject) cor.getResult();
    	
		Object[] pamar = new Object[]{ result.getMachineNo(),
    			result.getCutPieces(),result.getQualifiedNumber(),
    			result.getRejectedPieces(),result.getRejectedReason(),
    			result.getIsRemaining(),result.getLvalidLength(),
    			result.getProducer(),result.getProducerTime(),
    			result.getColShft(),result.getColGroup(),
    			result.getColUser(),result.getColTime(),
    			result.getPlineCode(),result.getPlanNo()};
		
		if(rt.getResult().size()>0){
			//修改
    		cor2= this.setDataBySqlID("CoupCutService.CutPlan.UpdateCutResult", pamar);
    	}else{
    		// 新增
    		cor2 = this.setDataBySqlID("CoupCutService.CutPlan.AddCutResult",pamar);
    	}
		
		/*if(String.valueOf(cor2.getResult()).equals("1")){
			coup.updatePipeNO(result.getPlanNo(), result.getCutPieces());
		}*/
		return cor2;
    	
    	
    	
    	//保存切割实绩（新增 修改）
    	
    	//修改计划状态
    	//将接箍料出库
    	//新增接箍坯料
    }

    public static void main(String[] args) {
        CoupCutService srv = new CoupCutService();
        srv.getSinglePlan(new CoupCutPlan());
    }
}
