package com.steering.pss.ydm.pipemanage;

import CoreFS.SA06.CoreReturnObject;
import CoreFS.SA08.CoreXmlSqlParsersFactory;
import CoreFS.SC01.CoreConstantData;
import CoreFS.Util.StringUtils;
import com.steering.comm.converter.CoreConverter;
import com.steering.comm.data.ComBaseInfo;
import com.steering.comm.db.DBCommon;
import com.steering.comm.tools.EntityTool;
import com.steering.comm.tools.StringTool;
import com.steering.pss.ydm.entity.MatZcMEntity1;

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 java.util.List;

public class FrmExamineJudge extends DBCommon{

	/***
	 * 获取改判实绩
	 * @param list
	 * @return
	 */
	public CoreReturnObject getGpData(ArrayList<String> list,String[] arr,String custo)
	{
		return getGpData(list,arr,custo,"","");
	}

	public CoreReturnObject getGpData(ArrayList<String> list,String[] arr,String custom,String startTime,String endTime)
	{
		String sql = CoreXmlSqlParsersFactory.getSql("FrmExamineJudge.getGpData");
		sql += CoreConverter.toSqlConditionString("BELONG_CODE", arr);
		sql += " AND SUPPLY_UNIT_CODE = '"+custom+"'";
		if(!list.get(0).equals(""))
		{
			sql += " AND JUDGE_STOVE_NO_OLD = '"+list.get(0)+"'";
		}
		if (!StringUtils.isEmpty(startTime) && !StringUtils.isEmpty(startTime)) {
			sql += " AND CREATE_TIME >= TO_DATE('" + startTime + "','YYYY-MM-DD HH24:MI:SS')";
			sql += " AND CREATE_TIME <= TO_DATE('" + endTime + "','YYYY-MM-DD HH24:MI:SS')";
		}
		return this.getData(sql);
	}
	
