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 javax.script.ScriptException;

import com.steering.comm.db.DBCommon;

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

/*
 * 计算销售合同成品长度和扎管控制壁厚
 * @author 杨天海
 * @version 1.1
 * @Date 2015.05.23
 */
public class CoreOrderNumCompute extends DBCommon{
	/*
	 * 计算成品长度和控制壁厚
	 */
	public CoreReturnObject aimLengthandThick(ArrayList<String> list) throws ScriptException{
		CoreReturnObject cro = new CoreReturnObject();
		ArrayList<String> result = new ArrayList<String>();
		
		Connection conn = null;
		PreparedStatement psta = null;
		try{
			String sql = "";			//执行的sql语句
			ResultSet rs = null;		//执行sql语句的结果
			CoreTolerOfNkParam tolerance = new CoreTolerOfNkParam();
			LenComputeParam Calculate = new LenComputeParam();		//计算长度参数
			LenComputeOutParam CalResult = new LenComputeOutParam();	//计算结果
			
			double Billetmax = 0;		//坯料max(mm)
			double Sizing = 0;			//定径后max(m)
			double burnout_rate = 0;	//烧损率
			String sic = "";		//公差标准索引号
			double wallLower = 0;		//壁厚公差下限(mm)
			double wallUpper = 0;		//壁厚公差上限(mm)
			
			///客户端参数传递
			String pline_code = list.get(0);							//产线代码
			String msc_pline = list.get(1);								//制程索引
			String strMsc = list.get(2);								//冶金规范码
			double outdiameter = Double.parseDouble(list.get(3));		//外径
			double wallthick = Double.parseDouble(list.get(4));			//壁厚
			double lengthmax = Double.parseDouble(list.get(5));			//最大长度
			double lengthmin = Double.parseDouble(list.get(6));			//最小长度
			boolean shrt_len_rato = false;								//是否短尺随炉
			double sigl_len = Double.parseDouble(list.get(8));			//单倍尺
			double dimater = Double.parseDouble(list.get(9));			//坯料断面
			if(!list.get(7).equals("0")){
				shrt_len_rato = true;
			}
			
			conn = this.getDao(CoreConstantData.sysbeanstr).getConnection();
			conn.setAutoCommit(false);
			
			//查询公差标准索引号
			sql = CoreXmlSqlParsersFactory.getSql("CoreSaleOrderOP.querySpecstand");
			psta = conn.prepareStatement(sql,ResultSet.TYPE_SCROLL_SENSITIVE,ResultSet.CONCUR_UPDATABLE);
			psta.setString(1, strMsc);
			psta.setString(2, msc_pline);
			rs = psta.executeQuery();
			while(rs.next()){
				if(rs.getString("SIC")!=null){
					sic = rs.getString("SIC");
				}
			}
			rs.close();
			psta.close();
			conn.commit();

			///计算壁厚公差上下限
			tolerance.setOutdiameter(outdiameter);
			tolerance.setWallthick(wallthick);
			tolerance.setSic(sic);
			tolerance.setBias_code("404002");
			tolerance.setLengthmin(lengthmin);
			tolerance.setLengthmax(lengthmax);
			tolerance = new CoreTolerOfNk().queryTolerance(tolerance);
			wallLower = tolerance.getWallLower();
			wallUpper = tolerance.getWallUpper();
			
			/*///查询质量设计结果表，确定适用产线。
			sql = "SELECT * FROM SLM_ORD_DESIGN_MSC_PL_GX_PRL T " +
					"WHERE T.DESGIN_KEY=? AND T.MSC_PLINE=? AND T.PROCESS_CODE='D'";
			psta = conn.prepareStatement(sql,ResultSet.TYPE_SCROLL_SENSITIVE,ResultSet.CONCUR_UPDATABLE);
			psta.setString(1, desgin_key);
			psta.setString(2, msc_pline);
			rs = psta.executeQuery();
			while(rs.next()){
				pline_code = rs.getString("PLINE_CODE");
			}
			rs.close();
			psta.close();
			conn.commit();
			*/
			
			///查询产线基础表，获取机组信息。
			sql = CoreXmlSqlParsersFactory.getSql("CoreSaleOrderOP.queryBasepline");
			psta = conn.prepareStatement(sql,ResultSet.TYPE_SCROLL_SENSITIVE,ResultSet.CONCUR_UPDATABLE);
			psta.setString(1, pline_code);
			rs = psta.executeQuery();
			while(rs.next()){
				if(rs.getString("LENGTH_MAX_RING")!=null){
					Billetmax = Double.parseDouble(rs.getString("LENGTH_MAX_RING"));
				}
				if(rs.getString("LENGTH_COLDBED")!=null){
					Sizing = Double.parseDouble(rs.getString("LENGTH_COLDBED"));
				}
				if(rs.getString("BURNOUT_RATE")!=null){
					burnout_rate = Double.parseDouble(rs.getString("BURNOUT_RATE"))/100;
				}
			}
			rs.close();
			psta.close();
			conn.commit();
			
			
			Calculate.setOutdiameter(outdiameter);					//外径
			Calculate.setWallthick(wallthick);						//壁厚
			Calculate.setWallLower(wallLower);						//壁厚公差下限(mm)
			Calculate.setWallUpper(wallUpper);						//壁厚公差上限(mm)
			Calculate.setShrtstove(shrt_len_rato);					//短尺随炉
			Calculate.setSigllen(sigl_len);							//单倍尺长
			Calculate.setBurning(burnout_rate);						//烧损
			Calculate.setDiameter(dimater);							//断面(管坯直径)
			Calculate.setLengthmax(lengthmax);						//最大长度
			Calculate.setLengthmin(lengthmin);						//最小长度
			Calculate.setCutheadtail(1);							//切头尾(默认为1)
			Calculate.setBilletmax(Billetmax);						//管坯最长
			Calculate.setSizing(Sizing);							//冷床最长
			///计算过程
			CalResult = CoreLengthCompute.comput(Calculate);
			//计算结果
			result.add(CalResult.getAimwallthick());		//控制壁厚
			result.add(CalResult.getAimlength());		//目标长度
			result.add(CalResult.getCutnum());			//最佳分切
			result.add(CalResult.getLengthZz());			//轧制长度
			result.add(CalResult.getLossprcent());		//损失率
			result.add(CalResult.getBillet());			//铸坯长度
		}
		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.setResult(result);
		return cro;
	}
	
}