package com.steering.mes.mcp.heatTreatment;

import CoreFS.SA06.CoreReturnObject;
import CoreFS.SA08.CoreXmlSqlParsersFactory;
import CoreFS.SC01.CoreConstantData;
import com.steering.comm.converter.CoreConverter;
import com.steering.comm.converter.EntityTool;
import com.steering.comm.db.DBCommon;
import com.steering.mes.mcp.entity.HttReportEntity;

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

public class HttReport extends DBCommon{
	/**
	 * 查询报表数据
	 * @param start
	 * @param end
	 * @param heatid
	 * @param judgeNo
	 * @return
	 */
    public CoreReturnObject doQuery(String[] plineCode) {

        String sql = "";
        sql = CoreXmlSqlParsersFactory
                .getSql("HttReport.doQuery");
        sql += CoreConverter.toSqlConditionString("PLINE_CODE", plineCode);

        sql +=" ORDER BY PLINE_CODE,REPORT_TYPE,MACHINE_NO";
        return this.getData(sql);
    }
	/**
	 * 查询新增数据是否相同
	 * @param start
	 * @param end
	 * @param heatid
	 * @param judgeNo
	 * @return
	 */
    public CoreReturnObject getSameData(String plinCode,String reportName,String reportType,String MachineNo) {

        String sql = "";
        sql = CoreXmlSqlParsersFactory
                .getSql("HttReport.doSameData");
        if(!plinCode.equals(""))
        {
        	sql += " AND PLINE_CODE = '"+plinCode+"'";
        }
        if(!reportName.equals(""))
        {
        	sql += " AND REPORT_NAME = '"+reportName+"'";
        }
        if(!reportType.equals(""))
        {
        	sql += " AND REPORT_TYPE = '"+reportType+"'";
        }
        if(!MachineNo.equals("")){
            sql += " AND MACHINE_NO =  '"+MachineNo+"'";
        }

        return this.getData(sql);
    }
    
