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.ydm.Stuffmanage.Entity.MatGpMEntity;
import com.steering.pss.ydm.Stuffmanage.Entity.StlIncisionEntity;
import com.steering.pss.ydm.Stuffmanage.Entity.YmdGpInlistEntity;

public class FrmStuffInputStore extends DBCommon{

	/***
	 * 切割实绩查询
	 * @param parm
	 * @return
	 */
	public CoreReturnObject doQuery(ArrayList<String> parm)
	{
	   return this.getDataBySqlID("FrmStuffInputStore.doQuery", parm.toArray());	
	}
	/***
	 * 查询收料实绩
	 * @param parm
	 * @return
	 */
	public CoreReturnObject doQueryMat(ArrayList<String> parm)
	{
	   return this.getDataBySqlID("FrmStuffInputStore.doQueryMat", parm.toArray());	
	}
	/***
	 * 查询入库实绩
	 * @param parm
	 * @return
	 */
	public CoreReturnObject doQueryInList(ArrayList<String> parm)
	{
		return this.getDataBySqlID("FrmStuffInputStore.doQueryInList", parm.toArray());	
	}
	/***
	 * 查询炉计划
	 * @param startTim
	 * @param endTim
	 * @param stoveNo
	 * @return
	 */
	public CoreReturnObject doQueryPlan(String startTim,String endTim,String stoveNo)
	{
		String sql = CoreXmlSqlParsersFactory.getSql("FrmStuffSurfaceInspection.doQueryPlan");
		if(!startTim.equals(""))
		{
			sql += " AND T.STARTTIME >=TO_DATE('" + startTim
			+ "', 'YYYY-MM-DD HH24:MI:SS')AND T.STARTTIME <=TO_DATE('"
			+ endTim + "', 'YYYY-MM-DD HH24:MI:SS') ";
		}
		if(!stoveNo.equals(""))
		{
			sql += " AND T.STOVE_NO  = '"+ stoveNo +"'";
		}
		return this.getData(sql);
	}
	
