package com.steering.mes.mcp.Vrp;

import CoreFS.SA06.CoreReturnObject;
import CoreFS.SA08.CoreXmlSqlParsersFactory;
import CoreFS.SC01.CoreConstantData;
import com.steering.comm.converter.EntityTool;
import com.steering.comm.db.DBCommon;
import com.steering.mes.mcp.common.PlanService;
import com.steering.mes.mcp.entity.PlnZyDbkCEntity;
import com.steering.mes.mcp.entity.PlnZyDbkMEntity;
import com.steering.mes.mcp.entity.PortVrpBatchSampleResultEntity;
import com.steering.mes.mcp.entity.VrpMeasuringResultEntity;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.text.SimpleDateFormat;
import java.util.*;

/**
 * 镦拔扩测量实绩
 * @author lzh 2015-7-20
 */
public class FrmVrpMeasuring extends DBCommon {
    /**
     * 测量新增
     * @param load
     * @return
     */
    public CoreReturnObject saveMeasuringResult(VrpMeasuringResultEntity load,
            String slgx, String plinCode,String groupNo) {
        CoreReturnObject core = new CoreReturnObject();
        Connection connection = this.getDao(CoreConstantData.sysbeanstr)
                .getConnection();
        PreparedStatement psta = null;
        Date dt = new Date();
        SimpleDateFormat matter1 = new SimpleDateFormat("yyMMdd");
        SimpleDateFormat matter = new SimpleDateFormat("yyyy-MM-dd");
        SimpleDateFormat matter2 = new SimpleDateFormat("yyyyMMddHHmm");
        Calendar ca = Calendar.getInstance();
        ca.set(Calendar.DATE, ca.getActualMaximum(Calendar.DATE));
        Date now = new Date();
        SimpleDateFormat dateFormat = new SimpleDateFormat(
                "yyyy-MM-dd HH:mm:ss", Locale.CHINA);
        String systime = dateFormat.format(now);
        try {
            connection.setAutoCommit(false); // 取消自动提交事务
            String sqlAdd = CoreXmlSqlParsersFactory
                    .getSql("FrmVrpMeasuring.AddVrpMeasuring");// 新增测量实绩
            PreparedStatement patAdd = connection.prepareStatement(sqlAdd);
            String addPlnInfo = CoreXmlSqlParsersFactory
                    .getSql("FrmVrpExpandResult.AddComInfoResult");// 新增公共信息
            PreparedStatement patsPlan = connection
                    .prepareStatement(addPlnInfo);
            String addPlnInfo1 = CoreXmlSqlParsersFactory
                    .getSql("FrmVrpMeasuring.AddComInfoResult1");// 查询材料状态和工序点编码
            PreparedStatement patsPlan1 = connection
                    .prepareStatement(addPlnInfo1);
            String updatePath = CoreXmlSqlParsersFactory
                    .getSql("FrmVrpMeasuring.UpdatePath");// 修改队列表工艺路径
            PreparedStatement pastupdatePath = connection
                    .prepareStatement(updatePath);
            String sqlupdate = CoreXmlSqlParsersFactory
                    .getSql("FrmVrpMeasuring.UPDATEDbkPlan");// 修改镦拔扩计划表工序开始状态
            PreparedStatement patupdate = connection
                    .prepareStatement(sqlupdate);
            String addConLoag = CoreXmlSqlParsersFactory
                    .getSql("FrmVrpExpandResult.AddConLoag");// 新增调度信息
            PreparedStatement pastAddLoag = connection
                    .prepareStatement(addConLoag);
            // 获取镦拔扩计划实体类
            PlnZyDbkMEntity pln = PlanService.getSendDBKPlanCInfo(connection,
                    load.getHeatPlanNo());
            // 获取镦拔扩计划从表实体类
            PlnZyDbkCEntity plnC = PlanService.getSendDBKPlanCInfoC(connection,
                    load.getHeatPlanNo());
            if (pln == null)
                return core;
            if (!pln.getPlanstatus().equals("20")) {
                core.setV_errMsg("该计划不是计划中状态！");
                return core;
            }

            String resultNo = "";
            String sqlloading = CoreXmlSqlParsersFactory
                    .getSql("FrmVrpMeasuring.queryMeasuringKEY");// 查询最大实绩编号
            psta = connection.prepareStatement(sqlloading);
            psta.setObject(1, plinCode + slgx + matter1.format(dt));
            psta.setObject(2, matter.format(dt));
            psta.setObject(3, pln.getPlineCode());
            ResultSet rs1 = psta.executeQuery();
            if (rs1.next()) {
                resultNo = rs1.getString("RESULT_NO");
            }
            rs1.close();
            psta.close();
            // 新增测量实绩
            patAdd.setObject(1, resultNo);
            patAdd.setObject(2, pln.getHeatPlanNo());
            patAdd.setObject(3, pln.getZyBatchId());
            patAdd.setObject(4, pln.getProPlanId());
            patAdd.setObject(5, pln.getGxPlanNo());
            patAdd.setObject(6, pln.getPlineCode());
            patAdd.setObject(7, pln.getPlineName());
            patAdd.setObject(8, load.getProNum());
            patAdd.setObject(9, load.getProWt());
            patAdd.setObject(10, load.getFailNum());
            patAdd.setObject(11, load.getFailWt());
            patAdd.setObject(12, load.getOfflineNum());
            patAdd.setObject(13, load.getOfflineWt());
            patAdd.setObject(14, load.getReNum());
            patAdd.setObject(15, load.getReWt());
            patAdd.setObject(16, "");// 监制要求
            patAdd.setObject(17, pln.getIfMonitor());
            patAdd.setObject(18, load.getProducer());
            patAdd.setObject(19, pln.getPrdcrNo());
            if (load.getProducer().equals("")) {
                patAdd.setObject(20, "");
            }
            else {
                patAdd.setObject(20, load.getProducerTime());
            }
            patAdd.setObject(21, load.getLoadTime());
            patAdd.setObject(22, load.getLoadShift());
            patAdd.setObject(23, load.getLoadGroup());
            patAdd.setObject(24, load.getLoadUser());
            patAdd.setObject(25, "1");
            patAdd.setObject(26, systime);
            patAdd.setObject(27, load.getAccNum());
            patAdd.setObject(28, load.getAccWt());
            patAdd.setObject(29, load.getFailReason());
            if(!load.getFailNum().equals("0"))
            {
            	if(load.getFailReason()==null||load.getFailReason().equals(""))
            	{
            		core.setV_errMsg("有废品必须填写废品原因!");
                	return core;
            	}
            }
            patAdd.addBatch();
            // 新增公共信息
            patsPlan.setObject(1, resultNo);
            patsPlan.setObject(2, pln.getHeatPlanNo());
            patsPlan.setObject(3, slgx);
            patsPlan.setObject(4, slgx);
            patsPlan.setObject(5, pln.getPlineCode());
            patsPlan.setObject(6, pln.getPlineName());
            patsPlan.setObject(7, pln.getZyBatchId());
            patsPlan.setObject(8, pln.getProPlanId());
            patsPlan.setObject(9, pln.getGxPlanNo());
            patsPlan.setObject(10, pln.getIfMonitor());
            patsPlan.setObject(11, pln.getPrdcrNo());
            patsPlan.setObject(12, "");// 监制要求
            patsPlan.setObject(13, load.getProducer());
            if (load.getProducer().equals("")) {
                patsPlan.setObject(14, "");
            }
            else {
                patsPlan.setObject(14, load.getProducerTime());
            }
            patsPlan.setObject(15, pln.getCraftFileNo());
            patsPlan.setObject(16, pln.getCraftFileMan());
            patsPlan.setObject(17, pln.getCraftSeq());
            patsPlan.setObject(18, pln.getJudgeStoveNo());
            patsPlan.setObject(19, plnC.getBatchNo());
            patsPlan.setObject(20, groupNo);// 组号
            patsPlan.setObject(21, pln.getGradecode());
            patsPlan.setObject(22, pln.getGradename());
            patsPlan.setObject(23, pln.getLengthIn());
            patsPlan.setObject(24, pln.getOutdiameterIn());
            patsPlan.setObject(25, pln.getAimlength());
            patsPlan.setObject(26, pln.getWallthickIn());
            patsPlan.setObject(27, pln.getLengthminIn());
            patsPlan.setObject(28, pln.getLengthmaxIn());
            patsPlan.setObject(29, pln.getLenCuthead());
            patsPlan.setObject(30, pln.getLenCutend());
            patsPlan.setObject(31, pln.getAimoutdiameter());
            patsPlan.setObject(32, pln.getAimwallthick());
            patsPlan.setObject(33, pln.getOutwlId());// 物料码
            patsPlan.setObject(34, pln.getOutwlDesc());
            patsPlan.setObject(35, pln.getInwlId());
            patsPlan.setObject(36, pln.getInwlDesc());
            patsPlan.setObject(37, load.getProNum());
            patsPlan.setObject(38, load.getProWt());
            patsPlan.setObject(39, load.getFailNum());
            patsPlan.setObject(40, load.getFailWt());
            patsPlan.setObject(41, load.getOfflineNum());
            patsPlan.setObject(42, load.getOfflineWt());
            patsPlan.setObject(43, load.getReNum());
            patsPlan.setObject(44, load.getReWt());
            patsPlan.setObject(45, "1");
            patsPlan.setObject(46, load.getLoadTime());
            patsPlan.setObject(47, "");
            patsPlan.setObject(48, load.getLoadUser());
            patsPlan.setObject(49, load.getLoadShift());
            patsPlan.setObject(50, load.getLoadGroup());
            patsPlan.setObject(51, systime);
            patsPlan.setObject(52, "");
            patsPlan.setObject(53, pln.getSteelcode());
            patsPlan.setObject(54, pln.getSteelname());
            patsPlan.setObject(55, pln.getSpecCode());
            patsPlan.setObject(56, pln.getSpecName());
            patsPlan.addBatch();

            // 新增调度日志表
            pastAddLoag.setObject(1, matter2.format(now));
            pastAddLoag.setObject(2, pln.getPlineCode());
            pastAddLoag.setObject(3, pln.getJudgeStoveNo());
            pastAddLoag.setObject(4, pln.getZyBatchId());
            pastAddLoag.setObject(5, pln.getOrderNo() + pln.getOrderSeq()
                    + pln.getDeliveryNo());
            pastAddLoag.setObject(6, pln.getCraftFileNo());
            pastAddLoag.setObject(7, pln.getPrdcrNo());
            pastAddLoag.setObject(8, pln.getGradecode());
            pastAddLoag.setObject(9, pln.getGradename());
            pastAddLoag.setObject(10, load.getLoadShift());
            pastAddLoag.setObject(11, load.getLoadGroup());
            pastAddLoag.setObject(12, load.getProNum());
            pastAddLoag.setObject(13, load.getProWt());
            pastAddLoag.setObject(14, load.getFailNum());
            pastAddLoag.setObject(15, load.getFailWt());
            pastAddLoag.setObject(16, "1");
            pastAddLoag.setObject(17, systime);
            pastAddLoag.setObject(18, load.getLoadUser());
            pastAddLoag.setObject(19, resultNo);
            pastAddLoag.setObject(20, pln.getHeatPlanNo());
            pastAddLoag.setObject(21, plnC.getBatchNo());
            pastAddLoag.setObject(22, groupNo);
            pastAddLoag.addBatch();

            ArrayList<String> matNo = new ArrayList<String>();
            String sqlloading1 = CoreXmlSqlParsersFactory
                    .getSql("FrmVrpMeasuring.queryExpandNUM");// 查询管号队列表里合格的材料号
            psta = connection.prepareStatement(sqlloading1);
            psta.setObject(1, pln.getHeatPlanNo());
            psta.setObject(2, Integer.parseInt(load.getFailNum()) + 1);
            ResultSet rs2 = psta.executeQuery();
            while (rs2.next()) {
                matNo.add(rs2.getString("MAT_NO"));
            }
            rs2.close();
            psta.close();



            if(matNo.size()!=Integer.parseInt(load.getFailNum())  ){
                core.setV_errMsg("没有足够的未下线的合格管！请确认");
                return core;
            }

            for (int i = 0; i < matNo.size(); i++) {
                patsPlan1.setObject(1, "10");
                patsPlan1.setObject(2, slgx);
                patsPlan1.setObject(3, pln.getHeatPlanNo());
                patsPlan1.setObject(4, matNo.get(i));
                patsPlan1.addBatch();
            }
            // 修改工艺路径
            pastupdatePath.setObject(1, "1");
            pastupdatePath.setObject(2, pln.getHeatPlanNo());
            pastupdatePath.addBatch();

            // 修改工序点开始状态（计划）
            patupdate.setObject(1, "50");
            patupdate.setObject(2, pln.getHeatPlanNo());
            patupdate.addBatch();
            patupdate.executeBatch();		
            pastupdatePath.executeBatch();
            patsPlan1.executeBatch();
            patAdd.executeBatch();
            patsPlan.executeBatch();
            pastAddLoag.executeBatch();


        	// 如果有废品新增到材料表
			if (!load.getFailNum().equals("") && !load.getFailNum().equals("0")) {
				for (int i = 0; i < matNo.size(); i++) {

                    PlanService.AddOffLineMat(connection,"E",load.getLoadUser(),"22",
                            matNo.get(i),slgx,load.getHeatPlanNo(),load.getFailReason());
					
				}
			}
            core.setV_errMsg("新增成功！");
            core.setResult(1);
            connection.commit();
        }
        catch (Exception e) {
            core.setV_errMsg(e.getMessage());
            if (connection != null) {
                try {
                    connection.rollback(); // 回滚事务
                }
                catch (Exception e2) {
                }
            }
        }
        finally {
            try {
                if (connection != null && !connection.isClosed()) {
                    connection.close();
                }
            }
            catch (Exception e2) {
            }
        }
        return core;
    }

