package com.steering.mes.mcp.heatTreatment;

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.comm.tools.StringTool;
import com.steering.mes.mcp.common.PlanService;
import com.steering.mes.mcp.entity.*;

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

/**
 * 尺寸外观检查实绩
 * @author lzh 2015-7-13
 */
public class AppearanceService extends DBCommon {

    /**
     * 按计划号查询尺寸外观检查实绩
     * @param planNo 计划号
     * @return
     */
    public CoreReturnObject getAppearanceResultById(String heraNo) {
        Object[] params = new Object[] {heraNo };
        return this.getDataBySqlID("AppearanceService.GetAppearanceResultById",
                params);
    }
    /**
     * 查询外观检验合格支数的和
     */
	public CoreReturnObject getQueryExpandResult1(String heatNo) {
		return this.getDataBySqlID("AppearanceService.getQueryExpandResult1",
				new Object[] { heatNo });
	}
	/**
	 * 查询相同炉计划ID下外观检测
	 */
	public CoreReturnObject getQueryExpandResultStuts(String heatNo,
			String plinCode) {
		return this.getDataBySqlID(
				"AppearanceService.getQueryExpandResultStuts", new Object[] {
						heatNo, plinCode });
	}
    /**
     * 保存尺寸外观检查实绩
     * @param result 尺寸外观检查实绩
     * @return
     */
    public CoreReturnObject saveAppearanceResult(String loadStr,
           String PlanNo, String slgx,ArrayList<String> list1Str,ArrayList<String> list2Str) {
        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");

        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("AppearanceService.AddAppearanceResult");// 新增外观检验实绩
            PreparedStatement patAdd = connection.prepareStatement(sqlAdd);
           /* String sqlAddD = CoreXmlSqlParsersFactory
                    .getSql("AppearanceService.AddAppearanceD");// 新增外观检验记录
            PreparedStatement patAddD = connection.prepareStatement(sqlAddD);*/
            String sqlupdate = CoreXmlSqlParsersFactory
                    .getSql("AppearanceService.updatePlnZyRclM");// 修改热处理计划表
            PreparedStatement patupdate = connection
                    .prepareStatement(sqlupdate);
            String addPlnInfo = CoreXmlSqlParsersFactory
                    .getSql("HttLoadingService.AddComInfoResult");// 新增公共信息
            PreparedStatement patsPlan = connection
                    .prepareStatement(addPlnInfo);
            String addPlnInfo1 = CoreXmlSqlParsersFactory
                    .getSql("HeatTreatmentService.AddComInfoResult1");// 修改队列表材料，工序状态
            PreparedStatement patsPlan1 = connection
                    .prepareStatement(addPlnInfo1);
            String updatePath = CoreXmlSqlParsersFactory
                    .getSql("AppearanceService.UpdatePath");// 修改队列表工艺路径
            PreparedStatement pastupdatePath = connection
                    .prepareStatement(updatePath);



            HttAppearancecheckResultEntity load = EntityTool.getEntityByJSONStr(loadStr,HttAppearancecheckResultEntity.class);

            // 获取热处理计划实体了
            PlnZyRclMEntity pln = PlanService.getSendHttPlanCInfo(connection,
                    PlanNo);


            if (pln == null)
                return core;
            if (pln.getPlanstatus().equals("3")) {
                core.setV_errMsg("该计划已关闭！");
                return core;
            }
            String ifMonitNo = "";
            if (pln.getIfMonitor().equals("是")) {
                ifMonitNo = "1";
            }
            else {
                ifMonitNo = "0";
            }


            String cutFalg = "";
            String sql = CoreXmlSqlParsersFactory
                    .getSql("HttLoadingService.qureyGxComInfo");// 查询工序是否是结炉状态
            psta = connection.prepareStatement(sql);
            psta.setString(1, load.getHeatPlanNo());
           // psta.setString(2, load.getPlineCode());
            psta.setString(2, slgx);
            psta.setString(3, pln.getRealProcessSeq());
            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();



            int ProNum = 0;
            int FailNum = 0;

            if(StringTool.isInteger(load.getCheckpieces())){
                ProNum = Integer.parseInt(load.getCheckpieces());

            }

            if (StringTool.isInteger(load.getWastNum())) {
                FailNum = Integer.parseInt(load.getWastNum());
            }

            if(ProNum<=0) {
                core.setV_errMsg("检查支数不能为0！");
                return core;
            }

            if (ProNum < FailNum) {
                core.setV_errMsg("作业支数不能小于废品支数！");
                return core;
            }
            if(load.getOdmax()!=null&&!load.getOdmax().equals("")&&load.getOdmin()!=null&&!load.getOdmin().equals(""))
            if(Double.parseDouble(load.getOdmax())<=Double.parseDouble(load.getOdmin())){
                core.setV_errMsg("外径范围上限必须要大于下限！");
                return core;
            }


            String resultNo = "";
            String sqlloading = CoreXmlSqlParsersFactory
                    .getSql("AppearanceService.queryLoadRESULT");// 查询最大实绩编号
            psta = connection.prepareStatement(sqlloading);
            psta.setObject(1, pln.getPlineCode() + slgx + matter1.format(dt));
            psta.setObject(2, pln.getPlineCode() + slgx + matter1.format(dt));

            ResultSet rs1 = psta.executeQuery();
            if (rs1.next()) {
                resultNo = rs1.getString("RESULT_NO");
            }
            rs1.close();
            psta.close();

            // 新增尺寸检验实绩
            patAdd.setObject(1, resultNo);
            patAdd.setObject(2, load.getZyBatchId());
            //patAdd.setObject(3, pln.getProdIndex());
            patAdd.setObject(3, pln.getProPlanId());
            patAdd.setObject(4, pln.getGxPlanNo());
            patAdd.setObject(5, load.getOdmax());
            patAdd.setObject(6, load.getOdmin());
            patAdd.setObject(7, load.getOutofroundness());
            patAdd.setObject(8, load.getEndstraightness());
            patAdd.setObject(9, load.getFulllengthstraightness());
            patAdd.setObject(10, load.getCheckpieces());
            patAdd.setObject(11, load.getPassNum());
            patAdd.setObject(12, load.getDubiousNum());
            patAdd.setObject(13, load.getWastNum());
            patAdd.setObject(14, load.getColTime());
            patAdd.setObject(15, load.getColUser());
            patAdd.setObject(16, load.getColShift());
            patAdd.setObject(17, load.getColGroup());
            patAdd.setObject(18, load.getHeatPlanNo());
            patAdd.setObject(19, systime);
            patAdd.setObject(20, pln.getPlineCode());
            patAdd.setObject(21, pln.getPlineName());
            patAdd.setObject(22, load.getProcessSeq());
            patAdd.setObject(23, resultNo);
            patAdd.setObject(24, load.getBatchNo());
            patAdd.setObject(25, load.getResult());
            patAdd.addBatch();

         // 新增热处理公共信息
            patsPlan.setObject(1, resultNo);
            patsPlan.setObject(2, pln.getZyBatchId());
           // patsPlan.setObject(3, pln.getProdIndex());
            patsPlan.setObject(3, pln.getProPlanId());
            patsPlan.setObject(4, pln.getGxPlanNo());
            patsPlan.setObject(5, pln.getOrderSource());
            patsPlan.setObject(6, pln.getOrderNo());
            patsPlan.setObject(7, pln.getOrderSeq());
            patsPlan.setObject(8, pln.getReqBlId());
            patsPlan.setObject(9, pln.getPlineCode());
            patsPlan.setObject(10, pln.getPlineName());
            patsPlan.setObject(11, slgx);
            patsPlan.setObject(12, "尺寸检验");
            patsPlan.setObject(13, pln.getGradecode());
            patsPlan.setObject(14, pln.getGradename());
            patsPlan.setObject(15, pln.getSteelcode());
            patsPlan.setObject(16, pln.getSteelname());
            patsPlan.setObject(17, pln.getOutdiameterIn());
            patsPlan.setObject(18, pln.getWallthickIn());
            patsPlan.setObject(19, "");
            patsPlan.setObject(20, "");
            patsPlan.setObject(21, "");
            patsPlan.setObject(22, "");
            patsPlan.setObject(23, "");
            patsPlan.setObject(24, "");
            patsPlan.setObject(25, "");
            patsPlan.setObject(26, "");
            patsPlan.setObject(27, pln.getOutwlId());
            patsPlan.setObject(28, pln.getOutwlDesc());
            patsPlan.setObject(29, pln.getInwlId());
            patsPlan.setObject(30, pln.getInwlDesc());
            patsPlan.setObject(31, pln.getMonitorPoint());
            patsPlan.setObject(32, ifMonitNo);
            patsPlan.setObject(33, pln.getPrdcrNo());
            patsPlan.setObject(34, "");            
            patsPlan.setObject(35, "");            
            patsPlan.setObject(36, pln.getStationRoad());
            patsPlan.setObject(37, pln.getCraftFileNo());
            patsPlan.setObject(38, pln.getCraftFileMan());
            patsPlan.setObject(39, pln.getCraftSeq());
            patsPlan.setObject(40, load.getCheckpieces());
            patsPlan.setObject(41, "");
            patsPlan.setObject(42, load.getWastNum());
            patsPlan.setObject(43, resultNo);
            patsPlan.setObject(44, "");
            patsPlan.setObject(45, "");
            patsPlan.setObject(46, "");
            patsPlan.setObject(47, "");
            patsPlan.setObject(48, "1");
            patsPlan.setObject(49, load.getColTime());
            patsPlan.setObject(50, "");
            patsPlan.setObject(51, load.getColUser());
            patsPlan.setObject(52, load.getColShift());
            patsPlan.setObject(53, load.getColGroup());
            patsPlan.setObject(54, "1");
            patsPlan.setObject(55, systime);
            patsPlan.setObject(56, "");
            patsPlan.setObject(57, load.getHeatPlanNo());
            patsPlan.setObject(58, pln.getJudgeStoveNo());
            patsPlan.setObject(59, pln.getBatchNo());
            patsPlan.setObject(60, "");
            patsPlan.setObject(61, pln.getProduccode());
            patsPlan.setObject(62, pln.getProducname());
            patsPlan.setObject(63, pln.getSpecCode());
            patsPlan.setObject(64, pln.getSpecName());
            patsPlan.setObject(65, load.getProcessSeq());
            patsPlan.addBatch();
 /*           for (int i = 0; i < list1Str.size(); i++) {
                // ArrayList<HttAppearancecheckResultEntityD> list =
                // (ArrayList<HttAppearancecheckResultEntityD>) list1.get(i);
                HttAppearancecheckResultDEntity resultD =EntityTool.getEntityByJSONStr(list1Str.get(i),HttAppearancecheckResultDEntity.class);
                patAddD.setObject(1, resultNo);
                patAddD.setObject(2, resultNo);           
                patAddD.setObject(3, resultD.getSeqNo());
                patAddD.setObject(4, resultD.getPipeNo());
                patAddD.setObject(5, resultD.getEndaStraightness());
                patAddD.setObject(6, resultD.getEndaAvgOd());
                patAddD.setObject(7, resultD.getEndaMaxOd());
                patAddD.setObject(8, resultD.getEndaMinOd());
                patAddD.setObject(9, resultD.getEndaOvality());
                patAddD.setObject(10, resultD.getEndbStraightness());
                patAddD.setObject(11, resultD.getEndbAvgOd());
                patAddD.setObject(12, resultD.getEndbMinOd());
                patAddD.setObject(13, resultD.getEndbMaxOd());
                patAddD.setObject(14, resultD.getEndbOvality());
                patAddD.setObject(15, resultD.getHeatnoLast());
                patAddD.setObject(16, systime);
                patAddD.setObject(17, load.getColUser());
                patAddD.addBatch();
                patAddD.executeBatch();
            }*/
            FrmHttBugD.AddHttWaste(connection, load.getHeatPlanNo(), resultNo, pln.getPlineCode(), load.getColUser(), load.getProcessSeq(), slgx, list1Str, list2Str);

            patAdd.executeBatch();
            patsPlan.executeBatch();

            // 修改工艺路径
            pastupdatePath.setObject(1, "1");
            pastupdatePath.setObject(2, load.getHeatPlanNo());
            pastupdatePath.addBatch();
            pastupdatePath.executeBatch();
            // 修改计划表
           
            patupdate.setObject(1, "70");
            patupdate.setObject(2, load.getHeatPlanNo());
            patupdate.addBatch();
            patupdate.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 deleteAppearanceResult(
            String heratNo,String rentNo,
            String slgx) {
        CoreReturnObject core = new CoreReturnObject();
        Connection connection = this.getDao(CoreConstantData.sysbeanstr)
                .getConnection();
        PreparedStatement psta = null;
        try {
            connection.setAutoCommit(false); // 取消自动提交事务
            String sqlDelete = CoreXmlSqlParsersFactory
                    .getSql("AppearanceService.deleteHttCrackResult");// 删除外观检验实绩
            PreparedStatement patDelete = connection
                    .prepareStatement(sqlDelete);
            String sqlDeleteD = CoreXmlSqlParsersFactory
                    .getSql("AppearanceService.deleteHttCrackResultD");// 删除抽检记录实绩
            PreparedStatement patDeleteD = connection
                    .prepareStatement(sqlDeleteD);
            String sqlDeleteD1 = CoreXmlSqlParsersFactory
                    .getSql("AppearanceService.deleteHttCrackResultD1");// 删除抽检记录实绩1
            PreparedStatement patDeleteD1 = connection
                    .prepareStatement(sqlDeleteD1);
            String sqlDeletepln = CoreXmlSqlParsersFactory
                    .getSql("AppearanceService.updatePlnZyRclM");// 删除时修改热处理计划表
            PreparedStatement patDeletepln = connection
                    .prepareStatement(sqlDeletepln);
            String updatePlnInfo = CoreXmlSqlParsersFactory
                    .getSql("HttLoadingService.deleteComInfoResult");// 删除公共信息
            PreparedStatement patsupdatePlan = connection
                    .prepareStatement(updatePlnInfo);
            String addPlnInfo1 = CoreXmlSqlParsersFactory
                    .getSql("HeatTreatmentService.AddComInfoResult1");// 查询材料状态和工序点编码
            PreparedStatement patsPlan1 = connection
                    .prepareStatement(addPlnInfo1);
            String updatePath2 = CoreXmlSqlParsersFactory
                    .getSql("AppearanceService.UpdatePath");// 修改队列表工艺路径
            PreparedStatement pastupdatePath = connection
                    .prepareStatement(updatePath2);




            // 获取热处理计划实体了
            PlnZyRclMEntity pln = PlanService.getSendHttPlanCInfo(connection,
                    heratNo);

            if (pln == null)
                return core;
            if (pln.getPlanstatus().equals("3")) {
                core.setV_errMsg("该计划已关闭！");
                return core;
            }


            String Sql=CoreXmlSqlParsersFactory
                    .getSql("AppearanceService.GetAppResult");
            Sql += " WHERE RESULT_NO = '" + rentNo + "'";
            HttAppearancecheckResultEntity result =  EntityTool.getEntityBySqlNoClsCon(Sql,connection,HttAppearancecheckResultEntity.class);

            if(result==null || result.getResultNo().equals("")){
                core.setV_errMsg("数据发生改变,请先刷新！");
                return core;
            }

            if(!pln.getPlineCode().equals(result.getPlineCode())){
                core.setV_errMsg("不允许删除其他产线实绩,若需删除,请离线委内回原产线进行删除！");
                return core;
            }

            if(!pln.getRealProcessSeq().equals(result.getProcessSeq())){
                core.setV_errMsg("该记录是上次热处理的实绩,无法删除！");
                return core;
            }


            String cutFalg = "";
            String sql = CoreXmlSqlParsersFactory
                    .getSql("HttLoadingService.qureyGxComInfo");// 查询工序是否是结炉状态
            psta = connection.prepareStatement(sql);
            psta.setString(1, result.getHeatPlanNo());
       //     psta.setString(2, result.getPlineCode());
            psta.setString(2, slgx);
            psta.setString(3, pln.getRealProcessSeq());
            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();

            int detCount =0;

            if(StringTool.isInteger(result.getWastNum())){
                detCount = Integer.parseInt(result.getWastNum());
            }


            FrmHttBugD.DeleteHttWaste(connection,heratNo,rentNo,pln.getPlineCode(),result.getProcessSeq(),slgx,detCount);

			

                    // 删除外观检验实绩
                    patDelete.setObject(1, rentNo);
                    patDelete.addBatch();

                    // 删除公共实绩
                    patsupdatePlan.setObject(1, rentNo);
                    patsupdatePlan.addBatch();

                    // 删除抽检记录
                    patDeleteD1.setObject(1, rentNo);
                    patDeleteD1.addBatch();

                
                patDelete.executeBatch();
                patsupdatePlan.executeBatch();
                patDeleteD1.executeBatch();



            // 修改工艺路径
            pastupdatePath.setObject(1, "0");
            pastupdatePath.setObject(2, rentNo);
            pastupdatePath.addBatch();
            pastupdatePath.executeBatch();
            // 修改计划表
            patDeletepln.setObject(1, "60");
            patDeletepln.setObject(2, rentNo);
            patDeletepln.addBatch();
            patDeletepln.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 updateAppearanceResult(String  loadStr,
            ArrayList<String> listStr,String plineCode,String SlGx) {
        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("AppearanceService.UpdateAppearanceResult");// 修改外观检验实绩
            PreparedStatement patDelete = connection
                    .prepareStatement(sqlDelete);
            String sqlDeleteD = CoreXmlSqlParsersFactory
                    .getSql("AppearanceService.UpdateAppearanceD");// 修改外观检验结果实绩
            PreparedStatement patDeleteD = connection
                    .prepareStatement(sqlDeleteD);
            String updatePlnInfo = CoreXmlSqlParsersFactory
                    .getSql("AppearanceService.updateComInfoResult");// 修改公共信息
            PreparedStatement patsupdatePlan = connection
                    .prepareStatement(updatePlnInfo);
            String addPlnInfo1 = CoreXmlSqlParsersFactory
                    .getSql("HeatTreatmentService.AddComInfoResult1");// 查询材料状态和工序点编码
            PreparedStatement patsPlan1 = connection
                    .prepareStatement(addPlnInfo1);
            HttAppearancecheckResultEntity load = EntityTool.getEntityByJSONStr(loadStr,HttAppearancecheckResultEntity.class);

            String cutFalg = "";
            String sql = CoreXmlSqlParsersFactory
                    .getSql("HeatTreatmentService.qureyGxComInfo");// 查询工序是否是结炉状态
            PreparedStatement psta = connection.prepareStatement(sql);
            psta.setString(1, load.getHeatPlanNo());
            psta.setString(2, plineCode);
            psta.setString(3, SlGx);
            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.getOdmax());
            patDelete.setObject(2, load.getOdmin());
            patDelete.setObject(3, load.getOutofroundness());
            patDelete.setObject(4, load.getEndstraightness());
            patDelete.setObject(5, load.getFulllengthstraightness());
            patDelete.setObject(6, load.getCheckpieces());
            patDelete.setObject(7, load.getPassNum());
            patDelete.setObject(8, load.getDubiousNum());
            patDelete.setObject(9, load.getWastNum());
            patDelete.setObject(10, load.getColTime());
            patDelete.setObject(11, load.getColShift());
            patDelete.setObject(12, load.getColGroup());
            patDelete.setObject(13, systime);
            patDelete.setObject(14, load.getColUser());
            patDelete.setObject(15, load.getResultNo());
            patDelete.addBatch();

            // 修改公共实绩
            patsupdatePlan.setObject(1, load.getPassNum());
            patsupdatePlan.setObject(2, "");
            patsupdatePlan.setObject(3, load.getWastNum());
            patsupdatePlan.setObject(4, "");
            patsupdatePlan.setObject(5, load.getColTime());
            patsupdatePlan.setObject(6, load.getColShift());
            patsupdatePlan.setObject(7, load.getColGroup());
            patsupdatePlan.setObject(8, load.getResultNo());
            patsupdatePlan.addBatch();
            // 修改抽检记录
            for (int i = 0; i < listStr.size(); i++) {
                HttAppearancecheckResultDEntity resultD =EntityTool.getEntityByJSONStr(listStr.get(i),HttAppearancecheckResultDEntity.class);
                patDeleteD.setObject(1, resultD.getPipeNo());
                patDeleteD.setObject(2, resultD.getEndaStraightness());
                patDeleteD.setObject(3, resultD.getEndaAvgOd());
                patDeleteD.setObject(4, resultD.getEndaMaxOd());
                patDeleteD.setObject(5, resultD.getEndaMinOd());
                patDeleteD.setObject(6, resultD.getEndaOvality());
                patDeleteD.setObject(7, resultD.getEndbStraightness());
                patDeleteD.setObject(8, resultD.getEndbAvgOd());
                patDeleteD.setObject(9, resultD.getEndbMinOd());
                patDeleteD.setObject(10, resultD.getEndbMaxOd());
                patDeleteD.setObject(11, resultD.getEndbOvality());
                patDeleteD.setObject(12, systime);
                patDeleteD.setObject(13, load.getColUser());
                patDeleteD.setObject(14, resultD.getResultNo());
                patDeleteD.setObject(15, resultD.getMResultNo());
                patDeleteD.addBatch();
            }
            patDelete.executeBatch();
            patsupdatePlan.executeBatch();
            patDeleteD.executeBatch();
            
            
            int count = 0;
            int num = 0;
            int FAIL = 0;
            String sqlloading1 = CoreXmlSqlParsersFactory
                    .getSql("HeatTreatmentService.queryExpandFailNUM");// 查询管号队列表里废品数量
            psta = connection.prepareStatement(sqlloading1);
            psta.setObject(1, load.getHeatPlanNo());
            psta.setObject(2, SlGx);
            ResultSet rs2 = psta.executeQuery();
            if (rs2.next()) {
                count = Integer.parseInt(rs2.getString("ACT_COUNT"));
            }
            rs2.close();
            psta.close();
            String sqlloading2 = CoreXmlSqlParsersFactory
                    .getSql("AppearanceService.queryExpandFailNUM1");// 查询工序废品数量
            psta = connection.prepareStatement(sqlloading2);
            psta.setObject(1, load.getHeatPlanNo());
            ResultSet rs3 = psta.executeQuery();
            if (rs3.next()) {
                FAIL = Integer.parseInt(rs3.getString("WAST_NUM"));
            }
            rs3.close();
            psta.close();
            if (count > FAIL) {
                num = count - FAIL;
                ArrayList<String> matNo = new ArrayList<String>();
                String sqlloading3 = CoreXmlSqlParsersFactory
                        .getSql("HeatTreatmentService.queryExpandNUM2");// 查询管号队列表里废品材料号
                psta = connection.prepareStatement(sqlloading3);
                psta.setObject(1, load.getHeatPlanNo());
                psta.setObject(2, SlGx);
                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, "600208");
                    patsPlan1.setObject(3, load.getHeatPlanNo());
                    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("HeatTreatmentService.queryExpandNUM");// 查询管号队列表里合格材料号
                psta = connection.prepareStatement(sqlloading4);
                psta.setObject(1, load.getHeatPlanNo());
                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, "600208");
                    patsPlan1.setObject(3, load.getHeatPlanNo());
                    patsPlan1.setObject(4, matNo1.get(num));
                    patsPlan1.addBatch();

                    PlanService.AddOffLineMat(connection,"F",load.getColUser(),"22",
                            matNo1.get(num), SlGx, load.getHeatPlanNo());
                }
                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 getAppearanceDById(String resutNo) {
        return this.getDataBySqlID("AppearanceService.GetAppearanceDById",
                new Object[] { resutNo });
    }
    /***
     * 结炉
     * @param zYgNo
     * @return
     */
    public CoreReturnObject updateDoBeside(ArrayList<String> parm) {
        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("HttLoadingService.doBeside");// 修改工序状态为结炉
            PreparedStatement updateSqlPsta = connection
                    .prepareStatement(updateSql);
            String updatePln = CoreXmlSqlParsersFactory
                    .getSql("HttLoadingService.doPlnBeside");// 修改计划表结炉状态
            PreparedStatement updatePlnPsta = connection
                    .prepareStatement(updatePln);
            // int count = 0;

            String AllFailUpdatePlanStatus = CoreXmlSqlParsersFactory
                    .getSql("HttLoadingService.updateAllFailStatus");//
            PreparedStatement psAllFailUpdatePlanStatus = connection
                    .prepareStatement(AllFailUpdatePlanStatus);

            // 获取热处理计划实体了
            PlnZyRclMEntity pln = PlanService.getSendHttPlanCInfo(connection,
                    parm.get(0));

            if(pln==null){
                cro.setV_errMsg("无炉计划数据,,不允许此操作！");
                return cro;
            }

            String cutFalg = "";
            sql = CoreXmlSqlParsersFactory
                    .getSql("HttLoadingService.qureyGxComInfo");// 查询工序是否是结炉状态
            psta = connection.prepareStatement(sql);
            psta.setString(1, parm.get(0));
           // psta.setString(2, parm.get(1));
            psta.setString(2, parm.get(2));
            psta.setString(3, pln.getRealProcessSeq());
            rs = psta.executeQuery();
            if (rs.next()) {
                cutFalg = rs.getString("STATION_STATUS");
            }
            else {
                cro.setV_errMsg("无当前工序实绩,无法进行结炉!");
                return cro;
            }
            if (cutFalg.equals("2")) {
                cro.setV_errMsg("已结炉,不允许此操作！");
                return cro;
            }
            rs.close();
            psta.close();


            if(pln.getPlineCode().equals("C015")){
                String cutFalg1 = "";
                sql = CoreXmlSqlParsersFactory.getSql("HttLoadingService.qureyGxComInfo");//上一工序没结炉，无法结炉
                psta = connection.prepareStatement(sql);
                psta.setString(1, parm.get(0));
                // psta.setString(2, parm.get(1));
                psta.setObject(2, "600209");
                psta.setObject(3, pln.getRealProcessSeq());
                rs = psta.executeQuery();
                if (rs.next()) {
                    cutFalg1 = rs.getString("STATION_STATUS");
                } else if ((pln.getIfMatchOdd().equals("2") || pln.getIfMatchOdd().equals("4")) && pln.getBesideStatus().equals("80")) {
                    cutFalg1 = "2";
                } else {
                    cro.setV_errMsg("上一工序没结炉,不允许结炉!");
                    return cro;

                }

                if (cutFalg1.equals("1")) {
                    cro.setV_errMsg("上一工序没结炉,不允许结炉!");
                    return cro;
                }
                rs.close();
                psta.close();
            }
            else {
                if(!pln.getBesideStatus().equals("40") &&!pln.getBesideStatus().equals("50") && !pln.getBesideStatus().equals("60")    ) {
                    String cutFalg1 = "1";
                    sql = CoreXmlSqlParsersFactory.getSql("HeatTreatmentService.qureyGxComInfoNew");//上一工序没结炉，无法结炉
                    psta = connection.prepareStatement(sql);
                    psta.setString(1, parm.get(0));
                    psta.setObject(2, Integer.parseInt(parm.get(2)) - 1);
                    psta.setObject(3, Integer.parseInt(parm.get(2)) - 2);
                    psta.setObject(4, Integer.parseInt(parm.get(2)) - 3);
                    psta.setObject(5, pln.getRealProcessSeq());
                    rs = psta.executeQuery();
                    if (rs.next()) {
                        cutFalg1 = rs.getString("STATION_STATUS");
                    } else if (pln.getIfMatchOdd().equals("2") && pln.getBesideStatus().equals("60")) {
                        cutFalg1 = "2";
                    } else {
                        cro.setV_errMsg("上一工序没结炉,不允许结炉!");
                        return cro;
                    }

                    if (cutFalg1 == null || cutFalg1.equals("null") || cutFalg1.equals("")) {
                        if (( pln.getIfMatchOdd().equals("2") || pln.getIfMatchOdd().equals("4") ) && pln.getBesideStatus().equals("60")) {
                            cutFalg1 = "2";
                        }
                    }

                    if (cutFalg1 == null || cutFalg1.equals("1")) {
                        cro.setV_errMsg("上一工序没结炉,不允许结炉!");
                        return cro;
                    }
                    rs.close();
                    psta.close();
                }

            }

            int lxCount = 0;
            sql = CoreXmlSqlParsersFactory
                    .getSql("AppearanceService.qureyGxLxCount");// 查询是否离线
            psta = connection.prepareStatement(sql);
            psta.setString(1, parm.get(0));
           // psta.setString(2, parm.get(1));
            psta.setString(2, 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;
            }


            updateSqlPsta.setString(1, parm.get(0));
      //      updateSqlPsta.setString(2, parm.get(1));
            updateSqlPsta.setString(2, parm.get(2));
            updateSqlPsta.setString(3, pln.getRealProcessSeq());
            updateSqlPsta.addBatch();

            // 修改计划结炉状态
            updatePlnPsta.setObject(1, "70");
            updatePlnPsta.setObject(2, parm.get(0));
            updatePlnPsta.addBatch();

            updateSqlPsta.executeBatch();
            updatePlnPsta.executeBatch();

            psAllFailUpdatePlanStatus.setString(1, parm.get(0));
            psAllFailUpdatePlanStatus.addBatch();
            psAllFailUpdatePlanStatus.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) {
        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("HttLoadingService.deleteBeside");// 修改工序状态撤销结炉
            PreparedStatement updateSqlPsta = connection
                    .prepareStatement(updateSql);
            String updatePln = CoreXmlSqlParsersFactory
                    .getSql("HttLoadingService.doPlnBeside");// 修改计划表结炉状态
            PreparedStatement updatePlnPsta = connection
                    .prepareStatement(updatePln);


            String AllFailUpdatePlanStatus = CoreXmlSqlParsersFactory
                    .getSql("HttLoadingService.CancelAllFailStatus");
            PreparedStatement psAllFailUpdatePlanStatus = connection
                    .prepareStatement(AllFailUpdatePlanStatus);

            // 获取热处理计划实体了
            PlnZyRclMEntity pln = PlanService.getSendHttPlanCInfo(connection,
                    parm.get(0));

            if(pln==null){
                cro.setV_errMsg("无炉计划数据,,不允许此操作！");
                return cro;
            }


            String cutFalg = "";
            sql = CoreXmlSqlParsersFactory
                    .getSql("HttLoadingService.qureyGxComInfo");// 查询工序是否是结炉状态
            psta = connection.prepareStatement(sql);
            psta.setString(1, parm.get(0));
         //   psta.setString(2, parm.get(1));
            psta.setString(2, parm.get(2));
            psta.setString(3, pln.getRealProcessSeq());
            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(2, parm.get(2));
            updateSqlPsta.setString(3,  pln.getRealProcessSeq());
            updateSqlPsta.addBatch();

            // 修改计划结炉状态
            updatePlnPsta.setObject(1,  pln.getPlineCode().equals("C015")?"80":"60");
            updatePlnPsta.setObject(2, parm.get(0));
            updatePlnPsta.addBatch();
            updatePlnPsta.executeBatch();
            updateSqlPsta.executeBatch();

            psAllFailUpdatePlanStatus.setString(1, parm.get(0));
            psAllFailUpdatePlanStatus.addBatch();
            psAllFailUpdatePlanStatus.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(ArrayList<HttOfflineResultDentity> list1,HttAppearancecheckResultEntity load,
            ArrayList<String> list, String plinCode, String storageNo,String groudno) {
        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("AppearanceService.AddOffLine");// 新增离线实绩
            PreparedStatement patAdd = connection.prepareStatement(addOff);
            String addOffD = CoreXmlSqlParsersFactory
                    .getSql("AppearanceService.AddOffLineD");// 新增离线实绩从表
            PreparedStatement patAddD = connection.prepareStatement(addOffD);

            String updateMat = CoreXmlSqlParsersFactory
                    .getSql("AppearanceService.UpdateMatNo");// 修改材料状态
            PreparedStatement patMatNo = connection.prepareStatement(updateMat);

            String updatePath = CoreXmlSqlParsersFactory
                    .getSql("AppearanceService.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);

            int num = 0;
            int num1 = 0;
            int num2 = 0;
                if (slgx.equals("600202")) {
                    num = 1;
                    num1 = 2;
                    num2 = 4;
                }
                else if (slgx.equals("600203")) {
                    num = 1;
                    num1 = 4;
                    num2 = 6;
                }
                else if (slgx.equals("600204")) {
                    num = 1;
                    num1 = 6;
                    num2 = 8;
                }
                else if (slgx.equals("600205")) {
                    num = 1;
                    num1 = 8;
                    num2 = 10;
                }
                else if (slgx.equals("600206")) {
                    num = 1;
                    num1 = 10;
                    num2 = 12;
                }
                else if (slgx.equals("600207")) {
                    num = 1;
                    num1 = 12;
                    num2 = 14;
                }
                else if (slgx.equals("600208")) {
                    num = 1;
                    num1 = 16;
                    num2 = 18;
                }
                else if (slgx.equals("600209")) {
                    num = 1;
                    num1 = 18;
                    num2 = 20;
                }
                else if (slgx.equals("600210")) {
                    num = 1;
                    num1 = 20;
                    num2 = 22;
                }
                else if (slgx.equals("600211")) {
                    num = 1;
                    num1 = 22;
                    num2 = 24;
                }
         
            // 获取墩拔扩计划实体类
                PlnZyRclMEntity pln = PlanService.getSendHttPlanCInfo(connection,
                    heatPlanNo);
             // 获取墩拔扩计划从表实体类
                PlnZyRclCEntity plnC = PlanService.getSendHttPlanCInfoC(connection,
                    heatPlanNo);
            if (pln == null)
                return core;
            // if (!pln.getPlanstatus().equals("2")) {
            // core.setV_errMsg("该计划不是生产中的，请确认是否生产完成或未上料！");
            // return core;
            // }
            String cutFalg = "";
            String sql = CoreXmlSqlParsersFactory
                    .getSql("AppearanceService.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<PortHttBatchSampleResultEntity> matNo = new ArrayList<PortHttBatchSampleResultEntity>();
            String sqlloading1 = CoreXmlSqlParsersFactory
                    .getSql("AppearanceService.queryPortVrpHg");// 查询管号队列里合格的材料号
            psta = connection.prepareStatement(sqlloading1);
            psta.setObject(1, heatPlanNo);
            psta.setObject(2, groudno);
            psta.setObject(3, Integer.parseInt(count) + 1);
            ResultSet rs2 = psta.executeQuery();
            matNo = EntityTool.getEntitysByRs(rs2,
            		PortHttBatchSampleResultEntity.class);
            rs2.close();
            psta.close();
            if (matNo == null) {
                core.setV_errMsg("管号队列全为样管，不能离线！");
                return core;
            }
            int batchCount = 0;
            sqlloading1 = CoreXmlSqlParsersFactory
                    .getSql("AppearanceService.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 judge = "";
            String batch = "";
            String procode = "";
            String proname = "";
            String stecode = "";
            String stename = "";
            String gracode = "";
            String graname = "";
            String modecode = "";
            String modedesc = "";
            String stdcode = "";
            String stdname = "";
            String stdstyle = "";
            String stdstyledesc = "";
            String actdimater = "";
            String actheight = "";
            String actlen = "";
            String actlenmin = "";
            String actlenmax = "";

            int countNum = 0;
            double weightNum = 0;
            for (PortHttBatchSampleResultEntity 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, heatPlanNo);
                pastupdatePath.setObject(9, prot.getMatNo());
                pastupdatePath.addBatch();


                // 修改材料表状态
                patMatNo.setObject(1, "80150104");
                //patMatNo.setObject(2, "11");
                patMatNo.setObject(2, prot.getMatNo());
                patMatNo.addBatch();
//                List<MatZcMEntity> listMat = new ArrayList<MatZcMEntity>();
//				listMat = EntityTool.getEntitysByRs(rs, MatZcMEntity.class);
//				for (MatZcMEntity lvMat : listMat) {
//                // 新增入库表数据
//                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);
////                patIn.setObject(60, pln.getOrdLnDlyPk().substring(0,
////                        pln.getOrdLnDlyPk().length() - 6));// 合同pK
////                patIn.setObject(61, pln.getOrdLnDlyPk().substring(0,
////                        pln.getOrdLnDlyPk().length() - 3));
//                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();
            patMatNo.executeBatch();

            String ifMonitNo = "";
            if (pln.getIfMonitor().equals("是")) {
                ifMonitNo = "1";
            }
            else {
                ifMonitNo = "0";
            }
            String resultNo = "";
            String sqlloading = CoreXmlSqlParsersFactory
                    .getSql("AppearanceService.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());
            ResultSet rs1 = psta.executeQuery();
            if (rs1.next()) {
                resultNo = rs1.getString("RESULT_NO");
            }
            rs1.close();
            psta.close();

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

            patAdd.setObject(11, judge);
            patAdd.setObject(12, batch);
            patAdd.setObject(13, groudno);
            patAdd.setObject(14, pln.getProduccode());
            patAdd.setObject(15, pln.getProducname());
            patAdd.setObject(16, pln.getSteelcode());
            patAdd.setObject(17, pln.getSteelname());
            patAdd.setObject(18, pln.getGradecode());
            patAdd.setObject(19, pln.getGradename());
            patAdd.setObject(20, pln.getModelCode());
            patAdd.setObject(21, pln.getModelDesc());
            patAdd.setObject(22, pln.getStdCode());
            patAdd.setObject(23, pln.getStdName());
            patAdd.setObject(24, pln.getStdStyle());
            patAdd.setObject(25, pln.getStdStyleDesc());
            patAdd.setObject(26, actdimater);
            patAdd.setObject(27, actheight);
            patAdd.setObject(28, actlen);
            patAdd.setObject(29, actlenmin);
            patAdd.setObject(30, actlenmax);
            patAdd.setObject(31, pln.getOrdLnDlyPk());
            patAdd.setObject(32, pln.getOrderNo());
            patAdd.setObject(33, pln.getOrderSeq());
            patAdd.setObject(34, pln.getDeliveryNo());
            patAdd.setObject(35, countNum);
            patAdd.setObject(36, weightNum);
            patAdd.setObject(37, name);
            patAdd.setObject(38, bc);
            patAdd.setObject(39, bz);
            patAdd.setObject(40, time);
            patAdd.setObject(41, "1");
            patAdd.setObject(42, pln.getFeedHeatNo());
            // 如果有离线新增到材料表
         			if (!count.equals("") && !count.equals("0")) {
                        PlanService.AddOffLineMat(connection,"F",load.getColUser(),"11",
                                lvMat.getMatNo(),slgx, heatPlanNo);
         				}
            }

            // 新增离线从表
            for (int i = 0; i < list1.size(); i++) {
                String resultsqe = "";
                sqlloading = CoreXmlSqlParsersFactory
                        .getSql("AppearanceService.querymaxsqe");// 查询从表最大序号 
                psta = connection.prepareStatement(sqlloading);
                psta.setObject(1, matter1.format(now));
                psta.setObject(2, heatPlanNo);
                psta.setObject(3, plinCode);
                ResultSet rs3 = psta.executeQuery();
                if (rs3.next()) {
                    resultsqe = rs3.getString("RESULT_SQE");
                }
            
                rs3.close();
                psta.close();
                HttOfflineResultDentity resultD = list1.get(i);
                patAddD.setObject(1, resultNo);
                patAddD.setObject(2, resultsqe);
                patAddD.setObject(3, heatPlanNo);
                patAddD.setObject(4, plinCode);
                patAddD.setObject(5, pln.getPlineName());
                patAddD.setObject(6, count);	          
                patAddD.setObject(7, resultD.getGowhereCode());
                patAddD.setObject(8, resultD.getGowhereName());
                patAddD.setObject(9, time);
                patAddD.setObject(10, slgx);
                patAddD.setObject(11, slgxname);
                patAddD.addBatch();
                patAddD.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("AppearanceService.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("AppearanceService.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;
    }

    /***
     * 生成条码
     * @param zYgNo
     * @return
     */
    public CoreReturnObject GenerateBarcode(String HeatPlanNo) {
        CoreReturnObject cro = new CoreReturnObject();
        Connection connection = this.getDao(CoreConstantData.sysbeanstr)
                .getConnection();
        String sql = "";
        PreparedStatement psta = null;
        ResultSet rs = null;
        try {
            connection.setAutoCommit(false); // 取消自动提交事务
            String updateMat = CoreXmlSqlParsersFactory
                    .getSql("AppearanceService.UpdatePortHttBarCode");// 修改材料状态
            PreparedStatement patMatNo = connection.prepareStatement(updateMat);



            ArrayList<String> BatchNos =  new ArrayList<String>();

            sql = CoreXmlSqlParsersFactory
                    .getSql("HttLoadingService.QueryPhyBatchNo");
            psta = connection.prepareStatement(sql);
            psta.setString(1, HeatPlanNo);
            rs = psta.executeQuery();

            while (rs.next()){
                BatchNos.add(rs.getString("PHY_BATCH_NO"));
            }

            rs.close();
            psta.close();

            if(BatchNos.size()<=0){
                cro.setV_errMsg("请先分批再生成管号！");
                return cro;
            }

            for (String Batch :  BatchNos
                 ) {
                sql = CoreXmlSqlParsersFactory
                        .getSql("HttLoadingService.QueryPortHttByPhyBatch");//
                psta = connection.prepareStatement(sql);
                psta.setString(1, HeatPlanNo);
                psta.setString(2, Batch);
                rs = psta.executeQuery();

                List<PortHttBatchSampleResultEntity> Entitys = EntityTool.getEntitysByRs(rs,
                        PortHttBatchSampleResultEntity.class);

                Integer MaxSeq = 0;

                sql = CoreXmlSqlParsersFactory
                        .getSql("HttLoadingService.QueryMaxBarCode");//
                psta = connection.prepareStatement(sql);
                psta.setString(1, HeatPlanNo);
                psta.setString(2, Batch);
                rs = psta.executeQuery();
                if(rs.next()){
                    MaxSeq = rs.getInt("MaxSeq");
                }

                for (PortHttBatchSampleResultEntity Entity  :Entitys
                     ) {
                    if(Entity.getInstoreFlag().equals("1")) {
                        cro.setV_errMsg("批号" + Batch + "有未生成管号的管已入库，请撤销入库再生成管号！");
                        return cro;
                    }
                    patMatNo.setObject(1,MaxSeq++);
                    patMatNo.setObject(2,Entity.getHeatPlanNo());
                    patMatNo.setObject(3,Entity.getMatNo());
                    patMatNo.addBatch();

                }

            }

            patMatNo.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;
    }


    public CoreReturnObject UpdateReportUrl(String HeatPlanID, String Url) {
        return this.setDataBySqlID("AppearanceService.UpdateReportUrl",
                new Object[]{Url, HeatPlanID});
    }

}
