package com.steering.pss.ydm.pipemanage;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.text.DecimalFormat;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

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

import com.steering.comm.db.DBCommon;
import com.steering.comm.tools.StringTool;
import com.steering.pss.ydm.base.MeanValueCalculation;

public class FrmPipeManualIn extends DBCommon{

	/***
	 * 查询期初数据
	 * @param parm
	 * @return
	 */
	public CoreReturnObject queryManualIn(ArrayList<String> parm,String ss,String supplyUnit)
	{
		if(ss.equals("0"))
		{
			String sql = CoreXmlSqlParsersFactory.getSql("FrmPipeManualIn.queryManualIn");
			if(supplyUnit.equals("120501"))
			{
				sql += " AND (SUPPLY_UNIT_CODE = '"+supplyUnit+"' OR SUPPLY_UNIT_CODE IS NULL)";
			}else if(supplyUnit.equals("120504")){
			sql += " AND SUPPLY_UNIT_CODE = '"+supplyUnit+"'";
			}
			return this.getData(sql, parm.toArray());
//		  return this.getDataBySqlID("FrmPipeManualIn.queryManualIn", parm.toArray());
		}else
		{
			String sql = CoreXmlSqlParsersFactory.getSql("FrmPipeManualIn.queryManualInHalog");
			if(supplyUnit.equals("120501"))
			{
				sql += " AND (t.SUPPLY_UNIT_CODE = '"+supplyUnit+"' OR t.SUPPLY_UNIT_CODE IS NULL)";
			}else if(supplyUnit.equals("120504")){
				sql += " AND t.SUPPLY_UNIT_CODE = '"+supplyUnit+"'";
			}
			return this.getData(sql, parm.toArray());
//			return this.getDataBySqlID("FrmPipeManualIn.queryManualInHalog", parm.toArray());
		}
	}
	
