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.HttSignatureEntity;

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 HttSignature extends DBCommon{
	/**
	 * 查询报表数据
	 * @param start
	 * @param end
	 * @param heatid
	 * @param judgeNo
	 * @return
	 */
    public CoreReturnObject doQuery(String[] plineCode) {

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

        sql += "  ORDER BY PLINE_CODE,TO_NUMBER(PROCESS_NO), COL_GROUP, USER_NAME";
        return this.getData(sql);
    }
    /*
     * 修改图片路径
     */
    public CoreReturnObject updateScoure(String fileName,String plineCode)
    {
    	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>();
            String updateMat = CoreXmlSqlParsersFactory
                    .getSql("HttSignature.updateScoure");// 修改MAT_BC_BACKUP状态
            PreparedStatement patsUpMat = connection
                    .prepareStatement(updateMat);
            // 修改库存状态
            patsUpMat.setObject(1, fileName);
            patsUpMat.setObject(2, plineCode);
            patsUpMat.addBatch();
            patsUpMat.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 start
	 * @param end
	 * @param heatid
	 * @param judgeNo
	 * @return
	 */
    public CoreReturnObject getSameData(String plineCode,String process,String username,String colgroup) {

        String sql = "";
        sql = CoreXmlSqlParsersFactory
                .getSql("HttSignature.getSameData");
        if(!plineCode.equals(""))
        {
        	sql += " AND PLINE_CODE = '"+plineCode+"'";
        }
        if(!process.equals(""))
        {
        	sql += " AND PROCESS_NO = '"+process+"'";
        }
        if(!username.equals(""))
        {
        	sql += " AND USER_NAME = '"+username+"'";
        }
        if(!colgroup.equals(""))
        {
        	sql += " AND COL_GROUP = '"+colgroup+"'";
        }
        return this.getData(sql);
    }
    
    /**
     * 新增
     */
    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)   
         {
        	 HttSignatureEntity httEntity = EntityTool.getEntityByJSONStr(str, HttSignatureEntity.class);
        	 HttSignatureEntity httReport = new HttSignatureEntity();
            // 新增编号信息
        	 httReport.setPlineCode(httEntity.getPlineCode());
        	 httReport.setColGroup(httEntity.getColGroup());
        	 httReport.setUserName(httEntity.getUserName());
        	 httReport.setSignRoute(httEntity.getSignRoute());
        	 httReport.setRemark(httEntity.getRemark());
        	 httReport.setProcessNo(httEntity.getProcessNo());
        	 httReport.setImage(httEntity.getImage());
        	 httReport.setUseridGrade(httEntity.getUseridGrade());
             httReport.setResponsible(httEntity.getResponsible());
        	 String insertBcSql = CoreXmlSqlParsersFactory.getSql("HttSignature.addReportNo");
			    insertBcSql = EntityTool.fillSqlByEntity(insertBcSql, httReport);
			    sqllist.add(insertBcSql);
         }
        	 
			    core = this.getDao(CoreConstantData.sysbeanstr).ExcuteTransction(sqllist);
               
         if(core.getV_errMsg().equals(""))
         {
            core.setV_errMsg("新增成功！");
         }
        }
        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 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)   
         {
        	 HttSignatureEntity httEntity = EntityTool.getEntityByJSONStr(str, HttSignatureEntity.class);
        	 HttSignatureEntity httReport = new HttSignatureEntity();
            // 修改电子签名
        	 httReport.setPlineCode(httEntity.getPlineCode());
        	 httReport.setColGroup(httEntity.getColGroup());
        	 httReport.setUserName(httEntity.getUserName());
        	 httReport.setSignRoute(httEntity.getSignRoute());
        	 httReport.setRemark(httEntity.getRemark());
        	 httReport.setProcessNo(httEntity.getProcessNo());
        	 httReport.setImage(httEntity.getImage());
        	 httReport.setUseridGrade(httEntity.getUseridGrade());
        	 httReport.setUserId(httEntity.getUserId());
             httReport.setResponsible(httEntity.getResponsible());
           
        	 String insertBcSql = CoreXmlSqlParsersFactory.getSql("HttSignature.updateReportNo");
			    insertBcSql = EntityTool.fillSqlByEntity(insertBcSql, httReport);
			    sqllist.add(insertBcSql);
         }
         core = this.getDao(CoreConstantData.sysbeanstr).ExcuteTransction(sqllist);
         if(core.getV_errMsg().equals(""))
         {
            core.setV_errMsg("修改成功！");
         }
        }
        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 CoreReturnObject doDelete(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)   
         {
        	 HttSignatureEntity httEntity = EntityTool.getEntityByJSONStr(str, HttSignatureEntity.class);
        	 HttSignatureEntity httReport = new HttSignatureEntity();
            // 删除电子签名
        	 httReport.setPlineCode(httEntity.getPlineCode());
        	 httReport.setProcessNo(httEntity.getProcessNo());
        	 httReport.setColGroup(httEntity.getColGroup());
             httReport.setUserId(httEntity.getUserId());

        	 String insertBcSql = CoreXmlSqlParsersFactory.getSql("HttSignature.deleteReportNo");
			    insertBcSql = EntityTool.fillSqlByEntity(insertBcSql, httReport);
			    sqllist.add(insertBcSql);
         }
         core = this.getDao(CoreConstantData.sysbeanstr).ExcuteTransction(sqllist);
         if(core.getV_errMsg().equals(""))
         {
            core.setV_errMsg("删除成功！");
         }
        }
        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;
    }
}
