package com.steering.pss.ydm.Stuffmanage;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
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.comm.tools.EntityTool;
import com.steering.pss.buybillet.Entity.MatGpMEntity;

public class FrmMatGpM extends DBCommon{

	/***
	 * 查询
	 * @param parmList
	 * @return
	 */
	public CoreReturnObject doQuery(ArrayList<String> parmList)
	{
		String sql = CoreXmlSqlParsersFactory.getSql("FrmMatGpM.doQuery");
		if(!parmList.get(0).equals(""))
		{
			sql = sql.replace("#BAL_YEAR_MONTH#", " AND T.BAL_YEAR_MONTH = '"+parmList.get(0)+"'");
		}else
		{
			sql = sql.replace("#BAL_YEAR_MONTH#", "");
		}
		if(!parmList.get(1).equals(""))
		{
			sql = sql.replace("#JUDGE_STOVE_NO#", " AND T.JUDGE_STOVE_NO = '"+parmList.get(1)+"'");
		}else
		{
			sql = sql.replace("#JUDGE_STOVE_NO#", "");
		}
		return this.getData(sql);
	}
	
	/***
	 * 置余材
	 * @param parmList
	 * @return
	 */
	public CoreReturnObject takeBill(ArrayList<String> parmList)
	{
		CoreReturnObject cro = new CoreReturnObject();
		Connection connection = this.getDao(CoreConstantData.sysbeanstr)
				.getConnection();
		String sql = "";
		PreparedStatement psta = null;
		ResultSet rs = null;
		String PLANSTATUS= ""; //计划状态
		try {
			ArrayList<String> sqllist = new ArrayList<String>();
			for(String str: parmList)
			{
				MatGpMEntity matGpEntity = EntityTool.getEntityByJSONStr(str, MatGpMEntity.class);
				
				sql = CoreXmlSqlParsersFactory
				.getSql("FrmMatGpM.QueryPlanStatus");// 查询计划状态
				psta = connection.prepareStatement(sql);
				psta.setString(1, matGpEntity.getHeatPlanNoNext());
				rs = psta.executeQuery();
				if (rs.next()) {
					PLANSTATUS = rs.getString("PLANSTATUS");
				}
				rs.close();
				psta.close();
				if(!PLANSTATUS.equals("")){
					if (!PLANSTATUS.equals("00")) {
						cro.setV_errMsg("炉计划不是计划状态，不能置余材!");
						return cro;
					}
				}
				
				String updateMatSql = CoreXmlSqlParsersFactory.getSql("FrmMatGpM.takeBill");
				updateMatSql = EntityTool.fillSqlByEntity(updateMatSql, matGpEntity);
				sqllist.add(updateMatSql);
			}
			cro = this.getDao(CoreConstantData.sysbeanstr).ExcuteTransction(sqllist);
			if(cro.getV_errMsg().equals(""))
			{
				cro.setV_errMsg("置余材成功!");
			}
		} catch (Exception e) {
			System.out.println(e.getMessage());
			if (connection != null) {
				try {
					cro.setV_errMsg(e.getMessage());
					connection.rollback(); // 回滚事务
				} catch (Exception e2) {
				}
			}
		} finally {
			try {
				if (connection != null && !connection.isClosed()) {
					connection.close();
				}
			} catch (Exception e2) {
			}
		}
		return cro;
	}
}
