package com.steering.pss.ydm.pipemanage;

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;

public class FrmPipupdateStoreButress extends DBCommon {
	/***
	 * 查询材料状态
	 * @param list
	 * @return
	 */
	public CoreReturnObject selectMatStu(ArrayList<String> list)
	{
		return this.getDataBySqlID("FrmPipupdateStoreButress.selectMatStu", list.toArray());
	}
	
	/***
	 * 查询重量
	 * @param list
	 * @return
	 */
	public CoreReturnObject selectWgt(ArrayList<String> list)
	{
		return this.getDataBySqlID("FrmPipupdateStoreButress.selectWgt", list.toArray());
	}
	/***
	 * 批量修改库存位置
	 * @param parm
	 * @return
	 */
	public CoreReturnObject updateStore(ArrayList<Object> parm)
	{
		CoreReturnObject cro = new CoreReturnObject();
		Connection connection = this.getDao(CoreConstantData.sysbeanstr)
				.getConnection();
		PreparedStatement psta = null;
		ResultSet rs = null;
		String sql = "";
		try {
			connection.setAutoCommit(false); // 取消自动提交事务
			for(int i = 0; i<parm.size();i++)
			{
				ArrayList<String> matNo = new ArrayList<String>();
				ArrayList<String> list = (ArrayList<String>) parm.get(i);
				int count = Integer.parseInt(list.get(5)); //支数
				String selectMatNoSql = CoreXmlSqlParsersFactory.getSql("FrmPipupdateStoreButress.selectMatNo");//查询材料号
				psta = connection.prepareStatement(selectMatNoSql);
				psta.setString(1, list.get(0));
				psta.setString(2, list.get(1));
				psta.setString(3, list.get(2));
				psta.setString(4, list.get(3));
				psta.setString(5, list.get(4));
				psta.setString(6, list.get(7));
				psta.setString(7, list.get(8));
				psta.setString(8, String.valueOf(count+1));
				rs = psta.executeQuery();
				while(rs.next())
				{
					matNo.add(rs.getString("MAT_NO"));
				}
				rs.close();
				psta.close();
				for(int j = 0;j<matNo.size();j++)
				{
					sql = CoreXmlSqlParsersFactory.getSql("FrmPipupdateStoreButress.updateButress");//位置表 
					psta = connection.prepareStatement(sql);
					psta.setString(1, list.get(6));
					psta.setString(2, list.get(6) +"10101");
					psta.setString(3, matNo.get(j));
					psta.executeUpdate();
					psta.close();
					
					sql = CoreXmlSqlParsersFactory.getSql("FrmPipupdateStoreButress.updateInList");//修改入库表
					psta = connection.prepareStatement(sql);
					psta.setString(1, list.get(6));
					psta.setString(2, list.get(6) +"10101");
					psta.setString(3, matNo.get(j));
					psta.executeUpdate();
					psta.close();
				}
			}
			cro.setResult(1);
			cro.setV_errMsg("修改成功!");
			connection.commit();
		} catch (SQLException 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;
	}

}
