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.FrmMchPersonentity;
import com.steering.mes.mcp.entity.HttWasteResultEntity;
import com.steering.mes.mcp.entity.PortHttBatchSampleResultEntity;

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

public class FrmHttBugD extends DBCommon {
    public CoreReturnObject doQuery(String banci, String pline) {

        String sql = "";
        sql = CoreXmlSqlParsersFactory.getSql("FrmHttPerson.doQuery");

        if (!banci.equals("")) {
            sql += " AND T.PRO_BZ ='" + banci + "'";
        }
        if (!pline.equals("")) {
            sql += " AND T.PLINE_CODE ='" + pline + "'";
        }
        sql += " ORDER BY T.PLINE_CODE";
        return this.getData(sql);
    }

    public CoreReturnObject doQuerypline() {
        String sql = CoreXmlSqlParsersFactory.getSql("FrmHttPerson.doQuerypline");
        return this.getData(sql);
    }

    //查询可疑支数
    public CoreReturnObject doQueryAlarmNum(String heatNo, String resultNo) {
        String sql = CoreXmlSqlParsersFactory.getSql("FrmHttBugD.doQueryAlarmNum");
        if (!heatNo.equals("")) {
            sql += " AND T.HEAT_PLAN_NO ='" + heatNo + "'";
        }
        if (!resultNo.equals("")) {
            sql += " AND T.RESULT_NO ='" + resultNo + "'";
        }
        return this.getData(sql);
    }

    //查询废品支数
    public CoreReturnObject doQueryScrapNum(String heatNo, String resultNo) {
        String sql = CoreXmlSqlParsersFactory.getSql("FrmHttBugD.doQueryScrapNum");
        if (!heatNo.equals("")) {
            sql += " AND T.HEAT_PLAN_NO ='" + heatNo + "'";
        }
        if (!resultNo.equals("")) {
            sql += " AND T.RESULT_NO ='" + resultNo + "'";
        }
        return this.getData(sql);
    }

    //查询废品总米长
    public CoreReturnObject doQueryScrapLen(String heatNo, String resultNo) {
        String sql = CoreXmlSqlParsersFactory.getSql("FrmHttBugD.doQueryScrapLen");
        if (!heatNo.equals("")) {
            sql += " AND T.HEAT_PLAN_NO ='" + heatNo + "'";
        }
        if (!resultNo.equals("")) {
            sql += " AND T.RESULT_NO ='" + resultNo + "'";
        }
        return this.getData(sql);
    }

    //查

