package com.steering.mes.signature;

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

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

import com.steering.comm.converter.CoreConverter;
import com.steering.comm.db.DBCommon;
import com.steering.comm.tools.StringTool;
import com.steering.common.EntityTool;
import com.steering.mes.zgmil.entity.MilCraftworkSetEntity;

public class FrmTubTechnological extends DBCommon {

	/**
	 * 查询工艺参数
	 * @param arr
	 * @return
	 */
	public CoreReturnObject doQueryData(String[] arr)
	{
		String sql = CoreXmlSqlParsersFactory.getSql("FrmTubTechnological.doQueryData");
		sql += CoreConverter.toSqlConditionString("PLINE_CODE", arr);
		sql += " GROUP BY CLASS_NAME,PLINE_CODE,PLINE_NAME,STATION_CODE,STATION_DESC";
		sql += " ORDER BY CREATE_TIME";
		return this.getData(sql);
	}
	
	
	public CoreReturnObject doQuery(String className,String staionCode,String PlineCode)
	{
		return this.getDataBySqlID("FrmTubTechnological.doQuery", new Object[]{className,staionCode,PlineCode});
	}
	/***
	 * 查询产线
	 * @return
	 */
	public CoreReturnObject doQueryPline(String[] arr)
	{
		String sql = CoreXmlSqlParsersFactory.getSql("FrmTubTechnological.doQueryPline");
		sql += CoreConverter.toSqlConditionString("T.PLINE_CODE", arr);
		return this.getData(sql);
	}
	/**
	 * 查询工序点
	 * @return
	 */
	public CoreReturnObject doQueryStation()
	{
		return this.getDataBySqlID("FrmTubTechnological.doQueryStation");
	}
	/**
	 * 查询工艺项
	 * @return
	 */
	public CoreReturnObject doQueryFactor(String stationId)
	{
		return this.getDataBySqlID("FrmTubTechnological.doQueryFactor",new Object[]{stationId});
	}
	
	/***
	 * 维护工艺参数
	 * @param list
	 * @param parm
	 * @return
	 */
	public CoreReturnObject doAdd(ArrayList<String> list,ArrayList<String> parm)
	{
		CoreReturnObject cro = new CoreReturnObject();
        Connection connection = this.getDao(CoreConstantData.sysbeanstr)
                .getConnection();
        PreparedStatement psta = null;
        ResultSet rs = null;
        Date now = new Date();
        SimpleDateFormat dateFormat = new SimpleDateFormat(
        "yyyy-MM-dd HH:mm:ss");
        String systime = dateFormat.format(now);
        String classId = "";
        int m = 0;
        try {
			connection.setAutoCommit(false); // 取消自动提交事务
			String insertSql = CoreXmlSqlParsersFactory.getSql("FrmTubTechnological.doAdd");
			PreparedStatement insertSqlPsta = connection.prepareStatement(insertSql);
			classId = doClassId(connection);
			MilCraftworkSetEntity milCraTity = EntityTool.getEntityByJSONStr(list.get(0), MilCraftworkSetEntity.class);
			for(String str : parm)
			{
				MilCraftworkSetEntity milCraftEntity = EntityTool.getEntityByJSONStr(str, MilCraftworkSetEntity.class);
				insertSqlPsta.setString(1, 'B'+ StringTool.formatDecimal(((Integer.parseInt(classId.substring(1))+ Integer.parseInt(StringTool.formatDecimal(m, "00000")))),"00000"));
				insertSqlPsta.setString(2, milCraTity.getClassName());
				insertSqlPsta.setString(3, milCraftEntity.getFactorId());
				insertSqlPsta.setString(4, milCraftEntity.getFactorName());
				insertSqlPsta.setString(5, milCraftEntity.getFactorVal());
				insertSqlPsta.setString(6, milCraTity.getPlineCode());
				insertSqlPsta.setString(7, milCraTity.getPlineName());
				insertSqlPsta.setString(8, milCraTity.getStationCode());
				insertSqlPsta.setString(9, milCraTity.getStationDesc());
				insertSqlPsta.setString(10, milCraTity.getCreateName());
				insertSqlPsta.setString(11, systime);
				insertSqlPsta.addBatch();
				m++;
			}
			insertSqlPsta.executeBatch();
	        connection.commit();
	        cro.setV_errMsg("维护成功!");
        }
	   	catch (Exception e) {
            System.out.println(e.getMessage());
            if (connection != null) {
                try {
                	cro.setV_errMsg(e.getMessage());
                    connection.rollback(); // 回滚事务
                }
                catch (Exception e2) {
                }
            }
        }
        finally {
            try {
                if (connection != null && !connection.isClosed()) {
                    connection.close();
                }
            }
            catch (Exception e2) {
            }
        }
        return cro;
	}
	