	/***
	 * 新增库存
	 * @param parm
	 * @return
	 */
	@SuppressWarnings("null")
	public CoreReturnObject addMatZc(ArrayList<Object> parm)
	{
		CoreReturnObject cro = new CoreReturnObject();
        Connection connection = this.getDao(CoreConstantData.sysbeanstr)
                .getConnection();
		ArrayList<String> list = new ArrayList<String>();
		Date now = new Date();
		SimpleDateFormat dateFormat = new SimpleDateFormat(
        "yyyy-MM-dd HH:mm:ss");
        String systime = dateFormat.format(now);
		String matNo = "";
		try
		{
			connection.setAutoCommit(false); // 取消自动提交事务
			String matSql = CoreXmlSqlParsersFactory.getSql("FrmPipeManualIn.insertMat");
			PreparedStatement insertMat = connection.prepareStatement(matSql);
			
			String matSqlXs = CoreXmlSqlParsersFactory.getSql("matxs.insertMat");
			PreparedStatement insertMatXs = connection.prepareStatement(matSqlXs);
			
			String matSql1 = CoreXmlSqlParsersFactory.getSql("FrmPipeManualIn.insertMat1");
			PreparedStatement insertMat1 = connection.prepareStatement(matSql1);
			
			String matSql1Xs = CoreXmlSqlParsersFactory.getSql("matxs.insertMat1");
			PreparedStatement insertMat1Xs = connection.prepareStatement(matSql1Xs);
			
			String butressSql = CoreXmlSqlParsersFactory.getSql("FrmPipeManualIn.insertButess");
			PreparedStatement insertButess = connection.prepareStatement(butressSql);
			String updateIntalSql = CoreXmlSqlParsersFactory.getSql("FrmPipeManualIn.updateIntal");
			PreparedStatement updateIntal = connection.prepareStatement(updateIntalSql);
			
			String updateIntalSqlXs = CoreXmlSqlParsersFactory.getSql("matxs.updateIntal");
			PreparedStatement updateIntalXs = connection.prepareStatement(updateIntalSqlXs);
			for (int i = 0; i < parm.size(); i++) {
				list = (ArrayList<String>) parm.get(i);
				int count = Integer.parseInt(list.get(20));
				double wgt = Double.parseDouble(list.get(21));
				StringBuffer sb = new StringBuffer();
				String orderNo = list.get(28).toString(); // 合同号
				String orderSeq = list.get(29).toString(); // 合同行号
				String lcode = list.get(0).toString(); // 炉号
				String lotno = list.get(1).toString(); // 批号
//				String belongCode = "";
//				String belongName = "";
//				String orderPk = "";
//				String orderLinePk = "";
//				String orderSeqPk = "";
				int allCount = 0;
				String[] arr = null;
				List<String[]> pKList = FrmPipeManualIn.getOrderPk(connection, orderNo, orderSeq);
				if(pKList !=null)
				{
					arr = pKList.get(0);
				}
//				if(arr !=null)
//				{
//				  belongCode = arr[0];
//				  belongName = arr[1];
//				  orderPk = arr[2];
//				  orderLinePk = arr[3];
//				  orderSeqPk = arr[4];
//				}
				matNo = queryMatNo(connection, lcode, lotno);
				int g = Integer.parseInt(matNo.substring(matNo.length() - 4, matNo.length()));
				List<double[]> wgtList = MeanValueCalculation.MeanCalculation(
                        wgt, count);
				ArrayList<String> lengthList = null;
				if(!list.get(17).equals("") && !list.get(17).equals("0"))
				{
					lengthList = new ArrayList<String>();
                    List<double[]> lenList = MeanValueCalculation.MeanCalculation(Double.parseDouble(list.get(17)), count);
                    for(int k = 0;k<lenList.size();k++)
                    {
                    	double[] conLen = lenList.get(k);
                    	if((int)conLen[0]>0)
                    	{
                    		for(int x = 0;x<(int)conLen[0];x++)
                    		{
                    			DecimalFormat dt = new DecimalFormat("0.000");
                    			lengthList.add(dt.format(conLen[1]));
                    		}
                    	}
                    }
				}
				for (int j = 0; j < wgtList.size(); j++) {
					double[] conW = wgtList.get(j);
					if ((int) conW[0] >= 0) {
						for (int z = 0; z < (int) conW[0]; z++) {
							if(pKList !=null)
							{
								if (arr[2] != null && arr[3] != null
										&& arr[4] != null) {
									insertMat.setString(1, list.get(0));
									insertMat.setString(2, list.get(1));
									insertMat.setString(3, list.get(2));
									insertMat.setString(4, list.get(3));
									insertMat.setString(5, list.get(4));
									insertMat.setString(6, list.get(5));
									insertMat.setString(7, list.get(6));
									insertMat.setString(8, list.get(7));
									insertMat.setString(9, list.get(8));
									insertMat.setString(10, list.get(9));
									insertMat.setString(11, list.get(10));
									insertMat.setString(12, arr[5]);// 标准
									insertMat.setString(13, arr[6]);

									insertMat.setString(14, arr[0].toString());
									insertMat.setString(15, arr[1].toString());

									insertMat.setString(16, list.get(13));
									insertMat.setString(17, list.get(14));

									insertMat.setString(18, list.get(28));
									insertMat.setString(19, list.get(29));
									if(arr[10].length()<3)
									{
										
										int m = arr[10].toString().length();
										while (m < 3) {
											arr[10] = "0" + arr[10].toString();
											m = arr[10].toString().length();
										}			

									}
									//insertMat.setString(20, list.get(30));
									insertMat.setString(20, arr[10].toString());
									insertMat.setString(21, arr[2].toString());
									insertMat.setString(22, arr[3].toString());
									insertMat.setString(23, arr[4].toString());

									insertMat.setString(24, list.get(15));
									insertMat.setString(25, list.get(16));

									insertMat.setString(26, arr[7]);
									insertMat.setString(27, arr[8]);
									insertMat.setString(28, arr[9]);
									insertMat.setString(29, list.get(18));
									insertMat.setString(30, list.get(19));

									insertMat.setString(31, list.get(37));

									insertMat.setString(32, "1");
									insertMat.setObject(33, conW[1]);// 重量
									insertMat.setString(34, systime);
									insertMat.setString(35, list.get(list.size()-3));
									insertMat.setString(36, (lcode + lotno)
											+ StringTool.formatDecimal(g,
													"0000"));

									insertMat.setString(37, list.get(31));
									if (!list.get(17).equals("")) {
										insertMat.setString(38, lengthList
												.get(allCount));
									} else {
										insertMat.setString(38, "0");
									}
									insertMat.setString(39, list.get(12));
									insertMat.setString(40, list.get(32));
									insertMat.setString(41, list.get(35));
									insertMat.setString(42, list.get(36));

									insertMat.setString(43, list.get(33));
									insertMat.setString(44, list.get(34));
									insertMat.setString(45, list.get(list.size()-4));
									insertMat.addBatch();
									
									insertMatXs.setString(1, list.get(0));
									insertMatXs.setString(2, list.get(1));
									insertMatXs.setString(3, list.get(2));
									insertMatXs.setString(4, list.get(3));
									insertMatXs.setString(5, list.get(4));
									insertMatXs.setString(6, list.get(5));
									insertMatXs.setString(7, list.get(6));
									insertMatXs.setString(8, list.get(7));
									insertMatXs.setString(9, list.get(8));
									insertMatXs.setString(10, list.get(9));
									insertMatXs.setString(11, list.get(10));
									insertMatXs.setString(12, arr[5]);// 标准
									insertMatXs.setString(13, arr[6]);

									insertMatXs.setString(14, arr[0].toString());
									insertMatXs.setString(15, arr[1].toString());

									insertMatXs.setString(16, list.get(13));
									insertMatXs.setString(17, list.get(14));

									insertMatXs.setString(18, list.get(28));
									insertMatXs.setString(19, list.get(29));
									if(arr[10].length()<3)
									{
										
										int m = arr[10].toString().length();
										while (m < 3) {
											arr[10] = "0" + arr[10].toString();
											m = arr[10].toString().length();
										}			

									}
									//insertMatXs.setString(20, list.get(30));
									insertMatXs.setString(20, arr[10].toString());
									insertMatXs.setString(21, arr[2].toString());
									insertMatXs.setString(22, arr[3].toString());
									insertMatXs.setString(23, arr[4].toString());

									insertMatXs.setString(24, list.get(15));
									insertMatXs.setString(25, list.get(16));

									insertMatXs.setString(26, arr[7]);
									insertMatXs.setString(27, arr[8]);
									insertMatXs.setString(28, arr[9]);
									insertMatXs.setString(29, list.get(18));
									insertMatXs.setString(30, list.get(19));

									insertMatXs.setString(31, list.get(37));

									insertMatXs.setString(32, "1");
									insertMatXs.setObject(33, conW[1]);// 重量
									insertMatXs.setString(34, systime);
									insertMatXs.setString(35, list.get(list.size()-3));
									insertMatXs.setString(36, (lcode + lotno)
											+ StringTool.formatDecimal(g,
													"0000"));

									insertMatXs.setString(37, list.get(31));
									if (!list.get(17).equals("")) {
										insertMatXs.setString(38, lengthList
												.get(allCount));
									} else {
										insertMatXs.setString(38, "0");
									}
									insertMatXs.setString(39, list.get(12));
									insertMatXs.setString(40, list.get(32));
									insertMatXs.setString(41, list.get(35));
									insertMatXs.setString(42, list.get(36));

									insertMatXs.setString(43, list.get(33));
									insertMatXs.setString(44, list.get(34));
									insertMatXs.setString(45, list.get(list.size()-4));
									insertMatXs.addBatch();
									
								} else {
									insertMat1.setString(1, list.get(0));
									insertMat1.setString(2, list.get(1));
									insertMat1.setString(3, list.get(2));
									insertMat1.setString(4, list.get(3));
									insertMat1.setString(5, list.get(4));
									insertMat1.setString(6, list.get(5));
									insertMat1.setString(7, list.get(6));
									insertMat1.setString(8, list.get(7));
									insertMat1.setString(9, list.get(8));
									insertMat1.setString(10, list.get(9));
									insertMat1.setString(11, list.get(10));
									insertMat1.setString(12, "");
									insertMat1.setString(13, "");
									if (arr[0] != null) {
										insertMat1.setString(14, arr[0]
												.toString());
										insertMat1.setString(15, arr[1]
												.toString());
									} else {
//										if (orderNo.contains("XS")) {
//											insertMat1.setString(14, "100101");
//											insertMat1.setString(15, "销售公司");
//										}
//										if (orderNo.contains("XW")) {
//											insertMat1.setString(14, "100103");
//											insertMat1.setString(15, "钢贸公司");
//										}
//										if (orderNo.contains("TP")) {
//											insertMat1.setString(14, "100102");
//											insertMat1.setString(15, "国贸公司");
//										}
										insertMat1.setString(14, list.get(45));
										insertMat1.setString(15, list.get(46));
									}

									insertMat1.setString(16, list.get(13));// 标准类别
									insertMat1.setString(17, list.get(14));

									insertMat1.setString(18, list.get(15));
									insertMat1.setString(19, list.get(16));// 外径、壁厚

									insertMat1.setString(20, "");
									insertMat1.setString(21, "");
									insertMat1.setString(22, "");

									insertMat1.setString(23, list.get(18));// 长度上、下限
									insertMat1.setString(24, list.get(19));

									insertMat1.setString(25, list.get(37));
									insertMat1.setString(26, "1");
									insertMat1.setObject(27, conW[1]);// 重量
									insertMat1.setString(28, systime);
									insertMat1.setString(29, list.get(list.size()-3));
									insertMat1.setString(30, (lcode + lotno)
											+ StringTool.formatDecimal(g,
													"0000"));

									insertMat1.setString(31, list.get(31));
									if (!list.get(17).equals("")) {
										insertMat1.setString(32, lengthList
												.get(allCount));
									} else {
										insertMat1.setString(32, "0");
									}
									insertMat1.setString(33, list.get(12));
									insertMat1.setString(34, list.get(32));
									insertMat1.setString(35, list.get(35));
									insertMat1.setString(36, list.get(36));
									insertMat1.setString(37, list.get(33));
									insertMat1.setString(38, list.get(34));
									insertMat1.setString(39, list.get(list.size()-4));
									insertMat1.addBatch();
									
									insertMat1Xs.setString(1, list.get(0));
									insertMat1Xs.setString(2, list.get(1));
									insertMat1Xs.setString(3, list.get(2));
									insertMat1Xs.setString(4, list.get(3));
									insertMat1Xs.setString(5, list.get(4));
									insertMat1Xs.setString(6, list.get(5));
									insertMat1Xs.setString(7, list.get(6));
									insertMat1Xs.setString(8, list.get(7));
									insertMat1Xs.setString(9, list.get(8));
									insertMat1Xs.setString(10, list.get(9));
									insertMat1Xs.setString(11, list.get(10));
									insertMat1Xs.setString(12, "");
									insertMat1Xs.setString(13, "");
									if (arr[0] != null) {
										insertMat1Xs.setString(14, arr[0]
												.toString());
										insertMat1Xs.setString(15, arr[1]
												.toString());
									} else {
//										if (orderNo.contains("XS")) {
//											insertMat1Xs.setString(14, "100101");
//											insertMat1Xs.setString(15, "销售公司");
//										}
//										if (orderNo.contains("XW")) {
//											insertMat1Xs.setString(14, "100103");
//											insertMat1Xs.setString(15, "钢贸公司");
//										}
//										if (orderNo.contains("TP")) {
//											insertMat1Xs.setString(14, "100102");
//											insertMat1Xs.setString(15, "国贸公司");
//										}
										insertMat1Xs.setString(14, list.get(45));
										insertMat1Xs.setString(15, list.get(46));
									}

									insertMat1Xs.setString(16, list.get(13));// 标准类别
									insertMat1Xs.setString(17, list.get(14));

									insertMat1Xs.setString(18, list.get(15));
									insertMat1Xs.setString(19, list.get(16));// 外径、壁厚

									insertMat1Xs.setString(20, "");
									insertMat1Xs.setString(21, "");
									insertMat1Xs.setString(22, "");

									insertMat1Xs.setString(23, list.get(18));// 长度上、下限
									insertMat1Xs.setString(24, list.get(19));

									insertMat1Xs.setString(25, list.get(37));
									insertMat1Xs.setString(26, "1");
									insertMat1Xs.setObject(27, conW[1]);// 重量
									insertMat1Xs.setString(28, systime);
									insertMat1Xs.setString(29, list.get(list.size()-3));
									insertMat1Xs.setString(30, (lcode + lotno)
											+ StringTool.formatDecimal(g,
													"0000"));

									insertMat1Xs.setString(31, list.get(31));
									if (!list.get(17).equals("")) {
										insertMat1Xs.setString(32, lengthList
												.get(allCount));
									} else {
										insertMat1Xs.setString(32, "0");
									}
									insertMat1Xs.setString(33, list.get(12));
									insertMat1Xs.setString(34, list.get(32));
									insertMat1Xs.setString(35, list.get(35));
									insertMat1Xs.setString(36, list.get(36));
									insertMat1Xs.setString(37, list.get(33));
									insertMat1Xs.setString(38, list.get(34));
									insertMat1Xs.setString(39, list.get(list.size()-4));
									insertMat1Xs.addBatch();
									
								}	
							}else
							{
								insertMat1.setString(1, list.get(0));
								insertMat1.setString(2, list.get(1));
								insertMat1.setString(3, list.get(2));
								insertMat1.setString(4, list.get(3));
								insertMat1.setString(5, list.get(4));
								insertMat1.setString(6, list.get(5));
								insertMat1.setString(7, list.get(6));
								insertMat1.setString(8, list.get(7));
								insertMat1.setString(9, list.get(8));
								insertMat1.setString(10, list.get(9));
								insertMat1.setString(11, list.get(10));
								insertMat1.setString(12, "");
								insertMat1.setString(13, "");
//								if (orderNo.contains("XS")) {
//									insertMat1.setString(14, "100101");
//									insertMat1.setString(15, "销售公司");
//								}
//								if (orderNo.contains("XW")) {
//									insertMat1.setString(14, "100103");
//									insertMat1.setString(15, "钢贸公司");
//								}
//								if (orderNo.contains("TP")) {
//									insertMat1.setString(14, "100102");
//									insertMat1.setString(15, "国贸公司");
//								}
								insertMat1.setString(14, list.get(45));
								insertMat1.setString(15, list.get(46));
								insertMat1.setString(16, list.get(13));// 标准类别
								insertMat1.setString(17, list.get(14));

								insertMat1.setString(18, list.get(15));
								insertMat1.setString(19, list.get(16));// 外径、壁厚

								insertMat1.setString(20, "");
								insertMat1.setString(21, "");
								insertMat1.setString(22, "");

								insertMat1.setString(23, list.get(18));// 长度上、下限
								insertMat1.setString(24, list.get(19));

								insertMat1.setString(25, list.get(37));
								insertMat1.setString(26, "1");
								insertMat1.setObject(27, conW[1]);// 重量
								insertMat1.setString(28, systime);
								insertMat1.setString(29, list.get(list.size()-3));
								insertMat1.setString(30, (lcode + lotno)
										+ StringTool.formatDecimal(g,
												"0000"));

								insertMat1.setString(31, list.get(31));
								if (!list.get(17).equals("")) {
									insertMat1.setString(32, lengthList
											.get(allCount));
								} else {
									insertMat1.setString(32, "");
								}
								insertMat1.setString(33, list.get(12));
								insertMat1.setString(34, list.get(32));
								insertMat1.setString(35, list.get(35));
								insertMat1.setString(36, list.get(36));
								insertMat1.setString(37, list.get(33));
								insertMat1.setString(38, list.get(34));
								insertMat1.setString(39, list.get(list.size()-4));
								insertMat1.addBatch();
								
								insertMat1Xs.setString(1, list.get(0));
								insertMat1Xs.setString(2, list.get(1));
								insertMat1Xs.setString(3, list.get(2));
								insertMat1Xs.setString(4, list.get(3));
								insertMat1Xs.setString(5, list.get(4));
								insertMat1Xs.setString(6, list.get(5));
								insertMat1Xs.setString(7, list.get(6));
								insertMat1Xs.setString(8, list.get(7));
								insertMat1Xs.setString(9, list.get(8));
								insertMat1Xs.setString(10, list.get(9));
								insertMat1Xs.setString(11, list.get(10));
								insertMat1Xs.setString(12, "");
								insertMat1Xs.setString(13, "");
//								if (orderNo.contains("XS")) {
//									insertMat1Xs.setString(14, "100101");
//									insertMat1Xs.setString(15, "销售公司");
//								}
//								if (orderNo.contains("XW")) {
//									insertMat1Xs.setString(14, "100103");
//									insertMat1Xs.setString(15, "钢贸公司");
//								}
//								if (orderNo.contains("TP")) {
//									insertMat1Xs.setString(14, "100102");
//									insertMat1Xs.setString(15, "国贸公司");
//								}
								insertMat1Xs.setString(14, list.get(45));
								insertMat1Xs.setString(15, list.get(46));
								insertMat1Xs.setString(16, list.get(13));// 标准类别
								insertMat1Xs.setString(17, list.get(14));

								insertMat1Xs.setString(18, list.get(15));
								insertMat1Xs.setString(19, list.get(16));// 外径、壁厚

								insertMat1Xs.setString(20, "");
								insertMat1Xs.setString(21, "");
								insertMat1Xs.setString(22, "");

								insertMat1Xs.setString(23, list.get(18));// 长度上、下限
								insertMat1Xs.setString(24, list.get(19));

								insertMat1Xs.setString(25, list.get(37));
								insertMat1Xs.setString(26, "1");
								insertMat1Xs.setObject(27, conW[1]);// 重量
								insertMat1Xs.setString(28, systime);
								insertMat1Xs.setString(29, list.get(list.size()-3));
								insertMat1Xs.setString(30, (lcode + lotno)
										+ StringTool.formatDecimal(g,
												"0000"));

								insertMat1Xs.setString(31, list.get(31));
								if (!list.get(17).equals("")) {
									insertMat1Xs.setString(32, lengthList
											.get(allCount));
								} else {
									insertMat1Xs.setString(32, "");
								}
								insertMat1Xs.setString(33, list.get(12));
								insertMat1Xs.setString(34, list.get(32));
								insertMat1Xs.setString(35, list.get(35));
								insertMat1Xs.setString(36, list.get(36));
								insertMat1Xs.setString(37, list.get(33));
								insertMat1Xs.setString(38, list.get(34));
								insertMat1Xs.setString(39, list.get(list.size()-4));
								insertMat1Xs.addBatch();
								
							}
							insertButess.setString(1, list.get(0));
							insertButess.setString(2, list.get(1));
							insertButess.setString(3, list.get(2));
							insertButess.setString(4, list.get(25));
							insertButess.setString(5, list.get(25)+"10101");
							insertButess.setString(6, "");
							insertButess.setString(7, "");
							insertButess.setString(8, list.get(37));
							insertButess.setString(9, (lcode + lotno)
									+ StringTool.formatDecimal(g, "0000"));
							insertButess.setString(10, systime);
							insertButess.setString(11, list.get(39));
							insertButess.setString(12, list.get(40));
							insertButess.setString(13, list.get(41));
							insertButess.setString(14, list.get(42));
							 
							insertButess.addBatch();
							allCount++;
							g++;
						}
					}
				}
				updateIntal.setString(1, list.get(38));
				updateIntalXs.setString(1, list.get(38));
				updateIntal.addBatch();
				updateIntalXs.addBatch();
				
				
				insertMat.executeBatch();
				insertMatXs.executeBatch();
				insertMat1.executeBatch();
				insertMat1Xs.executeBatch();
				insertButess.executeBatch();
				updateIntal.executeBatch();
				updateIntalXs.executeBatch();
				
			}
			cro.setResult(1);
			cro.setV_errMsg("期初库存维护成功!");
			connection.commit();
		} catch (Exception e) {
			System.out.println(e.getMessage());
			if (connection != null) {
				try {
					connection.rollback(); // 回滚事务
				} catch (Exception e2) {
				}
			}
		} finally {
			try {
				if (connection != null && !connection.isClosed()) {
					connection.close();
				}
			} catch (Exception e2) {
			}
		}
		return cro;
	}
	
