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.MatGpProcessingEntity;
import com.steering.pss.ydm.Stuffmanage.Entity.MatGpTrackEntity;
import com.steering.pss.ydm.entity.MatZcMEntity;
import com.steering.pss.ydm.entity.MatZcTrackEntity;

public class FrmStuffFurnace extends DBCommon{

	/***
	 * 查询收料实绩
	 * @param parm
	 * @return
	 */
	public CoreReturnObject doQueryMat(ArrayList<String> parm)
	{
		return this.getDataBySqlID("FrmStuffFurnace.doQueryMat", parm.toArray());	
	}
	
	/***
	 * 查询组炉实绩
	 * @param parm
	 * @return
	 */
	public CoreReturnObject doQueryTrack(ArrayList<String> parm)
	{
		return this.getDataBySqlID("FrmStuffFurnace.doQueryTrack", parm.toArray());
	}
	
	/***
	 * 组炉
	 * @param parm
	 * @param stoveNo
	 * @return
	 */
	public CoreReturnObject doFurnace(ArrayList<String> parm,String judgeNo,String stoveNo,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;
        try {
			connection.setAutoCommit(false); // 取消自动提交事务
			String doFurnaceSql = CoreXmlSqlParsersFactory.getSql("FrmStuffFurnace.doFurnace");
			PreparedStatement doFurnacePsta = connection.prepareStatement(doFurnaceSql);
			for(String str : parm)
			{
				MatGpMEntity matGpEntity = EntityTool.getEntityByJSONStr(str, MatGpMEntity.class);
				doFurnacePsta.setString(1, judgeNo);
				doFurnacePsta.setString(2, matGpEntity.getStoveNo());
				doFurnacePsta.setString(3, matGpEntity.getJudgeStoveNo());
				doFurnacePsta.setString(4, matGpEntity.getGroudNo());
				doFurnacePsta.setString(5, matGpEntity.getActLenMin());
				doFurnacePsta.setString(6, matGpEntity.getActLenMax());
				doFurnacePsta.setString(7, matGpEntity.getActFixNum());
				doFurnacePsta.setString(8, matGpEntity.getActFixLen());
				doFurnacePsta.setInt(9, Integer.parseInt(matGpEntity.getFurnaceCount())+1);
				doFurnacePsta.setString(10, stoveNo);
				doFurnacePsta.setString(11, judgeNo);
				doFurnacePsta.setString(12, userName);
				doFurnacePsta.setString(13, departId);
				doFurnacePsta.setString(14, departMent);
				doFurnacePsta.setString(15, mangeNo);
				doFurnacePsta.setString(16, mangeMent);
				doFurnacePsta.setString(17, judgeNo);
				doFurnacePsta.setString(18, stoveNo);
				doFurnacePsta.addBatch();
			}
			doFurnacePsta.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 cancelFurnace(ArrayList<String> parm)
	{
		CoreReturnObject cro = new CoreReturnObject();
        Connection connection = this.getDao(CoreConstantData.sysbeanstr)
                .getConnection();
        PreparedStatement psta = null;
        String sql = "";
        ResultSet rs = null;
        try {
			connection.setAutoCommit(false); // 取消自动提交事务
		    String cancelFurnaceSql = CoreXmlSqlParsersFactory.getSql("FrmStuffFurnace.cancelFurnace");
		    PreparedStatement cancelFurnacePsta = connection.prepareStatement(cancelFurnaceSql);
		    for(String str:parm)
		    {
		    	MatGpTrackEntity matGpTrackTity = EntityTool.getEntityByJSONStr(str, MatGpTrackEntity.class);
		    	cancelFurnacePsta.setString(1, matGpTrackTity.getStoveNo());
		    	cancelFurnacePsta.setString(2, matGpTrackTity.getJudgeStoveNo());
		    	cancelFurnacePsta.setString(3, matGpTrackTity.getGroudNo());
		    	cancelFurnacePsta.setString(4, matGpTrackTity.getOldJudgeStoveNo());
		    	cancelFurnacePsta.setString(5, matGpTrackTity.getOldGroudNo());
		    	cancelFurnacePsta.setString(6, matGpTrackTity.getOldStoveNo());
		    	cancelFurnacePsta.setString(7, matGpTrackTity.getActLenMin());
		    	cancelFurnacePsta.setString(8, matGpTrackTity.getActLenMax());
		    	cancelFurnacePsta.setString(9, matGpTrackTity.getActFixNum());
		    	cancelFurnacePsta.setString(10, matGpTrackTity.getActFixLen());
		    	cancelFurnacePsta.addBatch();
		    }
		    cancelFurnacePsta.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 doSubContractor(ArrayList<String> parm,String userName)
	{
		CoreReturnObject cro = new CoreReturnObject();
        Connection connection = this.getDao(CoreConstantData.sysbeanstr)
                .getConnection();
        PreparedStatement psta = null;
        String sql = "";
        ResultSet rs = null;
        int acount = 0;
        try {
			connection.setAutoCommit(false); // 取消自动提交事务
			String insertSql =CoreXmlSqlParsersFactory.getSql("doSubContractor.insertMatProcessing");
			PreparedStatement insertPsta = connection.prepareStatement(insertSql);
			for(String str : parm)
			{
				MatGpMEntity matTity = EntityTool.getEntityByJSONStr(str, MatGpMEntity.class);
				sql = CoreXmlSqlParsersFactory.getSql("doSubContractor.selectCount");
				sql = EntityTool.fillSqlByEntity(sql, matTity);
				psta = connection.prepareStatement(sql);
				rs = psta.executeQuery();
				if(rs.next())
				{
					acount = Integer.parseInt(rs.getString("ACOUNT"));
				}
				rs.close();
				psta.close();
				if(Integer.parseInt(matTity.getActCount())>acount)
				{
					cro.setV_errMsg("外委支数不能大于库存现有支数!");
					return cro;
				}
				insertPsta.setString(1, matTity.getBugDeal());
				insertPsta.setString(2, matTity.getStoveNo());
				insertPsta.setString(3, matTity.getJudgeStoveNo());
				insertPsta.setString(4, matTity.getGroudNo());
				insertPsta.setString(5, matTity.getActLenMin());
				insertPsta.setString(6, matTity.getActLenMax());
				insertPsta.setString(7, matTity.getActFixNum());
				insertPsta.setString(8, matTity.getActFixLen());
				insertPsta.setInt(9, Integer.parseInt(matTity.getActCount())+1);
				
				insertPsta.setString(10, matTity.getBugDeal());
				insertPsta.setString(11, matTity.getMatStatus());
				insertPsta.setString(12, matTity.getProductFlag());
				insertPsta.setString(13, matTity.getStoveNo());
				insertPsta.setString(14, matTity.getJudgeStoveNo());
				insertPsta.setString(15, matTity.getGroudNo());
				insertPsta.setString(16, matTity.getBatchNo());
				insertPsta.setString(17, matTity.getCastNo());
				insertPsta.setString(18, matTity.getCcmid());
				insertPsta.setString(19, matTity.getSampleFlag());
				insertPsta.setString(20, matTity.getCuttingFlag());
				insertPsta.setString(21, matTity.getCuttingNum());
				insertPsta.setString(22, matTity.getMatOriginCode());
				insertPsta.setString(23, matTity.getMatOriginName());
				insertPsta.setString(24, matTity.getOriginMatNo());
				insertPsta.setString(25, matTity.getSoureCode());
				insertPsta.setString(26, matTity.getSoureName());
				insertPsta.setString(27, matTity.getMaterialNo());
				insertPsta.setString(28, matTity.getMaterialName());
				insertPsta.setString(29, matTity.getProduccode());
				insertPsta.setString(30, matTity.getProducname());
				insertPsta.setString(31, matTity.getJudgeGradecode());
				insertPsta.setString(32, matTity.getJudgeGradename());
				insertPsta.setString(33, matTity.getJudgeStdCode());
				insertPsta.setString(34, matTity.getJudgeStdName());
				insertPsta.setString(35, matTity.getProOrderNo());
				insertPsta.setString(36, matTity.getProOrderId());
				insertPsta.setString(37, matTity.getOrderNo());
				insertPsta.setString(38, matTity.getOrderSeq());
				insertPsta.setString(39, matTity.getOrderSeqNo());
				insertPsta.setString(40, matTity.getOrdPk());
				insertPsta.setString(41, matTity.getOrdLnPk());
				insertPsta.setString(42, matTity.getOrdLnDlyPk());
				insertPsta.setString(43, matTity.getPlineCode());
				insertPsta.setString(44, matTity.getPlineName());
				insertPsta.setString(45, matTity.getActDimater());
				insertPsta.setString(46, matTity.getActHeight());
				insertPsta.setString(47, matTity.getActLen());
				insertPsta.setString(48, matTity.getActLenMin());
				insertPsta.setString(49, matTity.getActLenMax());
				insertPsta.setString(50, matTity.getActFixNum());
				insertPsta.setString(51, matTity.getActFixLen());
				insertPsta.setString(52, matTity.getActCount());
				
				insertPsta.setString(53, matTity.getBelongCode());
				insertPsta.setString(54, matTity.getBelongName());
				insertPsta.setString(55, matTity.getBugNo());
				insertPsta.setString(56, matTity.getMemo());
				insertPsta.setString(57, userName);
				
				insertPsta.setString(58, matTity.getStoveNo());
				insertPsta.setString(59, matTity.getJudgeStoveNo());
				insertPsta.setString(60, matTity.getGroudNo());
				insertPsta.setString(61, matTity.getActLenMin());
				insertPsta.setString(62, matTity.getActLenMax());
				insertPsta.setString(63, matTity.getActFixNum());
				insertPsta.setString(64, matTity.getActFixLen());
				insertPsta.setInt(65, Integer.parseInt(matTity.getActCount())+1);
				
				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 parm
	 * @return
	 */
	public CoreReturnObject cancelSubContractor(ArrayList<String> parm)
	{
		CoreReturnObject cro = new CoreReturnObject();
        Connection connection = this.getDao(CoreConstantData.sysbeanstr)
                .getConnection();
        PreparedStatement psta = null;
        String sql = "";
        ResultSet rs = null;
        int acount = 0;
        try {
			connection.setAutoCommit(false); // 取消自动提交事务
			String cancelSql = CoreXmlSqlParsersFactory.getSql("cancelSubContractor.deleteMatGpProcessing");
			PreparedStatement cancelPsta = connection.prepareStatement(cancelSql);
			for(String str : parm)
			{
				MatGpProcessingEntity matGpProcess = EntityTool.getEntityByJSONStr(str, MatGpProcessingEntity.class);
				sql = CoreXmlSqlParsersFactory.getSql("cancelSubContractor.selectMatGpProcessing");//查询外委实绩是否回报
				psta = connection.prepareStatement(sql);
				psta.setString(1, matGpProcess.getProcessingNo());
				rs = psta.executeQuery();
				if(rs.next())
				{
					acount = Integer.parseInt(rs.getString("ACOUNT"));
				}
				rs.close();
				psta.close();
				if(acount>0)
				{
					cro.setV_errMsg("实绩已回报,不能撤销!");
					return cro;
				}
				cancelPsta.setString(1, matGpProcess.getProcessingNo());
				cancelPsta.setString(2, matGpProcess.getProcessingNo());
				cancelPsta.addBatch();
			}
			cancelPsta.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;
	}
}