    /**
     * 新增
     * @param load
     * @return
     */
    public CoreReturnObject doSave(ArrayList<String> list) {
        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); // 取消自动提交事务
            ArrayList<String> sqllist = new ArrayList<String>();
         for(String str:list)   
         {
        	 HttReportEntity httEntity = EntityTool.getEntityByJSONStr(str, HttReportEntity.class);
        	 HttReportEntity httReport = new HttReportEntity();
            // 新增编号信息
        	 httReport.setReportNo(httEntity.getReportNo());
        	 httReport.setPlineCode(httEntity.getPlineCode());
        	 httReport.setReportNoNew(httEntity.getReportNoNew());
        	 httReport.setReportNoTime(httEntity.getReportNoTime());
        	 httReport.setReportName(httEntity.getReportName());
        	 httReport.setColUser(httEntity.getColUser());
        	 httReport.setColTime(systime);
        	 httReport.setReportType(httEntity.getReportType());
        	 httReport.setRemark(httEntity.getRemark());
        	 httReport.setGenerateStation(httEntity.getGenerateStation());
        	 httReport.setReceivingStation(httEntity.getReceivingStation());
        	 httReport.setSaveStation(httEntity.getSaveStation());
        	 httReport.setStorageLife(httEntity.getStorageLife());
        	 httReport.setIntermediary(httEntity.getIntermediary());
        	 httReport.setEnglishName(httEntity.getEnglishName());
        	 if(httEntity.getMachineNo().equals(""))
        	 {
        		 httReport.setMachineNo("1");       		
        	 }else
        	 {
        		 httReport.setMachineNo(httEntity.getMachineNo());
        	 }
        	 if(httReport.getReportType().equals("0")){

                 httReport.setReportCpt("RepMchCrackDetetionProcess.cpt");
             } else  if(httReport.getReportType().equals("1")){

                 httReport.setReportCpt("RepMchCrackDetetionProcessShouTi.cpt");
             }else  if(httReport.getReportType().equals("10")){

                 httReport.setReportCpt("RepMchCrackDetetionProcess.cpt");
             }else  if(httReport.getReportType().equals("11")){

                 httReport.setReportCpt("RepMchCrackDetetionProcessShouDong.cpt");
             }else  if(httReport.getReportType().equals("12")){

                 httReport.setReportCpt("RepMchCrackDetetionProcessShouDong.cpt");
             }else  if(httReport.getReportType().equals("13")){

                 httReport.setReportCpt("RepMchCrackDetetionProcessShouDong.cpt");
             }else  if(httReport.getReportType().equals("14")){

                 httReport.setReportCpt("RepMchCrackDetetionProcessDianCi.cpt");
             }else  if(httReport.getReportType().equals("15")){

                 httReport.setReportCpt("RepMchCrackDetetionProcessShouTi.cpt");
             }else  if(httReport.getReportType().equals("16")){

                 httReport.setReportCpt("RepMchCrackDetetionProcessShouDong.cpt");
             }






             String insertBcSql = CoreXmlSqlParsersFactory.getSql("HttReport.addReportNo");
			    insertBcSql = EntityTool.fillSqlByEntity(insertBcSql, httReport);
			    sqllist.add(insertBcSql);
         }
         core = this.getDao(CoreConstantData.sysbeanstr).ExcuteTransction(sqllist);
         if(core.getV_errMsg().equals(""))
         {
            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 doUpdate(ArrayList<String> list) {
        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); // 取消自动提交事务
            ArrayList<String> sqllist = new ArrayList<String>();
         for(String str:list)   
         {
        	 HttReportEntity httEntity = EntityTool.getEntityByJSONStr(str, HttReportEntity.class);
        	 if(httEntity.getMachineNo().equals("")){
                 httEntity.setMachineNo("1");
             }
        	 HttReportEntity httReport = new HttReportEntity();
            // 修改编号信息
        	 httReport.setReportNo(httEntity.getReportNo());
        	 httReport.setPlineCode(httEntity.getPlineCode());
        	 httReport.setReportNoNew(httEntity.getReportNoNew());
        	 httReport.setReportNoTime(httEntity.getReportNoTime());
        	 httReport.setReportName(httEntity.getReportName()); 
        	 httReport.setColUser(httEntity.getColUser());
        	 httReport.setColTime(systime);
        	 httReport.setRemark(httEntity.getRemark());
        	 httReport.setGenerateStation(httEntity.getGenerateStation());
        	 httReport.setReceivingStation(httEntity.getReceivingStation());
        	 httReport.setSaveStation(httEntity.getSaveStation());
        	 httReport.setStorageLife(httEntity.getStorageLife());
        	 httReport.setIntermediary(httEntity.getIntermediary());
        	 httReport.setEnglishName(httEntity.getEnglishName());
        	 httReport.setReportType(httEntity.getReportType());
        	 httReport.setMachineNo(httEntity.getMachineNo());
        	 String insertBcSql = CoreXmlSqlParsersFactory.getSql("HttReport.updateReportNo");
			    insertBcSql = EntityTool.fillSqlByEntity(insertBcSql, httReport);
			    sqllist.add(insertBcSql);
         }
         core = this.getDao(CoreConstantData.sysbeanstr).ExcuteTransction(sqllist);
         if(core.getV_errMsg().equals(""))
         {
            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 doCancel(ArrayList<String> list) {
        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); // 取消自动提交事务
            ArrayList<String> sqllist = new ArrayList<String>();
         for(String str:list)   
         {
        	 HttReportEntity httEntity = EntityTool.getEntityByJSONStr(str, HttReportEntity.class);
        	 HttReportEntity httReport = new HttReportEntity();
            // 删除编号信息
        	 httReport.setPlineCode(httEntity.getPlineCode());
        	 httReport.setReportName(httEntity.getReportName());
           
        	 String insertBcSql = CoreXmlSqlParsersFactory.getSql("HttReport.cancelReportNo");
			    insertBcSql = EntityTool.fillSqlByEntity(insertBcSql, httReport);
			    sqllist.add(insertBcSql);
         }
         core = this.getDao(CoreConstantData.sysbeanstr).ExcuteTransction(sqllist);
         if(core.getV_errMsg().equals(""))
         {
            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;
    }

}