    public static void AddHttWaste(Connection connection, String heatNo, String resultNo, String PlineCode, String username, String ProcessSeq, String gx, ArrayList<String> list3, ArrayList<String> list4, ArrayList<String> list5) throws Exception {
        PreparedStatement psta = null;

        String sqlAddW = CoreXmlSqlParsersFactory.getSql("FrmHttBugD.AddExpandDResult");// 新增可疑从表实绩
        PreparedStatement patAddW = connection.prepareStatement(sqlAddW);

        String sqlAddD1 = CoreXmlSqlParsersFactory.getSql("FrmHttBugD.AddExpandDResult1");// 新增废品从表实绩
        PreparedStatement patAddD1 = connection.prepareStatement(sqlAddD1);

        String sqlAddD3 = CoreXmlSqlParsersFactory.getSql("FrmHttBugD.AddExpandDResult3");// 新增废品从表实绩
        PreparedStatement patAddD3 = connection.prepareStatement(sqlAddD3);


        String UpdatePort = CoreXmlSqlParsersFactory.getSql("FrmHttBugD.UpdatePortHtt");// 更改管号队列状态
        PreparedStatement patsUpdatePort = connection.prepareStatement(UpdatePort);

        String UpdatePortMatNo = CoreXmlSqlParsersFactory.getSql("FrmHttBugD.AddWasteMatNo");// 新增废品从表实绩
        PreparedStatement patsUpdatePortMatNo = connection.prepareStatement(UpdatePortMatNo);

        // 新增可疑从表实绩
        for (int i = 0; i < list3.size(); i++) {
            String failBill = "";
            String sqlloading2 = CoreXmlSqlParsersFactory.getSql("FrmHttBugD.queryMaxfailBillNew");// 生成废品缺陷编号
            psta = connection.prepareStatement(sqlloading2);
            psta.setObject(1, resultNo);
            psta.setObject(2, resultNo);
            ResultSet rs4 = psta.executeQuery();
            if (rs4.next()) {
                failBill = rs4.getString("DEF_SERIAL_ITEM");
            }
            rs4.close();
            psta.close();
            String mresultNo = "";
            String sqlloading3 = CoreXmlSqlParsersFactory.getSql("FrmHttBugD.queryJumpDRESULT");// 查询从表最大实绩编号
            psta = connection.prepareStatement(sqlloading3);
            psta.setObject(1, heatNo);
            ResultSet rs3 = psta.executeQuery();
            if (rs3.next()) {
                mresultNo = rs3.getString("DEF_SERIAL_NO");
            }

            rs3.close();
            psta.close();
            HttWasteResultEntity resultD = EntityTool.getEntityByJSONStr(list3.get(i), HttWasteResultEntity.class);
            patAddW.setObject(1, failBill);
            patAddW.setObject(2, mresultNo);
            patAddW.setObject(3, resultNo);
            patAddW.setObject(4, heatNo);
            patAddW.setObject(5, resultD.getDefFillCode());
            patAddW.setObject(6, resultD.getDefFillName());
            patAddW.setObject(7, resultD.getDefFillNum());
            patAddW.setObject(8, resultD.getDefFillWt());
            patAddW.setObject(9, PlineCode);
            patAddW.setObject(10, resultD.getBatchNo());
            patAddW.addBatch();
            patAddW.executeBatch();

            String[] MatNos = resultD.getMatNo().split(",");

            for (String matNo : MatNos) {
                patsUpdatePortMatNo.setObject(1, failBill);
                patsUpdatePortMatNo.setObject(2, mresultNo);
                patsUpdatePortMatNo.setObject(3, matNo);
                patsUpdatePortMatNo.addBatch();
            }

            patsUpdatePortMatNo.executeBatch();
        }

        //
        for (int i = 0; i < list5.size(); i++) {
            String failBill = "";
            String sqlloading2 = CoreXmlSqlParsersFactory.getSql("FrmHttBugD.queryMaxfailBillNew");// 生成废品缺陷编号
            psta = connection.prepareStatement(sqlloading2);
            psta.setObject(1, resultNo);
            psta.setObject(2, resultNo);
            ResultSet rs4 = psta.executeQuery();
            if (rs4.next()) {
                failBill = rs4.getString("DEF_SERIAL_ITEM");
            }
            rs4.close();
            psta.close();
            String mresultNo = "";
            String sqlloading3 = CoreXmlSqlParsersFactory.getSql("FrmHttBugD.queryJumpDRESULT");// 查询从表最大实绩编号
            psta = connection.prepareStatement(sqlloading3);
            psta.setObject(1, heatNo);
            ResultSet rs3 = psta.executeQuery();
            if (rs3.next()) {
                mresultNo = rs3.getString("DEF_SERIAL_NO");
            }

            rs3.close();
            psta.close();
            HttWasteResultEntity resultD = EntityTool.getEntityByJSONStr(list5.get(i), HttWasteResultEntity.class);
            patAddD3.setObject(1, failBill);
            patAddD3.setObject(2, mresultNo);
            patAddD3.setObject(3, resultNo);
            patAddD3.setObject(4, heatNo);
            patAddD3.setObject(5, PlineCode);
            patAddD3.setObject(6, resultD.getBatchNo());
            patAddD3.setObject(7, resultD.getTotalLen());
            patAddD3.addBatch();
            patAddD3.executeBatch();

            String[] MatNos = resultD.getMatNo().split(",");

            for (String matNo : MatNos) {
                patsUpdatePortMatNo.setObject(1, failBill);
                patsUpdatePortMatNo.setObject(2, mresultNo);
                patsUpdatePortMatNo.setObject(3, matNo);
                patsUpdatePortMatNo.addBatch();
            }

            patsUpdatePortMatNo.executeBatch();
        }



        // 新增废品从表实绩
        for (int j = 0; j < list4.size(); j++) {

            HttWasteResultEntity resultD = EntityTool.getEntityByJSONStr(list4.get(j), HttWasteResultEntity.class);
            int count = 0;
            if (StringTool.isInteger(resultD.getDefFillNum())) {
                count = Integer.parseInt(resultD.getDefFillNum());
            } else {
                throw new Exception("请输入正确的废品支数");
            }

/*            ArrayList<String> matNo = new ArrayList<String>();
            String sqlloading1 = CoreXmlSqlParsersFactory
                    .getSql("FrmHttBugD.queryExpandNUMWithProcessSeq");// 查询管号队列里合格的材料号
            psta = connection.prepareStatement(sqlloading1);
            psta.setObject(1, heatNo);
            psta.setObject(2,ProcessSeq);
            psta.setObject(3,resultD.getBatchNo());
            psta.setObject(4,resultD.getBatchNo());
            psta.setObject(5, count + 1);
            ResultSet rs2 = psta.executeQuery();
            while (rs2.next()) {
                matNo.add(rs2.getString("MAT_NO"));
            }
            rs2.close();
            psta.close();

            if(matNo.size()<count){
                throw new Exception("批号" +resultD.getBatchNo() +"没有足够的合格支数");
            }*/


            String failBill = "";
            String sqlloading2 = CoreXmlSqlParsersFactory.getSql("FrmHttBugD.queryMaxfailBillNew");// 生成废品缺陷编号
            psta = connection.prepareStatement(sqlloading2);
            psta.setObject(1, resultNo);
            psta.setObject(2, resultNo);
            ResultSet rs3 = psta.executeQuery();
            if (rs3.next()) {
                failBill = rs3.getString("DEF_SERIAL_ITEM");
            }
            rs3.close();
            psta.close();
            String mresultNo = "";
            String sqlloading4 = CoreXmlSqlParsersFactory.getSql("FrmHttBugD.queryJumpDRESULT");// 查询从表最大实绩编号
            psta = connection.prepareStatement(sqlloading4);
            psta.setObject(1, heatNo);
            ResultSet rs4 = psta.executeQuery();
            if (rs4.next()) {
                mresultNo = rs4.getString("DEF_SERIAL_NO");
            }
            rs4.close();
            psta.close();

/*
            for (int i = 0; i < matNo.size(); i++) {
                patsUpdatePort.setObject(1, "10");
                patsUpdatePort.setObject(2, gx);
                patsUpdatePort.setObject(3, resultNo);
                patsUpdatePort.setObject(4, heatNo);
                patsUpdatePort.setObject(5, matNo.get(i));
                patsUpdatePort.addBatch();

                PlanService.AddOffLineMat(connection,"F",username,"22",
                        matNo.get(i),gx, heatNo);
            }
*/


            patAddD1.setObject(1, failBill);
            patAddD1.setObject(2, mresultNo);
            patAddD1.setObject(3, resultNo);
            patAddD1.setObject(4, heatNo);
            patAddD1.setObject(5, resultD.getDefFillCode());
            patAddD1.setObject(6, resultD.getDefFillName());
            patAddD1.setObject(7, resultD.getDefFillNum());
            patAddD1.setObject(8,  resultD.getDefFillWt());
            patAddD1.setObject(9, PlineCode);
            patAddD1.setObject(10, resultD.getBatchNo());
            patAddD1.addBatch();
            patAddD1.executeBatch();


            String[] MatNos = resultD.getMatNo().split(",");

            for (String matNo : MatNos) {

                String sql = CoreXmlSqlParsersFactory.getSql("FrmHttBugD.queryPortHttWithKey");// 查询管号队列里合格的材料号
                psta = connection.prepareStatement(sql);
                psta.setObject(1, heatNo);
                psta.setObject(2, ProcessSeq);
                psta.setObject(3, matNo);
                ResultSet rs2 = psta.executeQuery();

                List<PortHttBatchSampleResultEntity> sendPlanCEntitys = EntityTool.getEntitysByRs(rs2, PortHttBatchSampleResultEntity.class);
                rs2.close();
                psta.close();

                if (sendPlanCEntitys.size() <= 0) {
                    throw new Exception("找不到管号" + matNo + "的信息");
                }

                if (!gx.equals("600201") && !sendPlanCEntitys.get(0).getPhyBatchNo().equals(resultD.getBatchNo())) {
                    throw new Exception("管号" + matNo + "数据发生改变,不属于批号" + resultD.getBatchNo() + "下");
                }

                if (sendPlanCEntitys.get(0).getInstoreFlag().equals("1")) {
                    throw new Exception("管号" + matNo + "已经入库");
                }

                if (!sendPlanCEntitys.get(0).getMatStatus().equals("00")) {
                    throw new Exception("管号" + matNo + "不是合格管");
                }

                patsUpdatePort.setObject(1, "10");
                patsUpdatePort.setObject(2, gx);
                patsUpdatePort.setObject(3, resultNo);
                patsUpdatePort.setObject(4, heatNo);
                patsUpdatePort.setObject(5, matNo);
                patsUpdatePort.addBatch();

                PlanService.AddOffLineMat(connection, "F", username, "22", matNo, gx, heatNo, resultD.getDefFillName());

                patsUpdatePortMatNo.setObject(1, failBill);
                patsUpdatePortMatNo.setObject(2, mresultNo);
                patsUpdatePortMatNo.setObject(3, matNo);
                patsUpdatePortMatNo.addBatch();

            }
            patsUpdatePort.executeBatch();
            patsUpdatePortMatNo.executeBatch();
        }

    }