    /**
     * 撤销测量
     * @param planNo
     * @return
     */
    public CoreReturnObject returnMeasuringResult(ArrayList<String> list1,
            String herPlanNo, String plinCode, String gx, Integer detCount) {
        CoreReturnObject core = new CoreReturnObject();
        Connection connection = this.getDao(CoreConstantData.sysbeanstr)
                .getConnection();
        try {
            connection.setAutoCommit(false); // 取消自动提交事务
            String sqlDelete = CoreXmlSqlParsersFactory
                    .getSql("FrmVrpMeasuring.ReturnVrpMeasuringResult");// 删除测量实绩
            PreparedStatement patDelete = connection
                    .prepareStatement(sqlDelete);
            String updatePlnInfo = CoreXmlSqlParsersFactory
                    .getSql("FrmVrpLoading.deleteVrpResult");// 删除公共信息表
            PreparedStatement patsupdatePlan = connection
                    .prepareStatement(updatePlnInfo);
            String addPlnInfo1 = CoreXmlSqlParsersFactory
                    .getSql("FrmVrpMeasuring.AddComInfoResult1");// 查询材料状态和工序点编码
            PreparedStatement patsPlan1 = connection
                    .prepareStatement(addPlnInfo1);
            String updatePath = CoreXmlSqlParsersFactory
                    .getSql("FrmVrpMeasuring.UpdatePath");// 修改队列表工艺路径
            PreparedStatement pastupdatePath = connection
                    .prepareStatement(updatePath);
            String sqlupdate = CoreXmlSqlParsersFactory
                    .getSql("FrmVrpMeasuring.UPDATEDbkPlan");// 修改镦拔扩计划表工序开始状态
            PreparedStatement patupdate = connection
                    .prepareStatement(sqlupdate);
            String deleteConLoag = CoreXmlSqlParsersFactory
                    .getSql("FrmVrpExpandResult.DeleteConLoag");// 删除调度日志
            PreparedStatement pastDeleteLoag = connection
                    .prepareStatement(deleteConLoag);
            // 获取镦拔扩计划实体类
            PlnZyDbkMEntity pln = PlanService.getSendDBKPlanCInfo(connection,
                    herPlanNo);
            if (pln == null)
                return core;
            if (!pln.getPlanstatus().equals("20")) {
                core.setV_errMsg("该计划不是生产中的状态！");
                return core;
            }
            String cutFalg = "";
            String sql = CoreXmlSqlParsersFactory
                    .getSql("FrmVrpExpandResult.qureyGxComInfo");// 查询工序是否是结炉状态
            PreparedStatement psta = connection.prepareStatement(sql);
            psta.setString(1, herPlanNo);
            psta.setString(2, plinCode);
            psta.setString(3, gx);
            ResultSet rs = psta.executeQuery();
            if (rs.next()) {
                cutFalg = rs.getString("STATION_STATUS");
            }
            rs.close();
            psta.close();
            if (cutFalg.equals("2")) {
                core.setV_errMsg("已结炉,不允许此操作！");
                return core;
            }
           
            
/*            int lxCount = 0;
            sql = CoreXmlSqlParsersFactory
                    .getSql("FrmVrpMeasuring.qureyGxLxCount");// 查询是否离线
            psta = connection.prepareStatement(sql);
            psta.setString(1, herPlanNo);
            psta.setString(2, plinCode);
            psta.setString(3, gx);
            rs = psta.executeQuery();
            if (rs.next()) {
                lxCount = Integer.parseInt(rs.getString("COUNT"));
            }
            rs.close();
            psta.close();
            if (lxCount!=0) {
                core.setV_errMsg("该工序点有离线记录,不允许此操作！");
                return core;
            }*/
            // for (int i = 0; i < list1.size(); i++) {
            // ArrayList<String> list = (ArrayList<String>) list1.get(i);
            String resNo = list1.get(0);
            // 删除测量实绩
            patDelete.setObject(1, resNo);
            patDelete.addBatch();
            // 删除公共信息
            patsupdatePlan.setObject(1, resNo);
            patsupdatePlan.addBatch();

            // 删除调度日志
            pastDeleteLoag.setObject(1, herPlanNo);
            pastDeleteLoag.setObject(2, resNo);
            pastDeleteLoag.addBatch();
            // }
            patDelete.executeBatch();
            patsupdatePlan.executeBatch();
            pastDeleteLoag.executeBatch();
            // 查询计划下剩余上料支、吨
            double count = 0;
            double weghit = 0;
            int indCount = 0;
            String sqlloading = CoreXmlSqlParsersFactory
                    .getSql("FrmVrpMeasuring.queryCountMeasuring");// 查询计划下剩余上料支、吨
            psta = connection.prepareStatement(sqlloading);
            psta.setObject(1, herPlanNo);
            rs = psta.executeQuery();
            if (rs.next()) {
                count = Double.parseDouble(rs.getString("PRO_NUM"));
                weghit = Double.parseDouble(rs.getString("PRO_WT"));
                indCount = Integer.parseInt(rs.getString("COUNT"));
            }
            rs.close();
            psta.close();

            int count1 = 0;
            String sqlloading1 = CoreXmlSqlParsersFactory
                    .getSql("FrmVrpMeasuring.queryExpandFailNUM");// 查询管号队列表里废品数量
            psta = connection.prepareStatement(sqlloading1);
            psta.setObject(1, herPlanNo);
            psta.setObject(2, gx);
            ResultSet rs2 = psta.executeQuery();
            if (rs2.next()) {
                count1 = Integer.parseInt(rs2.getString("ACT_COUNT"));
            }
            rs2.close();
            psta.close();
            if(count1 < detCount)
            {
            	core.setV_errMsg("管号队列的废品支数小于实绩废品支数，请检查!");
                return core;
            }else {
                ArrayList<String> matNo = new ArrayList<String>();
                String sqlloading3 = CoreXmlSqlParsersFactory
                        .getSql("FrmVrpMeasuring.queryExpandNUM2");// 查询管号队列表里废品材料号
                psta = connection.prepareStatement(sqlloading3);
                psta.setObject(1, herPlanNo);
                psta.setObject(2, gx);
                psta.setObject(3, detCount + 1);
                ResultSet rs4 = psta.executeQuery();
                while (rs4.next()) {
                    matNo.add(rs4.getString("MAT_NO"));
                }
                rs4.close();
                psta.close();
                

				
                for (int i = 0; i < matNo.size(); i++) {
                    patsPlan1.setObject(1, "00");
                    patsPlan1.setObject(2, gx);
                    patsPlan1.setObject(3, herPlanNo);
                    patsPlan1.setObject(4, matNo.get(i));
                    patsPlan1.addBatch();

                    PlanService.DeleteOffLineMat(connection,matNo.get(i));
    				
                }
                patsPlan1.executeBatch();
            }
            // 修改工艺路径
            pastupdatePath.setObject(1, "0");
            pastupdatePath.setObject(2, herPlanNo);
            pastupdatePath.addBatch();

            // 修改工序点开始状态（计划）
            patupdate.setObject(1, "00");
            patupdate.setObject(2, pln.getHeatPlanNo());
            patupdate.addBatch();
            patupdate.executeBatch();

            pastupdatePath.executeBatch();
            core.setV_errMsg("删除成功！");
            core.setResult(1);
            connection.commit();
        }
        catch (Exception e) {
            core.setV_errMsg(e.getMessage());
            if (connection != null) {
                try {
                    connection.rollback(); // 回滚事务
                }
                catch (Exception e2) {
                }
            }
        }
        finally {
            try {
                if (connection != null && !connection.isClosed()) {
                    connection.close();
                }
            }
            catch (Exception e2) {
            }
        }
        return core;
    }

