package com.steering.mes.zgmil.coup;

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

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

import com.steering.comm.db.DBCommon;
import com.steering.common.EntityTool;
import com.steering.mes.zgmil.entity.MilSizingRackEntity1;

public class FrmDispatch extends DBCommon{

	/**
	 * 查询生产炉信息
	 * @param parm
	 * @param arr
	 * @return
	 */
	public CoreReturnObject doQueryMilPlan(ArrayList<String> parm,String plineCode)
	{
		String sql = CoreXmlSqlParsersFactory.getSql("FrmDispatch.doQueryMilPlan");
		sql += " AND PLINE_CODE = '"+ plineCode +"'";
	    return this.getData(sql, parm.toArray());
	}
	/***
	 * 查询管号队列
	 * @param parm
	 * @return
	 */
	public CoreReturnObject doQueryMilSizingRack()
	{
		return this.getDataBySqlID("FrmDispatch.doQueryMilSizingRack");
	}
	
	/**
	 * 查询停机原因
	 * @return
	 */
	public CoreReturnObject doQueryBaseReason()
	{
		return this.getDataBySqlID("FrmDispatch.doQueryBaseReason");
	}
	
	/***
	 * 查询停机数据
	 * @param parm
	 * @return
	 */
	public CoreReturnObject doQueryStopData(ArrayList<String> parm)
	{
		return this.getDataBySqlID("FrmDispatch.doQueryStopData", parm.toArray());
	}
	
	/***
	 * 插入停机数据
	 * @param list
	 * @param startTim
	 * @param reaSon
	 * @return
	 */
	public CoreReturnObject stopSizingRack(ArrayList<String> list,String startTim,String reaSon,String userName,String order,String group)
	{
		CoreReturnObject cro = new CoreReturnObject();
        Connection connection = this.getDao(CoreConstantData.sysbeanstr)
                .getConnection();
        String sql  = "";
        PreparedStatement psta = null;
        ResultSet rs = null;
        int acount = 0;
        try {
			connection.setAutoCommit(false); // 取消自动提交事务
			String insertSql = CoreXmlSqlParsersFactory.getSql("FrmDispatch.stopSizingRack");
			PreparedStatement insertPsta = connection.prepareStatement(insertSql);
			MilSizingRackEntity1 milSizRack = EntityTool.getEntityByJSONStr(list.get(0), MilSizingRackEntity1.class);
			sql = CoreXmlSqlParsersFactory.getSql("FrmDispatch.selectStopData");
			psta = connection.prepareStatement(sql);
			psta.setString(1, milSizRack.getMatNo());
			rs = psta.executeQuery();
			if(rs.next())
			{
				acount = Integer.parseInt(rs.getString("ACOUNT"));
			}
			rs.close();
			psta.close();
			if(acount > 0)
			{
				cro.setV_errMsg("材料号"+milSizRack.getMatNo()+"已有停机记录,不允许重复停机!");
				return cro;
			}
			insertPsta.setString(1, milSizRack.getLotNo());
			insertPsta.setString(2, milSizRack.getStoveNo());
			insertPsta.setString(3, milSizRack.getJudgeStoveNo());
			insertPsta.setString(4, milSizRack.getPlineCode());
			insertPsta.setString(5, startTim);
			insertPsta.setString(6, reaSon);
			insertPsta.setString(7, milSizRack.getMatNo());
			insertPsta.setString(8, userName);
			insertPsta.setString(9, order);
			insertPsta.setString(10, group);
			insertPsta.setString(11, milSizRack.getResultNo());
			insertPsta.addBatch();
			
			insertPsta.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 list
	 * @return
	 */
	public CoreReturnObject deleteStopData(ArrayList<String> list)
	{
		CoreReturnObject cro = new CoreReturnObject();
        Connection connection = this.getDao(CoreConstantData.sysbeanstr)
                .getConnection();
        try {
			connection.setAutoCommit(false); // 取消自动提交事务
			String deleteSql = CoreXmlSqlParsersFactory.getSql("FrmDispatch.deleteStopData");
			PreparedStatement deletePsta = connection.prepareStatement(deleteSql);
			deletePsta.setString(1, list.get(0).toString());
			deletePsta.addBatch();
			
			deletePsta.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;
	}
}
