package com.steering.pss.plan.jgtransa;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;

import CoreFS.SA06.CoreReturnObject;
import CoreFS.SA08.CoreXmlSqlParsersFactory;
import CoreFS.SC01.CoreConstantData;

import com.steering.comm.db.DBCommon;
import com.steering.pss.plan.base.CorePlnComm;
import com.steering.pss.plan.jgtransa.mode.PlnZyJgCutEntity;
import com.steering.pss.plan.jgtransa.mode.PlnZyJgProEntity;

/**
 * 接箍作业计划处理
 * @author 杨天海
 * @date 2015.07.02
 * @version v1.1
 */
public class CoreJgOperatPln extends DBCommon{
	
	/**
	 * 查询接箍备料需求
	 * @return
	 */
	public CoreReturnObject queryJglInform(){
		CoreReturnObject cro = new CoreReturnObject();
		cro = this.getDataBySqlID("CoreJgOperatPln.queryJglInform");
		return cro;
	}
	
	/**
	 * 查询接箍备料需求
	 * @return
	 */
	public CoreReturnObject queryBlRequire(){
		CoreReturnObject cro = new CoreReturnObject();
		cro = this.getDataBySqlID("CoreJgOperatPln.queryBlRequire");
		return cro;
	}
	
	/**
	 * 查询物料基础信息
	 * @return
	 */
	public CoreReturnObject queryBaseMaterial(){
		CoreReturnObject cro = new CoreReturnObject();
		cro = this.getDataBySqlID("CoreJgOperatPln.queryBaseMaterial");
		return cro;
	}
	
	/**
	 * 查询钢级基础信息
	 * @return
	 */
	public CoreReturnObject queryBaseSteel(){
		CoreReturnObject cro = new CoreReturnObject();
		cro = this.getDataBySqlID("CoreJgOperatPln.queryBaseSteel");
		return cro;
	}
	
	/**
	 * 根据接箍合同号查询接箍合同信息
	 * @param str 接箍合同号
	 * @return
	 */
	public CoreReturnObject queryJgOrder(String str){
		CoreReturnObject cro = new CoreReturnObject();
		cro = this.getDataBySqlID("CoreJgOperatPln.queryJgOrder", new Object[]{ str });
		return cro;
	}
	
	/**
	 * 查询接箍及备料关系
	 * @param str 接箍料需求编号
	 * @return
	 */
	public CoreReturnObject queryrelation(String str){
		CoreReturnObject cro = new CoreReturnObject();
		cro = this.getDataBySqlID("CoreJgOperatPln.queryrelation", new Object[]{ str });
		return cro;
	}
	
	/**
	 * 查询接箍切断计划
	 * @param list
	 * @return
	 */
	public CoreReturnObject queryJgCut(ArrayList<Object>  list){
		CoreReturnObject cro = new CoreReturnObject();
		cro = this.getDataBySqlID("CoreJgOperatPln.queryJgCut", list.toArray());
		return cro;
	}
	
	/**
	 * 查询接箍加工作业计划
	 * @param list
	 * @return
	 */
	public CoreReturnObject queryJgProcess(String str){
		CoreReturnObject cro = new CoreReturnObject();
		cro = this.getDataBySqlID("CoreJgOperatPln.queryJgProcess", new Object[]{ str });
		return cro;
	}
	
	/**
	 * 通过作业批次号查询接箍切断计划
	 * @param str 接箍作业计划批次号
	 * @return
	 */
	public CoreReturnObject queryJgCutByid(String str){
		CoreReturnObject cro = new CoreReturnObject();
		cro = this.getDataBySqlID("CoreJgOperatPln.queryJgCutByid", new Object[]{ str });
		return cro;
	}
	
