package com.steering.mes.mcp.Mch;

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.MchBugDEntity;
import com.steering.mes.mcp.entity.PortMchBatchSampleResultEntity;

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

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

        String sql = "";
        sql = CoreXmlSqlParsersFactory
                .getSql("FrmMchPerson.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("FrmMchPerson.doQuerypline");
        return this.getData(sql);
    }
    //查询废品详细信息
    public CoreReturnObject QueryFailData(String hertNo,String gx,String resultNo,String plineCode) {
    	String sql = CoreXmlSqlParsersFactory
                .getSql("FrmMchBugD.QueryFailData");
    	   if (!gx.equals("")) {
               sql += " AND T1.STATION_CODE ='" + gx + "'";
    	   }
               if (!hertNo.equals("")) {
                   sql += " AND T.HEAT_PLAN_NO ='" + hertNo + "'";
               }
                   if (!resultNo.equals("")) {
                       sql += " AND T.RESULT_NO ='" + resultNo + "'";  
                   }
                       if (!plineCode.equals("")) {
                           sql += " AND T.PLINE_CODE ='" + plineCode + "'";  
                       }
        return this.getData(sql);
    }
    
    //查询可疑详细信息
    public CoreReturnObject QueryKeyiFailData(String hertNo,String gx,String resultNo,String plineCode) {
    	String sql = CoreXmlSqlParsersFactory
                .getSql("FrmMchBugD.QueryKeyiFailData");
    	   if (!gx.equals("")) {
               sql += " AND T1.STATION_CODE ='" + gx + "'";
    	   }
               if (!hertNo.equals("")) {
                   sql += " AND T.HEAT_PLAN_NO ='" + hertNo + "'";
               }
                   if (!resultNo.equals("")) {
                       sql += " AND T.RESULT_NO ='" + resultNo + "'";  
                   }
                       if (!plineCode.equals("")) {
                           sql += " AND T.PLINE_CODE ='" + plineCode + "'";  
                       }
        return this.getData(sql);
    }
    //查询可疑支数
    public CoreReturnObject doQueryAlarmNum(String heatNo,String resultNo) {
    	String sql = CoreXmlSqlParsersFactory
                .getSql("FrmMchBugD.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("FrmMchBugD.doQueryScrapNum");
    	  if (!heatNo.equals("")) {
              sql += " AND T.HEAT_PLAN_NO ='" + heatNo + "'";
          }     
          if (!resultNo.equals("")) {
              sql += " AND T.RESULT_NO ='" + resultNo + "'";
          }  
        return this.getData(sql);
    }
    
    
    
    /**
     * 新增
     * @param load
     * @return
     */
    public CoreReturnObject addPerson(ArrayList<MchBugDEntity> list1,ArrayList<MchBugDEntity> 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("FrmMchBugD.AddExpandDResult");// 新增可疑从表实绩
            PreparedStatement patAddD = connection.prepareStatement(sqlAddD);
            
            String sqlAddD1 = CoreXmlSqlParsersFactory
                    .getSql("FrmMchBugD.AddExpandDResult1");// 新增废品从表实绩
            PreparedStatement patAddD1 = connection.prepareStatement(sqlAddD1);
            
            String resultNo = "";
            if(slgx.equals("701301"))
            {
           
            String sqlloading1 = CoreXmlSqlParsersFactory
                    .getSql("FrmMchBugD.queryExpandMaxRESULT1");// 查询最大实绩编号
            psta = connection.prepareStatement(sqlloading1);
            psta.setObject(1, pline + slgx + matter1.format(now));
            psta.setObject(2, pline);
            ResultSet rs2 = psta.executeQuery();
            if (rs2.next()) {
                resultNo = rs2.getString("RESULT_NO");
           }
            rs2.close();
            psta.close();
            
            }else if(slgx.equals("701302"))
            {
            	  String sqlloading1 = CoreXmlSqlParsersFactory
                          .getSql("FrmMchBugD.queryExpandMaxRESULT2");// 查询最大实绩编号
                  psta = connection.prepareStatement(sqlloading1);
                  psta.setObject(1, pline + slgx + matter1.format(now));
                  psta.setObject(2, pline);
                  ResultSet rs2 = psta.executeQuery();
                  if (rs2.next()) {
                      resultNo = rs2.getString("RESULT_NO");
                 }
                  rs2.close();
                  psta.close();
            }else if(slgx.equals("701303"))
            {
            	 String sqlloading1 = CoreXmlSqlParsersFactory
                         .getSql("FrmMchBugD.queryExpandMaxRESULT3");// 查询最大实绩编号
                 psta = connection.prepareStatement(sqlloading1);
                 psta.setObject(1, pline + slgx + matter1.format(now));
                 psta.setObject(2, pline);
                 ResultSet rs2 = psta.executeQuery();
                 if (rs2.next()) {
                     resultNo = rs2.getString("RESULT_NO");
                }
                 rs2.close();
                 psta.close();
            }else if(slgx.equals("701304"))
            {
            	 String sqlloading1 = CoreXmlSqlParsersFactory
                         .getSql("FrmMchBugD.queryExpandMaxRESULT4");// 查询最大实绩编号
                 psta = connection.prepareStatement(sqlloading1);
                 psta.setObject(1, pline + slgx + matter1.format(now));
                 psta.setObject(2, pline);
                 ResultSet rs2 = psta.executeQuery();
                 if (rs2.next()) {
                     resultNo = rs2.getString("RESULT_NO");
                }
                 rs2.close();
                 psta.close();
            }else if(slgx.equals("701305"))
            {
            	 String sqlloading1 = CoreXmlSqlParsersFactory
                         .getSql("FrmMchBugD.queryExpandMaxRESULT5");// 查询最大实绩编号
                 psta = connection.prepareStatement(sqlloading1);
                 psta.setObject(1, pline + slgx + matter1.format(now));
                 psta.setObject(2, pline);
                 ResultSet rs2 = psta.executeQuery();
                 if (rs2.next()) {
                     resultNo = rs2.getString("RESULT_NO");
                }
                 rs2.close();
                 psta.close();
            }else if(slgx.equals("701306"))
            {
            	 String sqlloading1 = CoreXmlSqlParsersFactory
                         .getSql("FrmMchBugD.queryExpandMaxRESULT6");// 查询最大实绩编号
                 psta = connection.prepareStatement(sqlloading1);
                 psta.setObject(1, pline + slgx + matter1.format(now));
                 psta.setObject(2, pline);
                 ResultSet rs2 = psta.executeQuery();
                 if (rs2.next()) {
                     resultNo = rs2.getString("RESULT_NO");
                }
                 rs2.close();
                 psta.close();
            }else if(slgx.equals("701307"))
            {
            	 String sqlloading1 = CoreXmlSqlParsersFactory
                         .getSql("FrmMchBugD.queryExpandMaxRESULT7");// 查询最大实绩编号
                 psta = connection.prepareStatement(sqlloading1);
                 psta.setObject(1, pline + slgx + matter1.format(now));
                 psta.setObject(2, pline);
                 ResultSet rs2 = psta.executeQuery();
                 if (rs2.next()) {
                     resultNo = rs2.getString("RESULT_NO");
                }
                 rs2.close();
                 psta.close();
            }else if(slgx.equals("701308"))
            {
            	 String sqlloading1 = CoreXmlSqlParsersFactory
                         .getSql("FrmMchBugD.queryExpandMaxRESULT8");// 查询最大实绩编号
                 psta = connection.prepareStatement(sqlloading1);
                 psta.setObject(1, pline + slgx + matter1.format(now));
                 psta.setObject(2, pline);
                 ResultSet rs2 = psta.executeQuery();
                 if (rs2.next()) {
                     resultNo = rs2.getString("RESULT_NO");
                }
                 rs2.close();
                 psta.close();
            }else if(slgx.equals("701309"))
            {
            	 String sqlloading1 = CoreXmlSqlParsersFactory
                         .getSql("FrmMchBugD.queryExpandMaxRESULT9");// 查询最大实绩编号
                 psta = connection.prepareStatement(sqlloading1);
                 psta.setObject(1, pline + slgx + matter1.format(now));
                 psta.setObject(2, pline);
                 ResultSet rs2 = psta.executeQuery();
                 if (rs2.next()) {
                     resultNo = rs2.getString("RESULT_NO");
                }
                 rs2.close();
                 psta.close();
            }else if(slgx.equals("701310"))
            {
            	 String sqlloading1 = CoreXmlSqlParsersFactory
                         .getSql("FrmMchBugD.queryExpandMaxRESULT10");// 查询最大实绩编号
                 psta = connection.prepareStatement(sqlloading1);
                 psta.setObject(1, pline + slgx + matter1.format(now));
                 psta.setObject(2, pline);
                 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("FrmMchBugD.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("FAIL_BILL");
                }
                 rs1.close();
                 psta.close();
                String mresultNo = "";
                String sqlloading = CoreXmlSqlParsersFactory
                        .getSql("FrmMchBugD.queryJumpDRESULT");// 查询从表最大实绩编号
                psta = connection.prepareStatement(sqlloading);
                psta.setObject(1, heatNo);
                ResultSet rs3 = psta.executeQuery();
                if (rs3.next()) {
                    mresultNo = rs3.getString("FAIL_BILL_SQE");
                }
            
                rs3.close();
                psta.close();
                MchBugDEntity resultD = list1.get(i);
                patAddD.setObject(1, failBill);
                patAddD.setObject(2, mresultNo);
                patAddD.setObject(3, resultNo);
                patAddD.setObject(4, heatNo);
                patAddD.setObject(5, resultD.getFailTypeName());
                patAddD.setObject(6, resultD.getFailNum());
                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("FrmMchBugD.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("FAIL_BILL");
                }
                 rs1.close();
                 psta.close();
                String mresultNo = "";
                String sqlloading = CoreXmlSqlParsersFactory
                        .getSql("FrmMchBugD.queryJumpDRESULT");// 查询从表最大实绩编号
                psta = connection.prepareStatement(sqlloading);
                psta.setObject(1, heatNo);
                ResultSet rs4 = psta.executeQuery();
                if (rs4.next()) {
                    mresultNo = rs4.getString("FAIL_BILL_SQE");
                }
                rs4.close();
                psta.close();
                MchBugDEntity resultD = list2.get(j);
                patAddD1.setObject(1, failBill);
                patAddD1.setObject(2, mresultNo);
                patAddD1.setObject(3, resultNo);
                patAddD1.setObject(4, heatNo);
                patAddD1.setObject(5, resultD.getFailTypeName());
                patAddD1.setObject(6, resultD.getFailNum());
                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 UpdateFail(ArrayList<MchBugDEntity> list1,ArrayList<MchBugDEntity> list2,
    		String gx,String plineCode,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 sqlDeteD = CoreXmlSqlParsersFactory
                    .getSql("FrmMchBugD.updateFail");// 修改
            PreparedStatement patDetele = connection.prepareStatement(sqlDeteD);
            String sqlDete = CoreXmlSqlParsersFactory
                    .getSql("FrmMchBugD.updateFailD");// 修改
            PreparedStatement patDeleteD = connection.prepareStatement(sqlDete);
            for(int i=0;i<list1.size();i++)
            {
                MchBugDEntity resultD = list1.get(i);
            // 修改可疑信息
            patDetele.setObject(1, resultD.getFailTypeName());
            patDetele.setObject(2, resultD.getFailDirect());
            patDetele.setObject(3, resultD.getFailNum());
            patDetele.setObject(4, resultD.getResultNo());
            patDetele.setObject(5, resultD.getFailBill());
            patDetele.setObject(6, resultD.getFailBillSqe());
            patDetele.addBatch();
            patDetele.executeBatch();
            }
            for(int i = 0;i<list2.size();i++)
            {
                MchBugDEntity resultD = list2.get(i);
            // 修改废品信息
            patDeleteD.setObject(1, resultD.getFailTypeName());
            patDeleteD.setObject(2, resultD.getFailDirect());
            patDeleteD.setObject(3, resultD.getFailNum());
            patDeleteD.setObject(4, resultD.getResultNo());
            patDeleteD.setObject(5, resultD.getFailBill());
            patDeleteD.setObject(6, resultD.getFailBillSqe());
            patDeleteD.addBatch();
            patDeleteD.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 deleteFail(ArrayList<MchBugDEntity> list1,ArrayList<MchBugDEntity> list2,
    		String gx,String plineCode,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 sqlDeteD = CoreXmlSqlParsersFactory
                    .getSql("FrmMchBugD.deleteFail");// 删除
            PreparedStatement patDetele = connection.prepareStatement(sqlDeteD);
            String sqlDete = CoreXmlSqlParsersFactory
                    .getSql("FrmMchBugD.deleteFailD");// 删除
            PreparedStatement patDeleteD = connection.prepareStatement(sqlDete);
            for(int i=0;i<list1.size();i++)
            {
                MchBugDEntity resultD = list1.get(i);
            // 删除可疑信息
            patDetele.setObject(1, resultD.getResultNo());
            patDetele.setObject(2, resultD.getFailBill());
            patDetele.setObject(3, resultD.getFailBillSqe());
            patDetele.addBatch();
            patDetele.executeBatch();
            }
            for(int i = 0;i<list2.size();i++)
            {
                MchBugDEntity resultD = list2.get(i);
            // 删除废品信息
            patDeleteD.setObject(1, resultD.getResultNo());
            patDeleteD.setObject(2, resultD.getFailBill());
            patDeleteD.setObject(3, resultD.getFailBillSqe());
            patDeleteD.addBatch();
            patDeleteD.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;
    }


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


        public static void AddMchWaste(Connection connection, String heatNo, String resultNo, String PlineCode, String username, String gx, ArrayList<String> list3, ArrayList<String> list4, boolean IgnoreTr) throws Exception {
            AddMchWaste( connection,  heatNo,  resultNo,  PlineCode,  username,  gx,  list3, list4,new ArrayList<String>(),  IgnoreTr,"0");
    }

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

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

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

        String sqlAddUp = CoreXmlSqlParsersFactory.getSql("FrmMchBugD.AddExpandDResult3");// 新增可疑从表实绩
        PreparedStatement patAddUp = connection.prepareStatement(sqlAddUp);



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

        String UpdateMatStauts = CoreXmlSqlParsersFactory.getSql("FrmMchBugD.updateMatStatus");// 更改管号队列状态
        PreparedStatement patsUpdateMatStauts = connection.prepareStatement(UpdateMatStauts);

        String UpdateMesureData = CoreXmlSqlParsersFactory.getSql("FrmMchBugD.UpdateMesureData");// 更改管号队列状态
        PreparedStatement patsUpdateMesureData = connection.prepareStatement(UpdateMesureData);

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

        String failBill = "";
        String sqlloading2 = CoreXmlSqlParsersFactory.getSql("FrmMchBugD.queryMaxfailBillNew");// 生成废品缺陷编号
        psta = connection.prepareStatement(sqlloading2);
        psta.setObject(1, resultNo);
        psta.setObject(2, resultNo);
        ResultSet rs4 = psta.executeQuery();
        if (rs4.next()) {
            failBill = rs4.getString("FAIL_BILL");
        }
        rs4.close();
        psta.close();

        // 新增可疑从表实绩
        for (int i = 0; i < list3.size(); i++) {

            String mresultNo = "";
            String sqlloading3 = CoreXmlSqlParsersFactory.getSql("FrmMchBugD.queryJumpDRESULTNEW");// 查询从表最大实绩编号
            psta = connection.prepareStatement(sqlloading3);
            psta.setObject(1, failBill);
            ResultSet rs3 = psta.executeQuery();
            if (rs3.next()) {
                mresultNo = rs3.getString("FAIL_BILL_SQE");
            }

            rs3.close();
            psta.close();
            MchBugDEntity resultD = EntityTool.getEntityByJSONStr(list3.get(i), MchBugDEntity.class);
            patAddW.setObject(1, failBill);
            patAddW.setObject(2, mresultNo);
            patAddW.setObject(3, resultNo);
            patAddW.setObject(4, heatNo);
            patAddW.setObject(5, resultD.getFailTypeCode());
            patAddW.setObject(6, resultD.getFailTypeName());
            patAddW.setObject(7, resultD.getFailNum());
            patAddW.setObject(8, resultD.getFailWt());
            patAddW.setObject(9, PlineCode);
            patAddW.setObject(10, resultD.getBatchNo());
            patAddW.addBatch();
            patAddW.executeBatch();

            if (!resultD.getMatNo().equals("")) {
                String[] MatNos = resultD.getMatNo().split(",");

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



        failBill = "";
        sqlloading2 = CoreXmlSqlParsersFactory.getSql("FrmMchBugD.queryMaxfailBillNew");// 生成废品缺陷编号
        psta = connection.prepareStatement(sqlloading2);
        psta.setObject(1, resultNo);
        psta.setObject(2, resultNo);
        rs4 = psta.executeQuery();
        if (rs4.next()) {
            failBill = rs4.getString("FAIL_BILL");
        }
        rs4.close();
        psta.close();

        // 新增不合实绩
        for (int i = 0; i < list5.size(); i++) {

            String mresultNo = "";
            String sqlloading3 = CoreXmlSqlParsersFactory.getSql("FrmMchBugD.queryJumpDRESULTNEW");// 查询从表最大实绩编号
            psta = connection.prepareStatement(sqlloading3);
            psta.setObject(1, failBill);
            ResultSet rs3 = psta.executeQuery();
            if (rs3.next()) {
                mresultNo = rs3.getString("FAIL_BILL_SQE");
            }

            rs3.close();
            psta.close();
            MchBugDEntity resultD = EntityTool.getEntityByJSONStr(list5.get(i), MchBugDEntity.class);
            patAddUp.setObject(1, failBill);
            patAddUp.setObject(2, mresultNo);
            patAddUp.setObject(3, resultNo);
            patAddUp.setObject(4, heatNo);
            patAddUp.setObject(5, resultD.getFailTypeCode());
            patAddUp.setObject(6, resultD.getFailTypeName());
            patAddUp.setObject(7, resultD.getFailNum());
            patAddUp.setObject(8, resultD.getFailWt());
            patAddUp.setObject(9, PlineCode);
            patAddUp.setObject(10, resultD.getBatchNo());
            patAddUp.addBatch();
            patAddUp.executeBatch();

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

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



        failBill = "";
        sqlloading2 = CoreXmlSqlParsersFactory.getSql("FrmMchBugD.queryMaxfailBillNew");// 生成废品缺陷编号
        psta = connection.prepareStatement(sqlloading2);
        psta.setObject(1, resultNo);
        psta.setObject(2, resultNo);
        rs4 = psta.executeQuery();
        if (rs4.next()) {
            failBill = rs4.getString("FAIL_BILL");
        }
        rs4.close();
        psta.close();

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

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



            String mresultNo = "";
            String sqlloading3 = CoreXmlSqlParsersFactory.getSql("FrmMchBugD.queryJumpDRESULTNEW");// 查询从表最大实绩编号
            psta = connection.prepareStatement(sqlloading3);
            psta.setObject(1, failBill);
            ResultSet rs3 = psta.executeQuery();
            if (rs3.next()) {
                mresultNo = rs3.getString("FAIL_BILL_SQE");
            }

            rs3.close();
            psta.close();


            patAddD1.setObject(1, failBill);
            patAddD1.setObject(2, mresultNo);
            patAddD1.setObject(3, resultNo);
            patAddD1.setObject(4, heatNo);
            patAddD1.setObject(5, resultD.getFailTypeCode());
            patAddD1.setObject(6, resultD.getFailTypeName());
            patAddD1.setObject(7, resultD.getFailNum());
            patAddD1.setObject(8, resultD.getFailWt());
            patAddD1.setObject(9, PlineCode);
            patAddD1.setObject(10, resultD.getBatchNo());
            patAddD1.addBatch();
            patAddD1.executeBatch();


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

            for (String matNo : MatNos) {

                if(IsEntrust.equals("1")){
                    patsUpdateMatStauts.setObject(1,"22");
                    patsUpdateMatStauts.setObject(2, matNo);
                    patsUpdateMatStauts.addBatch();

                }else if(IsEntrust.equals("0")){

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

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



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

                    String MatNoStr = "管号:" +matNo+ "/喷印管号:"  +sendPlanCEntitys.get(0).getMeasuringSeq() ;

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

                    if (sendPlanCEntitys.get(0).getInstoreFlag().equals("1")) {
                        throw new Exception(MatNoStr+" 已经入库, 请先撤销入库");
                    }

                    if (!sendPlanCEntitys.get(0).getMatStatus().equals("00")) {
                        throw new Exception(MatNoStr+ "不是合格管, 请确认是否离线或已经判废");
                    }

                    if (!sendPlanCEntitys.get(0).getMeasuringSeq().equals("")) {
                        patsUpdateMesureData.setObject(1, "10");
                        patsUpdateMesureData.setObject(2, heatNo);
                        patsUpdateMesureData.setObject(3, matNo);
                        patsUpdateMesureData.addBatch();
                        patsUpdateMesureData.executeBatch();
                    }


                    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, "G", username, "22", matNo, gx, heatNo,resultD.getFailTypeName());
                }

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

            }
            patsUpdatePort.executeBatch();
            patsUpdateMatStauts.executeBatch();
        }

        patsUpdatePortMatNo.executeBatch();
    }


    public CoreReturnObject AddMchWaste(String HeatlPlanNo,  String Gx,String PlineCode, String UserID ,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); // 取消自动提交事务

            AddMchWaste(connection, HeatlPlanNo, MatNo
                    ,PlineCode,UserID, Gx, new ArrayList<String>(), HttWaste);

            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 static void DeleteMchWaste(Connection connection, String heatNo, String resultNo, String PlineCode, String gx, int detCount) throws Exception {

        DeleteMchWaste( connection,  heatNo,  resultNo,  PlineCode,  gx,  detCount,"0");
    }
    public static void DeleteMchWasteDbk(Connection connection, String heatNo, String resultNo, String PlineCode, String gx, int detCount) throws Exception {

        DeleteMchWasteDbk( connection,  heatNo,  resultNo,  PlineCode,  gx,  detCount,"0");
    }
    public static void DeleteMchWasteDbk(Connection connection, String heatNo, String resultNo, String PlineCode, String gx, int detCount, String IsEntrust ) throws Exception {

        if (detCount > 0) {

            PreparedStatement psta = null;

            if(IsEntrust.equals("0")) {

                ArrayList<String> matNo = new ArrayList<String>();
                String sqlloading3 = CoreXmlSqlParsersFactory.getSql("FrmMchBugD.QueryFailPortHttDbk");// 查询管号队列表里废品材料号
                psta = connection.prepareStatement(sqlloading3);
                psta.setObject(1, heatNo);
                psta.setObject(2, gx);
                psta.setObject(3, resultNo);
                psta.setObject(4, 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("FrmMchBugD.UpdatePortHttDbk");// 新增废品从表实绩
                PreparedStatement patsUpdatePort = connection.prepareStatement(UpdatePort);

                String UpdateMesureData = CoreXmlSqlParsersFactory.getSql("FrmMchBugD.UpdateMesureDataDbk");
                PreparedStatement patsUpdateMesureData = connection.prepareStatement(UpdateMesureData);


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

                    patsUpdateMesureData.setObject(1, "00");
                    patsUpdateMesureData.setObject(2, heatNo);
                    patsUpdateMesureData.setObject(3, matNo.get(i));
                    patsUpdateMesureData.addBatch();
                    patsUpdateMesureData.executeBatch();


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

                patsUpdatePort.executeBatch();
            }else if(IsEntrust.equals("1")) {
                String UpdateMatStauts = CoreXmlSqlParsersFactory.getSql("FrmMchBugD.updateMatStatus2");// 更改管号队列状态
                PreparedStatement patsUpdateMatStauts = connection.prepareStatement(UpdateMatStauts);
                patsUpdateMatStauts.setObject(1,"11");
                patsUpdateMatStauts.setObject(2, resultNo);
                patsUpdateMatStauts.addBatch();
                patsUpdateMatStauts.executeBatch();
            }
        }

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


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

    }
    public static void DeleteMchWaste(Connection connection, String heatNo, String resultNo, String PlineCode, String gx, int detCount, String IsEntrust ) throws Exception {

        if (detCount > 0) {

            PreparedStatement psta = null;

            if(IsEntrust.equals("0")) {

                ArrayList<String> matNo = new ArrayList<String>();
                String sqlloading3 = CoreXmlSqlParsersFactory.getSql("FrmMchBugD.QueryFailPortHtt");// 查询管号队列表里废品材料号
                psta = connection.prepareStatement(sqlloading3);
                psta.setObject(1, heatNo);
                psta.setObject(2, gx);
                psta.setObject(3, resultNo);
                psta.setObject(4, 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("FrmMchBugD.UpdatePortHtt");// 新增废品从表实绩
                PreparedStatement patsUpdatePort = connection.prepareStatement(UpdatePort);

                String UpdateMesureData = CoreXmlSqlParsersFactory.getSql("FrmMchBugD.UpdateMesureData");
                PreparedStatement patsUpdateMesureData = connection.prepareStatement(UpdateMesureData);


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

                    patsUpdateMesureData.setObject(1, "00");
                    patsUpdateMesureData.setObject(2, heatNo);
                    patsUpdateMesureData.setObject(3, matNo.get(i));
                    patsUpdateMesureData.addBatch();
                    patsUpdateMesureData.executeBatch();


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

                patsUpdatePort.executeBatch();
            }else if(IsEntrust.equals("1")) {
                String UpdateMatStauts = CoreXmlSqlParsersFactory.getSql("FrmMchBugD.updateMatStatus2");// 更改管号队列状态
                PreparedStatement patsUpdateMatStauts = connection.prepareStatement(UpdateMatStauts);
                patsUpdateMatStauts.setObject(1,"11");
                patsUpdateMatStauts.setObject(2, resultNo);
                patsUpdateMatStauts.addBatch();
                patsUpdateMatStauts.executeBatch();
            }
        }

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


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

    }
    /**
     * 接箍撤销
     * @param connection
     * @param heatNo
     * @param resultNo
     * @param PlineCode
     * @param gx
     * @param detCount
     * @throws Exception
     */
    public static void DeleteMchWasteJg(Connection connection, String woid, String resultNo, String PlineCode, String gx, int detCount) throws Exception {

        if (detCount > 0) {

//            PreparedStatement psta = null;
//
//            ArrayList<String> matNo = new ArrayList<String>();
//            String sqlloading3 = CoreXmlSqlParsersFactory.getSql("FrmMchBugD.QueryFailPortJg");// 查询管号队列表里废品材料号
//            psta = connection.prepareStatement(sqlloading3);
//            psta.setObject(1, woid);
//            psta.setObject(2, gx);
//            psta.setObject(3,resultNo);
//            psta.setObject(4, 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("FrmMchBugD.UpdatePortJg");// 修改接箍管号队列状态
//            PreparedStatement patsUpdatePort = connection.prepareStatement(UpdatePort);
//
//
//
//
//            for (int i = 0; i < matNo.size(); i++) {
//                patsUpdatePort.setObject(1, "00");
//                patsUpdatePort.setObject(2, "700301");
//                patsUpdatePort.setObject(3, "");
//                patsUpdatePort.setObject(4, woid);
//                patsUpdatePort.setObject(5, matNo.get(i));
//                patsUpdatePort.addBatch();


               // PlanService.DeleteOffLineMatJg(connection, woid, resultNo);
//            }
//
//            patsUpdatePort.executeBatch();

        }

    }



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

            DeleteMchWaste(connection,HeatlPlanNo,MatNo,PlineCode,Gx,1,"0");

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




}