	public static List<String[]> getOrderPk(Connection con,
            String orderNo,String orderSeq) throws SQLException {
        List<String[]> list = new ArrayList<String[]>();
        String sql = CoreXmlSqlParsersFactory
                .getSql("FrmPipeManualIn.getOrderPk");
        PreparedStatement ps = con.prepareStatement(sql);
        ps.setObject(1, orderNo);
        ps.setObject(2, orderSeq);
        ResultSet rs = ps.executeQuery();
        while (rs.next()) {
            String[] strArray = new String[11];
            strArray[0] = rs.getString("SALE_ORG");
            strArray[1] = rs.getString("SALE_ORG_DESC");
            strArray[2] = rs.getString("ORD_PK");
            strArray[3] = rs.getString("ORD_LN_PK");
            strArray[4] = rs.getString("ORD_LN_DLY_PK");
            strArray[5] = rs.getString("STD_CODE");
            strArray[6] = rs.getString("STD_NAME");
            strArray[7] = rs.getString("LEN_NO");
            strArray[8] = rs.getString("LEN_DESC");
            strArray[9] = rs.getString("LEN_UNIT");
            strArray[10] = rs.getString("DELIVERY_NO");
            list.add(strArray);
        }
        rs.close();
        ps.close();
        if (list == null || list.size() == 0) {
            return null;
        }
        return list;
    }
	/**
     * 根据炉号、批号自动生成材料号
     * @param conn c
     * @param strLcode 传递参数
     * @return obj
     * @throws Exception 异常处理
     */
    public String queryMatNo(Connection conn, String strLcode, String strBatch)
            throws Exception {
        int count = 0;
        PreparedStatement psta = null;
        String sqlLovsqe = CoreXmlSqlParsersFactory
                .getSql("CorePipeInTemp.QueryMatNo");
        psta = conn.prepareStatement(sqlLovsqe);
        psta.setString(1, strLcode);
        psta.setString(2, strBatch);
        ResultSet rs = psta.executeQuery();
        if (rs.next()) {
            String res = rs.getString(1);
            if (res == null) {
                res = strLcode + strBatch + "0001";
            }
            else {
                count = Integer.parseInt(res.substring(res.length() - 4, res
                        .length()));
            }
        }
        rs.close();
        psta.close();
        return (strLcode + strBatch)
                + StringTool.formatDecimal(++count, "0000");
    }

