package com.steering.mes.mcp.heatTreatment;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Calendar;
import java.util.Date;
import java.util.Locale;

import CoreFS.SA06.CoreReturnObject;
import CoreFS.SA08.CoreXmlSqlParsersFactory;
import CoreFS.SC01.CoreConstantData;

import com.steering.comm.db.DBCommon;
import com.steering.mes.mcp.entity.FrmMchPersonentity;
import com.steering.mes.mcp.entity.HttWasteResultEntity;


public class FrmHttWasteResult extends DBCommon {

	 public CoreReturnObject doQuery(String banci,String pline) {

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

	        if (!banci.equals("")) {
	            sql += " AND T.PRO_BC ='" + 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 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<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("FrmHttWasteResult.AddExpandDResult");// 新增可疑从表实绩
	            PreparedStatement patAddD = connection.prepareStatement(sqlAddD);
	            
	            String sqlAddD1 = CoreXmlSqlParsersFactory
	                    .getSql("FrmHttWasteResult.AddExpandDResult1");// 新增废品从表实绩
	            PreparedStatement patAddD1 = connection.prepareStatement(sqlAddD1);
	            
	            String resultNo = "";
	            if(slgx.equals("600201"))
	            {
	           
	            String sqlloading1 = CoreXmlSqlParsersFactory
	                    .getSql("FrmHttWasteResult.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("600202"))
	            {
	            	  String sqlloading1 = CoreXmlSqlParsersFactory
	                          .getSql("FrmHttWasteResult.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("600203"))
	            {
	            	 String sqlloading1 = CoreXmlSqlParsersFactory
	                         .getSql("FrmHttWasteResult.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("600204"))
	            {
	            	 String sqlloading1 = CoreXmlSqlParsersFactory
	                         .getSql("FrmHttWasteResult.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("600205"))
	            {
	            	 String sqlloading1 = CoreXmlSqlParsersFactory
	                         .getSql("FrmHttWasteResult.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("600206"))
	            {
	            	 String sqlloading1 = CoreXmlSqlParsersFactory
	                         .getSql("FrmHttWasteResult.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("600207"))
	            {
	            	 String sqlloading1 = CoreXmlSqlParsersFactory
	                         .getSql("FrmHttWasteResult.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("600208"))
	            {
	            	 String sqlloading1 = CoreXmlSqlParsersFactory
	                         .getSql("FrmHttWasteResult.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("600209"))
	            {
	            	 String sqlloading1 = CoreXmlSqlParsersFactory
	                         .getSql("FrmHttWasteResult.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();
	            }
	            
	            // 新增可疑从表实绩
	            for (int i = 0; i < list1.size(); i++) {
	            	 String defSerialItem = "";
	                 String sqlloading2 = CoreXmlSqlParsersFactory
	                         .getSql("FrmHttWasteResult.queryMaxfailBill");// 生成废品缺陷编号
	                 psta = connection.prepareStatement(sqlloading2);
	                 psta.setObject(1, resultNo);
	                 psta.setObject(2, heatNo);
	                 psta.setObject(3, "1");
	                 ResultSet rs1 = psta.executeQuery();
	                 if (rs1.next()) {
	                 	defSerialItem = rs1.getString("DEF_SERIAL_ITEM");
	                }
	                 rs1.close();
	                 psta.close();
	                String defSerialNo = "";
	                String sqlloading = CoreXmlSqlParsersFactory
	                        .getSql("FrmHttWasteResult.queryJumpDRESULT");// 查询从表最大实绩编号
	                psta = connection.prepareStatement(sqlloading);
	                psta.setObject(1, heatNo);
	                ResultSet rs3 = psta.executeQuery();
	                if (rs3.next()) {
	                    defSerialNo = rs3.getString("DEF_SERIAL_NO");
	                }
	            
	                rs3.close();
	                psta.close();
	                HttWasteResultEntity resultD = list1.get(i);
	                patAddD.setObject(1, defSerialItem);
	                patAddD.setObject(2, defSerialNo);
	                patAddD.setObject(3, heatNo);
	                patAddD.setObject(4, "1");
	                patAddD.setObject(5, resultD.getDefFillName());
	                patAddD.setObject(6, "");	              
	                patAddD.setObject(7, 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();
	                HttWasteResultEntity resultD = list2.get(j);
	                patAddD1.setObject(1, failBill);
	                patAddD1.setObject(2, mresultNo);
	                patAddD1.setObject(3, resultNo);
	                patAddD1.setObject(4, heatNo);
	                patAddD1.setObject(5, "");
	                patAddD1.setObject(6, "");
	                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("FrmMchPerson.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("FrmMchPerson.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;
	    }
}