	/***
	 * 收料
	 * @param parm
	 * @return
	 */
	public CoreReturnObject receiveData(ArrayList<String> parm,String userName)
	{
		CoreReturnObject cro = new CoreReturnObject();
        Connection connection = this.getDao(CoreConstantData.sysbeanstr)
                .getConnection();
        PreparedStatement psta = null;
        String sql = "";
        ResultSet rs = null;
        String acount = "";
        try {
			connection.setAutoCommit(false); // 取消自动提交事务
			String insertMatSql = CoreXmlSqlParsersFactory.getSql("FrmStuffInputStore.receiveData");
			PreparedStatement insertMatPsta = connection.prepareStatement(insertMatSql);
			for(String str : parm)
			{
				StlIncisionEntity stlInEntity = EntityTool.getEntityByJSONStr(str, StlIncisionEntity.class);
				sql = CoreXmlSqlParsersFactory.getSql("FrmStuffInputStore.selectStlIn");
				psta = connection.prepareStatement(sql);
				psta.setString(1, stlInEntity.getStoveNo());
				psta.setString(2, stlInEntity.getJudgeStoveNo());
				psta.setString(3, stlInEntity.getBatchGroudNo());
				psta.setString(4, stlInEntity.getLenMin());
				psta.setString(5, stlInEntity.getLenMax());
				psta.setString(6, stlInEntity.getFixNum());
				psta.setString(7, stlInEntity.getFixLen());
				rs = psta.executeQuery();
				if(rs.next())
				{
					acount = rs.getString("ACOUNT");
				}
				rs.close();
				psta.close();
				if(Integer.parseInt(acount)>0)
				{
					cro.setV_errMsg("该炉【"+stlInEntity.getJudgeStoveNo()+"】已进行过收料了,不允许重复收料!");
					return cro;
				}
				insertMatPsta.setString(1, stlInEntity.getStoveNo());
				insertMatPsta.setString(2, stlInEntity.getJudgeStoveNo());
				insertMatPsta.setString(3, stlInEntity.getBatchGroudNo());
				insertMatPsta.setString(4, stlInEntity.getLenMin());
				insertMatPsta.setString(5, stlInEntity.getLenMax());
				insertMatPsta.setString(6, stlInEntity.getFixNum());
				insertMatPsta.setString(7, stlInEntity.getFixLen());
				insertMatPsta.setString(8, userName);
				insertMatPsta.addBatch();
			}
			insertMatPsta.executeBatch();
			connection.commit();
			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;
	}
	
	/***
	 * 撤销收料
	 * @param parm
	 * @return
	 */
	public CoreReturnObject cancelReceive(ArrayList<String> parm)
	{
		CoreReturnObject cro = new CoreReturnObject();
        Connection connection = this.getDao(CoreConstantData.sysbeanstr)
                .getConnection();
        PreparedStatement psta = null;
        String sql = "";
        ResultSet rs = null;
        String acount = "";
        try {
			connection.setAutoCommit(false); // 取消自动提交事务
			String cancelReciveSql = CoreXmlSqlParsersFactory.getSql("FrmStuffInputStore.cancelReceive");
			PreparedStatement cancelRecivePsta = connection.prepareStatement(cancelReciveSql);
			for(String str:parm)
			{
				MatGpMEntity matGpEntity = EntityTool.getEntityByJSONStr(str, MatGpMEntity.class);
				cancelRecivePsta.setString(1, matGpEntity.getStoveNo());
				cancelRecivePsta.setString(2, matGpEntity.getJudgeStoveNo());
				cancelRecivePsta.setString(3, matGpEntity.getGroudNo());
				cancelRecivePsta.setString(4, matGpEntity.getActLenMin());
				cancelRecivePsta.setString(5, matGpEntity.getActLenMax());
				cancelRecivePsta.setString(6, matGpEntity.getActFixNum());
				cancelRecivePsta.setString(7, matGpEntity.getActFixLen());
				cancelRecivePsta.addBatch();
			}
			cancelRecivePsta.executeBatch();
			connection.commit();
			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;
	}
	
	/***
	 * 确认入库
	 * @param parm
	 * @return
	 */
	public CoreReturnObject ensureInPutData(ArrayList<String> parm,String storeNo,String strInBound,String[] storeType,String userName,String mangeNo,String mangeMent,String departId,String departMent)
	{
		CoreReturnObject cro = new CoreReturnObject();
        Connection connection = this.getDao(CoreConstantData.sysbeanstr)
                .getConnection();
        PreparedStatement psta = null;
        String sql = "";
        ResultSet rs = null;
        String acount = "";
        try {
			connection.setAutoCommit(false); // 取消自动提交事务
			String ensureInPutSql = CoreXmlSqlParsersFactory.getSql("FrmStuffInputStore.ensureInPutData");
			PreparedStatement ensureInPutPsta = connection.prepareStatement(ensureInPutSql);
			for(String str:parm)
			{
				MatGpMEntity matGpEntity = EntityTool.getEntityByJSONStr(str, MatGpMEntity.class);
				ensureInPutPsta.setString(1, strInBound);
				ensureInPutPsta.setString(2, strInBound);
				ensureInPutPsta.setString(3, matGpEntity.getStoveNo());
				ensureInPutPsta.setString(4, matGpEntity.getJudgeStoveNo());
				ensureInPutPsta.setString(5, matGpEntity.getGroudNo());
				ensureInPutPsta.setString(6, matGpEntity.getActLenMin());
				ensureInPutPsta.setString(7, matGpEntity.getActLenMax());
				ensureInPutPsta.setString(8, matGpEntity.getActFixNum());
				ensureInPutPsta.setString(9, matGpEntity.getActFixLen());
				ensureInPutPsta.setString(10, storeNo);
				ensureInPutPsta.setString(11, storeNo+"10101");
				ensureInPutPsta.setString(12, storeType[0].toString());
				ensureInPutPsta.setString(13, storeType[1].toString());
				ensureInPutPsta.setString(14, storeType[2].toString());
				ensureInPutPsta.setString(15, storeType[3].toString());
				ensureInPutPsta.setString(16, userName);
				ensureInPutPsta.setString(17, departId);
				ensureInPutPsta.setString(18, departMent);
				ensureInPutPsta.setString(19, mangeNo);
				ensureInPutPsta.setString(20, mangeMent);
				ensureInPutPsta.setString(21, storeNo);
				ensureInPutPsta.setString(22, storeNo+"10101");
				ensureInPutPsta.setString(23, departId);
				ensureInPutPsta.setString(24, departMent);
				ensureInPutPsta.setString(25, mangeNo);
				ensureInPutPsta.setString(26, mangeMent);
				ensureInPutPsta.setString(27, storeType[0].toString());
				ensureInPutPsta.setString(28, storeType[1].toString());
				ensureInPutPsta.setString(29, storeType[2].toString());
				ensureInPutPsta.setString(30, storeType[3].toString());
				ensureInPutPsta.setString(31, userName);
				ensureInPutPsta.setString(32, strInBound);
				ensureInPutPsta.addBatch();
			}
			ensureInPutPsta.executeBatch();
			connection.commit();
			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;
	}
	/***
	 * 撤销入库
	 * @param parm
	 * @return
	 */
	public CoreReturnObject cancelInPutData(ArrayList<String> parm)
	{
		CoreReturnObject cro = new CoreReturnObject();
        Connection connection = this.getDao(CoreConstantData.sysbeanstr)
                .getConnection();
        PreparedStatement psta = null;
        String sql = "";
        ResultSet rs = null;
        String acount = "";
        try {
			connection.setAutoCommit(false); // 取消自动提交事务
			String cancelInPutSql = CoreXmlSqlParsersFactory.getSql("FrmStuffInputStore.cancelInPutData");
			PreparedStatement cancelInPutPsta = connection.prepareStatement(cancelInPutSql);
			for(String str : parm)
			{
			   YmdGpInlistEntity ydmGpInListEntity = EntityTool.getEntityByJSONStr(str, YmdGpInlistEntity.class);
			   cancelInPutPsta.setString(1, ydmGpInListEntity.getInstockDoc());
			   cancelInPutPsta.addBatch();
			}
			cancelInPutPsta.executeBatch();
			connection.commit();
			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;
	}
	/***
	 * 查询仓库类型
	 * @param storeNo
	 * @return
	 */
	public CoreReturnObject getStoreType(String storeNo)
	{
	   return this.getDataBySqlID("FrmStuffInputStore.getStoreType", new Object[]{storeNo});	
	}
}