    public CoreReturnObject updateMat(ArrayList<Object> parm)
    {
    	CoreReturnObject cro = new CoreReturnObject();
        Connection connection = this.getDao(CoreConstantData.sysbeanstr)
                .getConnection();
		Date now = new Date();
		SimpleDateFormat dateFormat = new SimpleDateFormat(
        "yyyy-MM-dd HH:mm:ss");
        String systime = dateFormat.format(now);
		try
		{
			connection.setAutoCommit(false); // 取消自动提交事务
			String updateMatSql= CoreXmlSqlParsersFactory.getSql("FrmPipeManualIn.updateMat");//修改材料表
			PreparedStatement updateMatpsta = connection.prepareStatement(updateMatSql);
			String updateInitalSql = CoreXmlSqlParsersFactory.getSql("FrmPipeManualIn.updateLog");//修改期初
			PreparedStatement updateInitalpsta = connection.prepareStatement(updateInitalSql);
			for(int i = 0;i<parm.size();i++)
			{
				ArrayList<String> list = (ArrayList<String>) parm.get(i);
				String[] arr = null;
				List<String[]> pKList = FrmPipeManualIn.getOrderPk(connection, list.get(3), list.get(4));
				if(pKList !=null)
				{
					arr = pKList.get(0);
				}
				if(pKList !=null)
				{
					if (arr[2] != null && arr[3] != null
							&& arr[4] != null)
					{
						updateMatpsta.setString(1, arr[2]);
						updateMatpsta.setString(2, arr[3]);
						updateMatpsta.setString(3, arr[4]);
//						updateMatpsta.setString(4, "801403");
//						updateMatpsta.setString(5, "80150301");
					}else
					{
						updateMatpsta.setString(1, "");
						updateMatpsta.setString(2, "");
						updateMatpsta.setString(3, "");
//						updateMatpsta.setString(4, "801402");
//						updateMatpsta.setString(5, "80150202");
					}
				}else
				{
					updateMatpsta.setString(1, "");
					updateMatpsta.setString(2, "");
					updateMatpsta.setString(3, "");
//					updateMatpsta.setString(4, "801402");
//					updateMatpsta.setString(5, "80150202");
				}
				updateMatpsta.setString(6, list.get(0));
				updateMatpsta.setString(7, list.get(1));
				updateMatpsta.setString(8, list.get(2));
                updateMatpsta.addBatch();
                
                updateInitalpsta.setString(1, list.get(6));
                updateInitalpsta.addBatch();
			}
			
			
			updateMatpsta.executeBatch();
			updateInitalpsta.executeBatch();
			cro.setResult(1);
			cro.setV_errMsg("修改成功!");
			connection.commit();
		} catch (Exception e) {
			System.out.println(e.getMessage());
			if (connection != null) {
				try {
					connection.rollback(); // 回滚事务
				} catch (Exception e2) {
				}
			}
		} finally {
			try {
				if (connection != null && !connection.isClosed()) {
					connection.close();
				}
			} catch (Exception e2) {
			}
		}
		return cro;
    }
}