	/**
	 * 新增一个接箍作业计划
	 * @param list
	 * @return
	 */
	public CoreReturnObject addCutbatch(ArrayList<Object>  list){
		CoreReturnObject cro = new CoreReturnObject();
		Connection conn = null;
		PreparedStatement psta = null;
		String Cutbatch=list.get(0).toString();	//"JG-年月日"
		try{
			String sql = CoreXmlSqlParsersFactory.getSql("CoreJgOperatPln.querySerialNum");//执行的sql语句
			ResultSet rs = null;		//执行sql语句的结果
			conn = this.getDao(CoreConstantData.sysbeanstr).getConnection();
			conn.setAutoCommit(false);
			
			psta = conn.prepareStatement(sql);
			psta.setString(1, Cutbatch);
			rs = psta.executeQuery();
			while(rs.next()){
				CoreReturnObject isInsert = null;
				//查询后三位流水号
				if(rs.getString("MAXID")!=null){
					String Serialnum = CorePlnComm.padding(rs.getString("MAXID"),'0',3);
					Cutbatch = Cutbatch + Serialnum;
				}
				else{
					Cutbatch = Cutbatch + "001";
				}
				
				//新增作业计划，并返回切断作业批次号
				list.set(0, Cutbatch);
				isInsert = this.setDataBySqlID("CoreJgOperatPln.addCutbatch", list.toArray());
				if(isInsert != null && String.valueOf(isInsert.getResult()).equals("1")){
					cro = this.getDataBySqlID(
							"CoreJgOperatPln.queryJgCutByid", new Object[]{ Cutbatch });
				}
				break;
			}
			rs.close();
			psta.close();
			conn.commit();
		}
		catch(SQLException sqle){
			try {
				conn.rollback();
			} catch (SQLException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
		}
		finally{
			try {
				if(conn != null && !conn.isClosed()){
					conn.close();
				}
			} catch (SQLException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
		}
		return cro;
	}
	
	/**
	 * 更新接箍作业计划信息
	 * @param arguments 作业计划实体类
	 * @param arguJgPro 接箍加工作业实体类
	 * @param listBl 接箍料需求接箍切断计划量
	 * @param listRel 接箍料明细接箍计划已排个数
	 * @return
	 */
	public CoreReturnObject updateJgCutplan(PlnZyJgCutEntity arguments,ArrayList<PlnZyJgProEntity> listJgPro,
			ArrayList<Object> listBl,ArrayList<ArrayList<Object>> listRel,ArrayList<ArrayList<Object>> listMat){
		CoreReturnObject cro = new CoreReturnObject();
		CoreReturnObject croDel = new CoreReturnObject();
		ArrayList<String> sqllist = new ArrayList<String>();
        ArrayList<Object[]> parmlist = new ArrayList<Object[]>();
        
        //作业计划信息
        Object[] argument = new Object[]{ 
        		arguments.getPlineCode(),
        		arguments.getPlineName(),
        		arguments.getReqBlid(),
        		arguments.getJudgeStoveno(),
        		arguments.getBatchNo(),
        		arguments.getWlIdin(),
        		arguments.getCodeJgbl(),
        		arguments.getSteelCode(),
        		arguments.getSteelName(),
        		arguments.getOutdiAmeterBl(),
        		arguments.getWallthickBl(),
        		arguments.getJgblLength(),
        		arguments.getJgLength(),
        		arguments.getCutNumeach(),
        		arguments.getJgblWeight(),
        		arguments.getJgblNum(),
        		arguments.getCutNumtot(),
        		arguments.getGroupCut(),
        		arguments.getPlanTime(),
        		arguments.getPlanUsetime(),
        		arguments.getUpdateName(),
        		arguments.getUpdateTime(),
        		arguments.getCutBatchId()
        		};
        
        sqllist.add("CoreJgOperatPln.updateJgCutplan");
    	parmlist.add(argument);
        
    	//更新接箍料需求接箍切断计划量
        sqllist.add("CoreJgOperatPln.updateJglPlanWt");
    	parmlist.add(listBl.toArray());
        
    	//更新接箍料明细接箍计划已排个数
        for(int i = 0; i < listRel.size(); i++){
        	Object[] argu = new Object[]{
        			listJgPro.get(i).getCoupcutBatchId(),//切断作业批次号
        			listJgPro.get(i).getJgOrderno(),//接箍合同号
        			listJgPro.get(i).getPlineCode(),//接箍产线代码
                    listJgPro.get(i).getPlineName(),//接箍产线名称
                    listJgPro.get(i).getReqBlid(),//接箍料需求号
                    listJgPro.get(i).getHeatNo(),//坯料来源炉号
                    listJgPro.get(i).getBatchNo(),//坯料来源性能批号
                    listJgPro.get(i).getCodeJg(),//接箍码
                    listJgPro.get(i).getOutDiameter(),//外径
                    listJgPro.get(i).getWallThick(),//壁厚
                    listJgPro.get(i).getAimLength(),//接箍长度
                    listJgPro.get(i).getModelCode(),//扣型代码
                    listJgPro.get(i).getPrdcrNo(),//监制编号
                    listJgPro.get(i).getIfMonitorJg(),
                    listJgPro.get(i).getPlanProcNum(),//计划加工个数
                    listJgPro.get(i).getPlanStatus(),//计划状态
                    listJgPro.get(i).getGroupJgx(),
                    listJgPro.get(i).getPlanTime(),
                    listJgPro.get(i).getPlanUsetime(),
                    listJgPro.get(i).getCreateName(),
                    listJgPro.get(i).getCreateTime(),
                    listJgPro.get(i).getUpdateName(),
                    listJgPro.get(i).getUpdateTime()
        	};
        	
        	sqllist.add("CoreJgOperatPln.addPlnZyJgPro");
        	parmlist.add(argu);
        	
        	sqllist.add("CoreJgOperatPln.updateJglRelPlanNum");
        	parmlist.add(listRel.get(i).toArray());
        }
        
        for(int i=0; i<listMat.size(); i++){
        	sqllist.add("CoreJgOperatPln.updateIfbatchedjg");
        	parmlist.add(listMat.get(i).toArray());
        }
        
        //删除原接箍加工作业计划
        croDel = this.setDataBySqlID("CoreJgOperatPln.deletePlnJgZyPro",
        		new Object[]{ arguments.getCutBatchId() });
        if(String.valueOf(croDel.getResult()).equals("1")){
        	cro = this.setDataById(sqllist, parmlist);
        }
		return cro;
	}
	
	/**
	 * 下发计划
	 * @param list 作业计划批次号
	 * @return
	 */
	public CoreReturnObject issuedJgCutPln(ArrayList<Object> list){
		CoreReturnObject cro = new CoreReturnObject();
		ArrayList<String> sqllist = new ArrayList<String>();
        ArrayList<Object[]> parmlist = new ArrayList<Object[]>();
        
        for(int i=0; i<list.size(); i++){
        	//下发接箍切断作业计划
        	sqllist.add("CoreJgOperatPln.issuedJgCutPln");
        	parmlist.add(new Object[]{ list.get(i) });
        	
        	//下发接箍加工作业计划
        	sqllist.add("CoreJgOperatPln.issuedPlnJgZyPro");
        	parmlist.add(new Object[]{ list.get(i) });
        }
        cro = this.setDataById(sqllist, parmlist);
		return cro;
	}
	
	/**
	 * 取消下发计划
	 * @param list 作业计划批次号
	 * @return
	 */
	public CoreReturnObject CancelissuedPln(ArrayList<Object> list){
		CoreReturnObject cro = new CoreReturnObject();
		ArrayList<String> sqllist = new ArrayList<String>();
        ArrayList<Object[]> parmlist = new ArrayList<Object[]>();
        
        for(int i=0; i<list.size(); i++){
        	//取消下发接箍切断作业计划
        	sqllist.add("CoreJgOperatPln.CancelissuedPln");
        	parmlist.add(new Object[]{ list.get(i) });

        	//取消下发接箍加工作业计划
        	sqllist.add("CoreJgOperatPln.CancelissuedPlnPro");
        	parmlist.add(new Object[]{ list.get(i) });
        }
        cro = this.setDataById(sqllist, parmlist);
		return cro;
	}
	
	/**
	 * 批量删除接箍切断作业计划
	 * @param str 作业计划批次号
	 * @return
	 */
	public CoreReturnObject deleteJgCutPln(ArrayList<Object> list){
		CoreReturnObject cro = new CoreReturnObject();
		ArrayList<String> sqllist = new ArrayList<String>();
        ArrayList<Object[]> parmlist = new ArrayList<Object[]>();
        Connection conn = null;
    	PreparedStatement psta = null;
    	try{
    		conn = this.getDao(CoreConstantData.sysbeanstr).getConnection();
			conn.setAutoCommit(false);
    		for(int i=0; i<list.size(); i++){
    			//更新接箍料明细接箍计划已排个数
    			String sql = CoreXmlSqlParsersFactory.getSql("CoreJgOperatPln.queryJgProcess");
    			psta = conn.prepareStatement(sql);
    			psta.setString(1, list.get(i).toString());
    			ResultSet rs = psta.executeQuery();
    			while(rs.next()){
    				if(rs.getString("REQ_BL_ID")!=null && 
    						rs.getString("JG_ORDERNO")!=null &&
    						rs.getString("PLAN_PROC_NUM")!=null){
    					String plan_num = "-" + rs.getString("PLAN_PROC_NUM");
    					String jg_orderno = rs.getString("JG_ORDERNO");
    					String req_bl_id = rs.getString("REQ_BL_ID");
    					sqllist.add("CoreJgOperatPln.updateJglRelPlanNum");
    					parmlist.add(new Object[]{ plan_num, jg_orderno, req_bl_id});
    				}
    			}
    			rs.close();
            	psta.close();
            	conn.commit();
    			
            	//更新接箍料需求接箍切断计划量
            	sql = CoreXmlSqlParsersFactory.getSql("CoreJgOperatPln.queryJgCutByid");
    			psta = conn.prepareStatement(sql);
    			psta.setString(1, list.get(i).toString());
    			rs = psta.executeQuery();
            	while(rs.next()){
            		if(rs.getString("REQ_BL_ID")!=null && 
    						rs.getString("JGBL_WEIGHT")!=null &&
    						rs.getString("JGBL_NUM")!=null){
            			String jgbl_weight = "-" + rs.getString("JGBL_WEIGHT");
    					String jgbl_num = "-" + rs.getString("JGBL_NUM");
    					String req_bl_id = rs.getString("REQ_BL_ID");
    					sqllist.add("CoreJgOperatPln.updateJglPlanWt");
    					parmlist.add(new Object[]{ jgbl_weight, jgbl_num, req_bl_id});
            		}
            	}
            	rs.close();
            	psta.close();
            	conn.commit();
    			
            	//删除接箍切断计划时，更新钢管生产物料主表已做接箍组批
    			sqllist.add("CoreJgOperatPln.updateDeleIfBatchedJg");
    			parmlist.add(new Object[]{ "", "0", "", list.get(i) });
    			
    			//删除接箍切断作业计划
    			sqllist.add("CoreJgOperatPln.deleteCutbatch");
    			parmlist.add(new Object[]{ list.get(i) });

    			//删除接箍加工作业计划
    			sqllist.add("CoreJgOperatPln.deletePlnJgZyPro");
    			parmlist.add(new Object[]{ list.get(i) });
    		}
    	}
    	catch(SQLException sqle){
    		try {
    			conn.rollback();
    		} catch (SQLException e) {
    			// TODO Auto-generated catch block
    			e.printStackTrace();
    		}
    	}
    	finally{
    		try {
    			if(conn != null && !conn.isClosed()){
    				conn.close();
    			}
    		} catch (SQLException e) {
    			// TODO Auto-generated catch block
    			e.printStackTrace();
    		}
    	}
        cro = this.setDataById(sqllist, parmlist);
		return cro;
	}
	
	/**
	 * 查询钢管生产物料主表
	 * @param list
	 * @return
	 */
	public CoreReturnObject queryMatzcm(ArrayList<Object> list){
		CoreReturnObject cro = new CoreReturnObject();
		cro = this.getDataBySqlID("CoreJgOperatPln.queryMatzcm", list.toArray());
		return cro;
	}
	
}