	/***
	 * 提交改判
	 * @return
	 */
	public CoreReturnObject submitGpData(ArrayList<ArrayList<String>> parm)
	{
		CoreReturnObject cro = new CoreReturnObject();
        Connection connection = this.getDao(CoreConstantData.sysbeanstr)
                .getConnection();
        Date now = new Date();
        SimpleDateFormat dateFormat = new SimpleDateFormat(
                "yyyy-MM-dd HH:mm:ss");
        String systime = dateFormat.format(now);
        PreparedStatement psta = null;
        String sql = "";
        ResultSet rs = null;
        String staus = "";
        try {
        	connection.setAutoCommit(false); // 取消自动提交事务
        	String submitSql = CoreXmlSqlParsersFactory.getSql("FrmExamineJudge.submitGpData");
        	PreparedStatement submitPsta = connection.prepareStatement(submitSql);
        	for (ArrayList<String> arrayList : parm) {
        		sql =CoreXmlSqlParsersFactory.getSql("FrmExamineJudge.examineDataStaus");
        		psta = connection.prepareStatement(sql);
        		psta.setString(1, arrayList.get(0));
        		rs = psta.executeQuery();
        		if(rs.next())
        		{
        			staus = rs.getString("AGREE_FLAG");
        		}
        		rs.close();
        		psta.close();
        		if(!staus.equals("0"))
        		{
        			cro.setV_errMsg("此改判信息已提交,不允许重复提交!");
        			return cro;
        		}
				submitPsta.setString(1, arrayList.get(1));
				submitPsta.setString(2, arrayList.get(2));
				submitPsta.setString(3, arrayList.get(0));
				submitPsta.addBatch();
			}
        	submitPsta.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 noExamineData(ArrayList<ArrayList<String>> parm)
	{
		CoreReturnObject cro = new CoreReturnObject();
        Connection connection = this.getDao(CoreConstantData.sysbeanstr)
                .getConnection();
        Date now = new Date();
        SimpleDateFormat dateFormat = new SimpleDateFormat(
                "yyyy-MM-dd HH:mm:ss");
        String systime = dateFormat.format(now);
        PreparedStatement psta = null;
        String sql = "";
        String staus = "";
        ResultSet rs = null;
        try {
        	connection.setAutoCommit(false); // 取消自动提交事务
        	String argeeSql = CoreXmlSqlParsersFactory.getSql("noExamineData.updateArgeeFlag");//修改标志
        	PreparedStatement updateArgeePsta =connection.prepareStatement(argeeSql);
        	String updateLockSql = CoreXmlSqlParsersFactory.getSql("noExamineData.updateLockFlag");//释放物料
        	PreparedStatement updateLockPsta = connection.prepareStatement(updateLockSql);
        	for (ArrayList<String> arrayList : parm) {
        		sql = CoreXmlSqlParsersFactory.getSql("FrmExamineJudge.examineDataStaus");
        		psta = connection.prepareStatement(sql);
        		psta.setString(1, arrayList.get(0));
        		rs = psta.executeQuery();
        		if(rs.next())
        		{
        			staus = rs.getString("AGREE_FLAG");
        		}
        		rs.close();
        		psta.close();
        		if(staus.equals("2")||staus.equals("3"))
       		 {
       			 cro.setV_errMsg("此数据已经审批,不允许再次审批!");
       			 return cro;
       		 }
       		 if(staus.equals("0"))
       		 {
       			 cro.setV_errMsg("此改判信息未提交,不允许审批!");
       			 return cro;
       		 }
        		updateLockPsta.setString(1, arrayList.get(0));
        		updateLockPsta.addBatch();
        		
        		updateArgeePsta.setString(1, arrayList.get(1));
        		updateArgeePsta.setString(2, arrayList.get(2));
        		updateArgeePsta.setString(3, arrayList.get(0));
        		updateArgeePsta.addBatch();
			}
        	updateArgeePsta.executeBatch();
        	updateLockPsta.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 examineData(ArrayList<ArrayList<String>> parm,String custom)
	{
		CoreReturnObject cro = new CoreReturnObject();
        Connection connection = this.getDao(CoreConstantData.sysbeanstr)
                .getConnection();
        Date now = new Date();
        SimpleDateFormat dateFormat = new SimpleDateFormat(
                "yyyy-MM-dd HH:mm:ss");
        SimpleDateFormat dt = new SimpleDateFormat(
        "yyyyMM");
        SimpleDateFormat dt1 = new SimpleDateFormat(
        "yyyyMM");
        String systime = dateFormat.format(now);//当前时间
        String bathYear = dt.format(now);//结算年月
        String docTime = dt1.format(now);
        ResultSet rs = null;
        PreparedStatement psta = null;
        String sql = "";
        String outListDoc = "";//出库单号
        String inListDoc = "";//入库单号
        String matNo = "";//材料号
        String groupNo = "";//组号
		String supplyUnitCode = "";
		String supplyUnit = "";
        try {
        	connection.setAutoCommit(false); // 取消自动提交事务
        	
        	String deleteButessSql = CoreXmlSqlParsersFactory.getSql("examineData.deleteButess");//删除位置表
        	PreparedStatement deleteButessPsta = connection.prepareStatement(deleteButessSql);
        	String insertOutList = CoreXmlSqlParsersFactory.getSql("examineData.insertOutList");//写出库表(出库)
        	PreparedStatement insertOutListPsta = connection.prepareStatement(insertOutList);
        	
        	String insertOutListXs = CoreXmlSqlParsersFactory.getSql("examineDataXs.insertOutList");//写销售出库表(出库)
        	PreparedStatement insertOutListPstaXs = connection.prepareStatement(insertOutListXs);
        	
        	String insertMatSql = CoreXmlSqlParsersFactory.getSql("examineData.insertMat");//写材料表(入库)
        	PreparedStatement insertMatPsta = connection.prepareStatement(insertMatSql);
        	
        	String insertMatSqlXs = CoreXmlSqlParsersFactory.getSql("examineDataXs.insertMat");//写材料表(入库)
        	PreparedStatement insertMatPstaXs = connection.prepareStatement(insertMatSqlXs);
        	
        	String insertButessSql = CoreXmlSqlParsersFactory.getSql("examineData.insertButess");//写位置表
        	PreparedStatement insertButessPsta = connection.prepareStatement(insertButessSql);
        	
        	String insertInListSql = CoreXmlSqlParsersFactory.getSql("examineData.insertInList");//写入库表
        	PreparedStatement insertInListPsta = connection.prepareStatement(insertInListSql);
        	
        	String argeeSql = CoreXmlSqlParsersFactory.getSql("examineData.updateArgeeFlag");//回写改判信息
        	PreparedStatement updateArgeePsta =connection.prepareStatement(argeeSql);

        	if(custom.equals("120501"))
			{
				supplyUnitCode = "120501";
				supplyUnit = "天津钢管制造有限公司";
			}else if(custom.equals("120504"))
			{
				supplyUnitCode = "120504";
				supplyUnit = "江苏天淮钢管有限公司";
			}
        	for (ArrayList<String> arrayList : parm) {
        		outListDoc = querySendSqlDoc(connection, arrayList.get(7).toString(), arrayList.get(9).toString(),"OUT");//出库单号
        		int mDoc = Integer.parseInt(outListDoc.substring(outListDoc.length() - 5, outListDoc.length()));
        		
        		inListDoc = querySendSqlDoc(connection, arrayList.get(7).toString(), arrayList.get(9).toString(),"IN");//入库单号
        		int iDoc = Integer.parseInt(inListDoc.substring(inListDoc.length() - 5, inListDoc.length()));
        		String outListSeq = querySendSql(connection,outListDoc,"OUT"); //出库流水号
                int mSeq = Integer.parseInt(outListSeq.substring(outListSeq.length() - 4, outListSeq.length()));
                
                String inListSeq = querySendSql(connection,inListDoc,"IN"); //入库流水号
                int iSeq = Integer.parseInt(inListSeq.substring(inListSeq.length() - 4, inListSeq.length()));
                
        		groupNo = queryGroupNo(connection,arrayList.get(10).toString(),arrayList.get(11).toString());
        		matNo = queryMatNoM(connection, arrayList.get(10),arrayList.get(11)); //材料号
        		int mMatNo = Integer.parseInt(matNo.substring(matNo.length() - 4, matNo.length()));
				sql = CoreXmlSqlParsersFactory.getSql("examineData.selectMatNo");//查询封锁了的材料号
        		psta = connection.prepareStatement(sql);
        		psta.setString(1, parm.get(0).toString());
        		rs = psta.executeQuery();
        		List<MatZcMEntity1> matZcList = EntityTool.getEntitysByRs(rs, MatZcMEntity1.class);
        		for (MatZcMEntity1 matZcMEntity1 : matZcList) {
        			
        			insertOutListPsta.setString(1, arrayList.get(3).toString());
        			insertOutListPsta.setString(2, arrayList.get(4).toString());
        			insertOutListPsta.setString(3, matZcMEntity1.getProductFlag());
        			insertOutListPsta.setString(4, matZcMEntity1.getMatStatus());
        			insertOutListPsta.setString(5, matZcMEntity1.getJudgeStdName());
        			insertOutListPsta.setString(6, matZcMEntity1.getJudgeStdCode());
        			insertOutListPsta.setString(7, bathYear);
        			insertOutListPsta.setString(8, outListDoc
			                + StringTool.formatDecimal(mSeq, "0000"));//出库流水
        			insertOutListPsta.setString(9, arrayList.get(9).toString()+docTime+ StringTool.formatDecimal(mDoc, "00000"));//出库单号
        			insertOutListPsta.setString(10, arrayList.get(7).toString());
        			insertOutListPsta.setString(11, arrayList.get(8).toString());
        			
        			insertOutListPsta.setString(12, matZcMEntity1.getMatNo());
        			insertOutListPsta.setString(13, matZcMEntity1.getJudgeStoveNo());
        			insertOutListPsta.setString(14, matZcMEntity1.getBatchNo());
        			insertOutListPsta.setString(15, matZcMEntity1.getBatchGroudNo());
        			insertOutListPsta.setString(16, matZcMEntity1.getMaterialNo());
        			insertOutListPsta.setString(17, matZcMEntity1.getMaterialName());
        			insertOutListPsta.setString(18, matZcMEntity1.getProduccode());
        			insertOutListPsta.setString(19, matZcMEntity1.getProducname());
        			insertOutListPsta.setString(20, matZcMEntity1.getStdCode());
        			insertOutListPsta.setString(21, matZcMEntity1.getStdName());
        			insertOutListPsta.setString(22, matZcMEntity1.getGradecode());
        			insertOutListPsta.setString(23, matZcMEntity1.getGradename());
        			insertOutListPsta.setString(24, matZcMEntity1.getSteelcode());
        			insertOutListPsta.setString(25, matZcMEntity1.getSteelname());
        			insertOutListPsta.setString(26, matZcMEntity1.getStdStyle());
        			insertOutListPsta.setString(27, matZcMEntity1.getStdStyleDesc());
        			insertOutListPsta.setString(28, matZcMEntity1.getSpecCode());
        			insertOutListPsta.setString(29, matZcMEntity1.getSpecName());
        			insertOutListPsta.setString(30, matZcMEntity1.getModelCode());
        			insertOutListPsta.setString(31, matZcMEntity1.getModelDesc());
        			insertOutListPsta.setString(32, matZcMEntity1.getPlineCode());
        			insertOutListPsta.setString(33, matZcMEntity1.getPlineName());
        			insertOutListPsta.setString(34, matZcMEntity1.getActDimater());
        			insertOutListPsta.setString(35, matZcMEntity1.getActHeight());
        			insertOutListPsta.setString(36, matZcMEntity1.getActLen());
        			insertOutListPsta.setString(37, matZcMEntity1.getActLenMin());
        			insertOutListPsta.setString(38, matZcMEntity1.getActLenMax());
        			insertOutListPsta.setString(39, matZcMEntity1.getActCount());
        			insertOutListPsta.setString(40, matZcMEntity1.getActWeight());
        			insertOutListPsta.setString(41, matZcMEntity1.getActTheoryWeight());
        			
        			insertOutListPsta.setString(42, matZcMEntity1.getBelongCode());
        			insertOutListPsta.setString(43, matZcMEntity1.getBelongName());
        			insertOutListPsta.setString(44, matZcMEntity1.getStorageNo());
        			insertOutListPsta.setString(45, matZcMEntity1.getStorageNo()+"10101");
        			insertOutListPsta.setString(46, arrayList.get(5).toString());
        			insertOutListPsta.setString(47, arrayList.get(6).toString());
        			insertOutListPsta.setString(48, systime);
        			insertOutListPsta.setString(49, "1");
        			insertOutListPsta.setString(50, "1");
        			insertOutListPsta.setString(51, matZcMEntity1.getPipmemo());
        			insertOutListPsta.setString(52, "1");
        			insertOutListPsta.setString(53, arrayList.get(2).toString());
        			insertOutListPsta.setString(54, systime);
        			insertOutListPsta.setString(55,supplyUnitCode);
        			insertOutListPsta.setString(56,supplyUnit);
        			insertOutListPsta.addBatch();
        			
        			insertOutListPstaXs.setString(1, arrayList.get(3).toString());
        			insertOutListPstaXs.setString(2, arrayList.get(4).toString());
        			insertOutListPstaXs.setString(3, matZcMEntity1.getProductFlag());
        			insertOutListPstaXs.setString(4, matZcMEntity1.getMatStatus());
        			insertOutListPstaXs.setString(5, matZcMEntity1.getJudgeStdName());
        			insertOutListPstaXs.setString(6, matZcMEntity1.getJudgeStdCode());
        			insertOutListPstaXs.setString(7, bathYear);
        			insertOutListPstaXs.setString(8, outListDoc
			                + StringTool.formatDecimal(mSeq, "0000"));//出库流水
        			insertOutListPstaXs.setString(9, arrayList.get(9).toString()+docTime+ StringTool.formatDecimal(mDoc, "00000"));//出库单号
        			insertOutListPstaXs.setString(10, arrayList.get(7).toString());
        			insertOutListPstaXs.setString(11, arrayList.get(8).toString());
        			
        			insertOutListPstaXs.setString(12, matZcMEntity1.getMatNo());
        			insertOutListPstaXs.setString(13, matZcMEntity1.getJudgeStoveNo());
        			insertOutListPstaXs.setString(14, matZcMEntity1.getBatchNo());
        			insertOutListPstaXs.setString(15, matZcMEntity1.getBatchGroudNo());
        			insertOutListPstaXs.setString(16, matZcMEntity1.getMaterialNo());
        			insertOutListPstaXs.setString(17, matZcMEntity1.getMaterialName());
        			insertOutListPstaXs.setString(18, matZcMEntity1.getProduccode());
        			insertOutListPstaXs.setString(19, matZcMEntity1.getProducname());
        			insertOutListPstaXs.setString(20, matZcMEntity1.getStdCode());
        			insertOutListPstaXs.setString(21, matZcMEntity1.getStdName());
        			insertOutListPstaXs.setString(22, matZcMEntity1.getGradecode());
        			insertOutListPstaXs.setString(23, matZcMEntity1.getGradename());
        			insertOutListPstaXs.setString(24, matZcMEntity1.getSteelcode());
        			insertOutListPstaXs.setString(25, matZcMEntity1.getSteelname());
        			insertOutListPstaXs.setString(26, matZcMEntity1.getStdStyle());
        			insertOutListPstaXs.setString(27, matZcMEntity1.getStdStyleDesc());
        			insertOutListPstaXs.setString(28, matZcMEntity1.getSpecCode());
        			insertOutListPstaXs.setString(29, matZcMEntity1.getSpecName());
        			insertOutListPstaXs.setString(30, matZcMEntity1.getModelCode());
        			insertOutListPstaXs.setString(31, matZcMEntity1.getModelDesc());
        			insertOutListPstaXs.setString(32, matZcMEntity1.getPlineCode());
        			insertOutListPstaXs.setString(33, matZcMEntity1.getPlineName());
        			insertOutListPstaXs.setString(34, matZcMEntity1.getActDimater());
        			insertOutListPstaXs.setString(35, matZcMEntity1.getActHeight());
        			insertOutListPstaXs.setString(36, matZcMEntity1.getActLen());
        			insertOutListPstaXs.setString(37, matZcMEntity1.getActLenMin());
        			insertOutListPstaXs.setString(38, matZcMEntity1.getActLenMax());
        			insertOutListPstaXs.setString(39, matZcMEntity1.getActCount());
        			insertOutListPstaXs.setString(40, matZcMEntity1.getActWeight());
        			insertOutListPstaXs.setString(41, matZcMEntity1.getActTheoryWeight());
        			
        			insertOutListPstaXs.setString(42, matZcMEntity1.getBelongCode());
        			insertOutListPstaXs.setString(43, matZcMEntity1.getBelongName());
        			insertOutListPstaXs.setString(44, matZcMEntity1.getStorageNo());
        			insertOutListPstaXs.setString(45, matZcMEntity1.getStorageNo()+"10101");
        			insertOutListPstaXs.setString(46, arrayList.get(5).toString());
        			insertOutListPstaXs.setString(47, arrayList.get(6).toString());
        			insertOutListPstaXs.setString(48, systime);
        			insertOutListPstaXs.setString(49, "1");
        			insertOutListPstaXs.setString(50, "1");
        			insertOutListPstaXs.setString(51, matZcMEntity1.getPipmemo());
        			insertOutListPstaXs.setString(52, "1");
        			insertOutListPstaXs.setString(53, arrayList.get(2).toString());
        			insertOutListPstaXs.setString(54, systime);
        			insertOutListPstaXs.setString(55,supplyUnitCode);
        			insertOutListPstaXs.setString(56,supplyUnit);
        			insertOutListPstaXs.addBatch();
        			
        			
        			insertMatPsta.setString(1, matZcMEntity1.getJudgeStoveNo());
        			insertMatPsta.setString(2, matZcMEntity1.getBatchNo());
        			insertMatPsta.setString(3, groupNo);
        			insertMatPsta.setString(4, arrayList.get(12));
        			insertMatPsta.setString(5, arrayList.get(13));
        			insertMatPsta.setString(6, arrayList.get(14));
        			insertMatPsta.setString(7, arrayList.get(15));
        			insertMatPsta.setString(8, arrayList.get(16));
        			insertMatPsta.setString(9, arrayList.get(17));
        			insertMatPsta.setString(10, arrayList.get(18));
        			insertMatPsta.setString(11, arrayList.get(19));
        			insertMatPsta.setString(12, matZcMEntity1.getStdCode());
        			insertMatPsta.setString(13, matZcMEntity1.getStdName());
        			insertMatPsta.setString(14, matZcMEntity1.getBelongCode());
        			insertMatPsta.setString(15, matZcMEntity1.getBelongName());
        			insertMatPsta.setString(16, arrayList.get(20));
        			insertMatPsta.setString(17,arrayList.get(21));
        			insertMatPsta.setString(18, matZcMEntity1.getActDimater());
        			insertMatPsta.setString(19, matZcMEntity1.getActHeight());
        			insertMatPsta.setString(20, matZcMEntity1.getLenNo());
        			insertMatPsta.setString(21, matZcMEntity1.getLenDesc());
        			insertMatPsta.setString(22, matZcMEntity1.getLenUnit());
        			insertMatPsta.setString(23, matZcMEntity1.getActLenMin());
        			insertMatPsta.setString(24, matZcMEntity1.getActLenMax());
        			insertMatPsta.setString(25, arrayList.get(2));
        			insertMatPsta.setString(26, matZcMEntity1.getActCount());
        			insertMatPsta.setString(27, matZcMEntity1.getActWeight());
        			insertMatPsta.setString(28, systime);
        			insertMatPsta.setString(29, systime);
        			insertMatPsta.setString(30, (arrayList.get(10) + arrayList.get(11))
			                + StringTool.formatDecimal(mMatNo, "0000"));
        			insertMatPsta.setString(31, matZcMEntity1.getMemo());
        			insertMatPsta.setString(32, "1");
        			insertMatPsta.setString(33, "1");
        			insertMatPsta.setString(34, matZcMEntity1.getActLen());
        			insertMatPsta.setString(35, matZcMEntity1.getJudgeStdName());
        			insertMatPsta.setString(36, bathYear);
        			insertMatPsta.setString(37, matZcMEntity1.getPlineCode());
        			insertMatPsta.setString(38, matZcMEntity1.getPlineName());
        			insertMatPsta.setString(39, matZcMEntity1.getGradecode());
        			insertMatPsta.setString(40, matZcMEntity1.getGradename());
        			insertMatPsta.setString(41, matZcMEntity1.getPipmemo());
        			insertMatPsta.setString(42, matZcMEntity1.getOldOrderNo());
        			insertMatPsta.setString(43, matZcMEntity1.getJudgeResultTime());
        			insertMatPsta.setString(44, matZcMEntity1.getMatOriginCode());
        			insertMatPsta.setString(45, matZcMEntity1.getMatOriginName());
        			insertMatPsta.setString(46, matZcMEntity1.getSoureCode());
        			insertMatPsta.setString(47, matZcMEntity1.getSoureName());
        			insertMatPsta.setString(48, arrayList.get(22));
        			insertMatPsta.setString(49, arrayList.get(23));
        			insertMatPsta.setString(50, matZcMEntity1.getLotNo());
        			insertMatPsta.setString(51, matZcMEntity1.getStoveNo());
        			insertMatPsta.setString(52, matZcMEntity1.getZbsFlag());
        			insertMatPsta.setString(53, matZcMEntity1.getZbsId());
        			insertMatPsta.setString(54,supplyUnitCode);
        			insertMatPsta.setString(55,supplyUnit);
        			
        			insertMatPsta.addBatch();
        			
        			insertMatPstaXs.setString(1, matZcMEntity1.getJudgeStoveNo());
        			insertMatPstaXs.setString(2, matZcMEntity1.getBatchNo());
        			insertMatPstaXs.setString(3, groupNo);
        			insertMatPstaXs.setString(4, arrayList.get(12));
        			insertMatPstaXs.setString(5, arrayList.get(13));
        			insertMatPstaXs.setString(6, arrayList.get(14));
        			insertMatPstaXs.setString(7, arrayList.get(15));
        			insertMatPstaXs.setString(8, arrayList.get(16));
        			insertMatPstaXs.setString(9, arrayList.get(17));
        			insertMatPstaXs.setString(10, arrayList.get(18));
        			insertMatPstaXs.setString(11, arrayList.get(19));
        			insertMatPstaXs.setString(12, matZcMEntity1.getStdCode());
        			insertMatPstaXs.setString(13, matZcMEntity1.getStdName());
        			insertMatPstaXs.setString(14, matZcMEntity1.getBelongCode());
        			insertMatPstaXs.setString(15, matZcMEntity1.getBelongName());
        			insertMatPstaXs.setString(16, arrayList.get(20));
        			insertMatPstaXs.setString(17,arrayList.get(21));
        			insertMatPstaXs.setString(18, matZcMEntity1.getActDimater());
        			insertMatPstaXs.setString(19, matZcMEntity1.getActHeight());
        			insertMatPstaXs.setString(20, matZcMEntity1.getLenNo());
        			insertMatPstaXs.setString(21, matZcMEntity1.getLenDesc());
        			insertMatPstaXs.setString(22, matZcMEntity1.getLenUnit());
        			insertMatPstaXs.setString(23, matZcMEntity1.getActLenMin());
        			insertMatPstaXs.setString(24, matZcMEntity1.getActLenMax());
        			insertMatPstaXs.setString(25, arrayList.get(2));
        			insertMatPstaXs.setString(26, matZcMEntity1.getActCount());
        			insertMatPstaXs.setString(27, matZcMEntity1.getActWeight());
        			insertMatPstaXs.setString(28, systime);
        			insertMatPstaXs.setString(29, systime);
        			insertMatPstaXs.setString(30, (arrayList.get(10) + arrayList.get(11))
			                + StringTool.formatDecimal(mMatNo, "0000"));
        			insertMatPstaXs.setString(31, matZcMEntity1.getMemo());
        			insertMatPstaXs.setString(32, "1");
        			insertMatPstaXs.setString(33, "1");
        			insertMatPstaXs.setString(34, matZcMEntity1.getActLen());
        			insertMatPstaXs.setString(35, matZcMEntity1.getJudgeStdName());
        			insertMatPstaXs.setString(36, bathYear);
        			insertMatPstaXs.setString(37, matZcMEntity1.getPlineCode());
        			insertMatPstaXs.setString(38, matZcMEntity1.getPlineName());
        			insertMatPstaXs.setString(39, matZcMEntity1.getGradecode());
        			insertMatPstaXs.setString(40, matZcMEntity1.getGradename());
        			insertMatPstaXs.setString(41, matZcMEntity1.getPipmemo());
        			insertMatPstaXs.setString(42, matZcMEntity1.getOldOrderNo());
        			insertMatPstaXs.setString(43, matZcMEntity1.getJudgeResultTime());
        			insertMatPstaXs.setString(44, matZcMEntity1.getMatOriginCode());
        			insertMatPstaXs.setString(45, matZcMEntity1.getMatOriginName());
        			insertMatPstaXs.setString(46, matZcMEntity1.getSoureCode());
        			insertMatPstaXs.setString(47, matZcMEntity1.getSoureName());
        			insertMatPstaXs.setString(48, arrayList.get(22));
        			insertMatPstaXs.setString(49, arrayList.get(23));
        			insertMatPstaXs.setString(50, matZcMEntity1.getLotNo());
        			insertMatPstaXs.setString(51, matZcMEntity1.getStoveNo());
        			insertMatPstaXs.setString(52, matZcMEntity1.getZbsFlag());
        			insertMatPstaXs.setString(53, matZcMEntity1.getZbsId());
        			insertMatPstaXs.setString(54,supplyUnitCode);
        			insertMatPstaXs.setString(55,supplyUnit);
        			
        			insertMatPstaXs.addBatch();
        			
        			
        			insertButessPsta.setString(1, matZcMEntity1.getJudgeStoveNo());
        			insertButessPsta.setString(2, matZcMEntity1.getBatchNo());
        			insertButessPsta.setString(3, groupNo);
        			insertButessPsta.setString(4, matZcMEntity1.getStorageNo());
        			insertButessPsta.setString(5, matZcMEntity1.getStorageNo()+"10101");
        			insertButessPsta.setString(6, matZcMEntity1.getStorageTypeNo());
        			insertButessPsta.setString(7, matZcMEntity1.getStorageTypeName());
        			insertButessPsta.setString(8, arrayList.get(2));
        			insertButessPsta.setString(9, (arrayList.get(10) + arrayList.get(11))
			                + StringTool.formatDecimal(mMatNo, "0000"));
        			insertButessPsta.setString(10, systime);
        			insertButessPsta.setString(11, arrayList.get(5));
        			insertButessPsta.setString(12, arrayList.get(6));
        			insertButessPsta.setString(13, arrayList.get(3));
        			insertButessPsta.setString(13, arrayList.get(4));
        			insertButessPsta.addBatch();
        			
        			
        			
        			mSeq++;
        			mMatNo++;
				}
        		updateArgeePsta.setString(1, arrayList.get(1));
        		updateArgeePsta.setString(2, arrayList.get(2));
        		updateArgeePsta.setString(3, arrayList.get(0));
        		updateArgeePsta.addBatch();
        		mDoc++;
			}
        	updateArgeePsta.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;
	}
	
	
	public CoreReturnObject examineData1(ArrayList<ArrayList<String>> parm,String custom)
	{
		CoreReturnObject cro = new CoreReturnObject();
        Connection connection = this.getDao(CoreConstantData.sysbeanstr)
                .getConnection();
        String qcmSql = "";
        PreparedStatement qcmPsta = null;
        ResultSet rs = null;
        String staus = "";
        String supplyUnitCode = "";
        String supplyUnit = "";
		Date now = new Date();
		SimpleDateFormat dateFormat = new SimpleDateFormat(
				"yyyy-MM-dd HH:mm:ss");
		String yearMoth = "120504".equals(custom)? ComBaseInfo.GetBalYear_2416(now): ComBaseInfo.GetBalYear(now);
		String systime = dateFormat.format(now);
        try {
        	connection.setAutoCommit(false); // 取消自动提交事务
        	String sql = CoreXmlSqlParsersFactory.getSql("FrmExamineJudge.examineData_R");
        	PreparedStatement psta = connection.prepareStatement(sql);
        	
        	String sqlXs = CoreXmlSqlParsersFactory.getSql("FrmExamineJudgeXs.examineData_R");
        	PreparedStatement pstaXs = connection.prepareStatement(sqlXs);
        	
        	String sql1 = CoreXmlSqlParsersFactory.getSql("FrmExamineJudge.examineData1_");
        	PreparedStatement psta1 = connection.prepareStatement(sql1);
        	
        	String sql1Xs = CoreXmlSqlParsersFactory.getSql("FrmExamineJudgeXs.examineData1_");
        	PreparedStatement psta1Xs = connection.prepareStatement(sql1Xs);
        	
			if(custom.equals("120501"))
			{
				supplyUnitCode = "120501";
				supplyUnit = "天津钢管制造有限公司";
			}else if(custom.equals("120504"))
			{
				supplyUnitCode = "120504";
				supplyUnit = "江苏天淮钢管有限公司";
			}
        	for (ArrayList<String> arrayList : parm) {
        		 
        		 qcmSql = CoreXmlSqlParsersFactory.getSql("FrmExamineJudge.examineDataStaus");//获取数据状态
        		 qcmPsta = connection.prepareStatement(qcmSql);
        		 qcmPsta.setString(1, arrayList.get(0));
        		 rs = qcmPsta.executeQuery();
        		 if(rs.next())
        		 {
        			 staus = rs.getString("AGREE_FLAG");
        		 }
        		 rs.close();
        		 qcmPsta.close();
        		 if(staus.equals("2")||staus.equals("3"))
        		 {
        			 cro.setV_errMsg("此数据已经审批,不允许再次审批!");
        			 return cro;
        		 }
        		 if(staus.equals("0"))
        		 {
        			 cro.setV_errMsg("此改判信息未提交,不允许审批!");
        			 return cro;
        		 }


        		  //定义参数
				  psta.setObject(1, arrayList.get(16));
        	      psta.setObject(2, arrayList.get(24));
        	      psta.setObject(3, arrayList.get(24));
        	      psta.setObject(4, arrayList.get(10));
        	      psta.setObject(5, arrayList.get(11));
        	      psta.setObject(6, arrayList.get(10));
        	      psta.setObject(7, arrayList.get(11));
        	      psta.setObject(8, arrayList.get(10));
        	      psta.setObject(9, arrayList.get(11));
        	      psta.setObject(10, arrayList.get(0));
        	      
        	      pstaXs.setObject(1, arrayList.get(16));
        	      pstaXs.setObject(2, arrayList.get(24));
        	      pstaXs.setObject(3, arrayList.get(24));
        	      pstaXs.setObject(4, arrayList.get(10));
        	      pstaXs.setObject(5, arrayList.get(11));
        	      pstaXs.setObject(6, arrayList.get(10));
        	      pstaXs.setObject(7, arrayList.get(11));
        	      pstaXs.setObject(8, arrayList.get(10));
        	      pstaXs.setObject(9, arrayList.get(11));
        	      pstaXs.setObject(10, arrayList.get(0));
        	      
        	   
        	      //材料表参数
        	      psta.setObject(11, arrayList.get(12));
        	      psta.setObject(12, arrayList.get(13));
        	      psta.setObject(13, arrayList.get(14));
        	      psta.setObject(14, arrayList.get(15));
        	      psta.setObject(15, arrayList.get(16));
        	      psta.setObject(16, arrayList.get(17));
        	      psta.setObject(17, arrayList.get(18));
        	      psta.setObject(18, arrayList.get(19));
        	      psta.setObject(19, arrayList.get(20));
        	      psta.setObject(20, arrayList.get(21));
        	      psta.setObject(21, arrayList.get(2));
        	      psta.setObject(22, arrayList.get(22));
        	      psta.setObject(23, arrayList.get(23));
        	      
        	      //材料表参数
        	      pstaXs.setObject(11, arrayList.get(12));
        	      pstaXs.setObject(12, arrayList.get(13));
        	      pstaXs.setObject(13, arrayList.get(14));
        	      pstaXs.setObject(14, arrayList.get(15));
        	      pstaXs.setObject(15, arrayList.get(16));
        	      pstaXs.setObject(16, arrayList.get(17));
        	      pstaXs.setObject(17, arrayList.get(18));
        	      pstaXs.setObject(18, arrayList.get(19));
        	      pstaXs.setObject(19, arrayList.get(20));
        	      pstaXs.setObject(20, arrayList.get(21));
        	      pstaXs.setObject(21, arrayList.get(2));
        	      pstaXs.setObject(22, arrayList.get(22));
        	      pstaXs.setObject(23, arrayList.get(23));
        	      
        	      //CRM
        	      psta.setObject(24,arrayList.get(22));
				psta.setObject(25,arrayList.get(23));
				psta.setObject(26,arrayList.get(12));
				psta.setObject(27,arrayList.get(13));
				psta.setObject(28,arrayList.get(14));
				psta.setObject(29,arrayList.get(15));
				psta.setObject(30,arrayList.get(16));
				psta.setObject(31,arrayList.get(17));
				psta.setObject(32,arrayList.get(18));
				psta.setObject(33,arrayList.get(19));
				psta.setObject(34,arrayList.get(20));
				psta.setObject(35,arrayList.get(21));



//        	      psta.setObject(35,arrayList.get(22));
//				psta.setObject(36,arrayList.get(23));
//				psta.setObject(37,arrayList.get(12));
//				psta.setObject(38,arrayList.get(13));
//				psta.setObject(39,arrayList.get(14));
//				psta.setObject(40,arrayList.get(15));
//				psta.setObject(41,arrayList.get(16));
//				psta.setObject(42,arrayList.get(17));
//				psta.setObject(43,arrayList.get(18));
//				psta.setObject(44,arrayList.get(19));
//				psta.setObject(45,arrayList.get(20));
//				psta.setObject(46,arrayList.get(21));
//				psta.setObject(47,systime);
//				psta.setObject(48,systime);


        	      //位置表参数
        	      psta.setObject(36, arrayList.get(2));
        	      psta.setObject(37, arrayList.get(5));
        	      psta.setObject(38, arrayList.get(6));
        	      psta.setObject(39, arrayList.get(3));
        	      psta.setObject(40, arrayList.get(4));
        	      //入库表参数
        	      psta.setObject(41, arrayList.get(5));
        	      psta.setObject(42, arrayList.get(6));
        	      psta.setObject(43, arrayList.get(24));
        	      psta.setObject(44, arrayList.get(25));
        	      psta.setObject(45, arrayList.get(12));//品名
        	      psta.setObject(46, arrayList.get(13));
        	      psta.setObject(47, arrayList.get(14));//钢级
        	      psta.setObject(48, arrayList.get(15));
        	      psta.setObject(49, arrayList.get(20));//标准类别
        	      psta.setObject(50, arrayList.get(21));
        	      psta.setObject(51, arrayList.get(16));//规格
        	      psta.setObject(52, arrayList.get(17));
        	      psta.setObject(53, arrayList.get(18));//扣型
        	      psta.setObject(54, arrayList.get(19));
        	      psta.setObject(55, arrayList.get(2));
        	      psta.setObject(56, arrayList.get(3));//部门
        	      psta.setObject(57, arrayList.get(4));
        	      psta.setObject(58,yearMoth);
        	      psta.setObject(59, arrayList.get(22));//物料
        	      psta.setObject(60, arrayList.get(23));
        	      
        	      //入库表参数
        	      pstaXs.setObject(24, arrayList.get(5));
        	      pstaXs.setObject(25, arrayList.get(6));
        	      pstaXs.setObject(26, arrayList.get(24));
        	      pstaXs.setObject(27, arrayList.get(25));
        	      pstaXs.setObject(28, arrayList.get(12));//品名
        	      pstaXs.setObject(29, arrayList.get(13));
        	      pstaXs.setObject(30, arrayList.get(14));//钢级
        	      pstaXs.setObject(31, arrayList.get(15));
        	      pstaXs.setObject(32, arrayList.get(20));//标准类别
        	      pstaXs.setObject(33, arrayList.get(21));
        	      pstaXs.setObject(34, arrayList.get(16));//规格
        	      pstaXs.setObject(35, arrayList.get(17));
        	      pstaXs.setObject(36, arrayList.get(18));//扣型
        	      pstaXs.setObject(37, arrayList.get(19));
        	      pstaXs.setObject(38, arrayList.get(2));
        	      pstaXs.setObject(39, arrayList.get(3));//部门
        	      pstaXs.setObject(40, arrayList.get(4));
        	      pstaXs.setObject(41,yearMoth);
        	      pstaXs.setObject(42, arrayList.get(22));//物料
        	      pstaXs.setObject(43, arrayList.get(23));
        	      
        	      //末尾参数
        	      psta.setObject(61, arrayList.get(1));//审批说明
        	      psta.setObject(62, arrayList.get(2));//判定人
        	      psta.setObject(63, arrayList.get(0));//判定ID
        	      psta.addBatch();
        	      
        	      psta1.setObject(1, arrayList.get(7));
        		  psta1.setObject(2, arrayList.get(7));
        		  psta1.setObject(3, arrayList.get(0));
        		  psta1.setObject(4,systime);
				  psta1.setObject(5,systime);
        		  psta1.setObject(6, arrayList.get(3));
        	      psta1.setObject(7, arrayList.get(4));
        	      psta1.setObject(8,yearMoth);
        	      psta1.setObject(9, arrayList.get(7));
        	      psta1.setObject(10, arrayList.get(8));
        	      psta1.setObject(11, arrayList.get(5).toString());
        	      psta1.setObject(12, arrayList.get(6).toString());
        	      psta1.setObject(13, arrayList.get(2));
        	      psta1.addBatch();
        	      
        	      psta1Xs.setObject(1, arrayList.get(7));
        		  psta1Xs.setObject(2, arrayList.get(7));
        		  psta1Xs.setObject(3, arrayList.get(0));
        		  psta1Xs.setObject(4, arrayList.get(3));
        	      psta1Xs.setObject(5, arrayList.get(4));
        	      psta1Xs.setObject(6,yearMoth);
        	      psta1Xs.setObject(7, arrayList.get(7));
        	      psta1Xs.setObject(8, arrayList.get(8));
        	      psta1Xs.setObject(9, arrayList.get(5).toString());
        	      psta1Xs.setObject(10, arrayList.get(6).toString());
        	      psta1Xs.setObject(11, arrayList.get(2));
        	      psta1Xs.addBatch();
        	      
        	}
        	psta.executeBatch();
        	psta1.executeBatch();
        	pstaXs.executeBatch();
        	psta1Xs.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 conn c
     * @param strlist 传递参数
     * @return obj
     * @throws Exception 异常处理
     */
    public String querySendSqlDoc(Connection conn, String strlist,String memo,String flag)
            throws Exception {
        int seq = 0;
         PreparedStatement psta = null;
         Date dt = new Date();
         String sqlLovsqe = "";
         SimpleDateFormat matter1 = new SimpleDateFormat("yyyy-MM-dd");
         SimpleDateFormat matter2 = new SimpleDateFormat("yyMMdd");
         if(flag.equals("OUT"))
         {
			sqlLovsqe = CoreXmlSqlParsersFactory
					.getSql("FrmExamineJudge.getOutDoc");
         }else if(flag.equals("IN"))
         {
        	 sqlLovsqe = CoreXmlSqlParsersFactory
				.getSql("CorePipeInTemp.getInListDoc");
         }
        psta = conn.prepareStatement(sqlLovsqe);
        psta.setString(1, matter1.format(dt));
        psta.setString(2, strlist);
        ResultSet rs = psta.executeQuery();
        if (rs.next()) {
            String res = rs.getString(1);
            if (res == null) {
                res = memo + matter2.format(dt) + "00001";
            }
            else {
                seq = Integer.parseInt(res.substring(8, 13));
            }
        }
        rs.close();
        psta.close();
        return  memo + matter2.format(dt) + StringTool.formatDecimal(++seq, "00000");
    }
    
    /**
     * 每支材料对应的出库流水号
     * @param conn c
     * @param strlist 传递参数
     * @return obj
     * @throws Exception 异常处理
     */
    public String querySendSql(Connection conn, String strlist,String flag)
            throws Exception {
        int seq = 0;
         PreparedStatement psta = null;
         Date dt = new Date();
         SimpleDateFormat matter1 = new SimpleDateFormat("yyyy-MM-dd");
         String sqlLovsqe = "";
         if(flag.equals("OUT"))
         {
			sqlLovsqe = CoreXmlSqlParsersFactory
					.getSql("FrmExamineJudge.getOutDoc1");
         }else if(flag.equals("IN"))
         {
        	 sqlLovsqe = CoreXmlSqlParsersFactory
				.getSql("CorePipeInTemp.getInListDoc1");
         }
        psta = conn.prepareStatement(sqlLovsqe);
        psta.setString(1, matter1.format(dt));
        psta.setString(2, strlist);
        ResultSet rs = psta.executeQuery();
        if (rs.next()) {
            String res = rs.getString(1);
            if (res == null) {
                res = strlist + "0001";
            }
            else {
                seq = Integer.parseInt(res.substring(13, 17));
            }
        }
        rs.close();
        psta.close();
        return strlist + StringTool.formatDecimal(++seq, "0000");
    }
    
    /**
     * 根据炉号、批号自动组号
     * @param conn c
     * @param strLcode 传递参数
     * @return obj
     * @throws Exception 异常处理
     */
    public String queryGroupNo(Connection conn, String strLcode, String strBatch)
            throws Exception {
        int count = 0;
        PreparedStatement psta = null;
        String sqlLovsqe = CoreXmlSqlParsersFactory
                .getSql("FrmExamineJudge.queryGroupNo");
        psta = conn.prepareStatement(sqlLovsqe);
        psta.setString(1, strLcode);
        psta.setString(2, strBatch);
        ResultSet rs = psta.executeQuery();
        if (rs.next()) {
            String res = rs.getString(1);
            if (res == null) {
                res = "001";
            }
            else {
                count = Integer.parseInt(res.substring(res.length() - 3, res
                        .length()));
            }
        }
        rs.close();
        psta.close();
        return StringTool.formatDecimal(++count, "000");
    }
    
    /**
     * 根据炉号、批号自动生成材料号
     * @param conn c
     * @param strLcode 传递参数
     * @return obj
     * @throws Exception 异常处理
     */
    public String queryMatNoM(Connection conn, String strLcode, String strBatch)
            throws Exception {
        int count = 0;
        PreparedStatement psta = null;
        String sqlLovsqe = CoreXmlSqlParsersFactory
                .getSql("FrmExamineJudge.QueryMatNoM");
        psta = conn.prepareStatement(sqlLovsqe);
        psta.setString(1, strLcode);
        psta.setString(2, strBatch);
        ResultSet rs = psta.executeQuery();
        if (rs.next()) {
            String res = rs.getString(1);
            if (res == null) {
                res = strLcode + strBatch + "0001";
            }
            else {
                count = Integer.parseInt(res.substring(res.length() - 4, res
                        .length()));
            }
        }
        rs.close();
        psta.close();
        return (strLcode + strBatch)
                + StringTool.formatDecimal(++count, "0000");
    }
}
