package com.steering.pss.plan.order;

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;

/**
 * 形成工序合同
 * @author 杨天海
 * @version 1.1
 */
public class CorePrcOrder extends DBCommon{
	private ArrayList<String> sqllist = new ArrayList<String>();
	private ArrayList<Object[]> parmlist = new ArrayList<Object[]>();
	/**
     * 下发形成工序合同
     * @param list 工序信息
     */
    public void creatProcess(ArrayList<String> list){
    	//CoreReturnObject cro = new CoreReturnObject();
    	String Msc_pline = list.get(16);
    	String Desgin_key = list.get(17);
    	double finallyweight = Double.parseDouble(list.get(13));
    	
    	sqllist.clear();
    	parmlist.clear();
    	Connection conn = null;
    	PreparedStatement psta = null;
    	try{
    		ArrayList<Integer> log_seq = new ArrayList<Integer>();
    		ArrayList<String> Process = new ArrayList<String>();
    		int process_d = 0;			//扎管次数
    		int process_e = 0;			//墩拔阔次数
    		int process_f = 0;			//热处理次数
    		int process_g = 0;			//加工次数
    		conn = this.getDao(CoreConstantData.sysbeanstr).getConnection();
    		conn.setAutoCommit(false);
    		String sql = CoreXmlSqlParsersFactory.getSql("CoreSaleOrderOP.queryMscplPrc");
    		psta = conn.prepareStatement(sql);
    		psta.setString(1, Desgin_key);
    		psta.setString(2, Msc_pline);
    		ResultSet rs = psta.executeQuery();
    		///读取经过的工序
    		while(rs.next()){
    			if(rs.getString("PROCESS_CODE").equals("D")){
    				log_seq.add(Integer.parseInt(rs.getString("BACKLOG_SEQ")));
    				Process.add("D");
    				process_d++;
    			}
    			if(rs.getString("PROCESS_CODE").equals("E")){
    				log_seq.add(Integer.parseInt(rs.getString("BACKLOG_SEQ")));
    				Process.add("E");
    				process_e++;
    			}
    			if(rs.getString("PROCESS_CODE").equals("F")){
    				log_seq.add(Integer.parseInt(rs.getString("BACKLOG_SEQ")));
    				Process.add("F");
    				process_f++;
    			}
    			if(rs.getString("PROCESS_CODE").equals("G")){
    				log_seq.add(Integer.parseInt(rs.getString("BACKLOG_SEQ")));
    				Process.add("G");
    				process_g++;
    			}
    		}
    		rs.close();
    		psta.close();
    		conn.commit();
    		for(int i = log_seq.size(); i>0; i--){
    			int process_seq = 0;	//同工序流水号
    			if(Process.get(i-1).equals("D")){
    				ArrayList<String> listDel = new ArrayList<String>();
    				listDel.add(list.get(1));
    				listDel.add(list.get(2));
    				listDel.add(list.get(3));
    				CoreReturnObject croDel = this.setDataBySqlID("CorePrcOrder.DelSaleProZg", listDel.toArray());
    				if(String.valueOf(croDel.getResult()).equals("1")){
    					process_seq = process_d--;
    					if(i==log_seq.size()){
    						//如果是末工序
    						finallyweight = orderZg(list,process_seq,finallyweight,"0","");
    					}
    					else{
    						finallyweight = orderZg(list,process_seq,finallyweight,"1",Process.get(i));
    					}
    				}
    			}
    			if(Process.get(i-1).equals("E")){
    				ArrayList<String> listDel = new ArrayList<String>();
    				listDel.add(list.get(1));
    				listDel.add(list.get(2));
    				listDel.add(list.get(3));
    				CoreReturnObject croDel = this.setDataBySqlID("CorePrcOrder.DelSaleProBj", listDel.toArray());
    				if(String.valueOf(croDel.getResult()).equals("1")){
    					process_seq = process_e--;
    					if(i==log_seq.size()){
    						//如果是末工序
    						finallyweight = orderBj(list,process_seq,finallyweight,"1","0",Process.get(i-2),"");
    					}
    					else if(i==1){
    						//如果是首工序
    						finallyweight = orderBj(list,process_seq,finallyweight,"0","1","",Process.get(i));
    					}
    					else{
    						finallyweight = orderBj(list,process_seq,finallyweight,"1","1",Process.get(i-2),Process.get(i));
    					}
    				}
    			}
    			if(Process.get(i-1).equals("F")){
    				ArrayList<String> listDel = new ArrayList<String>();
    				listDel.add(list.get(1));
    				listDel.add(list.get(2));
    				listDel.add(list.get(3));
    				CoreReturnObject croDel = this.setDataBySqlID("CorePrcOrder.DelSaleProRcl", listDel.toArray());
    				if(String.valueOf(croDel.getResult()).equals("1")){
    					process_seq = process_f--;
    					if(i==log_seq.size()){
    						finallyweight = orderRcl(list,process_seq,finallyweight,"1","0",Process.get(i-2),"");
    					}
    					else if(i==1){
    						finallyweight = orderRcl(list,process_seq,finallyweight,"0","1","",Process.get(i));
    					}
    					else{
    						finallyweight = orderRcl(list,process_seq,finallyweight,"1","1",Process.get(i-2),Process.get(i));
    					}
    				}
    			}
    			if(Process.get(i-1).equals("G")){
    				ArrayList<String> listDel = new ArrayList<String>();
    				listDel.add(list.get(1));
    				listDel.add(list.get(2));
    				listDel.add(list.get(3));
    				CoreReturnObject croDel = this.setDataBySqlID("CorePrcOrder.DelSaleProJgx", listDel.toArray());
    				if(String.valueOf(croDel.getResult()).equals("1")){
    					process_seq = process_g--;
    					if(i==log_seq.size()){
    						finallyweight = orderJgx(list,process_seq,finallyweight,"1","0",Process.get(i-2),"");
    					}
    					else if(i==1){
    						finallyweight = orderJgx(list,process_seq,finallyweight,"0","1","",Process.get(i));
    					}
    					else{
    						finallyweight = orderJgx(list,process_seq,finallyweight,"1","1",Process.get(i-2),Process.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.getDao(CoreConstantData.sysbeanstr).ExcuteTransction(
        ///		sqllist, parmlist);
    	///return cro;
    }
    
    /**
     * 形成扎管合同
     * @param list 销售合同信息
     * @param process_seq 同工序流水号
     * @param weight 合同量
     * @param isendgx 是否末工序
     * @param next_gx 下个工序
     * @return
     */
    public Double orderZg(ArrayList<String> list,int process_seq, double weight,
    		String isendgx,String next_gx){
    	String pro_order_no = "";
    	String Msc_pline = list.get(16); //制程
    	String Desgin_key = list.get(17);//desgin_key
    	String Material_no = list.get(18);//物料编码
    	String inwl_gp = "";		//投入管坯编码
    	String outwl_id = "";		//工序产出物料编码
    	String getrate = "1";		//成材率
    	String if_monitor_zg = "0";	//是否轧管监制
    	String prdcr_no = list.get(19);	//监制编号

    	ArrayList<String> listOrder = new ArrayList<String>();
    	Connection conn = null;
    	PreparedStatement psta = null;
    	try{
    		///查询轧管合同号
    		conn = this.getDao(CoreConstantData.sysbeanstr).getConnection();
    		conn.setAutoCommit(false);
    		String sql = "SELECT * FROM PLN_ORDER_ZG T ORDER BY TO_NUMBER(SUBSTR(T.PRO_ORDER_NO,4)) ASC";
    		psta = conn.prepareStatement(sql,ResultSet.TYPE_SCROLL_SENSITIVE,ResultSet.CONCUR_UPDATABLE);
    		ResultSet rs = psta.executeQuery();
    		rs.last();
    		if(rs.getRow()==0){
    			pro_order_no = "ZG_1";
    		}
    		else{
    			pro_order_no = "ZG_" + String.valueOf(Integer.valueOf(
    					rs.getString("PRO_ORDER_NO").substring(3)) + 1);
    		}
    		rs.close();
    		psta.close();
    		conn.commit();
    		///查询投入产出物料
    		sql = CoreXmlSqlParsersFactory.getSql("CoreSaleOrderOP.queryLine_bom");
    		psta = conn.prepareStatement(sql,ResultSet.TYPE_SCROLL_SENSITIVE,ResultSet.CONCUR_UPDATABLE);
    		psta.setString(1, Desgin_key);
    		rs = psta.executeQuery();
    		while(rs.next()){
    			PreparedStatement pstab = null;
    			String sqlb =  CoreXmlSqlParsersFactory.getSql("CoreSaleOrderOP.queryBomlist");
    			String strbom = "";				//由desgin_key查出对应的bom编号
    			pstab = conn.prepareStatement(sqlb,ResultSet.TYPE_SCROLL_SENSITIVE,ResultSet.CONCUR_UPDATABLE);
    			if(rs.getString("BOM")!=null){
    				strbom = rs.getString("BOM");
    			}
    			pstab.setString(1, strbom);
    			pstab.setString(2, Msc_pline);
    			pstab.setString(3, Material_no);
    			ResultSet result = pstab.executeQuery();
    			while(result.next()){
    				if(result.getString("D_OUT_MATERIAL_NO")!=null){
    					outwl_id = result.getString("D_OUT_MATERIAL_NO");
    				}
    				if(result.getString("D_IN_MATERIAL_NO")!=null){
    					inwl_gp = result.getString("D_IN_MATERIAL_NO");
    				}
    				
    				///查询成材率
    				PreparedStatement pstac = null;
    				String sqlc = CoreXmlSqlParsersFactory.getSql("CorePrcOrder.queryZgGetrate");
    				pstac = conn.prepareStatement(sqlc,ResultSet.TYPE_SCROLL_SENSITIVE,ResultSet.CONCUR_UPDATABLE);
    				pstac.setString(1, inwl_gp);
    				pstac.setString(2, outwl_id);
    				ResultSet result1 = pstac.executeQuery();
    				result1.last();
    				if(result1.getRow()!=0){
    					if(getrate.equals("1") || getrate.compareTo(result1.getString("PRD_RATE")) > 0){
    						if(result1.getString("PRD_RATE")!=null){
    							getrate = result1.getString("PRD_RATE");
    						}
    					}
    				}
    				result1.close();
    				pstac.close();
    				conn.commit();
    			}
    			result.close();
				pstab.close();
				conn.commit();
    		}
    		rs.close();
    		psta.close();
    		conn.commit();
    		///查询监制明细
    		if(!prdcr_no.equals("")){
    			sql = CoreXmlSqlParsersFactory.getSql("CoreSaleOrderOP.queryOrderPrdcr");
    			psta = conn.prepareStatement(sql,ResultSet.TYPE_SCROLL_SENSITIVE,ResultSet.CONCUR_UPDATABLE);
    			psta.setString(1, prdcr_no);
    			rs = psta.executeQuery();
    			while(rs.next()){
    				if(rs.getString("PROCESS_CDOE").equals("D")){
    					if_monitor_zg = "1";
    				}
    			}
    		}
    		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();
    		}
    	}
    	// #start 扎管合同
    	listOrder.add(pro_order_no);	//扎管合同号
    	listOrder.add(list.get(0));		//合同来源（销售合同、备料需求）
    	listOrder.add(list.get(1));		//合同号
    	listOrder.add(list.get(2));		//合同行号
    	listOrder.add(list.get(3));		//交货编码
    	listOrder.add(list.get(4));		//备料需求号
    	listOrder.add(String.valueOf(process_seq));	//同工序流水号
    	listOrder.add(list.get(5));		//钢级(牌号)代码
    	listOrder.add(list.get(6));		//钢级(牌号)名称
    	listOrder.add(list.get(7));		//外径
    	listOrder.add(list.get(8));		//壁厚
    	listOrder.add(list.get(9));		//控制壁厚
    	listOrder.add(list.get(10));	//最大长度
    	listOrder.add(list.get(11));	//最小长度
    	listOrder.add(list.get(12));	//控制长度
    	listOrder.add(outwl_id);		//产出物料编码
    	listOrder.add(inwl_gp);			//投入物料编码
    	listOrder.add(getrate);			//成材率
    	listOrder.add(String.valueOf(weight));	//合同重量
    	listOrder.add(list.get(14));	//合同支数
    	listOrder.add(list.get(15));	//最晚完成日期
    	listOrder.add(list.get(16));	//制程索引号
    	listOrder.add(isendgx);		//	是否末工序
    	listOrder.add(next_gx);			//后一工序
    	listOrder.add(if_monitor_zg);	//是否轧管监制
    	listOrder.add(prdcr_no);		//监制编号
    	listOrder.add(list.get(20));	//创建人
    	listOrder.add(list.get(21));	//创建时间
    	// #end
    	sqllist.add(CoreXmlSqlParsersFactory.getSql("CorePrcOrder.insertZgOrder"));
        parmlist.add(listOrder.toArray());
    	weight = weight/(Double.parseDouble(getrate));
    	return weight;
    }
    
    /**
     * 形成变径合同
     * @param list 销售合同信息
     * @param process_seq 同工序流水号
     * @param weight 合同量
     * @param isbegingx 是否首工序
     * @param isendgx 是否末工序
     * @param previous_gx 前一工序
     * @param next_gx 下个工序
     * @return
     */
    public Double orderBj(ArrayList<String> list,int process_seq, double weight,
    		String isbegingx,String isendgx,String previous_gx,String next_gx){
    	String pro_order_no = "";
    	String Msc_pline = list.get(16); //制程
    	String Desgin_key = list.get(17);//desgin_key
    	String Material_no = list.get(18);//物料编码
    	String inwl_gp = "";		//投入管坯编码
    	String outwl_id = "";		//工序产出物料编码
    	String getrate = "1";		//成材率
    	String if_monitor_bj = "0";	//是否变径工序监制
    	String prdcr_no = list.get(19);	//监制编号

    	ArrayList<String> listOrder = new ArrayList<String>();
    	
    	Connection conn = null;
    	PreparedStatement psta = null;
    	try{
    		///查询变径合同号
    		conn = this.getDao(CoreConstantData.sysbeanstr).getConnection();
    		conn.setAutoCommit(false);
    		String sql = "SELECT * FROM PLN_ORDER_BJ T ORDER BY TO_NUMBER(SUBSTR(T.PRO_ORDER_NO,4)) ASC";
    		psta = conn.prepareStatement(sql,ResultSet.TYPE_SCROLL_SENSITIVE,ResultSet.CONCUR_UPDATABLE);
    		ResultSet rs = psta.executeQuery();
    		rs.last();
    		if(rs.getRow()==0){
    			pro_order_no = "BJ_1";
    		}
    		else{
    			pro_order_no = "BJ_" + String.valueOf(Integer.valueOf(
    					rs.getString("PRO_ORDER_NO").substring(3)) + 1);
    		}
    		rs.close();
    		psta.close();
    		conn.commit();
    		///查询投入产出物料
    		sql = CoreXmlSqlParsersFactory.getSql("CoreSaleOrderOP.queryLine_bom");
    		psta = conn.prepareStatement(sql,ResultSet.TYPE_SCROLL_SENSITIVE,ResultSet.CONCUR_UPDATABLE);
    		psta.setString(1, Desgin_key);
    		rs = psta.executeQuery();
    		while(rs.next()){
    			PreparedStatement pstab = null;
    			String sqlb =  CoreXmlSqlParsersFactory.getSql("CoreSaleOrderOP.queryBomlist");
    			String strbom = "";				//由desgin_key查出对应的bom编号
    			pstab = conn.prepareStatement(sqlb,ResultSet.TYPE_SCROLL_SENSITIVE,ResultSet.CONCUR_UPDATABLE);
    			if(rs.getString("BOM")!=null){
    				strbom = rs.getString("BOM");
    			}
    			pstab.setString(1, strbom);
    			pstab.setString(2, Msc_pline);
    			pstab.setString(3, Material_no);
    			ResultSet result = pstab.executeQuery();
    			while(result.next()){
    				if(result.getString("E_OUT_MATERIAL_NO")!=null){
    					outwl_id = result.getString("E_OUT_MATERIAL_NO");
    				}
    				if(result.getString("E_IN_MATERIAL_NO")!=null){
    					inwl_gp = result.getString("E_IN_MATERIAL_NO");
    				}
    				
    				///查询成材率
    				PreparedStatement pstac = null;
    				String sqlc = CoreXmlSqlParsersFactory.getSql("CorePrcOrder.queryBjGetrate");
    				pstac = conn.prepareStatement(sqlc,ResultSet.TYPE_SCROLL_SENSITIVE,ResultSet.CONCUR_UPDATABLE);
    				pstac.setString(1, inwl_gp);
    				pstac.setString(2, outwl_id);
    				ResultSet result1 = pstac.executeQuery();
    				result1.last();
    				if(result1.getRow()!=0){
    					if(getrate.equals("1") || getrate.compareTo(result1.getString("PRD_RATE")) > 0){
    						if(result1.getString("PRD_RATE")!=null){
    							getrate = result1.getString("PRD_RATE");
    						}
    					}
    				}
    				result1.close();
    				pstac.close();
    				conn.commit();
    			}
    			result.close();
				pstab.close();
				conn.commit();
    		}
    		rs.close();
    		psta.close();
    		conn.commit();
    		///查询监制明细
    		if(!prdcr_no.equals("")){
    			sql = CoreXmlSqlParsersFactory.getSql("CoreSaleOrderOP.queryOrderPrdcr");
    			psta = conn.prepareStatement(sql,ResultSet.TYPE_SCROLL_SENSITIVE,ResultSet.CONCUR_UPDATABLE);
    			psta.setString(1, prdcr_no);
    			rs = psta.executeQuery();
    			while(rs.next()){
    				if(rs.getString("PROCESS_CDOE").equals("E")){
    					if_monitor_bj = "1";
    				}
    			}
    		}
    		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();
    		}
    	}
    	// #start 变径合同
    	listOrder.add(pro_order_no);	//变径合同号
    	listOrder.add(list.get(0));		//合同来源（销售合同、备料需求）
    	listOrder.add(list.get(1));		//合同号
    	listOrder.add(list.get(2));		//合同行号
    	listOrder.add(list.get(3));		//交货编码
    	listOrder.add(list.get(4));		//备料需求号
    	listOrder.add(String.valueOf(process_seq));	//同工序流水号
    	listOrder.add(list.get(5));		//钢级(牌号)代码
    	listOrder.add(list.get(6));		//钢级(牌号)名称
    	listOrder.add(list.get(7));		//外径
    	listOrder.add(list.get(8));		//壁厚
    	listOrder.add(list.get(9));		//控制壁厚
    	listOrder.add(list.get(10));	//最大长度
    	listOrder.add(list.get(11));	//最小长度
    	listOrder.add(list.get(12));	//控制长度
    	listOrder.add(outwl_id);		//产出物料编码
    	listOrder.add(inwl_gp);			//投入物料编码
    	listOrder.add(getrate);			//成材率
    	listOrder.add(String.valueOf(weight));	//合同重量
    	listOrder.add(list.get(14));	//合同支数
    	listOrder.add(list.get(15));	//最晚完成日期
    	listOrder.add(list.get(16));	//制程索引号
    	listOrder.add(isbegingx);		//	是否首工序
    	listOrder.add(isendgx);		//	是否末工序
    	listOrder.add(previous_gx);		//前一工序
    	listOrder.add(next_gx);			//后一工序
    	listOrder.add(if_monitor_bj);	//是否变径工序监制
    	listOrder.add(prdcr_no);		//监制编号
    	listOrder.add(list.get(20));	//创建人
    	listOrder.add(list.get(21));	//创建时间
    	// #end
    	sqllist.add(CoreXmlSqlParsersFactory.getSql("CorePrcOrder.insertBjOrder"));
        parmlist.add(listOrder.toArray());
    	weight = weight/(Double.parseDouble(getrate));
    	return weight;
    }
    
    /**
     * 形成热处理合同
     * @param list 销售合同信息
     * @param process_seq 同工序流水号
     * @param weight 合同量
     * @param isbegingx 是否首工序
     * @param isendgx 是否末工序
     * @param previous_gx 前一工序
     * @param next_gx 下个工序
     * @return
     */
    public Double orderRcl(ArrayList<String> list,int process_seq, double weight,
    		String isbegingx,String isendgx,String previous_gx,String next_gx){
    	String pro_order_no = "";
    	String Msc_pline = list.get(16); //制程
    	String Desgin_key = list.get(17);//desgin_key
    	String Material_no = list.get(18);//物料编码
    	String inwl_gp = "";		//投入管坯编码
    	String outwl_id = "";		//工序产出物料编码
    	String getrate = "1";		//成材率
    	String if_monitor_rcl = "0";//是否热处理监制
    	String prdcr_no = list.get(19);	//监制编号

    	ArrayList<String> listOrder = new ArrayList<String>();
    	
    	Connection conn = null;
    	PreparedStatement psta = null;
    	try{
    		///查询变径合同号
    		conn = this.getDao(CoreConstantData.sysbeanstr).getConnection();
    		conn.setAutoCommit(false);
    		String sql = "SELECT * FROM PLN_ORDER_RCL T ORDER BY TO_NUMBER(SUBSTR(T.PRO_ORDER_NO,5)) ASC";
    		psta = conn.prepareStatement(sql,ResultSet.TYPE_SCROLL_SENSITIVE,ResultSet.CONCUR_UPDATABLE);
    		ResultSet rs = psta.executeQuery();
    		rs.last();
    		if(rs.getRow() == 0){
    			pro_order_no = "RCL_1";
    		}
    		else{
    			pro_order_no = "RCL_" + String.valueOf(Integer.valueOf(
    					rs.getString("PRO_ORDER_NO").substring(4)) + 1);
    		}
    		rs.close();
    		psta.close();
    		conn.commit();
    		///查询投入产出物料
    		sql = CoreXmlSqlParsersFactory.getSql("CoreSaleOrderOP.queryLine_bom");
    		psta = conn.prepareStatement(sql,ResultSet.TYPE_SCROLL_SENSITIVE,ResultSet.CONCUR_UPDATABLE);
    		psta.setString(1, Desgin_key);
    		rs = psta.executeQuery();
    		while(rs.next()){
    			PreparedStatement pstab = null;
    			String sqlb =  CoreXmlSqlParsersFactory.getSql("CoreSaleOrderOP.queryBomlist");
    			String strbom = "";				//由desgin_key查出对应的bom编号
    			pstab = conn.prepareStatement(sqlb,ResultSet.TYPE_SCROLL_SENSITIVE,ResultSet.CONCUR_UPDATABLE);
    			if(rs.getString("BOM")!=null){
    				strbom = rs.getString("BOM");
    			}
    			pstab.setString(1, strbom);
    			pstab.setString(2, Msc_pline);
    			pstab.setString(3, Material_no);
    			ResultSet result = pstab.executeQuery();
    			while(result.next()){
    				if(result.getString("F_OUT_MATERIAL_NO")!=null){
    					outwl_id = result.getString("F_OUT_MATERIAL_NO");
    				}
    				if(result.getString("F_IN_MATERIAL_NO")!=null){
    					inwl_gp = result.getString("F_IN_MATERIAL_NO");
    				}
    				
    				///查询成材率
    				PreparedStatement pstac = null;
    				String sqlc = CoreXmlSqlParsersFactory.getSql("CorePrcOrder.queryRclGetrate");
    				pstac = conn.prepareStatement(sqlc,ResultSet.TYPE_SCROLL_SENSITIVE,ResultSet.CONCUR_UPDATABLE);
    				pstac.setString(1, inwl_gp);
    				pstac.setString(2, outwl_id);
    				ResultSet result1 = pstac.executeQuery();
    				result1.last();
    				if(result1.getRow()!=0){
    					if(getrate.equals("1") || getrate.compareTo(result1.getString("PRD_RATE")) > 0){
    						if(result1.getString("PRD_RATE")!=null){
    							getrate = result1.getString("PRD_RATE");
    						}
    					}
    				}
    				result1.close();
    				pstac.close();
    				conn.commit();
    			}
    			result.close();
				pstab.close();
				conn.commit();
    		}
    		rs.close();
    		psta.close();
    		conn.commit();
    		///查询监制明细
    		if(!prdcr_no.equals("")){
    			sql = CoreXmlSqlParsersFactory.getSql("CoreSaleOrderOP.queryOrderPrdcr");
    			psta = conn.prepareStatement(sql,ResultSet.TYPE_SCROLL_SENSITIVE,ResultSet.CONCUR_UPDATABLE);
    			psta.setString(1, prdcr_no);
    			rs = psta.executeQuery();
    			while(rs.next()){
    				if(rs.getString("PROCESS_CDOE").equals("F")){
    					if_monitor_rcl = "1";
    				}
    			}
    		}
    		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();
    		}
    	}
    	// #start 变径合同
    	listOrder.add(pro_order_no);	//变径合同号
    	listOrder.add(list.get(0));		//合同来源（销售合同、备料需求）
    	listOrder.add(list.get(1));		//合同号
    	listOrder.add(list.get(2));		//合同行号
    	listOrder.add(list.get(3));		//交货编码
    	listOrder.add(list.get(4));		//备料需求号
    	listOrder.add(String.valueOf(process_seq));	//同工序流水号
    	listOrder.add(list.get(5));		//钢级(牌号)代码
    	listOrder.add(list.get(6));		//钢级(牌号)名称
    	listOrder.add(list.get(7));		//外径
    	listOrder.add(list.get(8));		//壁厚
    	listOrder.add(list.get(9));		//控制壁厚
    	listOrder.add(list.get(10));	//最大长度
    	listOrder.add(list.get(11));	//最小长度
    	listOrder.add(list.get(12));	//控制长度
    	listOrder.add(outwl_id);		//产出物料编码
    	listOrder.add(inwl_gp);			//投入物料编码
    	listOrder.add(getrate);			//成材率
    	listOrder.add(String.valueOf(weight));	//合同重量
    	listOrder.add(list.get(14));	//合同支数
    	listOrder.add(list.get(15));	//最晚完成日期
    	listOrder.add(list.get(16));	//制程索引号
    	listOrder.add(isbegingx);		//	是否首工序
    	listOrder.add(isendgx);		//	是否末工序
    	listOrder.add(previous_gx);		//前一工序
    	listOrder.add(next_gx);			//后一工序
    	listOrder.add(if_monitor_rcl);	//是否热处理监制
    	listOrder.add(prdcr_no);		//监制编号
    	listOrder.add(list.get(20));	//创建人
    	listOrder.add(list.get(21));	//创建时间
    	// #end
    	sqllist.add(CoreXmlSqlParsersFactory.getSql("CorePrcOrder.insertRclOrder"));
        parmlist.add(listOrder.toArray());
    	weight = weight/(Double.parseDouble(getrate));
    	return weight;
    }
    
    /**
     * 形成加工线工序合同
     * @param list 销售合同信息
     * @param process_seq 同工序流水号
     * @param weight 合同量
     * @param isbegingx 是否首工序
     * @param isendgx 是否末工序
     * @param previous_gx 前一工序
     * @param next_gx 下个工序
     * @return
     */
    public Double orderJgx(ArrayList<String> list,int process_seq, double weight,
    		String isbegingx,String isendgx,String previous_gx,String next_gx){
    	String pro_order_no = "";
    	String Msc_pline = list.get(16); //制程
    	String Desgin_key = list.get(17);//desgin_key
    	String Material_no = list.get(18);//物料编码
    	String inwl_gp = "";		//投入管坯编码
    	String outwl_id = "";		//工序产出物料编码
    	String getrate = "1";		//成材率
    	String if_monitor_jgx = "0";//是否加工线监制
    	String prdcr_no = list.get(19);	//监制编号

    	ArrayList<String> listOrder = new ArrayList<String>();
    	
    	Connection conn = null;
    	PreparedStatement psta = null;
    	try{
    		///查询变径合同号
    		conn = this.getDao(CoreConstantData.sysbeanstr).getConnection();
    		conn.setAutoCommit(false);
    		String sql = "SELECT * FROM PLN_ORDER_JGX T ORDER BY TO_NUMBER(SUBSTR(T.PRO_ORDER_NO,5)) ASC";
    		psta = conn.prepareStatement(sql,ResultSet.TYPE_SCROLL_SENSITIVE,ResultSet.CONCUR_UPDATABLE);
    		ResultSet rs = psta.executeQuery();
    		rs.last();
    		if(rs.getRow()==0){
    			pro_order_no = "JGX_1";
    		}
    		else{
    			pro_order_no = "JGX_" + String.valueOf(Integer.valueOf(
    					rs.getString("PRO_ORDER_NO").substring(4)) + 1);
    		}
    		rs.close();
    		psta.close();
    		conn.commit();
    		///查询投入产出物料
    		sql = CoreXmlSqlParsersFactory.getSql("CoreSaleOrderOP.queryLine_bom");
    		psta = conn.prepareStatement(sql,ResultSet.TYPE_SCROLL_SENSITIVE,ResultSet.CONCUR_UPDATABLE);
    		psta.setString(1, Desgin_key);
    		rs = psta.executeQuery();
    		while(rs.next()){
    			PreparedStatement pstab = null;
    			String sqlb =  CoreXmlSqlParsersFactory.getSql("CoreSaleOrderOP.queryBomlist");
    			String strbom = "";				//由desgin_key查出对应的bom编号
    			pstab = conn.prepareStatement(sqlb,ResultSet.TYPE_SCROLL_SENSITIVE,ResultSet.CONCUR_UPDATABLE);
    			if(rs.getString("BOM")!=null){
    				strbom = rs.getString("BOM");
    			}
    			pstab.setString(1, strbom);
    			pstab.setString(2, Msc_pline);
    			pstab.setString(3, Material_no);
    			ResultSet result = pstab.executeQuery();
    			while(result.next()){
    				if(result.getString("I_OUT_MATERIAL_NO")!=null){
    					outwl_id = result.getString("I_OUT_MATERIAL_NO");
    				}
    				if(result.getString("I_IN_MATERIAL_NO")!=null){
    					inwl_gp = result.getString("I_IN_MATERIAL_NO");
    				}
    				
    				///查询成材率
    				PreparedStatement pstac = null;
    				String sqlc = CoreXmlSqlParsersFactory.getSql("CorePrcOrder.queryJgxGetrate");
    				pstac = conn.prepareStatement(sqlc,ResultSet.TYPE_SCROLL_SENSITIVE,ResultSet.CONCUR_UPDATABLE);
    				pstac.setString(1, inwl_gp);
    				pstac.setString(2, outwl_id);
    				ResultSet result1 = pstac.executeQuery();
    				result1.last();
    				if(result.getRow()!=0){
    					if(getrate.equals("1") || getrate.compareTo(result1.getString("PRD_RATE")) > 0){
    						if(result1.getString("PRD_RATE")!=null){
    							getrate = result1.getString("PRD_RATE");
    						}
    					}
    				}
    				result1.close();
    				pstac.close();
    				conn.commit();
    			}
    			result.close();
				pstab.close();
				conn.commit();
    		}
    		rs.close();
    		psta.close();
    		conn.commit();
    		///查询监制明细
    		if(!prdcr_no.equals("")){
    			sql = CoreXmlSqlParsersFactory.getSql("CoreSaleOrderOP.queryOrderPrdcr");
    			psta = conn.prepareStatement(sql,ResultSet.TYPE_SCROLL_SENSITIVE,ResultSet.CONCUR_UPDATABLE);
    			psta.setString(1, prdcr_no);
    			rs = psta.executeQuery();
    			while(rs.next()){
    				if(rs.getString("PROCESS_CDOE").equals("G")){
    					if_monitor_jgx = "1";
    				}
    			}
    		}
    		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();
    		}
    	}
    	// #start 变径合同
    	listOrder.add(pro_order_no);	//变径合同号
    	listOrder.add(list.get(0));		//合同来源（销售合同、备料需求）
    	listOrder.add(list.get(1));		//合同号
    	listOrder.add(list.get(2));		//合同行号
    	listOrder.add(list.get(3));		//交货编码
    	listOrder.add(list.get(4));		//备料需求号
    	listOrder.add(String.valueOf(process_seq));	//同工序流水号
    	listOrder.add(list.get(5));		//钢级(牌号)代码
    	listOrder.add(list.get(6));		//钢级(牌号)名称
    	listOrder.add(list.get(7));		//外径
    	listOrder.add(list.get(8));		//壁厚
    	listOrder.add(list.get(9));		//控制壁厚
    	listOrder.add(list.get(10));	//最大长度
    	listOrder.add(list.get(11));	//最小长度
    	listOrder.add(list.get(12));	//控制长度
    	listOrder.add(outwl_id);		//产出物料编码
    	listOrder.add(inwl_gp);			//投入物料编码
    	listOrder.add(getrate);			//成材率
    	listOrder.add(String.valueOf(weight));	//合同重量
    	listOrder.add(list.get(14));	//合同支数
    	listOrder.add(list.get(15));	//最晚完成日期
    	listOrder.add(list.get(16));	//制程索引号
    	listOrder.add(isbegingx);		//	是否首工序
    	listOrder.add(isendgx);		//	是否末工序
    	listOrder.add(previous_gx);		//前一工序
    	listOrder.add(next_gx);			//后一工序
    	listOrder.add(if_monitor_jgx);	//是否加工线监制
    	listOrder.add(prdcr_no);		//监制编号
    	listOrder.add(list.get(20));	//创建人
    	listOrder.add(list.get(21));	//创建时间
    	// #end
    	sqllist.add(CoreXmlSqlParsersFactory.getSql("CorePrcOrder.insertJgxOrder"));
        parmlist.add(listOrder.toArray());
    	weight = weight/(Double.parseDouble(getrate));
    	return weight;
    }
    
    /**
     * 备料下发形成工序合同
     * @param list 工序信息
     */
    public void creatBlPro(ArrayList<String> list){
    	///CoreReturnObject cro = new CoreReturnObject();
    	String Msc_pline = list.get(16);
    	String strMsc = list.get(22);
    	double finallyweight = Double.parseDouble(list.get(13));
    	
    	sqllist.clear();
    	parmlist.clear();
    	Connection conn = null;
    	PreparedStatement psta = null;
    	try{
    		ArrayList<Integer> log_seq = new ArrayList<Integer>();
    		ArrayList<String> Process=new ArrayList<String>();
    		int process_d = 0;			//扎管次数
    		int process_e = 0;			//墩拔阔次数
    		int process_f = 0;			//热处理次数
    		int process_g = 0;			//加工次数
    		conn = this.getDao(CoreConstantData.sysbeanstr).getConnection();
    		conn.setAutoCommit(false);
    		String sql = CoreXmlSqlParsersFactory.getSql("CoreSaleOrderOP.queryblprocess");
    		psta = conn.prepareStatement(sql);
    		psta.setString(1, strMsc);
    		psta.setString(2, Msc_pline);
    		ResultSet rs = psta.executeQuery();
    		///读取经过工序
    		while(rs.next()){
    			if(rs.getString("PROCESS_CODE").equals("D")){
    				log_seq.add(Integer.parseInt(rs.getString("BACKLOG_SEQ")));
    				Process.add("D");
    				process_d++;
    			}
    			if(rs.getString("PROCESS_CODE").equals("E")){
    				log_seq.add(Integer.parseInt(rs.getString("BACKLOG_SEQ")));
    				Process.add("E");
    				process_e++;
    			}
    			if(rs.getString("PROCESS_CODE").equals("F")){
    				log_seq.add(Integer.parseInt(rs.getString("BACKLOG_SEQ")));
    				Process.add("F");
    				process_f++;
    			}
    			if(rs.getString("PROCESS_CODE").equals("G")){
    				log_seq.add(Integer.parseInt(rs.getString("BACKLOG_SEQ")));
    				Process.add("G");
    				process_g++;
    			}
    		}
    		rs.close();
    		psta.close();
    		conn.commit();
    		for(int i = log_seq.size(); i>0; i--){
    			int process_seq = 0;//同工序流水号
    			if(Process.get(i-1).equals("D")){
    				ArrayList<String> listDel = new ArrayList<String>();
    				listDel.add(list.get(4));
    				CoreReturnObject croDel = this.setDataBySqlID("CorePrcOrder.DelBlProZg", listDel.toArray());
    				if(String.valueOf(croDel.getResult()).equals("1")){
    					process_seq = process_d--;
    					if(i==log_seq.size()){
    						finallyweight = orderZg(list,process_seq,finallyweight,"0","");
    					}
    					else{
    						finallyweight = orderZg(list,process_seq,finallyweight,"1",Process.get(i));
    					}
    				}
    			}
    			if(Process.get(i-1).equals("E")){
    				ArrayList<String> listDel = new ArrayList<String>();
    				listDel.add(list.get(4));
    				CoreReturnObject croDel = this.setDataBySqlID("CorePrcOrder.DelBlProBj", listDel.toArray());
    				if(String.valueOf(croDel.getResult()).equals("1")){
    					process_seq = process_e--;
    					if(i==log_seq.size()){
    						finallyweight = orderBj(list,process_seq,finallyweight,"1","0",Process.get(i-2),"");
    					}
    					else if(i==1){
    						finallyweight = orderBj(list,process_seq,finallyweight,"0","1","",Process.get(i));
    					}
    					else{
    						finallyweight = orderBj(list,process_seq,finallyweight,"1","1",Process.get(i-2),Process.get(i));
    					}
    				}
    			}
    			if(Process.get(i-1).equals("F")){
    				ArrayList<String> listDel = new ArrayList<String>();
    				listDel.add(list.get(4));
    				CoreReturnObject croDel = this.setDataBySqlID("CorePrcOrder.DelBlProRcl", listDel.toArray());
    				if(String.valueOf(croDel.getResult()).equals("1")){
    					process_seq = process_f--;
    					if(i==log_seq.size()){
    						finallyweight = orderRcl(list,process_seq,finallyweight,"1","0",Process.get(i-2),"");
    					}
    					else if(i==1){
    						finallyweight = orderRcl(list,process_seq,finallyweight,"0","1","",Process.get(i));
    					}
    					else{
    						finallyweight = orderRcl(list,process_seq,finallyweight,"1","1",Process.get(i-2),Process.get(i));
    					}
    				}
    			}
    			if(Process.get(i-1).equals("G")){
    				ArrayList<String> listDel = new ArrayList<String>();
    				listDel.add(list.get(4));
    				CoreReturnObject croDel = this.setDataBySqlID("CorePrcOrder.DelBlProJgx", listDel.toArray());
    				if(String.valueOf(croDel.getResult()).equals("1")){
    					process_seq = process_g--;
    					if(i==log_seq.size()){
    						finallyweight = orderJgx(list,process_seq,finallyweight,"1","0",Process.get(i-2),"");
    					}
    					else if(i==1){
    						finallyweight = orderJgx(list,process_seq,finallyweight,"0","1","",Process.get(i));
    					}
    					else{
    						finallyweight = orderJgx(list,process_seq,finallyweight,"1","1",Process.get(i-2),Process.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.getDao(CoreConstantData.sysbeanstr).ExcuteTransction(
        ///		sqllist, parmlist);
    	///return cro;
    }

    /**
     * 管坯需求处理
     * @param list	管坯信息
     * @return
     */
    public void creatReqinfGp(ArrayList<String> list){
    	//CoreReturnObject cro = new CoreReturnObject();
    	Connection conn = null;
    	PreparedStatement psta = null;
    	try{
    		ArrayList<String> listDel = new ArrayList<String>();
    		listDel.add(list.get(0));
    		listDel.add(list.get(1));
    		listDel.add(list.get(2));
    		CoreReturnObject croDel = this.setDataBySqlID("CorePrcOrder.DelSaleGpReq", listDel.toArray());
    		if(String.valueOf(croDel.getResult()).equals("1")){
    			String if_monitor_lg = "0";	//是否轧管监制
    			String prdcr_no = list.get(15);	//监制编号
    			String gpreq_id = "GP_1";		//管坯需求编号
    			ArrayList<String> parms = new ArrayList<String>();
    			
    			String sql = "SELECT T.GPREQ_ID FROM PLN_REQINF_GP T ORDER" +
    					" BY TO_NUMBER(SUBSTR(T.GPREQ_ID,4)) ASC";
    			conn = this.getDao(CoreConstantData.sysbeanstr).getConnection();
    			conn.setAutoCommit(false);
    			psta = conn.prepareStatement(sql,ResultSet.TYPE_SCROLL_SENSITIVE,ResultSet.CONCUR_UPDATABLE);
    			ResultSet rs = psta.executeQuery();
    			rs.last();
    			if(rs.getRow()!=0){
    				gpreq_id = "GP_" + String.valueOf(Integer.valueOf(
    						rs.getString("GPREQ_ID").substring(3)) + 1);
    			}
    			rs.close();
    			psta.close();
    			conn.commit();
    			///查询监制明细
    			if(!prdcr_no.equals("")){
    				sql = CoreXmlSqlParsersFactory.getSql("CoreSaleOrderOP.queryOrderPrdcr");
    				psta = conn.prepareStatement(sql,ResultSet.TYPE_SCROLL_SENSITIVE,ResultSet.CONCUR_UPDATABLE);
    				psta.setString(1, prdcr_no);
    				rs = psta.executeQuery();
    				while(rs.next()){
    					if(rs.getString("PROCESS_CDOE").equals("A")){
    						if_monitor_lg = "1";
    					}
    				}
    			}
    			rs.close();
    			psta.close();
    			conn.commit();
    			// #start 管坯需求信息
    			parms.add(gpreq_id);	//管坯需求号
    			parms.add("1");			//是否末端工序
    			parms.add("销售合同");	//合同来源
    			parms.add(list.get(0));	//合同号
    			parms.add(list.get(1));	//合同行号
    			parms.add(list.get(2));	//交货编码
    			parms.add(list.get(3));	//客户编号
    			parms.add(list.get(4));	//客户名称
    			parms.add(list.get(5));	//产品规范码
    			parms.add(list.get(6));	//冶金规范码
    			parms.add(list.get(7));	//制程索引号
    			parms.add(list.get(8));	//钢级(牌号)代码
    			parms.add(list.get(9));	//钢级(牌号)名称
    			parms.add(list.get(10));	//钢种代码
    			parms.add(list.get(11));	//管坯直径
    			parms.add(list.get(12));	//管坯长度
    			parms.add(list.get(13));	//管坯物料编码
    			parms.add(list.get(14));	//物料描述
    			parms.add(if_monitor_lg);	//是否炼钢监制
    			parms.add(prdcr_no);	//监制编号
    			parms.add(list.get(16));	//管坯单重
    			parms.add(list.get(17));	//需求总重量
    			parms.add(list.get(18));	//需求支数
    			// #end
    			this.sqllist.add(CoreXmlSqlParsersFactory.getSql("CorePrcOrder.insertGpReqinf"));
    			this.parmlist.add(parms.toArray());    
    			
    			//cro = this.setDataBySqlID("CorePrcOrder.insertGpReqinf", parms.toArray());
    		}
    	}
    	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;
    }
    
    /**
     * 构造函数
     */
    public CorePrcOrder(){
    	this.parmlist = new ArrayList<Object[]>();
    	this.sqllist = new ArrayList<String>();
    }
    
    /**
     * 获取sql集合
     */
    public ArrayList<String> getsqllist(){
    	return  this.sqllist;
    }
    
    /**
     * 获取插入参数集合
     */
    public ArrayList<Object[]> getparmlist(){
    	return this.parmlist;
    }
    
    /**
     * @param str 要左填充的字符串
     * @param charc 要填充的字符
     * @param len 填充后的长度
     * @return 填充后的字符串
    public static String padding(String str,char charc,int len){
    	StringBuffer sbf = new StringBuffer("");

    	for(int i=0;i<(len-str.length());i++){
    		sbf.append(charc);
    	}
    	return sbf.toString()+str;
    }
     */

}