    /**
     * 修改测量
     * @param planNo
     * @return
     */
    public CoreReturnObject updateMeasuringResult(
            VrpMeasuringResultEntity load, String rentNo, String heartNO,
            String gx) {
        CoreReturnObject core = new CoreReturnObject();
        Connection connection = this.getDao(CoreConstantData.sysbeanstr)
                .getConnection();
        Date now = new Date();
        SimpleDateFormat dateFormat = new SimpleDateFormat(
                "yyyy-MM-dd HH:mm:ss", Locale.CHINA);
        String systime = dateFormat.format(now);
        try {
            connection.setAutoCommit(false); // 取消自动提交事务
            String sqlDelete = CoreXmlSqlParsersFactory
                    .getSql("FrmVrpMeasuring.updateVrpMeasuringResult");// 修改测量实绩
            PreparedStatement patDelete = connection
                    .prepareStatement(sqlDelete);
            String updatePlnInfo = CoreXmlSqlParsersFactory
                    .getSql("FrmVrpExpandResult.updateComInfoResult");// 修改公共信息
            PreparedStatement patsupdatePlan = connection
                    .prepareStatement(updatePlnInfo);
            String updateConLoag = CoreXmlSqlParsersFactory
                    .getSql("FrmVrpExpandResult.UpdateConLoag");// 修改调度日志
            PreparedStatement pastUpdateLoag = connection
                    .prepareStatement(updateConLoag);
            String addPlnInfo1 = CoreXmlSqlParsersFactory
                    .getSql("FrmVrpMeasuring.AddComInfoResult1");// 查询材料状态和工序点编码
            PreparedStatement patsPlan1 = connection
                    .prepareStatement(addPlnInfo1);



            // 获取镦拔扩计划实体类
            PlnZyDbkMEntity pln = PlanService.getSendDBKPlanCInfo(connection,
                    heartNO);
            if (pln == null)
                return core;
            if (!pln.getPlanstatus().equals("20")) {
                core.setV_errMsg("该计划不是生产中的状态！");
                return core;
            }
            String cutFalg = "";
            String sql = CoreXmlSqlParsersFactory
                    .getSql("FrmVrpExpandResult.qureyGxComInfo");// 查询工序是否是结炉状态
            PreparedStatement psta = connection.prepareStatement(sql);
            psta.setString(1, heartNO);
            psta.setString(2, load.getPlineCode());
            psta.setString(3, gx);
            ResultSet rs = psta.executeQuery();
            if (rs.next()) {
                cutFalg = rs.getString("STATION_STATUS");
            }
            if (cutFalg.equals("2")) {
                core.setV_errMsg("已结炉,不允许此操作！");
                return core;
            }
            rs.close();
            psta.close();
            // 修改测量实绩
            patDelete.setObject(1, load.getProNum());
            patDelete.setObject(2, load.getProWt());
            patDelete.setObject(3, load.getLoadShift());
            patDelete.setObject(4, load.getLoadGroup());
            patDelete.setObject(5, load.getAccNum());
            patDelete.setObject(6, load.getAccWt());
            patDelete.setObject(7, load.getLoadUser());
            patDelete.setObject(8, load.getFailNum());
            patDelete.setObject(9, load.getFailWt());
            patDelete.setObject(10, load.getFailReason());
            patDelete.setObject(11, systime);
            patDelete.setObject(12, load.getProducer());
            patDelete.setObject(13, load.getProducerTime());
            patDelete.setObject(14, rentNo);
            patDelete.addBatch();
            // 修改测量公共信息
            patsupdatePlan.setObject(1, load.getProducer());
            patsupdatePlan.setObject(2, load.getProducerTime());
            patsupdatePlan.setObject(3, load.getProNum());
            patsupdatePlan.setObject(4, load.getProWt());
            patsupdatePlan.setObject(5, load.getFailNum());
            patsupdatePlan.setObject(6, load.getFailWt());
            patsupdatePlan.setObject(7, load.getLoadTime());
            patsupdatePlan.setObject(8, load.getLoadShift());
            patsupdatePlan.setObject(9, load.getLoadGroup());
            patsupdatePlan.setObject(10,rentNo);
            patsupdatePlan.addBatch();

            // 修改调度日志
            pastUpdateLoag.setObject(1, load.getProNum());
            pastUpdateLoag.setObject(2, load.getProWt());
            pastUpdateLoag.setObject(3, load.getFailNum());
            pastUpdateLoag.setObject(4, load.getFailWt());
            pastUpdateLoag.setObject(5, load.getLoadShift());
            pastUpdateLoag.setObject(6, load.getLoadGroup());
            pastUpdateLoag.setObject(7, heartNO);
            pastUpdateLoag.setObject(8, rentNo);
            pastUpdateLoag.addBatch();

            patDelete.executeBatch();
            patsupdatePlan.executeBatch();
            pastUpdateLoag.executeBatch();
            // 查询计划下剩余上料支、吨
            // double count = 0;
            // double weghit = 0;
            // int indCount = 0;
            // String sqlloading = CoreXmlSqlParsersFactory
            // .getSql("FrmVrpLoading.queryVrpCount");
            // psta = connection.prepareStatement(sqlloading);
            // psta.setObject(1, heartNO);
            // rs = psta.executeQuery();
            // if (rs.next()) {
            // count = Double.parseDouble(rs.getString("PRO_NUM"));
            // weghit = Double.parseDouble(rs.getString("PRO_WT"));
            // indCount = Integer.parseInt(rs.getString("COUNT"));
            // }
            // rs.close();
            // psta.close();
            int count = 0;
            int num = 0;
            int FAIL = 0;
            String sqlloading1 = CoreXmlSqlParsersFactory
                    .getSql("FrmVrpMeasuring.queryExpandFailNUM");// 查询管号队列表里废品数量
            psta = connection.prepareStatement(sqlloading1);
            psta.setObject(1, heartNO);
            psta.setObject(2, gx);
            ResultSet rs2 = psta.executeQuery();
            if (rs2.next()) {
                count = Integer.parseInt(rs2.getString("ACT_COUNT"));
            }
            rs2.close();
            psta.close();
            String sqlloading2 = CoreXmlSqlParsersFactory
                    .getSql("FrmVrpMeasuring.queryExpandFailNUM1");// 查询工序废品数量
            psta = connection.prepareStatement(sqlloading2);
            psta.setObject(1, heartNO);
            ResultSet rs3 = psta.executeQuery();
            if (rs3.next()) {
                FAIL = Integer.parseInt(rs3.getString("FAIL_NUM"));
            }
            rs3.close();
            psta.close();
            if (count > FAIL) {
                num = count - FAIL;
                ArrayList<String> matNo = new ArrayList<String>();
                String sqlloading3 = CoreXmlSqlParsersFactory
                        .getSql("FrmVrpMeasuring.queryExpandNUM2");// 查询管号队列表里废品材料号
                psta = connection.prepareStatement(sqlloading3);
                psta.setObject(1, heartNO);
                psta.setObject(2, gx);
                psta.setObject(3, num + 1);
                ResultSet rs4 = psta.executeQuery();
                while (rs4.next()) {
                    matNo.add(rs4.getString("MAT_NO"));
                }
                rs4.close();
                psta.close();
                for (num = 0; num < matNo.size(); num++) {
                    patsPlan1.setObject(1, "00");
                    patsPlan1.setObject(2, gx);
                    patsPlan1.setObject(3, heartNO);
                    patsPlan1.setObject(4, matNo.get(num));
                    patsPlan1.addBatch();

                    PlanService.DeleteOffLineMat(connection,matNo.get(num));
                }
                patsPlan1.executeBatch();
            }
            else if (count < FAIL) {
                num = FAIL - count;
                ArrayList<String> matNo1 = new ArrayList<String>();
                String sqlloading4 = CoreXmlSqlParsersFactory
                        .getSql("FrmVrpMeasuring.queryExpandNUM");// 查询管号队列表里合格材料号
                psta = connection.prepareStatement(sqlloading4);
                psta.setObject(1, heartNO);
                psta.setObject(2, num + 1);
                ResultSet rs5 = psta.executeQuery();
                while (rs5.next()) {
                    matNo1.add(rs5.getString("MAT_NO"));
                }
                rs5.close();
                psta.close();
                for (num = 0; num < matNo1.size(); num++) {
                    patsPlan1.setObject(1, "10");
                    patsPlan1.setObject(2, gx);
                    patsPlan1.setObject(3, heartNO);
                    patsPlan1.setObject(4, matNo1.get(num));
                    patsPlan1.addBatch();
                    


                    PlanService.AddOffLineMat(connection,"E",load.getLoadUser(),"22",
                            matNo1.get(num),gx,heartNO);
                    
                }				
                patsPlan1.executeBatch();
            }
            core.setV_errMsg("修改成功！");
            core.setResult(1);
            connection.commit();
        }
        catch (Exception e) {
            core.setV_errMsg(e.getMessage());
            if (connection != null) {
                try {
                    connection.rollback(); // 回滚事务
                }
                catch (Exception e2) {
                }
            }
        }
        finally {
            try {
                if (connection != null && !connection.isClosed()) {
                    connection.close();
                }
            }
            catch (Exception e2) {
            }
        }
        return core;
    }