    public static void AddHttWaste(Connection connection, String heatNo, String resultNo, String PlineCode, String username, String ProcessSeq, String gx, ArrayList<String> list3, ArrayList<String> list4) throws Exception {
        AddHttWaste(connection, heatNo, resultNo, PlineCode, username, ProcessSeq, gx, list3, list4, new ArrayList<String>());
    }


    public static void DeleteHttWaste(Connection connection, String heatNo, String resultNo, String PlineCode, String ProcessSeq, String gx, int detCount) throws Exception {

        if (detCount > 0) {

            PreparedStatement psta = null;

            ArrayList<String> matNo = new ArrayList<String>();
            String sqlloading3 = CoreXmlSqlParsersFactory.getSql("HeatTreatmentService.QueryFailPortHtt");// 查询管号队列表里废品材料号
            psta = connection.prepareStatement(sqlloading3);
            psta.setObject(1, heatNo);
            psta.setObject(2, gx);
            psta.setObject(3, ProcessSeq);
            psta.setObject(4, resultNo);
            psta.setObject(5, detCount + 1);
            ResultSet rs4 = psta.executeQuery();
            while (rs4.next()) {
                matNo.add(rs4.getString("MAT_NO"));
            }
            rs4.close();
            psta.close();


            if (matNo.size() < detCount) {
                throw new Exception("管号队列的废品支数小于实绩废品支数，请检查");
            }

            String UpdatePort = CoreXmlSqlParsersFactory.getSql("FrmHttBugD.UpdatePortHtt");// 新增废品从表实绩
            PreparedStatement patsUpdatePort = connection.prepareStatement(UpdatePort);


            for (int i = 0; i < matNo.size(); i++) {
                patsUpdatePort.setObject(1, "00");
                patsUpdatePort.setObject(2, "600201");
                patsUpdatePort.setObject(3, "");
                patsUpdatePort.setObject(4, heatNo);
                patsUpdatePort.setObject(5, matNo.get(i));
                patsUpdatePort.addBatch();

                PlanService.DeleteOffLineMat(connection, matNo.get(i));
            }

            patsUpdatePort.executeBatch();

        }

        String sqlloadingdet = CoreXmlSqlParsersFactory.getSql("FrmHttCrackDetectResult.detelefailnum2");// 删除废品缺陷实绩
        PreparedStatement pstadet = connection.prepareStatement(sqlloadingdet);
        pstadet.setString(1, resultNo);
        pstadet.addBatch();
        pstadet.executeBatch();


        sqlloadingdet = CoreXmlSqlParsersFactory.getSql("FrmHttCrackDetectResult.detelefailnum");// 删除废品缺陷实绩
        pstadet = connection.prepareStatement(sqlloadingdet);
        pstadet.setString(1, resultNo);
        pstadet.addBatch();
        pstadet.executeBatch();

    }