	/**
	 * 生成版本ID(Bxxxxx)
	 * @param con
	 * @return
	 */
	public String doClassId(Connection con)
	{
		String classId = "";
		PreparedStatement psta = null;
		ResultSet rs = null;
		try
		{
			String sql = CoreXmlSqlParsersFactory
					.getSql("FrmTubTechnological.doClassId");
			psta = con.prepareStatement(sql);
			rs = psta.executeQuery();
			if (rs.next()) {
				classId = rs.getString("CLASS_ID");
			}
			rs.close();
			psta.close();
		}catch (Exception e) {
			System.out.println(e.getMessage());
		}
		return classId;
	}
	/**
	 * 修改工艺项
	 * @param parm
	 * @return
	 */
	public CoreReturnObject doUpdateData(ArrayList<String> parm)
	{
		CoreReturnObject cro = new CoreReturnObject();
        Connection connection = this.getDao(CoreConstantData.sysbeanstr)
                .getConnection();
        PreparedStatement psta = null;
        ResultSet rs = null;
        Date now = new Date();
        SimpleDateFormat dateFormat = new SimpleDateFormat(
        "yyyy-MM-dd HH:mm:ss");
        String systime = dateFormat.format(now);
        try {
			connection.setAutoCommit(false); // 取消自动提交事务
			String updateSql = CoreXmlSqlParsersFactory.getSql("FrmTubTechnological.doUpdateData");
			PreparedStatement updatePsta = connection.prepareStatement(updateSql);
			for(String str : parm)
			{
				MilCraftworkSetEntity milTity = EntityTool.getEntityByJSONStr(str, MilCraftworkSetEntity.class);
				updatePsta.setString(1, milTity.getFactorId());
				updatePsta.setString(2, milTity.getFactorName());
				updatePsta.setString(3, milTity.getFactorVal());
				updatePsta.setString(4,milTity.getUpdateName());
				updatePsta.setString(5,systime);
				updatePsta.setString(6, milTity.getClassId());
				updatePsta.addBatch();
			}
			updatePsta.executeBatch();
			connection.commit();
			cro.setV_errMsg("修改成功!");
        }
	   	catch (Exception e) {
            System.out.println(e.getMessage());
            if (connection != null) {
                try {
                	cro.setV_errMsg(e.getMessage());
                    connection.rollback(); // 回滚事务
                }
                catch (Exception e2) {
                }
            }
        }
        finally {
            try {
                if (connection != null && !connection.isClosed()) {
                    connection.close();
                }
            }
            catch (Exception e2) {
            }
        }
        return cro;
	}
	/***
	 * 查询工艺项类型
	 * @param fatorId
	 * @return
	 */
	public CoreReturnObject doQueryType(String fatorId)
	{
		return this.getDataBySqlID("FrmTubTechnological.doQueryType", new Object[]{fatorId});
	}
	/**
	 * 作废工艺项
	 * @param parm
	 * @return
	 */
	public CoreReturnObject doDeleteData(ArrayList<String> parm)
	{
		CoreReturnObject cro = new CoreReturnObject();
        Connection connection = this.getDao(CoreConstantData.sysbeanstr)
                .getConnection();
        PreparedStatement psta = null;
        ResultSet rs = null;
        Date now = new Date();
        SimpleDateFormat dateFormat = new SimpleDateFormat(
        "yyyy-MM-dd HH:mm:ss");
        String systime = dateFormat.format(now);
        try {
			connection.setAutoCommit(false); // 取消自动提交事务
			String deleteSql = CoreXmlSqlParsersFactory.getSql("FrmTubTechnological.doDeleteData");
			PreparedStatement deletePsta = connection.prepareStatement(deleteSql);
			for(String str : parm)
			{
				MilCraftworkSetEntity milTity = EntityTool.getEntityByJSONStr(str, MilCraftworkSetEntity.class);
				deletePsta.setString(1, milTity.getClassId());
				deletePsta.addBatch();
			}
			deletePsta.executeBatch();
			connection.commit();
			cro.setV_errMsg("作废成功!");
        }
	   	catch (Exception e) {
            System.out.println(e.getMessage());
            if (connection != null) {
                try {
                	cro.setV_errMsg(e.getMessage());
                    connection.rollback(); // 回滚事务
                }
                catch (Exception e2) {
                }
            }
        }
        finally {
            try {
                if (connection != null && !connection.isClosed()) {
                    connection.close();
                }
            }
            catch (Exception e2) {
            }
        }
        return cro;
	}
	/***
	 * 判断是否存在相同产线、工序、版本
	 * @param plineCode
	 * @param stationId
	 * @param className
	 * @return
	 */
	public CoreReturnObject getCraftSet(String plineCode,String stationId,String className)
	{
		return this.getDataBySqlID("FrmTubTechnological.getCraftSet", new Object[]{plineCode,stationId,className});
	}
}