    /**
     * 根据计划号查询镦拔扩测量实绩
     * @param planNo 计划号
     * @return
     */
    public CoreReturnObject getMeasuringResult(String heatNo) {
        return this.getDataBySqlID("FrmVrpMeasuring.GetQueryMeasuring",
                new Object[] { heatNo });
    }

    /**
     * 根据计划号查询镦拔扩测量实绩合格支数的和
     * @param planNo 计划号
     * @return
     */
    public CoreReturnObject getMeasuringResult1(String heatNo) {
        return this.getDataBySqlID("FrmVrpMeasuring.GetQueryMeasuring1",
                new Object[] { heatNo });
    }

    /***
     * 结炉
     * @param zYgNo
     * @return
     */
    public CoreReturnObject updateDoBeside(ArrayList<String> parm,String num) {
        CoreReturnObject cro = new CoreReturnObject();
        Connection connection = this.getDao(CoreConstantData.sysbeanstr)
                .getConnection();
        String sql = "";
        PreparedStatement psta = null;
        ResultSet rs = null;
        Date now = new Date();
        SimpleDateFormat matter2 = new SimpleDateFormat("yyyyMMddHHmm");
        try {
            connection.setAutoCommit(false); // 取消自动提交事务
            String updateSql = CoreXmlSqlParsersFactory
                    .getSql("FrmVrpExpandResult.doBeside");// 修改工序状态为结炉
            PreparedStatement updateSqlPsta = connection
                    .prepareStatement(updateSql);
            String sqlupdate = CoreXmlSqlParsersFactory
                    .getSql("FrmVrpMeasuring.UPDATEDbkBesideStatrs");// 修改镦拔扩计划表工序结炉状态
            PreparedStatement patupdate = connection
                    .prepareStatement(sqlupdate);
            String updateConLoag = CoreXmlSqlParsersFactory
                    .getSql("FrmVrpExpandResult.UpdateNodeConLoag");// 结炉修改调度日志
            PreparedStatement pastUpdateLoag = connection
                    .prepareStatement(updateConLoag);
            String cutFalg = "";
            sql = CoreXmlSqlParsersFactory
                    .getSql("FrmVrpExpandResult.qureyGxComInfo");// 查询工序是否是结炉状态
            psta = connection.prepareStatement(sql);
            psta.setString(1, parm.get(0));
            psta.setString(2, parm.get(1));
            psta.setObject(3, Integer.parseInt(parm.get(2)) - 1);
            rs = psta.executeQuery();
            if (rs.next()) {
                cutFalg = rs.getString("STATION_STATUS");
            }
            if (cutFalg.equals("1")) {
                cro.setV_errMsg("上一道工序没有结炉,不允许此工序结炉!");
                return cro;
            }
            
            rs.close();
            psta.close();
            int lxCount = 0;
            sql = CoreXmlSqlParsersFactory
                    .getSql("FrmVrpMeasuring.qureyGxLxCount");// 查询是否离线
            psta = connection.prepareStatement(sql);
            psta.setString(1, parm.get(0));
            psta.setString(2, parm.get(1));
            psta.setString(3, parm.get(2));
            rs = psta.executeQuery();
            if (rs.next()) {
                lxCount = Integer.parseInt(rs.getString("COUNT"));
            }
            rs.close();
            psta.close();
            if (lxCount!=0) {
                cro.setV_errMsg("该工序点有离线记录,不允许此操作！");
                return cro;
            }
            String cutFalg1 = "";
            sql = CoreXmlSqlParsersFactory
                    .getSql("FrmVrpExpandResult.qureyGxComInfo");// 查询工序是否是结炉状态
            psta = connection.prepareStatement(sql);
            psta.setString(1, parm.get(0));
            psta.setString(2, parm.get(1));
            psta.setString(3, parm.get(2));
            rs = psta.executeQuery();
            if (rs.next()) {
                cutFalg1 = rs.getString("STATION_STATUS");
            }
            else {
                cro.setV_errMsg("无当前工序实绩,无法进行结炉!");
                return cro;
            }
            if (cutFalg1.equals("2")) {
                cro.setV_errMsg("已结炉,不允许此操作！");
                return cro;
            }
            rs.close();
            psta.close();

            updateSqlPsta.setString(1, parm.get(0));
            updateSqlPsta.setString(2, parm.get(1));
            updateSqlPsta.setString(3, parm.get(2));
            updateSqlPsta.addBatch();
            
            String actCount = "";
            //如果该工序都是废品结炉后变成生产完成
            sql = CoreXmlSqlParsersFactory.getSql("FrmVrpColding.queryPortStuts");//查询管号队列状态
            psta = connection.prepareStatement(sql);
            psta.setString(1, parm.get(0));
            psta.setString(2, parm.get(1));
            rs = psta.executeQuery();
            if(rs.next())
            {
            	actCount = rs.getString("ACT_COUNT");
            }
            rs.close();
            psta.close();
            if(Integer.parseInt(actCount)==0)
            {
            	 String updateSqlPlnStuts1 = CoreXmlSqlParsersFactory
                         .getSql("FrmVrpColding.plnupdateStuts");// 结炉修改计划工序状态为生产完成
                 PreparedStatement updateSqlPsta2 = connection
                         .prepareStatement(updateSqlPlnStuts1);
                 updateSqlPsta2.setObject(1, parm.get(0));
                 updateSqlPsta2.addBatch();
                 updateSqlPsta2.executeBatch();
            }else{
            	   // 修改工序点结炉状态（计划）
                patupdate.setObject(1, "50");
                patupdate.setObject(2, parm.get(0));
                patupdate.addBatch();
                patupdate.executeBatch();
            }

         

            // 修改调度结炉标识
            pastUpdateLoag.setObject(1, matter2.format(now));
            pastUpdateLoag.setObject(2, "1");
            pastUpdateLoag.setObject(3, parm.get(0));
            pastUpdateLoag.addBatch();

            updateSqlPsta.executeBatch();
          
            pastUpdateLoag.executeBatch();
            cro.setV_errMsg("结炉成功！");
            connection.commit();
        }
        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 zYgNo
     * @return
     */
    public CoreReturnObject undoBeside(ArrayList<String> parm,String num) {
        CoreReturnObject cro = new CoreReturnObject();
        Connection connection = this.getDao(CoreConstantData.sysbeanstr)
                .getConnection();
        String sql = "";
        PreparedStatement psta = null;
        ResultSet rs = null;
        try {
            connection.setAutoCommit(false); // 取消自动提交事务
            String updateSql = CoreXmlSqlParsersFactory
                    .getSql("FrmVrpExpandResult.deleteBeside");// 修改工序状态撤销结炉
            PreparedStatement updateSqlPsta = connection
                    .prepareStatement(updateSql);
            String sqlupdate = CoreXmlSqlParsersFactory
                    .getSql("FrmVrpMeasuring.UPDATEDbkBesideStatrs");// 修改镦拔扩计划表工序结炉状态
            PreparedStatement patupdate = connection
                    .prepareStatement(sqlupdate);
            String updateConLoag = CoreXmlSqlParsersFactory
                    .getSql("FrmVrpExpandResult.UpdateNodeConLoag");// 结炉修改调度日志
            PreparedStatement pastUpdateLoag = connection
                    .prepareStatement(updateConLoag);
            String cutFalg2 = "";
            sql = CoreXmlSqlParsersFactory
                    .getSql("FrmVrpExpandResult.qureyGxComInfo1");// 查询是否是组炉状态
            psta = connection.prepareStatement(sql);
            psta.setString(1, parm.get(0));
            psta.setString(2, parm.get(1));
            rs = psta.executeQuery();
            if (rs.next()) {
                cutFalg2 = rs.getString("GROUP_FLAG");
            }
            if (cutFalg2.equals("1")) {
                cro.setV_errMsg("已经组炉,不允许撤销结炉!");
                return cro;
            }
            rs.close();
            psta.close();
            String cutFalg1 = "";
            sql = CoreXmlSqlParsersFactory
                    .getSql("FrmVrpExpandResult.qureyGxComInfo");// 查询工序是否是结炉状态
            psta = connection.prepareStatement(sql);
            psta.setString(1, parm.get(0));
            psta.setString(2, parm.get(1));
            psta.setObject(3, Integer.parseInt(parm.get(2)) + 2);
            rs = psta.executeQuery();
            if (rs.next()) {
                cutFalg1 = rs.getString("STATION_STATUS");
            }
            if (cutFalg1.equals("2")) {
                cro.setV_errMsg("下一道工序没有撤销结炉,不允许此工序撤销结炉!");
                return cro;
            }
            rs.close();
            psta.close();
            String cutFalg = "";
            sql = CoreXmlSqlParsersFactory
                    .getSql("FrmVrpExpandResult.qureyGxComInfo");// 查询工序是否是结炉状态
            psta = connection.prepareStatement(sql);
            psta.setString(1, parm.get(0));
            psta.setString(2, parm.get(1));
            psta.setString(3, parm.get(2));
            rs = psta.executeQuery();
            if (rs.next()) {
                cutFalg = rs.getString("STATION_STATUS");
            }
            else {
                cro.setV_errMsg("无当前工序实绩,无法进行撤销结炉!");
                return cro;
            }
            if (cutFalg.equals("1")) {
                cro.setV_errMsg("未结炉,不允许此操作！");
                return cro;
            }
            rs.close();
            psta.close();
            updateSqlPsta.setString(1, parm.get(0));
            updateSqlPsta.setString(2, parm.get(1));
            updateSqlPsta.setString(3, parm.get(2));
            updateSqlPsta.addBatch();
            
            String actCount = "";
            //如果该工序都是废品结炉后变成生产完成
            sql = CoreXmlSqlParsersFactory.getSql("FrmVrpColding.queryPortStuts");//查询管号队列状态
            psta = connection.prepareStatement(sql);
            psta.setString(1, parm.get(0));
            psta.setString(2, parm.get(1));
            rs = psta.executeQuery();
            if(rs.next())
            {
            	actCount = rs.getString("ACT_COUNT");
            }
            rs.close();
            psta.close();
            if(Integer.parseInt(actCount)==0)
            {
            	 String updateSqlPlnStuts1 = CoreXmlSqlParsersFactory
                         .getSql("FrmVrpColding.updateplnupdateStuts");// 结炉修改计划工序状态为生产完成
                 PreparedStatement updateSqlPsta2 = connection
                         .prepareStatement(updateSqlPlnStuts1);
                 updateSqlPsta2.setObject(1, parm.get(0));
                 updateSqlPsta2.addBatch();
                 updateSqlPsta2.executeBatch();
            }else{
            	  // 修改工序点结炉状态（计划）
                patupdate.setObject(1, "00");
                patupdate.setObject(2, parm.get(0));
                patupdate.addBatch();
            }

          
            // 修改调度结炉标识
            pastUpdateLoag.setObject(1, "");
            pastUpdateLoag.setObject(2, "0");
            pastUpdateLoag.setObject(3, parm.get(0));
            pastUpdateLoag.addBatch();

            updateSqlPsta.executeBatch();
            patupdate.executeBatch();
            pastUpdateLoag.executeBatch();
            cro.setV_errMsg("撤销结炉成功!");
            connection.commit();
        }
        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 load
     * @return
     */
    public CoreReturnObject offLineExpandResult(VrpMeasuringResultEntity load,
            ArrayList<String> list, String plinCode, String storageNo,String groupNo) {
        CoreReturnObject core = new CoreReturnObject();
        Connection connection = this.getDao(CoreConstantData.sysbeanstr)
                .getConnection();
        PreparedStatement psta = null;
        Date dt = new Date();
        SimpleDateFormat matter1 = new SimpleDateFormat("yyMMdd");
        SimpleDateFormat matter = new SimpleDateFormat("yyyyMMdd");
        Calendar ca = Calendar.getInstance();
        ca.set(Calendar.DATE, ca.getActualMaximum(Calendar.DATE));
        Date now = new Date();
        SimpleDateFormat dateFormat = new SimpleDateFormat(
                "yyyy-MM-dd HH:mm:ss", Locale.CHINA);
        String systime = dateFormat.format(now);
        try {
            connection.setAutoCommit(false); // 取消自动提交事务
            String addOff = CoreXmlSqlParsersFactory
                    .getSql("FrmVrpExpandResult.AddOffLine");// 新增离线实绩
            PreparedStatement patAdd = connection.prepareStatement(addOff);
            String MarkOff = CoreXmlSqlParsersFactory
                    .getSql("FrmVrpExpandResult.MarkUnCancelFlag");// 标记之前同炉批组返线的离线实绩为无法撤销数据
            PreparedStatement patMarkOff = connection.prepareStatement(MarkOff);

//            String addIn = CoreXmlSqlParsersFactory
//                    .getSql("FrmVrpExpandResult.AddYdmInList");// 新增入库信息
//            PreparedStatement patIn = connection.prepareStatement(addIn);
            String updateMat = CoreXmlSqlParsersFactory
                    .getSql("FrmVrpExpandResult.UpdateMatNo");// 修改材料状态
            PreparedStatement patMatNo = connection.prepareStatement(updateMat);

            String updatePath = CoreXmlSqlParsersFactory
                    .getSql("FrmVrpExpandResult.UpdatePathPortVrp");// 修改队列表工艺路径,材料，工序状态
            PreparedStatement pastupdatePath = connection
                    .prepareStatement(updatePath);



            String heatPlanNo = list.get(0);
            String slgx = list.get(1);
            String slgxname = list.get(2);
            String name = list.get(3);
            String time = list.get(4);
            String bc = list.get(5);
            String bz = list.get(6);
            String count = list.get(7);
            String judge = list.get(8);
            String batch = list.get(9);
            String txtMemo = list.get(10);

            int num = 0;
            int num1 = 0;
            int num2 = 0;
            if (plinCode.equals("C018")) {
                if (slgx.equals("650102")) {
                    num = 1;
                    num1 = 2;
                    num2 = 4;
                }
                else if (slgx.equals("650103")) {
                    num = 1;
                    num1 = 4;
                    num2 = 6;
                }
                else if (slgx.equals("650104")) {
                    num = 1;
                    num1 = 6;
                    num2 = 8;
                }
                else if (slgx.equals("650105")) {
                    num = 1;
                    num1 = 8;
                    num2 = 10;
                }
                else if (slgx.equals("650106")) {
                    num = 1;
                    num1 = 10;
                    num2 = 12;
                }
                else if (slgx.equals("650107")) {
                    num = 1;
                    num1 = 12;
                    num2 = 14;
                }

            }
            else if (plinCode.equals("C021")) {
                if (slgx.equals("650402")) {
                    num = 1;
                    num1 = 2;
                    num2 = 4;
                }
                else if (slgx.equals("650403")) {
                    num = 1;
                    num1 = 4;
                    num2 = 6;
                }
                else if (slgx.equals("650404")) {
                    num = 1;
                    num1 = 6;
                    num2 = 8;
                }
                else if (slgx.equals("650405")) {
                    num = 1;
                    num1 = 8;
                    num2 = 10;
                }
            }
            else if (plinCode.equals("C014")) {
                if (slgx.equals("650502")) {
                    num = 1;
                    num1 = 2;
                    num2 = 4;
                }
                else if (slgx.equals("650503")) {
                    num = 1;
                    num1 = 4;
                    num2 = 6;
                }
                else if (slgx.equals("650504")) {
                    num = 1;
                    num1 = 6;
                    num2 = 8;
                }
                else if (slgx.equals("650505")) {
                    num = 1;
                    num1 = 8;
                    num2 = 10;
                }
            }
            else if (plinCode.equals("C013")) {
                if (slgx.equals("650602")) {
                    num = 1;
                    num1 = 2;
                    num2 = 4;
                }
                else if (slgx.equals("650603")) {
                    num = 1;
                    num1 = 4;
                    num2 = 6;
                }
                else if (slgx.equals("650604")) {
                    num = 1;
                    num1 = 6;
                    num2 = 8;
                }
                else if (slgx.equals("650605")) {
                    num = 1;
                    num1 = 8;
                    num2 = 10;
                }
            }
            else if (plinCode.equals("C070")) {
                if (slgx.equals("650702")) {
                    num = 1;
                    num1 = 2;
                    num2 = 4;
                }
                else if (slgx.equals("650703")) {
                    num = 1;
                    num1 = 4;
                    num2 = 6;
                }
                else if (slgx.equals("650704")) {
                    num = 1;
                    num1 = 6;
                    num2 = 8;
                }
                else if (slgx.equals("650705")) {
                    num = 1;
                    num1 = 8;
                    num2 = 10;
                }
            }

            // 获取镦拔扩计划实体类
            PlnZyDbkMEntity pln = PlanService.getSendDBKPlanCInfo(connection,
                    heatPlanNo);
            // 获取镦拔扩计划实体类
            PlnZyDbkCEntity plnC = PlanService.getSendDBKPlanCInfoC(connection,
                    heatPlanNo);
            if (pln == null)
                return core;
            // if (!pln.getPlanstatus().equals("2")) {
            // core.setV_errMsg("该计划不是生产中的，请确认是否生产完成或未上料！");
            // return core;
            // }
            String cutFalg = "";
            String sql = CoreXmlSqlParsersFactory
                    .getSql("FrmVrpExpandResult.qureyGxComInfo");// 查询工序结炉状态
            PreparedStatement psta1 = connection.prepareStatement(sql);
            psta1.setString(1, heatPlanNo);
            psta1.setString(2, plinCode);
            psta1.setString(3, slgx);
            ResultSet rs = psta1.executeQuery();
            if (rs.next()) {
                cutFalg = rs.getString("STATION_STATUS");
            }
            if (cutFalg.equals("2")) {
                core.setV_errMsg("已结炉,不允许此操作！");
                return core;
            }
            rs.close();
            psta1.close();
            List<PortVrpBatchSampleResultEntity> matNo = new ArrayList<PortVrpBatchSampleResultEntity>();
            String sqlloading1 = CoreXmlSqlParsersFactory
                    .getSql("FrmVrpExpandResult.queryPortVrpHg");// 查询管号队列里合格的材料号
            psta = connection.prepareStatement(sqlloading1);
            psta.setObject(1, heatPlanNo);
            psta.setObject(2, judge);
            psta.setObject(3, batch);
            psta.setObject(4, groupNo);
            psta.setObject(5, Integer.parseInt(count) + 1);
            ResultSet rs2 = psta.executeQuery();
            matNo = EntityTool.getEntitysByRs(rs2,
                    PortVrpBatchSampleResultEntity.class);
            rs2.close();
            psta.close();
            if (matNo == null) {
                core.setV_errMsg("管号队列全为样管，不能离线！");
                return core;
            }
            int batchCount = 0;
            sqlloading1 = CoreXmlSqlParsersFactory
                    .getSql("FrmVrpExpandResult.queryPortVrpBatchHg");// 查询管号队列里不是样管的合格支
            psta = connection.prepareStatement(sqlloading1);
            psta.setObject(1, heatPlanNo);
            rs2 = psta.executeQuery();
            if (rs2.next()) {
                batchCount = Integer.parseInt(rs2.getString("ACT_COUNT"));
            }
            rs2.close();
            psta.close();
            if (batchCount < Integer.parseInt(count)) {
                core.setV_errMsg("管号队列材料未交库合格支数不足,请重新输入离线支数！");
                return core;
            }
            String inListDoc = querySendSqlDoc(connection, "800708", "LX");// 入库单号
            String A = querySendSql(connection, inListDoc);
            int g = Integer.parseInt(A.substring(A.length() - 4, A.length()));


            String actdimater = "";
            String actheight = "";
            String actlen = "";
            String actlenmin = "";
            String actlenmax = "";

            String resultNo = "";
            String sqlloading = CoreXmlSqlParsersFactory
                    .getSql("FrmVrpExpandResult.queryOffMaxRESULT");// 查询离线最大实绩编号
            psta = connection.prepareStatement(sqlloading);
            psta.setObject(1, plinCode + slgx + "L" + matter1.format(dt));
            psta.setObject(2, matter.format(dt));
            psta.setObject(3, pln.getPlineCode());
            psta.setObject(4, plinCode + slgx + "L" + matter1.format(dt));
            ResultSet rs1 = psta.executeQuery();
            if (rs1.next()) {
                resultNo = rs1.getString("RESULT_NO");
            }
            rs1.close();
            psta.close();

            int countNum = 0;
            double weightNum = 0;
            for (PortVrpBatchSampleResultEntity prot : matNo) {
                countNum += Integer.parseInt(prot.getActCount());
                weightNum += Double.parseDouble(prot.getActWeight());
                // 修改管号队列状态
                pastupdatePath.setObject(1, num);
                pastupdatePath.setObject(2, num1);
                pastupdatePath.setObject(3, "2");
                pastupdatePath.setObject(4, num2);
                pastupdatePath.setObject(5, "20");
                pastupdatePath.setObject(6, "10");
                pastupdatePath.setObject(7, slgx);
                pastupdatePath.setObject(8, resultNo);
				pastupdatePath.setObject(9, heatPlanNo);
				pastupdatePath.setObject(10, prot.getMatNo());
                pastupdatePath.addBatch();


                // 修改材料表状态
                patMatNo.setObject(1, "80150104");
                //patMatNo.setObject(2, "11");
                patMatNo.setObject(2, prot.getMatNo());
                patMatNo.addBatch();
//                // 新增入库表数据
//                patIn.setObject(1, inListDoc
//                        + StringTool.formatDecimal(g, "0000"));
//                patIn.setObject(2, inListDoc);
//                patIn.setObject(3, "800708");
//                patIn.setObject(4, "离线入库");
//                patIn.setObject(5, "");
//                patIn.setObject(6, prot.getMatNo());
//                patIn.setObject(7, "");
//                patIn.setObject(8, "");
//                patIn.setObject(9, prot.getJudgeStoveNo());
//                patIn.setObject(10, prot.getBatchNo());
//                patIn.setObject(11, prot.getGroupNo());
//                patIn.setObject(12, "");
//                patIn.setObject(13, "");
//                patIn.setObject(14, "");
//                patIn.setObject(15, "");
//                patIn.setObject(16, "");
//                patIn.setObject(17, "");
//                patIn.setObject(18, pln.getGradecode());
//                patIn.setObject(19, pln.getGradename());
//                patIn.setObject(20, "");
//                patIn.setObject(21, "");
//                patIn.setObject(22, "");
//                patIn.setObject(23, "");
//                patIn.setObject(24, "");
//                patIn.setObject(25, "");
//                patIn.setObject(26, "");
//                patIn.setObject(27, "");
//                patIn.setObject(28, "");
//                patIn.setObject(29, pln.getPlineCode());
//                patIn.setObject(30, pln.getPlineName());
//                patIn.setObject(31, prot.getActDimater());
//                patIn.setObject(32, prot.getActHeight());
//                patIn.setObject(33, prot.getActLen());
//                patIn.setObject(34, prot.getActLenMin());
//                patIn.setObject(35, prot.getActLenMax());
//                patIn.setObject(36, prot.getActCount());
//                patIn.setObject(37, prot.getActWeight());
//                patIn.setObject(38, prot.getActTheoryWeight());
//                patIn.setObject(39, "");// 组织
//                patIn.setObject(40, "");
//                patIn.setObject(41, storageNo);//
//                patIn.setObject(42, storageNo + "10101");
//                patIn.setObject(43, "");
//                patIn.setObject(44, "");
//                patIn.setObject(45, storageAtte);
//                patIn.setObject(46, storageAtteName);
//                patIn.setObject(47, storageType);
//                patIn.setObject(48, storageTypeName);
//                patIn.setObject(49, managNo);
//                patIn.setObject(50, managName);
//                patIn.setObject(51, time);
//                patIn.setObject(52, bc);
//                patIn.setObject(53, bz);
//                patIn.setObject(54, time);
//                patIn.setObject(55, bc);
//                patIn.setObject(56, bz);
//                patIn.setObject(57, "");
//                patIn.setObject(58, name);
//                patIn.setObject(59, time);
//                if(pln.getOrdLnDlyPk()==""||pln.getOrdLnDlyPk()==null)
//                {
//                	 patIn.setObject(60, "");// 合同pK
//                     patIn.setObject(61, "");	
//                }else
//                {
//                patIn.setObject(60, pln.getOrdLnDlyPk().substring(0,
//                        pln.getOrdLnDlyPk().length() - 6));// 合同pK
//                patIn.setObject(61, pln.getOrdLnDlyPk().substring(0,
//                        pln.getOrdLnDlyPk().length() - 3));
//                }
//                patIn.setObject(62, pln.getOrdLnDlyPk());
//                patIn.setObject(63, depNo);
//                patIn.setObject(64, depName);
//                patIn.setObject(65, pln.getOrderNo());// 合同号
//                patIn.setObject(66, pln.getOrderSeq());
//                patIn.setObject(67, pln.getDeliveryNo());
//                patIn.setObject(68, "801401");
//                patIn.setObject(69, "80150104");
//                patIn.setObject(70, "");
//                patIn.setObject(71, "");
//                patIn.setObject(72, time);
//                patIn.setObject(73, "");
//                patIn.setObject(74, "");
//                patIn.setObject(75, "");
//                patIn.setObject(76, "");
//                patIn.setObject(77, "");
//                patIn.setObject(78, "");
//                patIn.setObject(79, "");
//                patIn.setObject(80, "");
//                patIn.setObject(81, "1");
//                patIn.addBatch();
//                g++;
                judge = prot.getJudgeStoveNo();
                batch = prot.getBatchNo();
                actdimater = prot.getActDimater();
                actheight = prot.getActHeight();
                actlen = prot.getActLen();
                actlenmin = prot.getActLenMin();
                actlenmax = prot.getActLenMax();
            }
            pastupdatePath.executeBatch();
           // patIn.executeBatch();
            patMatNo.executeBatch();


            for (PortVrpBatchSampleResultEntity lvMat : matNo) {
            // 新增离线实绩
            patAdd.setObject(1, resultNo);
            patAdd.setObject(2, heatPlanNo);
            patAdd.setObject(3, slgx);
            patAdd.setObject(4, slgx);
            patAdd.setObject(5, pln.getPlineCode());
            patAdd.setObject(6, pln.getPlineName());
            patAdd.setObject(7, pln.getZyBatchId());
            patAdd.setObject(8, pln.getProPlanId());
            patAdd.setObject(9, pln.getGxPlanNo());

            patAdd.setObject(10, judge);
            patAdd.setObject(11, batch);
            patAdd.setObject(12, pln.getProduccode());
            patAdd.setObject(13, pln.getProducname());
            patAdd.setObject(14, pln.getSteelcode());
            patAdd.setObject(15, pln.getSteelname());
            patAdd.setObject(16, pln.getGradecode());
            patAdd.setObject(17, pln.getGradename());
            patAdd.setObject(18, pln.getModelCode());
            patAdd.setObject(19, pln.getModelDesc());
            patAdd.setObject(20, pln.getStdCode());
            patAdd.setObject(21, pln.getStdName());
            patAdd.setObject(22, pln.getStdStyle());
            patAdd.setObject(23, pln.getStdStyleDesc());
            patAdd.setObject(24, pln.getAimoutdiameter());
                patAdd.setObject(25, pln.getAimwallthick());
            patAdd.setObject(26, actlen);
            patAdd.setObject(27, actlenmin);
            patAdd.setObject(28, actlenmax);

            patAdd.setObject(29, countNum);
            patAdd.setObject(30, weightNum);
            patAdd.setObject(31, time);
            patAdd.setObject(32, name);
            patAdd.setObject(33, bc);
            patAdd.setObject(34, bz);
            patAdd.setObject(35, pln.getOrdLnDlyPk());
            patAdd.setObject(36, pln.getOrderNo());
            patAdd.setObject(37, pln.getOrderSeq());
            patAdd.setObject(38, pln.getDeliveryNo());
            patAdd.setObject(39, groupNo);
            patAdd.setObject(40, pln.getFlagStoveNo());
            patAdd.setObject(41, txtMemo);
            patAdd.setObject(42, pln.getFeedHeatNo());

            
    		// 如果有废品新增到材料表
			if (!count.equals("") && !count.equals("0")) {

                PlanService.AddOffLineMatWithResultNo(connection,"E",load.getLoadUser(),"11",
                        lvMat.getMatNo(),slgx,heatPlanNo,resultNo);
				}
            }

            patMarkOff.setObject(1,pln.getHeatPlanNo());
            patMarkOff.setObject(2,pln.getJudgeStoveNo());
            patMarkOff.setObject(3,batch);
            patMarkOff.setObject(4,groupNo);
            patMarkOff.setObject(5,slgx);
            patMarkOff.addBatch();
            patMarkOff.executeBatch();

            patAdd.addBatch();
            patAdd.executeBatch();

            core.setV_errMsg("离线成功！");
            core.setResult(1);
            connection.commit();
        }
        catch (Exception e) {
            core.setV_errMsg(e.getMessage());
            if (connection != null) {
                try {
                    connection.rollback(); // 回滚事务
                }
                catch (Exception e2) {
                }
            }
        }
        finally {
            try {
                if (connection != null && !connection.isClosed()) {
                    connection.close();
                }
            }
            catch (Exception e2) {
            }
        }
        return core;
    }

    /**
     * 每支材料对应的入库单号
     * @param conn c
     * @param strlist 传递参数
     * @return obj
     * @throws Exception 异常处理
     */
    public String querySendSqlDoc(Connection conn, String strlist, String memo)
            throws Exception {
        String seq = "";
        PreparedStatement psta = null;
        Date dt = new Date();
        SimpleDateFormat matter1 = new SimpleDateFormat("yyyy-MM-dd");
        SimpleDateFormat matter2 = new SimpleDateFormat("yyMMdd");
        String sqlLovsqe = CoreXmlSqlParsersFactory
                .getSql("FrmVrpExpandResult.getInListDoc");
        psta = conn.prepareStatement(sqlLovsqe);
        psta.setString(1, memo);
        psta.setString(2, matter2.format(dt));
        psta.setString(3, matter1.format(dt));
        psta.setString(4, strlist);
        ResultSet rs = psta.executeQuery();
        if (rs.next()) {
            seq = rs.getString("INSTOCK_DOC");
        }
        rs.close();
        psta.close();
        return seq;
    }

    /**
     * 每支材料对应的入库流水号
     * @param conn c
     * @param strlist 传递参数
     * @return obj
     * @throws Exception 异常处理
     */
    public String querySendSql(Connection conn, String strlist)
            throws Exception {
        String seq = "";
        PreparedStatement psta = null;
        String sqlLovsqe = CoreXmlSqlParsersFactory
                .getSql("FrmVrpExpandResult.getInListDoc1");
        psta = conn.prepareStatement(sqlLovsqe);
        psta.setString(1, strlist);
        ResultSet rs = psta.executeQuery();
        if (rs.next()) {
            seq = rs.getString("INSTOCK_SQE");
        }
        rs.close();
        psta.close();
        return seq;
    }

}