    public CoreReturnObject AddHttWaste(String HeatlPlanNo,  String Gx,String PlineCode, String UserID ,Integer ProcessSeq,String MatNo, ArrayList<String> HttWaste) {
        CoreReturnObject core = new CoreReturnObject();
        Connection connection = this.getDao(CoreConstantData.sysbeanstr)
                .getConnection();
        PreparedStatement psta = null;
        SimpleDateFormat matter1 = new SimpleDateFormat("yyMMdd");
        try {
            connection.setAutoCommit(false); // 取消自动提交事务

            AddHttWaste(connection, HeatlPlanNo, MatNo
                    ,PlineCode,UserID, ProcessSeq +"", Gx, new ArrayList<String>(), HttWaste, new ArrayList<String>());

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

    public CoreReturnObject AddHttWaste(String HeatlPlanNo,  String Gx,String PlineCode, String UserID ,Integer ProcessSeq,ArrayList<String> MatNos, ArrayList<String> HttWaste) {
        CoreReturnObject core = new CoreReturnObject();
        Connection connection = this.getDao(CoreConstantData.sysbeanstr)
                .getConnection();
        PreparedStatement psta = null;
        SimpleDateFormat matter1 = new SimpleDateFormat("yyMMdd");
        try {
            connection.setAutoCommit(false); // 取消自动提交事务


            for (int i = 0; i < MatNos.size(); i++) {

                ArrayList<String> Wastes = new ArrayList<String>();
                Wastes.add(HttWaste.get(i));
                AddHttWaste(connection, HeatlPlanNo, MatNos.get(i)
                        ,PlineCode,UserID, ProcessSeq +"", Gx, new ArrayList<String>(),  Wastes, new ArrayList<String>());
            }



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


    public CoreReturnObject DeleteHttWaste(String HeatlPlanNo, String MatNo ,String Gx,String PlineCode ,Integer ProcessSeq) {
        CoreReturnObject core = new CoreReturnObject();
        Connection connection = this.getDao(CoreConstantData.sysbeanstr)
                .getConnection();
        PreparedStatement psta = null;
        SimpleDateFormat matter1 = new SimpleDateFormat("yyMMdd");
        try {
            connection.setAutoCommit(false); // 取消自动提交事务

            DeleteHttWaste(connection,HeatlPlanNo,MatNo,PlineCode,ProcessSeq+"",Gx,1);

            core.setV_errMsg("剔除成功！");
            core.setResult(1);
            connection.commit();
        }
        catch (Exception e) {
            core.setV_errMsg(e.getMessage());
            core.setV_errCode(-1);
            core.setResult(1);
            if (connection != null) {
                try {
                    connection.rollback(); // 回滚事务
                }
                catch (Exception e2) {
                }
            }
        }
        finally {
            try {
                if (connection != null && !connection.isClosed()) {
                    connection.close();
                }
            }
            catch (Exception e2) {
            }
        }
        return core;
    }
    /**
     * 新增
     *
     * @param load
     * @return
     */
    public CoreReturnObject addPerson(ArrayList<HttWasteResultEntity> list1, ArrayList<HttWasteResultEntity> list2, String slgx, String pline, String heatNo) {
        CoreReturnObject core = new CoreReturnObject();
        Connection connection = this.getDao(CoreConstantData.sysbeanstr).getConnection();
        PreparedStatement psta = null;
        Date now = 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));
        SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss", Locale.CHINA);
        String systime = dateFormat.format(now);
        try {
            connection.setAutoCommit(false); // 取消自动提交事务
            String sqlAddD = CoreXmlSqlParsersFactory.getSql("FrmHttBugD.AddExpandDResult");// 新增可疑从表实绩
            PreparedStatement patAddD = connection.prepareStatement(sqlAddD);

            String sqlAddD1 = CoreXmlSqlParsersFactory.getSql("FrmHttBugD.AddExpandDResult1");// 新增废品从表实绩
            PreparedStatement patAddD1 = connection.prepareStatement(sqlAddD1);

            String resultNo = "";
            if (slgx.equals("600201")) {

                String sqlloading1 = CoreXmlSqlParsersFactory.getSql("FrmHttBugD.queryExpandMaxRESULT1");// 查询最大实绩编号
                psta = connection.prepareStatement(sqlloading1);
                psta.setObject(1, pline + slgx + matter1.format(now));
                psta.setObject(2, pline);
                psta.setObject(3, heatNo);
                ResultSet rs2 = psta.executeQuery();
                if (rs2.next()) {
                    resultNo = rs2.getString("RESULT_NO");
                }
                rs2.close();
                psta.close();

            } else if (slgx.equals("600202")) {
                String sqlloading1 = CoreXmlSqlParsersFactory.getSql("FrmHttBugD.queryExpandMaxRESULT2");// 查询最大实绩编号
                psta = connection.prepareStatement(sqlloading1);
                psta.setObject(1, pline + slgx + matter1.format(now));
                psta.setObject(2, pline);
                psta.setObject(3, heatNo);
                ResultSet rs2 = psta.executeQuery();
                if (rs2.next()) {
                    resultNo = rs2.getString("RESULT_NO");
                }
                rs2.close();
                psta.close();
            } else if (slgx.equals("600204")) {
                String sqlloading1 = CoreXmlSqlParsersFactory.getSql("FrmHttBugD.queryExpandMaxRESULT3");// 查询最大实绩编号
                psta = connection.prepareStatement(sqlloading1);
                psta.setObject(1, pline + slgx + matter1.format(now));
                psta.setObject(2, pline);
                psta.setObject(3, heatNo);
                ResultSet rs2 = psta.executeQuery();
                if (rs2.next()) {
                    resultNo = rs2.getString("RESULT_NO");
                }
                rs2.close();
                psta.close();
            } else if (slgx.equals("600208")) {
                String sqlloading1 = CoreXmlSqlParsersFactory.getSql("FrmHttBugD.queryExpandMaxRESULT4");// 查询最大实绩编号
                psta = connection.prepareStatement(sqlloading1);
                psta.setObject(1, pline + slgx + matter1.format(now));
                psta.setObject(2, pline);
                psta.setObject(3, heatNo);
                ResultSet rs2 = psta.executeQuery();
                if (rs2.next()) {
                    resultNo = rs2.getString("RESULT_NO");
                }
                rs2.close();
                psta.close();
            } else if (slgx.equals("600209")) {
                String sqlloading1 = CoreXmlSqlParsersFactory.getSql("FrmHttBugD.queryExpandMaxRESULT5");// 查询最大实绩编号
                psta = connection.prepareStatement(sqlloading1);
                psta.setObject(1, pline + slgx + matter1.format(now));
                psta.setObject(2, pline);
                psta.setObject(3, heatNo);
                ResultSet rs2 = psta.executeQuery();
                if (rs2.next()) {
                    resultNo = rs2.getString("RESULT_NO");
                }
                rs2.close();
                psta.close();
            }

            // 新增可疑从表实绩
            for (int i = 0; i < list1.size(); i++) {
                String failBill = "";
                String sqlloading2 = CoreXmlSqlParsersFactory.getSql("FrmHttBugD.queryMaxfailBill");// 生成废品缺陷编号
                psta = connection.prepareStatement(sqlloading2);
                psta.setObject(1, resultNo);
                psta.setObject(2, heatNo);
                psta.setObject(3, "1");
                ResultSet rs1 = psta.executeQuery();
                if (rs1.next()) {
                    failBill = rs1.getString("DEF_SERIAL_ITEM");
                }
                rs1.close();
                psta.close();
                String mresultNo = "";
                String sqlloading = CoreXmlSqlParsersFactory.getSql("FrmHttBugD.queryJumpDRESULT");// 查询从表最大实绩编号
                psta = connection.prepareStatement(sqlloading);
                psta.setObject(1, heatNo);
                ResultSet rs3 = psta.executeQuery();
                if (rs3.next()) {
                    mresultNo = rs3.getString("DEF_SERIAL_NO");
                }

                rs3.close();
                psta.close();
                HttWasteResultEntity resultD = list1.get(i);
                patAddD.setObject(1, failBill);
                patAddD.setObject(2, mresultNo);
                patAddD.setObject(3, resultNo);
                patAddD.setObject(4, heatNo);
                patAddD.setObject(5, resultD.getDefFillName());
                patAddD.setObject(6, resultD.getDefFillNum());
                patAddD.setObject(7, "1");
                patAddD.setObject(8, pline);
                patAddD.addBatch();
                patAddD.executeBatch();
            }
            // 新增废品从表实绩
            for (int j = 0; j < list2.size(); j++) {
                String failBill = "";
                String sqlloading2 = CoreXmlSqlParsersFactory.getSql("FrmHttBugD.queryMaxfailBill");// 生成废品缺陷编号
                psta = connection.prepareStatement(sqlloading2);
                psta.setObject(1, resultNo);
                psta.setObject(2, heatNo);
                psta.setObject(3, "2");
                ResultSet rs1 = psta.executeQuery();
                if (rs1.next()) {
                    failBill = rs1.getString("DEF_SERIAL_ITEM");
                }
                rs1.close();
                psta.close();
                String mresultNo = "";
                String sqlloading = CoreXmlSqlParsersFactory.getSql("FrmHttBugD.queryJumpDRESULT");// 查询从表最大实绩编号
                psta = connection.prepareStatement(sqlloading);
                psta.setObject(1, heatNo);
                ResultSet rs4 = psta.executeQuery();
                if (rs4.next()) {
                    mresultNo = rs4.getString("DEF_SERIAL_NO");
                }
                rs4.close();
                psta.close();
                HttWasteResultEntity resultD = list2.get(j);
                patAddD1.setObject(1, failBill);
                patAddD1.setObject(2, mresultNo);
                patAddD1.setObject(3, resultNo);
                patAddD1.setObject(4, heatNo);
                patAddD1.setObject(5, resultD.getDefFillName());
                patAddD1.setObject(6, resultD.getDefFillNum());
                patAddD1.setObject(7, "1");
                patAddD1.setObject(8, pline);
                patAddD1.addBatch();
                patAddD1.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 load
     * @return
     */
    public CoreReturnObject updatePerson(FrmMchPersonentity load, String name) {
        CoreReturnObject core = new CoreReturnObject();
        Connection connection = this.getDao(CoreConstantData.sysbeanstr).getConnection();
        PreparedStatement psta = null;
        Date now = 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));
        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("FrmHttPerson.updatePerson");// 修改
            PreparedStatement patAdd = connection.prepareStatement(sqlAdd);
            // 新增倒棱实绩
            patAdd.setObject(1, name);
            patAdd.setObject(2, load.getPlineCode());
            patAdd.setObject(3, load.getProBc());
            patAdd.setObject(4, load.getManName());
            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 load
     * @return
     */
    public CoreReturnObject deletePerson(String name) {
        CoreReturnObject core = new CoreReturnObject();
        Connection connection = this.getDao(CoreConstantData.sysbeanstr).getConnection();
        PreparedStatement psta = null;
        Date now = 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));
        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("FrmHttPerson.deletePerson");// 修改
            PreparedStatement patAdd = connection.prepareStatement(sqlAdd);
            // 新增倒棱实绩
            patAdd.setObject(1, name);
            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;
    }

}
